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