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