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>
39 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
41 char const * const ids_[] = {
42 "Frameless", "Boxed", "ovalbox",
43 "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
44 size_t const ids_size = sizeof(ids_) / sizeof(char *);
45 ids = vector<string>(ids_, ids_ + ids_size);
47 gui_names.push_back(_("No frame"));
48 gui_names.push_back(_("Simple rectangular frame"));
49 gui_names.push_back(_("Oval frame, thin"));
50 gui_names.push_back(_("Oval frame, thick"));
51 gui_names.push_back(_("Drop shadow"));
52 gui_names.push_back(_("Shaded background"));
53 gui_names.push_back(_("Double rectangular frame"));
57 void box_gui_tokens_special_length(vector<string> & ids,
58 vector<docstring> & gui_names)
60 char const * const ids_[] = {
61 "none", "height", "depth",
62 "totalheight", "width"};
63 size_t const ids_size = sizeof(ids_) / sizeof(char *);
64 ids = vector<string>(ids_, ids_ + ids_size);
66 gui_names.push_back(_("None"));
67 gui_names.push_back(_("Height"));
68 gui_names.push_back(_("Depth"));
69 gui_names.push_back(_("Total Height"));
70 gui_names.push_back(_("Width"));
74 GuiBox::GuiBox(GuiView & lv)
75 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
79 // fill the box type choice
80 box_gui_tokens(ids_, gui_names_);
81 for (unsigned int i = 0; i < gui_names_.size(); ++i)
82 typeCO->addItem(toqstr(gui_names_[i]));
84 // add the special units to the height choice
85 // width needs different handling
86 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
87 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
88 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
90 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
91 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
92 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
93 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
95 connect(widthED, SIGNAL(textChanged(QString)),
96 this, SLOT(change_adaptor()));
97 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98 this, SLOT(change_adaptor()));
99 connect(valignCO, SIGNAL(highlighted(QString)),
100 this, SLOT(change_adaptor()));
101 connect(heightCB, SIGNAL(stateChanged(int)),
102 this, SLOT(change_adaptor()));
103 connect(heightED, SIGNAL(textChanged(const QString &)),
104 this, SLOT(change_adaptor()));
105 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
106 this, SLOT(change_adaptor()));
107 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
108 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
109 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
110 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
111 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
112 connect(innerBoxCO, SIGNAL(activated(const QString&)),
113 this, SLOT(innerBoxChanged(const QString &)));
114 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
115 connect(pagebreakCB, SIGNAL(stateChanged(int)),
116 this, SLOT(pagebreakClicked()));
118 heightED->setValidator(unsignedLengthValidator(heightED));
119 widthED->setValidator(unsignedLengthValidator(widthED));
121 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
123 bc().addReadOnly(typeCO);
124 bc().addReadOnly(innerBoxCO);
125 bc().addReadOnly(valignCO);
126 bc().addReadOnly(ialignCO);
127 bc().addReadOnly(halignCO);
128 bc().addReadOnly(widthED);
129 bc().addReadOnly(widthUnitsLC);
130 bc().addReadOnly(heightCB);
131 bc().addReadOnly(heightED);
132 bc().addReadOnly(heightUnitsLC);
133 bc().addReadOnly(pagebreakCB);
135 bc().setRestore(restorePB);
137 bc().setApply(applyPB);
138 bc().setCancel(closePB);
140 // initialize the length validator
141 bc().addCheckedLineEdit(widthED, widthLA);
142 bc().addCheckedLineEdit(heightED, heightCB);
146 void GuiBox::change_adaptor()
152 void GuiBox::innerBoxChanged(const QString & str)
154 bool const ibox = (str != qt_("None"));
155 valignCO->setEnabled(ibox);
156 ialignCO->setEnabled(ibox);
157 halignCO->setEnabled(!ibox);
158 heightCB->setEnabled(ibox);
159 if (heightCB->checkState() == Qt::Checked && ibox) {
160 heightED->setEnabled(true);
161 heightUnitsLC->setEnabled(true);
167 void GuiBox::typeChanged(int index)
169 bool const frameless = (index == 0);
171 valignCO->setEnabled(true);
172 ialignCO->setEnabled(true);
173 halignCO->setEnabled(false);
174 heightCB->setEnabled(true);
175 heightED->setEnabled(true);
176 heightUnitsLC->setEnabled(true);
180 pagebreakCB->setChecked(false);
181 pagebreakCB->setEnabled(index == 1);
182 int itype = innerBoxCO->currentIndex();
183 setInnerType(frameless, itype);
187 void GuiBox::restoreClicked()
189 setInnerType(true, 2);
190 widthED->setText("100");
191 widthUnitsLC->setCurrentItem(Length::PCW);
192 heightCB->setCheckState(Qt::Checked);
193 heightED->setText("1");
194 for (int j = 0; j < heightUnitsLC->count(); j++) {
195 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
196 heightUnitsLC->setCurrentItem(j);
201 void GuiBox::pagebreakClicked()
203 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
204 innerBoxCO->setEnabled(!pbreak);
206 valignCO->setEnabled(false);
207 ialignCO->setEnabled(false);
208 halignCO->setEnabled(false);
209 heightCB->setEnabled(false);
210 heightED->setEnabled(false);
211 heightUnitsLC->setEnabled(false);
214 typeChanged(typeCO->currentIndex());
218 void GuiBox::updateContents()
220 string type = params_.type;
221 if (type == "Framed") {
222 pagebreakCB->setChecked(true);
225 pagebreakCB->setChecked(false);
227 pagebreakCB->setEnabled(type == "Boxed");
229 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
231 typeCO->setCurrentIndex(i);
235 unsigned int inner_type = 2;
236 if (!params_.inner_box)
239 if (params_.use_parbox)
242 bool frameless = (params_.type == "Frameless");
243 setInnerType(frameless, inner_type);
245 char c = params_.pos;
246 valignCO->setCurrentIndex(string("tcb").find(c, 0));
247 c = params_.inner_pos;
248 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
250 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
252 bool ibox = params_.inner_box;
253 valignCO->setEnabled(ibox);
254 ialignCO->setEnabled(ibox);
255 halignCO->setEnabled(!ibox);
258 Length::UNIT default_unit =
259 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
261 lengthToWidgets(widthED, widthUnitsLC,
262 (params_.width).asString(), default_unit);
264 string const special = params_.special;
265 if (!special.empty() && special != "none") {
267 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
268 if (special == ids_spec_[i])
269 spc = toqstr(gui_names_spec_[i].c_str());
271 for (int j = 0; j < widthUnitsLC->count(); j++) {
272 if (widthUnitsLC->itemText(j) == spc)
273 widthUnitsLC->setCurrentIndex(j);
277 lengthToWidgets(heightED, heightUnitsLC,
278 (params_.height).asString(), default_unit);
280 string const height_special = params_.height_special;
281 if (!height_special.empty() && height_special != "none") {
283 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
284 if (height_special == ids_spec_[i])
285 hspc = toqstr(gui_names_spec_[i].c_str());
287 for (int j = 0; j != heightUnitsLC->count(); j++) {
288 if (heightUnitsLC->itemText(j) == hspc)
289 heightUnitsLC->setCurrentIndex(j);
292 // set no optional height when the value is the default "1\height"
293 // (special units like \height are handled as "in",
294 if (height_special == "totalheight" &&
295 params_.height == Length("1in"))
296 heightCB->setCheckState(Qt::Unchecked);
298 heightCB->setCheckState(Qt::Checked);
300 heightCB->setEnabled(ibox);
304 void GuiBox::applyView()
306 bool pagebreak = pagebreakCB->isChecked();
308 params_.type = "Framed";
310 params_.type = ids_[typeCO->currentIndex()];
312 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
313 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
315 params_.pos = "tcb"[valignCO->currentIndex()];
316 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
317 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
321 QString special = widthUnitsLC->currentText();
322 QString value = widthED->text();
323 if (special == qt_("Height")) {
326 } else if (special == qt_("Depth")) {
329 } else if (special == qt_("Total Height")) {
332 } else if (special == qt_("Width")) {
336 // the user might insert a non-special value in the line edit
337 if (isValidLength(fromqstr(value))) {
341 params_.special = ids_spec_[i];
345 width = fromqstr(value);
346 // beware: bogosity! the unit is simply ignored in this case
349 width = widgetsToLength(widthED, widthUnitsLC);
352 params_.width = Length(width);
356 special = heightUnitsLC->currentText();
357 value = heightED->text();
358 if (special == qt_("Height")) {
361 } else if (special == qt_("Depth")) {
364 } else if (special == qt_("Total Height")) {
367 } else if (special == qt_("Width")) {
371 // the user might insert a non-special value in the line edit
372 if (isValidLength(fromqstr(value))) {
376 params_.height_special = ids_spec_[i];
379 if (spec && !isValidLength(fromqstr(heightED->text()))) {
380 height = fromqstr(value);
381 // beware: bogosity! the unit is simply ignored in this case
384 height = widgetsToLength(heightED, heightUnitsLC);
386 // the height parameter is omitted in InsetBox.cpp when the value
387 // is "1in" and "Total Height" is used as unit.
388 // 1in + "Total Height" means "1\height" which is the LaTeX default when
389 // no height is given
390 if (heightCB->checkState() == Qt::Checked)
391 params_.height = Length(height);
393 params_.height = Length("1in");
394 params_.height_special = ids_spec_[3];
399 void GuiBox::setSpecial(bool ibox)
401 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
402 // check if the widget contains the special units
403 int count = widthUnitsLC->count();
404 bool has_special = false;
405 for (int i = 0; i < count; i++)
406 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
408 // insert 'em if needed...
409 if (!ibox && !has_special) {
410 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
411 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
412 // ... or remove 'em if needed
413 } else if (ibox && has_special) {
414 widthUnitsLC->clear();
415 for (int i = 0; i < num_units; i++)
416 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
421 void GuiBox::setInnerType(bool frameless, int i)
423 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
424 // we have to remove "none" then and adjust the combo
427 innerBoxCO->addItem(qt_("Parbox"));
428 innerBoxCO->addItem(qt_("Minipage"));
430 innerBoxCO->setCurrentIndex(i - 1);
432 innerBoxCO->setCurrentIndex(i);
434 if (innerBoxCO->count() == 2)
437 innerBoxCO->addItem(qt_("None"));
438 innerBoxCO->addItem(qt_("Parbox"));
439 innerBoxCO->addItem(qt_("Minipage"));
440 innerBoxCO->setCurrentIndex(i);
444 bool GuiBox::initialiseParams(string const & data)
446 InsetBoxMailer::string2params(data, params_);
452 void GuiBox::clearParams()
454 params_ = InsetBoxParams("");
458 void GuiBox::dispatchParams()
460 dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
464 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
467 } // namespace frontend
471 #include "GuiBox_moc.cpp"