Debian Codegraph @

src/tools/dlgedit/gui_file.cc

raw
1 /*
2 Copyright (C) 2002/2004 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file common/gui_file.cc
21 *
22 * @author Kai Sterker
23 * @brief Wrapper for the GtkFileChooser widget
24 */
25
26 #include <gtk/gtk.h>
27 #include "gui_file.h"
28 #include "util.h"
29
30 // create a new file selection window
31 GuiFile::GuiFile (GtkWindow *parent, GtkFileChooserAction action, const std::string &title, const std::string &file)
32 : GuiModalDialog (parent)
33 {
34 const gchar *label;
35
36 switch (action)
37 {
38 case GTK_FILE_CHOOSER_ACTION_SAVE:
39 {
40 label = GTK_STOCK_SAVE;
41 break;
42 }
43 case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER:
44 {
45 label = GTK_STOCK_NEW;
46 break;
47 }
48 default: label = GTK_STOCK_OPEN;
49 }
50
51 window = gtk_file_chooser_dialog_new (title.c_str (), parent, action,
52 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, label, GTK_RESPONSE_ACCEPT, NULL);
53
54 // forbid multiple selections
55 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER(window), FALSE);
56
57 // set current directory or file
58 if (file[file.length()-1] == '/')
59 {
60 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(window), file.c_str ());
61 }
62 else
63 {
64 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(window), file.c_str ());
65 }
66
67 if (action == GTK_FILE_CHOOSER_ACTION_SAVE || action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER)
68 {
69 gchar *name = g_path_get_basename (file.c_str ());
70 gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(window), name);
71 g_free (name);
72 }
73 }
74
75 // clean up
76 GuiFile::~GuiFile ()
77 {
78 }
79
80 // display file select widget
81 bool GuiFile::run ()
82 {
83 if (gtk_dialog_run (GTK_DIALOG (window)) == GTK_RESPONSE_ACCEPT)
84 {
85 gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (window));
86 File = MK_UNIX_PATH (filename);
87 g_free (filename);
88
89 return true;
90 }
91
92 return false;
93 }
94
95 void GuiFile::add_shortcut (const std::string & shortcut)
96 {
97 if (shortcut.length () > 0)
98 {
99 gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER(window), shortcut.c_str (), NULL);
100 }
101 }
102
103 void GuiFile::add_filter (const std::string & pattern, const std::string & name)
104 {
105 unsigned long pos;
106 std::string pattern_str = pattern;
107 GtkFileFilter *filter = gtk_file_filter_new ();
108 gtk_file_filter_set_name (filter, name.c_str ());
109
110 while ((pos = pattern_str.find ('|')) != std::string::npos)
111 {
112 std::string filter_str = pattern_str.substr (0, pos);
113 pattern_str = pattern_str.substr (pos + 1);
114 gtk_file_filter_add_pattern (filter, filter_str.c_str ());
115 }
116
117 gtk_file_filter_add_pattern (filter, pattern_str.c_str ());
118 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(window), filter);
119 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER(window), filter);
120 }