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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 #include <sys/types.h>
36 #include "prop-editor.h"
38 static gchar *backend = "gtk+";
39 static gboolean rtl = FALSE;
40 static GOptionEntry entries[] = {
41 { "backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "The filesystem backend to use.", "gtk+" },
42 { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &rtl, "Force right-to-left layout.", NULL },
46 static gchar *gtk_src_dir = NULL;
50 win_style_set_cb (GtkWidget *win)
52 GtkWidget *content_area, *action_area;
54 content_area = gtk_dialog_get_content_area (GTK_DIALOG (win));
55 action_area = gtk_dialog_get_action_area (GTK_DIALOG (win));
57 gtk_container_set_border_width (GTK_CONTAINER (content_area), 12);
58 gtk_box_set_spacing (GTK_BOX (content_area), 24);
59 gtk_container_set_border_width (GTK_CONTAINER (action_area), 0);
60 gtk_box_set_spacing (GTK_BOX (action_area), 6);
64 delete_event_cb (GtkWidget *editor,
68 gtk_widget_hide (editor);
75 properties_button_clicked_cb (GtkWidget *button,
80 editor = g_object_get_data (entry, "properties-dialog");
84 editor = create_prop_editor (G_OBJECT (entry), G_TYPE_INVALID);
85 gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
86 gtk_window_set_transient_for (GTK_WINDOW (editor),
87 GTK_WINDOW (gtk_widget_get_toplevel (button)));
88 g_signal_connect (editor, "delete-event", G_CALLBACK (delete_event_cb), NULL);
89 g_object_set_data (entry, "properties-dialog", editor);
92 gtk_window_present (GTK_WINDOW (editor));
97 print_selected_path_clicked_cb (GtkWidget *button,
100 gchar *folder, *filename;
102 folder = gtk_file_chooser_get_current_folder (user_data);
103 filename = gtk_file_chooser_get_filename (user_data);
104 g_message ("Currently Selected:\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
111 add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
116 if (!gtk_file_chooser_add_shortcut_folder (user_data, gtk_src_dir, &err))
118 g_message ("Couldn't add `%s' as shortcut folder: %s", gtk_src_dir,
124 g_message ("Added `%s' as shortcut folder.", gtk_src_dir);
129 del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
134 if (!gtk_file_chooser_remove_shortcut_folder (user_data, gtk_src_dir, &err))
136 g_message ("Couldn't remove `%s' as shortcut folder: %s", gtk_src_dir,
142 g_message ("Removed `%s' as shortcut folder.", gtk_src_dir);
147 unselect_all_clicked_cb (GtkWidget *button,
150 gtk_file_chooser_unselect_all (user_data);
154 tests_button_clicked_cb (GtkButton *real_button,
159 tests = g_object_get_data (user_data, "tests-dialog");
163 GtkWidget *box, *button;
165 tests = gtk_window_new (GTK_WINDOW_TOPLEVEL);
166 gtk_window_set_title (GTK_WINDOW (tests),
167 "Tests - TestFileChooserButton");
168 gtk_container_set_border_width (GTK_CONTAINER (tests), 12);
169 gtk_window_set_transient_for (GTK_WINDOW (tests),
170 GTK_WINDOW (gtk_widget_get_toplevel (user_data)));
172 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
173 gtk_container_add (GTK_CONTAINER (tests), box);
174 gtk_widget_show (box);
176 button = gtk_button_new_with_label ("Print Selected Path");
177 g_signal_connect (button, "clicked",
178 G_CALLBACK (print_selected_path_clicked_cb), user_data);
179 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
180 gtk_widget_show (button);
182 button = gtk_button_new_with_label ("Add $PWD's Parent as Shortcut");
183 g_signal_connect (button, "clicked",
184 G_CALLBACK (add_pwds_parent_as_shortcut_clicked_cb), user_data);
185 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
186 gtk_widget_show (button);
188 button = gtk_button_new_with_label ("Remove $PWD's Parent as Shortcut");
189 g_signal_connect (button, "clicked",
190 G_CALLBACK (del_pwds_parent_as_shortcut_clicked_cb), user_data);
191 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
192 gtk_widget_show (button);
194 button = gtk_button_new_with_label ("Unselect all");
195 g_signal_connect (button, "clicked",
196 G_CALLBACK (unselect_all_clicked_cb), user_data);
197 gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
198 gtk_widget_show (button);
200 g_signal_connect (tests, "delete-event", G_CALLBACK (delete_event_cb), NULL);
201 g_object_set_data (user_data, "tests-dialog", tests);
204 gtk_window_present (GTK_WINDOW (tests));
208 chooser_current_folder_changed_cb (GtkFileChooser *chooser,
211 gchar *folder, *filename;
213 folder = gtk_file_chooser_get_current_folder_uri (chooser);
214 filename = gtk_file_chooser_get_uri (chooser);
215 g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
216 G_OBJECT_TYPE_NAME (chooser), folder, filename);
222 chooser_selection_changed_cb (GtkFileChooser *chooser,
227 filename = gtk_file_chooser_get_uri (chooser);
228 g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n",
229 G_OBJECT_TYPE_NAME (chooser), filename);
234 chooser_file_activated_cb (GtkFileChooser *chooser,
237 gchar *folder, *filename;
239 folder = gtk_file_chooser_get_current_folder_uri (chooser);
240 filename = gtk_file_chooser_get_uri (chooser);
241 g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
242 G_OBJECT_TYPE_NAME (chooser), folder, filename);
248 chooser_update_preview_cb (GtkFileChooser *chooser,
253 filename = gtk_file_chooser_get_preview_uri (chooser);
254 if (filename != NULL)
256 g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n",
257 G_OBJECT_TYPE_NAME (chooser), filename);
267 GtkWidget *win, *vbox, *frame, *alignment, *group_box;
268 GtkWidget *hbox, *label, *chooser, *button;
269 GtkSizeGroup *label_group;
270 GOptionContext *context;
273 context = g_option_context_new ("- test GtkFileChooserButton widget");
274 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
275 g_option_context_add_group (context, gtk_get_option_group (TRUE));
276 g_option_context_parse (context, &argc, &argv, NULL);
277 g_option_context_free (context);
279 gtk_init (&argc, &argv);
281 /* to test rtl layout, use "--right-to-left" */
283 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
285 cwd = g_get_current_dir();
286 gtk_src_dir = g_path_get_dirname (cwd);
289 win = gtk_dialog_new_with_buttons ("TestFileChooserButton", NULL, 0,
290 GTK_STOCK_QUIT, GTK_RESPONSE_CLOSE, NULL);
291 g_signal_connect (win, "style-set", G_CALLBACK (win_style_set_cb), NULL);
292 g_signal_connect (win, "response", G_CALLBACK (gtk_main_quit), NULL);
294 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
295 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (win))), vbox);
297 frame = gtk_frame_new ("<b>GtkFileChooserButton</b>");
298 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
299 gtk_label_set_use_markup (GTK_LABEL (gtk_frame_get_label_widget (GTK_FRAME (frame))), TRUE);
300 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
302 alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
303 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 12, 0);
304 gtk_container_add (GTK_CONTAINER (frame), alignment);
306 label_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
308 group_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
309 gtk_container_add (GTK_CONTAINER (alignment), group_box);
312 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
313 gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);
315 label = gtk_label_new_with_mnemonic ("_Open:");
316 gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
317 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
318 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
320 chooser = gtk_file_chooser_button_new ("Select A File - testfilechooserbutton",
321 GTK_FILE_CHOOSER_ACTION_OPEN);
322 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
323 gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
324 gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
325 g_signal_connect (chooser, "current-folder-changed",
326 G_CALLBACK (chooser_current_folder_changed_cb), NULL);
327 g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
328 g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
329 g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
330 gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE, 0);
332 button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
333 g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
334 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
336 button = gtk_button_new_with_label ("Tests");
337 g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
338 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
341 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
342 gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);
344 label = gtk_label_new_with_mnemonic ("Select _Folder:");
345 gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
346 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
347 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
349 chooser = gtk_file_chooser_button_new ("Select A Folder - testfilechooserbutton",
350 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
351 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
352 gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
353 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
354 gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
355 g_signal_connect (chooser, "current-folder-changed",
356 G_CALLBACK (chooser_current_folder_changed_cb), NULL);
357 g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
358 g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
359 g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
360 gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE, 0);
362 button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
363 g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
364 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
366 button = gtk_button_new_with_label ("Tests");
367 g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
368 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
370 g_object_unref (label_group);
372 gtk_widget_show_all (win);
373 gtk_window_present (GTK_WINDOW (win));