2005-07-13 05:44:22 +00:00
|
|
|
/* testfilechooser.c
|
|
|
|
* Copyright (C) 2003 Red Hat, Inc.
|
|
|
|
* Author: Owen Taylor
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2004-03-06 03:38:59 +00:00
|
|
|
#include <config.h>
|
2003-11-15 20:35:55 +00:00
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
#include <string.h>
|
2003-07-23 22:30:32 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2003-11-15 20:35:55 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-07-23 22:30:32 +00:00
|
|
|
#include <unistd.h>
|
2003-11-15 20:35:55 +00:00
|
|
|
#endif
|
2003-03-21 20:34:02 +00:00
|
|
|
#include <gtk/gtk.h>
|
2003-11-15 20:35:55 +00:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
# include <io.h>
|
2004-03-08 00:55:21 +00:00
|
|
|
# define localtime_r(t,b) *(b) = *localtime (t)
|
2003-11-15 20:35:55 +00:00
|
|
|
# ifndef S_ISREG
|
|
|
|
# define S_ISREG(m) ((m) & _S_IFREG)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
#include "prop-editor.h"
|
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
static GtkWidget *preview_label;
|
|
|
|
static GtkWidget *preview_image;
|
2004-03-02 19:38:54 +00:00
|
|
|
static GtkFileChooserAction action;
|
2003-07-23 22:30:32 +00:00
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
static void
|
|
|
|
print_current_folder (GtkFileChooser *chooser)
|
|
|
|
{
|
|
|
|
gchar *uri;
|
|
|
|
|
|
|
|
uri = gtk_file_chooser_get_current_folder_uri (chooser);
|
2003-12-14 19:03:02 +00:00
|
|
|
g_print ("Current folder changed :\n %s\n", uri ? uri : "(null)");
|
2003-03-21 20:34:02 +00:00
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_selected (GtkFileChooser *chooser)
|
|
|
|
{
|
|
|
|
GSList *uris = gtk_file_chooser_get_uris (chooser);
|
|
|
|
GSList *tmp_list;
|
|
|
|
|
|
|
|
g_print ("Selection changed :\n");
|
|
|
|
for (tmp_list = uris; tmp_list; tmp_list = tmp_list->next)
|
|
|
|
{
|
|
|
|
gchar *uri = tmp_list->data;
|
|
|
|
g_print (" %s\n", uri);
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
g_print ("\n");
|
|
|
|
g_slist_free (uris);
|
|
|
|
}
|
|
|
|
|
2003-03-25 00:11:06 +00:00
|
|
|
static void
|
|
|
|
response_cb (GtkDialog *dialog,
|
|
|
|
gint response_id)
|
|
|
|
{
|
2003-09-11 00:56:46 +00:00
|
|
|
if (response_id == GTK_RESPONSE_OK)
|
|
|
|
{
|
|
|
|
GSList *list;
|
|
|
|
|
|
|
|
list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog));
|
|
|
|
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
g_print ("Selected files:\n");
|
|
|
|
|
|
|
|
for (l = list; l; l = l->next)
|
|
|
|
{
|
|
|
|
g_print ("%s\n", (char *) l->data);
|
|
|
|
g_free (l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free (list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_print ("No selected files\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
g_print ("Dialog was closed\n");
|
2004-03-02 17:03:04 +00:00
|
|
|
|
2003-03-25 00:11:06 +00:00
|
|
|
gtk_main_quit ();
|
|
|
|
}
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
static gboolean
|
|
|
|
no_backup_files_filter (const GtkFileFilterInfo *filter_info,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2004-01-28 21:46:02 +00:00
|
|
|
gsize len = filter_info->display_name ? strlen (filter_info->display_name) : 0;
|
2003-07-23 15:31:10 +00:00
|
|
|
if (len > 0 && filter_info->display_name[len - 1] == '~')
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
static void
|
|
|
|
filter_changed (GtkFileChooserDialog *dialog,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
g_print ("file filter changed\n");
|
|
|
|
}
|
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
static char *
|
|
|
|
format_time (time_t t)
|
|
|
|
{
|
|
|
|
gchar buf[128];
|
|
|
|
struct tm tm_buf;
|
|
|
|
time_t now = time (NULL);
|
|
|
|
const char *format;
|
|
|
|
|
|
|
|
if (abs (now - t) < 24*60*60)
|
|
|
|
format = "%X";
|
|
|
|
else
|
|
|
|
format = "%x";
|
|
|
|
|
|
|
|
localtime_r (&t, &tm_buf);
|
|
|
|
if (strftime (buf, sizeof (buf), format, &tm_buf) == 0)
|
|
|
|
return g_strdup ("<unknown>");
|
|
|
|
else
|
|
|
|
return g_strdup (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
format_size (gint64 size)
|
|
|
|
{
|
|
|
|
if (size < (gint64)1024)
|
|
|
|
return g_strdup_printf ("%d bytes", (gint)size);
|
|
|
|
else if (size < (gint64)1024*1024)
|
|
|
|
return g_strdup_printf ("%.1f K", size / (1024.));
|
|
|
|
else if (size < (gint64)1024*1024*1024)
|
|
|
|
return g_strdup_printf ("%.1f M", size / (1024.*1024.));
|
|
|
|
else
|
|
|
|
return g_strdup_printf ("%.1f G", size / (1024.*1024.*1024.));
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#define _(s) (s)
|
|
|
|
|
|
|
|
static void
|
2005-07-15 06:07:13 +00:00
|
|
|
size_prepared_cb (GdkPixbufLoader *loader,
|
2003-07-23 22:30:32 +00:00
|
|
|
int width,
|
|
|
|
int height,
|
2004-03-02 17:03:04 +00:00
|
|
|
int *data)
|
2003-07-23 22:30:32 +00:00
|
|
|
{
|
2004-03-02 17:03:04 +00:00
|
|
|
int des_width = data[0];
|
|
|
|
int des_height = data[1];
|
|
|
|
|
|
|
|
if (des_height >= height && des_width >= width) {
|
|
|
|
/* Nothing */
|
|
|
|
} else if ((double)height * des_width > (double)width * des_height) {
|
|
|
|
width = 0.5 + (double)width * des_height / (double)height;
|
|
|
|
height = des_height;
|
2003-07-23 22:30:32 +00:00
|
|
|
} else {
|
2004-03-02 17:03:04 +00:00
|
|
|
height = 0.5 + (double)height * des_width / (double)width;
|
|
|
|
width = des_width;
|
2003-07-23 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gdk_pixbuf_loader_set_size (loader, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
GdkPixbuf *
|
|
|
|
my_new_from_file_at_size (const char *filename,
|
2005-07-15 06:07:13 +00:00
|
|
|
int width,
|
2003-07-23 22:30:32 +00:00
|
|
|
int height,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
GdkPixbufLoader *loader;
|
|
|
|
GdkPixbuf *pixbuf;
|
2004-03-02 17:03:04 +00:00
|
|
|
int info[2];
|
2003-09-09 15:18:29 +00:00
|
|
|
struct stat st;
|
2003-07-23 22:30:32 +00:00
|
|
|
|
|
|
|
guchar buffer [4096];
|
|
|
|
int length;
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
g_return_val_if_fail (width > 0 && height > 0, NULL);
|
|
|
|
|
2003-09-09 15:18:29 +00:00
|
|
|
if (stat (filename, &st) != 0) {
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Could not get information for file '%s': %s"),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG (st.st_mode))
|
|
|
|
return NULL;
|
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
f = fopen (filename, "rb");
|
|
|
|
if (!f) {
|
|
|
|
g_set_error (error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno (errno),
|
|
|
|
_("Failed to open file '%s': %s"),
|
|
|
|
filename, g_strerror (errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
loader = gdk_pixbuf_loader_new ();
|
|
|
|
#ifdef DONT_PRESERVE_ASPECT
|
|
|
|
gdk_pixbuf_loader_set_size (loader, width, height);
|
|
|
|
#else
|
2004-03-02 17:03:04 +00:00
|
|
|
info[0] = width;
|
|
|
|
info[1] = height;
|
|
|
|
g_signal_connect (loader, "size-prepared", G_CALLBACK (size_prepared_cb), info);
|
2005-07-15 06:07:13 +00:00
|
|
|
#endif
|
2003-07-23 22:30:32 +00:00
|
|
|
|
|
|
|
while (!feof (f)) {
|
|
|
|
length = fread (buffer, 1, sizeof (buffer), f);
|
|
|
|
if (length > 0)
|
|
|
|
if (!gdk_pixbuf_loader_write (loader, buffer, length, error)) {
|
|
|
|
gdk_pixbuf_loader_close (loader, NULL);
|
|
|
|
fclose (f);
|
2004-11-19 23:30:47 +00:00
|
|
|
g_object_unref (loader);
|
2003-07-23 22:30:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
|
2004-03-08 09:56:34 +00:00
|
|
|
g_assert (*error == NULL);
|
2003-07-23 22:30:32 +00:00
|
|
|
if (!gdk_pixbuf_loader_close (loader, error)) {
|
2004-11-19 23:30:47 +00:00
|
|
|
g_object_unref (loader);
|
2003-07-23 22:30:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
|
|
|
|
|
|
|
|
if (!pixbuf) {
|
2004-11-19 23:30:47 +00:00
|
|
|
g_object_unref (loader);
|
2004-03-08 09:56:34 +00:00
|
|
|
|
|
|
|
/* did the loader set an error? */
|
|
|
|
if (*error != NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
g_set_error (error,
|
|
|
|
GDK_PIXBUF_ERROR,
|
|
|
|
GDK_PIXBUF_ERROR_FAILED,
|
|
|
|
_("Failed to load image '%s': reason not known, probably a corrupt image file"),
|
|
|
|
filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_ref (pixbuf);
|
|
|
|
|
2004-11-19 23:30:47 +00:00
|
|
|
g_object_unref (loader);
|
2003-07-23 22:30:32 +00:00
|
|
|
|
|
|
|
return pixbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_preview_cb (GtkFileChooser *chooser)
|
|
|
|
{
|
|
|
|
gchar *filename = gtk_file_chooser_get_preview_filename (chooser);
|
|
|
|
gboolean have_preview = FALSE;
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
GdkPixbuf *pixbuf;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
pixbuf = my_new_from_file_at_size (filename, 128, 128, &error);
|
|
|
|
if (pixbuf)
|
|
|
|
{
|
|
|
|
gtk_image_set_from_pixbuf (GTK_IMAGE (preview_image), pixbuf);
|
|
|
|
g_object_unref (pixbuf);
|
|
|
|
gtk_widget_show (preview_image);
|
|
|
|
gtk_widget_hide (preview_label);
|
|
|
|
have_preview = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
if (stat (filename, &buf) == 0)
|
|
|
|
{
|
|
|
|
gchar *preview_text;
|
|
|
|
gchar *size_str;
|
|
|
|
gchar *modified_time;
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
size_str = format_size (buf.st_size);
|
|
|
|
modified_time = format_time (buf.st_mtime);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
preview_text = g_strdup_printf ("<i>Modified:</i>\t%s\n"
|
|
|
|
"<i>Size:</i>\t%s\n",
|
|
|
|
modified_time,
|
|
|
|
size_str);
|
|
|
|
gtk_label_set_markup (GTK_LABEL (preview_label), preview_text);
|
|
|
|
g_free (modified_time);
|
|
|
|
g_free (size_str);
|
|
|
|
g_free (preview_text);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
gtk_widget_hide (preview_image);
|
|
|
|
gtk_widget_show (preview_label);
|
|
|
|
have_preview = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
g_free (filename);
|
2004-03-03 14:27:00 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
g_error_free (error);
|
2003-07-23 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_file_chooser_set_preview_widget_active (chooser, have_preview);
|
|
|
|
}
|
|
|
|
|
2004-03-06 03:38:59 +00:00
|
|
|
static void
|
|
|
|
set_current_folder (GtkFileChooser *chooser,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
if (!gtk_file_chooser_set_current_folder (chooser, name))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (chooser),
|
|
|
|
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Could not set the folder to %s",
|
|
|
|
name);
|
|
|
|
gtk_dialog_run (GTK_DIALOG (dialog));
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-02 20:05:08 +00:00
|
|
|
static void
|
2004-03-02 21:55:01 +00:00
|
|
|
set_folder_nonexistent_cb (GtkButton *button,
|
2004-03-02 20:05:08 +00:00
|
|
|
GtkFileChooser *chooser)
|
|
|
|
{
|
2004-03-06 03:38:59 +00:00
|
|
|
set_current_folder (chooser, "/nonexistent");
|
2004-03-02 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-02 21:55:01 +00:00
|
|
|
set_folder_existing_nonexistent_cb (GtkButton *button,
|
2004-03-02 20:05:08 +00:00
|
|
|
GtkFileChooser *chooser)
|
|
|
|
{
|
2004-03-06 03:38:59 +00:00
|
|
|
set_current_folder (chooser, "/usr/nonexistent");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_filename (GtkFileChooser *chooser,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
if (!gtk_file_chooser_set_filename (chooser, name))
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (chooser),
|
|
|
|
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"Could not select %s",
|
|
|
|
name);
|
|
|
|
gtk_dialog_run (GTK_DIALOG (dialog));
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
}
|
2004-03-02 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-02 21:55:01 +00:00
|
|
|
set_filename_nonexistent_cb (GtkButton *button,
|
2004-03-02 20:05:08 +00:00
|
|
|
GtkFileChooser *chooser)
|
|
|
|
{
|
2004-03-06 03:38:59 +00:00
|
|
|
set_filename (chooser, "/nonexistent");
|
2004-03-02 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-03-02 21:55:01 +00:00
|
|
|
set_filename_existing_nonexistent_cb (GtkButton *button,
|
2004-03-02 20:05:08 +00:00
|
|
|
GtkFileChooser *chooser)
|
|
|
|
{
|
2004-03-06 03:38:59 +00:00
|
|
|
set_filename (chooser, "/usr/nonexistent");
|
2004-03-02 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-25 01:35:09 +00:00
|
|
|
static void
|
|
|
|
unmap_and_remap_cb (GtkButton *button,
|
|
|
|
GtkFileChooser *chooser)
|
|
|
|
{
|
|
|
|
gtk_widget_hide (GTK_WIDGET (chooser));
|
|
|
|
gtk_widget_show (GTK_WIDGET (chooser));
|
|
|
|
}
|
|
|
|
|
2004-03-17 19:01:00 +00:00
|
|
|
static void
|
|
|
|
kill_dependent (GtkWindow *win, GtkObject *dep)
|
|
|
|
{
|
|
|
|
gtk_object_destroy (dep);
|
|
|
|
g_object_unref (dep);
|
|
|
|
}
|
|
|
|
|
2004-09-09 12:46:25 +00:00
|
|
|
static void
|
|
|
|
notify_multiple_cb (GtkWidget *dialog,
|
|
|
|
GParamSpec *pspec,
|
|
|
|
GtkWidget *button)
|
|
|
|
{
|
|
|
|
gboolean multiple;
|
|
|
|
|
|
|
|
multiple = gtk_file_chooser_get_select_multiple (GTK_FILE_CHOOSER (dialog));
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2004-09-09 12:46:25 +00:00
|
|
|
gtk_widget_set_sensitive (button, multiple);
|
|
|
|
}
|
|
|
|
|
2005-07-15 06:07:13 +00:00
|
|
|
static GtkFileChooserConfirmation
|
|
|
|
confirm_overwrite_cb (GtkFileChooser *chooser,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *button;
|
|
|
|
int response;
|
|
|
|
GtkFileChooserConfirmation conf;
|
|
|
|
|
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (chooser))),
|
|
|
|
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_QUESTION,
|
|
|
|
GTK_BUTTONS_NONE,
|
|
|
|
"What do you want to do?");
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Use the stock confirmation dialog");
|
|
|
|
gtk_widget_show (button);
|
|
|
|
gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 1);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Type a new file name");
|
|
|
|
gtk_widget_show (button);
|
|
|
|
gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 2);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Accept the file name");
|
|
|
|
gtk_widget_show (button);
|
|
|
|
gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, 3);
|
|
|
|
|
|
|
|
response = gtk_dialog_run (GTK_DIALOG (dialog));
|
|
|
|
|
|
|
|
switch (response)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
conf = GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
conf = GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
conf = GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *control_window;
|
|
|
|
GtkWidget *vbbox;
|
|
|
|
GtkWidget *button;
|
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *prop_editor;
|
2004-03-02 21:55:01 +00:00
|
|
|
GtkWidget *extra;
|
2003-07-23 15:31:10 +00:00
|
|
|
GtkFileFilter *filter;
|
2003-07-23 22:30:32 +00:00
|
|
|
GtkWidget *preview_vbox;
|
2004-03-02 19:38:54 +00:00
|
|
|
int i;
|
2004-03-10 14:56:28 +00:00
|
|
|
gboolean multiple = FALSE;
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
/* to test rtl layout, set RTL=1 in the environment */
|
|
|
|
if (g_getenv ("RTL"))
|
|
|
|
gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2004-03-02 19:38:54 +00:00
|
|
|
action = GTK_FILE_CHOOSER_ACTION_OPEN;
|
|
|
|
|
|
|
|
/* lame-o arg parsing */
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (! strcmp ("--action=open", argv[i]))
|
|
|
|
action = GTK_FILE_CHOOSER_ACTION_OPEN;
|
|
|
|
else if (! strcmp ("--action=save", argv[i]))
|
|
|
|
action = GTK_FILE_CHOOSER_ACTION_SAVE;
|
|
|
|
else if (! strcmp ("--action=select_folder", argv[i]))
|
|
|
|
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
|
|
|
|
else if (! strcmp ("--action=create_folder", argv[i]))
|
|
|
|
action = GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER;
|
2004-03-10 14:56:28 +00:00
|
|
|
else if (! strcmp ("--multiple", argv[i]))
|
|
|
|
multiple = TRUE;
|
2004-03-02 19:38:54 +00:00
|
|
|
}
|
|
|
|
|
2003-03-26 17:09:26 +00:00
|
|
|
dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
|
2004-03-02 19:38:54 +00:00
|
|
|
"action", action,
|
2004-04-11 19:15:24 +00:00
|
|
|
"file-system-backend", "gtk+",
|
2004-03-10 14:56:28 +00:00
|
|
|
"select-multiple", multiple,
|
2003-03-26 17:09:26 +00:00
|
|
|
NULL);
|
2004-03-02 19:38:54 +00:00
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case GTK_FILE_CHOOSER_ACTION_OPEN:
|
|
|
|
case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (dialog), "Select a file");
|
|
|
|
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case GTK_FILE_CHOOSER_ACTION_SAVE:
|
|
|
|
case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
|
|
|
|
gtk_window_set_title (GTK_WINDOW (dialog), "Save a file");
|
|
|
|
gtk_dialog_add_buttons (GTK_DIALOG (dialog),
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
}
|
2003-03-25 00:11:06 +00:00
|
|
|
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-11 19:18:36 +00:00
|
|
|
g_signal_connect (dialog, "selection-changed",
|
2003-03-21 20:34:02 +00:00
|
|
|
G_CALLBACK (print_selected), NULL);
|
2003-07-11 19:18:36 +00:00
|
|
|
g_signal_connect (dialog, "current-folder-changed",
|
2003-03-21 20:34:02 +00:00
|
|
|
G_CALLBACK (print_current_folder), NULL);
|
2003-03-25 00:11:06 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
|
|
|
G_CALLBACK (response_cb), NULL);
|
2005-07-15 06:07:13 +00:00
|
|
|
g_signal_connect (dialog, "confirm-overwrite",
|
|
|
|
G_CALLBACK (confirm_overwrite_cb), NULL);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
|
|
/* Filters */
|
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_set_name (filter, "All Files");
|
|
|
|
gtk_file_filter_add_pattern (filter, "*");
|
|
|
|
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_set_name (filter, "No backup files");
|
|
|
|
gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_DISPLAY_NAME,
|
|
|
|
no_backup_files_filter, NULL, NULL);
|
|
|
|
gtk_file_filter_add_mime_type (filter, "image/png");
|
|
|
|
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
|
|
|
|
|
2005-07-15 06:07:13 +00:00
|
|
|
g_signal_connect (dialog, "notify::filter",
|
2004-11-07 05:42:01 +00:00
|
|
|
G_CALLBACK (filter_changed), NULL);
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
/* Make this filter the default */
|
|
|
|
gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog), filter);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_set_name (filter, "PNG and JPEG");
|
|
|
|
gtk_file_filter_add_mime_type (filter, "image/jpeg");
|
|
|
|
gtk_file_filter_add_mime_type (filter, "image/png");
|
|
|
|
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
|
|
|
|
|
2004-11-07 05:42:01 +00:00
|
|
|
filter = gtk_file_filter_new ();
|
|
|
|
gtk_file_filter_set_name (filter, "Images");
|
|
|
|
gtk_file_filter_add_pixbuf_formats (filter);
|
|
|
|
gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
/* Preview widget */
|
2004-03-15 06:54:34 +00:00
|
|
|
/* THIS IS A TERRIBLE PREVIEW WIDGET, AND SHOULD NOT BE COPIED AT ALL.
|
|
|
|
*/
|
2003-07-23 22:30:32 +00:00
|
|
|
preview_vbox = gtk_vbox_new (0, FALSE);
|
2004-11-07 05:42:01 +00:00
|
|
|
/*gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (dialog), preview_vbox);*/
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
preview_label = gtk_label_new (NULL);
|
|
|
|
gtk_box_pack_start (GTK_BOX (preview_vbox), preview_label, TRUE, TRUE, 0);
|
|
|
|
gtk_misc_set_padding (GTK_MISC (preview_label), 6, 6);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
preview_image = gtk_image_new ();
|
|
|
|
gtk_box_pack_start (GTK_BOX (preview_vbox), preview_image, TRUE, TRUE, 0);
|
|
|
|
gtk_misc_set_padding (GTK_MISC (preview_image), 6, 6);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
update_preview_cb (GTK_FILE_CHOOSER (dialog));
|
|
|
|
g_signal_connect (dialog, "update-preview",
|
|
|
|
G_CALLBACK (update_preview_cb), NULL);
|
2003-09-03 05:52:35 +00:00
|
|
|
|
|
|
|
/* Extra widget */
|
2004-03-02 20:05:08 +00:00
|
|
|
|
2004-03-02 21:55:01 +00:00
|
|
|
extra = gtk_check_button_new_with_mnemonic ("Lar_t whoever asks about this button");
|
|
|
|
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (extra), TRUE);
|
|
|
|
gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), extra);
|
2003-10-18 04:29:40 +00:00
|
|
|
|
|
|
|
/* Shortcuts */
|
|
|
|
|
|
|
|
gtk_file_chooser_add_shortcut_folder_uri (GTK_FILE_CHOOSER (dialog),
|
|
|
|
"file:///usr/share/pixmaps",
|
|
|
|
NULL);
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
/* show_all() to reveal bugs in composite widget handling */
|
|
|
|
gtk_widget_show_all (dialog);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
2003-07-23 22:30:32 +00:00
|
|
|
/* Extra controls for manipulating the test environment
|
|
|
|
*/
|
2003-03-21 20:34:02 +00:00
|
|
|
prop_editor = create_prop_editor (G_OBJECT (dialog), GTK_TYPE_FILE_CHOOSER);
|
|
|
|
|
|
|
|
control_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
vbbox = gtk_vbutton_box_new ();
|
|
|
|
gtk_container_add (GTK_CONTAINER (control_window), vbbox);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_mnemonic ("_Select all");
|
2004-03-10 14:56:28 +00:00
|
|
|
gtk_widget_set_sensitive (button, multiple);
|
2003-03-21 20:34:02 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect_swapped (button, "clicked",
|
|
|
|
G_CALLBACK (gtk_file_chooser_select_all), dialog);
|
2005-07-15 06:07:13 +00:00
|
|
|
g_signal_connect (dialog, "notify::select-multiple",
|
2004-09-09 12:46:25 +00:00
|
|
|
G_CALLBACK (notify_multiple_cb), button);
|
2005-07-15 06:07:13 +00:00
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
button = gtk_button_new_with_mnemonic ("_Unselect all");
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect_swapped (button, "clicked",
|
|
|
|
G_CALLBACK (gtk_file_chooser_unselect_all), dialog);
|
|
|
|
|
2004-03-02 21:55:01 +00:00
|
|
|
button = gtk_button_new_with_label ("set_current_folder (\"/nonexistent\")");
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (set_folder_nonexistent_cb), dialog);
|
|
|
|
|
2004-09-14 19:37:48 +00:00
|
|
|
button = gtk_button_new_with_label ("set_current_folder (\"/usr/nonexistent\")");
|
2004-03-02 21:55:01 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (set_folder_existing_nonexistent_cb), dialog);
|
|
|
|
|
2004-09-14 19:37:48 +00:00
|
|
|
button = gtk_button_new_with_label ("set_filename (\"/nonexistent\")");
|
2004-03-02 21:55:01 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (set_filename_nonexistent_cb), dialog);
|
|
|
|
|
2004-09-14 19:37:48 +00:00
|
|
|
button = gtk_button_new_with_label ("set_filename (\"/usr/nonexistent\")");
|
2004-03-02 21:55:01 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (set_filename_existing_nonexistent_cb), dialog);
|
|
|
|
|
2005-01-25 01:35:09 +00:00
|
|
|
button = gtk_button_new_with_label ("Unmap and remap");
|
|
|
|
gtk_container_add (GTK_CONTAINER (vbbox), button);
|
|
|
|
g_signal_connect (button, "clicked",
|
|
|
|
G_CALLBACK (unmap_and_remap_cb), dialog);
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
gtk_widget_show_all (control_window);
|
2004-03-17 19:01:00 +00:00
|
|
|
|
|
|
|
g_object_ref (control_window);
|
2004-11-19 23:30:47 +00:00
|
|
|
g_signal_connect (dialog, "destroy",
|
2004-03-17 19:01:00 +00:00
|
|
|
G_CALLBACK (kill_dependent), control_window);
|
|
|
|
|
|
|
|
/* We need to hold a ref until we have destroyed the widgets, just in case
|
|
|
|
* someone else destroys them. We explicitly destroy windows to catch leaks.
|
|
|
|
*/
|
|
|
|
g_object_ref (dialog);
|
2003-03-21 20:34:02 +00:00
|
|
|
gtk_main ();
|
2004-03-17 19:01:00 +00:00
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
g_object_unref (dialog);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|