]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlPrefs.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlPrefs.C
1 /**
2  * \file ControlPrefs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlPrefs.h"
14
15 #include "helper_funcs.h"
16 #include "Kernel.h"
17
18 #include "bufferlist.h"
19 #include "gettext.h"
20 #include "funcrequest.h"
21 #include "paper.h"
22 #include "LColor.h"
23
24 #include "support/filefilterlist.h"
25
26 #include <sstream>
27
28 using std::ostringstream;
29 using std::pair;
30 using std::string;
31 using std::vector;
32
33
34 extern BufferList bufferlist;
35
36 namespace lyx {
37
38 using support::FileFilterList;
39
40 namespace frontend {
41
42
43 ControlPrefs::ControlPrefs(Dialog & parent)
44         : Dialog::Controller(parent),
45           redraw_gui_(false),
46           update_screen_font_(false)
47 {}
48
49
50 bool ControlPrefs::initialiseParams(std::string const &)
51 {
52         rc_ = lyxrc;
53         formats_ = ::formats;
54         converters_ = ::converters;
55         converters_.update(formats_);
56         movers_ = ::movers;
57         colors_.clear();
58         redraw_gui_ = false;
59         update_screen_font_ = false;
60
61         return true;
62 }
63
64
65 void ControlPrefs::dispatchParams()
66 {
67         ostringstream ss;
68         rc_.write(ss, true);
69         kernel().dispatch(FuncRequest(LFUN_LYXRC_APPLY, ss.str()));
70
71         // FIXME: these need lfuns
72         bufferlist.setCurrentAuthor(rc_.user_name, rc_.user_email);
73
74         ::formats = formats_;
75
76         ::converters = converters_;
77         ::converters.update(::formats);
78         ::converters.buildGraph();
79
80         ::movers = movers_;
81
82         vector<string>::const_iterator it = colors_.begin();
83         vector<string>::const_iterator const end = colors_.end();
84         for (; it != end; ++it)
85                 kernel().dispatch(FuncRequest(LFUN_SET_COLOR, *it));
86         colors_.clear();
87
88         if (redraw_gui_) {
89                 kernel().redrawGUI();
90                 redraw_gui_ = false;
91         }
92
93         if (update_screen_font_) {
94                 kernel().dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
95                 update_screen_font_ = false;
96         }
97
98         // The Save button has been pressed
99         if (dialog().isClosing()) {
100                 kernel().dispatch(FuncRequest(LFUN_SAVEPREFERENCES));
101         }
102 }
103
104
105 void ControlPrefs::redrawGUI()
106 {
107         redraw_gui_ = true;
108 }
109
110
111 void ControlPrefs::setColor(LColor_color col, string const & hex)
112 {
113         colors_.push_back(lcolor.getLyXName(col) + ' ' + hex);
114 }
115
116
117 void ControlPrefs::updateScreenFonts()
118 {
119         update_screen_font_ = true;
120 }
121
122
123 string const ControlPrefs::browsebind(string const & file) const
124 {
125         return browseLibFile("bind", file, "bind", _("Choose bind file"),
126                           FileFilterList(_("LyX bind files (*.bind)")));
127 }
128
129
130 string const ControlPrefs::browseUI(string const & file) const
131 {
132         return browseLibFile("ui", file, "ui", _("Choose UI file"),
133                           FileFilterList(_("LyX UI files (*.ui)")));
134 }
135
136
137 string const ControlPrefs::browsekbmap(string const & file) const
138 {
139         return browseLibFile("kbd", file, "kmap", _("Choose keyboard map"),
140                           FileFilterList(_("LyX keyboard maps (*.kmap)")));
141 }
142
143
144 string const ControlPrefs::browsedict(string const & file) const
145 {
146         return browseFile(file, _("Choose personal dictionary"),
147                           FileFilterList(_("*.ispell")));
148 }
149
150
151 string const ControlPrefs::browse(string const & file,
152                                   string const & title) const
153 {
154         return browseFile(file, title, FileFilterList(), true);
155 }
156
157
158 string const ControlPrefs::browsedir(string const & path,
159                                      string const & title) const
160 {
161         return browseDir(path, title);
162 }
163
164
165 // We support less paper sizes than the document dialog
166 // Therefore this adjustment is needed.
167 PAPER_SIZE const ControlPrefs::toPaperSize(int i) const
168 {
169         switch (i) {
170         case 0:
171                 return PAPER_DEFAULT;
172         case 1:
173                 return PAPER_USLETTER;
174         case 2:
175                 return PAPER_USLEGAL;
176         case 3:
177                 return PAPER_USEXECUTIVE;
178         case 4:
179                 return PAPER_A3;
180         case 5:
181                 return PAPER_A4;
182         case 6:
183                 return PAPER_A5;
184         case 7:
185                 return PAPER_B5;
186         default:
187                 // should not happen
188                 return PAPER_DEFAULT;
189         }
190 }
191
192
193 int const ControlPrefs::fromPaperSize(PAPER_SIZE papersize) const
194 {
195         switch (papersize) {
196         case PAPER_DEFAULT:
197                 return 0;
198         case PAPER_USLETTER:
199                 return 1;
200         case PAPER_USLEGAL:
201                 return 2;
202         case PAPER_USEXECUTIVE:
203                 return 3;
204         case PAPER_A3:
205                 return 4;
206         case PAPER_A4:
207                 return 5;
208         case PAPER_A5:
209                 return 6;
210         case PAPER_B5:
211                 return 7;
212         default:
213                 // should not happen
214                 return 0;
215         }
216 }
217
218 } // namespace frontend
219 } // namespace lyx