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