]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/FileDialogPrivate.C
9e1e49a2169d998325c9271fbf53ffcc398c284b
[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
28 namespace lyx {
29
30 using lyx::docstring;
31
32 using std::string;
33
34 FileDialog::Private::Private(docstring const & title,
35                              kb_action action,
36                              FileDialog::Button /*b1*/,
37                              FileDialog::Button /*b2*/) :
38         action_(action),
39         fileChooser_("You shouldn't see this", Gtk::FILE_CHOOSER_ACTION_OPEN)
40 {
41         fileChooser_.set_title(lyx::to_utf8(title));
42 }
43
44
45 FileDialog::Result const
46 FileDialog::Private::open(docstring const & path,
47                           lyx::support::FileFilterList const & filters,
48                           docstring const & suggested)
49 {
50         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_OPEN);
51         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
52         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
53
54         return showChooser(path, filters, suggested);
55 }
56
57
58 FileDialog::Result const FileDialog::Private::opendir(docstring const & path,
59                                                       docstring const & suggested)
60 {
61         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
62         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
63         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
64
65         return showChooser(path, lyx::support::FileFilterList(), suggested);
66 }
67
68
69 FileDialog::Result const FileDialog::Private::save(docstring const & path,
70                                                    lyx::support::FileFilterList const & filters,
71                                                    docstring const & suggested)
72 {
73         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);
74         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
75         fileChooser_.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
76
77         return showChooser(path, filters, suggested);
78 }
79
80
81 FileDialog::Result const FileDialog::Private::showChooser(docstring const & path,
82                           lyx::support::FileFilterList const & filters,
83                           docstring const & suggested)
84 {
85         lyxerr[Debug::GUI] << "File Dialog with path \"" << lyx::to_utf8(path)
86                            << "\", mask \"" << lyx::to_utf8(filters.as_string())
87                            << "\", suggested \"" << lyx::to_utf8(suggested) << "\"\n";
88
89         for (lyx::support::FileFilterList::size_type i = 0; i < filters.size(); ++i) {
90                 typedef lyx::support::FileFilterList::Filter::glob_iterator glob_iterator;
91                 glob_iterator it = filters[i].begin();
92                 glob_iterator const end = filters[i].end();
93                 if (it == end)
94                         continue;
95
96                 Gtk::FileFilter filter;
97                 filter.set_name(lyx::to_utf8(filters[i].description()));
98                 for (; it != end; ++it)
99                         filter.add_pattern(*it);
100
101                 fileChooser_.add_filter(filter);
102         }
103
104         if (!path.empty())
105                 fileChooser_.set_current_folder(lyx::to_utf8(path));
106         if (!suggested.empty())
107                 fileChooser_.set_current_name(lyx::to_utf8(suggested));
108
109         fileChooser_.set_default_response(Gtk::RESPONSE_OK);
110         Result result;
111         result.first = FileDialog::Chosen;
112         if (fileChooser_.run() == Gtk::RESPONSE_OK)
113                 result.second = lyx::from_utf8(fileChooser_.get_filename());
114         else
115                 result.second = docstring();
116         return result;
117 }
118
119
120 } // namespace lyx