]> Pileus Git - ~andy/gtk/blob - tests/testlogout.c
Fix a few problems with custom color replacement
[~andy/gtk] / tests / testlogout.c
1 #include <gtk/gtk.h>
2
3 static GtkWidget *win;
4 static GtkWidget *inhibit_entry;
5 static GtkWidget *inhibit_logout;
6 static GtkWidget *inhibit_switch;
7 static GtkWidget *inhibit_suspend;
8 static GtkWidget *inhibit_idle;
9 static GtkWidget *inhibit_label;
10 static GtkWidget *end_combo;
11 static GtkWidget *end_confirm;
12
13 static void
14 end_session (GtkButton *button, GtkApplication *app)
15 {
16   const gchar *id;
17   GtkApplicationEndSessionStyle style;
18   gboolean confirm;
19
20   id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (end_combo));
21   if (g_strcmp0 (id, "logout") == 0)
22     style = GTK_APPLICATION_LOGOUT;
23   else if (g_strcmp0 (id, "reboot") == 0)
24     style = GTK_APPLICATION_REBOOT;
25   else if (g_strcmp0 (id, "shutdown") == 0)
26     style = GTK_APPLICATION_SHUTDOWN;
27   else
28     style = GTK_APPLICATION_LOGOUT;
29
30   confirm = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (end_confirm));
31
32   g_print ("Calling gtk_application_end_session: %d, %d\n", style, confirm);
33
34   gtk_application_end_session (app, style, confirm);
35 }
36
37 static void
38 inhibitor_toggled (GtkToggleButton *button, GtkApplication *app)
39 {
40   gboolean active;
41   const gchar *reason;
42   GtkApplicationInhibitFlags flags;
43   GtkWidget *toplevel;
44   guint cookie;
45
46   active = gtk_toggle_button_get_active (button);
47   reason = gtk_entry_get_text (GTK_ENTRY (inhibit_entry));
48
49   flags = 0;
50   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_logout)))
51     flags |= GTK_APPLICATION_INHIBIT_LOGOUT;
52   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_switch)))
53     flags |= GTK_APPLICATION_INHIBIT_SWITCH;
54   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_suspend)))
55     flags |= GTK_APPLICATION_INHIBIT_SUSPEND;
56   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_idle)))
57     flags |= GTK_APPLICATION_INHIBIT_IDLE;
58
59   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
60
61   if (active)
62     {
63       gchar *text;
64
65       g_print ("Calling gtk_application_inhibit: %d, '%s'\n", flags, reason);
66
67       cookie = gtk_application_inhibit (app, GTK_WINDOW (toplevel), flags, reason);
68       g_object_set_data (G_OBJECT (button), "cookie", GUINT_TO_POINTER (cookie));
69       if (cookie == 0)
70         {
71           g_signal_handlers_block_by_func (button, inhibitor_toggled, app);
72           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
73           g_signal_handlers_unblock_by_func (button, inhibitor_toggled, app);
74           active = FALSE;
75         }
76       else
77         {
78           text = g_strdup_printf ("%#x", cookie);
79           gtk_label_set_label (GTK_LABEL (inhibit_label), text);
80           g_free (text);
81         }
82     }
83   else
84     {
85       cookie = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (button), "cookie"));
86       g_print ("Calling gtk_application_uninhibit: %#x\n", cookie);
87       gtk_application_uninhibit (app, cookie);
88       gtk_label_set_label (GTK_LABEL (inhibit_label), "");
89     }
90
91   gtk_widget_set_sensitive (inhibit_entry, !active);
92   gtk_widget_set_sensitive (inhibit_logout, !active);
93   gtk_widget_set_sensitive (inhibit_switch, !active);
94   gtk_widget_set_sensitive (inhibit_suspend, !active);
95   gtk_widget_set_sensitive (inhibit_idle, !active);
96 }
97
98 static void
99 activate (GtkApplication *app,
100           gpointer        data)
101 {
102   GtkWidget *box;
103   GtkWidget *separator;
104   GtkWidget *grid;
105   GtkWidget *button;
106   GtkWidget *label;
107
108   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
109
110   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
111   g_object_set (box, "margin", 12, NULL);
112   gtk_container_add (GTK_CONTAINER (win), box);
113
114   grid = gtk_grid_new ();
115   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
116   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
117
118   gtk_container_add (GTK_CONTAINER (box), grid);
119
120   label = gtk_label_new ("Inhibitor");
121   gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
122
123   inhibit_label = gtk_label_new ("");
124   gtk_grid_attach (GTK_GRID (grid), inhibit_label, 1, 0, 1, 1);
125
126   inhibit_logout = gtk_check_button_new_with_label ("Logout");
127   gtk_grid_attach (GTK_GRID (grid), inhibit_logout, 1, 1, 1, 1);
128
129   inhibit_switch = gtk_check_button_new_with_label ("User switching");
130   gtk_grid_attach (GTK_GRID (grid), inhibit_switch, 1, 2, 1, 1);
131
132   inhibit_suspend = gtk_check_button_new_with_label ("Suspend");
133   gtk_grid_attach (GTK_GRID (grid), inhibit_suspend, 1, 4, 1, 1);
134
135   inhibit_idle = gtk_check_button_new_with_label ("Idle");
136   gtk_grid_attach (GTK_GRID (grid), inhibit_idle, 1, 5, 1, 1);
137
138   inhibit_entry = gtk_entry_new ();
139   gtk_grid_attach (GTK_GRID (grid), inhibit_entry, 1, 6, 1, 1);
140
141   button = gtk_toggle_button_new_with_label ("Inhibit");
142   g_signal_connect (button, "toggled",
143                     G_CALLBACK (inhibitor_toggled), app);
144   gtk_grid_attach (GTK_GRID (grid), button, 2, 6, 1, 1);
145
146   separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
147   gtk_container_add (GTK_CONTAINER (box), separator);
148
149   grid = gtk_grid_new ();
150   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
151   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
152
153   gtk_container_add (GTK_CONTAINER (box), grid);
154
155   end_combo = gtk_combo_box_text_new ();
156   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "logout", "Logout");
157   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "reboot", "Reboot");
158   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "shutdown", "Shutdown");
159   gtk_combo_box_set_active_id (GTK_COMBO_BOX (end_combo), "logout");
160   gtk_grid_attach (GTK_GRID (grid), end_combo, 0, 0, 1, 1);
161
162   end_confirm = gtk_check_button_new_with_label ("Confirm");
163   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (end_confirm), TRUE);
164   gtk_grid_attach (GTK_GRID (grid), end_confirm, 1, 0, 1, 1);
165
166   button = gtk_button_new_with_label ("Go");
167   g_signal_connect (button, "clicked",
168                     G_CALLBACK (end_session), app);
169
170   gtk_grid_attach (GTK_GRID (grid), button, 2, 0, 1, 1);
171
172   gtk_widget_show_all (win);
173
174   gtk_application_add_window (app, GTK_WINDOW (win));
175 }
176
177 static void
178 quit (GtkApplication *app,
179       gpointer        data)
180 {
181   g_print ("Received quit\n");
182   gtk_widget_destroy (win);
183 }
184
185 int
186 main (int argc, char *argv[])
187 {
188   GtkApplication *app;
189
190   app = gtk_application_new ("org.gtk.Test.session", 0);
191   g_object_set (app, "register-session", TRUE, NULL);
192
193   g_signal_connect (app, "activate",
194                     G_CALLBACK (activate), NULL);
195   g_signal_connect (app, "quit",
196                     G_CALLBACK (quit), NULL);
197
198   g_application_run (G_APPLICATION (app), argc, argv);
199
200   return 0;
201 }