Debian Codegraph @

demos/gtk-demo/modelbutton.c

raw
1 /* Model Button
2 *
3 * GtkModelButton is a button widget that is designed to be used with
4 * a GAction as model. The button will adjust its appearance according
5 * to the kind of action it is connected to.
6 *
7 * It is also possible to use GtkModelButton without a GAction. In this
8 * case, you should set the "role" attribute yourself, and connect to the
9 * "clicked" signal as you would for any other button.
10 *
11 * A common use of GtkModelButton is to implement menu-like content
12 * in popovers.
13 */
14
15 #include <gtk/gtk.h>
16
17 static void
18 tool_clicked (GtkButton *button)
19 {
20 gboolean active;
21
22 g_object_get (button, "active", &active, NULL);
23 g_object_set (button, "active", !active, NULL);
24 }
25
26 GtkWidget *
27 do_modelbutton (GtkWidget *do_widget)
28 {
29 static GtkWidget *window = NULL;
30 static GActionEntry win_entries[] = {
31 { "color", NULL, "s", "'red'", NULL },
32 { "chocolate", NULL, NULL, "true", NULL },
33 { "vanilla", NULL, NULL, "false", NULL },
34 { "sprinkles", NULL, NULL, NULL, NULL }
35 };
36
37 if (!window)
38 {
39 GtkBuilder *builder;
40 GActionGroup *actions;
41
42 builder = gtk_builder_new_from_resource ("/modelbutton/modelbutton.ui");
43 gtk_builder_add_callback_symbol (builder, "tool_clicked", G_CALLBACK (tool_clicked));
44 gtk_builder_connect_signals (builder, NULL);
45 window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
46 gtk_window_set_screen (GTK_WINDOW (window),
47 gtk_widget_get_screen (do_widget));
48 g_signal_connect (window, "destroy",
49 G_CALLBACK (gtk_widget_destroyed), &window);
50
51 actions = (GActionGroup*)g_simple_action_group_new ();
52 g_action_map_add_action_entries (G_ACTION_MAP (actions),
53 win_entries, G_N_ELEMENTS (win_entries),
54 window);
55 gtk_widget_insert_action_group (window, "win", actions);
56
57
58 g_object_unref (builder);
59 }
60
61 if (!gtk_widget_get_visible (window))
62 gtk_widget_show_all (window);
63 else
64 gtk_widget_destroy (window);
65
66
67 return window;
68 }
69