]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
remove stupid windows defines
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
1 /**
2  * \file GuiBox.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna (Minipage stuff)
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiBox.h"
16
17 #include "FuncRequest.h"
18 #include "LengthCombo.h"
19 #include "Length.h"
20 #include "LyXRC.h" // to set the default length values
21 #include "qt_helpers.h"
22 #include "Validator.h"
23
24 #include "insets/InsetBox.h"
25
26 #include "support/gettext.h"
27 #include "support/foreach.h"
28 #include "support/lstrings.h"
29
30 #include <QPushButton>
31 #include <QLineEdit>
32
33 #ifdef IN
34 #undef IN
35 #endif
36
37 using namespace std;
38
39
40 namespace lyx {
41 namespace frontend {
42
43 static QStringList boxGuiIds()
44 {
45         return QStringList()
46                 << "Frameless" << "Boxed"
47                 << "ovalbox" << "Ovalbox"
48                 << "Shadowbox" << "Shaded"
49                 << "Doublebox";
50 }
51
52
53 static QStringList boxGuiNames()
54 {
55         return QStringList()
56                 << qt_("No frame") << qt_("Simple rectangular frame")
57                 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
58                 << qt_("Drop shadow") << qt_("Shaded background")
59                 << qt_("Double rectangular frame");
60 }
61
62
63 static QStringList boxGuiSpecialLengthIds()
64 {
65         return QStringList() << "none" << "height" << "depth"
66                 << "totalheight" << "width";
67 }
68
69
70 static QStringList boxGuiSpecialLengthNames()
71 {
72         return QStringList() << qt_("None") << qt_("Height") << qt_("Depth")
73                 << qt_("Total Height") << qt_("Width");
74 }
75
76
77 GuiBox::GuiBox(GuiView & lv)
78         : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
79 {
80         setupUi(this);
81
82         // fill the box type choice
83         ids_ = boxGuiIds();
84         gui_names_ = boxGuiNames();
85         foreach (QString const & str, gui_names_)
86                 typeCO->addItem(str);
87
88         // add the special units to the height choice
89         // width needs different handling
90         ids_spec_ = boxGuiSpecialLengthIds();
91         gui_names_spec_ = boxGuiSpecialLengthNames();
92         foreach (QString const & str, gui_names_spec_)
93                 heightUnitsLC->addItem(str);
94
95         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
96         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
97         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
98         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99
100         connect(widthED, SIGNAL(textChanged(QString)),
101                 this, SLOT(change_adaptor()));
102         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
103                 this, SLOT(change_adaptor()));
104         connect(valignCO, SIGNAL(highlighted(QString)),
105                 this, SLOT(change_adaptor()));
106         connect(heightCB, SIGNAL(stateChanged(int)),
107                 this, SLOT(change_adaptor()));
108         connect(heightED, SIGNAL(textChanged(QString)),
109                 this, SLOT(change_adaptor()));
110         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
111                 this, SLOT(change_adaptor()));
112         connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
113         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
115         connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117         connect(innerBoxCO, SIGNAL(activated(QString)),
118                 this, SLOT(innerBoxChanged(QString)));
119         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
120         connect(pagebreakCB, SIGNAL(stateChanged(int)),
121                 this, SLOT(pagebreakClicked()));
122
123         heightED->setValidator(unsignedLengthValidator(heightED));
124         widthED->setValidator(unsignedLengthValidator(widthED));
125
126         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
127
128         bc().addReadOnly(typeCO);
129         bc().addReadOnly(innerBoxCO);
130         bc().addReadOnly(valignCO);
131         bc().addReadOnly(ialignCO);
132         bc().addReadOnly(halignCO);
133         bc().addReadOnly(widthED);
134         bc().addReadOnly(widthUnitsLC);
135         bc().addReadOnly(heightCB);
136         bc().addReadOnly(heightED);
137         bc().addReadOnly(heightUnitsLC);
138         bc().addReadOnly(pagebreakCB);
139
140         bc().setRestore(restorePB);
141         bc().setOK(okPB);
142         bc().setApply(applyPB);
143         bc().setCancel(closePB);
144
145         // initialize the length validator
146         bc().addCheckedLineEdit(widthED, widthLA);
147         bc().addCheckedLineEdit(heightED, heightCB);
148 }
149
150
151 void GuiBox::change_adaptor()
152 {
153         changed();
154 }
155
156
157 void GuiBox::innerBoxChanged(QString const & str)
158 {
159         bool const ibox = (str != qt_("None"));
160         valignCO->setEnabled(ibox);
161         ialignCO->setEnabled(ibox);
162         halignCO->setEnabled(!ibox);
163         heightCB->setEnabled(ibox);
164         pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
165         if (heightCB->checkState() == Qt::Checked && ibox) {
166                 heightED->setEnabled(true);
167                 heightUnitsLC->setEnabled(true);
168         }
169         setSpecial(ibox);
170 }
171
172
173 void GuiBox::typeChanged(int index)
174 {
175         bool const frameless = (index == 0);
176         if (frameless) {
177                 valignCO->setEnabled(true);
178                 ialignCO->setEnabled(true);
179                 halignCO->setEnabled(false);
180                 heightCB->setEnabled(true);
181                 heightED->setEnabled(true);
182                 heightUnitsLC->setEnabled(true);
183                 setSpecial(true);
184         }
185         if (index != 1)
186                 pagebreakCB->setChecked(false);
187         int itype = innerBoxCO->currentIndex();
188         pagebreakCB->setEnabled(index == 1 && itype == 0);
189         setInnerType(frameless, itype);
190 }
191
192
193 void GuiBox::restoreClicked()
194 {
195         setInnerType(true, 2);
196         widthED->setText("100");
197         widthUnitsLC->setCurrentItem(Length::PCW);
198         heightCB->setCheckState(Qt::Checked);
199         heightED->setText("1");
200         for (int i = 0; i != heightUnitsLC->count(); ++i) {
201                 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
202                         heightUnitsLC->setCurrentItem(i);
203         }
204 }
205
206
207 void GuiBox::pagebreakClicked()
208 {
209         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
210         innerBoxCO->setEnabled(!pbreak);
211         if (pbreak) {
212                 valignCO->setEnabled(false);
213                 ialignCO->setEnabled(false);
214                 halignCO->setEnabled(false);
215                 heightCB->setEnabled(false);
216                 heightED->setEnabled(false);
217                 heightUnitsLC->setEnabled(false);
218                 setSpecial(false);
219         } else {
220                 typeChanged(typeCO->currentIndex());
221         }
222         change_adaptor();
223 }
224
225
226 void GuiBox::updateContents()
227 {
228         QString type = toqstr(params_.type);
229         if (type == "Framed") {
230                 pagebreakCB->setChecked(true);
231                 type = "Boxed";
232         } else {
233                 pagebreakCB->setChecked(false);
234         }
235
236         pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
237
238         for (int i = 0; i != gui_names_.size(); ++i) {
239                 if (type == ids_[i])
240                         typeCO->setCurrentIndex(i);
241         }
242
243         // default: minipage
244         int inner_type = 2;
245         if (!params_.inner_box)
246                 // none
247                 inner_type = 0;
248         if (params_.use_parbox)
249                 // parbox
250                 inner_type = 1;
251         bool frameless = (params_.type == "Frameless");
252         setInnerType(frameless, inner_type);
253
254         char c = params_.pos;
255         valignCO->setCurrentIndex(string("tcb").find(c, 0));
256         c = params_.inner_pos;
257         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
258         c = params_.hor_pos;
259         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
260
261         bool ibox = params_.inner_box;
262         valignCO->setEnabled(ibox);
263         ialignCO->setEnabled(ibox);
264         halignCO->setEnabled(!ibox);
265         setSpecial(ibox);
266
267         Length::UNIT default_unit =
268                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
269
270         lengthToWidgets(widthED, widthUnitsLC,
271                 (params_.width).asString(), default_unit);
272
273         QString const special = toqstr(params_.special);
274         if (!special.isEmpty() && special != "none") {
275                 QString spc;
276                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
277                         if (special == ids_spec_[i])
278                                 spc = gui_names_spec_[i];
279                 }
280                 for (int i = 0; i != widthUnitsLC->count(); ++i) {
281                         if (widthUnitsLC->itemText(i) == spc)
282                                 widthUnitsLC->setCurrentIndex(i);
283                 }
284         }
285
286         lengthToWidgets(heightED, heightUnitsLC,
287                 (params_.height).asString(), default_unit);
288         
289         QString const height_special = toqstr(params_.height_special);
290         if (!height_special.isEmpty() && height_special != "none") {
291                 QString hspc;
292                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
293                         if (height_special == ids_spec_[i])
294                                 hspc = gui_names_spec_[i];
295                 }
296                 for (int i = 0; i != heightUnitsLC->count(); ++i) {
297                         if (heightUnitsLC->itemText(i) == hspc)
298                                 heightUnitsLC->setCurrentIndex(i);
299                 }
300         }
301         // set no optional height when the value is the default "1\height"
302         // (special units like \height are handled as "in",
303         if (height_special == "totalheight" &&  params_.height == Length("1in"))
304                 heightCB->setCheckState(Qt::Unchecked);
305         else
306                 heightCB->setCheckState(Qt::Checked);
307
308         heightCB->setEnabled(ibox);
309 }
310
311
312 void GuiBox::applyView()
313 {
314         bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
315         if (pagebreak)
316                 params_.type = "Framed";
317         else
318                 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
319
320         params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
321         params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
322
323         params_.pos = "tcb"[valignCO->currentIndex()];
324         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
325         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
326
327         int i = 0;
328         bool spec = false;
329         QString special = widthUnitsLC->currentText();
330         QString value = widthED->text();
331         if (special == qt_("Height")) {
332                 i = 1;
333                 spec = true;
334         } else if (special == qt_("Depth")) {
335                 i = 2;
336                 spec = true;
337         } else if (special == qt_("Total Height")) {
338                 i = 3;
339                 spec = true;
340         } else if (special == qt_("Width")) {
341                 i = 4;
342                 spec = true;
343         }
344         // the user might insert a non-special value in the line edit
345         if (isValidLength(fromqstr(value))) {
346                 i = 0;
347                 spec = false;
348         }
349         params_.special = fromqstr(ids_spec_[i]);
350
351         string width;
352         if (spec) {
353                 width = fromqstr(value);
354                 // beware: bogosity! the unit is simply ignored in this case
355                 width += "in";
356         } else {
357                 width = widgetsToLength(widthED, widthUnitsLC);
358         }
359
360         params_.width = Length(width);
361
362         i = 0;
363         spec = false;
364         special = heightUnitsLC->currentText();
365         value = heightED->text();
366         if (special == qt_("Height")) {
367                 i = 1;
368                 spec = true;
369         } else if (special == qt_("Depth")) {
370                 i = 2;
371                 spec = true;
372         } else if (special == qt_("Total Height")) {
373                 i = 3;
374                 spec = true;
375         } else if (special == qt_("Width")) {
376                 i = 4;
377                 spec = true;
378         }
379         // the user might insert a non-special value in the line edit
380         if (isValidLength(fromqstr(value))) {
381                 i = 0;
382                 spec = false;
383         }
384         params_.height_special = fromqstr(ids_spec_[i]);
385
386         string height;
387         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
388                 height = fromqstr(value);
389                 // beware: bogosity! the unit is simply ignored in this case
390                 height += "in";
391         } else
392                 height = widgetsToLength(heightED, heightUnitsLC);
393
394         // the height parameter is omitted in InsetBox.cpp when the value
395         // is "1in" and "Total Height" is used as unit.
396         // 1in + "Total Height" means "1\height" which is the LaTeX default when
397         // no height is given
398         if (heightCB->checkState() == Qt::Checked)
399                 params_.height = Length(height);
400         else {
401                 params_.height = Length("1in");
402                 params_.height_special = fromqstr(ids_spec_[3]);
403         }
404 }
405
406
407 void GuiBox::setSpecial(bool ibox)
408 {
409         // FIXME: Needed? Already done in the constructor
410         ids_spec_ = boxGuiSpecialLengthIds();
411         gui_names_spec_ = boxGuiSpecialLengthNames();
412
413         // check if the widget contains the special units
414         int count = widthUnitsLC->count();
415         bool has_special = false;
416         for (int i = 0; i != count; ++i)
417                 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
418                         has_special = true;
419         // insert 'em if needed...
420         if (!ibox && !has_special) {
421                 for (int i = 1; i < gui_names_spec_.size(); ++i)
422                         widthUnitsLC->addItem(gui_names_spec_[i]);
423         // ... or remove 'em if needed
424         } else if (ibox && has_special) {
425                 widthUnitsLC->clear();
426                 for (int i = 0; i != num_units; ++i)
427                         widthUnitsLC->addItem(qt_(unit_name_gui[i]));
428         }
429 }
430
431
432 void GuiBox::setInnerType(bool frameless, int i)
433 {
434         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
435         // we have to remove "none" then and adjust the combo
436         if (frameless) {
437                 innerBoxCO->clear();
438                 innerBoxCO->addItem(qt_("Parbox"));
439                 innerBoxCO->addItem(qt_("Minipage"));
440                 if (i != 0)
441                         innerBoxCO->setCurrentIndex(i - 1);
442                 else
443                         innerBoxCO->setCurrentIndex(i);
444         } else {
445                 if (innerBoxCO->count() == 2)
446                         ++i;
447                 innerBoxCO->clear();
448                 innerBoxCO->addItem(qt_("None"));
449                 innerBoxCO->addItem(qt_("Parbox"));
450                 innerBoxCO->addItem(qt_("Minipage"));
451                 innerBoxCO->setCurrentIndex(i);
452         }
453 }
454
455 bool GuiBox::initialiseParams(string const & data)
456 {
457         InsetBox::string2params(data, params_);
458         return true;
459
460 }
461
462
463 void GuiBox::clearParams()
464 {
465         params_ = InsetBoxParams("");
466 }
467
468
469 void GuiBox::dispatchParams()
470 {
471         dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
472 }
473
474
475 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
476
477
478 } // namespace frontend
479 } // namespace lyx
480
481
482 #include "GuiBox_moc.cpp"