]> Pileus Git - ~andy/gtk/blobdiff - tests/testgmenu.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testgmenu.c
index 4674362fc9ee49d5b63515e3a5e070d311b6b103..2f0512896b64c7c8135164ef7656036201c597cb 100644 (file)
@@ -13,9 +13,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <stdlib.h>
@@ -90,6 +88,7 @@ typedef struct {
   gchar        *target;
   gulong        enabled_changed_id;
   gulong        state_changed_id;
+  gulong        activate_handler;
 } ActionData;
 
 static void
@@ -125,7 +124,12 @@ toggle_state_changed (GActionGroup     *group,
                       GVariant         *state,
                       GtkCheckMenuItem *w)
 {
+  ActionData *a;
+
+  a = g_object_get_data (G_OBJECT (w), "action");
+  g_signal_handler_block (w, a->activate_handler);
   gtk_check_menu_item_set_active (w, g_variant_get_boolean (state));
+  g_signal_handler_unblock (w, a->activate_handler);
 }
 
 static void
@@ -138,9 +142,10 @@ radio_state_changed (GActionGroup     *group,
   gboolean b;
 
   a = g_object_get_data (G_OBJECT (w), "action");
+  g_signal_handler_block (w, a->activate_handler);
   b = g_strcmp0 (a->target, g_variant_get_string (state, NULL)) == 0;
-
   gtk_check_menu_item_set_active (w, b);
+  g_signal_handler_unblock (w, a->activate_handler);
 }
 
 /* Menuitem callbacks {{{2 */
@@ -150,44 +155,14 @@ item_activated (GtkWidget *w,
                 gpointer   data)
 {
   ActionData *a;
+  GVariant *parameter;
 
   a = g_object_get_data (G_OBJECT (w), "action");
-  g_action_group_activate_action (a->group, a->name, NULL);
-}
-
-static void
-toggle_item_toggled (GtkCheckMenuItem *w,
-                     gpointer          data)
-{
-  ActionData *a;
-  gboolean b;
-
-  a = g_object_get_data (G_OBJECT (w), "action");
-  b = gtk_check_menu_item_get_active (w);
-  g_action_group_change_action_state (a->group, a->name,
-                                      g_variant_new_boolean (b));
-}
-
-static void
-radio_item_toggled (GtkCheckMenuItem *w,
-                    gpointer          data)
-{
-  ActionData *a;
-  GVariant *v;
-
-  a = g_object_get_data (G_OBJECT (w), "action");
-  if (gtk_check_menu_item_get_active (w))
-    {
-      g_action_group_change_action_state (a->group, a->name,
-                                          g_variant_new_string (a->target));
-    }
+  if (a->target)
+    parameter = g_variant_new_string (a->target);
   else
-    {
-      v = g_action_group_get_action_state (a->group, a->name);
-      if (g_strcmp0 (g_variant_get_string (v, NULL), a->target) == 0)
-        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (w), TRUE);
-      g_variant_unref (v);
-    }
+    parameter = NULL;
+  g_action_group_activate_action (a->group, a->name, parameter);
 }
 
 /* GtkMenu construction {{{2 */
@@ -206,6 +181,7 @@ create_menuitem_from_model (GMenuModel   *model,
   const GVariantType *type;
   GVariant *v;
 
+  label = NULL;
   g_menu_model_get_item_attribute (model, item, G_MENU_ATTRIBUTE_LABEL, "s", &label);
 
   action = NULL;
@@ -243,11 +219,14 @@ create_menuitem_from_model (GMenuModel   *model,
                                                 G_CALLBACK (enabled_changed), w);
       g_free (s);
 
+      a->activate_handler = g_signal_connect (w, "activate", G_CALLBACK (item_activated), NULL);
+
       if (type == NULL)
-        g_signal_connect (w, "activate", G_CALLBACK (item_activated), NULL);
+        {
+          /* all set */
+        }
       else if (g_variant_type_equal (type, G_VARIANT_TYPE_BOOLEAN))
         {
-          g_signal_connect (w, "toggled", G_CALLBACK (toggle_item_toggled), NULL);
           s = g_strconcat ("action-state-changed::", action, NULL);
           a->state_changed_id = g_signal_connect (group, s,
                                                   G_CALLBACK (toggle_state_changed), w);
@@ -259,7 +238,6 @@ create_menuitem_from_model (GMenuModel   *model,
         }
       else if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
         {
-          g_signal_connect (w, "toggled", G_CALLBACK (radio_item_toggled), NULL);
           s = g_strconcat ("action-state-changed::", action, NULL);
           a->state_changed_id = g_signal_connect (group, s,
                                                   G_CALLBACK (radio_state_changed), w);
@@ -289,7 +267,8 @@ static void
 append_items_from_model (GtkWidget    *menu,
                          GMenuModel   *model,
                          GActionGroup *group,
-                         gboolean     *need_separator)
+                         gboolean     *need_separator,
+                         const gchar  *heading)
 {
   gint n;
   gint i;
@@ -297,6 +276,7 @@ append_items_from_model (GtkWidget    *menu,
   GtkWidget *menuitem;
   GtkWidget *submenu;
   GMenuModel *m;
+  gchar *label;
 
   n = g_menu_model_get_n_items (model);
 
@@ -305,16 +285,40 @@ append_items_from_model (GtkWidget    *menu,
       w = gtk_separator_menu_item_new ();
       gtk_widget_show (w);
       gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
-
       *need_separator = FALSE;
     }
 
+  if (heading != NULL)
+    {
+      w = gtk_menu_item_new_with_label (heading);
+      gtk_widget_show (w);
+      gtk_widget_set_sensitive (w, FALSE);
+      gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
+#if 0
+      /* FIXME: this interferes with toggle spacing */
+      w = gtk_bin_get_child (GTK_BIN (w));
+      gtk_misc_set_alignment (GTK_MISC (w), 0.5, 0.5);
+#endif
+    }
+
   for (i = 0; i < n; i++)
     {
       if ((m = g_menu_model_get_item_link (model, i, G_MENU_LINK_SECTION)))
         {
-          append_items_from_model (menu, m, group, need_separator);
+          label = NULL;
+          g_menu_model_get_item_attribute (model, i, G_MENU_ATTRIBUTE_LABEL, "s", &label);
+          append_items_from_model (menu, m, group, need_separator, label);
           g_object_unref (m);
+          g_free (label);
+
+          if (*need_separator)
+            {
+              w = gtk_separator_menu_item_new ();
+              gtk_widget_show (w);
+              gtk_menu_shell_append (GTK_MENU_SHELL (menu), w);
+              *need_separator = FALSE;
+            }
+
           continue;
         }
 
@@ -343,7 +347,7 @@ create_menu_from_model (GMenuModel   *model,
 
   w = gtk_menu_new ();
   need_separator = FALSE;
-  append_items_from_model (w, model, group, &need_separator);
+  append_items_from_model (w, model, group, &need_separator, NULL);
 
   return w;
 }
@@ -383,77 +387,79 @@ menu_holder_get_menu (MenuHolder *holder)
 /* The example menu {{{1 */
 
 static const gchar menu_markup[] =
+  "<interface>\n"
   "<menu id='edit-menu'>\n"
   "  <section>\n"
-  "    <item action='undo'>\n"
-  "      <attribute name='label' translatable='yes' context='Stock label'>'_Undo'</attribute>\n"
+  "    <item>\n"
+  "      <attribute name='action'>undo</attribute>\n"
+  "      <attribute name='label' translatable='yes' context='Stock label'>_Undo</attribute>\n"
+  "    </item>\n"
+  "    <item>\n"
+  "      <attribute name='label' translatable='yes'>Redo</attribute>\n"
+  "      <attribute name='action'>redo</attribute>\n"
   "    </item>\n"
-  "    <item label='Redo' action='redo'/>\n"
   "  </section>\n"
-  "  <section></section>\n"
-  "  <section label='Copy &amp; Paste'>\n"
-  "    <item label='Cut' action='cut'/>\n"
-  "    <item label='Copy' action='copy'/>\n"
-  "    <item label='Paste' action='paste'/>\n"
+  "  <section/>\n"
+  "  <section>\n"
+  "    <attribute name='label' translatable='yes'>Copy &amp; Paste</attribute>\n"
+  "    <item>\n"
+  "      <attribute name='label' translatable='yes'>Cut</attribute>\n"
+  "      <attribute name='action'>cut</attribute>\n"
+  "    </item>\n"
+  "    <item>\n"
+  "      <attribute name='label' translatable='yes'>Copy</attribute>\n"
+  "      <attribute name='action'>copy</attribute>\n"
+  "    </item>\n"
+  "    <item>\n"
+  "      <attribute name='label' translatable='yes'>Paste</attribute>\n"
+  "      <attribute name='action'>paste</attribute>\n"
+  "    </item>\n"
   "  </section>\n"
   "  <section>\n"
-  "    <item label='Bold' action='bold'/>\n"
-  "    <submenu label='Language'>\n"
-  "      <item label='Latin' action='lang' target='latin'/>\n"
-  "      <item label='Greek' action='lang' target='greek'/>\n"
-  "      <item label='Urdu'  action='lang' target='urdu'/>\n"
+  "    <item>\n"
+  "      <attribute name='label' translatable='yes'>Bold</attribute>\n"
+  "      <attribute name='action'>bold</attribute>\n"
+  "    </item>\n"
+  "    <submenu>\n"
+  "      <attribute name='label' translatable='yes'>Language</attribute>\n"
+  "      <item>\n"
+  "        <attribute name='label' translatable='yes'>Latin</attribute>\n"
+  "        <attribute name='action'>lang</attribute>\n"
+  "        <attribute name='target'>latin</attribute>\n"
+  "      </item>\n"
+  "      <item>\n"
+  "        <attribute name='label' translatable='yes'>Greek</attribute>\n"
+  "        <attribute name='action'>lang</attribute>\n"
+  "        <attribute name='target'>greek</attribute>\n"
+  "      </item>\n"
+  "      <item>\n"
+  "        <attribute name='label' translatable='yes'>Urdu</attribute>\n"
+  "        <attribute name='action'>lang</attribute>\n"
+  "        <attribute name='target'>urdu</attribute>\n"
+  "      </item>\n"
   "    </submenu>\n"
   "  </section>\n"
-  "</menu>\n";
-
-static void
-start_element (GMarkupParseContext *context,
-               const gchar         *element_name,
-               const gchar        **attribute_names,
-               const gchar        **attribute_values,
-               gpointer             user_data,
-               GError             **error)
-{
-  if (strcmp (element_name, "menu") == 0)
-    g_menu_markup_parser_start_menu (context, "gtk30", NULL);
-}
-
-static void
-end_element (GMarkupParseContext *context,
-             const gchar         *element_name,
-             gpointer             user_data,
-             GError             **error)
-{
-  GMenu **menu = user_data;
-
-  if (strcmp (element_name, "menu") == 0)
-    *menu = g_menu_markup_parser_end_menu (context);
-}
-
-static const GMarkupParser parser = {
-   start_element, end_element, NULL, NULL, NULL
-};
+  "</menu>\n"
+  "</interface>\n";
 
 static GMenuModel *
 get_model (void)
 {
-  GMarkupParseContext *context;
-  GMenu *menu = NULL;
   GError *error = NULL;
+  GtkBuilder *builder;
+  GMenuModel *menu;
 
-  context = g_markup_parse_context_new (&parser, 0, &menu, NULL);
-  if (!g_markup_parse_context_parse (context, menu_markup, -1, &error))
-    {
-       g_warning ("menu parsing failed: %s\n", error->message);
-       exit (1);
-    }
-  g_markup_parse_context_free (context);
-  g_assert (menu);
+  builder = gtk_builder_new ();
+  gtk_builder_add_from_string (builder, menu_markup, -1, &error);
+  g_assert_no_error (error);
 
-   return G_MENU_MODEL (menu);
+  menu = g_object_ref (gtk_builder_get_object (builder, "edit-menu"));
+  g_object_unref (builder);
+
+  return menu;
 }
 
- /* The example actions {{{1 */
+/* The example actions {{{1 */
 
 static void
 activate_action (GSimpleAction *action, GVariant *parameter, gpointer user_data)
@@ -462,23 +468,37 @@ activate_action (GSimpleAction *action, GVariant *parameter, gpointer user_data)
 }
 
 static void
-toggle_changed (GSimpleAction *action, GVariant *value, gpointer user_data)
+activate_toggle (GSimpleAction *action, GVariant *parameter, gpointer user_data)
 {
-  g_print ("Toggle action %s state changed to %d\n",
+  GVariant *old_state, *new_state;
+
+  old_state = g_action_get_state (G_ACTION (action));
+  new_state = g_variant_new_boolean (!g_variant_get_boolean (old_state));
+
+  g_print ("Toggle action %s activated, state changes from %d to %d\n",
            g_action_get_name (G_ACTION (action)),
-           g_variant_get_boolean (value));
+           g_variant_get_boolean (old_state),
+           g_variant_get_boolean (new_state));
 
-  g_simple_action_set_state (action, value);
+  g_simple_action_set_state (action, new_state);
+  g_variant_unref (old_state);
 }
 
 static void
-radio_changed (GSimpleAction *action, GVariant *value, gpointer user_data)
+activate_radio (GSimpleAction *action, GVariant *parameter, gpointer user_data)
 {
-  g_print ("Radio action %s state changed to %s\n",
+  GVariant *old_state, *new_state;
+
+  old_state = g_action_get_state (G_ACTION (action));
+  new_state = g_variant_new_string (g_variant_get_string (parameter, NULL));
+
+  g_print ("Radio action %s activated, state changes from %s to %s\n",
            g_action_get_name (G_ACTION (action)),
-           g_variant_get_string (value, NULL));
+           g_variant_get_string (old_state, NULL),
+           g_variant_get_string (new_state, NULL));
 
-  g_simple_action_set_state (action, value);
+  g_simple_action_set_state (action, new_state);
+  g_variant_unref (old_state);
 }
 
 static GActionEntry actions[] = {
@@ -487,8 +507,8 @@ static GActionEntry actions[] = {
   { "cut",   activate_action, NULL, NULL,      NULL },
   { "copy",  activate_action, NULL, NULL,      NULL },
   { "paste", activate_action, NULL, NULL,      NULL },
-  { "bold",  NULL,            NULL, "true",    toggle_changed },
-  { "lang",  NULL,            NULL, "'latin'", radio_changed },
+  { "bold",  activate_toggle, NULL, "true",    NULL },
+  { "lang",  activate_radio,  "s",  "'latin'", NULL },
 };
 
 static GActionGroup *
@@ -541,8 +561,10 @@ state_cell_func (GtkTreeViewColumn *column,
   gtk_cell_renderer_set_visible (cell, FALSE);
   g_object_set (cell, "mode", GTK_CELL_RENDERER_MODE_INERT, NULL);
 
-  if (state &&
-      g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN) &&
+  if (state == NULL)
+    return;
+
+  if (g_variant_is_of_type (state, G_VARIANT_TYPE_BOOLEAN) &&
       GTK_IS_CELL_RENDERER_TOGGLE (cell))
     {
       gtk_cell_renderer_set_visible (cell, TRUE);
@@ -550,8 +572,7 @@ state_cell_func (GtkTreeViewColumn *column,
       gtk_cell_renderer_toggle_set_active (GTK_CELL_RENDERER_TOGGLE (cell),
                                            g_variant_get_boolean (state));
     }
-  else if (state &&
-           g_variant_is_of_type (state, G_VARIANT_TYPE_STRING) &&
+  else if (g_variant_is_of_type (state, G_VARIANT_TYPE_STRING) &&
            GTK_IS_CELL_RENDERER_COMBO (cell))
     {
       gtk_cell_renderer_set_visible (cell, TRUE);
@@ -559,8 +580,7 @@ state_cell_func (GtkTreeViewColumn *column,
       g_object_set (cell, "text", g_variant_get_string (state, NULL), NULL);
     }
 
-  if (state)
-    g_variant_unref (state);
+  g_variant_unref (state);
 }
 
 static void
@@ -803,7 +823,7 @@ toggle_italic (GtkToggleButton *button, gpointer data)
     {
       action = g_simple_action_new_stateful ("italic", NULL, g_variant_new_boolean (FALSE));
       g_simple_action_group_insert (G_SIMPLE_ACTION_GROUP (group), G_ACTION (action));
-      g_signal_connect (action, "change-state", G_CALLBACK (toggle_changed), NULL);
+      g_signal_connect (action, "activate", G_CALLBACK (activate_toggle), NULL);
       g_object_unref (action);
       action_list_add (store, "italic");
       g_menu_insert (G_MENU (m), 1, "Italic", "italic");
@@ -969,9 +989,9 @@ main (int argc, char *argv[])
   if (do_import)
     {
       g_print ("Getting menus from the bus...\n");
-      model = (GMenuModel*)g_menu_proxy_get (bus, BUS_NAME, OBJ_PATH);
+      model = (GMenuModel*)g_dbus_menu_model_get (bus, BUS_NAME, OBJ_PATH);
       g_print ("Getting actions from the bus...\n");
-      group = (GActionGroup*)g_dbus_action_group_new_sync (bus, BUS_NAME, OBJ_PATH, 0, NULL, NULL);
+      group = (GActionGroup*)g_dbus_action_group_get (bus, BUS_NAME, OBJ_PATH);
     }
   else
     {
@@ -987,13 +1007,13 @@ main (int argc, char *argv[])
   if (do_export)
     {
       g_print ("Exporting menus on the bus...\n");
-      if (!g_menu_exporter_export (bus, OBJ_PATH, model, &error))
+      if (!g_dbus_connection_export_menu_model (bus, OBJ_PATH, model, &error))
         {
           g_warning ("Menu export failed: %s", error->message);
           exit (1);
         }
       g_print ("Exporting actions on the bus...\n");
-      if (!g_action_group_exporter_export (bus, OBJ_PATH, group, &error))
+      if (!g_dbus_connection_export_action_group (bus, OBJ_PATH, group, &error))
         {
           g_warning ("Action export failed: %s", error->message);
           exit (1);