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
|
|
|
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2012-07-11 13:51:43 +00:00
|
|
|
#include "gtkprivatetypebuiltins.h"
|
|
|
|
#include "gtktexthandleprivate.h"
|
|
|
|
#include "gtkmarshalers.h"
|
|
|
|
#include "gtkprivate.h"
|
2014-01-22 15:33:47 +00:00
|
|
|
#include "gtkwindowprivate.h"
|
2015-11-10 04:33:54 +00:00
|
|
|
#include "gtkwidgetprivate.h"
|
2018-06-12 10:33:03 +00:00
|
|
|
#include "gtkrendericonprivate.h"
|
2020-05-16 06:01:22 +00:00
|
|
|
#include "gtkcssboxesimplprivate.h"
|
|
|
|
#include "gtkcssnumbervalueprivate.h"
|
2020-01-27 23:27:36 +00:00
|
|
|
#include "gtkstylecontextprivate.h"
|
2012-07-11 13:51:43 +00:00
|
|
|
#include "gtkintl.h"
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
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
|
|
|
GtkWidget *parent;
|
|
|
|
GdkSurface *surface;
|
|
|
|
GskRenderer *renderer;
|
|
|
|
|
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-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,
|
|
|
|
GtkBorder *border)
|
2018-06-12 10:33:03 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidget *widget = GTK_WIDGET (handle);
|
|
|
|
GtkStyleContext *context;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
context = gtk_widget_get_style_context (widget);
|
2013-12-23 18:09:10 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
border->left = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_PADDING_LEFT), 100);
|
|
|
|
border->right = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_PADDING_RIGHT), 100);
|
|
|
|
border->top = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_PADDING_TOP), 100);
|
|
|
|
border->bottom = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_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-05-17 21:08:01 +00:00
|
|
|
double x, y;
|
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-03-28 14:36:09 +00:00
|
|
|
rect.x = handle->pointing_to.x;
|
|
|
|
rect.y = handle->pointing_to.y + handle->pointing_to.height - handle->border.top;
|
|
|
|
rect.width = req.width - handle->border.left - handle->border.right;
|
2020-02-03 14:59:49 +00:00
|
|
|
rect.height = 1;
|
|
|
|
|
|
|
|
gtk_widget_translate_coordinates (gtk_widget_get_parent (widget),
|
|
|
|
gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW),
|
2020-05-17 21:08:01 +00:00
|
|
|
rect.x, rect.y, &x, &y);
|
|
|
|
rect.x = x;
|
|
|
|
rect.y = y;
|
2014-01-22 15:41:00 +00:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
gtk_widget_allocate (widget,
|
2020-03-28 14:36:09 +00:00
|
|
|
gdk_surface_get_width (handle->surface),
|
|
|
|
gdk_surface_get_height (handle->surface),
|
2020-02-03 14:59:49 +00:00
|
|
|
-1, NULL);
|
2014-01-22 15:41:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 13:23:04 +00:00
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_native_check_resize (GtkNative *native)
|
2012-11-07 13:23:04 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle = GTK_TEXT_HANDLE (native);
|
|
|
|
GtkWidget *widget = GTK_WIDGET (native);
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
iface->check_resize = gtk_text_handle_native_check_resize;
|
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
|
|
|
}
|
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static gboolean
|
|
|
|
surface_event (GdkSurface *surface,
|
|
|
|
GdkEvent *event,
|
|
|
|
GtkTextHandle *handle)
|
2014-01-22 15:39:10 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_main_do_event (event);
|
|
|
|
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));
|
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-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);
|
|
|
|
g_signal_connect (handle->surface, "event", G_CALLBACK (surface_event), 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);
|
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-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_event, 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
|
|
|
}
|
|
|
|
|
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-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_present_surface (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);
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
gtk_text_handle_measure (GtkWidget *widget,
|
|
|
|
GtkOrientation orientation,
|
|
|
|
int for_size,
|
|
|
|
int *minimum,
|
|
|
|
int *natural,
|
|
|
|
int *minimum_baseline,
|
|
|
|
int *natural_baseline)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-05-16 06:12:50 +00:00
|
|
|
*natural = 0;
|
|
|
|
*minimum = 0;
|
2020-02-03 14:59:49 +00:00
|
|
|
*minimum_baseline = -1;
|
|
|
|
*natural_baseline = -1;
|
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);
|
|
|
|
|
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;
|
|
|
|
widget_class->measure = gtk_text_handle_measure;
|
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"));
|
2012-07-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-03 14:59:49 +00:00
|
|
|
handle_drag_begin (GtkGestureDrag *gesture,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkTextHandle *handle)
|
2012-07-11 13:51:43 +00:00
|
|
|
{
|
2020-02-03 14:59:49 +00:00
|
|
|
GtkWidget *widget;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
|
2020-02-03 14:59:49 +00:00
|
|
|
x -= gtk_widget_get_width (widget) / 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))
|
|
|
|
x -= gtk_widget_get_width (widget);
|
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
y += handle->border.top / 2;
|
2020-02-03 14:59:49 +00:00
|
|
|
|
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-03-28 14:36:09 +00:00
|
|
|
x = handle->pointing_to.x + handle->pointing_to.width / 2 +
|
|
|
|
start_x + offset_x - handle->dx;
|
|
|
|
y = handle->pointing_to.y + handle->pointing_to.height +
|
|
|
|
start_y + offset_y - handle->dy;
|
2012-07-11 13:51:43 +00:00
|
|
|
|
2020-03-28 14:36:09 +00:00
|
|
|
if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
|
2020-02-03 14:59:49 +00:00
|
|
|
x -= gtk_widget_get_width (widget) / 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))
|
|
|
|
x -= gtk_widget_get_width (widget);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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-02-03 14:59:49 +00:00
|
|
|
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_TOP);
|
|
|
|
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_BOTTOM);
|
|
|
|
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_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-02-03 14:59:49 +00:00
|
|
|
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_TOP);
|
|
|
|
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_BOTTOM);
|
|
|
|
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_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
|
|
|
{
|
|
|
|
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_TOP);
|
|
|
|
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_BOTTOM);
|
|
|
|
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_INSERTION_CURSOR);
|
|
|
|
}
|
|
|
|
}
|
2015-11-10 04:33:54 +00:00
|
|
|
|
2020-02-03 14:59:49 +00:00
|
|
|
static void
|
|
|
|
gtk_text_handle_init (GtkTextHandle *widget)
|
|
|
|
{
|
|
|
|
GtkEventController *controller;
|
|
|
|
|
|
|
|
controller = GTK_EVENT_CONTROLLER (gtk_gesture_drag_new ());
|
|
|
|
g_signal_connect (controller, "drag-begin",
|
|
|
|
G_CALLBACK (handle_drag_begin), widget);
|
|
|
|
g_signal_connect (controller, "drag-update",
|
|
|
|
G_CALLBACK (handle_drag_update), widget);
|
|
|
|
g_signal_connect (controller, "drag-end",
|
|
|
|
G_CALLBACK (handle_drag_end), widget);
|
|
|
|
gtk_widget_add_controller (GTK_WIDGET (widget), controller);
|
|
|
|
|
|
|
|
gtk_text_handle_update_for_role (GTK_TEXT_HANDLE (widget));
|
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
|
|
|
}
|