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>
40 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
42 char const * const ids_[] = {
43 "Frameless", "Boxed", "ovalbox",
44 "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
45 size_t const ids_size = sizeof(ids_) / sizeof(char *);
46 ids = vector<string>(ids_, ids_ + ids_size);
48 gui_names.push_back(_("No frame"));
49 gui_names.push_back(_("Simple rectangular frame"));
50 gui_names.push_back(_("Oval frame, thin"));
51 gui_names.push_back(_("Oval frame, thick"));
52 gui_names.push_back(_("Drop shadow"));
53 gui_names.push_back(_("Shaded background"));
54 gui_names.push_back(_("Double rectangular frame"));
58 void box_gui_tokens_special_length(vector<string> & ids,
59 vector<docstring> & gui_names)
61 char const * const ids_[] = {
62 "none", "height", "depth",
63 "totalheight", "width"};
64 size_t const ids_size = sizeof(ids_) / sizeof(char *);
65 ids = vector<string>(ids_, ids_ + ids_size);
67 gui_names.push_back(_("None"));
68 gui_names.push_back(_("Height"));
69 gui_names.push_back(_("Depth"));
70 gui_names.push_back(_("Total Height"));
71 gui_names.push_back(_("Width"));
75 GuiBox::GuiBox(GuiView & lv)
76 : GuiDialog(lv, "box"), params_("")
79 setViewTitle(_("Box Settings"));
81 // fill the box type choice
82 box_gui_tokens(ids_, gui_names_);
83 for (unsigned int i = 0; i < gui_names_.size(); ++i)
84 typeCO->addItem(toqstr(gui_names_[i]));
86 // add the special units to the height choice
87 // width needs different handling
88 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
89 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
90 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
92 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
93 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
94 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
95 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
97 connect(widthED, SIGNAL(textChanged(QString)),
98 this, SLOT(change_adaptor()));
99 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
100 this, SLOT(change_adaptor()));
101 connect(valignCO, SIGNAL(highlighted(QString)),
102 this, SLOT(change_adaptor()));
103 connect(heightCB, SIGNAL(stateChanged(int)),
104 this, SLOT(change_adaptor()));
105 connect(heightED, SIGNAL(textChanged(const QString &)),
106 this, SLOT(change_adaptor()));
107 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
108 this, SLOT(change_adaptor()));
109 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
110 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
111 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
112 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114 connect(innerBoxCO, SIGNAL(activated(const QString&)),
115 this, SLOT(innerBoxChanged(const QString &)));
116 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117 connect(pagebreakCB, SIGNAL(stateChanged(int)),
118 this, SLOT(pagebreakClicked()));
120 heightED->setValidator(unsignedLengthValidator(heightED));
121 widthED->setValidator(unsignedLengthValidator(widthED));
123 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
125 bc().addReadOnly(typeCO);
126 bc().addReadOnly(innerBoxCO);
127 bc().addReadOnly(valignCO);
128 bc().addReadOnly(ialignCO);
129 bc().addReadOnly(halignCO);
130 bc().addReadOnly(widthED);
131 bc().addReadOnly(widthUnitsLC);
132 bc().addReadOnly(heightCB);
133 bc().addReadOnly(heightED);
134 bc().addReadOnly(heightUnitsLC);
135 bc().addReadOnly(pagebreakCB);
137 bc().setRestore(restorePB);
139 bc().setApply(applyPB);
140 bc().setCancel(closePB);
142 // initialize the length validator
143 bc().addCheckedLineEdit(widthED, widthLA);
144 bc().addCheckedLineEdit(heightED, heightCB);
148 void GuiBox::closeEvent(QCloseEvent * e)
155 void GuiBox::change_adaptor()
161 void GuiBox::innerBoxChanged(const QString & str)
163 bool const ibox = (str != qt_("None"));
164 valignCO->setEnabled(ibox);
165 ialignCO->setEnabled(ibox);
166 halignCO->setEnabled(!ibox);
167 heightCB->setEnabled(ibox);
168 if (heightCB->checkState() == Qt::Checked && ibox) {
169 heightED->setEnabled(true);
170 heightUnitsLC->setEnabled(true);
176 void GuiBox::typeChanged(int index)
178 bool const frameless = (index == 0);
180 valignCO->setEnabled(true);
181 ialignCO->setEnabled(true);
182 halignCO->setEnabled(false);
183 heightCB->setEnabled(true);
184 heightED->setEnabled(true);
185 heightUnitsLC->setEnabled(true);
189 pagebreakCB->setChecked(false);
190 pagebreakCB->setEnabled(index == 1);
191 int itype = innerBoxCO->currentIndex();
192 setInnerType(frameless, itype);
196 void GuiBox::restoreClicked()
198 setInnerType(true, 2);
199 widthED->setText("100");
200 widthUnitsLC->setCurrentItem(Length::PCW);
201 heightCB->setCheckState(Qt::Checked);
202 heightED->setText("1");
203 for (int j = 0; j < heightUnitsLC->count(); j++) {
204 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
205 heightUnitsLC->setCurrentItem(j);
210 void GuiBox::pagebreakClicked()
212 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
213 innerBoxCO->setEnabled(!pbreak);
215 valignCO->setEnabled(false);
216 ialignCO->setEnabled(false);
217 halignCO->setEnabled(false);
218 heightCB->setEnabled(false);
219 heightED->setEnabled(false);
220 heightUnitsLC->setEnabled(false);
223 typeChanged(typeCO->currentIndex());
227 void GuiBox::updateContents()
229 string type = params_.type;
230 if (type == "Framed") {
231 pagebreakCB->setChecked(true);
234 pagebreakCB->setChecked(false);
236 pagebreakCB->setEnabled(type == "Boxed");
238 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
240 typeCO->setCurrentIndex(i);
244 unsigned int inner_type = 2;
245 if (!params_.inner_box)
248 if (params_.use_parbox)
251 bool frameless = (params_.type == "Frameless");
252 setInnerType(frameless, inner_type);
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));
259 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
261 bool ibox = params_.inner_box;
262 valignCO->setEnabled(ibox);
263 ialignCO->setEnabled(ibox);
264 halignCO->setEnabled(!ibox);
267 Length::UNIT default_unit =
268 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
270 lengthToWidgets(widthED, widthUnitsLC,
271 (params_.width).asString(), default_unit);
273 string const special = params_.special;
274 if (!special.empty() && special != "none") {
276 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
277 if (special == ids_spec_[i])
278 spc = toqstr(gui_names_spec_[i].c_str());
280 for (int j = 0; j < widthUnitsLC->count(); j++) {
281 if (widthUnitsLC->itemText(j) == spc)
282 widthUnitsLC->setCurrentIndex(j);
286 lengthToWidgets(heightED, heightUnitsLC,
287 (params_.height).asString(), default_unit);
289 string const height_special = params_.height_special;
290 if (!height_special.empty() && height_special != "none") {
292 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
293 if (height_special == ids_spec_[i])
294 hspc = toqstr(gui_names_spec_[i].c_str());
296 for (int j = 0; j != heightUnitsLC->count(); j++) {
297 if (heightUnitsLC->itemText(j) == hspc)
298 heightUnitsLC->setCurrentIndex(j);
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" &&
304 params_.height == Length("1in"))
305 heightCB->setCheckState(Qt::Unchecked);
307 heightCB->setCheckState(Qt::Checked);
309 heightCB->setEnabled(ibox);
313 void GuiBox::applyView()
315 bool pagebreak = pagebreakCB->isChecked();
317 params_.type = "Framed";
319 params_.type = ids_[typeCO->currentIndex()];
321 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
322 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
324 params_.pos = "tcb"[valignCO->currentIndex()];
325 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
326 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
330 QString special = widthUnitsLC->currentText();
331 QString value = widthED->text();
332 if (special == qt_("Height")) {
335 } else if (special == qt_("Depth")) {
338 } else if (special == qt_("Total Height")) {
341 } else if (special == qt_("Width")) {
345 // the user might insert a non-special value in the line edit
346 if (isValidLength(fromqstr(value))) {
350 params_.special = ids_spec_[i];
354 width = fromqstr(value);
355 // beware: bogosity! the unit is simply ignored in this case
358 width = widgetsToLength(widthED, widthUnitsLC);
361 params_.width = Length(width);
365 special = heightUnitsLC->currentText();
366 value = heightED->text();
367 if (special == qt_("Height")) {
370 } else if (special == qt_("Depth")) {
373 } else if (special == qt_("Total Height")) {
376 } else if (special == qt_("Width")) {
380 // the user might insert a non-special value in the line edit
381 if (isValidLength(fromqstr(value))) {
385 params_.height_special = ids_spec_[i];
388 if (spec && !isValidLength(fromqstr(heightED->text()))) {
389 height = fromqstr(value);
390 // beware: bogosity! the unit is simply ignored in this case
393 height = widgetsToLength(heightED, heightUnitsLC);
395 // the height parameter is omitted in InsetBox.cpp when the value
396 // is "1in" and "Total Height" is used as unit.
397 // 1in + "Total Height" means "1\height" which is the LaTeX default when
398 // no height is given
399 if (heightCB->checkState() == Qt::Checked)
400 params_.height = Length(height);
402 params_.height = Length("1in");
403 params_.height_special = ids_spec_[3];
408 void GuiBox::setSpecial(bool ibox)
410 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
411 // check if the widget contains the special units
412 int count = widthUnitsLC->count();
413 bool has_special = false;
414 for (int i = 0; i < count; i++)
415 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
417 // insert 'em if needed...
418 if (!ibox && !has_special) {
419 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
420 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
421 // ... or remove 'em if needed
422 } else if (ibox && has_special) {
423 widthUnitsLC->clear();
424 for (int i = 0; i < num_units; i++)
425 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
430 void GuiBox::setInnerType(bool frameless, int i)
432 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
433 // we have to remove "none" then and adjust the combo
436 innerBoxCO->addItem(qt_("Parbox"));
437 innerBoxCO->addItem(qt_("Minipage"));
439 innerBoxCO->setCurrentIndex(i - 1);
441 innerBoxCO->setCurrentIndex(i);
443 if (innerBoxCO->count() == 2)
446 innerBoxCO->addItem(qt_("None"));
447 innerBoxCO->addItem(qt_("Parbox"));
448 innerBoxCO->addItem(qt_("Minipage"));
449 innerBoxCO->setCurrentIndex(i);
453 bool GuiBox::initialiseParams(string const & data)
455 InsetBoxMailer::string2params(data, params_);
461 void GuiBox::clearParams()
463 params_ = InsetBoxParams("");
467 void GuiBox::dispatchParams()
469 dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
473 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
476 } // namespace frontend
480 #include "GuiBox_moc.cpp"