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
10 * Full author contact details are available in file CREDITS.
17 #include "FuncRequest.h"
18 #include "support/gettext.h"
20 #include "LengthCombo.h"
22 #include "qt_helpers.h"
23 #include "LyXRC.h" // to set the default length values
24 #include "Validator.h"
26 #include "insets/InsetBox.h"
28 #include "support/lstrings.h"
30 #include <QPushButton>
32 #include <QCloseEvent>
42 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
44 char const * const ids_[] = {
45 "Frameless", "Boxed", "ovalbox",
46 "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
47 size_t const ids_size = sizeof(ids_) / sizeof(char *);
48 ids = vector<string>(ids_, ids_ + ids_size);
50 gui_names.push_back(_("No frame"));
51 gui_names.push_back(_("Simple rectangular frame"));
52 gui_names.push_back(_("Oval frame, thin"));
53 gui_names.push_back(_("Oval frame, thick"));
54 gui_names.push_back(_("Drop shadow"));
55 gui_names.push_back(_("Shaded background"));
56 gui_names.push_back(_("Double rectangular frame"));
60 void box_gui_tokens_special_length(vector<string> & ids,
61 vector<docstring> & gui_names)
63 char const * const ids_[] = {
64 "none", "height", "depth",
65 "totalheight", "width"};
66 size_t const ids_size = sizeof(ids_) / sizeof(char *);
67 ids = vector<string>(ids_, ids_ + ids_size);
69 gui_names.push_back(_("None"));
70 gui_names.push_back(_("Height"));
71 gui_names.push_back(_("Depth"));
72 gui_names.push_back(_("Total Height"));
73 gui_names.push_back(_("Width"));
77 GuiBox::GuiBox(GuiView & lv)
78 : GuiDialog(lv, "box"), params_("")
81 setViewTitle(_("Box Settings"));
83 // fill the box type choice
84 box_gui_tokens(ids_, gui_names_);
85 for (unsigned int i = 0; i < gui_names_.size(); ++i)
86 typeCO->addItem(toqstr(gui_names_[i]));
88 // add the special units to the height choice
89 // width needs different handling
90 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
91 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
92 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
94 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
95 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
96 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
97 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99 connect(widthED, SIGNAL(textChanged(QString)),
100 this, SLOT(change_adaptor()));
101 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
102 this, SLOT(change_adaptor()));
103 connect(valignCO, SIGNAL(highlighted(QString)),
104 this, SLOT(change_adaptor()));
105 connect(heightCB, SIGNAL(stateChanged(int)),
106 this, SLOT(change_adaptor()));
107 connect(heightED, SIGNAL(textChanged(const QString &)),
108 this, SLOT(change_adaptor()));
109 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
110 this, SLOT(change_adaptor()));
111 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
112 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
114 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
115 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(innerBoxCO, SIGNAL(activated(const QString&)),
117 this, SLOT(innerBoxChanged(const QString &)));
118 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
119 connect(pagebreakCB, SIGNAL(stateChanged(int)),
120 this, SLOT(pagebreakClicked()));
122 heightED->setValidator(unsignedLengthValidator(heightED));
123 widthED->setValidator(unsignedLengthValidator(widthED));
125 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
127 bc().addReadOnly(typeCO);
128 bc().addReadOnly(innerBoxCO);
129 bc().addReadOnly(valignCO);
130 bc().addReadOnly(ialignCO);
131 bc().addReadOnly(halignCO);
132 bc().addReadOnly(widthED);
133 bc().addReadOnly(widthUnitsLC);
134 bc().addReadOnly(heightCB);
135 bc().addReadOnly(heightED);
136 bc().addReadOnly(heightUnitsLC);
137 bc().addReadOnly(pagebreakCB);
139 bc().setRestore(restorePB);
141 bc().setApply(applyPB);
142 bc().setCancel(closePB);
144 // initialize the length validator
145 bc().addCheckedLineEdit(widthED, widthLA);
146 bc().addCheckedLineEdit(heightED, heightCB);
150 void GuiBox::closeEvent(QCloseEvent * e)
157 void GuiBox::change_adaptor()
163 void GuiBox::innerBoxChanged(const QString & str)
165 bool const ibox = (str != qt_("None"));
166 valignCO->setEnabled(ibox);
167 ialignCO->setEnabled(ibox);
168 halignCO->setEnabled(!ibox);
169 heightCB->setEnabled(ibox);
170 if (heightCB->checkState() == Qt::Checked && ibox) {
171 heightED->setEnabled(true);
172 heightUnitsLC->setEnabled(true);
178 void GuiBox::typeChanged(int index)
180 bool const frameless = (index == 0);
182 valignCO->setEnabled(true);
183 ialignCO->setEnabled(true);
184 halignCO->setEnabled(false);
185 heightCB->setEnabled(true);
186 heightED->setEnabled(true);
187 heightUnitsLC->setEnabled(true);
191 pagebreakCB->setChecked(false);
192 pagebreakCB->setEnabled(index == 1);
193 int itype = innerBoxCO->currentIndex();
194 setInnerType(frameless, itype);
198 void GuiBox::restoreClicked()
200 setInnerType(true, 2);
201 widthED->setText("100");
202 widthUnitsLC->setCurrentItem(Length::PCW);
203 heightCB->setCheckState(Qt::Checked);
204 heightED->setText("1");
205 for (int j = 0; j < heightUnitsLC->count(); j++) {
206 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
207 heightUnitsLC->setCurrentItem(j);
212 void GuiBox::pagebreakClicked()
214 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
215 innerBoxCO->setEnabled(!pbreak);
217 valignCO->setEnabled(false);
218 ialignCO->setEnabled(false);
219 halignCO->setEnabled(false);
220 heightCB->setEnabled(false);
221 heightED->setEnabled(false);
222 heightUnitsLC->setEnabled(false);
225 typeChanged(typeCO->currentIndex());
229 void GuiBox::updateContents()
231 string type = params_.type;
232 if (type == "Framed") {
233 pagebreakCB->setChecked(true);
236 pagebreakCB->setChecked(false);
238 pagebreakCB->setEnabled(type == "Boxed");
240 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
242 typeCO->setCurrentIndex(i);
246 unsigned int inner_type = 2;
247 if (!params_.inner_box)
250 if (params_.use_parbox)
253 bool frameless = (params_.type == "Frameless");
254 setInnerType(frameless, inner_type);
256 char c = params_.pos;
257 valignCO->setCurrentIndex(string("tcb").find(c, 0));
258 c = params_.inner_pos;
259 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
261 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
263 bool ibox = params_.inner_box;
264 valignCO->setEnabled(ibox);
265 ialignCO->setEnabled(ibox);
266 halignCO->setEnabled(!ibox);
269 Length::UNIT default_unit =
270 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
272 lengthToWidgets(widthED, widthUnitsLC,
273 (params_.width).asString(), default_unit);
275 string const special = params_.special;
276 if (!special.empty() && special != "none") {
278 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
279 if (special == ids_spec_[i])
280 spc = toqstr(gui_names_spec_[i].c_str());
282 for (int j = 0; j < widthUnitsLC->count(); j++) {
283 if (widthUnitsLC->itemText(j) == spc)
284 widthUnitsLC->setCurrentIndex(j);
288 lengthToWidgets(heightED, heightUnitsLC,
289 (params_.height).asString(), default_unit);
291 string const height_special = params_.height_special;
292 if (!height_special.empty() && height_special != "none") {
294 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
295 if (height_special == ids_spec_[i])
296 hspc = toqstr(gui_names_spec_[i].c_str());
298 for (int j = 0; j != heightUnitsLC->count(); j++) {
299 if (heightUnitsLC->itemText(j) == hspc)
300 heightUnitsLC->setCurrentIndex(j);
303 // set no optional height when the value is the default "1\height"
304 // (special units like \height are handled as "in",
305 if (height_special == "totalheight" &&
306 params_.height == Length("1in"))
307 heightCB->setCheckState(Qt::Unchecked);
309 heightCB->setCheckState(Qt::Checked);
311 heightCB->setEnabled(ibox);
315 void GuiBox::applyView()
317 bool pagebreak = pagebreakCB->isChecked();
319 params_.type = "Framed";
321 params_.type = ids_[typeCO->currentIndex()];
323 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
324 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
326 params_.pos = "tcb"[valignCO->currentIndex()];
327 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
328 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
332 QString special = widthUnitsLC->currentText();
333 QString value = widthED->text();
334 if (special == qt_("Height")) {
337 } else if (special == qt_("Depth")) {
340 } else if (special == qt_("Total Height")) {
343 } else if (special == qt_("Width")) {
347 // the user might insert a non-special value in the line edit
348 if (isValidLength(fromqstr(value))) {
352 params_.special = ids_spec_[i];
356 width = fromqstr(value);
357 // beware: bogosity! the unit is simply ignored in this case
360 width = widgetsToLength(widthED, widthUnitsLC);
363 params_.width = Length(width);
367 special = heightUnitsLC->currentText();
368 value = heightED->text();
369 if (special == qt_("Height")) {
372 } else if (special == qt_("Depth")) {
375 } else if (special == qt_("Total Height")) {
378 } else if (special == qt_("Width")) {
382 // the user might insert a non-special value in the line edit
383 if (isValidLength(fromqstr(value))) {
387 params_.height_special = ids_spec_[i];
390 if (spec && !isValidLength(fromqstr(heightED->text()))) {
391 height = fromqstr(value);
392 // beware: bogosity! the unit is simply ignored in this case
395 height = widgetsToLength(heightED, heightUnitsLC);
397 // the height parameter is omitted in InsetBox.cpp when the value
398 // is "1in" and "Total Height" is used as unit.
399 // 1in + "Total Height" means "1\height" which is the LaTeX default when
400 // no height is given
401 if (heightCB->checkState() == Qt::Checked)
402 params_.height = Length(height);
404 params_.height = Length("1in");
405 params_.height_special = ids_spec_[3];
410 void GuiBox::setSpecial(bool ibox)
412 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
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)
419 // insert 'em if needed...
420 if (!ibox && !has_special) {
421 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
422 widthUnitsLC->addItem(toqstr(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]));
432 void GuiBox::setInnerType(bool frameless, int i)
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
438 innerBoxCO->addItem(qt_("Parbox"));
439 innerBoxCO->addItem(qt_("Minipage"));
441 innerBoxCO->setCurrentIndex(i - 1);
443 innerBoxCO->setCurrentIndex(i);
445 if (innerBoxCO->count() == 2)
448 innerBoxCO->addItem(qt_("None"));
449 innerBoxCO->addItem(qt_("Parbox"));
450 innerBoxCO->addItem(qt_("Minipage"));
451 innerBoxCO->setCurrentIndex(i);
455 bool GuiBox::initialiseParams(string const & data)
457 InsetBoxMailer::string2params(data, params_);
463 void GuiBox::clearParams()
465 params_ = InsetBoxParams("");
469 void GuiBox::dispatchParams()
471 dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
475 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
478 } // namespace frontend
482 #include "GuiBox_moc.cpp"