texthandle: Drop the Private struct

This commit is contained in:
Matthias Clasen 2020-03-28 10:36:09 -04:00
parent 28eb1fce04
commit 9287e0e7fb

View File

@ -31,8 +31,6 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
typedef struct _GtkTextHandlePrivate GtkTextHandlePrivate;
enum { enum {
DRAG_STARTED, DRAG_STARTED,
HANDLE_DRAGGED, HANDLE_DRAGGED,
@ -40,8 +38,10 @@ enum {
LAST_SIGNAL LAST_SIGNAL
}; };
struct _GtkTextHandlePrivate struct _GtkTextHandle
{ {
GtkWidget parent_instance;
GtkWidget *parent; GtkWidget *parent;
GdkSurface *surface; GdkSurface *surface;
GskRenderer *renderer; GskRenderer *renderer;
@ -57,15 +57,9 @@ struct _GtkTextHandlePrivate
guint has_point : 1; guint has_point : 1;
}; };
struct _GtkTextHandle
{
GtkWidget parent_instance;
};
static void gtk_text_handle_native_interface_init (GtkNativeInterface *iface); static void gtk_text_handle_native_interface_init (GtkNativeInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkTextHandle, gtk_text_handle, GTK_TYPE_WIDGET, G_DEFINE_TYPE_WITH_CODE (GtkTextHandle, gtk_text_handle, GTK_TYPE_WIDGET,
G_ADD_PRIVATE (GtkTextHandle)
G_IMPLEMENT_INTERFACE (GTK_TYPE_NATIVE, G_IMPLEMENT_INTERFACE (GTK_TYPE_NATIVE,
gtk_text_handle_native_interface_init)) gtk_text_handle_native_interface_init))
@ -74,19 +68,13 @@ static guint signals[LAST_SIGNAL] = { 0 };
static GdkSurface * static GdkSurface *
gtk_text_handle_native_get_surface (GtkNative *native) gtk_text_handle_native_get_surface (GtkNative *native)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (native); return GTK_TEXT_HANDLE (native)->surface;
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
return priv->surface;
} }
static GskRenderer * static GskRenderer *
gtk_text_handle_native_get_renderer (GtkNative *native) gtk_text_handle_native_get_renderer (GtkNative *native)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (native); return GTK_TEXT_HANDLE (native)->renderer;
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
return priv->renderer;
} }
static void static void
@ -139,29 +127,28 @@ gtk_text_handle_get_padding (GtkTextHandle *handle,
static void static void
gtk_text_handle_present_surface (GtkTextHandle *handle) gtk_text_handle_present_surface (GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GtkWidget *widget = GTK_WIDGET (handle); GtkWidget *widget = GTK_WIDGET (handle);
GdkPopupLayout *layout; GdkPopupLayout *layout;
GdkRectangle rect; GdkRectangle rect;
GtkRequisition req; GtkRequisition req;
gtk_widget_get_preferred_size (widget, NULL, &req); gtk_widget_get_preferred_size (widget, NULL, &req);
gtk_text_handle_get_padding (handle, &priv->border); gtk_text_handle_get_padding (handle, &handle->border);
rect.x = priv->pointing_to.x; rect.x = handle->pointing_to.x;
rect.y = priv->pointing_to.y + priv->pointing_to.height - priv->border.top; rect.y = handle->pointing_to.y + handle->pointing_to.height - handle->border.top;
rect.width = req.width - priv->border.left - priv->border.right; rect.width = req.width - handle->border.left - handle->border.right;
rect.height = 1; rect.height = 1;
gtk_widget_translate_coordinates (gtk_widget_get_parent (widget), gtk_widget_translate_coordinates (gtk_widget_get_parent (widget),
gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW), gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW),
rect.x, rect.y, &rect.x, &rect.y); rect.x, rect.y, &rect.x, &rect.y);
if (priv->role == GTK_TEXT_HANDLE_ROLE_CURSOR) if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
rect.x -= rect.width / 2; rect.x -= rect.width / 2;
else if ((priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END && else if ((handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END &&
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) || gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ||
(priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START && (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START &&
gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL)) gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL))
rect.x -= rect.width; rect.x -= rect.width;
@ -171,15 +158,15 @@ gtk_text_handle_present_surface (GtkTextHandle *handle)
gdk_popup_layout_set_anchor_hints (layout, gdk_popup_layout_set_anchor_hints (layout,
GDK_ANCHOR_FLIP_Y | GDK_ANCHOR_SLIDE_X); GDK_ANCHOR_FLIP_Y | GDK_ANCHOR_SLIDE_X);
gdk_popup_present (GDK_POPUP (priv->surface), gdk_popup_present (GDK_POPUP (handle->surface),
MAX (req.width, 1), MAX (req.width, 1),
MAX (req.height, 1), MAX (req.height, 1),
layout); layout);
gdk_popup_layout_unref (layout); gdk_popup_layout_unref (layout);
gtk_widget_allocate (widget, gtk_widget_allocate (widget,
gdk_surface_get_width (priv->surface), gdk_surface_get_width (handle->surface),
gdk_surface_get_height (priv->surface), gdk_surface_get_height (handle->surface),
-1, NULL); -1, NULL);
} }
@ -226,9 +213,8 @@ static void
surface_mapped_changed (GtkWidget *widget) surface_mapped_changed (GtkWidget *widget)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
gtk_widget_set_visible (widget, gdk_surface_get_mapped (priv->surface)); gtk_widget_set_visible (widget, gdk_surface_get_mapped (handle->surface));
} }
static void static void
@ -247,55 +233,52 @@ static void
gtk_text_handle_realize (GtkWidget *widget) gtk_text_handle_realize (GtkWidget *widget)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GdkSurface *parent_surface; GdkSurface *parent_surface;
GtkWidget *parent; GtkWidget *parent;
parent = gtk_widget_get_parent (widget); parent = gtk_widget_get_parent (widget);
parent_surface = gtk_native_get_surface (gtk_widget_get_native (parent)); parent_surface = gtk_native_get_surface (gtk_widget_get_native (parent));
priv->surface = gdk_surface_new_popup (parent_surface, FALSE); handle->surface = gdk_surface_new_popup (parent_surface, FALSE);
gdk_surface_set_widget (priv->surface, widget); gdk_surface_set_widget (handle->surface, widget);
g_signal_connect_swapped (priv->surface, "notify::mapped", g_signal_connect_swapped (handle->surface, "notify::mapped",
G_CALLBACK (surface_mapped_changed), widget); G_CALLBACK (surface_mapped_changed), widget);
g_signal_connect (priv->surface, "render", G_CALLBACK (surface_render), widget); g_signal_connect (handle->surface, "render", G_CALLBACK (surface_render), widget);
g_signal_connect (priv->surface, "event", G_CALLBACK (surface_event), widget); g_signal_connect (handle->surface, "event", G_CALLBACK (surface_event), widget);
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->realize (widget); GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->realize (widget);
priv->renderer = gsk_renderer_new_for_surface (priv->surface); handle->renderer = gsk_renderer_new_for_surface (handle->surface);
} }
static void static void
gtk_text_handle_unrealize (GtkWidget *widget) gtk_text_handle_unrealize (GtkWidget *widget)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unrealize (widget); GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unrealize (widget);
gsk_renderer_unrealize (priv->renderer); gsk_renderer_unrealize (handle->renderer);
g_clear_object (&priv->renderer); g_clear_object (&handle->renderer);
g_signal_handlers_disconnect_by_func (priv->surface, surface_render, widget); g_signal_handlers_disconnect_by_func (handle->surface, surface_render, widget);
g_signal_handlers_disconnect_by_func (priv->surface, surface_event, widget); g_signal_handlers_disconnect_by_func (handle->surface, surface_event, widget);
g_signal_handlers_disconnect_by_func (priv->surface, surface_mapped_changed, widget); g_signal_handlers_disconnect_by_func (handle->surface, surface_mapped_changed, widget);
gdk_surface_set_widget (priv->surface, NULL); gdk_surface_set_widget (handle->surface, NULL);
gdk_surface_destroy (priv->surface); gdk_surface_destroy (handle->surface);
g_clear_object (&priv->surface); g_clear_object (&handle->surface);
} }
static void static void
gtk_text_handle_map (GtkWidget *widget) gtk_text_handle_map (GtkWidget *widget)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->map (widget); GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->map (widget);
if (priv->has_point) if (handle->has_point)
gtk_text_handle_present_surface (handle); gtk_text_handle_present_surface (handle);
} }
@ -303,10 +286,9 @@ static void
gtk_text_handle_unmap (GtkWidget *widget) gtk_text_handle_unmap (GtkWidget *widget)
{ {
GtkTextHandle *handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unmap (widget); GTK_WIDGET_CLASS (gtk_text_handle_parent_class)->unmap (widget);
gdk_surface_hide (priv->surface); gdk_surface_hide (handle->surface);
} }
static void static void
@ -374,24 +356,23 @@ handle_drag_begin (GtkGestureDrag *gesture,
gdouble y, gdouble y,
GtkTextHandle *handle) GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GtkWidget *widget; GtkWidget *widget;
widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture)); widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
if (priv->role == GTK_TEXT_HANDLE_ROLE_CURSOR) if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
x -= gtk_widget_get_width (widget) / 2; x -= gtk_widget_get_width (widget) / 2;
else if ((priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END && else if ((handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END &&
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) || gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ||
(priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START && (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START &&
gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL)) gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL))
x -= gtk_widget_get_width (widget); x -= gtk_widget_get_width (widget);
y += priv->border.top / 2; y += handle->border.top / 2;
priv->dx = x; handle->dx = x;
priv->dy = y; handle->dy = y;
priv->dragged = TRUE; handle->dragged = TRUE;
g_signal_emit (handle, signals[DRAG_STARTED], 0); g_signal_emit (handle, signals[DRAG_STARTED], 0);
} }
@ -401,23 +382,22 @@ handle_drag_update (GtkGestureDrag *gesture,
gdouble offset_y, gdouble offset_y,
GtkWidget *widget) GtkWidget *widget)
{ {
GtkTextHandle *text_handle = GTK_TEXT_HANDLE (widget); GtkTextHandle *handle = GTK_TEXT_HANDLE (widget);
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (text_handle);
gdouble start_x, start_y; gdouble start_x, start_y;
gint x, y; gint x, y;
gtk_gesture_drag_get_start_point (gesture, &start_x, &start_y); gtk_gesture_drag_get_start_point (gesture, &start_x, &start_y);
x = priv->pointing_to.x + priv->pointing_to.width / 2 + x = handle->pointing_to.x + handle->pointing_to.width / 2 +
start_x + offset_x - priv->dx; start_x + offset_x - handle->dx;
y = priv->pointing_to.y + priv->pointing_to.height + y = handle->pointing_to.y + handle->pointing_to.height +
start_y + offset_y - priv->dy; start_y + offset_y - handle->dy;
if (priv->role == GTK_TEXT_HANDLE_ROLE_CURSOR) if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
x -= gtk_widget_get_width (widget) / 2; x -= gtk_widget_get_width (widget) / 2;
else if ((priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END && else if ((handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END &&
gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) || gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ||
(priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START && (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START &&
gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL)) gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL))
x -= gtk_widget_get_width (widget); x -= gtk_widget_get_width (widget);
@ -430,31 +410,28 @@ handle_drag_end (GtkGestureDrag *gesture,
gdouble offset_y, gdouble offset_y,
GtkTextHandle *handle) GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
g_signal_emit (handle, signals[DRAG_FINISHED], 0); g_signal_emit (handle, signals[DRAG_FINISHED], 0);
priv->dragged = FALSE; handle->dragged = FALSE;
} }
static void static void
gtk_text_handle_update_for_role (GtkTextHandle *handle) gtk_text_handle_update_for_role (GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
GtkWidget *widget = GTK_WIDGET (handle); GtkWidget *widget = GTK_WIDGET (handle);
if (priv->role == GTK_TEXT_HANDLE_ROLE_CURSOR) if (handle->role == GTK_TEXT_HANDLE_ROLE_CURSOR)
{ {
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_TOP); 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_BOTTOM);
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_INSERTION_CURSOR); gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_INSERTION_CURSOR);
} }
else if (priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END) else if (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_END)
{ {
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_TOP); 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_BOTTOM);
gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_INSERTION_CURSOR); gtk_widget_remove_css_class (widget, GTK_STYLE_CLASS_INSERTION_CURSOR);
} }
else if (priv->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START) else if (handle->role == GTK_TEXT_HANDLE_ROLE_SELECTION_START)
{ {
gtk_widget_add_css_class (widget, GTK_STYLE_CLASS_TOP); 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_BOTTOM);
@ -482,33 +459,27 @@ gtk_text_handle_init (GtkTextHandle *widget)
GtkTextHandle * GtkTextHandle *
gtk_text_handle_new (GtkWidget *parent) gtk_text_handle_new (GtkWidget *parent)
{ {
GtkWidget *handle; return g_object_new (GTK_TYPE_TEXT_HANDLE,
"parent", parent,
handle = g_object_new (GTK_TYPE_TEXT_HANDLE, "css-name", I_("cursor-handle"),
"css-name", I_("cursor-handle"), NULL);
NULL);
gtk_widget_set_parent (handle, parent);
return GTK_TEXT_HANDLE (handle);
} }
void void
gtk_text_handle_set_role (GtkTextHandle *handle, gtk_text_handle_set_role (GtkTextHandle *handle,
GtkTextHandleRole role) GtkTextHandleRole role)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
g_return_if_fail (GTK_IS_TEXT_HANDLE (handle)); g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));
if (priv->role == role) if (handle->role == role)
return; return;
priv->role = role; handle->role = role;
gtk_text_handle_update_for_role (handle); gtk_text_handle_update_for_role (handle);
if (gtk_widget_get_visible (GTK_WIDGET (handle))) if (gtk_widget_get_visible (GTK_WIDGET (handle)))
{ {
if (priv->has_point) if (handle->has_point)
gtk_text_handle_present_surface (handle); gtk_text_handle_present_surface (handle);
} }
} }
@ -516,29 +487,25 @@ gtk_text_handle_set_role (GtkTextHandle *handle,
GtkTextHandleRole GtkTextHandleRole
gtk_text_handle_get_role (GtkTextHandle *handle) gtk_text_handle_get_role (GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), GTK_TEXT_HANDLE_ROLE_CURSOR); g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), GTK_TEXT_HANDLE_ROLE_CURSOR);
return priv->role; return handle->role;
} }
void void
gtk_text_handle_set_position (GtkTextHandle *handle, gtk_text_handle_set_position (GtkTextHandle *handle,
const GdkRectangle *rect) const GdkRectangle *rect)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
g_return_if_fail (GTK_IS_TEXT_HANDLE (handle)); g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));
if (priv->pointing_to.x == rect->x && if (handle->pointing_to.x == rect->x &&
priv->pointing_to.y == rect->y && handle->pointing_to.y == rect->y &&
priv->pointing_to.width == rect->width && handle->pointing_to.width == rect->width &&
priv->pointing_to.height == rect->height) handle->pointing_to.height == rect->height)
return; return;
priv->pointing_to = *rect; handle->pointing_to = *rect;
priv->has_point = TRUE; handle->has_point = TRUE;
if (gtk_widget_is_visible (GTK_WIDGET (handle))) if (gtk_widget_is_visible (GTK_WIDGET (handle)))
gtk_text_handle_present_surface (handle); gtk_text_handle_present_surface (handle);
@ -547,9 +514,7 @@ gtk_text_handle_set_position (GtkTextHandle *handle,
gboolean gboolean
gtk_text_handle_get_is_dragged (GtkTextHandle *handle) gtk_text_handle_get_is_dragged (GtkTextHandle *handle)
{ {
GtkTextHandlePrivate *priv = gtk_text_handle_get_instance_private (handle);
g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), FALSE); g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), FALSE);
return priv->dragged; return handle->dragged;
} }