testfilechooserbutton.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 2 -*- */

/* GTK+: gtkfilechooserbutton.c
 * 
 * Copyright (c) 2004 James M. Cape <jcape@ignore-your.tv>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
#include "config.h"
24 25 26 27

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
28 29 30 31 32 33 34 35

#include <sys/types.h>
#include <sys/stat.h>

#include <string.h>

#include <gtk/gtk.h>

36 37 38 39
#ifdef G_OS_WIN32
#include <direct.h> /* for _getcwd() */
#endif

40 41
#include "prop-editor.h"

42 43 44 45 46 47 48 49 50 51
static gchar *backend = "gtk+";
static gboolean rtl = FALSE;
static GOptionEntry entries[] = {
  { "backend", 'b', 0, G_OPTION_ARG_STRING, &backend, "The filesystem backend to use.", "gtk+" },
  { "right-to-left", 'r', 0, G_OPTION_ARG_NONE, &rtl, "Force right-to-left layout.", NULL },
  { NULL }
};

static gchar *gtk_src_dir = NULL;

52 53 54 55 56 57 58 59 60 61 62

static void
win_style_set_cb (GtkWidget *win)
{
  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (win)->vbox), 12);
  gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (win)->vbox), 24);
  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (win)->action_area), 0);
  gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (win)->action_area), 6);
}

static gboolean
63 64 65
delete_event_cb (GtkWidget *editor,
		 gint       response,
		 gpointer   user_data)
66 67 68 69 70 71 72 73
{
  gtk_widget_hide (editor);

  return TRUE;
}


static void
74 75
properties_button_clicked_cb (GtkWidget *button,
			      GObject   *entry)
76 77 78 79 80 81 82 83
{
  GtkWidget *editor;

  editor = g_object_get_data (entry, "properties-dialog");

  if (editor == NULL)
    {
      editor = create_prop_editor (G_OBJECT (entry), G_TYPE_INVALID);
84
      gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
85 86
      gtk_window_set_transient_for (GTK_WINDOW (editor),
				    GTK_WINDOW (gtk_widget_get_toplevel (button)));
87
      g_signal_connect (editor, "delete-event", G_CALLBACK (delete_event_cb), NULL);
88 89 90 91 92 93 94 95
      g_object_set_data (entry, "properties-dialog", editor);
    }

  gtk_window_present (GTK_WINDOW (editor));
}


static void
96 97 98 99 100 101 102 103 104 105 106 107 108 109
print_selected_path_clicked_cb (GtkWidget *button,
				gpointer   user_data)
{
  gchar *folder, *filename;

  folder = gtk_file_chooser_get_current_folder (user_data);
  filename = gtk_file_chooser_get_filename (user_data);
  g_message ("Currently Selected:\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
	     folder, filename);
  g_free (folder);
  g_free (filename);
}

static void
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
					gpointer   user_data)
{
  GError *err = NULL;

  if (!gtk_file_chooser_add_shortcut_folder (user_data, gtk_src_dir, &err))
    {
      g_message ("Couldn't add `%s' as shortcut folder: %s", gtk_src_dir,
		 err->message);
      g_error_free (err);
    }
  else
    {
      g_message ("Added `%s' as shortcut folder.", gtk_src_dir);
    }
}

static void
del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
					gpointer   user_data)
{
  GError *err = NULL;

  if (!gtk_file_chooser_remove_shortcut_folder (user_data, gtk_src_dir, &err))
    {
      g_message ("Couldn't remove `%s' as shortcut folder: %s", gtk_src_dir,
		 err->message);
      g_error_free (err);
    }
  else
    {
      g_message ("Removed `%s' as shortcut folder.", gtk_src_dir);
    }
}

145 146 147 148 149 150 151
static void
unselect_all_clicked_cb (GtkWidget *button,
                         gpointer   user_data)
{
  gtk_file_chooser_unselect_all (user_data);
}

152 153
static void
tests_button_clicked_cb (GtkButton *real_button,
154 155 156 157 158 159 160 161
			 gpointer   user_data)
{
  GtkWidget *tests;

  tests = g_object_get_data (user_data, "tests-dialog");

  if (tests == NULL)
    {
162
      GtkWidget *box, *button;
163 164 165 166 167 168

      tests = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (tests),
			    "Tests - TestFileChooserButton");
      gtk_container_set_border_width (GTK_CONTAINER (tests), 12);
      gtk_window_set_transient_for (GTK_WINDOW (tests),
169
				    GTK_WINDOW (gtk_widget_get_toplevel (user_data)));
170 171 172 173 174

      box = gtk_vbox_new (FALSE, 0);
      gtk_container_add (GTK_CONTAINER (tests), box);
      gtk_widget_show (box);

175 176
      button = gtk_button_new_with_label ("Print Selected Path");
      g_signal_connect (button, "clicked",
177
			G_CALLBACK (print_selected_path_clicked_cb), user_data);
178 179 180 181 182 183 184 185 186 187 188 189 190 191
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);

      button = gtk_button_new_with_label ("Add $PWD's Parent as Shortcut");
      g_signal_connect (button, "clicked",
			G_CALLBACK (add_pwds_parent_as_shortcut_clicked_cb), user_data);
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);

      button = gtk_button_new_with_label ("Remove $PWD's Parent as Shortcut");
      g_signal_connect (button, "clicked",
			G_CALLBACK (del_pwds_parent_as_shortcut_clicked_cb), user_data);
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);
192

193 194 195 196 197 198
      button = gtk_button_new_with_label ("Unselect all");
      g_signal_connect (button, "clicked",
			G_CALLBACK (unselect_all_clicked_cb), user_data);
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);

199 200 201 202 203 204 205 206 207 208
      g_signal_connect (tests, "delete-event", G_CALLBACK (delete_event_cb), NULL);
      g_object_set_data (user_data, "tests-dialog", tests);
    }

  gtk_window_present (GTK_WINDOW (tests));
}

static void
chooser_current_folder_changed_cb (GtkFileChooser *chooser,
				   gpointer        user_data)
209 210 211
{
  gchar *folder, *filename;

212 213
  folder = gtk_file_chooser_get_current_folder_uri (chooser);
  filename = gtk_file_chooser_get_uri (chooser);
214
  g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
215 216 217 218 219 220
	     G_OBJECT_TYPE_NAME (chooser), folder, filename);
  g_free (folder);
  g_free (filename);
}

static void
221 222
chooser_selection_changed_cb (GtkFileChooser *chooser,
			      gpointer        user_data)
223
{
224
  gchar *filename;
225

226
  filename = gtk_file_chooser_get_uri (chooser);
227 228 229
  g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n",
	     G_OBJECT_TYPE_NAME (chooser), filename);
  g_free (filename);
230 231 232
}

static void
233 234
chooser_file_activated_cb (GtkFileChooser *chooser,
			   gpointer        user_data)
235 236 237
{
  gchar *folder, *filename;

238 239
  folder = gtk_file_chooser_get_current_folder_uri (chooser);
  filename = gtk_file_chooser_get_uri (chooser);
240 241
  g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
	     G_OBJECT_TYPE_NAME (chooser), folder, filename);
242 243 244 245 246
  g_free (folder);
  g_free (filename);
}

static void
247 248
chooser_update_preview_cb (GtkFileChooser *chooser,
			   gpointer        user_data)
249 250 251
{
  gchar *filename;

252
  filename = gtk_file_chooser_get_preview_uri (chooser);
253 254 255 256 257 258
  if (filename != NULL)
    {
      g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n",
		 G_OBJECT_TYPE_NAME (chooser), filename);
      g_free (filename);
    }
259 260
}

261

262
int
263 264
main (int   argc,
      char *argv[])
265
{
266 267
  GtkWidget *win, *vbox, *frame, *alignment, *group_box;
  GtkWidget *hbox, *label, *chooser, *button;
268
  GtkSizeGroup *label_group;
269 270 271 272 273 274 275 276 277
  GOptionContext *context;
  gchar cwd[2048];

  context = g_option_context_new ("- test GtkFileChooserButton widget");
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_add_group (context, gtk_get_option_group (TRUE));
  g_option_context_parse (context, &argc, &argv, NULL);
  g_option_context_free (context);

278 279
  gtk_init (&argc, &argv);

280 281
  /* to test rtl layout, use "--right-to-left" */
  if (rtl)
282 283
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

284 285 286
  getcwd (cwd, sizeof (cwd));
  gtk_src_dir = g_path_get_dirname (cwd);

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  win = gtk_dialog_new_with_buttons ("TestFileChooserButton", NULL, GTK_DIALOG_NO_SEPARATOR,
				     GTK_STOCK_QUIT, GTK_RESPONSE_CLOSE, NULL);
  g_signal_connect (win, "style-set", G_CALLBACK (win_style_set_cb), NULL);
  g_signal_connect (win, "response", G_CALLBACK (gtk_main_quit), NULL);

  vbox = gtk_vbox_new (FALSE, 18);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (win)->vbox), vbox);

  frame = gtk_frame_new ("<b>GtkFileChooserButton</b>");
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
  gtk_label_set_use_markup (GTK_LABEL (gtk_frame_get_label_widget (GTK_FRAME (frame))), TRUE);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

  alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 6, 0, 12, 0);
  gtk_container_add (GTK_CONTAINER (frame), alignment);
  
  label_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
  
  group_box = gtk_vbox_new (FALSE, 6);
  gtk_container_add (GTK_CONTAINER (alignment), group_box);

309
  /* OPEN */
310
  hbox = gtk_hbox_new (FALSE, 12);
311
  gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);
312

Matthias Clasen's avatar
2.5.3  
Matthias Clasen committed
313
  label = gtk_label_new_with_mnemonic ("_Open:");
314 315 316 317
  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

318 319
  chooser = gtk_file_chooser_button_new ("Select A File - testfilechooserbutton",
                                         GTK_FILE_CHOOSER_ACTION_OPEN);
320 321
  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
  gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
322 323 324 325 326 327 328 329
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
  g_signal_connect (chooser, "current-folder-changed",
		    G_CALLBACK (chooser_current_folder_changed_cb), NULL);
  g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
  g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
  g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
  gtk_container_add (GTK_CONTAINER (hbox), chooser);

330
  button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
331 332 333
  g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

334 335 336 337
  button = gtk_button_new_with_label ("Tests");
  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

338
  /* SELECT_FOLDER */
339 340 341 342 343 344 345 346
  hbox = gtk_hbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE, 0);

  label = gtk_label_new_with_mnemonic ("Select _Folder:");
  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

347 348
  chooser = gtk_file_chooser_button_new ("Select A Folder - testfilechooserbutton",
                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
349 350 351
  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
  gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
352 353 354 355 356 357 358 359
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
  g_signal_connect (chooser, "current-folder-changed",
		    G_CALLBACK (chooser_current_folder_changed_cb), NULL);
  g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
  g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
  g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
  gtk_container_add (GTK_CONTAINER (hbox), chooser);

360
  button = gtk_button_new_from_stock (GTK_STOCK_PROPERTIES);
361 362 363
  g_signal_connect (button, "clicked", G_CALLBACK (properties_button_clicked_cb), chooser);
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

364 365
  button = gtk_button_new_with_label ("Tests");
  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
366 367 368 369 370 371 372 373 374 375 376
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);

  g_object_unref (label_group);

  gtk_widget_show_all (win);
  gtk_window_present (GTK_WINDOW (win));

  gtk_main ();

  return 0;
}