]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkmenuitem.c
Fix typo from f2ab3af
[~andy/gtk] / gtk / gtkmenuitem.c
index 5a7b166748908a7c89cde86c14da3dcc1db2f0a0..576a64a15cfeaa7f2ce9ac194e144b0b5db91ee9 100644 (file)
@@ -43,7 +43,7 @@
 #include "gtksettings.h"
 #include "gtktypebuiltins.h"
 #include "a11y/gtkmenuitemaccessible.h"
-
+#include "deprecated/gtktearoffmenuitem.h"
 
 /**
  * SECTION:gtkmenuitem
@@ -97,7 +97,10 @@ enum {
 
   /* activatable properties */
   PROP_ACTIVATABLE_RELATED_ACTION,
-  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
+  PROP_ACTIVATABLE_USE_ACTION_APPEARANCE,
+
+  PROP_ACTION_NAME,
+  PROP_ACTION_TARGET
 };
 
 
@@ -179,6 +182,7 @@ static void gtk_menu_item_buildable_custom_finished(GtkBuildable        *buildab
                                                     const gchar         *tagname,
                                                     gpointer             user_data);
 
+static void gtk_menu_item_actionable_interface_init  (GtkActionableInterface *iface);
 static void gtk_menu_item_activatable_interface_init (GtkActivatableIface  *iface);
 static void gtk_menu_item_update                     (GtkActivatable       *activatable,
                                                       GtkAction            *action,
@@ -198,7 +202,58 @@ G_DEFINE_TYPE_WITH_CODE (GtkMenuItem, gtk_menu_item, GTK_TYPE_BIN,
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                 gtk_menu_item_buildable_interface_init)
                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
-                                                gtk_menu_item_activatable_interface_init))
+                                                gtk_menu_item_activatable_interface_init)
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE,
+                                                gtk_menu_item_actionable_interface_init))
+
+static void
+gtk_menu_item_set_action_name (GtkActionable *actionable,
+                               const gchar   *action_name)
+{
+  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);
+
+  if (!menu_item->priv->action_helper)
+    menu_item->priv->action_helper = gtk_action_helper_new (actionable);
+
+  gtk_action_helper_set_action_name (menu_item->priv->action_helper, action_name);
+}
+
+static void
+gtk_menu_item_set_action_target_value (GtkActionable *actionable,
+                                       GVariant      *action_target)
+{
+  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);
+
+  if (!menu_item->priv->action_helper)
+    menu_item->priv->action_helper = gtk_action_helper_new (actionable);
+
+  gtk_action_helper_set_action_target_value (menu_item->priv->action_helper, action_target);
+}
+
+static const gchar *
+gtk_menu_item_get_action_name (GtkActionable *actionable)
+{
+  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);
+
+  return gtk_action_helper_get_action_name (menu_item->priv->action_helper);
+}
+
+static GVariant *
+gtk_menu_item_get_action_target_value (GtkActionable *actionable)
+{
+  GtkMenuItem *menu_item = GTK_MENU_ITEM (actionable);
+
+  return gtk_action_helper_get_action_target_value (menu_item->priv->action_helper);
+}
+
+static void
+gtk_menu_item_actionable_interface_init (GtkActionableInterface *iface)
+{
+  iface->set_action_name = gtk_menu_item_set_action_name;
+  iface->get_action_name = gtk_menu_item_get_action_name;
+  iface->set_action_target_value = gtk_menu_item_set_action_target_value;
+  iface->get_action_target_value = gtk_menu_item_get_action_target_value;
+}
 
 static void
 gtk_menu_item_class_init (GtkMenuItemClass *klass)
@@ -397,6 +452,9 @@ gtk_menu_item_class_init (GtkMenuItemClass *klass)
   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
 
+  g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
+  g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
+
   gtk_widget_class_install_style_property_parser (widget_class,
                                                   g_param_spec_enum ("selected-shadow-type",
                                                                      "Selected Shadow Type",
@@ -547,6 +605,8 @@ gtk_menu_item_dispose (GObject *object)
   GtkMenuItem *menu_item = GTK_MENU_ITEM (object);
   GtkMenuItemPrivate *priv = menu_item->priv;
 
+  g_clear_object (&priv->action_helper);
+
   if (priv->action)
     {
       gtk_action_disconnect_accelerator (priv->action);
@@ -602,6 +662,12 @@ gtk_menu_item_set_property (GObject      *object,
     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
       gtk_menu_item_set_use_action_appearance (menu_item, g_value_get_boolean (value));
       break;
+    case PROP_ACTION_NAME:
+      gtk_menu_item_set_action_name (GTK_ACTIONABLE (menu_item), g_value_get_string (value));
+      break;
+    case PROP_ACTION_TARGET:
+      gtk_menu_item_set_action_target_value (GTK_ACTIONABLE (menu_item), g_value_get_variant (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -640,6 +706,12 @@ gtk_menu_item_get_property (GObject    *object,
     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
       g_value_set_boolean (value, priv->use_action_appearance);
       break;
+    case PROP_ACTION_NAME:
+      g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
+      break;
+    case PROP_ACTION_TARGET:
+      g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -1085,7 +1157,50 @@ activatable_update_label (GtkMenuItem *menu_item, GtkAction *action)
     }
 }
 
-gboolean _gtk_menu_is_empty (GtkWidget *menu);
+/*
+ * gtk_menu_is_empty:
+ * @menu: (allow-none): a #GtkMenu or %NULL
+ * 
+ * Determines whether @menu is empty. A menu is considered empty if it
+ * the only visible children are tearoff menu items or "filler" menu 
+ * items which were inserted to mark the menu as empty.
+ * 
+ * This function is used by #GtkAction.
+ *
+ * Return value: whether @menu is empty.
+ **/
+static gboolean
+gtk_menu_is_empty (GtkWidget *menu)
+{
+  GList *children, *cur;
+  gboolean result = TRUE;
+
+  g_return_val_if_fail (menu == NULL || GTK_IS_MENU (menu), TRUE);
+
+  if (!menu)
+    return FALSE;
+
+  children = gtk_container_get_children (GTK_CONTAINER (menu));
+
+  cur = children;
+  while (cur) 
+    {
+      if (gtk_widget_get_visible (cur->data))
+       {
+         if (!GTK_IS_TEAROFF_MENU_ITEM (cur->data) &&
+             !g_object_get_data (cur->data, "gtk-empty-menu-item"))
+            {
+             result = FALSE;
+              break;
+            }
+       }
+      cur = cur->next;
+    }
+  g_list_free (children);
+
+  return result;
+}
+
 
 static void
 gtk_menu_item_update (GtkActivatable *activatable,
@@ -1097,7 +1212,7 @@ gtk_menu_item_update (GtkActivatable *activatable,
 
   if (strcmp (property_name, "visible") == 0)
     _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
-                                   _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
+                                   gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
   else if (strcmp (property_name, "sensitive") == 0)
     gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
   else if (priv->use_action_appearance)
@@ -1127,7 +1242,7 @@ gtk_menu_item_sync_action_properties (GtkActivatable *activatable,
     return;
 
   _gtk_action_sync_menu_visible (action, GTK_WIDGET (menu_item),
-                                 _gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
+                                 gtk_menu_is_empty (gtk_menu_item_get_submenu (menu_item)));
 
   gtk_widget_set_sensitive (GTK_WIDGET (menu_item), gtk_action_is_sensitive (action));
 
@@ -1602,13 +1717,8 @@ gtk_menu_item_draw (GtkWidget *widget,
   child = gtk_bin_get_child (GTK_BIN (menu_item));
   parent = gtk_widget_get_parent (widget);
 
-  gtk_style_context_save (context);
-
   gtk_style_context_get_padding (context, state, &padding);
 
-  if (GTK_IS_MENU_BAR (parent))
-    gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENUBAR);
-
   if (child && (state & GTK_STATE_FLAG_PRELIGHT))
     {
       gtk_render_background (context, cr, x, y, w, h);
@@ -1673,8 +1783,6 @@ gtk_menu_item_draw (GtkWidget *widget,
 
   GTK_WIDGET_CLASS (gtk_menu_item_parent_class)->draw (widget, cr);
 
-  gtk_style_context_restore (context);
-
   return FALSE;
 }
 
@@ -1756,6 +1864,9 @@ gtk_real_menu_item_activate (GtkMenuItem *menu_item)
 {
   GtkMenuItemPrivate *priv = menu_item->priv;
 
+  if (priv->action_helper)
+    gtk_action_helper_activate (priv->action_helper);
+
   if (priv->action)
     gtk_action_activate (priv->action);
 }
@@ -2356,7 +2467,7 @@ _gtk_menu_item_refresh_accel_path (GtkMenuItem   *menu_item,
 }
 
 /**
- * gtk_menu_item_set_accel_path
+ * gtk_menu_item_set_accel_path:
  * @menu_item:  a valid #GtkMenuItem
  * @accel_path: (allow-none): accelerator path, corresponding to this menu
  *     item's functionality, or %NULL to unset the current path.
@@ -2416,7 +2527,7 @@ gtk_menu_item_set_accel_path (GtkMenuItem *menu_item,
 }
 
 /**
- * gtk_menu_item_get_accel_path
+ * gtk_menu_item_get_accel_path:
  * @menu_item:  a valid #GtkMenuItem
  *
  * Retrieve the accelerator path that was previously set on @menu_item.