]> Pileus Git - ~andy/gtk/blob - tests/testappchooser.c
tests: remove radio mode from the test
[~andy/gtk] / tests / testappchooser.c
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24 #include <gtk/gtk.h>
25
26 static GtkWidget *toplevel;
27 static GFile *file;
28 static GtkWidget *grid, *file_l, *open;
29 static GtkWidget *radio_file, *radio_content, *dialog;
30 static GtkWidget *app_chooser_widget;
31 static GtkWidget *recommended, *fallback, *other, *all;
32
33 static void
34 dialog_response (GtkDialog *d,
35                  gint response_id,
36                  gpointer user_data)
37 {
38   GAppInfo *app_info;
39   const gchar *name;
40
41   g_print ("Response: %d\n", response_id);
42
43   if (response_id == GTK_RESPONSE_OK)
44     {
45       app_info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (d));
46       name = g_app_info_get_name (app_info);
47       g_print ("Application selected: %s\n", name);
48
49       g_object_unref (app_info);
50     }
51
52   gtk_widget_destroy (GTK_WIDGET (d));
53   dialog = NULL;
54 }
55
56 static void
57 bind_props (void)
58 {
59   g_object_bind_property (recommended, "active",
60                           app_chooser_widget, "show-recommended",
61                           G_BINDING_SYNC_CREATE);
62   g_object_bind_property (fallback, "active",
63                           app_chooser_widget, "show-fallback",
64                           G_BINDING_SYNC_CREATE);
65   g_object_bind_property (other, "active",
66                           app_chooser_widget, "show-other",
67                           G_BINDING_SYNC_CREATE);
68   g_object_bind_property (all, "active",
69                           app_chooser_widget, "show-all",
70                           G_BINDING_SYNC_CREATE);
71 }
72
73 static void
74 prepare_dialog (void)
75 {
76   gboolean use_file = FALSE;
77   gchar *content_type = NULL;
78
79   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio_file)))
80     use_file = TRUE;
81   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radio_content)))
82     use_file = FALSE;
83
84   if (use_file)
85     {
86       dialog = gtk_app_chooser_dialog_new (GTK_WINDOW (toplevel),
87                                          0, file);
88     }
89   else
90     {
91       GFileInfo *info;
92
93       info = g_file_query_info (file,
94                                 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
95                                 0, NULL, NULL);
96       content_type = g_strdup (g_file_info_get_content_type (info));
97
98       g_object_unref (info);
99
100       dialog = gtk_app_chooser_dialog_new_for_content_type (GTK_WINDOW (toplevel),
101                                                           0, content_type);
102     }
103
104   g_signal_connect (dialog, "response",
105                     G_CALLBACK (dialog_response), NULL);
106
107   g_free (content_type);
108
109   app_chooser_widget = gtk_app_chooser_dialog_get_widget (GTK_APP_CHOOSER_DIALOG (dialog));
110   bind_props ();
111 }
112
113 static void
114 display_dialog (void)
115 {
116   if (dialog == NULL)
117     prepare_dialog ();
118   
119   gtk_widget_show (dialog);
120 }
121
122 static void
123 button_clicked (GtkButton *b,
124                 gpointer user_data)
125 {
126   GtkWidget *w;
127   gchar *path;
128
129   w = gtk_file_chooser_dialog_new ("Select file",
130                                    GTK_WINDOW (toplevel),
131                                    GTK_FILE_CHOOSER_ACTION_OPEN,
132                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
133                                    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
134                                    NULL);
135
136   gtk_dialog_run (GTK_DIALOG (w));
137   file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (w));
138   path = g_file_get_path (file);
139   gtk_button_set_label (GTK_BUTTON (file_l), path);
140
141   gtk_widget_destroy (w);
142
143   gtk_widget_set_sensitive (open, TRUE);
144
145   g_free (path);
146 }
147
148 int
149 main (int argc,
150       char **argv)
151 {
152   GtkWidget *w1;
153   gchar *path;
154
155   g_type_init ();
156   gtk_init (&argc, &argv);
157
158   toplevel = gtk_window_new (GTK_WINDOW_TOPLEVEL);
159   gtk_container_set_border_width (GTK_CONTAINER (toplevel), 12);
160   grid = gtk_grid_new ();
161
162   w1 = gtk_label_new ("File:");
163   gtk_widget_set_halign (w1, GTK_ALIGN_START);
164   gtk_grid_attach (GTK_GRID (grid),
165                    w1, 0, 0, 1, 1);
166
167   file_l = gtk_button_new ();
168   path = g_build_filename (g_get_current_dir (), "apple-red.png", NULL);
169   file = g_file_new_for_path (path);
170   gtk_button_set_label (GTK_BUTTON (file_l), path);
171   g_free (path);
172
173   gtk_widget_set_halign (file_l, GTK_ALIGN_START);
174   gtk_grid_attach_next_to (GTK_GRID (grid), file_l,
175                            w1, GTK_POS_RIGHT, 1, 1);
176   g_signal_connect (file_l, "clicked",
177                     G_CALLBACK (button_clicked), NULL);
178
179   radio_file = gtk_radio_button_new_with_label (NULL, "Use GFile");
180   radio_content = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (radio_file),
181                                                                "Use content type");
182
183   gtk_grid_attach (GTK_GRID (grid), radio_file,
184                    0, 1, 1, 1);
185   gtk_grid_attach_next_to (GTK_GRID (grid), radio_content,
186                            radio_file, GTK_POS_BOTTOM, 1, 1);
187
188   open = gtk_button_new_with_label ("Trigger App Chooser dialog");
189   gtk_grid_attach_next_to (GTK_GRID (grid), open,
190                            radio_content, GTK_POS_BOTTOM, 1, 1);
191
192   recommended = gtk_check_button_new_with_label ("Show recommended");
193   gtk_grid_attach_next_to (GTK_GRID (grid), recommended,
194                            open, GTK_POS_BOTTOM, 1, 1);
195   g_object_set (recommended, "active", TRUE, NULL);
196
197   fallback = gtk_check_button_new_with_label ("Show fallback");
198   gtk_grid_attach_next_to (GTK_GRID (grid), fallback,
199                            recommended, GTK_POS_RIGHT, 1, 1);
200
201   other = gtk_check_button_new_with_label ("Show other");
202   gtk_grid_attach_next_to (GTK_GRID (grid), other,
203                            fallback, GTK_POS_RIGHT, 1, 1);
204
205   all = gtk_check_button_new_with_label ("Show all");
206   gtk_grid_attach_next_to (GTK_GRID (grid), all,
207                            other, GTK_POS_RIGHT, 1, 1);
208
209   prepare_dialog ();
210   g_signal_connect (open, "clicked",
211                     G_CALLBACK (display_dialog), NULL);
212
213   gtk_container_add (GTK_CONTAINER (toplevel), grid);
214
215   gtk_widget_show_all (toplevel);
216   g_signal_connect (toplevel, "delete-event",
217                     G_CALLBACK (gtk_main_quit), NULL);
218
219   gtk_main ();
220
221   return EXIT_SUCCESS;
222 }