]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/FileDialogPrivate.C
get rid of broken_header.h and some unneeded tests
[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 _GLIBCPP_CONCEPT_CHECKS
16 #undef _GLIBCPP_CONCEPT_CHECKS
17 #endif
18
19 #include "FileDialogPrivate.h"
20
21 #include "support/filefilterlist.h"
22 #include "debug.h"
23
24 using std::string;
25
26 FileDialog::Private::Private(string const & title,
27                              kb_action action,
28                              FileDialog::Button /*b1*/,
29                              FileDialog::Button /*b2*/) :
30         action_(action),
31         fileChooser_("You shouldn't see this", Gtk::FILE_CHOOSER_ACTION_OPEN)
32 {
33         fileChooser_.set_title(title);
34 }
35
36
37 FileDialog::Result const
38 FileDialog::Private::open(string const & path,
39                           lyx::support::FileFilterList const & filters,
40                           string const & suggested)
41 {
42         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_OPEN);
43         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
44         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
45
46         return showChooser(path, filters, suggested);
47 }
48
49
50 FileDialog::Result const FileDialog::Private::opendir(string const & path,
51                                                       string const & suggested)
52 {
53         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
54         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
55         fileChooser_.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
56
57         return showChooser(path, lyx::support::FileFilterList(), suggested);
58 }
59
60
61 FileDialog::Result const FileDialog::Private::save(string const & path,
62                                                    lyx::support::FileFilterList const & filters,
63                                                    string const & suggested)
64 {
65         fileChooser_.set_action(Gtk::FILE_CHOOSER_ACTION_SAVE);
66         fileChooser_.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
67         fileChooser_.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);
68
69         return showChooser(path, filters, suggested);
70 }
71
72
73 FileDialog::Result const FileDialog::Private::showChooser(string const & path,
74                           lyx::support::FileFilterList const & filters,
75                           string const & suggested)
76 {
77         lyxerr[Debug::GUI] << "File Dialog with path \"" << path
78                            << "\", mask \"" << filters.as_string()
79                            << "\", suggested \"" << suggested << "\"\n";
80
81         for (lyx::support::FileFilterList::size_type i = 0; i < filters.size(); ++i) {
82                 typedef lyx::support::FileFilterList::Filter::glob_iterator glob_iterator;
83                 glob_iterator it = filters[i].begin();
84                 glob_iterator const end = filters[i].end();
85                 if (it == end)
86                         continue;
87
88                 Gtk::FileFilter filter;
89                 filter.set_name(filters[i].description());
90                 for (; it != end; ++it)
91                         filter.add_pattern(*it);
92
93                 fileChooser_.add_filter(filter);
94         }
95
96         if (!path.empty())
97                 fileChooser_.set_filename(path);
98         fileChooser_.set_default_response(Gtk::RESPONSE_OK);
99         Result result;
100         result.first = FileDialog::Chosen;
101         if (fileChooser_.run() == Gtk::RESPONSE_OK)
102                 result.second = fileChooser_.get_filename();
103         else
104                 result.second = string();
105         return result;
106 }