]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlCharacter.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlCharacter.C
1 /**
2  * \file ControlCharacter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlCharacter.h"
14 #include "ButtonController.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "bufferview_funcs.h"
19 #include "funcrequest.h"
20 #include "language.h"
21 #include "LColor.h"
22
23 using bv_funcs::font2string;
24
25 using std::string;
26
27 namespace lyx {
28 namespace frontend {
29
30 ControlCharacter::ControlCharacter(Dialog & parent)
31         : Dialog::Controller(parent),
32           font_(0), toggleall_(false)
33 {}
34
35
36 bool ControlCharacter::initialiseParams(string const &)
37 {
38         // Do this the first time only.
39         if (!font_.get())
40                 font_.reset(new LyXFont(LyXFont::ALL_IGNORE));
41
42         // so that the user can press Ok
43         if (getFamily()   != LyXFont::IGNORE_FAMILY ||
44             getSeries()   != LyXFont::IGNORE_SERIES ||
45             getShape()    != LyXFont::IGNORE_SHAPE  ||
46             getSize()     != LyXFont::IGNORE_SIZE ||
47             getBar()      != IGNORE ||
48             getColor()    != LColor::ignore ||
49             font_->language() != ignore_language)
50                 dialog().bc().valid();
51
52         return true;
53 }
54
55
56 void ControlCharacter::clearParams()
57 {}
58
59
60 void ControlCharacter::dispatchParams()
61 {
62         // Nothing to dispatch. (Can be called from the Toolbar.)
63         if (!font_.get())
64                 return;
65
66         string data;
67         if (font2string(*font_.get(), toggleall_, data)) {
68                 kernel().dispatch(FuncRequest(getLfun(), data));
69         }
70 }
71
72
73 LyXFont::FONT_FAMILY ControlCharacter::getFamily() const
74 {
75         if (!font_.get())
76                 return LyXFont::IGNORE_FAMILY;
77         return font_->family();
78 }
79
80
81 void ControlCharacter::setFamily(LyXFont::FONT_FAMILY val)
82 {
83         font_->setFamily(val);
84 }
85
86
87 LyXFont::FONT_SERIES ControlCharacter::getSeries() const
88 {
89         if (!font_.get())
90                 return LyXFont::IGNORE_SERIES;
91         return font_->series();
92 }
93
94
95 void ControlCharacter::setSeries(LyXFont::FONT_SERIES val)
96 {
97         font_->setSeries(val);
98 }
99
100
101 LyXFont::FONT_SHAPE ControlCharacter::getShape() const
102 {
103         if (!font_.get())
104                 return LyXFont::IGNORE_SHAPE;
105         return font_->shape();
106 }
107
108
109 void ControlCharacter::setShape(LyXFont::FONT_SHAPE val)
110 {
111         font_->setShape(val);
112 }
113
114
115 LyXFont::FONT_SIZE ControlCharacter::getSize() const
116 {
117         if (!font_.get())
118                 return LyXFont::IGNORE_SIZE;
119         return font_->size();
120 }
121
122
123 void ControlCharacter::setSize(LyXFont::FONT_SIZE val)
124 {
125         font_->setSize(val);
126 }
127
128
129 FONT_STATE ControlCharacter::getBar() const
130 {
131         if (!font_.get())
132                 return IGNORE;
133
134         if (font_->emph() == LyXFont::TOGGLE)
135                 return EMPH_TOGGLE;
136
137         if (font_->underbar() == LyXFont::TOGGLE)
138                 return UNDERBAR_TOGGLE;
139
140         if (font_->noun() == LyXFont::TOGGLE)
141                 return NOUN_TOGGLE;
142
143         if (font_->emph() == LyXFont::IGNORE &&
144             font_->underbar() == LyXFont::IGNORE &&
145             font_->noun() == LyXFont::IGNORE)
146                 return IGNORE;
147
148         return INHERIT;
149 }
150
151
152 void ControlCharacter::setBar(FONT_STATE val)
153 {
154         switch (val) {
155         case IGNORE:
156                 font_->setEmph(LyXFont::IGNORE);
157                 font_->setUnderbar(LyXFont::IGNORE);
158                 font_->setNoun(LyXFont::IGNORE);
159                 break;
160
161         case EMPH_TOGGLE:
162                 font_->setEmph(LyXFont::TOGGLE);
163                 break;
164
165         case UNDERBAR_TOGGLE:
166                 font_->setUnderbar(LyXFont::TOGGLE);
167                 break;
168
169         case NOUN_TOGGLE:
170                 font_->setNoun(LyXFont::TOGGLE);
171                 break;
172
173         case INHERIT:
174                 font_->setEmph(LyXFont::INHERIT);
175                 font_->setUnderbar(LyXFont::INHERIT);
176                 font_->setNoun(LyXFont::INHERIT);
177                 break;
178         }
179 }
180
181
182 LColor_color ControlCharacter::getColor() const
183 {
184         if (!font_.get())
185                 return LColor::ignore;
186
187         return font_->color();
188 }
189
190
191 void ControlCharacter::setColor(LColor_color val)
192 {
193         switch (val) {
194         case LColor::ignore:
195         case LColor::none:
196         case LColor::black:
197         case LColor::white:
198         case LColor::red:
199         case LColor::green:
200         case LColor::blue:
201         case LColor::cyan:
202         case LColor::magenta:
203         case LColor::yellow:
204         case LColor::inherit:
205                 font_->setColor(val);
206                 break;
207         default:
208                 break;
209         }
210 }
211
212
213 string ControlCharacter::getLanguage() const
214 {
215         if (font_.get() && font_->language())
216                 return font_->language()->lang();
217         return "ignore";
218 }
219
220
221 void ControlCharacter::setLanguage(string const & val)
222 {
223         if (val == "ignore")
224                 font_->setLanguage(ignore_language);
225
226         else if (val == "reset")
227                 font_->setLanguage(kernel().buffer().params().language);
228
229         else
230                 font_->setLanguage(languages.getLanguage(val));
231 }
232
233
234 bool ControlCharacter::getToggleAll() const
235 {
236         return toggleall_;
237 }
238
239
240 void ControlCharacter::setToggleAll(bool t)
241 {
242         toggleall_ = t;
243 }
244
245 } // namespace frontend
246 } // namespace lyx