]> Pileus Git - ~andy/gtk/blob - tests/testlockbutton.c
win32: correctly handle difference between tab_pos and gap_side when drawing an exten...
[~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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gtk/gtk.h>
22 #include <gio/gio.h>
23
24 /* a fake permission implementation */
25
26 #define G_TYPE_TEST_PERMISSION      (g_test_permission_get_type ())
27 #define G_TEST_PERMISSION(inst)     (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
28                                      G_TYPE_TEST_PERMISSION,             \
29                                      GTestPermission))
30 #define G_IS_TEST_PERMISSION(inst)  (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
31                                      G_TYPE_TEST_PERMISSION))
32
33 typedef struct _GTestPermission GTestPermission;
34 typedef struct _GTestPermissionClass GTestPermissionClass;
35
36 struct _GTestPermission
37 {
38   GPermission parent;
39
40   gboolean success;
41 };
42
43 struct _GTestPermissionClass
44 {
45   GPermissionClass parent_class;
46 };
47
48 G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION)
49
50 static void
51 g_test_permission_init (GTestPermission *test)
52 {
53 }
54
55 static gboolean
56 update_allowed (GTestPermission  *test,
57                 gboolean          allowed,
58                 GError          **error)
59 {
60   gboolean can_acquire, can_release;
61
62   g_object_get (test,
63                 "can-acquire", &can_acquire,
64                 "can-release", &can_release,
65                 NULL);
66
67   if (test->success)
68     {
69       g_permission_impl_update (G_PERMISSION (test),
70                                 allowed, can_acquire, can_release);
71       return TRUE;
72     }
73   else
74     {
75       g_set_error_literal (error,
76                            G_IO_ERROR, G_IO_ERROR_FAILED, "Sorry, no luck");
77       return FALSE;
78     }
79 }
80
81 static gboolean
82 acquire (GPermission   *permission,
83          GCancellable  *cancellable,
84          GError       **error)
85 {
86   GTestPermission *test = G_TEST_PERMISSION (permission);
87   return update_allowed (test, TRUE, error);
88 }
89
90 static void
91 acquire_async (GPermission         *permission,
92                GCancellable        *cancellable,
93                GAsyncReadyCallback  callback,
94                gpointer             user_data)
95 {
96   GSimpleAsyncResult *result;
97   g_print ("GTestPermission::acquire_async\n");
98   result = g_simple_async_result_new ((GObject*)permission,
99                                       callback,
100                                       user_data,
101                                       acquire_async);
102   g_simple_async_result_complete (result);
103   g_object_unref (result);
104 }
105
106 gboolean
107 acquire_finish (GPermission   *permission,
108                 GAsyncResult  *result,
109                 GError       **error)
110 {
111   GTestPermission *test = G_TEST_PERMISSION (permission);
112   g_print ("GTestPermission::acquire_finish\n");
113   return update_allowed (test, TRUE, error);
114 }
115
116 static gboolean
117 release (GPermission   *permission,
118          GCancellable  *cancellable,
119          GError       **error)
120 {
121   GTestPermission *test = G_TEST_PERMISSION (permission);
122   return update_allowed (test, FALSE, error);
123 }
124
125 static void
126 release_async (GPermission         *permission,
127                GCancellable        *cancellable,
128                GAsyncReadyCallback  callback,
129                gpointer             user_data)
130 {
131   GSimpleAsyncResult *result;
132   result = g_simple_async_result_new ((GObject*)permission,
133                                       callback,
134                                       user_data,
135                                       acquire_async);
136   g_simple_async_result_complete (result);
137   g_object_unref (result);
138 }
139
140 gboolean
141 release_finish (GPermission   *permission,
142                 GAsyncResult  *result,
143                 GError       **error)
144 {
145   GTestPermission *test = G_TEST_PERMISSION (permission);
146   return update_allowed (test, FALSE, error);
147 }
148
149 static void
150 g_test_permission_class_init (GTestPermissionClass *class)
151 {
152   GPermissionClass *permission_class = G_PERMISSION_CLASS (class);
153
154   permission_class->acquire = acquire;
155   permission_class->acquire_async = acquire_async;
156   permission_class->acquire_finish = acquire_finish;
157
158   permission_class->release = release;
159   permission_class->release_async = release_async;
160   permission_class->release_finish = release_finish;
161 }
162
163 void
164 g_test_permission_set_success (GTestPermission *permission,
165                                gboolean         success)
166 {
167   permission->success = success;
168 }
169
170 static GtkWidget *allowed_button;
171 static GtkWidget *can_acquire_button;
172 static GtkWidget *can_release_button;
173 static GtkWidget *success_button;
174
175 static void
176 update_clicked (GtkButton *button, GtkLockButton *lockbutton)
177 {
178   GPermission *permission;
179   gboolean allowed, can_acquire, can_release;
180   gboolean success;
181
182   permission = gtk_lock_button_get_permission (lockbutton);
183
184   allowed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (allowed_button));
185   can_acquire = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_acquire_button));
186   can_release = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_release_button));
187   success = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (success_button));
188   g_permission_impl_update (permission, allowed, can_acquire, can_release);
189   g_test_permission_set_success (G_TEST_PERMISSION (permission), success);
190 }
191
192 static GtkWidget *content;
193
194 static void
195 permission_changed (GPermission *permission,
196                     GParamSpec  *pspec)
197 {
198   gboolean allowed, can_acquire, can_release;
199
200   g_object_get (permission,
201                 "allowed", &allowed,
202                 "can-acquire", &can_acquire,
203                 "can-release", &can_release,
204                 NULL);
205
206   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed);
207   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire);
208   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release);
209
210   gtk_widget_set_sensitive (content, allowed);
211 }
212
213 int
214 main (int argc, char *argv[])
215 {
216   GtkWidget *window;
217   GtkWidget *dialog;
218   GtkWidget *button;
219   GtkWidget *box;
220   GtkWidget *bbox;
221   GtkWidget *update;
222   GPermission *permission;
223
224   gtk_init (&argc, &argv);
225
226   permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL);
227
228   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
229   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
230   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
231
232   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
233   gtk_container_add (GTK_CONTAINER (window), box);
234
235   allowed_button = gtk_check_button_new_with_label ("Allowed");
236   gtk_container_add (GTK_CONTAINER (box), allowed_button);
237   can_acquire_button = gtk_check_button_new_with_label ("Can acquire");
238   gtk_container_add (GTK_CONTAINER (box), can_acquire_button);
239   can_release_button = gtk_check_button_new_with_label ("Can release");
240   gtk_container_add (GTK_CONTAINER (box), can_release_button);
241   success_button = gtk_check_button_new_with_label ("Will succeed");
242   gtk_container_add (GTK_CONTAINER (box), success_button);
243   update = gtk_button_new_with_label ("Update");
244   gtk_container_add (GTK_CONTAINER (box), update);
245   g_signal_connect (permission, "notify",
246                     G_CALLBACK (permission_changed), NULL);
247
248   button = gtk_lock_button_new (permission);
249
250   g_signal_connect (update, "clicked",
251                     G_CALLBACK (update_clicked), button);
252
253   dialog = gtk_dialog_new_with_buttons ("Dialog", NULL, 0,
254                                         "Close", GTK_RESPONSE_CLOSE,
255                                         "Some other action", GTK_RESPONSE_APPLY,
256                                         NULL);
257   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
258
259   bbox = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
260   gtk_container_add (GTK_CONTAINER (bbox), button);
261   gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox), button, TRUE);
262   gtk_button_box_set_child_non_homogeneous (GTK_BUTTON_BOX (bbox), button, TRUE);
263
264   content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
265   gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 1"));
266   gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 2"));
267   gtk_widget_set_sensitive (content, FALSE);
268
269   gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), content);
270
271   gtk_widget_show_all (window);
272   gtk_widget_show_all (dialog);
273
274   gtk_main ();
275
276   return 0;
277 }