]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDocument.cpp
Add items to toggle "Force uppercase" and "Full author list" to the context menu
[lyx.git] / src / frontends / qt4 / GuiDocument.cpp
1 /**
2  * \file GuiDocument.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Richard Heck (modules)
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiDocument.h"
15
16 #include "CategorizedCombo.h"
17 #include "GuiApplication.h"
18 #include "GuiBranches.h"
19 #include "GuiIndices.h"
20 #include "GuiSelectionManager.h"
21 #include "LaTeXHighlighter.h"
22 #include "LengthCombo.h"
23 #include "PanelStack.h"
24 #include "Validator.h"
25
26 #include "LayoutFile.h"
27 #include "BranchList.h"
28 #include "buffer_funcs.h"
29 #include "Buffer.h"
30 #include "BufferParams.h"
31 #include "BufferView.h"
32 #include "CiteEnginesList.h"
33 #include "Color.h"
34 #include "ColorCache.h"
35 #include "Converter.h"
36 #include "Cursor.h"
37 #include "Encoding.h"
38 #include "FloatPlacement.h"
39 #include "Format.h"
40 #include "FuncRequest.h"
41 #include "HSpace.h"
42 #include "IndicesList.h"
43 #include "Language.h"
44 #include "LaTeXFeatures.h"
45 #include "LaTeXFonts.h"
46 #include "Layout.h"
47 #include "LayoutEnums.h"
48 #include "LayoutModuleList.h"
49 #include "LyXRC.h"
50 #include "ModuleList.h"
51 #include "OutputParams.h"
52 #include "PDFOptions.h"
53 #include "qt_helpers.h"
54 #include "Spacing.h"
55 #include "TextClass.h"
56 #include "Undo.h"
57 #include "VSpace.h"
58
59 #include "insets/InsetListingsParams.h"
60
61 #include "support/debug.h"
62 #include "support/FileName.h"
63 #include "support/filetools.h"
64 #include "support/gettext.h"
65 #include "support/lassert.h"
66 #include "support/lstrings.h"
67
68 #include "frontends/alert.h"
69
70 #include <QAbstractItemModel>
71 #include <QHeaderView>
72 #include <QColor>
73 #include <QColorDialog>
74 #include <QCloseEvent>
75 #include <QFontDatabase>
76 #include <QScrollBar>
77 #include <QTextBoundaryFinder>
78 #include <QTextCursor>
79
80 #include <sstream>
81 #include <vector>
82
83 #ifdef IN
84 #undef IN
85 #endif
86
87
88 // a style sheet for buttons
89 // this is for example used for the background color setting button
90 static inline QString colorButtonStyleSheet(QColor const & bgColor)
91 {
92         if (bgColor.isValid()) {
93                 QString rc = QLatin1String("background-color:");
94                 rc += bgColor.name();
95                 return rc;
96         }
97         return QString();
98 }
99
100
101 using namespace std;
102 using namespace lyx::support;
103
104
105 namespace {
106
107 char const * const tex_graphics[] =
108 {
109         "default", "dvialw", "dvilaser", "dvipdf", "dvipdfm", "dvipdfmx",
110         "dvips", "dvipsone", "dvitops", "dviwin", "dviwindo", "dvi2ps", "emtex",
111         "ln", "oztex", "pctexhp", "pctexps", "pctexwin", "pctex32", "pdftex",
112         "psprint", "pubps", "tcidvi", "textures", "truetex", "vtex", "xdvi",
113         "xetex", "none", ""
114 };
115
116
117 char const * const tex_graphics_gui[] =
118 {
119         N_("Default"), "dvialw", "DviLaser", "dvipdf", "DVIPDFM", "DVIPDFMx",
120         "Dvips", "DVIPSONE", "DVItoPS", "DVIWIN", "DVIWindo", "dvi2ps", "EmTeX",
121         "LN", "OzTeX", "pctexhp", "pctexps", "pctexwin", "PCTeX32", "pdfTeX",
122         "psprint", "pubps", "tcidvi", "Textures", "TrueTeX", "VTeX", "xdvi",
123         "XeTeX", N_("None"), ""
124 };
125
126
127 char const * backref_opts[] =
128 {
129         "false", "section", "slide", "page", ""
130 };
131
132
133 char const * backref_opts_gui[] =
134 {
135         N_("Off"), N_("Section"), N_("Slide"), N_("Page"), ""
136 };
137
138
139 vector<string> engine_types_;
140 vector<pair<string, QString> > pagestyles;
141
142 QMap<QString, QString> rmfonts_;
143 QMap<QString, QString> sffonts_;
144 QMap<QString, QString> ttfonts_;
145 QMap<QString, QString> mathfonts_;
146
147
148 } // anonymous namespace
149
150 namespace lyx {
151
152 RGBColor set_backgroundcolor;
153 bool is_backgroundcolor;
154 RGBColor set_fontcolor;
155 bool is_fontcolor;
156 RGBColor set_notefontcolor;
157 RGBColor set_boxbgcolor;
158 bool forced_fontspec_activation;
159
160 namespace {
161 // used when sorting the textclass list.
162 class less_textclass_avail_desc
163         : public binary_function<string, string, int>
164 {
165 public:
166         bool operator()(string const & lhs, string const & rhs) const
167         {
168                 // Ordering criteria:
169                 //   1. Availability of text class
170                 //   2. Description (lexicographic)
171                 LayoutFile const & tc1 = LayoutFileList::get()[lhs];
172                 LayoutFile const & tc2 = LayoutFileList::get()[rhs];
173                 int const order = compare_no_case(
174                         translateIfPossible(from_utf8(tc1.description())),
175                         translateIfPossible(from_utf8(tc2.description())));
176                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
177                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() && order < 0);
178         }
179 };
180
181 }
182
183 namespace frontend {
184 namespace {
185
186 vector<string> getRequiredList(string const & modName)
187 {
188         LyXModule const * const mod = theModuleList[modName];
189         if (!mod)
190                 return vector<string>(); //empty such thing
191         return mod->getRequiredModules();
192 }
193
194
195 vector<string> getExcludedList(string const & modName)
196 {
197         LyXModule const * const mod = theModuleList[modName];
198         if (!mod)
199                 return vector<string>(); //empty such thing
200         return mod->getExcludedModules();
201 }
202
203
204 docstring getModuleCategory(string const & modName)
205 {
206         LyXModule const * const mod = theModuleList[modName];
207         if (!mod)
208                 return docstring();
209         return from_utf8(mod->category());
210 }
211
212
213 docstring getModuleDescription(string const & modName)
214 {
215         LyXModule const * const mod = theModuleList[modName];
216         if (!mod)
217                 return _("Module not found!");
218         // FIXME Unicode
219         return translateIfPossible(from_utf8(mod->getDescription()));
220 }
221
222
223 vector<string> getPackageList(string const & modName)
224 {
225         LyXModule const * const mod = theModuleList[modName];
226         if (!mod)
227                 return vector<string>(); //empty such thing
228         return mod->getPackageList();
229 }
230
231
232 bool isModuleAvailable(string const & modName)
233 {
234         LyXModule const * const mod = theModuleList[modName];
235         if (!mod)
236                 return false;
237         return mod->isAvailable();
238 }
239
240 } // anonymous namespace
241
242
243 /////////////////////////////////////////////////////////////////////
244 //
245 // ModuleSelectionManager
246 //
247 /////////////////////////////////////////////////////////////////////
248
249 /// SelectionManager for use with modules
250 class ModuleSelectionManager : public GuiSelectionManager
251 {
252 public:
253         ///
254         ModuleSelectionManager(
255                 QTreeView * availableLV,
256                 QListView * selectedLV,
257                 QPushButton * addPB,
258                 QPushButton * delPB,
259                 QPushButton * upPB,
260                 QPushButton * downPB,
261                 GuiIdListModel * availableModel,
262                 GuiIdListModel * selectedModel,
263                 GuiDocument const * container)
264         : GuiSelectionManager(availableLV, selectedLV, addPB, delPB,
265                                 upPB, downPB, availableModel, selectedModel), container_(container)
266                 {}
267         ///
268         void updateProvidedModules(LayoutModuleList const & pm)
269                         { provided_modules_ = pm.list(); }
270         ///
271         void updateExcludedModules(LayoutModuleList const & em)
272                         { excluded_modules_ = em.list(); }
273 private:
274         ///
275         virtual void updateAddPB();
276         ///
277         virtual void updateUpPB();
278         ///
279         virtual void updateDownPB();
280         ///
281         virtual void updateDelPB();
282         /// returns availableModel as a GuiIdListModel
283         GuiIdListModel * getAvailableModel()
284         {
285                 return dynamic_cast<GuiIdListModel *>(availableModel);
286         }
287         /// returns selectedModel as a GuiIdListModel
288         GuiIdListModel * getSelectedModel()
289         {
290                 return dynamic_cast<GuiIdListModel *>(selectedModel);
291         }
292         /// keeps a list of the modules the text class provides
293         list<string> provided_modules_;
294         /// similarly...
295         list<string> excluded_modules_;
296         ///
297         GuiDocument const * container_;
298 };
299
300 void ModuleSelectionManager::updateAddPB()
301 {
302         int const arows = availableModel->rowCount();
303         QModelIndexList const avail_sels =
304                         availableLV->selectionModel()->selectedIndexes();
305
306         // disable if there aren't any modules (?), if none of them is chosen
307         // in the dialog, or if the chosen one is already selected for use.
308         if (arows == 0 || avail_sels.isEmpty() || isSelected(avail_sels.first())) {
309                 addPB->setEnabled(false);
310                 return;
311         }
312
313         QModelIndex const & idx = availableLV->selectionModel()->currentIndex();
314         string const modname = getAvailableModel()->getIDString(idx.row());
315
316         bool const enable =
317                 container_->params().layoutModuleCanBeAdded(modname);
318         addPB->setEnabled(enable);
319 }
320
321
322 void ModuleSelectionManager::updateDownPB()
323 {
324         int const srows = selectedModel->rowCount();
325         if (srows == 0) {
326                 downPB->setEnabled(false);
327                 return;
328         }
329         QModelIndex const & curidx = selectedLV->selectionModel()->currentIndex();
330         int const curRow = curidx.row();
331         if (curRow < 0 || curRow >= srows - 1) { // invalid or last item
332                 downPB->setEnabled(false);
333                 return;
334         }
335
336         // determine whether immediately succeding element requires this one
337         string const curmodname = getSelectedModel()->getIDString(curRow);
338         string const nextmodname = getSelectedModel()->getIDString(curRow + 1);
339
340         vector<string> reqs = getRequiredList(nextmodname);
341
342         // if it doesn't require anything....
343         if (reqs.empty()) {
344                 downPB->setEnabled(true);
345                 return;
346         }
347
348         // Enable it if this module isn't required.
349         // FIXME This should perhaps be more flexible and check whether, even
350         // if the next one is required, there is also an earlier one that will do.
351         downPB->setEnabled(
352                         find(reqs.begin(), reqs.end(), curmodname) == reqs.end());
353 }
354
355 void ModuleSelectionManager::updateUpPB()
356 {
357         int const srows = selectedModel->rowCount();
358         if (srows == 0) {
359                 upPB->setEnabled(false);
360                 return;
361         }
362
363         QModelIndex const & curIdx = selectedLV->selectionModel()->currentIndex();
364         int curRow = curIdx.row();
365         if (curRow <= 0 || curRow > srows - 1) { // first item or invalid
366                 upPB->setEnabled(false);
367                 return;
368         }
369         string const curmodname = getSelectedModel()->getIDString(curRow);
370
371         // determine whether immediately preceding element is required by this one
372         vector<string> reqs = getRequiredList(curmodname);
373
374         // if this one doesn't require anything....
375         if (reqs.empty()) {
376                 upPB->setEnabled(true);
377                 return;
378         }
379
380
381         // Enable it if the preceding module isn't required.
382         // NOTE This is less flexible than it might be. We could check whether, even
383         // if the previous one is required, there is an earlier one that would do.
384         string const premod = getSelectedModel()->getIDString(curRow - 1);
385         upPB->setEnabled(find(reqs.begin(), reqs.end(), premod) == reqs.end());
386 }
387
388 void ModuleSelectionManager::updateDelPB()
389 {
390         int const srows = selectedModel->rowCount();
391         if (srows == 0) {
392                 deletePB->setEnabled(false);
393                 return;
394         }
395
396         QModelIndex const & curidx =
397                 selectedLV->selectionModel()->currentIndex();
398         int const curRow = curidx.row();
399         if (curRow < 0 || curRow >= srows) { // invalid index?
400                 deletePB->setEnabled(false);
401                 return;
402         }
403
404         string const curmodname = getSelectedModel()->getIDString(curRow);
405
406         // We're looking here for a reason NOT to enable the button. If we
407         // find one, we disable it and return. If we don't, we'll end up at
408         // the end of the function, and then we enable it.
409         for (int i = curRow + 1; i < srows; ++i) {
410                 string const thisMod = getSelectedModel()->getIDString(i);
411                 vector<string> reqs = getRequiredList(thisMod);
412                 //does this one require us?
413                 if (find(reqs.begin(), reqs.end(), curmodname) == reqs.end())
414                         //no...
415                         continue;
416
417                 // OK, so this module requires us
418                 // is there an EARLIER module that also satisfies the require?
419                 // NOTE We demand that it be earlier to keep the list of modules
420                 // consistent with the rule that a module must be proceeded by a
421                 // required module. There would be more flexible ways to proceed,
422                 // but that would be a lot more complicated, and the logic here is
423                 // already complicated. (That's why I've left the debugging code.)
424                 // lyxerr << "Testing " << thisMod << endl;
425                 bool foundone = false;
426                 for (int j = 0; j < curRow; ++j) {
427                         string const mod = getSelectedModel()->getIDString(j);
428                         // lyxerr << "In loop: Testing " << mod << endl;
429                         // do we satisfy the require?
430                         if (find(reqs.begin(), reqs.end(), mod) != reqs.end()) {
431                                 // lyxerr << mod << " does the trick." << endl;
432                                 foundone = true;
433                                 break;
434                         }
435                 }
436                 // did we find a module to satisfy the require?
437                 if (!foundone) {
438                         // lyxerr << "No matching module found." << endl;
439                         deletePB->setEnabled(false);
440                         return;
441                 }
442         }
443         // lyxerr << "All's well that ends well." << endl;
444         deletePB->setEnabled(true);
445 }
446
447
448 /////////////////////////////////////////////////////////////////////
449 //
450 // PreambleModule
451 //
452 /////////////////////////////////////////////////////////////////////
453
454 PreambleModule::PreambleModule() : current_id_(0)
455 {
456         // This is not a memory leak. The object will be destroyed
457         // with this.
458         // @ is letter in the LyX user preamble
459         (void) new LaTeXHighlighter(preambleTE->document(), true);
460         preambleTE->setFont(guiApp->typewriterSystemFont());
461         preambleTE->setWordWrapMode(QTextOption::NoWrap);
462         setFocusProxy(preambleTE);
463         connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
464 }
465
466
467 void PreambleModule::update(BufferParams const & params, BufferId id)
468 {
469         QString preamble = toqstr(params.preamble);
470         // Nothing to do if the params and preamble are unchanged.
471         if (id == current_id_
472                 && preamble == preambleTE->document()->toPlainText())
473                 return;
474
475         QTextCursor cur = preambleTE->textCursor();
476         // Save the coords before switching to the new one.
477         preamble_coords_[current_id_] =
478                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
479
480         // Save the params address for further use.
481         current_id_ = id;
482         preambleTE->document()->setPlainText(preamble);
483         Coords::const_iterator it = preamble_coords_.find(current_id_);
484         if (it == preamble_coords_.end())
485                 // First time we open this one.
486                 preamble_coords_[current_id_] = make_pair(0, 0);
487         else {
488                 // Restore saved coords.
489                 QTextCursor cur = preambleTE->textCursor();
490                 cur.setPosition(it->second.first);
491                 preambleTE->setTextCursor(cur);
492                 preambleTE->verticalScrollBar()->setValue(it->second.second);
493         }
494 }
495
496
497 void PreambleModule::apply(BufferParams & params)
498 {
499         params.preamble = qstring_to_ucs4(preambleTE->document()->toPlainText());
500 }
501
502
503 void PreambleModule::closeEvent(QCloseEvent * e)
504 {
505         // Save the coords before closing.
506         QTextCursor cur = preambleTE->textCursor();
507         preamble_coords_[current_id_] =
508                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
509         e->accept();
510 }
511
512
513 /////////////////////////////////////////////////////////////////////
514 //
515 // LocalLayout
516 //
517 /////////////////////////////////////////////////////////////////////
518
519
520 LocalLayout::LocalLayout() : current_id_(0), validated_(false)
521 {
522         connect(locallayoutTE, SIGNAL(textChanged()), this, SLOT(textChanged()));
523         connect(validatePB, SIGNAL(clicked()), this, SLOT(validatePressed()));
524         connect(convertPB, SIGNAL(clicked()), this, SLOT(convertPressed()));
525 }
526
527
528 void LocalLayout::update(BufferParams const & params, BufferId id)
529 {
530         QString layout = toqstr(params.getLocalLayout(false));
531         // Nothing to do if the params and preamble are unchanged.
532         if (id == current_id_
533                 && layout == locallayoutTE->document()->toPlainText())
534                 return;
535
536         // Save the params address for further use.
537         current_id_ = id;
538         locallayoutTE->document()->setPlainText(layout);
539         validate();
540 }
541
542
543 void LocalLayout::apply(BufferParams & params)
544 {
545         docstring const layout =
546                 qstring_to_ucs4(locallayoutTE->document()->toPlainText());
547         params.setLocalLayout(layout, false);
548 }
549
550
551 void LocalLayout::hideConvert()
552 {
553         convertPB->setEnabled(false);
554         convertLB->setText("");
555         convertPB->hide();
556         convertLB->hide();
557 }
558
559
560 void LocalLayout::textChanged()
561 {
562         static const QString message =
563                 qt_("Press button to check validity...");
564         string const layout =
565                 fromqstr(locallayoutTE->document()->toPlainText().trimmed());
566
567         if (layout.empty()) {
568                 validated_ = true;
569                 validatePB->setEnabled(false);
570                 validLB->setText("");
571                 hideConvert();
572                 changed();
573         } else if (!validatePB->isEnabled()) {
574                 // if that's already enabled, we shouldn't need to do anything.
575                 validated_ = false;
576                 validLB->setText(message);
577                 validatePB->setEnabled(true);
578                 hideConvert();
579                 changed();
580         }
581 }
582
583
584 void LocalLayout::convert() {
585         string const layout =
586                 fromqstr(locallayoutTE->document()->toPlainText().trimmed());
587         string const newlayout = TextClass::convert(layout);
588         if (!newlayout.empty())
589                 locallayoutTE->setPlainText(toqstr(newlayout));
590         validate();
591 }
592
593
594 void LocalLayout::convertPressed() {
595         convert();
596         hideConvert();
597         changed();
598 }
599
600
601 void LocalLayout::validate() {
602         // Bold text
603         static const QString vpar("<p style=\"font-weight: bold;\">%1</p>");
604         // Flashy red bold text
605         static const QString ivpar("<p style=\"color: #c00000; font-weight: bold; \">"
606                                    "%1</p>");
607         string const layout =
608                 fromqstr(locallayoutTE->document()->toPlainText().trimmed());
609         if (!layout.empty()) {
610                 TextClass::ReturnValues const ret = TextClass::validate(layout);
611                 validated_ = (ret == TextClass::OK) || (ret == TextClass::OK_OLDFORMAT);
612                 validatePB->setEnabled(false);
613                 validLB->setText(validated_ ? vpar.arg(qt_("Layout is valid!"))
614                                             : ivpar.arg(qt_("Layout is invalid!")));
615                 if (ret == TextClass::OK_OLDFORMAT) {
616                         convertPB->show();
617                         // Testing conversion to LYXFILE_LAYOUT_FORMAT at this point
618                         // already.
619                         if (TextClass::convert(layout).empty()) {
620                                 // Conversion failed. If LAYOUT_FORMAT > LYXFILE_LAYOUT_FORMAT,
621                                 // then maybe the layout is still valid, but its format is more
622                                 // recent than LYXFILE_LAYOUT_FORMAT. However, if LAYOUT_FORMAT
623                                 // == LYXFILE_LAYOUT_FORMAT then something is definitely wrong.
624                                 convertPB->setEnabled(false);
625                                 const QString text = (LAYOUT_FORMAT == LYXFILE_LAYOUT_FORMAT)
626                                         ? ivpar.arg(qt_("Conversion to current format impossible!"))
627                                         : vpar.arg(qt_("Conversion to current stable format "
628                                                        "impossible."));
629                                 convertLB->setText(text);
630                         } else {
631                                 convertPB->setEnabled(true);
632                                 convertLB->setText(qt_("Convert to current format"));
633                         }
634                         convertLB->show();
635                 } else {
636                         convertPB->hide();
637                         convertLB->hide();
638                 }
639         }
640 }
641
642
643 void LocalLayout::validatePressed() {
644         validate();
645         changed();
646 }
647
648
649 /////////////////////////////////////////////////////////////////////
650 //
651 // DocumentDialog
652 //
653 /////////////////////////////////////////////////////////////////////
654
655
656 GuiDocument::GuiDocument(GuiView & lv)
657         : GuiDialog(lv, "document", qt_("Document Settings")),
658           biblioChanged_(false), nonModuleChanged_(false)
659 {
660         setupUi(this);
661
662         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
663         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
664         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
665         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
666
667         connect(savePB, SIGNAL(clicked()), this, SLOT(saveDefaultClicked()));
668         connect(defaultPB, SIGNAL(clicked()), this, SLOT(useDefaultsClicked()));
669
670         // Manage the restore, ok, apply, restore and cancel/close buttons
671         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
672         bc().setOK(okPB);
673         bc().setApply(applyPB);
674         bc().setCancel(closePB);
675         bc().setRestore(restorePB);
676
677
678         // text layout
679         textLayoutModule = new UiWidget<Ui::TextLayoutUi>;
680         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
681                 this, SLOT(change_adaptor()));
682         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
683                 this, SLOT(setLSpacing(int)));
684         connect(textLayoutModule->lspacingLE, SIGNAL(textChanged(const QString &)),
685                 this, SLOT(change_adaptor()));
686
687         connect(textLayoutModule->indentRB, SIGNAL(clicked()),
688                 this, SLOT(change_adaptor()));
689         connect(textLayoutModule->indentRB, SIGNAL(toggled(bool)),
690                 textLayoutModule->indentCO, SLOT(setEnabled(bool)));
691         connect(textLayoutModule->indentCO, SIGNAL(activated(int)),
692                 this, SLOT(change_adaptor()));
693         connect(textLayoutModule->indentCO, SIGNAL(activated(int)),
694                 this, SLOT(setIndent(int)));
695         connect(textLayoutModule->indentLE, SIGNAL(textChanged(const QString &)),
696                 this, SLOT(change_adaptor()));
697         connect(textLayoutModule->indentLengthCO, SIGNAL(activated(int)),
698                 this, SLOT(change_adaptor()));
699
700         connect(textLayoutModule->skipRB, SIGNAL(clicked()),
701                 this, SLOT(change_adaptor()));
702         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)),
703                 textLayoutModule->skipCO, SLOT(setEnabled(bool)));
704         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
705                 this, SLOT(change_adaptor()));
706         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
707                 this, SLOT(setSkip(int)));
708         connect(textLayoutModule->skipLE, SIGNAL(textChanged(const QString &)),
709                 this, SLOT(change_adaptor()));
710         connect(textLayoutModule->skipLengthCO, SIGNAL(activated(int)),
711                 this, SLOT(change_adaptor()));
712
713         connect(textLayoutModule->indentRB, SIGNAL(toggled(bool)),
714                 this, SLOT(enableIndent(bool)));
715         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)),
716                 this, SLOT(enableSkip(bool)));
717
718         connect(textLayoutModule->twoColumnCB, SIGNAL(clicked()),
719                 this, SLOT(change_adaptor()));
720         connect(textLayoutModule->twoColumnCB, SIGNAL(clicked()),
721                 this, SLOT(setColSep()));
722         connect(textLayoutModule->justCB, SIGNAL(clicked()),
723                 this, SLOT(change_adaptor()));
724
725         textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
726                 textLayoutModule->lspacingLE));
727         textLayoutModule->indentLE->setValidator(new LengthValidator(
728                 textLayoutModule->indentLE));
729         textLayoutModule->skipLE->setValidator(new LengthValidator(
730                 textLayoutModule->skipLE));
731
732         textLayoutModule->indentCO->addItem(qt_("Default"));
733         textLayoutModule->indentCO->addItem(qt_("Custom"));
734         textLayoutModule->skipCO->addItem(qt_("SmallSkip"));
735         textLayoutModule->skipCO->addItem(qt_("MedSkip"));
736         textLayoutModule->skipCO->addItem(qt_("BigSkip"));
737         textLayoutModule->skipCO->addItem(qt_("Custom"));
738         textLayoutModule->lspacingCO->insertItem(
739                 Spacing::Single, qt_("Single"));
740         textLayoutModule->lspacingCO->insertItem(
741                 Spacing::Onehalf, qt_("OneHalf"));
742         textLayoutModule->lspacingCO->insertItem(
743                 Spacing::Double, qt_("Double"));
744         textLayoutModule->lspacingCO->insertItem(
745                 Spacing::Other, qt_("Custom"));
746         // initialize the length validator
747         bc().addCheckedLineEdit(textLayoutModule->indentLE);
748         bc().addCheckedLineEdit(textLayoutModule->skipLE);
749
750
751         // master/child handling
752         masterChildModule = new UiWidget<Ui::MasterChildUi>;
753
754         connect(masterChildModule->childrenTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
755                 this, SLOT(includeonlyClicked(QTreeWidgetItem *, int)));
756         connect(masterChildModule->includeonlyRB, SIGNAL(toggled(bool)),
757                 masterChildModule->childrenTW, SLOT(setEnabled(bool)));
758         connect(masterChildModule->includeonlyRB, SIGNAL(toggled(bool)),
759                 masterChildModule->maintainAuxCB, SLOT(setEnabled(bool)));
760         connect(masterChildModule->includeallRB, SIGNAL(clicked()),
761                 this, SLOT(change_adaptor()));
762         connect(masterChildModule->includeonlyRB, SIGNAL(clicked()),
763                 this, SLOT(change_adaptor()));
764         connect(masterChildModule->maintainAuxCB, SIGNAL(clicked()),
765                 this, SLOT(change_adaptor()));
766         masterChildModule->childrenTW->setColumnCount(2);
767         masterChildModule->childrenTW->headerItem()->setText(0, qt_("Child Document"));
768         masterChildModule->childrenTW->headerItem()->setText(1, qt_("Include to Output"));
769         masterChildModule->childrenTW->resizeColumnToContents(1);
770         masterChildModule->childrenTW->resizeColumnToContents(2);
771
772
773         // Formats
774         outputModule = new UiWidget<Ui::OutputUi>;
775
776         connect(outputModule->defaultFormatCO, SIGNAL(activated(int)),
777                 this, SLOT(change_adaptor()));
778         connect(outputModule->mathimgSB, SIGNAL(valueChanged(double)),
779                 this, SLOT(change_adaptor()));
780         connect(outputModule->strictCB, SIGNAL(stateChanged(int)),
781                 this, SLOT(change_adaptor()));
782         connect(outputModule->cssCB, SIGNAL(stateChanged(int)),
783                 this, SLOT(change_adaptor()));
784         connect(outputModule->mathoutCB, SIGNAL(currentIndexChanged(int)),
785                 this, SLOT(change_adaptor()));
786
787         connect(outputModule->outputsyncCB, SIGNAL(clicked()),
788                 this, SLOT(change_adaptor()));
789         connect(outputModule->synccustomCB, SIGNAL(editTextChanged(QString)),
790                 this, SLOT(change_adaptor()));
791         outputModule->synccustomCB->addItem("");
792         outputModule->synccustomCB->addItem("\\synctex=1");
793         outputModule->synccustomCB->addItem("\\synctex=-1");
794         outputModule->synccustomCB->addItem("\\usepackage[active]{srcltx}");
795
796         outputModule->synccustomCB->setValidator(new NoNewLineValidator(
797                 outputModule->synccustomCB));
798
799         connect(outputModule->saveTransientPropertiesCB, SIGNAL(clicked()),
800                 this, SLOT(change_adaptor()));
801
802         // fonts
803         fontModule = new FontModule;
804         connect(fontModule->osFontsCB, SIGNAL(clicked()),
805                 this, SLOT(change_adaptor()));
806         connect(fontModule->osFontsCB, SIGNAL(toggled(bool)),
807                 this, SLOT(osFontsChanged(bool)));
808         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
809                 this, SLOT(change_adaptor()));
810         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
811                 this, SLOT(romanChanged(int)));
812         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
813                 this, SLOT(change_adaptor()));
814         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
815                 this, SLOT(sansChanged(int)));
816         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
817                 this, SLOT(change_adaptor()));
818         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
819                 this, SLOT(ttChanged(int)));
820         connect(fontModule->fontsMathCO, SIGNAL(activated(int)),
821                 this, SLOT(change_adaptor()));
822         connect(fontModule->fontsMathCO, SIGNAL(activated(int)),
823                 this, SLOT(mathFontChanged(int)));
824         connect(fontModule->fontsDefaultCO, SIGNAL(activated(int)),
825                 this, SLOT(change_adaptor()));
826         connect(fontModule->fontencCO, SIGNAL(activated(int)),
827                 this, SLOT(change_adaptor()));
828         connect(fontModule->fontencCO, SIGNAL(activated(int)),
829                 this, SLOT(fontencChanged(int)));
830         connect(fontModule->fontencLE, SIGNAL(textChanged(const QString &)),
831                 this, SLOT(change_adaptor()));
832         connect(fontModule->fontsizeCO, SIGNAL(activated(int)),
833                 this, SLOT(change_adaptor()));
834         connect(fontModule->cjkFontLE, SIGNAL(textChanged(const QString &)),
835                 this, SLOT(change_adaptor()));
836         connect(fontModule->microtypeCB, SIGNAL(clicked()),
837                 this, SLOT(change_adaptor()));
838         connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)),
839                 this, SLOT(change_adaptor()));
840         connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)),
841                 this, SLOT(change_adaptor()));
842         connect(fontModule->fontScCB, SIGNAL(clicked()),
843                 this, SLOT(change_adaptor()));
844         connect(fontModule->fontScCB, SIGNAL(toggled(bool)),
845                 this, SLOT(fontScToggled(bool)));
846         connect(fontModule->fontOsfCB, SIGNAL(clicked()),
847                 this, SLOT(change_adaptor()));
848         connect(fontModule->fontOsfCB, SIGNAL(toggled(bool)),
849                 this, SLOT(fontOsfToggled(bool)));
850
851         fontModule->fontencLE->setValidator(new NoNewLineValidator(
852                 fontModule->fontencLE));
853         fontModule->cjkFontLE->setValidator(new NoNewLineValidator(
854                 fontModule->cjkFontLE));
855
856         updateFontlist();
857
858         fontModule->fontsizeCO->addItem(qt_("Default"));
859         fontModule->fontsizeCO->addItem(qt_("10"));
860         fontModule->fontsizeCO->addItem(qt_("11"));
861         fontModule->fontsizeCO->addItem(qt_("12"));
862
863         fontModule->fontencCO->addItem(qt_("Default"), QString("global"));
864         fontModule->fontencCO->addItem(qt_("Custom"), QString("custom"));
865         fontModule->fontencCO->addItem(qt_("None (no fontenc)"), QString("default"));
866
867         for (int n = 0; GuiDocument::fontfamilies_gui[n][0]; ++n)
868                 fontModule->fontsDefaultCO->addItem(
869                         qt_(GuiDocument::fontfamilies_gui[n]));
870
871         if (!LaTeXFeatures::isAvailable("fontspec"))
872                 fontModule->osFontsCB->setToolTip(
873                         qt_("Use OpenType and TrueType fonts directly (requires XeTeX or LuaTeX)\n"
874                             "You need to install the package \"fontspec\" to use this feature"));
875
876
877         // page layout
878         pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
879         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
880                 this, SLOT(papersizeChanged(int)));
881         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
882                 this, SLOT(papersizeChanged(int)));
883         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
884                 this, SLOT(change_adaptor()));
885         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
886                 this, SLOT(change_adaptor()));
887         connect(pageLayoutModule->paperheightLE, SIGNAL(textChanged(const QString &)),
888                 this, SLOT(change_adaptor()));
889         connect(pageLayoutModule->paperwidthLE, SIGNAL(textChanged(const QString &)),
890                 this, SLOT(change_adaptor()));
891         connect(pageLayoutModule->paperwidthUnitCO, SIGNAL(activated(int)),
892                 this, SLOT(change_adaptor()));
893         connect(pageLayoutModule->paperheightUnitCO, SIGNAL(activated(int)),
894                 this, SLOT(change_adaptor()));
895         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
896                 this, SLOT(change_adaptor()));
897         connect(pageLayoutModule->landscapeRB, SIGNAL(clicked()),
898                 this, SLOT(change_adaptor()));
899         connect(pageLayoutModule->facingPagesCB, SIGNAL(clicked()),
900                 this, SLOT(change_adaptor()));
901         connect(pageLayoutModule->pagestyleCO, SIGNAL(activated(int)),
902                 this, SLOT(change_adaptor()));
903
904         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
905         pageLayoutModule->pagestyleCO->addItem(qt_("empty"));
906         pageLayoutModule->pagestyleCO->addItem(qt_("plain"));
907         pageLayoutModule->pagestyleCO->addItem(qt_("headings"));
908         pageLayoutModule->pagestyleCO->addItem(qt_("fancy"));
909         bc().addCheckedLineEdit(pageLayoutModule->paperheightLE,
910                 pageLayoutModule->paperheightL);
911         bc().addCheckedLineEdit(pageLayoutModule->paperwidthLE,
912                 pageLayoutModule->paperwidthL);
913
914         QComboBox * cb = pageLayoutModule->papersizeCO;
915         cb->addItem(qt_("Default"));
916         cb->addItem(qt_("Custom"));
917         cb->addItem(qt_("US letter"));
918         cb->addItem(qt_("US legal"));
919         cb->addItem(qt_("US executive"));
920         cb->addItem(qt_("A0"));
921         cb->addItem(qt_("A1"));
922         cb->addItem(qt_("A2"));
923         cb->addItem(qt_("A3"));
924         cb->addItem(qt_("A4"));
925         cb->addItem(qt_("A5"));
926         cb->addItem(qt_("A6"));
927         cb->addItem(qt_("B0"));
928         cb->addItem(qt_("B1"));
929         cb->addItem(qt_("B2"));
930         cb->addItem(qt_("B3"));
931         cb->addItem(qt_("B4"));
932         cb->addItem(qt_("B5"));
933         cb->addItem(qt_("B6"));
934         cb->addItem(qt_("C0"));
935         cb->addItem(qt_("C1"));
936         cb->addItem(qt_("C2"));
937         cb->addItem(qt_("C3"));
938         cb->addItem(qt_("C4"));
939         cb->addItem(qt_("C5"));
940         cb->addItem(qt_("C6"));
941         cb->addItem(qt_("JIS B0"));
942         cb->addItem(qt_("JIS B1"));
943         cb->addItem(qt_("JIS B2"));
944         cb->addItem(qt_("JIS B3"));
945         cb->addItem(qt_("JIS B4"));
946         cb->addItem(qt_("JIS B5"));
947         cb->addItem(qt_("JIS B6"));
948         // remove the %-items from the unit choice
949         pageLayoutModule->paperwidthUnitCO->noPercents();
950         pageLayoutModule->paperheightUnitCO->noPercents();
951         pageLayoutModule->paperheightLE->setValidator(unsignedLengthValidator(
952                 pageLayoutModule->paperheightLE));
953         pageLayoutModule->paperwidthLE->setValidator(unsignedLengthValidator(
954                 pageLayoutModule->paperwidthLE));
955
956
957         // margins
958         marginsModule = new UiWidget<Ui::MarginsUi>;
959         connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
960                 this, SLOT(setCustomMargins(bool)));
961         connect(marginsModule->marginCB, SIGNAL(clicked()),
962                 this, SLOT(change_adaptor()));
963         connect(marginsModule->topLE, SIGNAL(textChanged(QString)),
964                 this, SLOT(change_adaptor()));
965         connect(marginsModule->topUnit, SIGNAL(activated(int)),
966                 this, SLOT(change_adaptor()));
967         connect(marginsModule->bottomLE, SIGNAL(textChanged(QString)),
968                 this, SLOT(change_adaptor()));
969         connect(marginsModule->bottomUnit, SIGNAL(activated(int)),
970                 this, SLOT(change_adaptor()));
971         connect(marginsModule->innerLE, SIGNAL(textChanged(QString)),
972                 this, SLOT(change_adaptor()));
973         connect(marginsModule->innerUnit, SIGNAL(activated(int)),
974                 this, SLOT(change_adaptor()));
975         connect(marginsModule->outerLE, SIGNAL(textChanged(QString)),
976                 this, SLOT(change_adaptor()));
977         connect(marginsModule->outerUnit, SIGNAL(activated(int)),
978                 this, SLOT(change_adaptor()));
979         connect(marginsModule->headheightLE, SIGNAL(textChanged(QString)),
980                 this, SLOT(change_adaptor()));
981         connect(marginsModule->headheightUnit, SIGNAL(activated(int)),
982                 this, SLOT(change_adaptor()));
983         connect(marginsModule->headsepLE, SIGNAL(textChanged(QString)),
984                 this, SLOT(change_adaptor()));
985         connect(marginsModule->headsepUnit, SIGNAL(activated(int)),
986                 this, SLOT(change_adaptor()));
987         connect(marginsModule->footskipLE, SIGNAL(textChanged(QString)),
988                 this, SLOT(change_adaptor()));
989         connect(marginsModule->footskipUnit, SIGNAL(activated(int)),
990                 this, SLOT(change_adaptor()));
991         connect(marginsModule->columnsepLE, SIGNAL(textChanged(QString)),
992                 this, SLOT(change_adaptor()));
993         connect(marginsModule->columnsepUnit, SIGNAL(activated(int)),
994                 this, SLOT(change_adaptor()));
995         marginsModule->topLE->setValidator(new LengthValidator(
996                 marginsModule->topLE));
997         marginsModule->bottomLE->setValidator(new LengthValidator(
998                 marginsModule->bottomLE));
999         marginsModule->innerLE->setValidator(new LengthValidator(
1000                 marginsModule->innerLE));
1001         marginsModule->outerLE->setValidator(new LengthValidator(
1002                 marginsModule->outerLE));
1003         marginsModule->headsepLE->setValidator(new LengthValidator(
1004                 marginsModule->headsepLE));
1005         marginsModule->headheightLE->setValidator(new LengthValidator(
1006                 marginsModule->headheightLE));
1007         marginsModule->footskipLE->setValidator(new LengthValidator(
1008                 marginsModule->footskipLE));
1009         marginsModule->columnsepLE->setValidator(new LengthValidator(
1010                 marginsModule->columnsepLE));
1011
1012         bc().addCheckedLineEdit(marginsModule->topLE,
1013                 marginsModule->topL);
1014         bc().addCheckedLineEdit(marginsModule->bottomLE,
1015                 marginsModule->bottomL);
1016         bc().addCheckedLineEdit(marginsModule->innerLE,
1017                 marginsModule->innerL);
1018         bc().addCheckedLineEdit(marginsModule->outerLE,
1019                 marginsModule->outerL);
1020         bc().addCheckedLineEdit(marginsModule->headsepLE,
1021                 marginsModule->headsepL);
1022         bc().addCheckedLineEdit(marginsModule->headheightLE,
1023                 marginsModule->headheightL);
1024         bc().addCheckedLineEdit(marginsModule->footskipLE,
1025                 marginsModule->footskipL);
1026         bc().addCheckedLineEdit(marginsModule->columnsepLE,
1027                 marginsModule->columnsepL);
1028
1029
1030         // language & quote
1031         langModule = new UiWidget<Ui::LanguageUi>;
1032         connect(langModule->languageCO, SIGNAL(activated(int)),
1033                 this, SLOT(change_adaptor()));
1034         connect(langModule->languageCO, SIGNAL(activated(int)),
1035                 this, SLOT(languageChanged(int)));
1036         connect(langModule->defaultencodingRB, SIGNAL(clicked()),
1037                 this, SLOT(change_adaptor()));
1038         connect(langModule->otherencodingRB, SIGNAL(clicked()),
1039                 this, SLOT(change_adaptor()));
1040         connect(langModule->encodingCO, SIGNAL(activated(int)),
1041                 this, SLOT(change_adaptor()));
1042         connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
1043                 this, SLOT(change_adaptor()));
1044         connect(langModule->languagePackageCO, SIGNAL(activated(int)),
1045                 this, SLOT(change_adaptor()));
1046         connect(langModule->languagePackageLE, SIGNAL(textChanged(QString)),
1047                 this, SLOT(change_adaptor()));
1048         connect(langModule->languagePackageCO, SIGNAL(currentIndexChanged(int)),
1049                 this, SLOT(languagePackageChanged(int)));
1050         connect(langModule->dynamicQuotesCB, SIGNAL(clicked()),
1051                 this, SLOT(change_adaptor()));
1052
1053         langModule->languagePackageLE->setValidator(new NoNewLineValidator(
1054                 langModule->languagePackageLE));
1055
1056         QAbstractItemModel * language_model = guiApp->languageModel();
1057         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
1058         language_model->sort(0);
1059         langModule->languageCO->setModel(language_model);
1060         langModule->languageCO->setModelColumn(0);
1061
1062         // Always put the default encoding in the first position.
1063         langModule->encodingCO->addItem(qt_("Language Default (no inputenc)"));
1064         QStringList encodinglist;
1065         Encodings::const_iterator it = encodings.begin();
1066         Encodings::const_iterator const end = encodings.end();
1067         for (; it != end; ++it)
1068                 if (!it->unsafe())
1069                         encodinglist.append(qt_(it->guiName()));
1070         encodinglist.sort();
1071         langModule->encodingCO->addItems(encodinglist);
1072
1073         langModule->languagePackageCO->addItem(
1074                 qt_("Default"), toqstr("default"));
1075         langModule->languagePackageCO->addItem(
1076                 qt_("Automatic"), toqstr("auto"));
1077         langModule->languagePackageCO->addItem(
1078                 qt_("Always Babel"), toqstr("babel"));
1079         langModule->languagePackageCO->addItem(
1080                 qt_("Custom"), toqstr("custom"));
1081         langModule->languagePackageCO->addItem(
1082                 qt_("None[[language package]]"), toqstr("none"));
1083
1084
1085         // color
1086         colorModule = new UiWidget<Ui::ColorUi>;
1087         connect(colorModule->fontColorPB, SIGNAL(clicked()),
1088                 this, SLOT(changeFontColor()));
1089         connect(colorModule->delFontColorTB, SIGNAL(clicked()),
1090                 this, SLOT(deleteFontColor()));
1091         connect(colorModule->noteFontColorPB, SIGNAL(clicked()),
1092                 this, SLOT(changeNoteFontColor()));
1093         connect(colorModule->delNoteFontColorTB, SIGNAL(clicked()),
1094                 this, SLOT(deleteNoteFontColor()));
1095         connect(colorModule->backgroundPB, SIGNAL(clicked()),
1096                 this, SLOT(changeBackgroundColor()));
1097         connect(colorModule->delBackgroundTB, SIGNAL(clicked()),
1098                 this, SLOT(deleteBackgroundColor()));
1099         connect(colorModule->boxBackgroundPB, SIGNAL(clicked()),
1100                 this, SLOT(changeBoxBackgroundColor()));
1101         connect(colorModule->delBoxBackgroundTB, SIGNAL(clicked()),
1102                 this, SLOT(deleteBoxBackgroundColor()));
1103
1104
1105         // numbering
1106         numberingModule = new UiWidget<Ui::NumberingUi>;
1107         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
1108                 this, SLOT(change_adaptor()));
1109         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
1110                 this, SLOT(change_adaptor()));
1111         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
1112                 this, SLOT(updateNumbering()));
1113         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
1114                 this, SLOT(updateNumbering()));
1115         numberingModule->tocTW->setColumnCount(3);
1116         numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
1117         numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
1118         numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
1119         setSectionResizeMode(numberingModule->tocTW->header(), QHeaderView::ResizeToContents);
1120
1121         // biblio
1122         biblioModule = new UiWidget<Ui::BiblioUi>;
1123         connect(biblioModule->citeEngineCO, SIGNAL(activated(int)),
1124                 this, SLOT(citeEngineChanged(int)));
1125         connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
1126                 this, SLOT(citeStyleChanged()));
1127         connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
1128                 this, SLOT(biblioChanged()));
1129         connect(biblioModule->bibtexCO, SIGNAL(activated(int)),
1130                 this, SLOT(bibtexChanged(int)));
1131         connect(biblioModule->bibtexOptionsLE, SIGNAL(textChanged(QString)),
1132                 this, SLOT(biblioChanged()));
1133         connect(biblioModule->defaultBiblioCO, SIGNAL(activated(int)),
1134                 this, SLOT(biblioChanged()));
1135         connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
1136                 this, SLOT(biblioChanged()));
1137         connect(biblioModule->defaultBiblioCO, SIGNAL(editTextChanged(QString)),
1138                 this, SLOT(updateResetDefaultBiblio()));
1139         connect(biblioModule->rescanBibliosPB, SIGNAL(clicked()),
1140                 this, SLOT(rescanBibFiles()));
1141         connect(biblioModule->resetDefaultBiblioPB, SIGNAL(clicked()),
1142                 this, SLOT(resetDefaultBibfile()));
1143
1144         biblioModule->citeEngineCO->clear();
1145         for (LyXCiteEngine const & cet : theCiteEnginesList) {
1146                 biblioModule->citeEngineCO->addItem(qt_(cet.getName()), toqstr(cet.getID()));
1147                 int const i = biblioModule->citeEngineCO->findData(toqstr(cet.getID()));
1148                 biblioModule->citeEngineCO->setItemData(i, qt_(cet.getDescription()),
1149                                                         Qt::ToolTipRole);
1150         }
1151
1152         biblioModule->bibtexOptionsLE->setValidator(new NoNewLineValidator(
1153                 biblioModule->bibtexOptionsLE));
1154         biblioModule->defaultBiblioCO->lineEdit()->setValidator(new NoNewLineValidator(
1155                 biblioModule->defaultBiblioCO->lineEdit()));
1156
1157         // NOTE: we do not provide "custom" here for security reasons!
1158         biblioModule->bibtexCO->clear();
1159         biblioModule->bibtexCO->addItem(qt_("Default"), QString("default"));
1160         for (set<string>::const_iterator it = lyxrc.bibtex_alternatives.begin();
1161                              it != lyxrc.bibtex_alternatives.end(); ++it) {
1162                 QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
1163                 biblioModule->bibtexCO->addItem(command, command);
1164         }
1165
1166
1167         // indices
1168         indicesModule = new GuiIndices;
1169         connect(indicesModule, SIGNAL(changed()),
1170                 this, SLOT(change_adaptor()));
1171
1172
1173         // maths
1174         mathsModule = new UiWidget<Ui::MathsUi>;
1175         QStringList headers;
1176         headers << qt_("Package") << qt_("Load automatically")
1177                 << qt_("Load always") << qt_("Do not load");
1178         mathsModule->packagesTW->setHorizontalHeaderLabels(headers);
1179         setSectionResizeMode(mathsModule->packagesTW->horizontalHeader(), QHeaderView::Stretch);
1180         map<string, string> const & packages = BufferParams::auto_packages();
1181         mathsModule->packagesTW->setRowCount(packages.size());
1182         int i = 0;
1183         for (map<string, string>::const_iterator it = packages.begin();
1184              it != packages.end(); ++it) {
1185                 docstring const package = from_ascii(it->first);
1186                 QString autoTooltip = qt_(it->second);
1187                 QString alwaysTooltip;
1188                 if (package == "amsmath")
1189                         alwaysTooltip =
1190                                 qt_("The AMS LaTeX packages are always used");
1191                 else
1192                         alwaysTooltip = toqstr(bformat(
1193                                 _("The LaTeX package %1$s is always used"),
1194                                 package));
1195                 QString neverTooltip;
1196                 if (package == "amsmath")
1197                         neverTooltip =
1198                                 qt_("The AMS LaTeX packages are never used");
1199                 else
1200                         neverTooltip = toqstr(bformat(
1201                                 _("The LaTeX package %1$s is never used"),
1202                                 package));
1203                 QRadioButton * autoRB = new QRadioButton(mathsModule);
1204                 QRadioButton * alwaysRB = new QRadioButton(mathsModule);
1205                 QRadioButton * neverRB = new QRadioButton(mathsModule);
1206                 QButtonGroup * packageGroup = new QButtonGroup(mathsModule);
1207                 packageGroup->addButton(autoRB);
1208                 packageGroup->addButton(alwaysRB);
1209                 packageGroup->addButton(neverRB);
1210                 autoRB->setToolTip(autoTooltip);
1211                 alwaysRB->setToolTip(alwaysTooltip);
1212                 neverRB->setToolTip(neverTooltip);
1213                 QTableWidgetItem * pack = new QTableWidgetItem(toqstr(package));
1214                 mathsModule->packagesTW->setItem(i, 0, pack);
1215                 mathsModule->packagesTW->setCellWidget(i, 1, autoRB);
1216                 mathsModule->packagesTW->setCellWidget(i, 2, alwaysRB);
1217                 mathsModule->packagesTW->setCellWidget(i, 3, neverRB);
1218
1219                 connect(autoRB, SIGNAL(clicked()),
1220                         this, SLOT(change_adaptor()));
1221                 connect(alwaysRB, SIGNAL(clicked()),
1222                         this, SLOT(change_adaptor()));
1223                 connect(neverRB, SIGNAL(clicked()),
1224                         this, SLOT(change_adaptor()));
1225                 ++i;
1226         }
1227         connect(mathsModule->allPackagesAutoPB, SIGNAL(clicked()),
1228                 this, SLOT(allPackagesAuto()));
1229         connect(mathsModule->allPackagesAlwaysPB, SIGNAL(clicked()),
1230                 this, SLOT(allPackagesAlways()));
1231         connect(mathsModule->allPackagesNotPB, SIGNAL(clicked()),
1232                 this, SLOT(allPackagesNot()));
1233         connect(mathsModule->allPackagesAutoPB, SIGNAL(clicked()),
1234                 this, SLOT(change_adaptor()));
1235         connect(mathsModule->allPackagesAlwaysPB, SIGNAL(clicked()),
1236                 this, SLOT(change_adaptor()));
1237         connect(mathsModule->allPackagesNotPB, SIGNAL(clicked()),
1238                 this, SLOT(change_adaptor()));
1239
1240
1241         // latex class
1242         latexModule = new UiWidget<Ui::LaTeXUi>;
1243         connect(latexModule->optionsLE, SIGNAL(textChanged(QString)),
1244                 this, SLOT(change_adaptor()));
1245         connect(latexModule->defaultOptionsCB, SIGNAL(clicked()),
1246                 this, SLOT(change_adaptor()));
1247         connect(latexModule->psdriverCO, SIGNAL(activated(int)),
1248                 this, SLOT(change_adaptor()));
1249         connect(latexModule->classCO, SIGNAL(activated(int)),
1250                 this, SLOT(classChanged_adaptor()));
1251         connect(latexModule->classCO, SIGNAL(activated(int)),
1252                 this, SLOT(change_adaptor()));
1253         connect(latexModule->layoutPB, SIGNAL(clicked()),
1254                 this, SLOT(browseLayout()));
1255         connect(latexModule->layoutPB, SIGNAL(clicked()),
1256                 this, SLOT(change_adaptor()));
1257         connect(latexModule->childDocGB, SIGNAL(clicked()),
1258                 this, SLOT(change_adaptor()));
1259         connect(latexModule->childDocLE, SIGNAL(textChanged(QString)),
1260                 this, SLOT(change_adaptor()));
1261         connect(latexModule->childDocPB, SIGNAL(clicked()),
1262                 this, SLOT(browseMaster()));
1263         connect(latexModule->suppressDateCB, SIGNAL(clicked()),
1264                 this, SLOT(change_adaptor()));
1265         connect(latexModule->refstyleCB, SIGNAL(clicked()),
1266                 this, SLOT(change_adaptor()));
1267
1268         latexModule->optionsLE->setValidator(new NoNewLineValidator(
1269                 latexModule->optionsLE));
1270         latexModule->childDocLE->setValidator(new NoNewLineValidator(
1271                 latexModule->childDocLE));
1272
1273         // postscript drivers
1274         for (int n = 0; tex_graphics[n][0]; ++n) {
1275                 QString enc = qt_(tex_graphics_gui[n]);
1276                 latexModule->psdriverCO->addItem(enc);
1277         }
1278         // latex classes
1279         LayoutFileList const & bcl = LayoutFileList::get();
1280         vector<LayoutFileIndex> classList = bcl.classList();
1281         sort(classList.begin(), classList.end(), less_textclass_avail_desc());
1282
1283         vector<LayoutFileIndex>::const_iterator cit  = classList.begin();
1284         vector<LayoutFileIndex>::const_iterator cen = classList.end();
1285         for (int i = 0; cit != cen; ++cit, ++i) {
1286                 LayoutFile const & tc = bcl[*cit];
1287                 bool const available = tc.isTeXClassAvailable();
1288                 docstring const guiname = translateIfPossible(from_utf8(tc.description()));
1289                 // tooltip sensu "KOMA-Script Article [Class 'scrartcl']"
1290                 QString tooltip = toqstr(bformat(_("%1$s [Class '%2$s']"), guiname, from_utf8(tc.latexname())));
1291                 if (!available) {
1292                         docstring const output_type = (tc.outputType() == lyx::DOCBOOK) ? _("DocBook") : _("LaTeX");
1293                         tooltip += '\n' + toqstr(bformat(_("Class not found by LyX. "
1294                                                            "Please check if you have the matching %1$s class "
1295                                                            "and all required packages (%2$s) installed."),
1296                                                          output_type, from_utf8(tc.prerequisites(", "))));
1297                 }
1298                 latexModule->classCO->addItemSort(toqstr(tc.name()),
1299                                                   toqstr(guiname),
1300                                                   toqstr(translateIfPossible(from_utf8(tc.category()))),
1301                                                   tooltip,
1302                                                   true, true, true, available);
1303         }
1304
1305
1306         // branches
1307         branchesModule = new GuiBranches;
1308         connect(branchesModule, SIGNAL(changed()),
1309                 this, SLOT(change_adaptor()));
1310         connect(branchesModule, SIGNAL(renameBranches(docstring const &, docstring const &)),
1311                 this, SLOT(branchesRename(docstring const &, docstring const &)));
1312         connect(branchesModule, SIGNAL(okPressed()), this, SLOT(slotOK()));
1313         updateUnknownBranches();
1314
1315
1316         // preamble
1317         preambleModule = new PreambleModule;
1318         connect(preambleModule, SIGNAL(changed()),
1319                 this, SLOT(change_adaptor()));
1320
1321         localLayout = new LocalLayout;
1322         connect(localLayout, SIGNAL(changed()),
1323                 this, SLOT(change_adaptor()));
1324
1325
1326         // bullets
1327         bulletsModule = new BulletsModule;
1328         connect(bulletsModule, SIGNAL(changed()),
1329                 this, SLOT(change_adaptor()));
1330
1331
1332         // Modules
1333         modulesModule = new UiWidget<Ui::ModulesUi>;
1334         modulesModule->availableLV->header()->setVisible(false);
1335         setSectionResizeMode(modulesModule->availableLV->header(), QHeaderView::ResizeToContents);
1336         modulesModule->availableLV->header()->setStretchLastSection(false);
1337         selectionManager =
1338                 new ModuleSelectionManager(modulesModule->availableLV,
1339                         modulesModule->selectedLV,
1340                         modulesModule->addPB, modulesModule->deletePB,
1341                         modulesModule->upPB, modulesModule->downPB,
1342                         availableModel(), selectedModel(), this);
1343         connect(selectionManager, SIGNAL(updateHook()),
1344                 this, SLOT(updateModuleInfo()));
1345         connect(selectionManager, SIGNAL(selectionChanged()),
1346                 this, SLOT(modulesChanged()));
1347
1348
1349         // PDF support
1350         pdfSupportModule = new UiWidget<Ui::PDFSupportUi>;
1351         connect(pdfSupportModule->use_hyperrefGB, SIGNAL(toggled(bool)),
1352                 this, SLOT(change_adaptor()));
1353         connect(pdfSupportModule->titleLE, SIGNAL(textChanged(QString)),
1354                 this, SLOT(change_adaptor()));
1355         connect(pdfSupportModule->authorLE, SIGNAL(textChanged(QString)),
1356                 this, SLOT(change_adaptor()));
1357         connect(pdfSupportModule->subjectLE, SIGNAL(textChanged(QString)),
1358                 this, SLOT(change_adaptor()));
1359         connect(pdfSupportModule->keywordsLE, SIGNAL(textChanged(QString)),
1360                 this, SLOT(change_adaptor()));
1361         connect(pdfSupportModule->bookmarksGB, SIGNAL(toggled(bool)),
1362                 this, SLOT(change_adaptor()));
1363         connect(pdfSupportModule->bookmarksnumberedCB, SIGNAL(toggled(bool)),
1364                 this, SLOT(change_adaptor()));
1365         connect(pdfSupportModule->bookmarksopenGB, SIGNAL(toggled(bool)),
1366                 this, SLOT(change_adaptor()));
1367         connect(pdfSupportModule->bookmarksopenlevelSB, SIGNAL(valueChanged(int)),
1368                 this, SLOT(change_adaptor()));
1369         connect(pdfSupportModule->breaklinksCB, SIGNAL(toggled(bool)),
1370                 this, SLOT(change_adaptor()));
1371         connect(pdfSupportModule->pdfborderCB, SIGNAL(toggled(bool)),
1372                 this, SLOT(change_adaptor()));
1373         connect(pdfSupportModule->colorlinksCB, SIGNAL(toggled(bool)),
1374                 this, SLOT(change_adaptor()));
1375         connect(pdfSupportModule->backrefCO, SIGNAL(activated(int)),
1376                 this, SLOT(change_adaptor()));
1377         connect(pdfSupportModule->pdfusetitleCB, SIGNAL(toggled(bool)),
1378                 this, SLOT(change_adaptor()));
1379         connect(pdfSupportModule->fullscreenCB, SIGNAL(toggled(bool)),
1380                 this, SLOT(change_adaptor()));
1381         connect(pdfSupportModule->optionsLE, SIGNAL(textChanged(QString)),
1382                 this, SLOT(change_adaptor()));
1383
1384         pdfSupportModule->titleLE->setValidator(new NoNewLineValidator(
1385                 pdfSupportModule->titleLE));
1386         pdfSupportModule->authorLE->setValidator(new NoNewLineValidator(
1387                 pdfSupportModule->authorLE));
1388         pdfSupportModule->subjectLE->setValidator(new NoNewLineValidator(
1389                 pdfSupportModule->subjectLE));
1390         pdfSupportModule->keywordsLE->setValidator(new NoNewLineValidator(
1391                 pdfSupportModule->keywordsLE));
1392         pdfSupportModule->optionsLE->setValidator(new NoNewLineValidator(
1393                 pdfSupportModule->optionsLE));
1394
1395         for (int i = 0; backref_opts[i][0]; ++i)
1396                 pdfSupportModule->backrefCO->addItem(qt_(backref_opts_gui[i]));
1397
1398
1399         // float
1400         floatModule = new FloatPlacement;
1401         connect(floatModule, SIGNAL(changed()),
1402                 this, SLOT(change_adaptor()));
1403
1404
1405         // listings
1406         listingsModule = new UiWidget<Ui::ListingsSettingsUi>;
1407         connect(listingsModule->listingsED, SIGNAL(textChanged()),
1408                 this, SLOT(change_adaptor()));
1409         connect(listingsModule->bypassCB, SIGNAL(clicked()),
1410                 this, SLOT(change_adaptor()));
1411         connect(listingsModule->bypassCB, SIGNAL(clicked()),
1412                 this, SLOT(setListingsMessage()));
1413         connect(listingsModule->listingsED, SIGNAL(textChanged()),
1414                 this, SLOT(setListingsMessage()));
1415         listingsModule->listingsTB->setPlainText(
1416                 qt_("Input listings parameters below. Enter ? for a list of parameters."));
1417
1418
1419         // add the panels
1420         docPS->addPanel(latexModule, N_("Document Class"));
1421         docPS->addPanel(masterChildModule, N_("Child Documents"));
1422         docPS->addPanel(modulesModule, N_("Modules"));
1423         docPS->addPanel(localLayout, N_("Local Layout"));
1424         docPS->addPanel(fontModule, N_("Fonts"));
1425         docPS->addPanel(textLayoutModule, N_("Text Layout"));
1426         docPS->addPanel(pageLayoutModule, N_("Page Layout"));
1427         docPS->addPanel(marginsModule, N_("Page Margins"));
1428         docPS->addPanel(langModule, N_("Language"));
1429         docPS->addPanel(colorModule, N_("Colors"));
1430         docPS->addPanel(numberingModule, N_("Numbering & TOC"));
1431         docPS->addPanel(biblioModule, N_("Bibliography"));
1432         docPS->addPanel(indicesModule, N_("Indexes"));
1433         docPS->addPanel(pdfSupportModule, N_("PDF Properties"));
1434         docPS->addPanel(mathsModule, N_("Math Options"));
1435         docPS->addPanel(floatModule, N_("Float Placement"));
1436         docPS->addPanel(listingsModule, N_("Listings[[inset]]"));
1437         docPS->addPanel(bulletsModule, N_("Bullets"));
1438         docPS->addPanel(branchesModule, N_("Branches"));
1439         docPS->addPanel(outputModule, N_("Formats[[output]]"));
1440         docPS->addPanel(preambleModule, N_("LaTeX Preamble"));
1441         docPS->setCurrentPanel("Document Class");
1442 // FIXME: hack to work around resizing bug in Qt >= 4.2
1443 // bug verified with Qt 4.2.{0-3} (JSpitzm)
1444 #if QT_VERSION >= 0x040200
1445         docPS->updateGeometry();
1446 #endif
1447 }
1448
1449
1450 void GuiDocument::onBufferViewChanged()
1451 {
1452         if (isVisibleView())
1453                 initialiseParams("");
1454 }
1455
1456
1457 void GuiDocument::saveDefaultClicked()
1458 {
1459         saveDocDefault();
1460 }
1461
1462
1463 void GuiDocument::useDefaultsClicked()
1464 {
1465         useClassDefaults();
1466 }
1467
1468
1469 void GuiDocument::change_adaptor()
1470 {
1471         nonModuleChanged_ = true;
1472         changed();
1473 }
1474
1475
1476 void GuiDocument::includeonlyClicked(QTreeWidgetItem * item, int)
1477 {
1478         if (item == 0)
1479                 return;
1480
1481         string child = fromqstr(item->text(0));
1482         if (child.empty())
1483                 return;
1484
1485         if (std::find(includeonlys_.begin(),
1486                       includeonlys_.end(), child) != includeonlys_.end())
1487                 includeonlys_.remove(child);
1488         else
1489                 includeonlys_.push_back(child);
1490
1491         updateIncludeonlys();
1492         change_adaptor();
1493 }
1494
1495
1496 QString GuiDocument::validateListingsParameters()
1497 {
1498         if (listingsModule->bypassCB->isChecked())
1499                 return QString();
1500         string params = fromqstr(listingsModule->listingsED->toPlainText());
1501         return toqstr(InsetListingsParams(params).validate());
1502 }
1503
1504
1505 void GuiDocument::setListingsMessage()
1506 {
1507         // FIXME THREAD
1508         static bool isOK = true;
1509         QString msg = validateListingsParameters();
1510         if (msg.isEmpty()) {
1511                 if (isOK)
1512                         return;
1513                 isOK = true;
1514                 // listingsTB->setTextColor("black");
1515                 listingsModule->listingsTB->setPlainText(
1516                         qt_("Input listings parameters below. "
1517                             "Enter ? for a list of parameters."));
1518         } else {
1519                 isOK = false;
1520                 // listingsTB->setTextColor("red");
1521                 listingsModule->listingsTB->setPlainText(msg);
1522         }
1523 }
1524
1525
1526 void GuiDocument::setLSpacing(int item)
1527 {
1528         textLayoutModule->lspacingLE->setEnabled(item == 3);
1529 }
1530
1531
1532 void GuiDocument::setIndent(int item)
1533 {
1534         bool const enable = (item == 1);
1535         textLayoutModule->indentLE->setEnabled(enable);
1536         textLayoutModule->indentLengthCO->setEnabled(enable);
1537         textLayoutModule->skipLE->setEnabled(false);
1538         textLayoutModule->skipLengthCO->setEnabled(false);
1539         isValid();
1540 }
1541
1542
1543 void GuiDocument::enableIndent(bool indent)
1544 {
1545         textLayoutModule->skipLE->setEnabled(!indent);
1546         textLayoutModule->skipLengthCO->setEnabled(!indent);
1547         if (indent)
1548                 setIndent(textLayoutModule->indentCO->currentIndex());
1549 }
1550
1551
1552 void GuiDocument::setSkip(int item)
1553 {
1554         bool const enable = (item == 3);
1555         textLayoutModule->skipLE->setEnabled(enable);
1556         textLayoutModule->skipLengthCO->setEnabled(enable);
1557         isValid();
1558 }
1559
1560
1561 void GuiDocument::enableSkip(bool skip)
1562 {
1563         textLayoutModule->indentLE->setEnabled(!skip);
1564         textLayoutModule->indentLengthCO->setEnabled(!skip);
1565         if (skip)
1566                 setSkip(textLayoutModule->skipCO->currentIndex());
1567 }
1568
1569
1570 void GuiDocument::setMargins()
1571 {
1572         bool const extern_geometry =
1573                 documentClass().provides("geometry");
1574         marginsModule->marginCB->setEnabled(!extern_geometry);
1575         if (extern_geometry) {
1576                 marginsModule->marginCB->setChecked(false);
1577                 setCustomMargins(true);
1578         } else {
1579                 marginsModule->marginCB->setChecked(!bp_.use_geometry);
1580                 setCustomMargins(!bp_.use_geometry);
1581         }
1582 }
1583
1584
1585 void GuiDocument::papersizeChanged(int paper_size)
1586 {
1587         setCustomPapersize(paper_size == 1);
1588 }
1589
1590
1591 void GuiDocument::setCustomPapersize(bool custom)
1592 {
1593         pageLayoutModule->paperwidthL->setEnabled(custom);
1594         pageLayoutModule->paperwidthLE->setEnabled(custom);
1595         pageLayoutModule->paperwidthUnitCO->setEnabled(custom);
1596         pageLayoutModule->paperheightL->setEnabled(custom);
1597         pageLayoutModule->paperheightLE->setEnabled(custom);
1598         pageLayoutModule->paperheightLE->setFocus();
1599         pageLayoutModule->paperheightUnitCO->setEnabled(custom);
1600 }
1601
1602
1603 void GuiDocument::setColSep()
1604 {
1605         setCustomMargins(marginsModule->marginCB->checkState() == Qt::Checked);
1606 }
1607
1608
1609 void GuiDocument::setCustomMargins(bool custom)
1610 {
1611         marginsModule->topL->setEnabled(!custom);
1612         marginsModule->topLE->setEnabled(!custom);
1613         marginsModule->topUnit->setEnabled(!custom);
1614
1615         marginsModule->bottomL->setEnabled(!custom);
1616         marginsModule->bottomLE->setEnabled(!custom);
1617         marginsModule->bottomUnit->setEnabled(!custom);
1618
1619         marginsModule->innerL->setEnabled(!custom);
1620         marginsModule->innerLE->setEnabled(!custom);
1621         marginsModule->innerUnit->setEnabled(!custom);
1622
1623         marginsModule->outerL->setEnabled(!custom);
1624         marginsModule->outerLE->setEnabled(!custom);
1625         marginsModule->outerUnit->setEnabled(!custom);
1626
1627         marginsModule->headheightL->setEnabled(!custom);
1628         marginsModule->headheightLE->setEnabled(!custom);
1629         marginsModule->headheightUnit->setEnabled(!custom);
1630
1631         marginsModule->headsepL->setEnabled(!custom);
1632         marginsModule->headsepLE->setEnabled(!custom);
1633         marginsModule->headsepUnit->setEnabled(!custom);
1634
1635         marginsModule->footskipL->setEnabled(!custom);
1636         marginsModule->footskipLE->setEnabled(!custom);
1637         marginsModule->footskipUnit->setEnabled(!custom);
1638
1639         bool const enableColSep = !custom &&
1640                         textLayoutModule->twoColumnCB->checkState() == Qt::Checked;
1641         marginsModule->columnsepL->setEnabled(enableColSep);
1642         marginsModule->columnsepLE->setEnabled(enableColSep);
1643         marginsModule->columnsepUnit->setEnabled(enableColSep);
1644 }
1645
1646
1647 void GuiDocument::changeBackgroundColor()
1648 {
1649         QColor const & newColor = QColorDialog::getColor(
1650                 rgb2qcolor(set_backgroundcolor), asQWidget());
1651         if (!newColor.isValid())
1652                 return;
1653         // set the button color and text
1654         colorModule->backgroundPB->setStyleSheet(
1655                 colorButtonStyleSheet(newColor));
1656         colorModule->backgroundPB->setText(qt_("&Change..."));
1657         // save color
1658         set_backgroundcolor = rgbFromHexName(fromqstr(newColor.name()));
1659         is_backgroundcolor = true;
1660         change_adaptor();
1661 }
1662
1663
1664 void GuiDocument::deleteBackgroundColor()
1665 {
1666         // set the button color back to default by setting an empty StyleSheet
1667         colorModule->backgroundPB->setStyleSheet(QLatin1String(""));
1668         // change button text
1669         colorModule->backgroundPB->setText(qt_("&Default..."));
1670         // save default color (white)
1671         set_backgroundcolor = rgbFromHexName("#ffffff");
1672         is_backgroundcolor = false;
1673         change_adaptor();
1674 }
1675
1676
1677 void GuiDocument::changeFontColor()
1678 {
1679         QColor const & newColor = QColorDialog::getColor(
1680                 rgb2qcolor(set_fontcolor), asQWidget());
1681         if (!newColor.isValid())
1682                 return;
1683         // set the button color and text
1684         colorModule->fontColorPB->setStyleSheet(
1685                 colorButtonStyleSheet(newColor));
1686         colorModule->fontColorPB->setText(qt_("&Change..."));
1687         // save color
1688         set_fontcolor = rgbFromHexName(fromqstr(newColor.name()));
1689         is_fontcolor = true;
1690         change_adaptor();
1691 }
1692
1693
1694 void GuiDocument::deleteFontColor()
1695 {
1696         // set the button color back to default by setting an empty StyleSheet
1697         colorModule->fontColorPB->setStyleSheet(QLatin1String(""));
1698         // change button text
1699         colorModule->fontColorPB->setText(qt_("&Default..."));
1700         // save default color (black)
1701         set_fontcolor = rgbFromHexName("#000000");
1702         is_fontcolor = false;
1703         change_adaptor();
1704 }
1705
1706
1707 void GuiDocument::changeNoteFontColor()
1708 {
1709         QColor const & newColor = QColorDialog::getColor(
1710                 rgb2qcolor(set_notefontcolor), asQWidget());
1711         if (!newColor.isValid())
1712                 return;
1713         // set the button color
1714         colorModule->noteFontColorPB->setStyleSheet(
1715                 colorButtonStyleSheet(newColor));
1716         // save color
1717         set_notefontcolor = rgbFromHexName(fromqstr(newColor.name()));
1718         change_adaptor();
1719 }
1720
1721
1722 void GuiDocument::deleteNoteFontColor()
1723 {
1724         // set the button color back to pref
1725         theApp()->getRgbColor(Color_greyedouttext, set_notefontcolor);
1726         colorModule->noteFontColorPB->setStyleSheet(
1727                 colorButtonStyleSheet(rgb2qcolor(set_notefontcolor)));
1728         change_adaptor();
1729 }
1730
1731
1732 void GuiDocument::changeBoxBackgroundColor()
1733 {
1734         QColor const & newColor = QColorDialog::getColor(
1735                 rgb2qcolor(set_boxbgcolor), asQWidget());
1736         if (!newColor.isValid())
1737                 return;
1738         // set the button color
1739         colorModule->boxBackgroundPB->setStyleSheet(
1740                 colorButtonStyleSheet(newColor));
1741         // save color
1742         set_boxbgcolor = rgbFromHexName(fromqstr(newColor.name()));
1743         change_adaptor();
1744 }
1745
1746
1747 void GuiDocument::deleteBoxBackgroundColor()
1748 {
1749         // set the button color back to pref
1750         theApp()->getRgbColor(Color_shadedbg, set_boxbgcolor);
1751         colorModule->boxBackgroundPB->setStyleSheet(
1752                 colorButtonStyleSheet(rgb2qcolor(set_boxbgcolor)));
1753         change_adaptor();
1754 }
1755
1756
1757 void GuiDocument::updateQuoteStyles(bool const set)
1758 {
1759         Language const * lang = lyx::languages.getLanguage(
1760                 fromqstr(langModule->languageCO->itemData(
1761                         langModule->languageCO->currentIndex()).toString()));
1762
1763         InsetQuotesParams::QuoteStyle def = bp_.getQuoteStyle(lang->quoteStyle());
1764
1765         langModule->quoteStyleCO->clear();
1766
1767         bool has_default = false;
1768         for (int i = 0; i < quoteparams.stylescount(); ++i) {
1769                 InsetQuotesParams::QuoteStyle qs = InsetQuotesParams::QuoteStyle(i);
1770                 if (qs == InsetQuotesParams::DynamicQuotes)
1771                         continue;
1772                 bool const langdef = (qs == def);
1773                 if (langdef) {
1774                         // add the default style on top
1775                         langModule->quoteStyleCO->insertItem(0,
1776                                 toqstr(quoteparams.getGuiLabel(qs, langdef)), qs);
1777                         has_default = true;
1778                 }
1779                 else
1780                         langModule->quoteStyleCO->addItem(
1781                                 toqstr(quoteparams.getGuiLabel(qs, langdef)), qs);
1782         }
1783         if (set && has_default)
1784                 // (re)set to the default style
1785                 langModule->quoteStyleCO->setCurrentIndex(0);
1786 }
1787
1788
1789 void GuiDocument::languageChanged(int i)
1790 {
1791         // some languages only work with polyglossia
1792         Language const * lang = lyx::languages.getLanguage(
1793                 fromqstr(langModule->languageCO->itemData(i).toString()));
1794         if (lang->babel().empty() && !lang->polyglossia().empty()) {
1795                         // If we force to switch fontspec on, store
1796                         // current state (#8717)
1797                         if (fontModule->osFontsCB->isEnabled())
1798                                 forced_fontspec_activation =
1799                                         !fontModule->osFontsCB->isChecked();
1800                         fontModule->osFontsCB->setChecked(true);
1801                         fontModule->osFontsCB->setEnabled(false);
1802         }
1803         else {
1804                 fontModule->osFontsCB->setEnabled(true);
1805                 // If we have forced to switch fontspec on,
1806                 // restore previous state (#8717)
1807                 if (forced_fontspec_activation)
1808                         fontModule->osFontsCB->setChecked(false);
1809                 forced_fontspec_activation = false;
1810         }
1811
1812         // set appropriate quotation mark style
1813         updateQuoteStyles(true);
1814 }
1815
1816
1817 void GuiDocument::osFontsChanged(bool nontexfonts)
1818 {
1819         bool const tex_fonts = !nontexfonts;
1820         // store current fonts
1821         QString const font_roman = fontModule->fontsRomanCO->itemData(
1822                         fontModule->fontsRomanCO->currentIndex()).toString();
1823         QString const font_sans = fontModule->fontsSansCO->itemData(
1824                         fontModule->fontsSansCO->currentIndex()).toString();
1825         QString const font_typewriter = fontModule->fontsTypewriterCO->itemData(
1826                         fontModule->fontsTypewriterCO->currentIndex()).toString();
1827         QString const font_math = fontModule->fontsMathCO->itemData(
1828                         fontModule->fontsMathCO->currentIndex()).toString();
1829         int const font_sf_scale = fontModule->scaleSansSB->value();
1830         int const font_tt_scale = fontModule->scaleTypewriterSB->value();
1831
1832         updateFontlist();
1833         // store default format
1834         QString const dformat = outputModule->defaultFormatCO->itemData(
1835                 outputModule->defaultFormatCO->currentIndex()).toString();
1836         updateDefaultFormat();
1837         // try to restore default format
1838         int index = outputModule->defaultFormatCO->findData(dformat);
1839         // set to default if format is not found
1840         if (index == -1)
1841                 index = 0;
1842         outputModule->defaultFormatCO->setCurrentIndex(index);
1843
1844         // try to restore fonts which were selected two toggles ago
1845         index = fontModule->fontsRomanCO->findData(fontModule->font_roman);
1846         if (index != -1)
1847                 fontModule->fontsRomanCO->setCurrentIndex(index);
1848         index = fontModule->fontsSansCO->findData(fontModule->font_sans);
1849         if (index != -1)
1850                 fontModule->fontsSansCO->setCurrentIndex(index);
1851         index = fontModule->fontsTypewriterCO->findData(fontModule->font_typewriter);
1852         if (index != -1)
1853                 fontModule->fontsTypewriterCO->setCurrentIndex(index);
1854         index = fontModule->fontsMathCO->findData(fontModule->font_math);
1855         if (index != -1)
1856                 fontModule->fontsMathCO->setCurrentIndex(index);
1857         // save fonts for next next toggle
1858         fontModule->font_roman = font_roman;
1859         fontModule->font_sans = font_sans;
1860         fontModule->font_typewriter = font_typewriter;
1861         fontModule->font_math = font_math;
1862         fontModule->font_sf_scale = font_sf_scale;
1863         fontModule->font_tt_scale = font_tt_scale;
1864
1865         langModule->encodingCO->setEnabled(tex_fonts &&
1866                 !langModule->defaultencodingRB->isChecked());
1867         langModule->defaultencodingRB->setEnabled(tex_fonts);
1868         langModule->otherencodingRB->setEnabled(tex_fonts);
1869
1870         fontModule->fontsDefaultCO->setEnabled(tex_fonts);
1871         fontModule->fontsDefaultLA->setEnabled(tex_fonts);
1872         fontModule->cjkFontLE->setEnabled(tex_fonts);
1873         fontModule->cjkFontLA->setEnabled(tex_fonts);
1874
1875         updateFontOptions();
1876
1877         fontModule->fontencLA->setEnabled(tex_fonts);
1878         fontModule->fontencCO->setEnabled(tex_fonts);
1879         if (!tex_fonts)
1880                 fontModule->fontencLE->setEnabled(false);
1881         else
1882                 fontencChanged(fontModule->fontencCO->currentIndex());
1883 }
1884
1885
1886 void GuiDocument::mathFontChanged(int)
1887 {
1888         updateFontOptions();
1889 }
1890
1891
1892 void GuiDocument::fontOsfToggled(bool state)
1893 {
1894         if (fontModule->osFontsCB->isChecked())
1895                 return;
1896         QString font = fontModule->fontsRomanCO->itemData(
1897                         fontModule->fontsRomanCO->currentIndex()).toString();
1898         if (hasMonolithicExpertSet(font))
1899                 fontModule->fontScCB->setChecked(state);
1900 }
1901
1902
1903 void GuiDocument::fontScToggled(bool state)
1904 {
1905         if (fontModule->osFontsCB->isChecked())
1906                 return;
1907         QString font = fontModule->fontsRomanCO->itemData(
1908                         fontModule->fontsRomanCO->currentIndex()).toString();
1909         if (hasMonolithicExpertSet(font))
1910                 fontModule->fontOsfCB->setChecked(state);
1911 }
1912
1913
1914 void GuiDocument::updateFontOptions()
1915 {
1916         bool const tex_fonts = !fontModule->osFontsCB->isChecked();
1917         QString font;
1918         if (tex_fonts)
1919                 font = fontModule->fontsSansCO->itemData(
1920                                 fontModule->fontsSansCO->currentIndex()).toString();
1921         bool scaleable = providesScale(font);
1922         fontModule->scaleSansSB->setEnabled(scaleable);
1923         fontModule->scaleSansLA->setEnabled(scaleable);
1924         if (tex_fonts)
1925                 font = fontModule->fontsTypewriterCO->itemData(
1926                                 fontModule->fontsTypewriterCO->currentIndex()).toString();
1927         scaleable = providesScale(font);
1928         fontModule->scaleTypewriterSB->setEnabled(scaleable);
1929         fontModule->scaleTypewriterLA->setEnabled(scaleable);
1930         if (tex_fonts)
1931                 font = fontModule->fontsRomanCO->itemData(
1932                                 fontModule->fontsRomanCO->currentIndex()).toString();
1933         fontModule->fontScCB->setEnabled(providesSC(font));
1934         fontModule->fontOsfCB->setEnabled(providesOSF(font));
1935         updateMathFonts(font);
1936 }
1937
1938
1939 void GuiDocument::updateFontsize(string const & items, string const & sel)
1940 {
1941         fontModule->fontsizeCO->clear();
1942         fontModule->fontsizeCO->addItem(qt_("Default"));
1943
1944         for (int n = 0; !token(items,'|',n).empty(); ++n)
1945                 fontModule->fontsizeCO->
1946                         addItem(toqstr(token(items,'|',n)));
1947
1948         for (int n = 0; n < fontModule->fontsizeCO->count(); ++n) {
1949                 if (fromqstr(fontModule->fontsizeCO->itemText(n)) == sel) {
1950                         fontModule->fontsizeCO->setCurrentIndex(n);
1951                         break;
1952                 }
1953         }
1954 }
1955
1956
1957 bool GuiDocument::ot1() const
1958 {
1959         QString const fontenc =
1960                 fontModule->fontencCO->itemData(fontModule->fontencCO->currentIndex()).toString();
1961         return (fontenc == "default"
1962                 || (fontenc == "global" && (lyxrc.fontenc == "default" || lyxrc.fontenc == "OT1"))
1963                 || (fontenc == "custom" && fontModule->fontencLE->text() == "OT1"));
1964 }
1965
1966
1967 bool GuiDocument::completeFontset() const
1968 {
1969         return (fontModule->fontsSansCO->itemData(
1970                         fontModule->fontsSansCO->currentIndex()).toString() == "default"
1971                 && fontModule->fontsSansCO->itemData(
1972                         fontModule->fontsTypewriterCO->currentIndex()).toString() == "default");
1973 }
1974
1975
1976 bool GuiDocument::noMathFont() const
1977 {
1978         return (fontModule->fontsMathCO->itemData(
1979                 fontModule->fontsMathCO->currentIndex()).toString() == "default");
1980 }
1981
1982
1983 void GuiDocument::updateTexFonts()
1984 {
1985         LaTeXFonts::TexFontMap texfontmap = theLaTeXFonts().getLaTeXFonts();
1986
1987         LaTeXFonts::TexFontMap::const_iterator it = texfontmap.begin();
1988         LaTeXFonts::TexFontMap::const_iterator end = texfontmap.end();
1989         for (; it != end; ++it) {
1990                 LaTeXFont lf = it->second;
1991                 if (lf.name().empty()) {
1992                         LYXERR0("Error: Unnamed font: " << it->first);
1993                         continue;
1994                 }
1995                 docstring const family = lf.family();
1996                 docstring guiname = translateIfPossible(lf.guiname());
1997                 if (!lf.available(ot1(), noMathFont()))
1998                         guiname += _(" (not installed)");
1999                 if (family == "rm")
2000                         rmfonts_.insert(toqstr(guiname), toqstr(it->first));
2001                 else if (family == "sf")
2002                         sffonts_.insert(toqstr(guiname), toqstr(it->first));
2003                 else if (family == "tt")
2004                         ttfonts_.insert(toqstr(guiname), toqstr(it->first));
2005                 else if (family == "math")
2006                         mathfonts_.insert(toqstr(guiname), toqstr(it->first));
2007         }
2008 }
2009
2010
2011 void GuiDocument::updateFontlist()
2012 {
2013         fontModule->fontsRomanCO->clear();
2014         fontModule->fontsSansCO->clear();
2015         fontModule->fontsTypewriterCO->clear();
2016         fontModule->fontsMathCO->clear();
2017
2018         // With fontspec (XeTeX, LuaTeX), we have access to all system fonts, but not the LaTeX fonts
2019         if (fontModule->osFontsCB->isChecked()) {
2020                 fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
2021                 fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
2022                 fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
2023                 QString unimath = qt_("Non-TeX Fonts Default");
2024                 if (!LaTeXFeatures::isAvailable("unicode-math"))
2025                         unimath += qt_(" (not available)");
2026                 fontModule->fontsMathCO->addItem(qt_("Class Default (TeX Fonts)"), QString("auto"));
2027                 fontModule->fontsMathCO->addItem(unimath, QString("default"));
2028
2029                 QFontDatabase fontdb;
2030                 QStringList families(fontdb.families());
2031                 for (QStringList::Iterator it = families.begin(); it != families.end(); ++it) {
2032                         fontModule->fontsRomanCO->addItem(*it, *it);
2033                         fontModule->fontsSansCO->addItem(*it, *it);
2034                         fontModule->fontsTypewriterCO->addItem(*it, *it);
2035                 }
2036                 return;
2037         }
2038
2039         if (rmfonts_.empty())
2040                 updateTexFonts();
2041
2042         fontModule->fontsRomanCO->addItem(qt_("Default"), QString("default"));
2043         QMap<QString, QString>::const_iterator rmi = rmfonts_.constBegin();
2044         while (rmi != rmfonts_.constEnd()) {
2045                 fontModule->fontsRomanCO->addItem(rmi.key(), rmi.value());
2046                 ++rmi;
2047         }
2048
2049         fontModule->fontsSansCO->addItem(qt_("Default"), QString("default"));
2050         QMap<QString, QString>::const_iterator sfi = sffonts_.constBegin();
2051         while (sfi != sffonts_.constEnd()) {
2052                 fontModule->fontsSansCO->addItem(sfi.key(), sfi.value());
2053                 ++sfi;
2054         }
2055
2056         fontModule->fontsTypewriterCO->addItem(qt_("Default"), QString("default"));
2057         QMap<QString, QString>::const_iterator tti = ttfonts_.constBegin();
2058         while (tti != ttfonts_.constEnd()) {
2059                 fontModule->fontsTypewriterCO->addItem(tti.key(), tti.value());
2060                 ++tti;
2061         }
2062
2063         fontModule->fontsMathCO->addItem(qt_("Automatic"), QString("auto"));
2064         fontModule->fontsMathCO->addItem(qt_("Class Default"), QString("default"));
2065         QMap<QString, QString>::const_iterator mmi = mathfonts_.constBegin();
2066         while (mmi != mathfonts_.constEnd()) {
2067                 fontModule->fontsMathCO->addItem(mmi.key(), mmi.value());
2068                 ++mmi;
2069         }
2070 }
2071
2072
2073 void GuiDocument::fontencChanged(int item)
2074 {
2075         fontModule->fontencLE->setEnabled(
2076                 fontModule->fontencCO->itemData(item).toString() == "custom");
2077         // The availability of TeX fonts depends on the font encoding
2078         updateTexFonts();
2079         updateFontOptions();
2080 }
2081
2082
2083 void GuiDocument::updateMathFonts(QString const & rm)
2084 {
2085         if (fontModule->osFontsCB->isChecked())
2086                 return;
2087         QString const math =
2088                 fontModule->fontsMathCO->itemData(fontModule->fontsMathCO->currentIndex()).toString();
2089         int const i = fontModule->fontsMathCO->findData("default");
2090         if (providesNoMath(rm) && i == -1)
2091                 fontModule->fontsMathCO->insertItem(1, qt_("Class Default"), QString("default"));
2092         else if (!providesNoMath(rm) && i != -1) {
2093                 int const c = fontModule->fontsMathCO->currentIndex();
2094                 fontModule->fontsMathCO->removeItem(i);
2095                 if (c == i)
2096                         fontModule->fontsMathCO->setCurrentIndex(0);
2097         }
2098 }
2099
2100
2101 void GuiDocument::romanChanged(int item)
2102 {
2103         if (fontModule->osFontsCB->isChecked())
2104                 return;
2105         QString const font =
2106                 fontModule->fontsRomanCO->itemData(item).toString();
2107         fontModule->fontScCB->setEnabled(providesSC(font));
2108         fontModule->fontOsfCB->setEnabled(providesOSF(font));
2109         updateMathFonts(font);
2110 }
2111
2112
2113 void GuiDocument::sansChanged(int item)
2114 {
2115         if (fontModule->osFontsCB->isChecked())
2116                 return;
2117         QString const font =
2118                 fontModule->fontsSansCO->itemData(item).toString();
2119         bool scaleable = providesScale(font);
2120         fontModule->scaleSansSB->setEnabled(scaleable);
2121         fontModule->scaleSansLA->setEnabled(scaleable);
2122 }
2123
2124
2125 void GuiDocument::ttChanged(int item)
2126 {
2127         if (fontModule->osFontsCB->isChecked())
2128                 return;
2129         QString const font =
2130                 fontModule->fontsTypewriterCO->itemData(item).toString();
2131         bool scaleable = providesScale(font);
2132         fontModule->scaleTypewriterSB->setEnabled(scaleable);
2133         fontModule->scaleTypewriterLA->setEnabled(scaleable);
2134 }
2135
2136
2137 void GuiDocument::updatePagestyle(string const & items, string const & sel)
2138 {
2139         pagestyles.clear();
2140         pageLayoutModule->pagestyleCO->clear();
2141         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
2142
2143         for (int n = 0; !token(items, '|', n).empty(); ++n) {
2144                 string style = token(items, '|', n);
2145                 QString style_gui = qt_(style);
2146                 pagestyles.push_back(pair<string, QString>(style, style_gui));
2147                 pageLayoutModule->pagestyleCO->addItem(style_gui);
2148         }
2149
2150         if (sel == "default") {
2151                 pageLayoutModule->pagestyleCO->setCurrentIndex(0);
2152                 return;
2153         }
2154
2155         int nn = 0;
2156
2157         for (size_t i = 0; i < pagestyles.size(); ++i)
2158                 if (pagestyles[i].first == sel)
2159                         nn = pageLayoutModule->pagestyleCO->findText(pagestyles[i].second);
2160
2161         if (nn > 0)
2162                 pageLayoutModule->pagestyleCO->setCurrentIndex(nn);
2163 }
2164
2165
2166 void GuiDocument::browseLayout()
2167 {
2168         QString const label1 = qt_("Layouts|#o#O");
2169         QString const dir1 = toqstr(lyxrc.document_path);
2170         QStringList const filter(qt_("LyX Layout (*.layout)"));
2171         QString file = browseRelToParent(QString(), bufferFilePath(),
2172                 qt_("Local layout file"), filter, false,
2173                 label1, dir1);
2174
2175         if (!file.endsWith(".layout"))
2176                 return;
2177
2178         FileName layoutFile = support::makeAbsPath(fromqstr(file),
2179                 fromqstr(bufferFilePath()));
2180
2181         int const ret = Alert::prompt(_("Local layout file"),
2182                 _("The layout file you have selected is a local layout\n"
2183                   "file, not one in the system or user directory.\n"
2184                   "Your document will not work with this layout if you\n"
2185                   "move the layout file to a different directory."),
2186                   1, 1, _("&Set Layout"), _("&Cancel"));
2187         if (ret == 1)
2188                 return;
2189
2190         // load the layout file
2191         LayoutFileList & bcl = LayoutFileList::get();
2192         string classname = layoutFile.onlyFileName();
2193         // this will update an existing layout if that layout has been loaded before.
2194         LayoutFileIndex name = support::onlyFileName(bcl.addLocalLayout(
2195                 classname.substr(0, classname.size() - 7),
2196                 layoutFile.onlyPath().absFileName()));
2197
2198         if (name.empty()) {
2199                 Alert::error(_("Error"),
2200                         _("Unable to read local layout file."));
2201                 return;
2202         }
2203
2204         const_cast<Buffer &>(buffer()).setLayoutPos(layoutFile.onlyPath().absFileName());
2205
2206         // do not trigger classChanged if there is no change.
2207         if (latexModule->classCO->currentText() == toqstr(name))
2208                 return;
2209
2210         // add to combo box
2211         bool const avail = latexModule->classCO->set(toqstr(name));
2212         if (!avail) {
2213                 LayoutFile const & tc = bcl[name];
2214                 docstring const guiname = translateIfPossible(from_utf8(tc.description()));
2215                 // tooltip sensu "KOMA-Script Article [Class 'scrartcl']"
2216                 QString tooltip = toqstr(bformat(_("%1$s [Class '%2$s']"), guiname, from_utf8(tc.latexname())));
2217                 tooltip += '\n' + qt_("This is a local layout file.");
2218                 latexModule->classCO->addItemSort(toqstr(tc.name()), toqstr(guiname),
2219                                                   toqstr(translateIfPossible(from_utf8(tc.category()))),
2220                                                   tooltip,
2221                                                   true, true, true, true);
2222                 latexModule->classCO->set(toqstr(name));
2223         }
2224
2225         classChanged();
2226 }
2227
2228
2229 void GuiDocument::browseMaster()
2230 {
2231         QString const title = qt_("Select master document");
2232         QString const dir1 = toqstr(lyxrc.document_path);
2233         QString const old = latexModule->childDocLE->text();
2234         QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
2235         QStringList const filter(qt_("LyX Files (*.lyx)"));
2236         QString file = browseRelToSub(old, docpath, title, filter, false,
2237                 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
2238
2239         if (!file.isEmpty())
2240                 latexModule->childDocLE->setText(file);
2241 }
2242
2243
2244 void GuiDocument::classChanged_adaptor()
2245 {
2246         const_cast<Buffer &>(buffer()).setLayoutPos(string());
2247         classChanged();
2248 }
2249
2250
2251 void GuiDocument::classChanged()
2252 {
2253         int idx = latexModule->classCO->currentIndex();
2254         if (idx < 0)
2255                 return;
2256         string const classname = fromqstr(latexModule->classCO->getData(idx));
2257
2258         if (applyPB->isEnabled()) {
2259                 int const ret = Alert::prompt(_("Unapplied changes"),
2260                                 _("Some changes in the dialog were not yet applied.\n"
2261                                 "If you do not apply now, they will be lost after this action."),
2262                                 1, 1, _("&Apply"), _("&Dismiss"));
2263                 if (ret == 0)
2264                         applyView();
2265         }
2266
2267         // We load the TextClass as soon as it is selected. This is
2268         // necessary so that other options in the dialog can be updated
2269         // according to the new class. Note, however, that, if you use
2270         // the scroll wheel when sitting on the combo box, we'll load a
2271         // lot of TextClass objects very quickly....
2272         if (!bp_.setBaseClass(classname)) {
2273                 Alert::error(_("Error"), _("Unable to set document class."));
2274                 return;
2275         }
2276         if (lyxrc.auto_reset_options)
2277                 bp_.useClassDefaults();
2278
2279         // With the introduction of modules came a distinction between the base
2280         // class and the document class. The former corresponds to the main layout
2281         // file; the latter is that plus the modules (or the document-specific layout,
2282         // or  whatever else there could be). Our parameters come from the document
2283         // class. So when we set the base class, we also need to recreate the document
2284         // class. Otherwise, we still have the old one.
2285         bp_.makeDocumentClass();
2286         paramsToDialog();
2287 }
2288
2289
2290 void GuiDocument::languagePackageChanged(int i)
2291 {
2292          langModule->languagePackageLE->setEnabled(
2293                 langModule->languagePackageCO->itemData(i).toString() == "custom");
2294 }
2295
2296
2297 void GuiDocument::biblioChanged()
2298 {
2299         biblioChanged_ = true;
2300         change_adaptor();
2301 }
2302
2303
2304 void GuiDocument::rescanBibFiles()
2305 {
2306         rescanTexStyles("bst");
2307 }
2308
2309
2310 void GuiDocument::resetDefaultBibfile()
2311 {
2312         QString const engine =
2313                 biblioModule->citeEngineCO->itemData(
2314                                 biblioModule->citeEngineCO->currentIndex()).toString();
2315
2316         CiteEngineType const cet =
2317                 CiteEngineType(biblioModule->citeStyleCO->itemData(
2318                                                           biblioModule->citeStyleCO->currentIndex()).toInt());
2319
2320         updateDefaultBiblio(theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet));
2321 }
2322
2323
2324 void GuiDocument::citeEngineChanged(int n)
2325 {
2326         QString const engine =
2327                 biblioModule->citeEngineCO->itemData(n).toString();
2328
2329         vector<string> const engs =
2330                 theCiteEnginesList[fromqstr(engine)]->getEngineType();
2331
2332         updateCiteStyles(engs);
2333         resetDefaultBibfile();
2334
2335         biblioChanged();
2336 }
2337
2338
2339 void GuiDocument::citeStyleChanged()
2340 {
2341         QString const engine =
2342                 biblioModule->citeEngineCO->itemData(
2343                                 biblioModule->citeEngineCO->currentIndex()).toString();
2344         if (theCiteEnginesList[fromqstr(engine)]->isDefaultBiblio(
2345                                 fromqstr(biblioModule->defaultBiblioCO->currentText())))
2346                 resetDefaultBibfile();
2347
2348         biblioChanged();
2349 }
2350
2351
2352 void GuiDocument::bibtexChanged(int n)
2353 {
2354         biblioModule->bibtexOptionsLE->setEnabled(
2355                 biblioModule->bibtexCO->itemData(n).toString() != "default");
2356         biblioChanged();
2357 }
2358
2359
2360 void GuiDocument::updateCiteStyles(vector<string> const & engs, CiteEngineType const & sel)
2361 {
2362         biblioModule->citeStyleCO->clear();
2363
2364         vector<string>::const_iterator it  = engs.begin();
2365         vector<string>::const_iterator end = engs.end();
2366         for (; it != end; ++it) {
2367                 if (*it == "default")
2368                         biblioModule->citeStyleCO->addItem(qt_("Basic numerical"),
2369                                                            ENGINE_TYPE_DEFAULT);
2370                 else if (*it == "authoryear")
2371                         biblioModule->citeStyleCO->addItem(qt_("Author-year"),
2372                                                            ENGINE_TYPE_AUTHORYEAR);
2373                 else if (*it == "numerical")
2374                         biblioModule->citeStyleCO->addItem(qt_("Author-number"),
2375                                                            ENGINE_TYPE_NUMERICAL);
2376         }
2377         int i = biblioModule->citeStyleCO->findData(sel);
2378         if (biblioModule->citeStyleCO->findData(sel) == -1)
2379                 i = 0;
2380         biblioModule->citeStyleCO->setCurrentIndex(i);
2381
2382         biblioModule->citationStyleL->setEnabled(engs.size() > 1);
2383         biblioModule->citeStyleCO->setEnabled(engs.size() > 1);
2384 }
2385
2386
2387 void GuiDocument::updateEngineType(string const & items, CiteEngineType const & sel)
2388 {
2389         engine_types_.clear();
2390
2391         int nn = 0;
2392
2393         for (int n = 0; !token(items, '|', n).empty(); ++n) {
2394                 nn += 1;
2395                 string style = token(items, '|', n);
2396                 engine_types_.push_back(style);
2397         }
2398
2399         updateCiteStyles(engine_types_, sel);
2400 }
2401
2402
2403 namespace {
2404         // FIXME unicode
2405         // both of these should take a vector<docstring>
2406
2407         // This is an insanely complicated attempt to make this sort of thing
2408         // work with RTL languages.
2409         docstring formatStrVec(vector<string> const & v, docstring const & s)
2410         {
2411                 //this mess formats the list as "v[0], v[1], ..., [s] v[n]"
2412                 if (v.empty())
2413                         return docstring();
2414                 if (v.size() == 1)
2415                         return translateIfPossible(from_utf8(v[0]));
2416                 if (v.size() == 2) {
2417                         docstring retval = _("%1$s and %2$s");
2418                         retval = subst(retval, _("and"), s);
2419                         return bformat(retval, translateIfPossible(from_utf8(v[0])),
2420                                        translateIfPossible(from_utf8(v[1])));
2421                 }
2422                 // The idea here is to format all but the last two items...
2423                 int const vSize = v.size();
2424                 docstring t2 = _("%1$s, %2$s");
2425                 docstring retval = translateIfPossible(from_utf8(v[0]));
2426                 for (int i = 1; i < vSize - 2; ++i)
2427                         retval = bformat(t2, retval, translateIfPossible(from_utf8(v[i])));
2428                 //...and then to  plug them, and the last two, into this schema
2429                 docstring t = _("%1$s, %2$s, and %3$s");
2430                 t = subst(t, _("and"), s);
2431                 return bformat(t, retval, translateIfPossible(from_utf8(v[vSize - 2])),
2432                                translateIfPossible(from_utf8(v[vSize - 1])));
2433         }
2434
2435         vector<string> idsToNames(vector<string> const & idList)
2436         {
2437                 vector<string> retval;
2438                 vector<string>::const_iterator it  = idList.begin();
2439                 vector<string>::const_iterator end = idList.end();
2440                 for (; it != end; ++it) {
2441                         LyXModule const * const mod = theModuleList[*it];
2442                         if (!mod)
2443                                 retval.push_back(to_utf8(bformat(_("%1$s (unavailable)"),
2444                                                 translateIfPossible(from_utf8(*it)))));
2445                         else
2446                                 retval.push_back(mod->getName());
2447                 }
2448                 return retval;
2449         }
2450 } // end anonymous namespace
2451
2452
2453 void GuiDocument::modulesToParams(BufferParams & bp)
2454 {
2455         // update list of loaded modules
2456         bp.clearLayoutModules();
2457         int const srows = modules_sel_model_.rowCount();
2458         for (int i = 0; i < srows; ++i)
2459                 bp.addLayoutModule(modules_sel_model_.getIDString(i));
2460
2461         // update the list of removed modules
2462         bp.clearRemovedModules();
2463         LayoutModuleList const & reqmods = bp.baseClass()->defaultModules();
2464         list<string>::const_iterator rit = reqmods.begin();
2465         list<string>::const_iterator ren = reqmods.end();
2466
2467         // check each of the default modules
2468         for (; rit != ren; ++rit) {
2469                 list<string>::const_iterator mit = bp.getModules().begin();
2470                 list<string>::const_iterator men = bp.getModules().end();
2471                 bool found = false;
2472                 for (; mit != men; ++mit) {
2473                         if (*rit == *mit) {
2474                                 found = true;
2475                                 break;
2476                         }
2477                 }
2478                 if (!found) {
2479                         // the module isn't present so must have been removed by the user
2480                         bp.addRemovedModule(*rit);
2481                 }
2482         }
2483 }
2484
2485 void GuiDocument::modulesChanged()
2486 {
2487         modulesToParams(bp_);
2488
2489         if (applyPB->isEnabled() && nonModuleChanged_) {
2490                 int const ret = Alert::prompt(_("Unapplied changes"),
2491                                 _("Some changes in the dialog were not yet applied.\n"
2492                                 "If you do not apply now, they will be lost after this action."),
2493                                 1, 1, _("&Apply"), _("&Dismiss"));
2494                 if (ret == 0)
2495                         applyView();
2496         }
2497
2498         bp_.makeDocumentClass();
2499         paramsToDialog();
2500         changed();
2501 }
2502
2503
2504 void GuiDocument::updateModuleInfo()
2505 {
2506         selectionManager->update();
2507
2508         //Module description
2509         bool const focus_on_selected = selectionManager->selectedFocused();
2510         QAbstractItemView * lv;
2511         if (focus_on_selected)
2512                 lv = modulesModule->selectedLV;
2513         else
2514                 lv = modulesModule->availableLV;
2515         if (lv->selectionModel()->selectedIndexes().isEmpty()) {
2516                 modulesModule->infoML->document()->clear();
2517                 return;
2518         }
2519         QModelIndex const & idx = lv->selectionModel()->currentIndex();
2520         GuiIdListModel const & id_model =
2521                         focus_on_selected  ? modules_sel_model_ : modules_av_model_;
2522         string const modName = id_model.getIDString(idx.row());
2523         docstring desc = getModuleDescription(modName);
2524
2525         LayoutModuleList const & provmods = bp_.baseClass()->providedModules();
2526         if (std::find(provmods.begin(), provmods.end(), modName) != provmods.end()) {
2527                 if (!desc.empty())
2528                         desc += "\n";
2529                 desc += _("Module provided by document class.");
2530         }
2531
2532         docstring cat = getModuleCategory(modName);
2533         if (!cat.empty()) {
2534                 if (!desc.empty())
2535                         desc += "\n";
2536                 desc += bformat(_("Category: %1$s."), cat);
2537         }
2538
2539         vector<string> pkglist = getPackageList(modName);
2540         docstring pkgdesc = formatStrVec(pkglist, _("and"));
2541         if (!pkgdesc.empty()) {
2542                 if (!desc.empty())
2543                         desc += "\n";
2544                 desc += bformat(_("Package(s) required: %1$s."), pkgdesc);
2545         }
2546
2547         pkglist = getRequiredList(modName);
2548         if (!pkglist.empty()) {
2549                 vector<string> const reqdescs = idsToNames(pkglist);
2550                 pkgdesc = formatStrVec(reqdescs, _("or"));
2551                 if (!desc.empty())
2552                         desc += "\n";
2553                 desc += bformat(_("Modules required: %1$s."), pkgdesc);
2554         }
2555
2556         pkglist = getExcludedList(modName);
2557         if (!pkglist.empty()) {
2558                 vector<string> const reqdescs = idsToNames(pkglist);
2559                 pkgdesc = formatStrVec(reqdescs, _( "and"));
2560                 if (!desc.empty())
2561                         desc += "\n";
2562                 desc += bformat(_("Modules excluded: %1$s."), pkgdesc);
2563         }
2564
2565         if (!isModuleAvailable(modName)) {
2566                 if (!desc.empty())
2567                         desc += "\n";
2568                 desc += _("WARNING: Some required packages are unavailable!");
2569         }
2570
2571         modulesModule->infoML->document()->setPlainText(toqstr(desc));
2572 }
2573
2574
2575 void GuiDocument::updateNumbering()
2576 {
2577         DocumentClass const & tclass = documentClass();
2578
2579         numberingModule->tocTW->setUpdatesEnabled(false);
2580         numberingModule->tocTW->clear();
2581
2582         int const depth = numberingModule->depthSL->value();
2583         int const toc = numberingModule->tocSL->value();
2584         QString const no = qt_("No");
2585         QString const yes = qt_("Yes");
2586         QTreeWidgetItem * item = 0;
2587
2588         DocumentClass::const_iterator lit = tclass.begin();
2589         DocumentClass::const_iterator len = tclass.end();
2590         for (; lit != len; ++lit) {
2591                 int const toclevel = lit->toclevel;
2592                 if (toclevel != Layout::NOT_IN_TOC && !lit->counter.empty()) {
2593                         item = new QTreeWidgetItem(numberingModule->tocTW);
2594                         item->setText(0, toqstr(translateIfPossible(lit->name())));
2595                         item->setText(1, (toclevel <= depth) ? yes : no);
2596                         item->setText(2, (toclevel <= toc) ? yes : no);
2597                 }
2598         }
2599
2600         numberingModule->tocTW->setUpdatesEnabled(true);
2601         numberingModule->tocTW->update();
2602 }
2603
2604
2605 void GuiDocument::updateDefaultFormat()
2606 {
2607         if (!bufferview())
2608                 return;
2609         // make a copy in order to consider unapplied changes
2610         BufferParams param_copy = buffer().params();
2611         param_copy.useNonTeXFonts = fontModule->osFontsCB->isChecked();
2612         int const idx = latexModule->classCO->currentIndex();
2613         if (idx >= 0) {
2614                 string const classname = fromqstr(latexModule->classCO->getData(idx));
2615                 param_copy.setBaseClass(classname);
2616                 param_copy.makeDocumentClass(true);
2617         }
2618         outputModule->defaultFormatCO->blockSignals(true);
2619         outputModule->defaultFormatCO->clear();
2620         outputModule->defaultFormatCO->addItem(qt_("Default"),
2621                                 QVariant(QString("default")));
2622         FormatList const & formats =
2623                                 param_copy.exportableFormats(true);
2624         for (Format const * f : formats)
2625                 outputModule->defaultFormatCO->addItem
2626                         (toqstr(translateIfPossible(f->prettyname())),
2627                          QVariant(toqstr(f->name())));
2628         outputModule->defaultFormatCO->blockSignals(false);
2629 }
2630
2631
2632 bool GuiDocument::isChildIncluded(string const & child)
2633 {
2634         if (includeonlys_.empty())
2635                 return false;
2636         return (std::find(includeonlys_.begin(),
2637                           includeonlys_.end(), child) != includeonlys_.end());
2638 }
2639
2640
2641 void GuiDocument::applyView()
2642 {
2643         // preamble
2644         preambleModule->apply(bp_);
2645         localLayout->apply(bp_);
2646
2647         // date
2648         bp_.suppress_date = latexModule->suppressDateCB->isChecked();
2649         bp_.use_refstyle  = latexModule->refstyleCB->isChecked();
2650
2651         // biblio
2652         string const engine =
2653                 fromqstr(biblioModule->citeEngineCO->itemData(
2654                                 biblioModule->citeEngineCO->currentIndex()).toString());
2655         bp_.setCiteEngine(engine);
2656
2657         CiteEngineType const style = CiteEngineType(biblioModule->citeStyleCO->itemData(
2658                 biblioModule->citeStyleCO->currentIndex()).toInt());
2659         if (theCiteEnginesList[engine]->hasEngineType(style))
2660                 bp_.setCiteEngineType(style);
2661         else
2662                 bp_.setCiteEngineType(ENGINE_TYPE_DEFAULT);
2663
2664         bp_.use_bibtopic =
2665                 biblioModule->bibtopicCB->isChecked();
2666
2667         bp_.setDefaultBiblioStyle(fromqstr(biblioModule->defaultBiblioCO->currentText()));
2668
2669         string const bibtex_command =
2670                 fromqstr(biblioModule->bibtexCO->itemData(
2671                         biblioModule->bibtexCO->currentIndex()).toString());
2672         string const bibtex_options =
2673                 fromqstr(biblioModule->bibtexOptionsLE->text());
2674         if (bibtex_command == "default" || bibtex_options.empty())
2675                 bp_.bibtex_command = bibtex_command;
2676         else
2677                 bp_.bibtex_command = bibtex_command + " " + bibtex_options;
2678
2679         if (biblioChanged_) {
2680                 buffer().invalidateBibinfoCache();
2681                 buffer().removeBiblioTempFiles();
2682         }
2683
2684         // Indices
2685         indicesModule->apply(bp_);
2686
2687         // language & quotes
2688         if (langModule->defaultencodingRB->isChecked()) {
2689                 bp_.inputenc = "auto";
2690         } else {
2691                 int i = langModule->encodingCO->currentIndex();
2692                 if (i == 0)
2693                         bp_.inputenc = "default";
2694                 else {
2695                         QString const enc_gui =
2696                                 langModule->encodingCO->currentText();
2697                         Encodings::const_iterator it = encodings.begin();
2698                         Encodings::const_iterator const end = encodings.end();
2699                         bool found = false;
2700                         for (; it != end; ++it) {
2701                                 if (qt_(it->guiName()) == enc_gui &&
2702                                     !it->unsafe()) {
2703                                         bp_.inputenc = it->name();
2704                                         found = true;
2705                                         break;
2706                                 }
2707                         }
2708                         if (!found) {
2709                                 // should not happen
2710                                 lyxerr << "GuiDocument::apply: Unknown encoding! Resetting to default" << endl;
2711                                 bp_.inputenc = "default";
2712                         }
2713                 }
2714         }
2715
2716         bp_.quotes_style = (InsetQuotesParams::QuoteStyle) langModule->quoteStyleCO->itemData(
2717                 langModule->quoteStyleCO->currentIndex()).toInt();
2718         bp_.dynamic_quotes = langModule->dynamicQuotesCB->isChecked();
2719
2720         QString const langname = langModule->languageCO->itemData(
2721                 langModule->languageCO->currentIndex()).toString();
2722         Language const * newlang = lyx::languages.getLanguage(fromqstr(langname));
2723         Cursor & cur = const_cast<BufferView *>(bufferview())->cursor();
2724         // If current cursor language was the document language, then update it too.
2725         if (cur.current_font.language() == bp_.language) {
2726                 cur.current_font.setLanguage(newlang);
2727                 cur.real_current_font.setLanguage(newlang);
2728         }
2729         bp_.language = newlang;
2730
2731         QString const pack = langModule->languagePackageCO->itemData(
2732                 langModule->languagePackageCO->currentIndex()).toString();
2733         if (pack == "custom")
2734                 bp_.lang_package =
2735                         fromqstr(langModule->languagePackageLE->text());
2736         else
2737                 bp_.lang_package = fromqstr(pack);
2738
2739         //color
2740         bp_.backgroundcolor = set_backgroundcolor;
2741         bp_.isbackgroundcolor = is_backgroundcolor;
2742         bp_.fontcolor = set_fontcolor;
2743         bp_.isfontcolor = is_fontcolor;
2744         bp_.notefontcolor = set_notefontcolor;
2745         bp_.boxbgcolor = set_boxbgcolor;
2746
2747         // numbering
2748         if (bp_.documentClass().hasTocLevels()) {
2749                 bp_.tocdepth = numberingModule->tocSL->value();
2750                 bp_.secnumdepth = numberingModule->depthSL->value();
2751         }
2752
2753         // bullets
2754         bp_.user_defined_bullet(0) = bulletsModule->bullet(0);
2755         bp_.user_defined_bullet(1) = bulletsModule->bullet(1);
2756         bp_.user_defined_bullet(2) = bulletsModule->bullet(2);
2757         bp_.user_defined_bullet(3) = bulletsModule->bullet(3);
2758
2759         // packages
2760         bp_.graphics_driver =
2761                 tex_graphics[latexModule->psdriverCO->currentIndex()];
2762
2763         // text layout
2764         int idx = latexModule->classCO->currentIndex();
2765         if (idx >= 0) {
2766                 string const classname = fromqstr(latexModule->classCO->getData(idx));
2767                 bp_.setBaseClass(classname);
2768         }
2769
2770         // Modules
2771         modulesToParams(bp_);
2772
2773         // Math
2774         map<string, string> const & packages = BufferParams::auto_packages();
2775         for (map<string, string>::const_iterator it = packages.begin();
2776              it != packages.end(); ++it) {
2777                 QTableWidgetItem * item = mathsModule->packagesTW->findItems(toqstr(it->first), Qt::MatchExactly)[0];
2778                 if (!item)
2779                         continue;
2780                 int row = mathsModule->packagesTW->row(item);
2781                 QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
2782                 if (rb->isChecked()) {
2783                         bp_.use_package(it->first, BufferParams::package_auto);
2784                         continue;
2785                 }
2786                 rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
2787                 if (rb->isChecked()) {
2788                         bp_.use_package(it->first, BufferParams::package_on);
2789                         continue;
2790                 }
2791                 rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
2792                 if (rb->isChecked())
2793                         bp_.use_package(it->first, BufferParams::package_off);
2794         }
2795
2796         // Page Layout
2797         if (pageLayoutModule->pagestyleCO->currentIndex() == 0)
2798                 bp_.pagestyle = "default";
2799         else {
2800                 QString style_gui = pageLayoutModule->pagestyleCO->currentText();
2801                 for (size_t i = 0; i != pagestyles.size(); ++i)
2802                         if (pagestyles[i].second == style_gui)
2803                                 bp_.pagestyle = pagestyles[i].first;
2804         }
2805
2806         // Text Layout
2807         switch (textLayoutModule->lspacingCO->currentIndex()) {
2808         case 0:
2809                 bp_.spacing().set(Spacing::Single);
2810                 break;
2811         case 1:
2812                 bp_.spacing().set(Spacing::Onehalf);
2813                 break;
2814         case 2:
2815                 bp_.spacing().set(Spacing::Double);
2816                 break;
2817         case 3: {
2818                 string s = widgetToDoubleStr(textLayoutModule->lspacingLE);
2819                 if (s.empty())
2820                         bp_.spacing().set(Spacing::Single);
2821                 else
2822                         bp_.spacing().set(Spacing::Other, s);
2823                 break;
2824                 }
2825         }
2826
2827         if (textLayoutModule->twoColumnCB->isChecked())
2828                 bp_.columns = 2;
2829         else
2830                 bp_.columns = 1;
2831
2832         bp_.justification = textLayoutModule->justCB->isChecked();
2833
2834         if (textLayoutModule->indentRB->isChecked()) {
2835                 // if paragraphs are separated by an indentation
2836                 bp_.paragraph_separation = BufferParams::ParagraphIndentSeparation;
2837                 switch (textLayoutModule->indentCO->currentIndex()) {
2838                 case 0:
2839                         bp_.setIndentation(HSpace(HSpace::DEFAULT));
2840                         break;
2841                 case 1: {
2842                         HSpace indent = HSpace(
2843                                 widgetsToLength(textLayoutModule->indentLE,
2844                                 textLayoutModule->indentLengthCO)
2845                                 );
2846                         bp_.setIndentation(indent);
2847                         break;
2848                         }
2849                 default:
2850                         // this should never happen
2851                         bp_.setIndentation(HSpace(HSpace::DEFAULT));
2852                         break;
2853                 }
2854         } else {
2855                 // if paragraphs are separated by a skip
2856                 bp_.paragraph_separation = BufferParams::ParagraphSkipSeparation;
2857                 switch (textLayoutModule->skipCO->currentIndex()) {
2858                 case 0:
2859                         bp_.setDefSkip(VSpace(VSpace::SMALLSKIP));
2860                         break;
2861                 case 1:
2862                         bp_.setDefSkip(VSpace(VSpace::MEDSKIP));
2863                         break;
2864                 case 2:
2865                         bp_.setDefSkip(VSpace(VSpace::BIGSKIP));
2866                         break;
2867                 case 3:
2868                         {
2869                         VSpace vs = VSpace(
2870                                 widgetsToLength(textLayoutModule->skipLE,
2871                                 textLayoutModule->skipLengthCO)
2872                                 );
2873                         bp_.setDefSkip(vs);
2874                         break;
2875                         }
2876                 default:
2877                         // this should never happen
2878                         bp_.setDefSkip(VSpace(VSpace::MEDSKIP));
2879                         break;
2880                 }
2881         }
2882
2883         bp_.options =
2884                 fromqstr(latexModule->optionsLE->text());
2885
2886         bp_.use_default_options =
2887                 latexModule->defaultOptionsCB->isChecked();
2888
2889         if (latexModule->childDocGB->isChecked())
2890                 bp_.master =
2891                         fromqstr(latexModule->childDocLE->text());
2892         else
2893                 bp_.master = string();
2894
2895         // Master/Child
2896         bp_.clearIncludedChildren();
2897         if (masterChildModule->includeonlyRB->isChecked()) {
2898                 list<string>::const_iterator it = includeonlys_.begin();
2899                 for (; it != includeonlys_.end() ; ++it) {
2900                         bp_.addIncludedChildren(*it);
2901                 }
2902         }
2903         bp_.maintain_unincluded_children =
2904                 masterChildModule->maintainAuxCB->isChecked();
2905
2906         // Float Placement
2907         bp_.float_placement = floatModule->get();
2908
2909         // Listings
2910         // text should have passed validation
2911         bp_.listings_params =
2912                 InsetListingsParams(fromqstr(listingsModule->listingsED->toPlainText())).params();
2913
2914         // Formats
2915         bp_.default_output_format = fromqstr(outputModule->defaultFormatCO->itemData(
2916                 outputModule->defaultFormatCO->currentIndex()).toString());
2917
2918         bool const nontexfonts = fontModule->osFontsCB->isChecked();
2919         bp_.useNonTeXFonts = nontexfonts;
2920
2921         bp_.output_sync = outputModule->outputsyncCB->isChecked();
2922
2923         bp_.output_sync_macro = fromqstr(outputModule->synccustomCB->currentText());
2924
2925         int mathfmt = outputModule->mathoutCB->currentIndex();
2926         if (mathfmt == -1)
2927                 mathfmt = 0;
2928         BufferParams::MathOutput const mo =
2929                 static_cast<BufferParams::MathOutput>(mathfmt);
2930         bp_.html_math_output = mo;
2931         bp_.html_be_strict = outputModule->strictCB->isChecked();
2932         bp_.html_css_as_file = outputModule->cssCB->isChecked();
2933         bp_.html_math_img_scale = outputModule->mathimgSB->value();
2934         bp_.display_pixel_ratio = theGuiApp()->pixelRatio();
2935
2936         bp_.save_transient_properties =
2937                 outputModule->saveTransientPropertiesCB->isChecked();
2938
2939         // fonts
2940         bp_.fonts_roman[nontexfonts] =
2941                 fromqstr(fontModule->fontsRomanCO->
2942                         itemData(fontModule->fontsRomanCO->currentIndex()).toString());
2943         bp_.fonts_roman[!nontexfonts] = fromqstr(fontModule->font_roman);
2944
2945         bp_.fonts_sans[nontexfonts] =
2946                 fromqstr(fontModule->fontsSansCO->
2947                         itemData(fontModule->fontsSansCO->currentIndex()).toString());
2948         bp_.fonts_sans[!nontexfonts] = fromqstr(fontModule->font_sans);
2949
2950         bp_.fonts_typewriter[nontexfonts] =
2951                 fromqstr(fontModule->fontsTypewriterCO->
2952                         itemData(fontModule->fontsTypewriterCO->currentIndex()).toString());
2953         bp_.fonts_typewriter[!nontexfonts] = fromqstr(fontModule->font_typewriter);
2954
2955         bp_.fonts_math[nontexfonts] =
2956                 fromqstr(fontModule->fontsMathCO->
2957                         itemData(fontModule->fontsMathCO->currentIndex()).toString());
2958         bp_.fonts_math[!nontexfonts] = fromqstr(fontModule->font_math);
2959
2960         QString const fontenc =
2961                 fontModule->fontencCO->itemData(fontModule->fontencCO->currentIndex()).toString();
2962         if (fontenc == "custom")
2963                 bp_.fontenc = fromqstr(fontModule->fontencLE->text());
2964         else
2965                 bp_.fontenc = fromqstr(fontenc);
2966
2967         bp_.fonts_cjk =
2968                 fromqstr(fontModule->cjkFontLE->text());
2969
2970         bp_.use_microtype = fontModule->microtypeCB->isChecked();
2971
2972         bp_.fonts_sans_scale[nontexfonts] = fontModule->scaleSansSB->value();
2973         bp_.fonts_sans_scale[!nontexfonts] = fontModule->font_sf_scale;
2974
2975         bp_.fonts_typewriter_scale[nontexfonts] = fontModule->scaleTypewriterSB->value();
2976         bp_.fonts_typewriter_scale[!nontexfonts] = fontModule->font_tt_scale;
2977
2978         bp_.fonts_expert_sc = fontModule->fontScCB->isChecked();
2979
2980         bp_.fonts_old_figures = fontModule->fontOsfCB->isChecked();
2981
2982         if (nontexfonts)
2983                 bp_.fonts_default_family = "default";
2984         else
2985                 bp_.fonts_default_family = GuiDocument::fontfamilies[
2986                         fontModule->fontsDefaultCO->currentIndex()];
2987
2988         if (fontModule->fontsizeCO->currentIndex() == 0)
2989                 bp_.fontsize = "default";
2990         else
2991                 bp_.fontsize =
2992                         fromqstr(fontModule->fontsizeCO->currentText());
2993
2994         // paper
2995         bp_.papersize = PAPER_SIZE(
2996                 pageLayoutModule->papersizeCO->currentIndex());
2997
2998         bp_.paperwidth = widgetsToLength(pageLayoutModule->paperwidthLE,
2999                 pageLayoutModule->paperwidthUnitCO);
3000
3001         bp_.paperheight = widgetsToLength(pageLayoutModule->paperheightLE,
3002                 pageLayoutModule->paperheightUnitCO);
3003
3004         if (pageLayoutModule->facingPagesCB->isChecked())
3005                 bp_.sides = TwoSides;
3006         else
3007                 bp_.sides = OneSide;
3008
3009         if (pageLayoutModule->landscapeRB->isChecked())
3010                 bp_.orientation = ORIENTATION_LANDSCAPE;
3011         else
3012                 bp_.orientation = ORIENTATION_PORTRAIT;
3013
3014         // margins
3015         bp_.use_geometry = !marginsModule->marginCB->isChecked();
3016
3017         Ui::MarginsUi const * m = marginsModule;
3018
3019         bp_.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
3020         bp_.topmargin = widgetsToLength(m->topLE, m->topUnit);
3021         bp_.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
3022         bp_.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
3023         bp_.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
3024         bp_.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
3025         bp_.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
3026         bp_.columnsep = widgetsToLength(m->columnsepLE, m->columnsepUnit);
3027
3028         // branches
3029         branchesModule->apply(bp_);
3030
3031         // PDF support
3032         PDFOptions & pdf = bp_.pdfoptions();
3033         pdf.use_hyperref = pdfSupportModule->use_hyperrefGB->isChecked();
3034         pdf.title = fromqstr(pdfSupportModule->titleLE->text());
3035         pdf.author = fromqstr(pdfSupportModule->authorLE->text());
3036         pdf.subject = fromqstr(pdfSupportModule->subjectLE->text());
3037         pdf.keywords = fromqstr(pdfSupportModule->keywordsLE->text());
3038
3039         pdf.bookmarks = pdfSupportModule->bookmarksGB->isChecked();
3040         pdf.bookmarksnumbered = pdfSupportModule->bookmarksnumberedCB->isChecked();
3041         pdf.bookmarksopen = pdfSupportModule->bookmarksopenGB->isChecked();
3042         pdf.bookmarksopenlevel = pdfSupportModule->bookmarksopenlevelSB->value();
3043
3044         pdf.breaklinks = pdfSupportModule->breaklinksCB->isChecked();
3045         pdf.pdfborder = pdfSupportModule->pdfborderCB->isChecked();
3046         pdf.pdfusetitle = pdfSupportModule->pdfusetitleCB->isChecked();
3047         pdf.colorlinks = pdfSupportModule->colorlinksCB->isChecked();
3048         pdf.backref =
3049                 backref_opts[pdfSupportModule->backrefCO->currentIndex()];
3050         if (pdfSupportModule->fullscreenCB->isChecked())
3051                 pdf.pagemode = pdf.pagemode_fullscreen;
3052         else
3053                 pdf.pagemode.clear();
3054         pdf.quoted_options = pdf.quoted_options_check(
3055                                 fromqstr(pdfSupportModule->optionsLE->text()));
3056
3057         // reset tracker
3058         nonModuleChanged_ = false;
3059 }
3060
3061
3062 void GuiDocument::paramsToDialog()
3063 {
3064         // set the default unit
3065         Length::UNIT const default_unit = Length::defaultUnit();
3066
3067         // preamble
3068         preambleModule->update(bp_, id());
3069         localLayout->update(bp_, id());
3070
3071         // date
3072         latexModule->suppressDateCB->setChecked(bp_.suppress_date);
3073         latexModule->refstyleCB->setChecked(bp_.use_refstyle);
3074
3075         // biblio
3076         string const cite_engine = bp_.citeEngine().list().front();
3077
3078         biblioModule->citeEngineCO->setCurrentIndex(
3079                 biblioModule->citeEngineCO->findData(toqstr(cite_engine)));
3080
3081         updateEngineType(documentClass().opt_enginetype(),
3082                 bp_.citeEngineType());
3083
3084         biblioModule->citeStyleCO->setCurrentIndex(
3085                 biblioModule->citeStyleCO->findData(bp_.citeEngineType()));
3086
3087         biblioModule->bibtopicCB->setChecked(
3088                 bp_.use_bibtopic);
3089
3090         updateDefaultBiblio(bp_.defaultBiblioStyle());
3091
3092         string command;
3093         string options =
3094                 split(bp_.bibtex_command, command, ' ');
3095
3096         int const bpos = biblioModule->bibtexCO->findData(toqstr(command));
3097         if (bpos != -1) {
3098                 biblioModule->bibtexCO->setCurrentIndex(bpos);
3099                 biblioModule->bibtexOptionsLE->setText(toqstr(options).trimmed());
3100         } else {
3101                 // We reset to default if we do not know the specified compiler
3102                 // This is for security reasons
3103                 biblioModule->bibtexCO->setCurrentIndex(
3104                         biblioModule->bibtexCO->findData(toqstr("default")));
3105                 biblioModule->bibtexOptionsLE->clear();
3106         }
3107         biblioModule->bibtexOptionsLE->setEnabled(
3108                 biblioModule->bibtexCO->currentIndex() != 0);
3109
3110         biblioChanged_ = false;
3111
3112         // indices
3113         // We may be called when there is no Buffer, e.g., when 
3114         // the last view has just been closed.
3115         bool const isReadOnly = isBufferAvailable() ? buffer().isReadonly() : false;
3116         indicesModule->update(bp_, isReadOnly);
3117
3118         // language & quotes
3119         int const pos = langModule->languageCO->findData(toqstr(
3120                 bp_.language->lang()));
3121         langModule->languageCO->setCurrentIndex(pos);
3122
3123         updateQuoteStyles();
3124
3125         langModule->quoteStyleCO->setCurrentIndex(
3126                 bp_.quotes_style);
3127         langModule->dynamicQuotesCB->setChecked(bp_.dynamic_quotes);
3128
3129         bool default_enc = true;
3130         if (bp_.inputenc != "auto") {
3131                 default_enc = false;
3132                 if (bp_.inputenc == "default") {
3133                         langModule->encodingCO->setCurrentIndex(0);
3134                 } else {
3135                         string enc_gui;
3136                         Encodings::const_iterator it = encodings.begin();
3137                         Encodings::const_iterator const end = encodings.end();
3138                         for (; it != end; ++it) {
3139                                 if (it->name() == bp_.inputenc &&
3140                                     !it->unsafe()) {
3141                                         enc_gui = it->guiName();
3142                                         break;
3143                                 }
3144                         }
3145                         int const i = langModule->encodingCO->findText(
3146                                         qt_(enc_gui));
3147                         if (i >= 0)
3148                                 langModule->encodingCO->setCurrentIndex(i);
3149                         else
3150                                 // unknown encoding. Set to default.
3151                                 default_enc = true;
3152                 }
3153         }
3154         langModule->defaultencodingRB->setChecked(default_enc);
3155         langModule->otherencodingRB->setChecked(!default_enc);
3156
3157         int const p = langModule->languagePackageCO->findData(toqstr(bp_.lang_package));
3158         if (p == -1) {
3159                 langModule->languagePackageCO->setCurrentIndex(
3160                           langModule->languagePackageCO->findData("custom"));
3161                 langModule->languagePackageLE->setText(toqstr(bp_.lang_package));
3162         } else {
3163                 langModule->languagePackageCO->setCurrentIndex(p);
3164                 langModule->languagePackageLE->clear();
3165         }
3166
3167         //color
3168         if (bp_.isfontcolor) {
3169                 colorModule->fontColorPB->setStyleSheet(
3170                         colorButtonStyleSheet(rgb2qcolor(bp_.fontcolor)));
3171         }
3172         set_fontcolor = bp_.fontcolor;
3173         is_fontcolor = bp_.isfontcolor;
3174
3175         colorModule->noteFontColorPB->setStyleSheet(
3176                 colorButtonStyleSheet(rgb2qcolor(bp_.notefontcolor)));
3177         set_notefontcolor = bp_.notefontcolor;
3178
3179         if (bp_.isbackgroundcolor) {
3180                 colorModule->backgroundPB->setStyleSheet(
3181                         colorButtonStyleSheet(rgb2qcolor(bp_.backgroundcolor)));
3182         }
3183         set_backgroundcolor = bp_.backgroundcolor;
3184         is_backgroundcolor = bp_.isbackgroundcolor;
3185
3186         colorModule->boxBackgroundPB->setStyleSheet(
3187                 colorButtonStyleSheet(rgb2qcolor(bp_.boxbgcolor)));
3188         set_boxbgcolor = bp_.boxbgcolor;
3189
3190         // numbering
3191         int const min_toclevel = documentClass().min_toclevel();
3192         int const max_toclevel = documentClass().max_toclevel();
3193         if (documentClass().hasTocLevels()) {
3194                 numberingModule->setEnabled(true);
3195                 numberingModule->depthSL->setMinimum(min_toclevel - 1);
3196                 numberingModule->depthSL->setMaximum(max_toclevel);
3197                 numberingModule->depthSL->setValue(bp_.secnumdepth);
3198                 numberingModule->tocSL->setMaximum(min_toclevel - 1);
3199                 numberingModule->tocSL->setMaximum(max_toclevel);
3200                 numberingModule->tocSL->setValue(bp_.tocdepth);
3201                 updateNumbering();
3202         } else {
3203                 numberingModule->setEnabled(false);
3204                 numberingModule->tocTW->clear();
3205         }
3206
3207         // bullets
3208         bulletsModule->setBullet(0, bp_.user_defined_bullet(0));
3209         bulletsModule->setBullet(1, bp_.user_defined_bullet(1));
3210         bulletsModule->setBullet(2, bp_.user_defined_bullet(2));
3211         bulletsModule->setBullet(3, bp_.user_defined_bullet(3));
3212         bulletsModule->init();
3213
3214         // packages
3215         int nitem = findToken(tex_graphics, bp_.graphics_driver);
3216         if (nitem >= 0)
3217                 latexModule->psdriverCO->setCurrentIndex(nitem);
3218         updateModuleInfo();
3219
3220         map<string, string> const & packages = BufferParams::auto_packages();
3221         for (map<string, string>::const_iterator it = packages.begin();
3222              it != packages.end(); ++it) {
3223                 QTableWidgetItem * item = mathsModule->packagesTW->findItems(toqstr(it->first), Qt::MatchExactly)[0];
3224                 if (!item)
3225                         continue;
3226                 int row = mathsModule->packagesTW->row(item);
3227                 switch (bp_.use_package(it->first)) {
3228                         case BufferParams::package_off: {
3229                                 QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 3);
3230                                 rb->setChecked(true);
3231                                 break;
3232                         }
3233                         case BufferParams::package_on: {
3234                                 QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 2);
3235                                 rb->setChecked(true);
3236                                 break;
3237                         }
3238                         case BufferParams::package_auto: {
3239                                 QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, 1);
3240                                 rb->setChecked(true);
3241                                 break;
3242                         }
3243                 }
3244         }
3245
3246         switch (bp_.spacing().getSpace()) {
3247                 case Spacing::Other: nitem = 3; break;
3248                 case Spacing::Double: nitem = 2; break;
3249                 case Spacing::Onehalf: nitem = 1; break;
3250                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
3251         }
3252
3253         // text layout
3254         string const & layoutID = bp_.baseClassID();
3255         setLayoutComboByIDString(layoutID);
3256
3257         updatePagestyle(documentClass().opt_pagestyle(),
3258                                  bp_.pagestyle);
3259
3260         textLayoutModule->lspacingCO->setCurrentIndex(nitem);
3261         if (bp_.spacing().getSpace() == Spacing::Other) {
3262                 doubleToWidget(textLayoutModule->lspacingLE,
3263                         bp_.spacing().getValueAsString());
3264         }
3265         setLSpacing(nitem);
3266
3267         if (bp_.paragraph_separation == BufferParams::ParagraphIndentSeparation) {
3268                 textLayoutModule->indentRB->setChecked(true);
3269                 string indentation = bp_.getIndentation().asLyXCommand();
3270                 int indent = 0;
3271                 if (indentation != "default") {
3272                         lengthToWidgets(textLayoutModule->indentLE,
3273                         textLayoutModule->indentLengthCO,
3274                         indentation, default_unit);
3275                         indent = 1;
3276                 }
3277                 textLayoutModule->indentCO->setCurrentIndex(indent);
3278                 setIndent(indent);
3279         } else {
3280                 textLayoutModule->skipRB->setChecked(true);
3281                 int skip = 0;
3282                 switch (bp_.getDefSkip().kind()) {
3283                 case VSpace::SMALLSKIP:
3284                         skip = 0;
3285                         break;
3286                 case VSpace::MEDSKIP:
3287                         skip = 1;
3288                         break;
3289                 case VSpace::BIGSKIP:
3290                         skip = 2;
3291                         break;
3292                 case VSpace::LENGTH:
3293                         {
3294                         skip = 3;
3295                         string const length = bp_.getDefSkip().asLyXCommand();
3296                         lengthToWidgets(textLayoutModule->skipLE,
3297                                 textLayoutModule->skipLengthCO,
3298                                 length, default_unit);
3299                         break;
3300                         }
3301                 default:
3302                         skip = 0;
3303                         break;
3304                 }
3305                 textLayoutModule->skipCO->setCurrentIndex(skip);
3306                 setSkip(skip);
3307         }
3308
3309         textLayoutModule->twoColumnCB->setChecked(
3310                 bp_.columns == 2);
3311         textLayoutModule->justCB->setChecked(bp_.justification);
3312
3313         if (!bp_.options.empty()) {
3314                 latexModule->optionsLE->setText(
3315                         toqstr(bp_.options));
3316         } else {
3317                 latexModule->optionsLE->setText(QString());
3318         }
3319
3320         // latex
3321         latexModule->defaultOptionsCB->setChecked(
3322                         bp_.use_default_options);
3323         updateSelectedModules();
3324         selectionManager->updateProvidedModules(
3325                         bp_.baseClass()->providedModules());
3326         selectionManager->updateExcludedModules(
3327                         bp_.baseClass()->excludedModules());
3328
3329         if (!documentClass().options().empty()) {
3330                 latexModule->defaultOptionsLE->setText(
3331                         toqstr(documentClass().options()));
3332         } else {
3333                 latexModule->defaultOptionsLE->setText(
3334                         toqstr(_("[No options predefined]")));
3335         }
3336
3337         latexModule->defaultOptionsLE->setEnabled(
3338                 bp_.use_default_options
3339                 && !documentClass().options().empty());
3340
3341         latexModule->defaultOptionsCB->setEnabled(
3342                 !documentClass().options().empty());
3343
3344         if (!bp_.master.empty()) {
3345                 latexModule->childDocGB->setChecked(true);
3346                 latexModule->childDocLE->setText(
3347                         toqstr(bp_.master));
3348         } else {
3349                 latexModule->childDocLE->setText(QString());
3350                 latexModule->childDocGB->setChecked(false);
3351         }
3352
3353         // Master/Child
3354         if (!bufferview() || !buffer().hasChildren()) {
3355                 masterChildModule->childrenTW->clear();
3356                 includeonlys_.clear();
3357                 docPS->showPanel("Child Documents", false);
3358                 if (docPS->isCurrentPanel("Child Documents"))
3359                         docPS->setCurrentPanel("Document Class");
3360         } else {
3361                 docPS->showPanel("Child Documents", true);
3362                 masterChildModule->setEnabled(true);
3363                 includeonlys_ = bp_.getIncludedChildren();
3364                 updateIncludeonlys();
3365         }
3366         masterChildModule->maintainAuxCB->setChecked(
3367                 bp_.maintain_unincluded_children);
3368
3369         // Float Settings
3370         floatModule->set(bp_.float_placement);
3371
3372         // ListingsSettings
3373         // break listings_params to multiple lines
3374         string lstparams =
3375                 InsetListingsParams(bp_.listings_params).separatedParams();
3376         listingsModule->listingsED->setPlainText(toqstr(lstparams));
3377
3378         // Fonts
3379         // some languages only work with polyglossia/XeTeX
3380         Language const * lang = lyx::languages.getLanguage(
3381                 fromqstr(langModule->languageCO->itemData(
3382                         langModule->languageCO->currentIndex()).toString()));
3383         bool const need_fontspec =
3384                 lang->babel().empty() && !lang->polyglossia().empty();
3385         bool const os_fonts_available =
3386                 bp_.baseClass()->outputType() == lyx::LATEX
3387                 && LaTeXFeatures::isAvailable("fontspec");
3388         fontModule->osFontsCB->setEnabled(os_fonts_available && !need_fontspec);
3389         fontModule->osFontsCB->setChecked(
3390                 (os_fonts_available && bp_.useNonTeXFonts) || need_fontspec);
3391         updateFontsize(documentClass().opt_fontsize(),
3392                         bp_.fontsize);
3393
3394         QString font = toqstr(bp_.fontsRoman());
3395         int rpos = fontModule->fontsRomanCO->findData(font);
3396         if (rpos == -1) {
3397                 rpos = fontModule->fontsRomanCO->count();
3398                 fontModule->fontsRomanCO->addItem(font + qt_(" (not installed)"), font);
3399         }
3400         fontModule->fontsRomanCO->setCurrentIndex(rpos);
3401         fontModule->font_roman = toqstr(bp_.fonts_roman[!bp_.useNonTeXFonts]);
3402
3403         font = toqstr(bp_.fontsSans());
3404         int spos = fontModule->fontsSansCO->findData(font);
3405         if (spos == -1) {
3406                 spos = fontModule->fontsSansCO->count();
3407                 fontModule->fontsSansCO->addItem(font + qt_(" (not installed)"), font);
3408         }
3409         fontModule->fontsSansCO->setCurrentIndex(spos);
3410         fontModule->font_sans = toqstr(bp_.fonts_sans[!bp_.useNonTeXFonts]);
3411
3412         font = toqstr(bp_.fontsTypewriter());
3413         int tpos = fontModule->fontsTypewriterCO->findData(font);
3414         if (tpos == -1) {
3415                 tpos = fontModule->fontsTypewriterCO->count();
3416                 fontModule->fontsTypewriterCO->addItem(font + qt_(" (not installed)"), font);
3417         }
3418         fontModule->fontsTypewriterCO->setCurrentIndex(tpos);
3419         fontModule->font_typewriter = toqstr(bp_.fonts_typewriter[!bp_.useNonTeXFonts]);
3420
3421         font = toqstr(bp_.fontsMath());
3422         int mpos = fontModule->fontsMathCO->findData(font);
3423         if (mpos == -1) {
3424                 mpos = fontModule->fontsMathCO->count();
3425                 fontModule->fontsMathCO->addItem(font + qt_(" (not installed)"), font);
3426         }
3427         fontModule->fontsMathCO->setCurrentIndex(mpos);
3428         fontModule->font_math = toqstr(bp_.fonts_math[!bp_.useNonTeXFonts]);
3429
3430         if (bp_.useNonTeXFonts && os_fonts_available) {
3431                 fontModule->fontencLA->setEnabled(false);
3432                 fontModule->fontencCO->setEnabled(false);
3433                 fontModule->fontencLE->setEnabled(false);
3434         } else {
3435                 fontModule->fontencLA->setEnabled(true);
3436                 fontModule->fontencCO->setEnabled(true);
3437                 fontModule->fontencLE->setEnabled(true);
3438                 romanChanged(rpos);
3439                 sansChanged(spos);
3440                 ttChanged(tpos);
3441         }
3442
3443         if (!bp_.fonts_cjk.empty())
3444                 fontModule->cjkFontLE->setText(
3445                         toqstr(bp_.fonts_cjk));
3446         else
3447                 fontModule->cjkFontLE->setText(QString());
3448         
3449         fontModule->microtypeCB->setChecked(bp_.use_microtype);
3450
3451         fontModule->fontScCB->setChecked(bp_.fonts_expert_sc);
3452         fontModule->fontOsfCB->setChecked(bp_.fonts_old_figures);
3453         fontModule->scaleSansSB->setValue(bp_.fontsSansScale());
3454         fontModule->font_sf_scale = bp_.fonts_sans_scale[!bp_.useNonTeXFonts];
3455         fontModule->scaleTypewriterSB->setValue(bp_.fontsTypewriterScale());
3456         fontModule->font_tt_scale = bp_.fonts_typewriter_scale[!bp_.useNonTeXFonts];
3457
3458         int nn = findToken(GuiDocument::fontfamilies, bp_.fonts_default_family);
3459         if (nn >= 0)
3460                 fontModule->fontsDefaultCO->setCurrentIndex(nn);
3461
3462         if (bp_.fontenc == "global" || bp_.fontenc == "default") {
3463                 fontModule->fontencCO->setCurrentIndex(
3464                         fontModule->fontencCO->findData(toqstr(bp_.fontenc)));
3465                 fontModule->fontencLE->setEnabled(false);
3466         } else {
3467                 fontModule->fontencCO->setCurrentIndex(1);
3468                 fontModule->fontencLE->setText(toqstr(bp_.fontenc));
3469         }
3470
3471         // Formats
3472         // This must be set _after_ fonts since updateDefaultFormat()
3473         // checks osFontsCB settings.
3474         // update combobox with formats
3475         updateDefaultFormat();
3476         int index = outputModule->defaultFormatCO->findData(toqstr(
3477                 bp_.default_output_format));
3478         // set to default if format is not found
3479         if (index == -1)
3480                 index = 0;
3481         outputModule->defaultFormatCO->setCurrentIndex(index);
3482
3483         outputModule->outputsyncCB->setChecked(bp_.output_sync);
3484         outputModule->synccustomCB->setEditText(toqstr(bp_.output_sync_macro));
3485
3486         outputModule->mathimgSB->setValue(bp_.html_math_img_scale);
3487         outputModule->mathoutCB->setCurrentIndex(bp_.html_math_output);
3488         outputModule->strictCB->setChecked(bp_.html_be_strict);
3489         outputModule->cssCB->setChecked(bp_.html_css_as_file);
3490
3491         outputModule->saveTransientPropertiesCB
3492                 ->setChecked(bp_.save_transient_properties);
3493
3494         // paper
3495         bool const extern_geometry =
3496                 documentClass().provides("geometry");
3497         int const psize = bp_.papersize;
3498         pageLayoutModule->papersizeCO->setCurrentIndex(psize);
3499         setCustomPapersize(!extern_geometry && psize == 1);
3500         pageLayoutModule->papersizeCO->setEnabled(!extern_geometry);
3501
3502         bool const landscape =
3503                 bp_.orientation == ORIENTATION_LANDSCAPE;
3504         pageLayoutModule->landscapeRB->setChecked(landscape);
3505         pageLayoutModule->portraitRB->setChecked(!landscape);
3506         pageLayoutModule->landscapeRB->setEnabled(!extern_geometry);
3507         pageLayoutModule->portraitRB->setEnabled(!extern_geometry);
3508
3509         pageLayoutModule->facingPagesCB->setChecked(
3510                 bp_.sides == TwoSides);
3511
3512         lengthToWidgets(pageLayoutModule->paperwidthLE,
3513                 pageLayoutModule->paperwidthUnitCO, bp_.paperwidth, default_unit);
3514         lengthToWidgets(pageLayoutModule->paperheightLE,
3515                 pageLayoutModule->paperheightUnitCO, bp_.paperheight, default_unit);
3516
3517         // margins
3518         Ui::MarginsUi * m = marginsModule;
3519
3520         setMargins();
3521
3522         lengthToWidgets(m->topLE, m->topUnit,
3523                 bp_.topmargin, default_unit);
3524
3525         lengthToWidgets(m->bottomLE, m->bottomUnit,
3526                 bp_.bottommargin, default_unit);
3527
3528         lengthToWidgets(m->innerLE, m->innerUnit,
3529                 bp_.leftmargin, default_unit);
3530
3531         lengthToWidgets(m->outerLE, m->outerUnit,
3532                 bp_.rightmargin, default_unit);
3533
3534         lengthToWidgets(m->headheightLE, m->headheightUnit,
3535                 bp_.headheight, default_unit);
3536
3537         lengthToWidgets(m->headsepLE, m->headsepUnit,
3538                 bp_.headsep, default_unit);
3539
3540         lengthToWidgets(m->footskipLE, m->footskipUnit,
3541                 bp_.footskip, default_unit);
3542
3543         lengthToWidgets(m->columnsepLE, m->columnsepUnit,
3544                 bp_.columnsep, default_unit);
3545
3546         // branches
3547         updateUnknownBranches();
3548         branchesModule->update(bp_);
3549
3550         // PDF support
3551         PDFOptions const & pdf = bp_.pdfoptions();
3552         pdfSupportModule->use_hyperrefGB->setChecked(pdf.use_hyperref);
3553         if (bp_.documentClass().provides("hyperref"))
3554                 pdfSupportModule->use_hyperrefGB->setTitle(qt_("C&ustomize Hyperref Options"));
3555         else
3556                 pdfSupportModule->use_hyperrefGB->setTitle(qt_("&Use Hyperref Support"));
3557         pdfSupportModule->titleLE->setText(toqstr(pdf.title));
3558         pdfSupportModule->authorLE->setText(toqstr(pdf.author));
3559         pdfSupportModule->subjectLE->setText(toqstr(pdf.subject));
3560         pdfSupportModule->keywordsLE->setText(toqstr(pdf.keywords));
3561
3562         pdfSupportModule->bookmarksGB->setChecked(pdf.bookmarks);
3563         pdfSupportModule->bookmarksnumberedCB->setChecked(pdf.bookmarksnumbered);
3564         pdfSupportModule->bookmarksopenGB->setChecked(pdf.bookmarksopen);
3565
3566         pdfSupportModule->bookmarksopenlevelSB->setValue(pdf.bookmarksopenlevel);
3567
3568         pdfSupportModule->breaklinksCB->setChecked(pdf.breaklinks);
3569         pdfSupportModule->pdfborderCB->setChecked(pdf.pdfborder);
3570         pdfSupportModule->pdfusetitleCB->setChecked(pdf.pdfusetitle);
3571         pdfSupportModule->colorlinksCB->setChecked(pdf.colorlinks);
3572
3573         nn = findToken(backref_opts, pdf.backref);
3574         if (nn >= 0)
3575                 pdfSupportModule->backrefCO->setCurrentIndex(nn);
3576
3577         pdfSupportModule->fullscreenCB->setChecked
3578                 (pdf.pagemode == pdf.pagemode_fullscreen);
3579
3580         pdfSupportModule->optionsLE->setText(
3581                 toqstr(pdf.quoted_options));
3582
3583         // Make sure that the bc is in the INITIAL state
3584         if (bc().policy().buttonStatus(ButtonPolicy::RESTORE))
3585                 bc().restore();
3586
3587         // clear changed branches cache
3588         changedBranches_.clear();
3589
3590         // reset tracker
3591         nonModuleChanged_ = false;
3592 }
3593
3594
3595 void GuiDocument::saveDocDefault()
3596 {
3597         // we have to apply the params first
3598         applyView();
3599         saveAsDefault();
3600 }
3601
3602
3603 void GuiDocument::updateAvailableModules()
3604 {
3605         modules_av_model_.clear();
3606         list<modInfoStruct> modInfoList = getModuleInfo();
3607         // Sort names according to the locale
3608         modInfoList.sort([](modInfoStruct const & a, modInfoStruct const & b) {
3609                         return 0 < b.name.localeAwareCompare(a.name);
3610                 });
3611         int i = 0;
3612         for (modInfoStruct const & m : modInfoList) {
3613                 modules_av_model_.insertRow(i, m.name, m.id, m.description);
3614                 ++i;
3615         }
3616 }
3617
3618
3619 void GuiDocument::updateSelectedModules()
3620 {
3621         modules_sel_model_.clear();
3622         list<modInfoStruct> const selModList = getSelectedModules();
3623         int i = 0;
3624         for (modInfoStruct const & m : selModList) {
3625                 modules_sel_model_.insertRow(i, m.name, m.id, m.description);
3626                 ++i;
3627         }
3628 }
3629
3630
3631 void GuiDocument::updateIncludeonlys()
3632 {
3633         masterChildModule->childrenTW->clear();
3634         QString const no = qt_("No");
3635         QString const yes = qt_("Yes");
3636
3637         if (includeonlys_.empty()) {
3638                 masterChildModule->includeallRB->setChecked(true);
3639                 masterChildModule->childrenTW->setEnabled(false);
3640                 masterChildModule->maintainAuxCB->setEnabled(false);
3641         } else {
3642                 masterChildModule->includeonlyRB->setChecked(true);
3643                 masterChildModule->childrenTW->setEnabled(true);
3644                 masterChildModule->maintainAuxCB->setEnabled(true);
3645         }
3646         ListOfBuffers children = buffer().getChildren();
3647         ListOfBuffers::const_iterator it  = children.begin();
3648         ListOfBuffers::const_iterator end = children.end();
3649         bool has_unincluded = false;
3650         bool all_unincluded = true;
3651         for (; it != end; ++it) {
3652                 QTreeWidgetItem * item = new QTreeWidgetItem(masterChildModule->childrenTW);
3653                 // FIXME Unicode
3654                 string const name =
3655                         to_utf8(makeRelPath(from_utf8((*it)->fileName().absFileName()),
3656                                                         from_utf8(buffer().filePath())));
3657                 item->setText(0, toqstr(name));
3658                 item->setText(1, isChildIncluded(name) ? yes : no);
3659                 if (!isChildIncluded(name))
3660                         has_unincluded = true;
3661                 else
3662                         all_unincluded = false;
3663         }
3664         // Both if all childs are included and if none is included
3665         // is equal to "include all" (i.e., ommit \includeonly).
3666         // Thus, reset the GUI.
3667         if (!has_unincluded || all_unincluded) {
3668                 masterChildModule->includeallRB->setChecked(true);
3669                 masterChildModule->childrenTW->setEnabled(false);
3670                 includeonlys_.clear();
3671         }
3672         // If all are included, we need to update again.
3673         if (!has_unincluded)
3674                 updateIncludeonlys();
3675 }
3676
3677
3678 void GuiDocument::updateDefaultBiblio(string const & style)
3679 {
3680         QString const bibstyle = toqstr(style);
3681         biblioModule->defaultBiblioCO->clear();
3682
3683         int item_nr = -1;
3684
3685         QStringList str = texFileList("bstFiles.lst");
3686         // test whether we have a valid list, otherwise run rescan
3687         if (str.isEmpty()) {
3688                 rescanTexStyles("bst");
3689                 str = texFileList("bstFiles.lst");
3690         }
3691         for (int i = 0; i != str.size(); ++i)
3692                 str[i] = onlyFileName(str[i]);
3693         // sort on filename only (no path)
3694         str.sort();
3695
3696         for (int i = 0; i != str.count(); ++i) {
3697                 QString item = changeExtension(str[i], "");
3698                 if (item == bibstyle)
3699                         item_nr = i;
3700                 biblioModule->defaultBiblioCO->addItem(item);
3701         }
3702
3703         if (item_nr == -1 && !bibstyle.isEmpty()) {
3704                 biblioModule->defaultBiblioCO->addItem(bibstyle);
3705                 item_nr = biblioModule->defaultBiblioCO->count() - 1;
3706         }
3707
3708         if (item_nr != -1)
3709                 biblioModule->defaultBiblioCO->setCurrentIndex(item_nr);
3710         else
3711                 biblioModule->defaultBiblioCO->clearEditText();
3712
3713         updateResetDefaultBiblio();
3714 }
3715
3716
3717 void GuiDocument::updateResetDefaultBiblio()
3718 {
3719         QString const engine =
3720                 biblioModule->citeEngineCO->itemData(
3721                                 biblioModule->citeEngineCO->currentIndex()).toString();
3722         CiteEngineType const cet =
3723                 CiteEngineType(biblioModule->citeStyleCO->itemData(
3724                                                           biblioModule->citeStyleCO->currentIndex()).toInt());
3725         biblioModule->resetDefaultBiblioPB->setEnabled(
3726                 theCiteEnginesList[fromqstr(engine)]->getDefaultBiblio(cet)
3727                         != fromqstr(biblioModule->defaultBiblioCO->currentText()));
3728 }
3729
3730
3731 void GuiDocument::updateContents()
3732 {
3733         // Nothing to do here as the document settings is not cursor dependant.
3734         return;
3735 }
3736
3737
3738 void GuiDocument::useClassDefaults()
3739 {
3740         if (applyPB->isEnabled()) {
3741                 int const ret = Alert::prompt(_("Unapplied changes"),
3742                                 _("Some changes in the dialog were not yet applied.\n"
3743                                   "If you do not apply now, they will be lost after this action."),
3744                                 1, 1, _("&Apply"), _("&Dismiss"));
3745                 if (ret == 0)
3746                         applyView();
3747         }
3748
3749         int idx = latexModule->classCO->currentIndex();
3750         string const classname = fromqstr(latexModule->classCO->getData(idx));
3751         if (!bp_.setBaseClass(classname)) {
3752                 Alert::error(_("Error"), _("Unable to set document class."));
3753                 return;
3754         }
3755         bp_.useClassDefaults();
3756         paramsToDialog();
3757 }
3758
3759
3760 void GuiDocument::setLayoutComboByIDString(string const & idString)
3761 {
3762         if (!latexModule->classCO->set(toqstr(idString)))
3763                 Alert::warning(_("Can't set layout!"),
3764                         bformat(_("Unable to set layout for ID: %1$s"), from_utf8(idString)));
3765 }
3766
3767
3768 bool GuiDocument::isValid()
3769 {
3770         return
3771                 validateListingsParameters().isEmpty() &&
3772                 localLayout->isValid() &&
3773                 (
3774                         // if we're asking for skips between paragraphs
3775                         !textLayoutModule->skipRB->isChecked() ||
3776                         // then either we haven't chosen custom
3777                         textLayoutModule->skipCO->currentIndex() != 3 ||
3778                         // or else a length has been given
3779                         !textLayoutModule->skipLE->text().isEmpty()
3780                 ) &&
3781                 (
3782                         // if we're asking for indentation
3783                         !textLayoutModule->indentRB->isChecked() ||
3784                         // then either we haven't chosen custom
3785                         textLayoutModule->indentCO->currentIndex() != 1 ||
3786                         // or else a length has been given
3787                         !textLayoutModule->indentLE->text().isEmpty()
3788                 );
3789 }
3790
3791
3792 char const * const GuiDocument::fontfamilies[5] = {
3793         "default", "rmdefault", "sfdefault", "ttdefault", ""
3794 };
3795
3796
3797 char const * GuiDocument::fontfamilies_gui[5] = {
3798         N_("Default"), N_("Roman"), N_("Sans Serif"), N_("Typewriter"), ""
3799 };
3800
3801
3802 bool GuiDocument::initialiseParams(string const &)
3803 {
3804         BufferView const * view = bufferview();
3805         if (!view) {
3806                 bp_ = BufferParams();
3807                 paramsToDialog();
3808                 return true;
3809         }
3810         bp_ = view->buffer().params();
3811         loadModuleInfo();
3812         updateAvailableModules();
3813         //FIXME It'd be nice to make sure here that the selected
3814         //modules are consistent: That required modules are actually
3815         //selected, and that we don't have conflicts. If so, we could
3816         //at least pop up a warning.
3817         paramsToDialog();
3818         return true;
3819 }
3820
3821
3822 void GuiDocument::clearParams()
3823 {
3824         bp_ = BufferParams();
3825 }
3826
3827
3828 BufferId GuiDocument::id() const
3829 {
3830         BufferView const * const view = bufferview();
3831         return view? &view->buffer() : 0;
3832 }
3833
3834
3835 list<GuiDocument::modInfoStruct> const & GuiDocument::getModuleInfo()
3836 {
3837         return moduleNames_;
3838 }
3839
3840
3841 list<GuiDocument::modInfoStruct> const
3842 GuiDocument::makeModuleInfo(LayoutModuleList const & mods)
3843 {
3844         list<modInfoStruct> mInfo;
3845         for (string const & name : mods) {
3846                 modInfoStruct m;
3847                 LyXModule const * const mod = theModuleList[name];
3848                 if (mod)
3849                         m = modInfo(*mod);
3850                 else {
3851                         m.id = name;
3852                         m.name = toqstr(name + " (") + qt_("Not Found") + toqstr(")");
3853                 }
3854                 mInfo.push_back(m);
3855         }
3856         return mInfo;
3857 }
3858
3859
3860 list<GuiDocument::modInfoStruct> const GuiDocument::getSelectedModules()
3861 {
3862         return makeModuleInfo(params().getModules());
3863 }
3864
3865
3866 list<GuiDocument::modInfoStruct> const GuiDocument::getProvidedModules()
3867 {
3868         return makeModuleInfo(params().baseClass()->providedModules());
3869 }
3870
3871
3872 DocumentClass const & GuiDocument::documentClass() const
3873 {
3874         return bp_.documentClass();
3875 }
3876
3877
3878 static void dispatch_bufferparams(Dialog const & dialog,
3879         BufferParams const & bp, FuncCode lfun, Buffer const * buf)
3880 {
3881         ostringstream ss;
3882         ss << "\\begin_header\n";
3883         bp.writeFile(ss, buf);
3884         ss << "\\end_header\n";
3885         dialog.dispatch(FuncRequest(lfun, ss.str()));
3886 }
3887
3888
3889 void GuiDocument::dispatchParams()
3890 {
3891         // We need a non-const buffer object.
3892         Buffer & buf = const_cast<BufferView *>(bufferview())->buffer();
3893         // There may be several undo records; group them (bug #8998)
3894         buf.undo().beginUndoGroup();
3895
3896         // This must come first so that a language change is correctly noticed
3897         setLanguage();
3898
3899         // Apply the BufferParams. Note that this will set the base class
3900         // and then update the buffer's layout.
3901         dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY, &buffer());
3902
3903         if (!params().master.empty()) {
3904                 FileName const master_file = support::makeAbsPath(params().master,
3905                            support::onlyPath(buffer().absFileName()));
3906                 if (isLyXFileName(master_file.absFileName())) {
3907                         Buffer * master = checkAndLoadLyXFile(master_file);
3908                         if (master) {
3909                                 if (master->isChild(const_cast<Buffer *>(&buffer())))
3910                                         const_cast<Buffer &>(buffer()).setParent(master);
3911                                 else
3912                                         Alert::warning(_("Assigned master does not include this file"),
3913                                                 bformat(_("You must include this file in the document\n"
3914                                                           "'%1$s' in order to use the master document\n"
3915                                                           "feature."), from_utf8(params().master)));
3916                         } else
3917                                 Alert::warning(_("Could not load master"),
3918                                                 bformat(_("The master document '%1$s'\n"
3919                                                            "could not be loaded."),
3920                                                            from_utf8(params().master)));
3921                 }
3922         }
3923
3924         // Generate the colours requested by each new branch.
3925         BranchList & branchlist = params().branchlist();
3926         if (!branchlist.empty()) {
3927                 BranchList::const_iterator it = branchlist.begin();
3928                 BranchList::const_iterator const end = branchlist.end();
3929                 for (; it != end; ++it) {
3930                         docstring const & current_branch = it->branch();
3931                         Branch const * branch = branchlist.find(current_branch);
3932                         string const x11hexname = X11hexname(branch->color());
3933                         // display the new color
3934                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
3935                         dispatch(FuncRequest(LFUN_SET_COLOR, str));
3936                 }
3937
3938                 // Open insets of selected branches, close deselected ones
3939                 dispatch(FuncRequest(LFUN_INSET_FORALL,
3940                         "Branch inset-toggle assign"));
3941         }
3942         // rename branches in the document
3943         executeBranchRenaming();
3944         // and clear changed branches cache
3945         changedBranches_.clear();
3946
3947         // Generate the colours requested by indices.
3948         IndicesList & indiceslist = params().indiceslist();
3949         if (!indiceslist.empty()) {
3950                 IndicesList::const_iterator it = indiceslist.begin();
3951                 IndicesList::const_iterator const end = indiceslist.end();
3952                 for (; it != end; ++it) {
3953                         docstring const & current_index = it->shortcut();
3954                         Index const * index = indiceslist.findShortcut(current_index);
3955                         string const x11hexname = X11hexname(index->color());
3956                         // display the new color
3957                         docstring const str = current_index + ' ' + from_ascii(x11hexname);
3958                         dispatch(FuncRequest(LFUN_SET_COLOR, str));
3959                 }
3960         }
3961         // FIXME LFUN
3962         // If we used an LFUN, we would not need these two lines:
3963         BufferView * bv = const_cast<BufferView *>(bufferview());
3964         bv->processUpdateFlags(Update::Force | Update::FitCursor);
3965
3966         // Don't forget to close the group. Note that it is important
3967         // to check that there is no early return in the method.
3968         buf.undo().endUndoGroup();
3969 }
3970
3971
3972 void GuiDocument::setLanguage() const
3973 {
3974         Language const * const newL = bp_.language;
3975         if (buffer().params().language == newL)
3976                 return;
3977
3978         string const & lang_name = newL->lang();
3979         dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
3980 }
3981
3982
3983 void GuiDocument::saveAsDefault() const
3984 {
3985         dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT, &buffer());
3986 }
3987
3988
3989 bool GuiDocument::providesOSF(QString const & font) const
3990 {
3991         if (fontModule->osFontsCB->isChecked())
3992                 // FIXME: we should check if the fonts really
3993                 // have OSF support. But how?
3994                 return true;
3995         return theLaTeXFonts().getLaTeXFont(
3996                                 qstring_to_ucs4(font)).providesOSF(ot1(),
3997                                                                    completeFontset(),
3998                                                                    noMathFont());
3999 }
4000
4001
4002 bool GuiDocument::providesSC(QString const & font) const
4003 {
4004         if (fontModule->osFontsCB->isChecked())
4005                 return false;
4006         return theLaTeXFonts().getLaTeXFont(
4007                                 qstring_to_ucs4(font)).providesSC(ot1(),
4008                                                                   completeFontset(),
4009                                                                   noMathFont());
4010 }
4011
4012
4013 bool GuiDocument::providesScale(QString const & font) const
4014 {
4015         if (fontModule->osFontsCB->isChecked())
4016                 return true;
4017         return theLaTeXFonts().getLaTeXFont(
4018                                 qstring_to_ucs4(font)).providesScale(ot1(),
4019                                                                      completeFontset(),
4020                                                                      noMathFont());
4021 }
4022
4023
4024 bool GuiDocument::providesNoMath(QString const & font) const
4025 {
4026         if (fontModule->osFontsCB->isChecked())
4027                 return false;
4028         return theLaTeXFonts().getLaTeXFont(
4029                                 qstring_to_ucs4(font)).providesNoMath(ot1(),
4030                                                                       completeFontset());
4031 }
4032
4033
4034 bool GuiDocument::hasMonolithicExpertSet(QString const & font) const
4035 {
4036         if (fontModule->osFontsCB->isChecked())
4037                 return false;
4038         return theLaTeXFonts().getLaTeXFont(
4039                                 qstring_to_ucs4(font)).hasMonolithicExpertSet(ot1(),
4040                                                                               completeFontset(),
4041                                                                               noMathFont());
4042 }
4043
4044
4045 //static
4046 GuiDocument::modInfoStruct GuiDocument::modInfo(LyXModule const & mod)
4047 {
4048         // FIXME Unicode: docstrings would be better for these parameters but this
4049         // change requires a lot of others
4050         modInfoStruct m;
4051         m.id = mod.getID();
4052         m.name = toqstr(translateIfPossible(from_utf8(mod.getName())));
4053         QString desc = toqstr(translateIfPossible(from_utf8(mod.getDescription())));
4054         // Find the first sentence of the description
4055         QTextBoundaryFinder bf(QTextBoundaryFinder::Sentence, desc);
4056         int pos = bf.toNextBoundary();
4057         if (pos > 0)
4058                 desc.truncate(pos);
4059         QString modulename = QString(qt_("(Module name: %1)")).arg(toqstr(m.id));
4060         // Tooltip is the desc followed by the module name
4061         m.description = QString("%1<i>%2</i>")
4062                 .arg(desc.isEmpty() ? QString() : QString("<p>%1</p>").arg(desc),
4063                      modulename);
4064         return m;
4065 }
4066
4067
4068 void GuiDocument::loadModuleInfo()
4069 {
4070         moduleNames_.clear();
4071         for (LyXModule const & mod : theModuleList)
4072                 if (mod.category().substr(0, 8) != "Citation")
4073                         moduleNames_.push_back(modInfo(mod));
4074 }
4075
4076
4077 void GuiDocument::updateUnknownBranches()
4078 {
4079         if (!bufferview())
4080                 return;
4081         list<docstring> used_branches;
4082         buffer().getUsedBranches(used_branches);
4083         list<docstring>::const_iterator it = used_branches.begin();
4084         QStringList unknown_branches;
4085         for (; it != used_branches.end() ; ++it) {
4086                 if (!buffer().params().branchlist().find(*it))
4087                         unknown_branches.append(toqstr(*it));
4088         }
4089         branchesModule->setUnknownBranches(unknown_branches);
4090 }
4091
4092
4093 void GuiDocument::branchesRename(docstring const & oldname, docstring const & newname)
4094 {
4095         map<docstring, docstring>::iterator it = changedBranches_.begin();
4096         for (; it != changedBranches_.end() ; ++it) {
4097                 if (it->second == oldname) {
4098                         // branch has already been renamed
4099                         it->second = newname;
4100                         return;
4101                 }
4102         }
4103         // store new name
4104         changedBranches_[oldname] = newname;
4105 }
4106
4107
4108 void GuiDocument::executeBranchRenaming() const
4109 {
4110         map<docstring, docstring>::const_iterator it = changedBranches_.begin();
4111         for (; it != changedBranches_.end() ; ++it) {
4112                 docstring const arg = '"' + it->first + '"' + " " + '"' + it->second + '"';
4113                 dispatch(FuncRequest(LFUN_BRANCHES_RENAME, arg));
4114         }
4115 }
4116
4117
4118 void GuiDocument::allPackagesAuto()
4119 {
4120         allPackages(1);
4121 }
4122
4123
4124 void GuiDocument::allPackagesAlways()
4125 {
4126         allPackages(2);
4127 }
4128
4129
4130 void GuiDocument::allPackagesNot()
4131 {
4132         allPackages(3);
4133 }
4134
4135
4136 void GuiDocument::allPackages(int col)
4137 {
4138         for (int row = 0; row < mathsModule->packagesTW->rowCount(); ++row) {
4139                 QRadioButton * rb = (QRadioButton*)mathsModule->packagesTW->cellWidget(row, col);
4140                 rb->setChecked(true);
4141         }
4142 }
4143
4144
4145 Dialog * createGuiDocument(GuiView & lv) { return new GuiDocument(lv); }
4146
4147
4148 } // namespace frontend
4149 } // namespace lyx
4150
4151 #include "moc_GuiDocument.cpp"