[gtk/wip/matthiasc/context-menu: 1049/1062] Add gtk_widget_set_context_menu



commit d8378b968c6b80f39de7de6a4604c404c6c9513e
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jan 26 18:50:14 2019 -0500

    Add gtk_widget_set_context_menu
    
    This lets apps associate a menu model with a widget.
    It is up to individual widgets to use it.

 gtk/gtkwidget.c        | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkwidget.h        |  7 ++++++
 gtk/gtkwidgetprivate.h |  2 ++
 3 files changed, 68 insertions(+)
---
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 07080d6b9e..01e6a1d447 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -575,6 +575,7 @@ enum {
   PROP_SCALE_FACTOR,
   PROP_CSS_NAME,
   PROP_LAYOUT_MANAGER,
+  PROP_CONTEXT_MENU,
   NUM_PROPERTIES
 };
 
@@ -1355,6 +1356,18 @@ gtk_widget_class_init (GtkWidgetClass *klass)
                          GTK_TYPE_LAYOUT_MANAGER,
                          GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkWidget:context-menu:
+   *
+   * The #GMenuModel that is used for this widgets context menu.
+   */
+  widget_props[PROP_CONTEXT_MENU] =
+    g_param_spec_object ("context-menu",
+                         P_("Context Menu"),
+                         P_("The menu model used for this widgets' context menu"),
+                         G_TYPE_MENU_MODEL,
+                         GTK_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (gobject_class, NUM_PROPERTIES, widget_props);
 
   /**
@@ -2294,6 +2307,9 @@ gtk_widget_set_property (GObject         *object,
     case PROP_LAYOUT_MANAGER:
       gtk_widget_set_layout_manager (widget, g_value_get_object (value));
       break;
+    case PROP_CONTEXT_MENU:
+      gtk_widget_set_context_menu (widget, g_value_get_object (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -2440,6 +2456,9 @@ gtk_widget_get_property (GObject         *object,
     case PROP_LAYOUT_MANAGER:
       g_value_set_object (value, gtk_widget_get_layout_manager (widget));
       break;
+    case PROP_CONTEXT_MENU:
+      g_value_set_object (value, gtk_widget_get_context_menu (widget));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -13406,3 +13425,43 @@ gtk_widget_get_layout_manager (GtkWidget *widget)
 
   return priv->layout_manager;
 }
+
+/**
+ * gtk_widget_set_context_menu:
+ * @widget: a #GtkWidget
+ * @menu: (nullable): a #GMenuModel
+ *
+ * Sets the menu model to use for the widgets
+ * context menu.
+ */
+void
+gtk_widget_set_context_menu (GtkWidget  *widget,
+                             GMenuModel *menu)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
+  g_return_if_fail (GTK_IS_WIDGET (widget));
+  g_return_if_fail (menu == NULL || G_IS_MENU_MODEL (menu));
+
+  if (g_set_object (&priv->context_menu, menu))
+    g_object_notify_by_pspec (G_OBJECT (widget), widget_props[PROP_CONTEXT_MENU]);
+}
+
+/**
+ * gtk_widget_get_context_menu:
+ * @widget: a #GtkWidget
+ *
+ * Returns the menu model that is used for
+ * this widgets context menu.
+ *
+ * Returns: (transfer none) (nullable): a #GMenuModel
+ */
+GMenuModel *
+gtk_widget_get_context_menu (GtkWidget *widget)
+{
+  GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
+
+  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+
+  return priv->context_menu;
+}
diff --git a/gtk/gtkwidget.h b/gtk/gtkwidget.h
index 94586e2253..3d766b9830 100644
--- a/gtk/gtkwidget.h
+++ b/gtk/gtkwidget.h
@@ -1027,6 +1027,13 @@ void                    gtk_widget_snapshot_child       (GtkWidget   *widget,
                                                          GtkWidget   *child,
                                                          GtkSnapshot *snapshot);
 
+GDK_AVAILABLE_IN_ALL
+void                    gtk_widget_set_context_menu     (GtkWidget  *widget,
+                                                         GMenuModel *menu);
+GDK_AVAILABLE_IN_ALL
+GMenuModel *            gtk_widget_get_context_menu     (GtkWidget  *widget);
+
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkWidget, g_object_unref)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkRequisition, gtk_requisition_free)
 
diff --git a/gtk/gtkwidgetprivate.h b/gtk/gtkwidgetprivate.h
index 26df307cc2..cf44b5582f 100644
--- a/gtk/gtkwidgetprivate.h
+++ b/gtk/gtkwidgetprivate.h
@@ -205,6 +205,8 @@ struct _GtkWidgetPrivate
 
   /* Pointer cursor */
   GdkCursor *cursor;
+
+  GMenuModel *context_menu;
 };
 
 void          gtk_widget_root               (GtkWidget *widget);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]