]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/FileDialogPrivate.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / gtk / FileDialogPrivate.C
1 /**
2  * \file FileDialogPrivate.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  * \author John Spray
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 // Too hard to make concept checks work with this file
15 #ifdef _GLIBCXX_CONCEPT_CHECKS
16 #undef _GLIBCXX_CONCEPT_CHECKS
17 #endif
18 #ifdef _GLIBCPP_CONCEPT_CHECKS
19 #undef _GLIBCPP_CONCEPT_CHECKS
20 #endif
21
22 #include "FileDialogPrivate.h"
23
24 #include "support/filefilterlist.h"
25 #include "debug.h"
26
27 using std::string;
28
29 FileDialog::Private::Private(string const & title,
30                              kb_action action,
31                              FileDialog::Button /*b1*/,
32                              FileDialog::Button /*b2*/) :
33         action_(action),
34         fileChooser_("You shouldn't see this", Gtk::FILE_CHOOSER_ACTION_OPEN)
35 {
36         fileChooser_.set_title(title);
37 }
38
39
40 FileDialog::Result const
41 FileDialog::Private::open(string const & path,
42                           lyx::support::FileFilterList const & filters,
43                           string const & suggested)
44 {
45         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_OPEN);
46         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
47         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
48
49         return showChooser(path, filters, suggested);
50 }
51
52
53 FileDialog::Result const FileDialog::Private::opendir(string const & path,
54                                                       string const & suggested)
55 {
56         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
57         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
58         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
59
60         return showChooser(path, lyx::support::FileFilterList(), suggested);
61 }
62
63
64 FileDialog::Result const FileDialog::Private::save(string const & path,
65                                                    lyx::support::FileFilterList const & filters,
66                                                    string const & suggested)
67 {
68         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);
69         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
70         fileChooser_.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
71
72         return showChooser(path, filters, suggested);
73 }
74
75
76 FileDialog::Result const FileDialog::Private::showChooser(string const & path,
77                           lyx::support::FileFilterList const & filters,
78                           string const & suggested)
79 {
80         lyxerr[Debug::GUI] << "File Dialog with path \"" << path
81                            << "\", mask \"" << filters.as_string()
82                            << "\", suggested \"" << suggested << "\"\n";
83
84         for (lyx::support::FileFilterList::size_type i = 0; i < filters.size(); ++i) {
85                 typedef lyx::support::FileFilterList::Filter::glob_iterator glob_iterator;
86                 glob_iterator it = filters[i].begin();
87                 glob_iterator const end = filters[i].end();
88                 if (it == end)
89                         continue;
90
91                 Gtk::FileFilter filter;
92                 filter.set_name(filters[i].description());
93                 for (; it != end; ++it)
94                         filter.add_pattern(*it);
95
96                 fileChooser_.add_filter(filter);
97         }
98
99         if (!path.empty())
100                 fileChooser_.set_current_folder(path);
101         if (!suggested.empty())
102                 fileChooser_.set_current_name(suggested);               
103                 
104         fileChooser_.set_default_response(Gtk::RESPONSE_OK);
105         Result result;
106         result.first = FileDialog::Chosen;
107         if (fileChooser_.run() == Gtk::RESPONSE_OK)
108                 result.second = fileChooser_.get_filename();
109         else
110                 result.second = string();
111         return result;
112 }