[gtk/wip/matthiasc/context-menu: 16/30] text view: Use a popover
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/wip/matthiasc/context-menu: 16/30] text view: Use a popover
- Date: Thu, 13 Jun 2019 00:25:33 +0000 (UTC)
commit c3640e85c96c9488c9299b12c07e019632da61cf
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Jun 11 03:41:30 2019 +0000
text view: Use a popover
gtk/gtktextview.c | 78 +++++++++++++++++++++++++++----------------------------
1 file changed, 39 insertions(+), 39 deletions(-)
---
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index abfee66997..580837eeb6 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -4459,11 +4459,7 @@ gtk_text_view_unrealize (GtkWidget *widget)
gtk_text_view_remove_validate_idles (text_view);
- if (priv->popup_menu)
- {
- gtk_widget_destroy (priv->popup_menu);
- priv->popup_menu = NULL;
- }
+ g_clear_pointer (&priv->popup_menu, gtk_widget_unparent);
gtk_im_context_set_client_widget (priv->im_context, NULL);
@@ -8504,13 +8500,6 @@ insert_emoji_activated (GSimpleAction *action,
gtk_text_view_insert_emoji (GTK_TEXT_VIEW (user_data));
}
-static void
-popup_menu_detach (GtkWidget *attach_widget,
- GtkMenu *menu)
-{
- GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
-}
-
static gboolean
range_contains_editable_text (const GtkTextIter *start,
const GtkTextIter *end,
@@ -8673,7 +8662,6 @@ gtk_text_view_do_popup (GtkTextView *text_view,
GtkTextViewPrivate *priv = text_view->priv;
GdkEvent *trigger_event;
GMenuModel *model;
- GtkWidget *menu;
if (!gtk_widget_get_realized (GTK_WIDGET (text_view)))
return;
@@ -8685,21 +8673,44 @@ gtk_text_view_do_popup (GtkTextView *text_view,
gtk_text_view_update_clipboard_actions (text_view);
- if (priv->popup_menu)
- gtk_widget_destroy (priv->popup_menu);
+ if (!priv->popup_menu)
+ {
+ model = gtk_widget_get_context_menu (GTK_WIDGET (text_view));
+ priv->popup_menu = gtk_popover_menu_new_from_model (GTK_WIDGET (text_view), model);
+ gtk_popover_set_position (GTK_POPOVER (priv->popup_menu), GTK_POS_BOTTOM);
- model = gtk_widget_get_context_menu (GTK_WIDGET (text_view));
- priv->popup_menu = menu = gtk_menu_new_from_model (model);
+ gtk_popover_set_has_arrow (GTK_POPOVER (priv->popup_menu), FALSE);
+ gtk_widget_set_halign (priv->popup_menu, GTK_ALIGN_START);
+ }
+
+ if (trigger_event && gdk_event_triggers_context_menu (trigger_event))
+ {
+ GdkDevice *device;
+ GdkRectangle rect = { 0, 0, 1, 1 };
- gtk_style_context_add_class (gtk_widget_get_style_context (priv->popup_menu),
- GTK_STYLE_CLASS_CONTEXT_MENU);
+ device = gdk_event_get_device (trigger_event);
- gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
- GTK_WIDGET (text_view),
- popup_menu_detach);
+ if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
+ device = gdk_device_get_associated_device (device);
- if (trigger_event && gdk_event_triggers_context_menu (trigger_event))
- gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu), trigger_event);
+ if (device)
+ {
+ GdkSurface *surface;
+ double px, py;
+
+ surface = gtk_native_get_surface (gtk_widget_get_native (GTK_WIDGET (text_view)));
+ gdk_surface_get_device_position (surface, device, &px, &py, NULL);
+ rect.x = round (px);
+ rect.y = round (py);
+
+ gtk_widget_translate_coordinates (GTK_WIDGET (gtk_widget_get_native (GTK_WIDGET (text_view))),
+ GTK_WIDGET (text_view),
+ rect.x, rect.y,
+ &rect.x, &rect.y);
+ }
+
+ gtk_popover_set_pointing_to (GTK_POPOVER (priv->popup_menu), &rect);
+ }
else
{
GtkTextIter iter;
@@ -8724,30 +8735,19 @@ gtk_text_view_do_popup (GtkTextView *text_view,
&iter_location.x,
&iter_location.y);
- gtk_menu_popup_at_rect (GTK_MENU (priv->popup_menu),
- gtk_native_get_surface (gtk_widget_get_native (GTK_WIDGET (text_view))),
- &iter_location,
- GDK_GRAVITY_SOUTH_EAST,
- GDK_GRAVITY_NORTH_WEST,
- trigger_event);
+ gtk_popover_set_pointing_to (GTK_POPOVER (priv->popup_menu), &iter_location);
}
- else
- gtk_menu_popup_at_widget (GTK_MENU (priv->popup_menu),
- GTK_WIDGET (text_view),
- GDK_GRAVITY_CENTER,
- GDK_GRAVITY_CENTER,
- trigger_event);
-
- gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
}
+ gtk_popover_popup (GTK_POPOVER (priv->popup_menu));
+
g_clear_object (&trigger_event);
}
static gboolean
gtk_text_view_popup_menu (GtkWidget *widget)
{
- gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
+ gtk_text_view_do_popup (GTK_TEXT_VIEW (widget), NULL);
return TRUE;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]