]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
4c77d5e0 GB |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
25 | ||
50a14534 | 26 | #include <gtk/gtk.h> |
4c77d5e0 | 27 | #include <glib/gi18n.h> |
50a14534 EB |
28 | |
29 | #include "vikfileentry.h" | |
30 | ||
31 | static void choose_file ( VikFileEntry *vfe ); | |
32 | ||
33 | struct _VikFileEntry { | |
34 | GtkHBox parent; | |
35 | GtkWidget *entry, *button; | |
36 | GtkWidget *file_selector; | |
79672b85 | 37 | GtkFileChooserAction action; |
50a14534 EB |
38 | }; |
39 | ||
40 | GType vik_file_entry_get_type (void) | |
41 | { | |
42 | static GType vs_type = 0; | |
43 | ||
44 | if (!vs_type) | |
45 | { | |
46 | static const GTypeInfo vs_info = | |
47 | { | |
48 | sizeof (VikFileEntryClass), | |
49 | NULL, /* base_init */ | |
50 | NULL, /* base_finalize */ | |
51 | NULL, /* class init */ | |
52 | NULL, /* class_finalize */ | |
53 | NULL, /* class_data */ | |
54 | sizeof (VikFileEntry), | |
55 | 0, | |
56 | NULL /* instance init */ | |
57 | }; | |
58 | vs_type = g_type_register_static ( GTK_TYPE_HBOX, "VikFileEntry", &vs_info, 0 ); | |
59 | } | |
60 | ||
61 | return vs_type; | |
62 | } | |
63 | ||
79672b85 | 64 | GtkWidget *vik_file_entry_new (GtkFileChooserAction action) |
50a14534 EB |
65 | { |
66 | VikFileEntry *vfe = VIK_FILE_ENTRY ( g_object_new ( VIK_FILE_ENTRY_TYPE, NULL ) ); | |
67 | vfe->entry = gtk_entry_new (); | |
4c77d5e0 | 68 | vfe->button = gtk_button_new_with_label ( _("Browse...") ); |
79672b85 | 69 | vfe->action = action; |
50a14534 EB |
70 | g_signal_connect_swapped ( G_OBJECT(vfe->button), "clicked", G_CALLBACK(choose_file), vfe ); |
71 | ||
72 | gtk_box_pack_start ( GTK_BOX(vfe), vfe->entry, TRUE, TRUE, 3 ); | |
73 | gtk_box_pack_start ( GTK_BOX(vfe), vfe->button, FALSE, FALSE, 3 ); | |
74 | ||
75 | vfe->file_selector = NULL; | |
76 | ||
77 | return GTK_WIDGET(vfe); | |
78 | } | |
79 | ||
46f1e006 | 80 | const gchar *vik_file_entry_get_filename ( VikFileEntry *vfe ) |
50a14534 EB |
81 | { |
82 | return gtk_entry_get_text ( GTK_ENTRY(vfe->entry) ); | |
83 | } | |
84 | ||
85 | void vik_file_entry_set_filename ( VikFileEntry *vfe, const gchar *filename ) | |
86 | { | |
87 | gtk_entry_set_text ( GTK_ENTRY(vfe->entry), filename ); | |
88 | } | |
89 | ||
90 | static void choose_file ( VikFileEntry *vfe ) | |
91 | { | |
92 | if ( ! vfe->file_selector ) | |
93 | { | |
94 | GtkWidget *win; | |
95 | g_assert ( (win = gtk_widget_get_toplevel(GTK_WIDGET(vfe))) ); | |
6e4a49aa MA |
96 | vfe->file_selector = gtk_file_chooser_dialog_new (_("Choose file"), |
97 | GTK_WINDOW(win), | |
79672b85 | 98 | vfe->action, /* open file or directory */ |
6e4a49aa MA |
99 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
100 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, | |
101 | NULL); | |
50a14534 EB |
102 | gtk_window_set_transient_for ( GTK_WINDOW(vfe->file_selector), GTK_WINDOW(win) ); |
103 | gtk_window_set_destroy_with_parent ( GTK_WINDOW(vfe->file_selector), TRUE ); | |
104 | } | |
105 | ||
6e4a49aa MA |
106 | if ( gtk_dialog_run ( GTK_DIALOG(vfe->file_selector) ) == GTK_RESPONSE_ACCEPT ) |
107 | gtk_entry_set_text ( GTK_ENTRY (vfe->entry), gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(vfe->file_selector) ) ); | |
50a14534 EB |
108 | gtk_widget_hide ( vfe->file_selector ); |
109 | } | |
110 |