X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;ds=sidebyside;f=tests%2Ftestactions.c;h=ec574c691212f6331694cf967a5d533ddf6064aa;hb=f62e4a02b66dfd6fec42d310359f4b3d5afaaea4;hp=8acb5ca1118e24538e37fc52b3fe1c5c832ad5a6;hpb=80581c3011871fa000433a881554ffc1e9363468;p=~andy%2Fgtk diff --git a/tests/testactions.c b/tests/testactions.c index 8acb5ca11..ec574c691 100644 --- a/tests/testactions.c +++ b/tests/testactions.c @@ -1,5 +1,23 @@ -#undef GTK_DISABLE_DEPRECATED -#include +/* 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, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" #include static GtkActionGroup *action_group = NULL; @@ -36,6 +54,19 @@ radio_action (GtkAction *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) { @@ -63,13 +94,15 @@ 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); } @@ -94,7 +127,10 @@ toolbar_size_large (GtkAction *action) 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", @@ -161,47 +197,57 @@ 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"; static void add_widget (GtkUIManager *merge, @@ -209,7 +255,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)) @@ -219,24 +265,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); @@ -249,17 +376,50 @@ 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 = 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); + action_group = gtk_action_group_new ("TestActions"); gtk_action_group_add_actions (action_group, entries, n_entries, @@ -274,12 +434,29 @@ main (int argc, char **argv) gtk_action_group_add_radio_actions (action_group, toolbar_entries, n_toolbar_entries, GTK_TOOLBAR_BOTH, - G_CALLBACK (radio_action), NULL); + 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");