3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Vigna (Minipage stuff)
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
18 #include "GuiApplication.h"
19 #include "ColorCache.h"
21 #include "LengthCombo.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
26 #include "insets/InsetBox.h"
28 #include "support/gettext.h"
29 #include "support/foreach.h"
30 #include "support/lstrings.h"
34 #include <QPushButton>
46 static QStringList boxGuiIds()
49 << "Frameless" << "Boxed"
50 << "ovalbox" << "Ovalbox"
51 << "Shadowbox" << "Shaded"
56 static QStringList boxGuiNames()
59 << qt_("No frame") << qt_("Simple rectangular frame")
60 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
61 << qt_("Drop shadow") << qt_("Shaded background")
62 << qt_("Double rectangular frame");
66 static QStringList boxGuiSpecialLengthIds()
68 return QStringList() << "height" << "depth"
69 << "totalheight" << "width";
73 static QStringList boxGuiSpecialLengthNames()
75 return QStringList() << qt_("Height") << qt_("Depth")
76 << qt_("Total Height") << qt_("Width");
80 static QList<ColorPair> colorData()
82 QList<ColorPair> colors;
83 colors << ColorPair(qt_("none"), Color_none);
84 colors << ColorPair(qt_("black"), Color_black);
85 colors << ColorPair(qt_("white"), Color_white);
86 colors << ColorPair(qt_("blue"), Color_blue);
87 colors << ColorPair(qt_("brown"), Color_brown);
88 colors << ColorPair(qt_("cyan"), Color_cyan);
89 colors << ColorPair(qt_("darkgray"), Color_darkgray);
90 colors << ColorPair(qt_("gray"), Color_gray);
91 colors << ColorPair(qt_("green"), Color_green);
92 colors << ColorPair(qt_("lightgray"), Color_lightgray);
93 colors << ColorPair(qt_("lime"), Color_lime);
94 colors << ColorPair(qt_("magenta"), Color_magenta);
95 colors << ColorPair(qt_("olive"), Color_olive);
96 colors << ColorPair(qt_("orange"), Color_orange);
97 colors << ColorPair(qt_("pink"), Color_pink);
98 colors << ColorPair(qt_("purple"), Color_purple);
99 colors << ColorPair(qt_("red"), Color_red);
100 colors << ColorPair(qt_("teal"), Color_teal);
101 colors << ColorPair(qt_("violet"), Color_violet);
102 colors << ColorPair(qt_("yellow"), Color_yellow);
108 void fillComboColor(QComboBox * combo, QList<T> const & list, bool const is_none)
110 QPixmap coloritem(32, 32);
112 // frameColorCO cannot be uncolored
114 combo->addItem(qt_("none"));
115 typename QList<T>::const_iterator cit = list.begin() + 1;
116 for (; cit != list.end(); ++cit) {
117 color = QColor(guiApp->colorCache().get(cit->second, false));
118 coloritem.fill(color);
119 combo->addItem(QIcon(coloritem), cit->first);
125 static int findPos2nd(QList<P> const & vec, QString val)
127 for (int i = 0; i != vec.size(); ++i)
128 if (vec[i].first == val)
134 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
138 // fill the box type choice
140 gui_names_ = boxGuiNames();
141 for (int i = 0; i != ids_.size(); ++i)
142 typeCO->addItem(gui_names_[i], ids_[i]);
144 // add the special units to the height choice
145 // width needs different handling
146 ids_spec_ = boxGuiSpecialLengthIds();
147 gui_names_spec_ = boxGuiSpecialLengthNames();
148 for (int i = 0; i != ids_spec_.size(); ++i)
149 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
151 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
152 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
153 this, SIGNAL(changed()));
154 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
155 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
156 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
157 this, SIGNAL(changed()));
158 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
159 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
160 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
161 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
162 this, SIGNAL(changed()));
163 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
164 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
165 this, SIGNAL(changed()));
166 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
167 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
168 this, SIGNAL(changed()));
169 connect(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
170 connect(backgroundColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
172 heightED->setValidator(unsignedLengthValidator(heightED));
173 widthED->setValidator(unsignedLengthValidator(widthED));
174 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
175 separationED->setValidator(unsignedLengthValidator(separationED));
176 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
178 // initialize the length validator
179 addCheckedWidget(widthED, widthCB);
180 addCheckedWidget(heightED, heightCB);
181 addCheckedWidget(thicknessED, thicknessLA);
182 addCheckedWidget(separationED, separationLA);
183 addCheckedWidget(shadowsizeED, shadowsizeLA);
187 // the background can be uncolored while the frame cannot
188 fillComboColor(frameColorCO, color, false);
189 fillComboColor(backgroundColorCO, color, true);
195 void GuiBox::on_innerBoxCO_activated(int /* index */)
198 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
199 // handle parbox and minipage the same way
200 bool const ibox = (itype != "none" && itype != "makebox");
201 if (heightCB->isChecked() && !ibox)
202 heightCB->setChecked(false);
203 widthCB->setChecked(!widthED->text().isEmpty());
209 void GuiBox::on_typeCO_activated(int index)
212 typeCO->itemData(index).toString();
213 bool const frameless = (type == "Frameless");
215 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
216 setInnerType(frameless, itype);
217 // refresh itype because it might have been changed in setInnerType
219 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
220 // handle parbox and minipage the same way
221 bool const ibox = (itype != "none" && itype != "makebox");
222 if (frameless && itype != "makebox") {
223 if (heightCB->isChecked() && !ibox)
224 heightCB->setChecked(false);
227 if (type != "Boxed") {
228 if (type != "Frameless")
229 widthCB->setChecked(itype != "none");
230 pagebreakCB->setChecked(false);
232 // assure that the frame color is black for frameless boxes to
233 // provide the color "none"
234 if (frameless && frameColorCO->currentIndex() != 0)
235 frameColorCO->setCurrentIndex(0);
240 void GuiBox::on_frameColorCO_currentIndexChanged(int /* index */)
242 // if there is a non-black frame color the background canot be uncolored
243 // therefore remove the entry "none" in this case
244 if (frameColorCO->currentIndex() != 0) {
245 if (backgroundColorCO->count() == color.count()) {
246 if (backgroundColorCO->currentIndex() == 0)
247 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("white")));
248 backgroundColorCO->removeItem(0);
251 if (backgroundColorCO->count() == color.count() - 1)
252 backgroundColorCO->insertItem(0, qt_("none"));
258 void GuiBox::initDialog()
260 setInnerType(true, toqstr("minipage"));
261 widthED->setText("100");
262 widthCB->setChecked(true);
263 widthCB->setEnabled(false);
264 widthUnitsLC->setCurrentItem(Length::PCW);
265 heightED->setText("1");
266 heightUnitsLC->setCurrentItem("totalheight");
267 // LaTeX's default for \fboxrule is 0.4 pt
268 thicknessED->setText("0.4");
269 thicknessUnitsLC->setCurrentItem(Length::PT);
270 // LaTeX's default for \fboxsep is 3 pt
271 separationED->setText("3");
272 separationUnitsLC->setCurrentItem(Length::PT);
273 // LaTeX's default for \shadowsize is 4 pt
274 shadowsizeED->setText("4");
275 shadowsizeUnitsLC->setCurrentItem(Length::PT);
279 void GuiBox::on_widthCB_stateChanged(int)
285 void GuiBox::on_heightCB_stateChanged(int /*state*/)
291 void GuiBox::on_pagebreakCB_stateChanged()
293 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
295 widthCB->setChecked(!pbreak);
297 on_typeCO_activated(typeCO->currentIndex());
305 void GuiBox::paramsToDialog(Inset const * inset)
307 InsetBox const * box = static_cast<InsetBox const *>(inset);
308 InsetBoxParams const & params = box->params();
309 QString type = toqstr(params.type);
310 if (type == "Framed") {
311 pagebreakCB->setChecked(true);
314 pagebreakCB->setChecked(false);
317 typeCO->setCurrentIndex(typeCO->findData(type));
320 QString inner_type = "minipage";
321 if (!params.inner_box)
323 if (params.use_parbox)
324 inner_type = "parbox";
325 if (params.use_makebox)
326 inner_type = "makebox";
327 bool const frameless = (params.type == "Frameless");
328 setInnerType(frameless, inner_type);
331 valignCO->setCurrentIndex(string("tcb").find(c, 0));
332 c = params.inner_pos;
333 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
335 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
337 bool ibox = (params.inner_box && !params.use_makebox);
338 valignCO->setEnabled(ibox);
339 ialignCO->setEnabled(ibox);
342 // halign is only allowed if a width is used
343 halignCO->setEnabled(widthCB->isChecked());
344 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
345 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
346 && halignCO->count() < 4)
347 halignCO->addItem(toqstr("Stretch"));
348 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
349 halignCO->removeItem(3);
350 // pagebreak is only allowed for Boxed without inner box
351 pagebreakCB->setEnabled(!ibox && type == "Boxed");
353 Length::UNIT const default_unit = Length::defaultUnit();
355 // the width can only be selected for makebox or framebox
356 widthCB->setEnabled(inner_type == "makebox"
358 && !ibox && !pagebreakCB->isChecked()));
359 if (params.width.empty()) {
360 widthCB->setChecked(false);
361 lengthToWidgets(widthED, widthUnitsLC,
362 params.width, default_unit);
364 widthCB->setChecked(true);
365 lengthToWidgets(widthED, widthUnitsLC,
366 params.width, default_unit);
367 QString const special = toqstr(params.special);
368 if (!special.isEmpty() && special != "none")
369 widthUnitsLC->setCurrentItem(special);
372 widthED->setEnabled(widthCB->isChecked());
373 widthUnitsLC->setEnabled(widthCB->isChecked());
375 lengthToWidgets(heightED, heightUnitsLC,
376 (params.height).asString(), default_unit);
378 QString const height_special = toqstr(params.height_special);
379 if (!height_special.isEmpty() && height_special != "none")
380 heightUnitsLC->setCurrentItem(height_special);
381 // set no optional height if the value is the default "1\height"
382 // (special units like \height are handled as "in",
383 // FIXME: this is a very bad UI, this check box should be disabled in
384 // this case, not forced to 'unchecked' state.
385 if (height_special == "totalheight" && params.height == Length("1in"))
386 heightCB->setCheckState(Qt::Unchecked);
388 heightCB->setCheckState(Qt::Checked);
390 heightCB->setEnabled(ibox);
392 // enable line thickness only for the rectangular frame types and drop shadow
393 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
394 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
395 lengthToWidgets(thicknessED, thicknessUnitsLC,
396 (params.thickness).asString(), default_unit);
397 // enable line separation for the allowed frame types
398 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
399 || type == "Doublebox" || type == "Shadowbox");
400 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
401 || type == "Doublebox" || type == "Shadowbox");
402 lengthToWidgets(separationED, separationUnitsLC,
403 (params.separation).asString(), default_unit);
404 // enable shadow size for drop shadow
405 shadowsizeED->setEnabled(type == "Shadowbox");
406 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
407 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
408 (params.shadowsize).asString(), default_unit);
410 frameColorCO->setCurrentIndex(findPos2nd(color, qt_(params.framecolor)) - 1);
411 // only if the framecolor is black the backgroundcolor has the entry "none"
412 if (frameColorCO->currentIndex() != 0)
413 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)) - 1);
415 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)));
419 docstring GuiBox::dialogToParams() const
421 bool const pagebreak =
422 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
427 box_type = fromqstr(typeCO->itemData(
428 typeCO->currentIndex()).toString());
430 InsetBoxParams params(box_type);
432 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
434 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
436 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
438 params.pos = "tcb"[valignCO->currentIndex()];
439 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
440 params.hor_pos = "lcrs"[halignCO->currentIndex()];
443 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
444 QString value = widthED->text();
446 if (widthED->isEnabled()) {
447 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
448 params.special = fromqstr(unit);
449 // Note: the unit is simply ignored in this case
450 params.width = Length(value.toDouble(), Length::IN);
452 params.special = "none";
453 // we must specify a valid length in this case
455 widthED->setText("0");
456 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
459 params.special = "none";
460 params.width = Length();
463 // the height parameter is omitted if the value
464 // is "1in" and "Total Height" is used as unit.
465 // 1in + "Total Height" means "1\height" which is the LaTeX default
466 // if no height is given
467 if (heightCB->checkState() == Qt::Unchecked) {
468 params.height = Length("1in");
469 params.height_special = "totalheight";
471 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
472 value = heightED->text();
473 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
474 params.height_special = fromqstr(unit);
475 // Note: the unit is simply ignored in this case
476 params.height = Length(value.toDouble(), Length::IN);
478 params.height_special = "none";
480 Length(widgetsToLength(heightED, heightUnitsLC));
484 // handle the line thickness, line separation and shadow size
485 if (thicknessED->isEnabled())
486 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
488 params.thickness = Length("0.4pt");
489 if (separationED->isEnabled())
490 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
492 params.separation = Length("3pt");
493 if (separationED->isEnabled())
494 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
496 params.shadowsize = Length("4pt");
497 if (frameColorCO->isEnabled())
498 params.framecolor = lcolor.getLaTeXName(color[frameColorCO->currentIndex() + 1].second);
500 params.framecolor = "black";
501 if (backgroundColorCO->isEnabled()) {
502 // only if the framecolor is black the backgroundcolor has the entry "none"
503 if (frameColorCO->currentIndex() != 0)
504 params.backgroundcolor = lcolor.getLaTeXName(color[backgroundColorCO->currentIndex() + 1].second);
506 params.backgroundcolor = lcolor.getLaTeXName(color[backgroundColorCO->currentIndex()].second);
508 params.backgroundcolor = "none";
510 return from_ascii(InsetBox::params2string(params));
514 bool GuiBox::checkWidgets(bool readonly) const
516 typeCO->setEnabled(!readonly);
519 pagebreakCB->setEnabled(false);
520 innerBoxCO->setEnabled(false);
521 valignCO->setEnabled(false);
522 ialignCO->setEnabled(false);
523 halignCO->setEnabled(false);
524 widthCB->setEnabled(false);
525 widthED->setEnabled(false);
526 widthUnitsLC->setEnabled(false);
527 heightED->setEnabled(false);
528 heightUnitsLC->setEnabled(false);
529 heightCB->setEnabled(false);
530 thicknessED->setEnabled(false);
531 thicknessUnitsLC->setEnabled(false);
532 separationED->setEnabled(false);
533 separationUnitsLC->setEnabled(false);
534 shadowsizeED->setEnabled(false);
535 shadowsizeUnitsLC->setEnabled(false);
537 QString const outer =
538 typeCO->itemData(typeCO->currentIndex()).toString();
539 QString const itype =
540 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
541 bool const ibox = (itype != "none" && itype != "makebox");
542 valignCO->setEnabled(ibox);
543 ialignCO->setEnabled(ibox);
544 if (heightCB->isChecked() && !ibox)
545 heightCB->setChecked(false);
546 heightCB->setEnabled(ibox);
547 // the width can only be selected for makebox or framebox
548 widthCB->setEnabled(itype == "makebox"
549 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
550 // except for Frameless and Boxed, the width cannot be specified if
551 // there is no inner box
552 bool const width_enabled =
553 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
554 // enable if width_enabled
555 widthED->setEnabled(width_enabled);
556 widthUnitsLC->setEnabled(width_enabled);
557 if (!widthCB->isChecked() && widthCB->isEnabled()) {
558 widthED->setEnabled(false);
559 widthUnitsLC->setEnabled(false);
561 // halign is only allowed if a width is used
562 halignCO->setEnabled(widthCB->isChecked());
563 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
564 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
565 && halignCO->count() < 4)
566 halignCO->addItem(toqstr("Stretch"));
567 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
568 halignCO->removeItem(3);
569 // pagebreak is only allowed for Boxed without inner box
570 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
572 heightED->setEnabled(itype != "none" && heightCB->isChecked());
573 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
574 heightCB->setEnabled(ibox);
576 // enable line thickness for the rectangular frame types and drop shadow
577 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
578 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
579 // set default values if empty
580 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
581 thicknessED->setText("0.4");
582 thicknessUnitsLC->setCurrentItem(Length::PT);
584 // enable line separation for the allowed frame types
585 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
586 || outer == "Doublebox" || outer == "Shadowbox");
587 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
588 || outer == "Doublebox" || outer == "Shadowbox");
589 // set default values if empty
590 if (separationED->text().isEmpty() && separationED->isEnabled()) {
591 separationED->setText("3");
592 separationUnitsLC->setCurrentItem(Length::PT);
594 // enable shadow size for drop shadow
595 shadowsizeED->setEnabled(outer == "Shadowbox");
596 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
597 // set default values if empty
598 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
599 shadowsizeED->setText("4");
600 shadowsizeUnitsLC->setCurrentItem(Length::PT);
602 // \fboxcolor and \colorbox cannot be used for fancybox boxes
603 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
604 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
607 return InsetParamsWidget::checkWidgets();
611 void GuiBox::setSpecial(bool ibox)
613 QString const last_item =
614 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
616 // check if the widget contains the special units
617 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
618 // insert 'em if needed...
619 if (!ibox && !has_special) {
620 for (int i = 1; i < ids_spec_.size(); ++i)
621 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
622 // ... or remove 'em if needed
623 } else if (ibox && has_special) {
624 for (int i = 1; i < ids_spec_.size(); ++i) {
625 int n = widthUnitsLC->findData(ids_spec_[i]);
627 widthUnitsLC->removeItem(n);
630 // restore selected text, if possible
631 widthUnitsLC->setCurrentItem(last_item);
635 void GuiBox::setInnerType(bool frameless, QString const & type)
637 // with "frameless" boxes, inner box is mandatory
638 // (i.e. is the actual box)
639 // we have to remove "none" then and adjust the combo
642 innerBoxCO->addItem(qt_("None"), toqstr("none"));
644 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
645 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
646 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
647 int i = (innerBoxCO->findData(type) != -1)
648 ? innerBoxCO->findData(type) : 0;
649 innerBoxCO->setCurrentIndex(i);
652 } // namespace frontend
656 #include "moc_GuiBox.cpp"