1/* testappchooser.c
2 * Copyright (C) 2010 Red Hat, Inc.
3 * Authors: Cosimo Cecchi
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 "config.h"
20
21#include <stdlib.h>
22#include <gtk/gtk.h>
23
24static GtkWidget *toplevel;
25static GFile *file;
26static GtkWidget *grid, *file_l, *open;
27static GtkWidget *radio_file, *radio_content, *dialog;
28static GtkWidget *app_chooser_widget;
29static GtkWidget *def, *recommended, *fallback, *other, *all;
30
31static void
32dialog_response (GtkDialog *d,
33 int response_id,
34 gpointer user_data)
35{
36 GAppInfo *app_info;
37 const char *name;
38
39 g_print (format: "Response: %d\n", response_id);
40
41 if (response_id == GTK_RESPONSE_OK)
42 {
43 app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (d));
44 if (app_info)
45 {
46 name = g_app_info_get_name (appinfo: app_info);
47 g_print (format: "Application selected: %s\n", name);
48 g_object_unref (object: app_info);
49 }
50 else
51 g_print (format: "No application selected\n");
52 }
53
54 gtk_window_destroy (GTK_WINDOW (d));
55 dialog = NULL;
56}
57
58static void
59bind_props (void)
60{
61 g_object_bind_property (source: def, source_property: "active",
62 target: app_chooser_widget, target_property: "show-default",
63 flags: G_BINDING_SYNC_CREATE);
64 g_object_bind_property (source: recommended, source_property: "active",
65 target: app_chooser_widget, target_property: "show-recommended",
66 flags: G_BINDING_SYNC_CREATE);
67 g_object_bind_property (source: fallback, source_property: "active",
68 target: app_chooser_widget, target_property: "show-fallback",
69 flags: G_BINDING_SYNC_CREATE);
70 g_object_bind_property (source: other, source_property: "active",
71 target: app_chooser_widget, target_property: "show-other",
72 flags: G_BINDING_SYNC_CREATE);
73 g_object_bind_property (source: all, source_property: "active",
74 target: app_chooser_widget, target_property: "show-all",
75 flags: G_BINDING_SYNC_CREATE);
76}
77
78static void
79prepare_dialog (void)
80{
81 gboolean use_file = FALSE;
82 char *content_type = NULL;
83
84 if (gtk_check_button_get_active (GTK_CHECK_BUTTON (radio_file)))
85 use_file = TRUE;
86 else if (gtk_check_button_get_active (GTK_CHECK_BUTTON (radio_content)))
87 use_file = FALSE;
88
89 if (use_file)
90 {
91 dialog = gtk_app_chooser_dialog_new (GTK_WINDOW (toplevel), flags: 0, file);
92 }
93 else
94 {
95 GFileInfo *info;
96
97 info = g_file_query_info (file,
98 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
99 flags: 0, NULL, NULL);
100 content_type = g_strdup (str: g_file_info_get_content_type (info));
101
102 g_object_unref (object: info);
103
104 dialog = gtk_app_chooser_dialog_new_for_content_type (GTK_WINDOW (toplevel),
105 flags: 0, content_type);
106 }
107
108 gtk_app_chooser_dialog_set_heading (GTK_APP_CHOOSER_DIALOG (dialog), heading: "Select one already, you <i>fool</i>");
109
110 g_signal_connect (dialog, "response",
111 G_CALLBACK (dialog_response), NULL);
112
113 g_free (mem: content_type);
114
115 app_chooser_widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
116 bind_props ();
117}
118
119static void
120display_dialog (void)
121{
122 if (dialog == NULL)
123 prepare_dialog ();
124
125 gtk_widget_show (widget: dialog);
126}
127
128static void
129on_open_response (GtkWidget *file_chooser,
130 int response)
131{
132 if (response == GTK_RESPONSE_ACCEPT)
133 {
134 char *path;
135
136 file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser));
137 path = g_file_get_path (file);
138
139 gtk_button_set_label (GTK_BUTTON (file_l), label: path);
140
141 g_free (mem: path);
142 }
143
144 gtk_window_destroy (GTK_WINDOW (file_chooser));
145
146 gtk_widget_set_sensitive (widget: open, TRUE);
147}
148
149static void
150button_clicked (GtkButton *b,
151 gpointer user_data)
152{
153 GtkWidget *w;
154
155 w = gtk_file_chooser_dialog_new (title: "Select file",
156 GTK_WINDOW (toplevel),
157 action: GTK_FILE_CHOOSER_ACTION_OPEN,
158 first_button_text: "_Cancel", GTK_RESPONSE_CANCEL,
159 "_Open", GTK_RESPONSE_ACCEPT,
160 NULL);
161
162 gtk_window_set_modal (GTK_WINDOW (w), TRUE);
163
164 g_signal_connect (w, "response",
165 G_CALLBACK (on_open_response),
166 NULL);
167
168 gtk_window_present (GTK_WINDOW (w));
169}
170
171static void
172quit_cb (GtkWidget *widget,
173 gpointer data)
174{
175 gboolean *done = data;
176
177 *done = TRUE;
178
179 g_main_context_wakeup (NULL);
180}
181
182int
183main (int argc, char **argv)
184{
185 GtkWidget *w1;
186 char *path;
187 gboolean done = FALSE;
188
189 gtk_init ();
190
191 toplevel = gtk_window_new ();
192 grid = gtk_grid_new ();
193
194 w1 = gtk_label_new (str: "File:");
195 gtk_widget_set_halign (widget: w1, align: GTK_ALIGN_START);
196 gtk_grid_attach (GTK_GRID (grid),
197 child: w1, column: 0, row: 0, width: 1, height: 1);
198
199 file_l = gtk_button_new ();
200 path = g_build_filename (GTK_SRCDIR, "apple-red.png", NULL);
201 file = g_file_new_for_path (path);
202 gtk_button_set_label (GTK_BUTTON (file_l), label: path);
203 g_free (mem: path);
204
205 gtk_widget_set_halign (widget: file_l, align: GTK_ALIGN_START);
206 gtk_grid_attach_next_to (GTK_GRID (grid), child: file_l,
207 sibling: w1, side: GTK_POS_RIGHT, width: 3, height: 1);
208 g_signal_connect (file_l, "clicked",
209 G_CALLBACK (button_clicked), NULL);
210
211 radio_file = gtk_check_button_new_with_label (label: "Use GFile");
212 radio_content = gtk_check_button_new_with_label (label: "Use content type");
213 gtk_check_button_set_group (GTK_CHECK_BUTTON (radio_content), GTK_CHECK_BUTTON (radio_file));
214 gtk_check_button_set_group (GTK_CHECK_BUTTON (radio_file), GTK_CHECK_BUTTON (radio_content));
215 gtk_check_button_set_active (GTK_CHECK_BUTTON (radio_file), TRUE);
216
217 gtk_grid_attach (GTK_GRID (grid), child: radio_file,
218 column: 0, row: 1, width: 1, height: 1);
219 gtk_grid_attach_next_to (GTK_GRID (grid), child: radio_content,
220 sibling: radio_file, side: GTK_POS_BOTTOM, width: 1, height: 1);
221
222 open = gtk_button_new_with_label (label: "Trigger App Chooser dialog");
223 gtk_grid_attach_next_to (GTK_GRID (grid), child: open,
224 sibling: radio_content, side: GTK_POS_BOTTOM, width: 1, height: 1);
225
226 recommended = gtk_check_button_new_with_label (label: "Show recommended");
227 gtk_grid_attach_next_to (GTK_GRID (grid), child: recommended,
228 sibling: open, side: GTK_POS_BOTTOM, width: 1, height: 1);
229 g_object_set (object: recommended, first_property_name: "active", TRUE, NULL);
230
231 fallback = gtk_check_button_new_with_label (label: "Show fallback");
232 gtk_grid_attach_next_to (GTK_GRID (grid), child: fallback,
233 sibling: recommended, side: GTK_POS_RIGHT, width: 1, height: 1);
234
235 other = gtk_check_button_new_with_label (label: "Show other");
236 gtk_grid_attach_next_to (GTK_GRID (grid), child: other,
237 sibling: fallback, side: GTK_POS_RIGHT, width: 1, height: 1);
238
239 all = gtk_check_button_new_with_label (label: "Show all");
240 gtk_grid_attach_next_to (GTK_GRID (grid), child: all,
241 sibling: other, side: GTK_POS_RIGHT, width: 1, height: 1);
242
243 def = gtk_check_button_new_with_label (label: "Show default");
244 gtk_grid_attach_next_to (GTK_GRID (grid), child: def,
245 sibling: all, side: GTK_POS_RIGHT, width: 1, height: 1);
246
247 g_object_set (object: recommended, first_property_name: "active", TRUE, NULL);
248 prepare_dialog ();
249 g_signal_connect (open, "clicked",
250 G_CALLBACK (display_dialog), NULL);
251
252 gtk_window_set_child (GTK_WINDOW (toplevel), child: grid);
253
254 gtk_widget_show (widget: toplevel);
255 g_signal_connect (toplevel, "destroy", G_CALLBACK (quit_cb), &done);
256
257 while (!done)
258 g_main_context_iteration (NULL, TRUE);
259
260 return EXIT_SUCCESS;
261}
262

source code of gtk/tests/testappchooser.c