2012-07-11 13:51:43 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright © 2012 Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2020-01-27 23:27:36 +00:00
|
|
|
|
2012-07-11 13:51:43 +00:00
|
|
|
#include "gtktexthandleprivate.h"
|
2021-10-03 06:20:30 +00:00
|
|
|
|
|
|
|
#include "gtkbinlayout.h"
|
2020-05-16 06:01:22 +00:00
|
|
|
#include "gtkcssboxesimplprivate.h"
|
2021-01-28 04:07:55 +00:00
|
|
|
#include "gtkcssnodeprivate.h"
|
2021-10-03 06:20:30 +00:00
|
|
|
#include "gtkgesturedrag.h"
|
|
|
|
#include "gtkgizmoprivate.h"
|
2012-07-11 13:51:43 +00:00
|
|
|
#include "gtkintl.h"
|
2021-10-03 06:20:30 +00:00
|
|
|
#include "gtkmarshalers.h"
|
|
|
|
#include "gtknativeprivate.h"
|
|
|
|
#include "gtkprivatetypebuiltins.h"
|
|
|
|
#include "gtkrendericonprivate.h"
|
|
|
|
#include "gtkwidgetprivate.h"
|
|
|
|
#include "gtkwindowprivate.h"
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2021-10-03 06:20:30 +00:00
|
|
|
#include "gdk/gdksurfaceprivate.h"
|
2012-07-11 13:51:43 +00:00
|
|
|
|
|
|
|
enum {
|
2015-06-07 16:45:34 +00:00
|
|
|
DRAG_STARTED,
|
2012-07-11 13:51:43 +00:00
|
|
|
HANDLE_DRAGGED,
|
|
|
|
DRAG_FINISHED,
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
struct _GtkTextHandle
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-03-28 14:36:09 +00:00
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GdkSurface *surface;
|
|
|
|
GskRenderer *renderer;
|
2020-11-05 14:11:09 +00:00
|
|
|
GtkEventController *controller;
|
|
|
|
GtkWidget *controller_widget;
|
2021-05-04 18:53:05 +00:00
|
|
|
GtkWidget *contents;
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2012-07-11 13:51:43 +00:00
|
|
|
GdkRectangle pointing_to;
|
2015-06-10 01:19:17 +00:00
|
|
|
GtkBorder border;
|
2020-07-24 13:54:49 +00:00
|
|
|
int dx;
|
|
|
|
int dy;
|
2020-02-03 14:59:49 +00:00
|
|
|
guint role : 2;
|
2012-07-11 13:51:43 +00:00
|
|
|
guint dragged : 1;
|
2012-11-07 13:23:04 +00:00
|
|
|
guint mode_visible : 1;
|
|
|
|
guint user_visible : 1;
|
|
|
|
guint has_point : 1;
|
2012-07-11 13:51:43 +00:00
|
|
|
};
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void gtk_text_handle_native_interface_init (GtkNativeInterface *iface);
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
static void handle_drag_begin (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkTextHandle *handle);
|
|
|
|
static void handle_drag_update (GtkGestureDrag *gesture,
|
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
|
|
|
GtkWidget *widget);
|
|
|
|
static void handle_drag_end (GtkGestureDrag *gesture,
|
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
|
|
|
GtkTextHandle *handle);
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkTextHandle, gtk_text_handle, GTK_TYPE_WIDGET,
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_NATIVE,
|
|
|
|
gtk_text_handle_native_interface_init))
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static guint signals[LAST_SIGNAL] = { 0 };
|
2013-12-23 18:09:10 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static GdkSurface *
|
|
|
|
gtk_text_handle_native_get_surface (GtkNative *native)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-03-28 14:36:09 +00:00
|
|
|
return GTK_TEXT_HANDLE (native)->surface;
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static GskRenderer *
|
|
|
|
gtk_text_handle_native_get_renderer (GtkNative *native)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-03-28 14:36:09 +00:00
|
|
|
return GTK_TEXT_HANDLE (native)->renderer;
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 10:33:03 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_native_get_surface_transform (GtkNative *native,
|
2020-05-17 21:08:01 +00:00
|
|
|
double *x,
|
|
|
|
double *y)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-05-16 06:01:22 +00:00
|
|
|
GtkCssBoxes css_boxes;
|
|
|
|
const graphene_rect_t *margin_rect;
|
|
|
|
|
|
|
|
gtk_css_boxes_init (&css_boxes, GTK_WIDGET (native));
|
|
|
|
margin_rect = gtk_css_boxes_get_margin_rect (&css_boxes);
|
|
|
|
|
|
|
|
*x = - margin_rect->origin.x;
|
|
|
|
*y = - margin_rect->origin.y;
|
2018-06-12 10:33:03 +00:00
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2019-01-18 07:01:40 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_get_padding (GtkTextHandle *handle,
|
2021-01-28 04:07:55 +00:00
|
|
|
GtkBorder *padding)
|
2018-06-12 10:33:03 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
2021-01-28 04:07:55 +00:00
|
|
|
GtkCssStyle *style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
|
2013-12-23 18:09:10 +00:00
|
|
|
|
2021-01-28 04:07:55 +00:00
|
|
|
padding->left = _gtk_css_number_value_get (style->size->padding_left, 100);
|
|
|
|
padding->right = _gtk_css_number_value_get (style->size->padding_right, 100);
|
|
|
|
padding->top = _gtk_css_number_value_get (style->size->padding_top, 100);
|
|
|
|
padding->bottom = _gtk_css_number_value_get (style->size->padding_bottom, 100);
|
2012-11-07 13:23:04 +00:00
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2014-01-22 15:41:00 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_present_surface (GtkTextHandle *handle)
|
2014-01-22 15:41:00 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
|
|
|
GdkPopupLayout *layout;
|
|
|
|
GdkRectangle rect;
|
|
|
|
GtkRequisition req;
|
2020-11-03 17:37:07 +00:00
|
|
|
GtkWidget *parent;
|
2020-02-03 14:59:49 +00:00
|
|
|
|
|
|
|
gtk_widget_get_preferred_size (widget, NULL, &req);
|
2020-03-28 14:36:09 +00:00
|
|
|
gtk_text_handle_get_padding (handle, &handle->border);
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2020-11-03 17:37:07 +00:00
|
|
|
parent = gtk_widget_get_parent (widget);
|
|
|
|
gtk_widget_get_surface_allocation (parent, &rect);
|
|
|
|
|
|
|
|
rect.x += handle->pointing_to.x;
|
|
|
|
rect.y += handle->pointing_to.y + handle->pointing_to.height - handle->border.top;
|
2020-03-28 14:36:09 +00:00
|
|
|
rect.width = req.width - handle->border.left - handle->border.right;
|
2020-02-03 14:59:49 +00:00
|
|
|
rect.height = 1;
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
|
2020-02-03 14:59:49 +00:00
|
|
|
rect.x -= rect.width / 2;
|
2020-03-28 14:36:09 +00:00
|
|
|
else if ((handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END &&
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ||
|
2020-03-28 14:36:09 +00:00
|
|
|
(handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START &&
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL))
|
|
|
|
rect.x -= rect.width;
|
|
|
|
|
|
|
|
layout = gdk_popup_layout_new (&rect,
|
|
|
|
GDK_GRAVITY_SOUTH,
|
|
|
|
GDK_GRAVITY_NORTH);
|
|
|
|
gdk_popup_layout_set_anchor_hints (layout,
|
|
|
|
GDK_ANCHOR_FLIP_Y | GDK_ANCHOR_SLIDE_X);
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
gdk_popup_present (GDK_POPUP (handle->surface),
|
2020-02-03 14:59:49 +00:00
|
|
|
MAX (req.width, 1),
|
|
|
|
MAX (req.height, 1),
|
|
|
|
layout);
|
|
|
|
gdk_popup_layout_unref (layout);
|
2014-01-22 15:41:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 09:53:16 +00:00
|
|
|
void
|
|
|
|
gtk_text_handle_present (GtkTextHandle *handle)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
if (!_gtk_widget_get_alloc_needed (widget))
|
|
|
|
gtk_widget_ensure_allocate (widget);
|
|
|
|
else if (gtk_widget_get_visible (widget))
|
|
|
|
gtk_text_handle_present_surface (handle);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:59:23 +00:00
|
|
|
static void
|
|
|
|
gtk_text_handle_native_layout (GtkNative *native,
|
|
|
|
int width,
|
|
|
|
int height)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (native);
|
|
|
|
|
|
|
|
if (_gtk_widget_get_alloc_needed (widget))
|
|
|
|
gtk_widget_allocate (widget, width, height, -1, NULL);
|
|
|
|
else
|
|
|
|
gtk_widget_ensure_allocate (widget);
|
|
|
|
}
|
|
|
|
|
2014-01-22 15:39:10 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_native_interface_init (GtkNativeInterface *iface)
|
2014-01-22 15:39:10 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
iface->get_surface = gtk_text_handle_native_get_surface;
|
|
|
|
iface->get_renderer = gtk_text_handle_native_get_renderer;
|
|
|
|
iface->get_surface_transform = gtk_text_handle_native_get_surface_transform;
|
2020-12-02 10:59:23 +00:00
|
|
|
iface->layout = gtk_text_handle_native_layout;
|
2014-01-22 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static gboolean
|
|
|
|
surface_render (GdkSurface *surface,
|
|
|
|
cairo_region_t *region,
|
|
|
|
GtkTextHandle *handle)
|
2014-01-22 15:39:10 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_widget_render (GTK_WIDGET (handle), surface, region);
|
|
|
|
return TRUE;
|
2014-01-22 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
surface_mapped_changed (GtkWidget *widget)
|
2014-01-22 15:39:10 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
2014-01-22 15:39:10 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
gtk_widget_set_visible (widget, gdk_surface_get_mapped (handle->surface));
|
2014-01-22 15:39:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void
|
|
|
|
gtk_text_handle_snapshot (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
2016-02-24 16:19:48 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkCssStyle *style = gtk_css_node_get_style (gtk_widget_get_css_node (widget));
|
2016-02-24 16:19:48 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_css_style_snapshot_icon (style,
|
|
|
|
snapshot,
|
|
|
|
gtk_widget_get_width (widget),
|
|
|
|
gtk_widget_get_height (widget));
|
2021-05-04 18:53:05 +00:00
|
|
|
|
|
|
|
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->snapshot (widget, snapshot);
|
2016-02-24 16:19:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-22 15:33:47 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_realize (GtkWidget *widget)
|
2014-01-22 15:33:47 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
|
|
|
GdkSurface *parent_surface;
|
|
|
|
GtkWidget *parent;
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
parent = gtk_widget_get_parent (widget);
|
|
|
|
parent_surface = gtk_native_get_surface (gtk_widget_get_native (parent));
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->surface = gdk_surface_new_popup (parent_surface, FALSE);
|
|
|
|
gdk_surface_set_widget (handle->surface, widget);
|
2020-11-05 14:11:09 +00:00
|
|
|
gdk_surface_set_input_region (handle->surface, cairo_region_create ());
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
g_signal_connect_swapped (handle->surface, "notify::mapped",
|
2020-02-03 14:59:49 +00:00
|
|
|
G_CALLBACK (surface_mapped_changed), widget);
|
2020-03-28 14:36:09 +00:00
|
|
|
g_signal_connect (handle->surface, "render", G_CALLBACK (surface_render), widget);
|
2014-01-22 15:39:10 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->realize (widget);
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->renderer = gsk_renderer_new_for_surface (handle->surface);
|
2020-12-02 10:59:23 +00:00
|
|
|
|
|
|
|
gtk_native_realize (GTK_NATIVE (handle));
|
2014-01-22 15:33:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_unrealize (GtkWidget *widget)
|
2014-01-22 15:33:47 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-12-02 10:59:23 +00:00
|
|
|
gtk_native_unrealize (GTK_NATIVE (handle));
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unrealize (widget);
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
gsk_renderer_unrealize (handle->renderer);
|
|
|
|
g_clear_object (&handle->renderer);
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (handle->surface, surface_render, widget);
|
|
|
|
g_signal_handlers_disconnect_by_func (handle->surface, surface_mapped_changed, widget);
|
2014-01-22 15:39:10 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
gdk_surface_set_widget (handle->surface, NULL);
|
|
|
|
gdk_surface_destroy (handle->surface);
|
|
|
|
g_clear_object (&handle->surface);
|
2014-01-22 15:33:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
static void
|
|
|
|
text_handle_set_up_gesture (GtkTextHandle *handle)
|
|
|
|
{
|
|
|
|
GtkNative *native;
|
|
|
|
|
|
|
|
/* The drag gesture is hooked on the parent native */
|
|
|
|
native = gtk_widget_get_native (gtk_widget_get_parent (GTK_WIDGET (handle)));
|
|
|
|
handle->controller_widget = GTK_WIDGET (native);
|
|
|
|
|
|
|
|
handle->controller = GTK_EVENT_CONTROLLER (gtk_gesture_drag_new ());
|
|
|
|
gtk_event_controller_set_propagation_phase (handle->controller,
|
|
|
|
GTK_PHASE_CAPTURE);
|
|
|
|
g_signal_connect (handle->controller, "drag-begin",
|
|
|
|
G_CALLBACK (handle_drag_begin), handle);
|
|
|
|
g_signal_connect (handle->controller, "drag-update",
|
|
|
|
G_CALLBACK (handle_drag_update), handle);
|
|
|
|
g_signal_connect (handle->controller, "drag-end",
|
|
|
|
G_CALLBACK (handle_drag_end), handle);
|
|
|
|
|
|
|
|
gtk_widget_add_controller (handle->controller_widget, handle->controller);
|
|
|
|
}
|
|
|
|
|
2012-07-11 13:51:43 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_map (GtkWidget *widget)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
2014-01-22 15:33:47 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->map (widget);
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->has_point)
|
2020-11-05 14:11:09 +00:00
|
|
|
{
|
|
|
|
gtk_text_handle_present_surface (handle);
|
|
|
|
text_handle_set_up_gesture (handle);
|
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_unmap (GtkWidget *widget)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unmap (widget);
|
2020-03-28 14:36:09 +00:00
|
|
|
gdk_surface_hide (handle->surface);
|
2020-11-05 14:11:09 +00:00
|
|
|
|
|
|
|
if (handle->controller_widget)
|
|
|
|
{
|
|
|
|
gtk_widget_remove_controller (handle->controller_widget,
|
|
|
|
handle->controller);
|
|
|
|
handle->controller_widget = NULL;
|
|
|
|
handle->controller = NULL;
|
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-05-04 18:53:05 +00:00
|
|
|
gtk_text_handle_dispose (GObject *object)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2021-05-04 18:53:05 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (object);
|
|
|
|
|
|
|
|
g_clear_pointer (&handle->contents, gtk_widget_unparent);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_text_handle_parent_class)->dispose (object);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_class_init (GtkTextHandleClass *klass)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
2012-07-11 13:51:43 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2021-05-04 18:53:05 +00:00
|
|
|
object_class->dispose = gtk_text_handle_dispose;
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
widget_class->snapshot = gtk_text_handle_snapshot;
|
|
|
|
widget_class->realize = gtk_text_handle_realize;
|
|
|
|
widget_class->unrealize = gtk_text_handle_unrealize;
|
|
|
|
widget_class->map = gtk_text_handle_map;
|
|
|
|
widget_class->unmap = gtk_text_handle_unmap;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
|
|
|
signals[HANDLE_DRAGGED] =
|
|
|
|
g_signal_new (I_("handle-dragged"),
|
|
|
|
G_OBJECT_CLASS_TYPE (object_class),
|
2020-02-03 14:59:49 +00:00
|
|
|
G_SIGNAL_RUN_LAST, 0,
|
2012-07-11 13:51:43 +00:00
|
|
|
NULL, NULL,
|
2020-02-03 14:59:49 +00:00
|
|
|
_gtk_marshal_VOID__INT_INT,
|
|
|
|
G_TYPE_NONE, 2,
|
2012-07-11 13:51:43 +00:00
|
|
|
G_TYPE_INT, G_TYPE_INT);
|
2015-06-07 16:45:34 +00:00
|
|
|
signals[DRAG_STARTED] =
|
|
|
|
g_signal_new (I_("drag-started"),
|
|
|
|
G_OBJECT_CLASS_TYPE (object_class),
|
|
|
|
G_SIGNAL_RUN_LAST, 0,
|
|
|
|
NULL, NULL,
|
2019-05-31 03:56:50 +00:00
|
|
|
NULL,
|
2020-02-03 14:59:49 +00:00
|
|
|
G_TYPE_NONE, 0, G_TYPE_NONE);
|
2012-07-11 13:51:43 +00:00
|
|
|
signals[DRAG_FINISHED] =
|
|
|
|
g_signal_new (I_("drag-finished"),
|
|
|
|
G_OBJECT_CLASS_TYPE (object_class),
|
|
|
|
G_SIGNAL_RUN_LAST, 0,
|
|
|
|
NULL, NULL,
|
2019-05-31 03:56:50 +00:00
|
|
|
NULL,
|
2020-02-03 14:59:49 +00:00
|
|
|
G_TYPE_NONE, 0, G_TYPE_NONE);
|
2020-04-25 00:56:38 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_css_name (widget_class, I_("cursor-handle"));
|
2021-05-04 18:53:05 +00:00
|
|
|
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
/* Relative to pointing_to x/y */
|
2012-07-11 13:51:43 +00:00
|
|
|
static void
|
2020-11-05 14:11:09 +00:00
|
|
|
handle_get_input_extents (GtkTextHandle *handle,
|
|
|
|
GtkBorder *border)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-11-05 14:11:09 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
|
2020-11-05 14:11:09 +00:00
|
|
|
{
|
|
|
|
border->left = (-gtk_widget_get_width (widget) / 2) - handle->border.left;
|
|
|
|
border->right = (gtk_widget_get_width (widget) / 2) + handle->border.right;
|
|
|
|
}
|
2020-03-28 14:36:09 +00:00
|
|
|
else if ((handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END &&
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ||
|
2020-03-28 14:36:09 +00:00
|
|
|
(handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START &&
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL))
|
2020-11-05 14:11:09 +00:00
|
|
|
{
|
|
|
|
border->left = -gtk_widget_get_width (widget) - handle->border.left;
|
|
|
|
border->right = handle->border.right;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
border->left = -handle->border.left;
|
|
|
|
border->right = gtk_widget_get_width (widget) + handle->border.right;
|
|
|
|
}
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
border->top = - handle->border.top;
|
|
|
|
border->bottom = gtk_widget_get_height (widget) + handle->border.bottom;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_drag_begin (GtkGestureDrag *gesture,
|
|
|
|
double x,
|
|
|
|
double y,
|
|
|
|
GtkTextHandle *handle)
|
|
|
|
{
|
|
|
|
GtkBorder input_extents;
|
|
|
|
double widget_x, widget_y;
|
|
|
|
|
|
|
|
x -= handle->pointing_to.x;
|
|
|
|
y -= handle->pointing_to.y;
|
|
|
|
|
|
|
|
/* Figure out if the coordinates fall into the handle input area, coordinates
|
|
|
|
* are relative to the parent widget.
|
|
|
|
*/
|
|
|
|
handle_get_input_extents (handle, &input_extents);
|
|
|
|
gtk_widget_translate_coordinates (handle->controller_widget,
|
|
|
|
gtk_widget_get_parent (GTK_WIDGET (handle)),
|
|
|
|
x, y, &widget_x, &widget_y);
|
|
|
|
|
|
|
|
if (widget_x < input_extents.left || widget_x >= input_extents.right ||
|
|
|
|
widget_y < input_extents.top || widget_y >= input_extents.bottom)
|
|
|
|
{
|
|
|
|
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-03 14:59:49 +00:00
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
|
|
|
|
/* Store untranslated coordinates here, so ::update does not need
|
|
|
|
* an extra translation
|
|
|
|
*/
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->dx = x;
|
|
|
|
handle->dy = y;
|
|
|
|
handle->dragged = TRUE;
|
2020-02-03 14:59:49 +00:00
|
|
|
g_signal_emit (handle, signals[DRAG_STARTED], 0);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void
|
|
|
|
handle_drag_update (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidget *widget)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-03-28 14:36:09 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
|
2020-07-24 20:32:16 +00:00
|
|
|
double start_x, start_y;
|
2020-07-24 13:54:49 +00:00
|
|
|
int x, y;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_gesture_drag_get_start_point (gesture, &start_x, &start_y);
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
x = start_x + offset_x - handle->dx;
|
2020-11-06 13:28:46 +00:00
|
|
|
y = start_y + offset_y - handle->dy + (handle->pointing_to.height / 2);
|
2020-02-03 14:59:49 +00:00
|
|
|
g_signal_emit (widget, signals[HANDLE_DRAGGED], 0, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_drag_end (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double offset_x,
|
|
|
|
double offset_y,
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle)
|
|
|
|
{
|
2020-11-05 14:11:09 +00:00
|
|
|
GdkEventSequence *sequence;
|
|
|
|
GtkEventSequenceState state;
|
|
|
|
|
|
|
|
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
|
|
|
|
state = gtk_gesture_get_sequence_state (GTK_GESTURE (gesture), sequence);
|
|
|
|
|
|
|
|
if (state == GTK_EVENT_SEQUENCE_CLAIMED)
|
|
|
|
g_signal_emit (handle, signals[DRAG_FINISHED], 0);
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->dragged = FALSE;
|
2020-02-03 14:59:49 +00:00
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void
|
|
|
|
gtk_text_handle_update_for_role (GtkTextHandle *handle)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
2012-11-07 13:23:04 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-08-13 23:49:02 +00:00
|
|
|
gtk_widget_remove_css_class (widget, "top");
|
|
|
|
gtk_widget_add_css_class (widget, "bottom");
|
|
|
|
gtk_widget_add_css_class (widget, "insertion-cursor");
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
2020-03-28 14:36:09 +00:00
|
|
|
else if (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END)
|
2015-11-10 04:33:54 +00:00
|
|
|
{
|
2020-08-13 23:49:02 +00:00
|
|
|
gtk_widget_remove_css_class (widget, "top");
|
|
|
|
gtk_widget_add_css_class (widget, "bottom");
|
|
|
|
gtk_widget_remove_css_class (widget, "insertion-cursor");
|
2015-11-10 04:33:54 +00:00
|
|
|
}
|
2020-03-28 14:36:09 +00:00
|
|
|
else if (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START)
|
2020-02-03 14:59:49 +00:00
|
|
|
{
|
2020-08-13 23:49:02 +00:00
|
|
|
gtk_widget_add_css_class (widget, "top");
|
|
|
|
gtk_widget_remove_css_class (widget, "bottom");
|
|
|
|
gtk_widget_remove_css_class (widget, "insertion-cursor");
|
2020-02-03 14:59:49 +00:00
|
|
|
}
|
2020-11-03 19:39:29 +00:00
|
|
|
|
|
|
|
gtk_widget_queue_draw (widget);
|
2020-02-03 14:59:49 +00:00
|
|
|
}
|
2015-11-10 04:33:54 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void
|
2020-11-05 14:11:09 +00:00
|
|
|
gtk_text_handle_init (GtkTextHandle *handle)
|
2020-02-03 14:59:49 +00:00
|
|
|
{
|
2021-05-04 18:53:05 +00:00
|
|
|
handle->contents = gtk_gizmo_new ("contents", NULL, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
gtk_widget_set_can_target (handle->contents, FALSE);
|
|
|
|
gtk_widget_set_parent (handle->contents, GTK_WIDGET (handle));
|
|
|
|
|
2020-11-05 14:11:09 +00:00
|
|
|
gtk_text_handle_update_for_role (handle);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *
|
|
|
|
gtk_text_handle_new (GtkWidget *parent)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-04-24 22:09:41 +00:00
|
|
|
GtkTextHandle *handle;
|
|
|
|
|
2020-04-25 00:56:38 +00:00
|
|
|
handle = g_object_new (GTK_TYPE_TEXT_HANDLE, NULL);
|
2020-04-24 22:09:41 +00:00
|
|
|
gtk_widget_set_parent (GTK_WIDGET (handle), parent);
|
|
|
|
|
|
|
|
return handle;
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_set_role (GtkTextHandle *handle,
|
|
|
|
GtkTextHandleRole role)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == role)
|
2012-07-11 13:51:43 +00:00
|
|
|
return;
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->role = role;
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_update_for_role (handle);
|
2013-01-11 16:22:02 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
if (gtk_widget_get_visible (GTK_WIDGET (handle)))
|
|
|
|
{
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->has_point)
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_present_surface (handle);
|
|
|
|
}
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandleRole
|
|
|
|
gtk_text_handle_get_role (GtkTextHandle *handle)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), GTK_TEXT_HANDLE_ROLE_CURSOR);
|
2013-12-23 18:09:10 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
return handle->role;
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
void
|
|
|
|
gtk_text_handle_set_position (GtkTextHandle *handle,
|
|
|
|
const GdkRectangle *rect)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->pointing_to.x == rect->x &&
|
|
|
|
handle->pointing_to.y == rect->y &&
|
|
|
|
handle->pointing_to.width == rect->width &&
|
|
|
|
handle->pointing_to.height == rect->height)
|
2020-02-03 14:59:49 +00:00
|
|
|
return;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
handle->pointing_to = *rect;
|
|
|
|
handle->has_point = TRUE;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
if (gtk_widget_is_visible (GTK_WIDGET (handle)))
|
|
|
|
gtk_text_handle_present_surface (handle);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
2015-06-10 18:52:02 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
gboolean
|
|
|
|
gtk_text_handle_get_is_dragged (GtkTextHandle *handle)
|
2015-06-10 18:52:02 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), FALSE);
|
2015-06-10 18:52:02 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
return handle->dragged;
|
2015-06-10 18:52:02 +00:00
|
|
|
}
|