]> Pileus Git - ~andy/gtk/blob - tests/testlockbutton.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testlockbutton.c
1 /* testlockbutton.c
2  * Copyright (C) 2011 Red Hat, Inc.
3  * Authors: Matthias Clasen
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <gtk/gtk.h>
20 #include <gio/gio.h>
21
22 /* a fake permission implementation */
23
24 #define G_TYPE_TEST_PERMISSION      (g_test_permission_get_type ())
25 #define G_TEST_PERMISSION(inst)     (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
26                                      G_TYPE_TEST_PERMISSION,             \
27                                      GTestPermission))
28 #define G_IS_TEST_PERMISSION(inst)  (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
29                                      G_TYPE_TEST_PERMISSION))
30
31 typedef struct _GTestPermission GTestPermission;
32 typedef struct _GTestPermissionClass GTestPermissionClass;
33
34 struct _GTestPermission
35 {
36   GPermission parent;
37
38   gboolean success;
39 };
40
41 struct _GTestPermissionClass
42 {
43   GPermissionClass parent_class;
44 };
45
46 G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION)
47
48 static void
49 g_test_permission_init (GTestPermission *test)
50 {
51 }
52
53 static gboolean
54 update_allowed (GTestPermission  *test,
55                 gboolean          allowed,
56                 GError          **error)
57 {
58   gboolean can_acquire, can_release;
59
60   g_object_get (test,
61                 "can-acquire", &can_acquire,
62                 "can-release", &can_release,
63                 NULL);
64
65   if (test->success)
66     {
67       g_permission_impl_update (G_PERMISSION (test),
68                                 allowed, can_acquire, can_release);
69       return TRUE;
70     }
71   else
72     {
73       g_set_error_literal (error,
74                            G_IO_ERROR, G_IO_ERROR_FAILED, "Sorry, no luck");
75       return FALSE;
76     }
77 }
78
79 static gboolean
80 acquire (GPermission   *permission,
81          GCancellable  *cancellable,
82          GError       **error)
83 {
84   GTestPermission *test = G_TEST_PERMISSION (permission);
85   return update_allowed (test, TRUE, error);
86 }
87
88 static void
89 acquire_async (GPermission         *permission,
90                GCancellable        *cancellable,
91                GAsyncReadyCallback  callback,
92                gpointer             user_data)
93 {
94   GSimpleAsyncResult *result;
95   g_print ("GTestPermission::acquire_async\n");
96   result = g_simple_async_result_new ((GObject*)permission,
97                                       callback,
98                                       user_data,
99                                       acquire_async);
100   g_simple_async_result_complete (result);
101   g_object_unref (result);
102 }
103
104 gboolean
105 acquire_finish (GPermission   *permission,
106                 GAsyncResult  *result,
107                 GError       **error)
108 {
109   GTestPermission *test = G_TEST_PERMISSION (permission);
110   g_print ("GTestPermission::acquire_finish\n");
111   return update_allowed (test, TRUE, error);
112 }
113
114 static gboolean
115 release (GPermission   *permission,
116          GCancellable  *cancellable,
117          GError       **error)
118 {
119   GTestPermission *test = G_TEST_PERMISSION (permission);
120   return update_allowed (test, FALSE, error);
121 }
122
123 static void
124 release_async (GPermission         *permission,
125                GCancellable        *cancellable,
126                GAsyncReadyCallback  callback,
127                gpointer             user_data)
128 {
129   GSimpleAsyncResult *result;
130   result = g_simple_async_result_new ((GObject*)permission,
131                                       callback,
132                                       user_data,
133                                       acquire_async);
134   g_simple_async_result_complete (result);
135   g_object_unref (result);
136 }
137
138 gboolean
139 release_finish (GPermission   *permission,
140                 GAsyncResult  *result,
141                 GError       **error)
142 {
143   GTestPermission *test = G_TEST_PERMISSION (permission);
144   return update_allowed (test, FALSE, error);
145 }
146
147 static void
148 g_test_permission_class_init (GTestPermissionClass *class)
149 {
150   GPermissionClass *permission_class = G_PERMISSION_CLASS (class);
151
152   permission_class->acquire = acquire;
153   permission_class->acquire_async = acquire_async;
154   permission_class->acquire_finish = acquire_finish;
155
156   permission_class->release = release;
157   permission_class->release_async = release_async;
158   permission_class->release_finish = release_finish;
159 }
160
161 void
162 g_test_permission_set_success (GTestPermission *permission,
163                                gboolean         success)
164 {
165   permission->success = success;
166 }
167
168 static GtkWidget *allowed_button;
169 static GtkWidget *can_acquire_button;
170 static GtkWidget *can_release_button;
171 static GtkWidget *success_button;
172
173 static void
174 update_clicked (GtkButton *button, GtkLockButton *lockbutton)
175 {
176   GPermission *permission;
177   gboolean allowed, can_acquire, can_release;
178   gboolean success;
179
180   permission = gtk_lock_button_get_permission (lockbutton);
181
182   allowed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (allowed_button));
183   can_acquire = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_acquire_button));
184   can_release = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_release_button));
185   success = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (success_button));
186   g_permission_impl_update (permission, allowed, can_acquire, can_release);
187   g_test_permission_set_success (G_TEST_PERMISSION (permission), success);
188 }
189
190 static GtkWidget *content;
191
192 static void
193 permission_changed (GPermission *permission,
194                     GParamSpec  *pspec)
195 {
196   gboolean allowed, can_acquire, can_release;
197
198   g_object_get (permission,
199                 "allowed", &allowed,
200                 "can-acquire", &can_acquire,
201                 "can-release", &can_release,
202                 NULL);
203
204   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed);
205   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire);
206   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release);
207
208   gtk_widget_set_sensitive (content, allowed);
209 }
210
211 int
212 main (int argc, char *argv[])
213 {
214   GtkWidget *window;
215   GtkWidget *dialog;
216   GtkWidget *button;
217   GtkWidget *box;
218   GtkWidget *bbox;
219   GtkWidget *update;
220   GPermission *permission;
221
222   gtk_init (&argc, &argv);
223
224   permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL);
225
226   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
227   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
228   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
229
230   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
231   gtk_container_add (GTK_CONTAINER (window), box);
232
233   allowed_button = gtk_check_button_new_with_label ("Allowed");
234   gtk_container_add (GTK_CONTAINER (box), allowed_button);
235   can_acquire_button = gtk_check_button_new_with_label ("Can acquire");
236   gtk_container_add (GTK_CONTAINER (box), can_acquire_button);
237   can_release_button = gtk_check_button_new_with_label ("Can release");
238   gtk_container_add (GTK_CONTAINER (box), can_release_button);
239   success_button = gtk_check_button_new_with_label ("Will succeed");
240   gtk_container_add (GTK_CONTAINER (box), success_button);
241   update = gtk_button_new_with_label ("Update");
242   gtk_container_add (GTK_CONTAINER (box), update);
243   g_signal_connect (permission, "notify",
244                     G_CALLBACK (permission_changed), NULL);
245
246   button = gtk_lock_button_new (permission);
247
248   g_signal_connect (update, "clicked",
249                     G_CALLBACK (update_clicked), button);
250
251   dialog = gtk_dialog_new_with_buttons ("Dialog", NULL, 0,
252                                         "Close", GTK_RESPONSE_CLOSE,
253                                         "Some other action", GTK_RESPONSE_APPLY,
254                                         NULL);
255   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
256
257   bbox = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
258   gtk_container_add (GTK_CONTAINER (bbox), button);
259   gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox), button, TRUE);
260   gtk_button_box_set_child_non_homogeneous (GTK_BUTTON_BOX (bbox), button, TRUE);
261
262   content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
263   gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 1"));
264   gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 2"));
265   gtk_widget_set_sensitive (content, FALSE);
266
267   gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), content);
268
269   gtk_widget_show_all (window);
270   gtk_widget_show_all (dialog);
271
272   gtk_main ();
273
274   return 0;
275 }