]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiCharacter.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[lyx.git] / src / frontends / qt4 / GuiCharacter.cpp
1 /**
2  * \file GuiCharacter.cpp
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  * \author Edwin Leuven
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiCharacter.h"
16
17 #include "qt_helpers.h"
18 #include "Font.h"
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "FuncRequest.h"
22 #include "Language.h"
23
24
25 using namespace std;
26
27 namespace lyx {
28 namespace frontend {
29
30 static QList<ShapePair> shapeData()
31 {
32         QList<ShapePair> shapes;
33         shapes << ShapePair(qt_("No change"), IGNORE_SHAPE);
34         shapes << ShapePair(qt_("Upright"), UP_SHAPE);
35         shapes << ShapePair(qt_("Italic"), ITALIC_SHAPE);
36         shapes << ShapePair(qt_("Slanted"), SLANTED_SHAPE);
37         shapes << ShapePair(qt_("Small Caps"), SMALLCAPS_SHAPE);
38         shapes << ShapePair(qt_("Reset"), INHERIT_SHAPE);
39         return shapes;
40 }
41
42
43 static QList<SizePair> sizeData()
44 {
45         QList<SizePair> sizes;
46         sizes << SizePair(qt_("No change"), FONT_SIZE_IGNORE);
47         sizes << SizePair(qt_("Tiny"), FONT_SIZE_TINY);
48         sizes << SizePair(qt_("Smallest"), FONT_SIZE_SCRIPT);
49         sizes << SizePair(qt_("Smaller"), FONT_SIZE_FOOTNOTE);
50         sizes << SizePair(qt_("Small"), FONT_SIZE_SMALL);
51         sizes << SizePair(qt_("Normal"), FONT_SIZE_NORMAL);
52         sizes << SizePair(qt_("Large"), FONT_SIZE_LARGE);
53         sizes << SizePair(qt_("Larger"), FONT_SIZE_LARGER);
54         sizes << SizePair(qt_("Largest"), FONT_SIZE_LARGEST);
55         sizes << SizePair(qt_("Huge"), FONT_SIZE_HUGE);
56         sizes << SizePair(qt_("Huger"), FONT_SIZE_HUGER);
57         sizes << SizePair(qt_("Increase"), FONT_SIZE_INCREASE);
58         sizes << SizePair(qt_("Decrease"), FONT_SIZE_DECREASE);
59         sizes << SizePair(qt_("Reset"), FONT_SIZE_INHERIT);
60         return sizes;
61 }
62
63
64 static QList<BarPair> barData()
65 {
66         QList<BarPair> bars;
67         bars << BarPair(qt_("No change"), IGNORE);
68         bars << BarPair(qt_("Emph"),      EMPH_TOGGLE);
69         bars << BarPair(qt_("Underbar"),  UNDERBAR_TOGGLE);
70         bars << BarPair(qt_("Noun"),      NOUN_TOGGLE);
71         bars << BarPair(qt_("Reset"),     INHERIT);
72         return bars;
73 }
74
75
76 static QList<ColorPair> colorData()
77 {
78         QList<ColorPair> colors;
79         colors << ColorPair(qt_("No change"), Color_ignore);
80         colors << ColorPair(qt_("No color"), Color_none);
81         colors << ColorPair(qt_("Black"), Color_black);
82         colors << ColorPair(qt_("White"), Color_white);
83         colors << ColorPair(qt_("Red"), Color_red);
84         colors << ColorPair(qt_("Green"), Color_green);
85         colors << ColorPair(qt_("Blue"), Color_blue);
86         colors << ColorPair(qt_("Cyan"), Color_cyan);
87         colors << ColorPair(qt_("Magenta"), Color_magenta);
88         colors << ColorPair(qt_("Yellow"), Color_yellow);
89         colors << ColorPair(qt_("Reset"), Color_inherit);
90         return colors;
91 }
92
93
94 static QList<SeriesPair> seriesData()
95 {
96         QList<SeriesPair> series;
97         series << SeriesPair(qt_("No change"), IGNORE_SERIES);
98         series << SeriesPair(qt_("Medium"),    MEDIUM_SERIES);
99         series << SeriesPair(qt_("Bold"),      BOLD_SERIES);
100         series << SeriesPair(qt_("Reset"),     INHERIT_SERIES);
101         return series;
102 }
103
104
105 static QList<FamilyPair> familyData()
106 {
107         QList<FamilyPair> families;
108         families << FamilyPair(qt_("No change"),  IGNORE_FAMILY);
109         families << FamilyPair(qt_("Roman"),      ROMAN_FAMILY);
110         families << FamilyPair(qt_("Sans Serif"), SANS_FAMILY);
111         families << FamilyPair(qt_("Typewriter"), TYPEWRITER_FAMILY);
112         families << FamilyPair(qt_("Reset"),      INHERIT_FAMILY);
113         return families;
114 }
115
116
117 GuiCharacter::GuiCharacter(GuiView & lv)
118         : GuiDialog(lv, "character", qt_("Text Style")), font_(ignore_font),
119           toggleall_(false), reset_lang_(false)
120 {
121         setupUi(this);
122
123         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
124         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
125         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
126
127         connect(miscCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
128         connect(sizeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
129         connect(familyCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
130         connect(seriesCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
131         connect(shapeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
132         connect(colorCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
133         connect(langCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
134         connect(toggleallCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
135
136 #ifdef Q_WS_MACX
137         // On Mac it's common to have tool windows which are always in the
138         // foreground and are hidden when the main window is not focused.
139         setWindowFlags(Qt::Tool);
140         autoapplyCB->setChecked(true);
141 #endif
142
143         family = familyData();
144         series = seriesData();
145         shape  = shapeData();
146         size   = sizeData();
147         bar    = barData();
148         color  = colorData();
149         language = languageData(true);
150
151         for (QList<FamilyPair>::const_iterator cit = family.begin();
152                 cit != family.end(); ++cit) {
153                 familyCO->addItem(cit->first);
154         }
155
156         for (QList<SeriesPair>::const_iterator cit = series.begin();
157                 cit != series.end(); ++cit) {
158                 seriesCO->addItem(cit->first);
159         }
160         for (QList<ShapePair>::const_iterator cit = shape.begin();
161                 cit != shape.end(); ++cit) {
162                 shapeCO->addItem(cit->first);
163         }
164         for (QList<SizePair>::const_iterator cit = size.begin();
165                 cit != size.end(); ++cit) {
166                 sizeCO->addItem(cit->first);
167         }
168         for (QList<BarPair>::const_iterator cit = bar.begin();
169                 cit != bar.end(); ++cit) {
170                 miscCO->addItem(cit->first);
171         }
172         for (QList<ColorPair>::const_iterator cit = color.begin();
173                 cit != color.end(); ++cit) {
174                 colorCO->addItem(cit->first);
175         }
176         for (QList<LanguagePair>::const_iterator cit = language.begin();
177                 cit != language.end(); ++cit) {
178                 langCO->addItem(cit->first);
179         }
180
181         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
182         bc().setOK(okPB);
183         bc().setApply(applyPB);
184         bc().setCancel(closePB);
185         bc().addReadOnly(familyCO);
186         bc().addReadOnly(seriesCO);
187         bc().addReadOnly(sizeCO);
188         bc().addReadOnly(shapeCO);
189         bc().addReadOnly(miscCO);
190         bc().addReadOnly(langCO);
191         bc().addReadOnly(colorCO);
192         bc().addReadOnly(toggleallCB);
193         bc().addReadOnly(autoapplyCB);
194
195 // FIXME: hack to work around resizing bug in Qt >= 4.2
196 // bug verified with Qt 4.2.{0-3} (JSpitzm)
197 #if QT_VERSION >= 0x040200
198         // qt resizes the comboboxes only after show(), so ...
199         QDialog::show();
200 #endif
201 }
202
203
204 void GuiCharacter::change_adaptor()
205 {
206         changed();
207
208         if (!autoapplyCB->isChecked())
209                 return;
210
211         // to be really good here, we should set the combos to the values of
212         // the current text, and make it appear as "no change" if the values
213         // stay the same between applys. Might be difficult though wrt to a
214         // moved cursor - jbl
215         slotApply();
216         familyCO->setCurrentIndex(0);
217         seriesCO->setCurrentIndex(0);
218         sizeCO->setCurrentIndex(0);
219         shapeCO->setCurrentIndex(0);
220         miscCO->setCurrentIndex(0);
221         langCO->setCurrentIndex(0);
222         colorCO->setCurrentIndex(0);
223 }
224
225
226 template<class B>
227 static int findPos2nd(QList<pair<QString, B> > const & vec, B const & val)
228 {
229         for (int i = 0; i != vec.size(); ++i)
230                 if (vec[i].second == val)
231                         return i;
232         return 0;
233 }
234
235
236 void GuiCharacter::updateContents()
237 {
238         familyCO->setCurrentIndex(findPos2nd(family, getFamily()));
239         seriesCO->setCurrentIndex(findPos2nd(series, getSeries()));
240         shapeCO->setCurrentIndex(findPos2nd(shape, getShape()));
241         sizeCO->setCurrentIndex(findPos2nd(size, getSize()));
242         miscCO->setCurrentIndex(findPos2nd(bar, getBar()));
243         colorCO->setCurrentIndex(findPos2nd(color, getColor()));
244         langCO->setCurrentIndex(findPos2nd(language, getLanguage()));
245
246         toggleallCB->setChecked(toggleall_);
247 }
248
249
250 void GuiCharacter::applyView()
251 {
252         setFamily(family[familyCO->currentIndex()].second);
253         setSeries(series[seriesCO->currentIndex()].second);
254         setShape(shape[shapeCO->currentIndex()].second);
255         setSize(size[sizeCO->currentIndex()].second);
256         setBar(bar[miscCO->currentIndex()].second);
257         setColor(color[colorCO->currentIndex()].second);
258         setLanguage(language[langCO->currentIndex()].second);
259
260         toggleall_ = toggleallCB->isChecked();
261 }
262
263
264 bool GuiCharacter::initialiseParams(string const &)
265 {
266         // so that the user can press Ok
267         if (getFamily()    != IGNORE_FAMILY
268             || getSeries() != IGNORE_SERIES
269             || getShape()  != IGNORE_SHAPE
270             || getSize()   != FONT_SIZE_IGNORE
271             || getBar()    != IGNORE
272             || getColor()  != Color_ignore
273             || font_.language() != ignore_language)
274                 setButtonsValid(true);
275
276         return true;
277 }
278
279
280 void GuiCharacter::dispatchParams()
281 {
282         dispatch(FuncRequest(getLfun(), font_.toString(toggleall_)));
283 }
284
285
286 FontFamily GuiCharacter::getFamily() const
287 {
288         return font_.fontInfo().family();
289 }
290
291
292 void GuiCharacter::setFamily(FontFamily val)
293 {
294         font_.fontInfo().setFamily(val);
295 }
296
297
298 FontSeries GuiCharacter::getSeries() const
299 {
300         return font_.fontInfo().series();
301 }
302
303
304 void GuiCharacter::setSeries(FontSeries val)
305 {
306         font_.fontInfo().setSeries(val);
307 }
308
309
310 FontShape GuiCharacter::getShape() const
311 {
312         return font_.fontInfo().shape();
313 }
314
315
316 void GuiCharacter::setShape(FontShape val)
317 {
318         font_.fontInfo().setShape(val);
319 }
320
321
322 FontSize GuiCharacter::getSize() const
323 {
324         return font_.fontInfo().size();
325 }
326
327
328 void GuiCharacter::setSize(FontSize val)
329 {
330         font_.fontInfo().setSize(val);
331 }
332
333
334 FontState GuiCharacter::getBar() const
335 {
336         if (font_.fontInfo().emph() == FONT_TOGGLE)
337                 return EMPH_TOGGLE;
338
339         if (font_.fontInfo().underbar() == FONT_TOGGLE)
340                 return UNDERBAR_TOGGLE;
341
342         if (font_.fontInfo().noun() == FONT_TOGGLE)
343                 return NOUN_TOGGLE;
344
345         if (font_.fontInfo().emph() == FONT_IGNORE
346             && font_.fontInfo().underbar() == FONT_IGNORE
347             && font_.fontInfo().noun() == FONT_IGNORE)
348                 return IGNORE;
349
350         return INHERIT;
351 }
352
353
354 void GuiCharacter::setBar(FontState val)
355 {
356         switch (val) {
357         case IGNORE:
358                 font_.fontInfo().setEmph(FONT_IGNORE);
359                 font_.fontInfo().setUnderbar(FONT_IGNORE);
360                 font_.fontInfo().setNoun(FONT_IGNORE);
361                 break;
362
363         case EMPH_TOGGLE:
364                 font_.fontInfo().setEmph(FONT_TOGGLE);
365                 break;
366
367         case UNDERBAR_TOGGLE:
368                 font_.fontInfo().setUnderbar(FONT_TOGGLE);
369                 break;
370
371         case NOUN_TOGGLE:
372                 font_.fontInfo().setNoun(FONT_TOGGLE);
373                 break;
374
375         case INHERIT:
376                 font_.fontInfo().setEmph(FONT_INHERIT);
377                 font_.fontInfo().setUnderbar(FONT_INHERIT);
378                 font_.fontInfo().setNoun(FONT_INHERIT);
379                 break;
380         }
381 }
382
383
384 ColorCode GuiCharacter::getColor() const
385 {
386         return font_.fontInfo().color();
387 }
388
389
390 void GuiCharacter::setColor(ColorCode val)
391 {
392         switch (val) {
393         case Color_ignore:
394         case Color_none:
395         case Color_black:
396         case Color_white:
397         case Color_red:
398         case Color_green:
399         case Color_blue:
400         case Color_cyan:
401         case Color_magenta:
402         case Color_yellow:
403         case Color_inherit:
404                 font_.fontInfo().setColor(val);
405                 break;
406         default:
407                 break;
408         }
409 }
410
411
412 QString GuiCharacter::getLanguage() const
413 {
414         if (reset_lang_)
415                 return "reset";
416         if (font_.language())
417                 return toqstr(font_.language()->lang());
418         return "ignore";
419 }
420
421
422 void GuiCharacter::setLanguage(QString const & val)
423 {
424         if (val == "ignore")
425                 font_.setLanguage(ignore_language);
426         else if (val == "reset") {
427                 reset_lang_ = true;
428                 // Ignored in getLanguage, but needed for dispatchParams
429                 font_.setLanguage(buffer().params().language);
430         } else {
431                 font_.setLanguage(languages.getLanguage(fromqstr(val)));
432         }
433 }
434
435
436 Dialog * createGuiCharacter(GuiView & lv) { return new GuiCharacter(lv); }
437
438
439 } // namespace frontend
440 } // namespace lyx
441
442 #include "GuiCharacter_moc.cpp"