testfilechooserbutton.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* -*- 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
Javier Jardón's avatar
Javier Jardón committed
18
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 20
 */

21
#include "config.h"
22 23 24 25

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

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

#include <string.h>

#include <gtk/gtk.h>

34 35 36 37 38 39 40 41 42 43
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;

44
static gboolean
45 46 47
delete_event_cb (GtkWidget *editor,
		 gint       response,
		 gpointer   user_data)
48 49 50 51 52 53 54 55
{
  gtk_widget_hide (editor);

  return TRUE;
}


static void
56 57 58 59 60 61 62 63 64 65 66 67 68 69
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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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);
    }
}

105 106 107 108 109 110 111
static void
unselect_all_clicked_cb (GtkWidget *button,
                         gpointer   user_data)
{
  gtk_file_chooser_unselect_all (user_data);
}

112 113
static void
tests_button_clicked_cb (GtkButton *real_button,
114 115 116 117 118 119 120 121
			 gpointer   user_data)
{
  GtkWidget *tests;

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

  if (tests == NULL)
    {
122
      GtkWidget *box, *button;
123 124 125 126 127

      tests = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_window_set_title (GTK_WINDOW (tests),
			    "Tests - TestFileChooserButton");
      gtk_window_set_transient_for (GTK_WINDOW (tests),
128
				    GTK_WINDOW (gtk_widget_get_toplevel (user_data)));
129

130
      box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
131 132 133
      gtk_container_add (GTK_CONTAINER (tests), box);
      gtk_widget_show (box);

134 135
      button = gtk_button_new_with_label ("Print Selected Path");
      g_signal_connect (button, "clicked",
136
			G_CALLBACK (print_selected_path_clicked_cb), user_data);
137
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE);
138 139 140 141 142
      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);
143
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE);
144 145 146 147 148
      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);
149
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE);
150
      gtk_widget_show (button);
151

152 153 154
      button = gtk_button_new_with_label ("Unselect all");
      g_signal_connect (button, "clicked",
			G_CALLBACK (unselect_all_clicked_cb), user_data);
155
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE);
156 157
      gtk_widget_show (button);

158 159 160 161 162 163 164 165 166 167
      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)
168 169 170
{
  gchar *folder, *filename;

171 172
  folder = gtk_file_chooser_get_current_folder_uri (chooser);
  filename = gtk_file_chooser_get_uri (chooser);
173
  g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
174 175 176 177 178 179
	     G_OBJECT_TYPE_NAME (chooser), folder, filename);
  g_free (folder);
  g_free (filename);
}

static void
180 181
chooser_selection_changed_cb (GtkFileChooser *chooser,
			      gpointer        user_data)
182
{
183
  gchar *filename;
184

185
  filename = gtk_file_chooser_get_uri (chooser);
186 187 188
  g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n",
	     G_OBJECT_TYPE_NAME (chooser), filename);
  g_free (filename);
189 190 191
}

static void
192 193
chooser_file_activated_cb (GtkFileChooser *chooser,
			   gpointer        user_data)
194 195 196
{
  gchar *folder, *filename;

197 198
  folder = gtk_file_chooser_get_current_folder_uri (chooser);
  filename = gtk_file_chooser_get_uri (chooser);
199 200
  g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
	     G_OBJECT_TYPE_NAME (chooser), folder, filename);
201 202 203 204 205
  g_free (folder);
  g_free (filename);
}

static void
206 207
chooser_update_preview_cb (GtkFileChooser *chooser,
			   gpointer        user_data)
208 209 210
{
  gchar *filename;

211
  filename = gtk_file_chooser_get_preview_uri (chooser);
212 213 214 215 216 217
  if (filename != NULL)
    {
      g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n",
		 G_OBJECT_TYPE_NAME (chooser), filename);
      g_free (filename);
    }
218 219
}

220

221
int
222 223
main (int   argc,
      char *argv[])
224
{
225
  GtkWidget *win, *vbox, *frame, *group_box;
226
  GtkWidget *hbox, *label, *chooser, *button;
227
  GtkSizeGroup *label_group;
228
  GOptionContext *context;
Javier Jardón's avatar
Javier Jardón committed
229
  gchar *cwd;
230 231 232 233 234 235 236

  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);

237 238
  gtk_init (&argc, &argv);

239 240
  /* to test rtl layout, use "--right-to-left" */
  if (rtl)
241 242
    gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);

Javier Jardón's avatar
Javier Jardón committed
243
  cwd = g_get_current_dir();
244
  gtk_src_dir = g_path_get_dirname (cwd);
Javier Jardón's avatar
Javier Jardón committed
245
  g_free (cwd);
246

247
  win = gtk_dialog_new_with_buttons ("TestFileChooserButton", NULL, 0,
248
				     "_Quit", GTK_RESPONSE_CLOSE, NULL);
249 250
  g_signal_connect (win, "response", G_CALLBACK (gtk_main_quit), NULL);

251
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
252
  g_object_set (vbox, "margin", 6, NULL);
253
  gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (win))), vbox);
254 255 256 257

  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);
258
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE);
259

260 261 262
  gtk_widget_set_halign (frame, GTK_ALIGN_FILL);
  gtk_widget_set_valign (frame, GTK_ALIGN_FILL);
  g_object_set (frame, "margin-top", 6, "margin-start", 12, NULL);
263

264
  label_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
265

266
  group_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
267
  gtk_container_add (GTK_CONTAINER (frame), group_box);
268

269
  /* OPEN */
270
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
271
  gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE);
272

Matthias Clasen's avatar
Matthias Clasen committed
273
  label = gtk_label_new_with_mnemonic ("_Open:");
274
  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
275
  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
276
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE);
277

278 279
  chooser = gtk_file_chooser_button_new ("Select A File - testfilechooserbutton",
                                         GTK_FILE_CHOOSER_ACTION_OPEN);
280 281
  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);
282 283 284 285 286 287
  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);
288
  gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE);
289

290 291
  button = gtk_button_new_with_label ("Tests");
  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
292
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE);
293

294
  /* SELECT_FOLDER */
295
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
296
  gtk_box_pack_start (GTK_BOX (group_box), hbox, FALSE, FALSE);
297 298 299

  label = gtk_label_new_with_mnemonic ("Select _Folder:");
  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
300
  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
301
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE);
302

303 304
  chooser = gtk_file_chooser_button_new ("Select A Folder - testfilechooserbutton",
                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
305 306 307
  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);
308 309 310 311 312 313
  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);
314
  gtk_box_pack_start (GTK_BOX (hbox), chooser, TRUE, TRUE);
315

316 317
  button = gtk_button_new_with_label ("Tests");
  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
318
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE);
319 320 321 322 323 324 325 326 327 328

  g_object_unref (label_group);

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

  gtk_main ();

  return 0;
}