]> Pileus Git - ~andy/gtk/blob - tests/testlogout.c
stylecontext: Do invalidation on first resize container
[~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
11 static void
12 inhibitor_toggled (GtkToggleButton *button, GtkApplication *app)
13 {
14   gboolean active;
15   const gchar *reason;
16   GtkApplicationInhibitFlags flags;
17   GtkWidget *toplevel;
18   guint cookie;
19
20   active = gtk_toggle_button_get_active (button);
21   reason = gtk_entry_get_text (GTK_ENTRY (inhibit_entry));
22
23   flags = 0;
24   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_logout)))
25     flags |= GTK_APPLICATION_INHIBIT_LOGOUT;
26   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_switch)))
27     flags |= GTK_APPLICATION_INHIBIT_SWITCH;
28   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_suspend)))
29     flags |= GTK_APPLICATION_INHIBIT_SUSPEND;
30   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_idle)))
31     flags |= GTK_APPLICATION_INHIBIT_IDLE;
32
33   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
34
35   if (active)
36     {
37       gchar *text;
38
39       g_print ("Calling gtk_application_inhibit: %d, '%s'\n", flags, reason);
40
41       cookie = gtk_application_inhibit (app, GTK_WINDOW (toplevel), flags, reason);
42       g_object_set_data (G_OBJECT (button), "cookie", GUINT_TO_POINTER (cookie));
43       if (cookie == 0)
44         {
45           g_signal_handlers_block_by_func (button, inhibitor_toggled, app);
46           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
47           g_signal_handlers_unblock_by_func (button, inhibitor_toggled, app);
48           active = FALSE;
49         }
50       else
51         {
52           text = g_strdup_printf ("%#x", cookie);
53           gtk_label_set_label (GTK_LABEL (inhibit_label), text);
54           g_free (text);
55         }
56     }
57   else
58     {
59       cookie = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (button), "cookie"));
60       g_print ("Calling gtk_application_uninhibit: %#x\n", cookie);
61       gtk_application_uninhibit (app, cookie);
62       gtk_label_set_label (GTK_LABEL (inhibit_label), "");
63     }
64
65   gtk_widget_set_sensitive (inhibit_entry, !active);
66   gtk_widget_set_sensitive (inhibit_logout, !active);
67   gtk_widget_set_sensitive (inhibit_switch, !active);
68   gtk_widget_set_sensitive (inhibit_suspend, !active);
69   gtk_widget_set_sensitive (inhibit_idle, !active);
70 }
71
72 static void
73 activate (GtkApplication *app,
74           gpointer        data)
75 {
76   GtkWidget *box;
77   GtkWidget *separator;
78   GtkWidget *grid;
79   GtkWidget *button;
80   GtkWidget *label;
81
82   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
83
84   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
85   g_object_set (box, "margin", 12, NULL);
86   gtk_container_add (GTK_CONTAINER (win), box);
87
88   grid = gtk_grid_new ();
89   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
90   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
91
92   gtk_container_add (GTK_CONTAINER (box), grid);
93
94   label = gtk_label_new ("Inhibitor");
95   gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
96
97   inhibit_label = gtk_label_new ("");
98   gtk_grid_attach (GTK_GRID (grid), inhibit_label, 1, 0, 1, 1);
99
100   inhibit_logout = gtk_check_button_new_with_label ("Logout");
101   gtk_grid_attach (GTK_GRID (grid), inhibit_logout, 1, 1, 1, 1);
102
103   inhibit_switch = gtk_check_button_new_with_label ("User switching");
104   gtk_grid_attach (GTK_GRID (grid), inhibit_switch, 1, 2, 1, 1);
105
106   inhibit_suspend = gtk_check_button_new_with_label ("Suspend");
107   gtk_grid_attach (GTK_GRID (grid), inhibit_suspend, 1, 4, 1, 1);
108
109   inhibit_idle = gtk_check_button_new_with_label ("Idle");
110   gtk_grid_attach (GTK_GRID (grid), inhibit_idle, 1, 5, 1, 1);
111
112   inhibit_entry = gtk_entry_new ();
113   gtk_grid_attach (GTK_GRID (grid), inhibit_entry, 1, 6, 1, 1);
114
115   button = gtk_toggle_button_new_with_label ("Inhibit");
116   g_signal_connect (button, "toggled",
117                     G_CALLBACK (inhibitor_toggled), app);
118   gtk_grid_attach (GTK_GRID (grid), button, 2, 6, 1, 1);
119
120   separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
121   gtk_container_add (GTK_CONTAINER (box), separator);
122
123   grid = gtk_grid_new ();
124   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
125   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
126
127   gtk_widget_show_all (win);
128
129   gtk_application_add_window (app, GTK_WINDOW (win));
130 }
131
132 static void
133 quit (GtkApplication *app,
134       gpointer        data)
135 {
136   g_print ("Received quit\n");
137   gtk_widget_destroy (win);
138 }
139
140 int
141 main (int argc, char *argv[])
142 {
143   GtkApplication *app;
144
145   app = gtk_application_new ("org.gtk.Test.session", 0);
146   g_object_set (app, "register-session", TRUE, NULL);
147
148   g_signal_connect (app, "activate",
149                     G_CALLBACK (activate), NULL);
150   g_signal_connect (app, "quit",
151                     G_CALLBACK (quit), NULL);
152
153   g_application_run (G_APPLICATION (app), argc, argv);
154
155   return 0;
156 }