gimp/libgimpwidgets/gimpfileentry.c

416 lines
11 KiB
C
Raw Normal View History

/* LIBGIMP - The GIMP Library
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
*
* gimpfileselection.c
* Copyright (C) 1999 Michael Natterer <mitschel@cs.tu-berlin.de>
*
* This library is free software; you can redistribute it and/or
1999-11-18 05:13:50 +08:00
* modify it under the terms of the GNU Lesser 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.
*
1999-11-18 05:13:50 +08:00
* You should have received a copy of the GNU Lesser 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.
*/
#include "config.h"
#include <glib.h> /* Needed here by Win32 gcc compilation */
1999-04-22 14:50:29 +08:00
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#include "gimpfileselection.h"
#include "libgimp/gimpintl.h"
#include "pixmaps/yes.xpm"
#include "pixmaps/no.xpm"
#ifdef G_OS_WIN32
Win32 portability changes: * config.h.win32, README.win32: Small changes. * tools/pdbgen/pdb/*.pdb: Include <string.h>. * app/*_cmds.c: Autogenerated files reflect above changes. * libgimp/makefile.msc app/makefile.msc: Various updates, including new object files. Gtk+ directory now should be called gtk+ (not gtk-plus). Use win32-specific gdk subdir. Glib directory now should be called just glib. * libgimp/gimp.def: Updates. * libgimp/gimpfeatures.h.win32: Made current with gimpfeatures.h.in. * libgimp/gimpfileselection.c: Define S_ISDIR and S_ISREG if necessary. * tools/pdbgen/pdb/fileops.pdb: Must have a statement (even an empty one) after a label. * app/fileops_cmds.c: Autogenerated file reflects above changes. * app/crop.c: Include <string.h>. * app/{app_procs,batch,fileops,datafiles,errorconsole,general, plug_in,temp_buf,tile_swap}.c: Test NATIVE_WIN32, not _MSC_VER. (NATIVE_WIN32 means we are using the Microsoft C runtime, even if we might be compiling with gcc.) * app/fileops.c: Don't include <process.h> here. * app/fileops.h: Do include <process.h> here. * app/gimpparasite.c: Include config.h, guard inclusion of <unistd.h>. (Is the inclusion of unistd.h in source files all over the place really necessary?) * app/ink.c: MSC doesn't handle conversion from unsigned __int64 to double, so cast to signed. * app/lut_funcs.c: Include config.h, and define rint() if necessary. * app/pixel_processor.c: Include config.h without "..", like in all the other places. Include <string.h> * app/text_tool.c: Guard the "POINTS" identifier that clashes with <windows.h>, sigh.
1999-05-05 05:32:17 +08:00
# ifndef S_ISDIR
# define S_ISDIR(m) ((m) & _S_IFDIR)
# endif
# ifndef S_ISREG
# define S_ISREG(m) ((m) & _S_IFREG)
# endif
#endif
/* callbacks */
static void gimp_file_selection_realize (GtkWidget *);
static void gimp_file_selection_entry_callback (GtkWidget *, gpointer);
static int gimp_file_selection_entry_focus_out_callback (GtkWidget *,
GdkEvent *, gpointer);
static void gimp_file_selection_browse_callback (GtkWidget *, gpointer);
/* private functions */
static void gimp_file_selection_check_filename (GimpFileSelection *gfs);
enum
{
FILENAME_CHANGED,
LAST_SIGNAL
};
1999-12-07 06:44:40 +08:00
static guint gimp_file_selection_signals[LAST_SIGNAL] = { 0 };
static GtkHBoxClass *parent_class = NULL;
static void
gimp_file_selection_destroy (GtkObject *object)
{
GimpFileSelection *gfs;
g_return_if_fail (object != NULL);
g_return_if_fail (GIMP_IS_FILE_SELECTION (object));
gfs = GIMP_FILE_SELECTION (object);
if (gfs->file_selection)
gtk_widget_destroy (gfs->file_selection);
1999-05-19 01:33:39 +08:00
if (gfs->title)
g_free (gfs->title);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void
gimp_file_selection_class_init (GimpFileSelectionClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
parent_class = gtk_type_class (gtk_hbox_get_type ());
gimp_file_selection_signals[FILENAME_CHANGED] =
gtk_signal_new ("filename_changed",
GTK_RUN_FIRST,
object_class->type,
GTK_SIGNAL_OFFSET (GimpFileSelectionClass,
filename_changed),
gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
gtk_object_class_add_signals (object_class, gimp_file_selection_signals,
LAST_SIGNAL);
class->filename_changed = NULL;
object_class->destroy = gimp_file_selection_destroy;
widget_class->realize = gimp_file_selection_realize;
}
static void
gimp_file_selection_init (GimpFileSelection *gfs)
{
gfs->title = NULL;
gfs->file_selection = NULL;
gfs->file_exists = FALSE;
gfs->check_valid = FALSE;
1999-05-19 01:33:39 +08:00
gtk_box_set_spacing (GTK_BOX (gfs), 2);
gtk_box_set_homogeneous (GTK_BOX (gfs), FALSE);
gfs->browse_button = gtk_button_new_with_label (" ... ");
1999-05-19 01:33:39 +08:00
gtk_box_pack_end (GTK_BOX (gfs), gfs->browse_button, FALSE, FALSE, 0);
gtk_signal_connect (GTK_OBJECT(gfs->browse_button), "clicked",
GTK_SIGNAL_FUNC (gimp_file_selection_browse_callback),
gfs);
gtk_widget_show (gfs->browse_button);
gfs->entry = gtk_entry_new ();
1999-05-19 01:33:39 +08:00
gtk_box_pack_end (GTK_BOX (gfs), gfs->entry, TRUE, TRUE, 0);
gtk_signal_connect (GTK_OBJECT (gfs->entry), "activate",
GTK_SIGNAL_FUNC (gimp_file_selection_entry_callback),
gfs);
gtk_signal_connect (GTK_OBJECT (gfs->entry), "focus_out_event",
GTK_SIGNAL_FUNC (gimp_file_selection_entry_focus_out_callback),
gfs);
gtk_widget_show (gfs->entry);
}
GtkType
add sample_colorize and curve_bend defs * plug-ins/common/plugin-defs.pl: add sample_colorize and curve_bend defs * libgimp/color_selector.h: minor consistency cleanup * libgimp/gimpchainbutton.[ch]: use new style gtk object helper macros * libgimp/gimpfileselection.c * libgimp/gimpmatrix.h: minor cleanup * libgimp/gimpintl.h: resync with gnome-i18n.h * libgimp/color_display.h * app/gimp.sym * app/gdisplay_color.[ch] * app/app_procs.c * app/gdisplay.h * app/image_render.c: color display transformation code. Still unfinished, so it's not activated yet. * app/buildmenu.h: remove unused defines (PULLDOWN, POPUP, OPTION) * app/colormaps.[ch] * app/image_render.c: remove vestigal dithering stuff * app/convolve.h * app/gimpdrawable.h * app/gimpimage.h * app/lut_funcs.h * app/paint_funcs.h * app/plug_in.h: enum nick changes from Marc * app/channel_ops.c * app/crop.c * app/gdisplay.c * app/gimpimage.[ch] * app/move.c: s/([A-Z]+)_GUIDE/ORIENTATION_$1/ * app/flip_tool.[ch] * app/shear_tool.[ch]: use ORIENTATION_* constants instead of our own * app/disp_callbacks.c: remove HORIZONTAL and VERTICAL #defines * app/general.h: enumified TOKEN_* symbols * app/lc_dialog.c * app/paint_funcs.c: remove unused variables * tools/pdbgen/lib.pl: autogen gimpenums.h (unfinished) * tools/pdbgen/stddefs.pdb: new std_orientation_enum, remove layer_mode shortcut since we've skipped it in app/ * tools/pdbgen/pdb/brush_select.pdb * tools/pdbgen/pdb/brushes.pdb * tools/pdbgen/pdb/drawable.pdb * tools/pdbgen/pdb/gimage.pdb * tools/pdbgen/pdb/guides.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/tools.pdb: reflect above enum changes, whitespace cleanups * tools/pdbgen/enums.pl * app/brush_select_cmds.c * app/brushes_cmds.c * app/color_cmds.c * app/drawable_cmds.c * app/gimage_cmds.c * app/layer_cmds.c * app/procedural_db_cmds.c * app/tools_cmds.c: reflect pdb and enum nick changes above -Yosh
1999-07-29 07:00:08 +08:00
gimp_file_selection_get_type (void)
{
static GtkType gfs_type = 0;
if (!gfs_type)
{
GtkTypeInfo gfs_info =
{
"GimpFileSelection",
sizeof (GimpFileSelection),
sizeof (GimpFileSelectionClass),
(GtkClassInitFunc) gimp_file_selection_class_init,
(GtkObjectInitFunc) gimp_file_selection_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL
};
gfs_type = gtk_type_unique (gtk_hbox_get_type (), &gfs_info);
}
return gfs_type;
}
GtkWidget *
gimp_file_selection_new (gchar *title,
gchar *filename,
gboolean dir_only,
gboolean check_valid)
{
GimpFileSelection *gfs;
gfs = gtk_type_new (gimp_file_selection_get_type ());
gfs->title = g_strdup (title);
gfs->dir_only = dir_only;
gfs->check_valid = check_valid;
1999-05-19 01:33:39 +08:00
gimp_file_selection_set_filename (gfs, filename);
return GTK_WIDGET (gfs);
}
static void
gimp_file_selection_realize (GtkWidget *widget)
{
GimpFileSelection *gfs;
GtkStyle *style;
gfs = GIMP_FILE_SELECTION (widget);
if (! gfs->check_valid)
return;
if (GTK_WIDGET_CLASS (parent_class)->realize)
(* GTK_WIDGET_CLASS (parent_class)->realize) (widget);
style = gtk_widget_get_style (widget);
gfs->yes_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
&gfs->yes_mask,
&style->bg[GTK_STATE_NORMAL],
yes_xpm);
gfs->no_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
&gfs->no_mask,
&style->bg[GTK_STATE_NORMAL],
no_xpm);
gfs->file_exists = gtk_pixmap_new (gfs->no_pixmap, gfs->no_mask);
gtk_box_pack_start (GTK_BOX (gfs), gfs->file_exists, FALSE, FALSE, 0);
gimp_file_selection_check_filename (gfs);
gtk_widget_show (gfs->file_exists);
}
gchar *
gimp_file_selection_get_filename (GimpFileSelection *gfs)
{
g_return_val_if_fail (gfs != NULL, g_strdup (""));
g_return_val_if_fail (GIMP_IS_FILE_SELECTION (gfs), g_strdup (""));
return gtk_editable_get_chars (GTK_EDITABLE (gfs->entry), 0, -1);
}
void
gimp_file_selection_set_filename (GimpFileSelection *gfs,
gchar *filename)
{
g_return_if_fail (gfs != NULL);
g_return_if_fail (GIMP_IS_FILE_SELECTION (gfs));
1999-11-07 02:19:41 +08:00
gtk_entry_set_text (GTK_ENTRY (gfs->entry), filename);
/* update everything
*/
gimp_file_selection_entry_callback (gfs->entry, (gpointer) gfs);
}
static void
gimp_file_selection_entry_callback (GtkWidget *widget,
gpointer data)
{
GimpFileSelection *gfs;
gchar *filename;
gint len;
gfs = GIMP_FILE_SELECTION (data);
/* filenames still need more sanity checking
* (erase double G_DIR_SEPARATORS, ...)
*/
filename = gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1);
filename = g_strstrip (filename);
while (((len = strlen (filename)) > 1) &&
(filename[len - 1] == G_DIR_SEPARATOR))
filename[len - 1] = '\0';
gtk_signal_handler_block_by_data (GTK_OBJECT (gfs->entry), gfs);
gtk_entry_set_text (GTK_ENTRY (gfs->entry), filename);
gtk_signal_handler_unblock_by_data (GTK_OBJECT (gfs->entry), gfs);
if (gfs->file_selection)
gtk_file_selection_set_filename (GTK_FILE_SELECTION (gfs->file_selection),
filename);
g_free (filename);
gimp_file_selection_check_filename (gfs);
1999-05-19 01:33:39 +08:00
gtk_entry_set_position (GTK_ENTRY (gfs->entry), -1);
gtk_signal_emit (GTK_OBJECT (gfs),
gimp_file_selection_signals[FILENAME_CHANGED]);
}
static int
gimp_file_selection_entry_focus_out_callback (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
gimp_file_selection_entry_callback (widget, data);
return TRUE;
}
/* local callbacks of gimp_file_selection_browse_callback() */
static void
gimp_file_selection_filesel_ok_callback (GtkWidget *widget,
gpointer data)
{
GimpFileSelection *gfs;
gchar *filename;
gfs = GIMP_FILE_SELECTION (data);
filename =
gtk_file_selection_get_filename (GTK_FILE_SELECTION (gfs->file_selection));
gtk_entry_set_text (GTK_ENTRY (gfs->entry), filename);
/* update everything */
gimp_file_selection_entry_callback (gfs->entry, data);
}
static void
gimp_file_selection_filesel_cancel_callback (GtkWidget *widget,
gpointer data)
{
gtk_widget_hide (GIMP_FILE_SELECTION (data)->file_selection);
}
static int
gimp_file_selection_filesel_delete_callback (GtkWidget *widget,
GdkEvent *event,
gpointer data)
{
gimp_file_selection_filesel_cancel_callback (NULL, data);
return TRUE;
}
static void
gimp_file_selection_browse_callback (GtkWidget *widget,
gpointer data)
{
GimpFileSelection *gfs;
gchar *filename;
gfs = GIMP_FILE_SELECTION (data);
filename = gtk_editable_get_chars (GTK_EDITABLE (gfs->entry), 0, -1);
if (gfs->file_selection == NULL)
{
if (gfs->dir_only)
{
gfs->file_selection = gtk_file_selection_new (gfs->title);
/* hiding these widgets uses internal gtk+ knowledge, but it's
* easier than creating my own directory browser -- michael
*/
gtk_widget_hide
(GTK_FILE_SELECTION (gfs->file_selection)->fileop_del_file);
gtk_widget_hide
(GTK_FILE_SELECTION (gfs->file_selection)->file_list->parent);
}
else
{
gfs->file_selection = gtk_file_selection_new (_("Select File"));
}
gtk_label_set_text (GTK_LABEL (GTK_BIN (GTK_FILE_SELECTION (gfs->file_selection)->ok_button)->child), _("Select"));
gtk_label_set_text (GTK_LABEL (GTK_BIN (GTK_FILE_SELECTION (gfs->file_selection)->cancel_button)->child), _("Close"));
gtk_signal_connect
(GTK_OBJECT (GTK_FILE_SELECTION (gfs->file_selection)->ok_button),
"clicked",
GTK_SIGNAL_FUNC (gimp_file_selection_filesel_ok_callback),
gfs);
gtk_signal_connect
(GTK_OBJECT (GTK_FILE_SELECTION (gfs->file_selection)->selection_entry),
"activate",
GTK_SIGNAL_FUNC (gimp_file_selection_filesel_ok_callback),
gfs);
gtk_signal_connect
(GTK_OBJECT (GTK_FILE_SELECTION (gfs->file_selection)->cancel_button),
"clicked",
GTK_SIGNAL_FUNC (gimp_file_selection_filesel_cancel_callback),
gfs);
gtk_signal_connect
(GTK_OBJECT (gfs), "unmap",
GTK_SIGNAL_FUNC (gimp_file_selection_filesel_cancel_callback),
gfs);
gtk_signal_connect
(GTK_OBJECT (gfs->file_selection), "delete_event",
GTK_SIGNAL_FUNC (gimp_file_selection_filesel_delete_callback),
gfs);
}
gtk_file_selection_set_filename (GTK_FILE_SELECTION (gfs->file_selection),
filename);
if (! GTK_WIDGET_VISIBLE (gfs->file_selection))
gtk_widget_show (gfs->file_selection);
else
gdk_window_raise (gfs->file_selection->window);
}
static void
gimp_file_selection_check_filename (GimpFileSelection *gfs)
{
static struct stat statbuf;
gchar* filename;
if (! gfs->check_valid)
return;
if (gfs->file_exists == NULL)
return;
filename = gtk_editable_get_chars (GTK_EDITABLE (gfs->entry), 0, -1);
if ((stat (filename, &statbuf) == 0) &&
(gfs->dir_only ? S_ISDIR(statbuf.st_mode) : S_ISREG(statbuf.st_mode)))
{
gtk_pixmap_set (GTK_PIXMAP (gfs->file_exists),
gfs->yes_pixmap, gfs->yes_mask);
}
else
{
gtk_pixmap_set (GTK_PIXMAP (gfs->file_exists),
gfs->no_pixmap, gfs->no_mask);
}
1999-11-07 02:19:41 +08:00
g_free (filename);
}