1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 /* GTK+: gtkfilechooserbutton.c
5 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
34 #include "prop-editor.h"
36 static gchar *backend = "gtk+";
37 static gboolean rtl = FALSE;
38 static GOptionEntry entries[] = {
39 { "backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "The filesystem backend to use.", "gtk+" },
40 { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &rtl, "Force right-to-left layout.", NULL },
44 static gchar *gtk_src_dir = NULL;
48 win_style_set_cb (GtkWidget *win)
50 GtkWidget *content_area, *action_area;
52 content_area = gtk_dialog_get_content_area (GTK_DIALOG (win));
53 action_area = gtk_dialog_get_action_area (GTK_DIALOG (win));
55 gtk_container_set_border_width (GTK_CONTAINER (content_area), 12);
56 gtk_box_set_spacing (GTK_BOX (content_area), 24);
57 gtk_container_set_border_width (GTK_CONTAINER (action_area), 0);
58 gtk_box_set_spacing (GTK_BOX (action_area), 6);
62 delete_event_cb (GtkWidget *editor,
66 gtk_widget_hide (editor);
73 properties_button_clicked_cb (GtkWidget *button,
78 editor = g_object_get_data (entry, "properties-dialog");
82 editor = create_prop_editor (G_OBJECT (entry), G_TYPE_INVALID);
83 gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
84 gtk_window_set_transient_for (GTK_WINDOW (editor),
85 GTK_WINDOW (gtk_widget_get_toplevel (button)));
86 g_signal_connect (editor, "delete-event", G_CALLBACK (delete_event_cb), NULL);
87 g_object_set_data (entry, "properties-dialog", editor);
90 gtk_window_present (GTK_WINDOW (editor));
95 print_selected_path_clicked_cb (GtkWidget *button,
98 gchar *folder, *filename;
100 folder = gtk_file_chooser_get_current_folder (user_data);
101 filename = gtk_file_chooser_get_filename (user_data);
102 g_message ("Currently Selected:\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
109 add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
114 if (!gtk_file_chooser_add_shortcut_folder (user_data, gtk_src_dir, &err))
116 g_message ("Couldn't add `%s' as shortcut folder: %s", gtk_src_dir,
122 g_message ("Added `%s' as shortcut folder.", gtk_src_dir);
127 del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
132 if (!gtk_file_chooser_remove_shortcut_folder (user_data, gtk_src_dir, &err))
134 g_message ("Couldn't remove `%s' as shortcut folder: %s", gtk_src_dir,
140 g_message ("Removed `%s' as shortcut folder.", gtk_src_dir);
145 unselect_all_clicked_cb (GtkWidget *button,
148 gtk_file_chooser_unselect_all (user_data);
152 tests_button_clicked_cb (GtkButton *real_button,
157 tests = g_object_get_data (user_data, "tests-dialog");
161 GtkWidget *box, *button;
163 tests = gtk_window_new (GTK_WINDOW_TOPLEVEL);
164 gtk_window_set_title (GTK_WINDOW (tests),
165 "Tests - TestFileChooserButton");
166 gtk_container_set_border_width (GTK_CONTAINER (tests), 12);
167 gtk_window_set_transient_for (GTK_WINDOW (tests),
168 GTK_WINDOW (gtk_widget_get_toplevel (user_data)));
170 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
171 gtk_container_add (GTK_CONTAINER (tests), box);
172 gtk_widget_show (box);
174 button = gtk_button_new_with_label ("Print Selected Path");
175 g_signal_connect (button, "clicked",
176 G_CALLBACK (print_selected_path_clicked_cb), user_data);
177 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
178 gtk_widget_show (button);
180 button = gtk_button_new_with_label ("Add $PWD's Parent as Shortcut");
181 g_signal_connect (button, "clicked",
182 G_CALLBACK (add_pwds_parent_as_shortcut_clicked_cb), user_data);
183 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
184 gtk_widget_show (button);
186 button = gtk_button_new_with_label ("Remove $PWD's Parent as Shortcut");
187 g_signal_connect (button, "clicked",
188 G_CALLBACK (del_pwds_parent_as_shortcut_clicked_cb), user_data);
189 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
190 gtk_widget_show (button);
192 button = gtk_button_new_with_label ("Unselect all");
193 g_signal_connect (button, "clicked",
194 G_CALLBACK (unselect_all_clicked_cb), user_data);
195 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
196 gtk_widget_show (button);
198 g_signal_connect (tests, "delete-event", G_CALLBACK (delete_event_cb), NULL);
199 g_object_set_data (user_data, "tests-dialog", tests);
202 gtk_window_present (GTK_WINDOW (tests));
206 chooser_current_folder_changed_cb (GtkFileChooser *chooser,
209 gchar *folder, *filename;
211 folder = gtk_file_chooser_get_current_folder_uri (chooser);
212 filename = gtk_file_chooser_get_uri (chooser);
213 g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
214 G_OBJECT_TYPE_NAME (chooser), folder, filename);
220 chooser_selection_changed_cb (GtkFileChooser *chooser,
225 filename = gtk_file_chooser_get_uri (chooser);
226 g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n",
227 G_OBJECT_TYPE_NAME (chooser), filename);
232 chooser_file_activated_cb (GtkFileChooser *chooser,
235 gchar *folder, *filename;
237 folder = gtk_file_chooser_get_current_folder_uri (chooser);
238 filename = gtk_file_chooser_get_uri (chooser);
239 g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
240 G_OBJECT_TYPE_NAME (chooser), folder, filename);
246 chooser_update_preview_cb (GtkFileChooser *chooser,
251 filename = gtk_file_chooser_get_preview_uri (chooser);
252 if (filename != NULL)
254 g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n",
255 G_OBJECT_TYPE_NAME (chooser), filename);
265 GtkWidget *win, *vbox, *frame, *alignment, *group_box;
266 GtkWidget *hbox, *label, *chooser, *button;
267 GtkSizeGroup *label_group;
268 GOptionContext *context;
271 context = g_option_context_new ("- test GtkFileChooserButton widget");
272 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
273 g_option_context_add_group (context, gtk_get_option_group (TRUE));
274 g_option_context_parse (context, &argc, &argv, NULL);
275 g_option_context_free (context);
277 gtk_init (&argc, &argv);
279 /* to test rtl layout, use "--right-to-left" */
281 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
283 cwd = g_get_current_dir();
284 gtk_src_dir = g_path_get_dirname (cwd);
287 win = gtk_dialog_new_with_buttons ("TestFileChooserButton", NULL, 0,
288 GTK_STOCK_QUIT, GTK_RESPONSE_CLOSE, NULL);
289 g_signal_connect (win, "style-set", G_CALLBACK (win_style_set_cb), NULL);
290 g_signal_connect (win, "response", G_CALLBACK (gtk_main_quit), NULL);
292 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
293 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (win))), vbox);
295 frame = gtk_frame_new ("<b>GtkFileChooserButton</b>");
296 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
297 gtk_label_set_use_markup (GTK_LABEL (gtk_frame_get_label_widget (GTK_FRAME (frame))), TRUE);
298 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
300 alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
301 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 12, 0);
302 gtk_container_add (GTK_CONTAINER (frame), alignment);
304 label_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
306 group_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
307 gtk_container_add (GTK_CONTAINER (alignment), group_box);
310 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
311 gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);
313 label = gtk_label_new_with_mnemonic ("_Open:");
314 gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
315 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
316 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
318 chooser = gtk_file_chooser_button_new ("Select A File - testfilechooserbutton",
319 GTK_FILE_CHOOSER_ACTION_OPEN);
320 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
321 gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
322 gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
323 g_signal_connect (chooser, "current-folder-changed",
324 G_CALLBACK (chooser_current_folder_changed_cb), NULL);
325 g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
326 g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
327 g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
328 gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE, 0);
330 button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
331 g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
332 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
334 button = gtk_button_new_with_label ("Tests");
335 g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
336 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
339 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
340 gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);
342 label = gtk_label_new_with_mnemonic ("Select _Folder:");
343 gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
344 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
345 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
347 chooser = gtk_file_chooser_button_new ("Select A Folder - testfilechooserbutton",
348 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
349 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
350 gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
351 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
352 gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
353 g_signal_connect (chooser, "current-folder-changed",
354 G_CALLBACK (chooser_current_folder_changed_cb), NULL);
355 g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
356 g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
357 g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
358 gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE, 0);
360 button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
361 g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
362 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
364 button = gtk_button_new_with_label ("Tests");
365 g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
366 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
368 g_object_unref (label_group);
370 gtk_widget_show_all (win);
371 gtk_window_present (GTK_WINDOW (win));