X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=tests%2Ftestactions.c;h=e59fa886013e00010d733d80afa10050022af1ea;hb=HEAD;hp=131d0ccd200a7ff332f85faf10d7bfa2f4bff341;hpb=77af2d637c944f1e61d290ede8ae61be95c5abb7;p=~andy%2Fgtk diff --git a/tests/testactions.c b/tests/testactions.c index 131d0ccd2..e59fa8860 100644 --- a/tests/testactions.c +++ b/tests/testactions.c @@ -1,11 +1,23 @@ -#undef GTK_DISABLE_DEPRECATED +/* testactions.c + * Copyright (C) 2003 Matthias Clasen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * 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, see . + */ + +#include "config.h" #include -#ifndef _ -# define _(String) (String) -# define N_(String) (String) -#endif - static GtkActionGroup *action_group = NULL; static GtkToolbar *toolbar = NULL; @@ -28,6 +40,31 @@ toggle_action (GtkAction *action) gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action))); } + +static void +radio_action (GtkAction *action) +{ + const gchar *name = gtk_action_get_name (action); + const gchar *typename = G_OBJECT_TYPE_NAME (action); + + g_message ("Action %s (type=%s) activated (active=%d) (value %d)", name, typename, + gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)), + gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action))); +} + +static void +recent_action (GtkAction *action) +{ + const gchar *name = gtk_action_get_name (action); + const gchar *typename = G_OBJECT_TYPE_NAME (action); + gchar *uri = gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (action)); + + g_message ("Action %s (type=%s) activated (uri=%s)", + name, typename, + uri ? uri : "no item selected"); + g_free (uri); +} + static void toggle_cnp_actions (GtkAction *action) { @@ -43,9 +80,9 @@ toggle_cnp_actions (GtkAction *action) action = gtk_action_group_get_action (action_group, "toggle-cnp"); if (sensitive) - g_object_set (action, "label", _("Disable Cut and paste ops"), NULL); + g_object_set (action, "label", "Disable Cut and paste ops", NULL); else - g_object_set (action, "label", _("Enable Cut and paste ops"), NULL); + g_object_set (action, "label", "Enable Cut and paste ops", NULL); } static void @@ -55,144 +92,160 @@ show_accel_dialog (GtkAction *action) } static void -toolbar_style (GtkAction *action, - gpointer user_data) +toolbar_style (GtkAction *action) { GtkToolbarStyle style; g_return_if_fail (toolbar != NULL); - style = GPOINTER_TO_INT (user_data); + + radio_action (action); + + style = gtk_radio_action_get_current_value (GTK_RADIO_ACTION (action)); gtk_toolbar_set_style (toolbar, style); } static void -toolbar_size (GtkAction *action, - gpointer user_data) +toolbar_size_small (GtkAction *action) { - GtkIconSize size; + g_return_if_fail (toolbar != NULL); + gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_SMALL_TOOLBAR); +} + +static void +toolbar_size_large (GtkAction *action) +{ g_return_if_fail (toolbar != NULL); - size = GPOINTER_TO_INT (user_data); - gtk_toolbar_set_icon_size (toolbar, size); + gtk_toolbar_set_icon_size (toolbar, GTK_ICON_SIZE_LARGE_TOOLBAR); } /* convenience functions for declaring actions */ -static GtkActionGroupEntry entries[] = { - { "Menu1Action", N_("Menu _1"), NULL, NULL, NULL, NULL, NULL }, - { "Menu2Action", N_("Menu _2"), NULL, NULL, NULL, NULL, NULL }, - - { "cut", N_("C_ut"), GTK_STOCK_CUT, "X", - N_("Cut the selected text to the clipboard"), - G_CALLBACK (activate_action), NULL }, - { "copy", N_("_Copy"), GTK_STOCK_COPY, "C", - N_("Copy the selected text to the clipboard"), - G_CALLBACK (activate_action), NULL }, - { "paste", N_("_Paste"), GTK_STOCK_PASTE, "V", - N_("Paste the text from the clipboard"), - G_CALLBACK (activate_action), NULL }, - { "bold", N_("_Bold"), GTK_STOCK_BOLD, "B", - N_("Change to bold face"), - G_CALLBACK (toggle_action), NULL, GTK_ACTION_TOGGLE }, - - { "justify-left", N_("_Left"), GTK_STOCK_JUSTIFY_LEFT, "L", - N_("Left justify the text"), - G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO }, - { "justify-center", N_("C_enter"), GTK_STOCK_JUSTIFY_CENTER, "E", - N_("Center justify the text"), - G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" }, - { "justify-right", N_("_Right"), GTK_STOCK_JUSTIFY_RIGHT, "R", - N_("Right justify the text"), - G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" }, - { "justify-fill", N_("_Fill"), GTK_STOCK_JUSTIFY_FILL, "J", - N_("Fill justify the text"), - G_CALLBACK (toggle_action), NULL, GTK_ACTION_RADIO, "justify-left" }, - { "quit", NULL, GTK_STOCK_QUIT, "Q", - N_("Quit the application"), - G_CALLBACK (gtk_main_quit), NULL }, - { "toggle-cnp", N_("Enable Cut/Copy/Paste"), NULL, NULL, - N_("Change the sensitivity of the cut, copy and paste actions"), - G_CALLBACK (toggle_cnp_actions), NULL, GTK_ACTION_TOGGLE }, - { "customise-accels", N_("Customise _Accels"), NULL, NULL, - N_("Customise keyboard shortcuts"), - G_CALLBACK (show_accel_dialog), NULL }, - { "toolbar-icons", N_("Icons"), NULL, NULL, - NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_ICONS), - GTK_ACTION_RADIO, NULL }, - { "toolbar-text", N_("Text"), NULL, NULL, - NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_TEXT), - GTK_ACTION_RADIO, "toolbar-icons" }, - { "toolbar-both", N_("Both"), NULL, NULL, - NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER (GTK_TOOLBAR_BOTH), - GTK_ACTION_RADIO, "toolbar-icons" }, - { "toolbar-both-horiz", N_("Both Horizontal"), NULL, NULL, - NULL, G_CALLBACK (toolbar_style), GINT_TO_POINTER(GTK_TOOLBAR_BOTH_HORIZ), - GTK_ACTION_RADIO, "toolbar-icons" }, - { "toolbar-small-icons", N_("Small Icons"), NULL, NULL, - NULL, - G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_SMALL_TOOLBAR) }, - { "toolbar-large-icons", N_("Large Icons"), NULL, NULL, - NULL, - G_CALLBACK (toolbar_size), GINT_TO_POINTER (GTK_ICON_SIZE_LARGE_TOOLBAR) }, +static GtkActionEntry entries[] = { + { "Menu1Action", NULL, "Menu _1" }, + { "Menu2Action", NULL, "Menu _2" }, + { "Menu3Action", NULL, "_Dynamic Menu" }, + + { "attach", "mail-attachment", "_Attachment...", "m", + "Attach a file", G_CALLBACK (activate_action) }, + { "cut", GTK_STOCK_CUT, "C_ut", "X", + "Cut the selected text to the clipboard", G_CALLBACK (activate_action) }, + { "copy", GTK_STOCK_COPY, "_Copy", "C", + "Copy the selected text to the clipboard", G_CALLBACK (activate_action) }, + { "paste", GTK_STOCK_PASTE, "_Paste", "V", + "Paste the text from the clipboard", G_CALLBACK (activate_action) }, + { "quit", GTK_STOCK_QUIT, NULL, "Q", + "Quit the application", G_CALLBACK (gtk_main_quit) }, + { "customise-accels", NULL, "Customise _Accels", NULL, + "Customise keyboard shortcuts", G_CALLBACK (show_accel_dialog) }, + { "toolbar-small-icons", NULL, "Small Icons", NULL, + NULL, G_CALLBACK (toolbar_size_small) }, + { "toolbar-large-icons", NULL, "Large Icons", NULL, + NULL, G_CALLBACK (toolbar_size_large) } }; static guint n_entries = G_N_ELEMENTS (entries); +static GtkToggleActionEntry toggle_entries[] = { + { "bold", GTK_STOCK_BOLD, "_Bold", "B", + "Change to bold face", + G_CALLBACK (toggle_action), FALSE }, + { "toggle-cnp", NULL, "Enable Cut/Copy/Paste", NULL, + "Change the sensitivity of the cut, copy and paste actions", + G_CALLBACK (toggle_cnp_actions), TRUE }, +}; +static guint n_toggle_entries = G_N_ELEMENTS (toggle_entries); + +enum { + JUSTIFY_LEFT, + JUSTIFY_CENTER, + JUSTIFY_RIGHT, + JUSTIFY_FILL +}; + +static GtkRadioActionEntry justify_entries[] = { + { "justify-left", GTK_STOCK_JUSTIFY_LEFT, "_Left", "L", + "Left justify the text", JUSTIFY_LEFT }, + { "justify-center", GTK_STOCK_JUSTIFY_CENTER, "C_enter", "E", + "Center justify the text", JUSTIFY_CENTER }, + { "justify-right", GTK_STOCK_JUSTIFY_RIGHT, "_Right", "R", + "Right justify the text", JUSTIFY_RIGHT }, + { "justify-fill", GTK_STOCK_JUSTIFY_FILL, "_Fill", "J", + "Fill justify the text", JUSTIFY_FILL } +}; +static guint n_justify_entries = G_N_ELEMENTS (justify_entries); + +static GtkRadioActionEntry toolbar_entries[] = { + { "toolbar-icons", NULL, "Icons", NULL, NULL, GTK_TOOLBAR_ICONS }, + { "toolbar-text", NULL, "Text", NULL, NULL, GTK_TOOLBAR_TEXT }, + { "toolbar-both", NULL, "Both", NULL, NULL, GTK_TOOLBAR_BOTH }, + { "toolbar-both-horiz", NULL, "Both Horizontal", NULL, NULL, GTK_TOOLBAR_BOTH_HORIZ } +}; +static guint n_toolbar_entries = G_N_ELEMENTS (toolbar_entries); + /* XML description of the menus for the test app. The parser understands * a subset of the Bonobo UI XML format, and uses GMarkup for parsing */ static const gchar *ui_info = -"\n" -" \n" -" \n" -" \n" -" \n" -" \n" +" \n" +" \n" +" \n" +" \n" +" \n" " \n" -" \n" -" \n" +" \n" +" \n" " \n" -" \n" +" \n" " \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" " \n" -" \n" -" \n" -" \n" -" \n" +" \n" " \n" -" \n" +" \n" +" \n" +" \n" +" \n" " \n" -" \n" -" \n" -" \n" -" \n" +" \n" " \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" " \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -" \n" -"\n"; +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n" +" \n"; static void add_widget (GtkUIManager *merge, @@ -200,7 +253,7 @@ add_widget (GtkUIManager *merge, GtkContainer *container) { - gtk_container_add (container, widget); + gtk_box_pack_start (GTK_BOX (container), widget, FALSE, FALSE, 0); gtk_widget_show (widget); if (GTK_IS_TOOLBAR (widget)) @@ -210,24 +263,105 @@ add_widget (GtkUIManager *merge, } } +static guint ui_id = 0; +static GtkActionGroup *dag = NULL; + +static void +ensure_update (GtkUIManager *manager) +{ + GTimer *timer; + double seconds; + gulong microsecs; + + timer = g_timer_new (); + g_timer_start (timer); + + gtk_ui_manager_ensure_update (manager); + + g_timer_stop (timer); + seconds = g_timer_elapsed (timer, µsecs); + g_timer_destroy (timer); + + g_print ("Time: %fs\n", seconds); +} + +static void +add_cb (GtkWidget *button, + GtkUIManager *manager) +{ + GtkWidget *spinbutton; + GtkAction *action; + int i, num; + char *name, *label; + + if (ui_id != 0 || dag != NULL) + return; + + spinbutton = g_object_get_data (G_OBJECT (button), "spinbutton"); + num = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spinbutton)); + + dag = gtk_action_group_new ("DynamicActions"); + gtk_ui_manager_insert_action_group (manager, dag, 0); + + ui_id = gtk_ui_manager_new_merge_id (manager); + + for (i = 0; i < num; i++) + { + name = g_strdup_printf ("DynAction%u", i); + label = g_strdup_printf ("Dynamic Item %d", i); + + action = g_object_new (GTK_TYPE_ACTION, + "name", name, + "label", label, + NULL); + gtk_action_group_add_action (dag, action); + g_object_unref (action); + + gtk_ui_manager_add_ui (manager, ui_id, "/menubar/DynamicMenu", + name, name, + GTK_UI_MANAGER_MENUITEM, FALSE); + } + + ensure_update (manager); +} + +static void +remove_cb (GtkWidget *button, + GtkUIManager *manager) +{ + if (ui_id == 0 || dag == NULL) + return; + + gtk_ui_manager_remove_ui (manager, ui_id); + ensure_update (manager); + ui_id = 0; + + gtk_ui_manager_remove_action_group (manager, dag); + g_object_unref (dag); + dag = NULL; +} + static void create_window (GtkActionGroup *action_group) { GtkUIManager *merge; GtkWidget *window; GtkWidget *box; + GtkWidget *hbox, *spinbutton, *button; GError *error = NULL; + merge = gtk_ui_manager_new (); + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (window), -1, -1); gtk_window_set_title (GTK_WINDOW (window), "Action Test"); + g_signal_connect_swapped (window, "destroy", G_CALLBACK (g_object_unref), merge); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); - box = gtk_vbox_new (FALSE, 0); + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); gtk_container_add (GTK_CONTAINER (window), box); gtk_widget_show (box); - merge = gtk_ui_manager_new (); gtk_ui_manager_insert_action_group (merge, action_group, 0); g_signal_connect (merge, "add_widget", G_CALLBACK (add_widget), box); @@ -240,26 +374,87 @@ create_window (GtkActionGroup *action_group) g_error_free (error); } + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0); + gtk_box_pack_end (GTK_BOX (box), hbox, FALSE, FALSE, 0); + gtk_widget_show (hbox); + + spinbutton = gtk_spin_button_new_with_range (100, 10000, 100); + gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0); + gtk_widget_show (spinbutton); + + button = gtk_button_new_with_label ("Add"); + gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); + gtk_widget_show (button); + + g_object_set_data (G_OBJECT (button), "spinbutton", spinbutton); + g_signal_connect (button, "clicked", G_CALLBACK (add_cb), merge); + + button = gtk_button_new_with_label ("Remove"); + gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); + gtk_widget_show (button); + + g_signal_connect (button, "clicked", G_CALLBACK (remove_cb), merge); + gtk_widget_show (window); } int main (int argc, char **argv) { + GtkAction *action; + gtk_init (&argc, &argv); if (g_file_test ("accels", G_FILE_TEST_IS_REGULAR)) gtk_accel_map_load ("accels"); - action_group = gtk_action_group_new ("TestActions"); - gtk_action_group_add_actions (action_group, entries, n_entries); + action = gtk_recent_action_new ("recent", + "Open Recent", "Open recent files", + NULL); + g_signal_connect (action, "item-activated", + G_CALLBACK (recent_action), + NULL); + g_signal_connect (action, "activate", + G_CALLBACK (recent_action), + NULL); - gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (gtk_action_group_get_action (action_group, "toggle-cnp")), TRUE); + action_group = gtk_action_group_new ("TestActions"); + gtk_action_group_add_actions (action_group, + entries, n_entries, + NULL); + gtk_action_group_add_toggle_actions (action_group, + toggle_entries, n_toggle_entries, + NULL); + gtk_action_group_add_radio_actions (action_group, + justify_entries, n_justify_entries, + JUSTIFY_LEFT, + G_CALLBACK (radio_action), NULL); + gtk_action_group_add_radio_actions (action_group, + toolbar_entries, n_toolbar_entries, + GTK_TOOLBAR_BOTH, + G_CALLBACK (toolbar_style), NULL); + gtk_action_group_add_action_with_accel (action_group, action, NULL); create_window (action_group); gtk_main (); +#ifdef DEBUG_UI_MANAGER + { + GList *action; + + for (action = gtk_action_group_list_actions (action_group); + action; + action = action->next) + { + GtkAction *a = action->data; + g_print ("action %s ref count %d\n", + gtk_action_get_name (a), G_OBJECT (a)->ref_count); + } + } +#endif + + g_object_unref (action); g_object_unref (action_group); gtk_accel_map_save ("accels");