]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDocument.cpp
Additional UI logic related to module requires and excludes.
[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 "BranchList.h"
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Color.h"
22 #include "EmbeddedFiles.h"
23 #include "Encoding.h"
24 #include "FloatPlacement.h"
25 #include "FuncRequest.h"
26 #include "support/gettext.h"
27 #include "GuiBranches.h"
28 #include "Language.h"
29 #include "LaTeXFeatures.h"
30 #include "LaTeXHighlighter.h"
31 #include "Layout.h"
32 #include "LengthCombo.h"
33 #include "LyXRC.h" // defaultUnit
34 #include "ModuleList.h"
35 #include "OutputParams.h"
36 #include "PanelStack.h"
37 #include "PDFOptions.h"
38 #include "qt_helpers.h"
39 #include "Spacing.h"
40 #include "TextClassList.h"
41 #include "Validator.h"
42
43 #include "insets/InsetListingsParams.h"
44
45 //#include "support/debug.h"
46 #include "support/FileName.h"
47 #include "support/filetools.h"
48 #include "support/lstrings.h"
49
50 #include <boost/bind.hpp>
51
52 #include <QCloseEvent>
53 #include <QScrollBar>
54 #include <QTextCursor>
55
56 #include <algorithm>
57 #include <sstream>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 ///
63 template<class Pair>
64 vector<typename Pair::second_type> const
65 getSecond(vector<Pair> const & pr)
66 {
67          vector<typename Pair::second_type> tmp(pr.size());
68          transform(pr.begin(), pr.end(), tmp.begin(),
69                                          boost::bind(&Pair::second, _1));
70          return tmp;
71 }
72
73 char const * const tex_graphics[] =
74 {
75         "default", "dvips", "dvitops", "emtex",
76         "ln", "oztex", "textures", "none", ""
77 };
78
79
80 char const * const tex_graphics_gui[] =
81 {
82         N_("Default"), "Dvips", "DVItoPS", "EmTeX",
83         "LN", "OzTeX", "Textures", N_("None"), ""
84 };
85
86
87 char const * const tex_fonts_roman[] =
88 {
89         "default", "cmr", "lmodern", "ae", "times", "palatino",
90         "charter", "newcent", "bookman", "utopia", "beraserif",
91         "ccfonts", "chancery", ""
92 };
93
94
95 char const * tex_fonts_roman_gui[] =
96 {
97         N_("Default"), N_("Computer Modern Roman"), N_("Latin Modern Roman"),
98         N_("AE (Almost European)"), N_("Times Roman"), N_("Palatino"),
99         N_("Bitstream Charter"), N_("New Century Schoolbook"), N_("Bookman"),
100         N_("Utopia"),  N_("Bera Serif"), N_("Concrete Roman"), N_("Zapf Chancery"),
101         ""
102 };
103
104
105 char const * const tex_fonts_sans[] =
106 {
107         "default", "cmss", "lmss", "helvet", "avant", "berasans", "cmbr", ""
108 };
109
110
111 char const * tex_fonts_sans_gui[] =
112 {
113         N_("Default"), N_("Computer Modern Sans"), N_("Latin Modern Sans"),
114         N_("Helvetica"), N_("Avant Garde"), N_("Bera Sans"), N_("CM Bright"), ""
115 };
116
117
118 char const * const tex_fonts_monospaced[] =
119 {
120         "default", "cmtt", "lmtt", "courier", "beramono", "luximono", "cmtl", ""
121 };
122
123
124 char const * tex_fonts_monospaced_gui[] =
125 {
126         N_("Default"), N_("Computer Modern Typewriter"),
127         N_("Latin Modern Typewriter"), N_("Courier"), N_("Bera Mono"),
128         N_("LuxiMono"), N_("CM Typewriter Light"), ""
129 };
130
131
132 vector<pair<string, lyx::docstring> > pagestyles;
133
134
135 namespace lyx {
136 namespace frontend {
137
138 namespace {
139         vector<string> getRequiredList(string const & modName) 
140         {
141                 LyXModule const * const mod = moduleList[modName];
142                 if (!mod)
143                         return vector<string>(); //empty such thing
144                 return mod->getRequiredModules();
145         }
146
147
148         vector<string> getExcludedList(string const & modName)
149         {
150                 LyXModule const * const mod = moduleList[modName];
151                 if (!mod)
152                         return vector<string>(); //empty such thing
153                 return mod->getExcludedModules();
154         }
155
156
157         docstring getModuleDescription(string const & modName)
158         {
159                 LyXModule const * const mod = moduleList[modName];
160                 if (!mod)
161                         return _("Module not found!");
162                 return from_ascii(mod->getDescription());
163         }
164
165
166         vector<string> getPackageList(string const & modName)
167         {
168                 LyXModule const * const mod = moduleList[modName];
169                 if (!mod)
170                         return vector<string>(); //empty such thing
171                 return mod->getPackageList();
172         }
173
174
175         bool isModuleAvailable(string const & modName)
176         {
177                 LyXModule * mod = moduleList[modName];
178                 if (!mod)
179                         return false;
180                 return mod->isAvailable();
181         }
182 } //anonymous namespace
183
184
185 ModuleSelMan::ModuleSelMan(
186         QListView * availableLV, 
187         QListView * selectedLV,
188         QPushButton * addPB, 
189         QPushButton * delPB, 
190         QPushButton * upPB, 
191         QPushButton * downPB,
192         QStringListModel * availableModel,
193         QStringListModel * selectedModel) :
194 GuiSelectionManager(availableLV, selectedLV, addPB, delPB,
195                     upPB, downPB, availableModel, selectedModel) 
196 {}
197         
198         
199 void ModuleSelMan::updateAddPB() 
200 {
201         int const arows = availableModel->stringList().size();
202         QModelIndexList const availSels = 
203                         availableLV->selectionModel()->selectedIndexes();
204         if (arows == 0 || availSels.isEmpty()  || isSelected(availSels.first())) {
205                 addPB->setEnabled(false);
206                 return;
207         }
208         
209         QModelIndex const & idx = availableLV->selectionModel()->currentIndex();
210         string const modName = fromqstr(idx.data().toString());
211         vector<string> reqs = getRequiredList(modName);
212         vector<string> excl = getExcludedList(modName);
213         
214         if (reqs.empty() && excl.empty()) {
215                 addPB->setEnabled(true);
216                 return;
217         }
218
219         QStringList const & qsl = selectedModel->stringList();
220         
221         //Check whether some required module is available
222         if (!reqs.empty()) {
223                 bool foundOne = false;
224                 vector<string>::const_iterator it  = reqs.begin();
225                 vector<string>::const_iterator end = reqs.end();
226                 for (; it != end; ++it) {
227                         if (qsl.contains(toqstr(*it))) {
228                                 foundOne = true;
229                                 break;
230                         }
231                 }
232                 if (!foundOne) {
233                         addPB->setEnabled(false);
234                         return;
235                 }
236         }
237         
238         //Check whether any excluded module is being used
239         if (!excl.empty()) {
240                 vector<string>::const_iterator it  = excl.begin();
241                 vector<string>::const_iterator end = excl.end();
242                 for (; it != end; ++it) {
243                         if (qsl.contains(toqstr(*it))) {
244                                 addPB->setEnabled(false);
245                                 return;
246                         }
247                 }
248         }
249
250         addPB->setEnabled(true);
251 }
252
253 void ModuleSelMan::updateDownPB()
254 {
255         int const srows = selectedModel->stringList().size();
256         if (srows == 0) {
257                 downPB->setEnabled(false);
258                 return;
259         }
260         QModelIndexList const selSels = 
261                         selectedLV->selectionModel()->selectedIndexes();
262         //disable if empty or last item is selected
263         if (selSels.empty() || selSels.first().row() == srows - 1) {
264                 downPB->setEnabled(false);
265                 return;
266         }
267         //determine whether immediately succeding element requires this one
268         QString const curModName = 
269                 selectedLV->selectionModel()->currentIndex().data().toString();
270         QStringList const & qsl = selectedModel->stringList();
271         int const curIdx = qsl.indexOf(curModName);
272         if (curIdx < 0 || curIdx == srows - 1) { //this shouldn't happen...
273                 downPB->setEnabled(false);
274                 return;
275         }
276         string nextModName = fromqstr(qsl[curIdx + 1]);
277
278         vector<string> reqs = getRequiredList(nextModName);
279
280         //if it doesn't require anything....
281         if (reqs.empty()) {
282                 downPB->setEnabled(true);
283                 return;
284         }
285
286         //FIXME This should perhaps be more flexible and check whether, even 
287         //if this one is required, there is also an earlier one that is required.
288         //enable it if this module isn't required
289         downPB->setEnabled(
290                         find(reqs.begin(), reqs.end(), fromqstr(curModName)) == reqs.end());
291 }
292
293 void ModuleSelMan::updateUpPB() 
294 {
295         int const srows = selectedModel->stringList().size();
296         if (srows == 0) {
297                 upPB->setEnabled(false);
298                 return;
299         }
300         QModelIndexList const selSels = 
301                         selectedLV->selectionModel()->selectedIndexes();
302         //disable if empty or first item is selected
303         if (selSels.empty() || selSels.first().row() == 0) {
304                 upPB->setEnabled(false);
305                 return;
306         }
307         //determine whether immediately preceding element is required by this one
308         QString const curModName = 
309                 selectedLV->selectionModel()->currentIndex().data().toString();
310         vector<string> reqs = getRequiredList(fromqstr(curModName));
311         
312         //if this one doesn't require anything....
313         if (reqs.empty()) {
314                 upPB->setEnabled(true);
315                 return;
316         }
317
318         QStringList const & qsl = selectedModel->stringList();
319         int const curIdx = qsl.indexOf(curModName);
320         if (curIdx <= 0) { //this shouldn't happen...
321                 upPB->setEnabled(false);
322                 return;
323         }
324         string preModName = fromqstr(qsl[curIdx - 1]);
325
326         //NOTE This is less flexible than it might be. You could check whether, even 
327         //if this one is required, there is also an earlier one that is required.
328         //enable it if the preceding module isn't required
329         upPB->setEnabled(find(reqs.begin(), reqs.end(), preModName) == reqs.end());
330 }
331
332 void ModuleSelMan::updateDelPB() 
333 {
334         int const srows = selectedModel->stringList().size();
335         if (srows == 0) {
336                 deletePB->setEnabled(false);
337                 return;
338         }
339         QModelIndexList const selSels = 
340                         selectedLV->selectionModel()->selectedIndexes();
341         if (selSels.empty() || selSels.first().row() < 0) {
342                 deletePB->setEnabled(false);
343                 return;
344         }
345         
346         //determine whether some LATER module requires this one
347         //NOTE Things are arranged so that this is the only way there
348         //can be a problem. At least, we hope so.
349         QString const curModName = 
350                         selectedLV->selectionModel()->currentIndex().data().toString();
351         QStringList const & qsl = selectedModel->stringList();
352         
353         //We're looking here for a reason NOT to enable the button. If we
354         //find one, we disable it and return. If we don't, we'll end up at
355         //the end of the function, and then we enable it.
356         QStringList::const_iterator it  = qsl.begin();
357         QStringList::const_iterator end = qsl.end();
358         bool found = false;
359         for (; it != end; ++it) {
360                 //skip over the ones preceding this one
361                 if (!found) {
362                         if (*it == curModName) {
363                                 found = true;
364                         }
365                         continue;
366                 }
367                         
368                 string const mod = fromqstr(*it);
369                 vector<string> reqs = getRequiredList(mod);
370                 //does this one require us?
371                 if (find(reqs.begin(), reqs.end(), fromqstr(curModName)) == reqs.end())
372                         //no...
373                         continue;
374
375                 //OK, so there is a module that requires us
376                 //is there an EARLIER module that satisfies the require?
377                 //NOTE We demand that it be earlier to keep the list of modules
378                 //consistent with the rule that a module must be proceeded by a
379                 //required module. There would be more flexible ways to proceed,
380                 //but that would be a lot more complicated, and the logic here is
381                 //already complicated. (That's why I've left the debugging code.)
382                 //lyxerr << "Testing " << mod << std::endl;
383                 QStringList::const_iterator it2  = qsl.begin();
384                 QStringList::const_iterator end2 = qsl.end();
385                 for (; it2 != end2; ++it2) {
386                         //lyxerr << "In loop: Testing " << fromqstr(*it2) << std::endl;
387                         if (*it2 == curModName) { //EARLIER!!
388                                 //no other module was found before this one, so...
389                                 //lyxerr << "Reached the end of the loop." << std::endl;
390                                 deletePB->setEnabled(false);
391                                 return;
392                         }
393                         //do we satisfy the require? 
394                         if (find(reqs.begin(), reqs.end(), fromqstr(*it2)) != reqs.end()) {
395                                 //lyxerr << fromqstr(*it2) << " does the trick." << std::endl;
396                                 break;
397                         }
398                 }
399                 //did we reach the end of the list?
400                 if (it2 == end2) {
401                         //lyxerr << "Reached end of list." << std::endl;
402                         deletePB->setEnabled(false);
403                         return;
404                 }
405         }
406         //lyxerr << "All's well that ends well." << std::endl;  
407         deletePB->setEnabled(true);
408 }
409
410
411 /////////////////////////////////////////////////////////////////////
412 //
413 // PreambleModule
414 //
415 /////////////////////////////////////////////////////////////////////
416
417 PreambleModule::PreambleModule(): current_id_(0)
418 {
419         // This is not a memory leak. The object will be destroyed
420         // with this.
421         (void) new LaTeXHighlighter(preambleTE->document());
422         setFocusProxy(preambleTE);
423         connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
424 }
425
426
427 void PreambleModule::update(BufferParams const & params, BufferId id)
428 {
429         QString preamble = toqstr(params.preamble);
430         // Nothing to do if the params and preamble are unchanged.
431         if (id == current_id_
432                 && preamble == preambleTE->document()->toPlainText())
433                 return;
434
435         QTextCursor cur = preambleTE->textCursor();
436         // Save the coords before switching to the new one.
437         preamble_coords_[current_id_] =
438                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
439
440         // Save the params address for further use.
441         current_id_ = id;
442         preambleTE->document()->setPlainText(preamble);
443         Coords::const_iterator it = preamble_coords_.find(current_id_);
444         if (it == preamble_coords_.end())
445                 // First time we open this one.
446                 preamble_coords_[current_id_] = make_pair(0,0);
447         else {
448                 // Restore saved coords.
449                 QTextCursor cur = preambleTE->textCursor();
450                 cur.setPosition(it->second.first);
451                 preambleTE->setTextCursor(cur);
452                 preambleTE->verticalScrollBar()->setValue(it->second.second);
453         }
454 }
455
456
457 void PreambleModule::apply(BufferParams & params)
458 {
459         params.preamble = fromqstr(preambleTE->document()->toPlainText());
460 }
461
462
463 void PreambleModule::closeEvent(QCloseEvent * e)
464 {
465         // Save the coords before closing.
466         QTextCursor cur = preambleTE->textCursor();
467         preamble_coords_[current_id_] =
468                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
469         e->accept();
470 }
471
472
473 /////////////////////////////////////////////////////////////////////
474 //
475 // DocumentDialog
476 //
477 /////////////////////////////////////////////////////////////////////
478
479
480
481 GuiDocument::GuiDocument(GuiView & lv)
482         : GuiDialog(lv, "document")
483 {
484         setupUi(this);
485         setViewTitle(_("Document Settings"));
486
487         lang_ = getSecond(getLanguageData(false));
488
489         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
490         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
491         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
492         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
493
494         connect(savePB, SIGNAL(clicked()), this, SLOT(saveDefaultClicked()));
495         connect(defaultPB, SIGNAL(clicked()), this, SLOT(useDefaultsClicked()));
496
497         // Manage the restore, ok, apply, restore and cancel/close buttons
498         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
499         bc().setOK(okPB);
500         bc().setApply(applyPB);
501         bc().setCancel(closePB);
502         bc().setRestore(restorePB);
503
504         textLayoutModule = new UiWidget<Ui::TextLayoutUi>;
505         // text layout
506         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
507                 this, SLOT(change_adaptor()));
508         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
509                 this, SLOT(setLSpacing(int)));
510         connect(textLayoutModule->lspacingLE, SIGNAL(textChanged(const QString&)),
511                 this, SLOT(change_adaptor()));
512         connect(textLayoutModule->skipRB, SIGNAL(clicked()),
513                 this, SLOT(change_adaptor()));
514         connect(textLayoutModule->indentRB, SIGNAL(clicked()),
515                 this, SLOT(change_adaptor()));
516         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
517                 this, SLOT(change_adaptor()));
518         connect(textLayoutModule->skipLE, SIGNAL(textChanged(const QString &)),
519                 this, SLOT(change_adaptor()));
520         connect(textLayoutModule->skipLengthCO, SIGNAL(activated(int)),
521                 this, SLOT(change_adaptor()));
522         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
523                 this, SLOT(setSkip(int)));
524         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)),
525                 this, SLOT(enableSkip(bool)));
526         connect(textLayoutModule->twoColumnCB, SIGNAL(clicked()),
527                 this, SLOT(change_adaptor()));
528         connect(textLayoutModule->listingsED, SIGNAL(textChanged()),
529                 this, SLOT(change_adaptor()));
530         connect(textLayoutModule->bypassCB, SIGNAL(clicked()), 
531                 this, SLOT(change_adaptor()));
532         connect(textLayoutModule->bypassCB, SIGNAL(clicked()), 
533                 this, SLOT(set_listings_msg()));
534         connect(textLayoutModule->listingsED, SIGNAL(textChanged()),
535                 this, SLOT(set_listings_msg()));
536         textLayoutModule->listingsTB->setPlainText(
537                 qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
538         textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
539                 textLayoutModule->lspacingLE));
540         textLayoutModule->skipLE->setValidator(unsignedLengthValidator(
541                 textLayoutModule->skipLE));
542
543         textLayoutModule->skipCO->addItem(qt_("SmallSkip"));
544         textLayoutModule->skipCO->addItem(qt_("MedSkip"));
545         textLayoutModule->skipCO->addItem(qt_("BigSkip"));
546         textLayoutModule->skipCO->addItem(qt_("Length"));
547         // remove the %-items from the unit choice
548         textLayoutModule->skipLengthCO->noPercents();
549         textLayoutModule->lspacingCO->insertItem(
550                 Spacing::Single, qt_("Single"));
551         textLayoutModule->lspacingCO->insertItem(
552                 Spacing::Onehalf, qt_("OneHalf"));
553         textLayoutModule->lspacingCO->insertItem(
554                 Spacing::Double, qt_("Double"));
555         textLayoutModule->lspacingCO->insertItem(
556                 Spacing::Other, qt_("Custom"));
557
558         // initialize the length validator
559         bc().addCheckedLineEdit(textLayoutModule->skipLE);
560
561         fontModule = new UiWidget<Ui::FontUi>;
562         // fonts
563         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
564                 this, SLOT(change_adaptor()));
565         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
566                 this, SLOT(romanChanged(int)));
567         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
568                 this, SLOT(change_adaptor()));
569         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
570                 this, SLOT(sansChanged(int)));
571         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
572                 this, SLOT(change_adaptor()));
573         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
574                 this, SLOT(ttChanged(int)));
575         connect(fontModule->fontsDefaultCO, SIGNAL(activated(int)),
576                 this, SLOT(change_adaptor()));
577         connect(fontModule->fontsizeCO, SIGNAL(activated(int)),
578                 this, SLOT(change_adaptor()));
579         connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)),
580                 this, SLOT(change_adaptor()));
581         connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)),
582                 this, SLOT(change_adaptor()));
583         connect(fontModule->fontScCB, SIGNAL(clicked()),
584                 this, SLOT(change_adaptor()));
585         connect(fontModule->fontOsfCB, SIGNAL(clicked()),
586                 this, SLOT(change_adaptor()));
587
588         for (int n = 0; tex_fonts_roman[n][0]; ++n) {
589                 QString font = qt_(tex_fonts_roman_gui[n]);
590                 if (!isFontAvailable(tex_fonts_roman[n]))
591                         font += qt_(" (not installed)");
592                 fontModule->fontsRomanCO->addItem(font);
593         }
594         for (int n = 0; tex_fonts_sans[n][0]; ++n) {
595                 QString font = qt_(tex_fonts_sans_gui[n]);
596                 if (!isFontAvailable(tex_fonts_sans[n]))
597                         font += qt_(" (not installed)");
598                 fontModule->fontsSansCO->addItem(font);
599         }
600         for (int n = 0; tex_fonts_monospaced[n][0]; ++n) {
601                 QString font = qt_(tex_fonts_monospaced_gui[n]);
602                 if (!isFontAvailable(tex_fonts_monospaced[n]))
603                         font += qt_(" (not installed)");
604                 fontModule->fontsTypewriterCO->addItem(font);
605         }
606
607         fontModule->fontsizeCO->addItem(qt_("Default"));
608         fontModule->fontsizeCO->addItem(qt_("10"));
609         fontModule->fontsizeCO->addItem(qt_("11"));
610         fontModule->fontsizeCO->addItem(qt_("12"));
611
612         for (int n = 0; GuiDocument::fontfamilies_gui[n][0]; ++n)
613                 fontModule->fontsDefaultCO->addItem(
614                         qt_(GuiDocument::fontfamilies_gui[n]));
615
616
617         pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
618         // page layout
619         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
620                 this, SLOT(setCustomPapersize(int)));
621         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
622                 this, SLOT(setCustomPapersize(int)));
623         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
624                 this, SLOT(portraitChanged()));
625         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
626                 this, SLOT(change_adaptor()));
627         connect(pageLayoutModule->paperheightLE, SIGNAL(textChanged(const QString &)),
628                 this, SLOT(change_adaptor()));
629         connect(pageLayoutModule->paperwidthLE, SIGNAL(textChanged(const QString &)),
630                 this, SLOT(change_adaptor()));
631         connect(pageLayoutModule->paperwidthUnitCO, SIGNAL(activated(int)),
632                 this, SLOT(change_adaptor()));
633         connect(pageLayoutModule->paperheightUnitCO, SIGNAL(activated(int)),
634                 this, SLOT(change_adaptor()));
635         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
636                 this, SLOT(change_adaptor()));
637         connect(pageLayoutModule->landscapeRB, SIGNAL(clicked()),
638                 this, SLOT(change_adaptor()));
639         connect(pageLayoutModule->facingPagesCB, SIGNAL(clicked()),
640                 this, SLOT(change_adaptor()));
641         connect(pageLayoutModule->pagestyleCO, SIGNAL(activated(int)),
642                 this, SLOT(change_adaptor()));
643
644         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
645         pageLayoutModule->pagestyleCO->addItem(qt_("empty"));
646         pageLayoutModule->pagestyleCO->addItem(qt_("plain"));
647         pageLayoutModule->pagestyleCO->addItem(qt_("headings"));
648         pageLayoutModule->pagestyleCO->addItem(qt_("fancy"));
649         bc().addCheckedLineEdit(pageLayoutModule->paperheightLE,
650                 pageLayoutModule->paperheightL);
651         bc().addCheckedLineEdit(pageLayoutModule->paperwidthLE,
652                 pageLayoutModule->paperwidthL);
653
654         // paper
655         QComboBox * cb = pageLayoutModule->papersizeCO;
656         cb->addItem(qt_("Default"));
657         cb->addItem(qt_("Custom"));
658         cb->addItem(qt_("US letter"));
659         cb->addItem(qt_("US legal"));
660         cb->addItem(qt_("US executive"));
661         cb->addItem(qt_("A3"));
662         cb->addItem(qt_("A4"));
663         cb->addItem(qt_("A5"));
664         cb->addItem(qt_("B3"));
665         cb->addItem(qt_("B4"));
666         cb->addItem(qt_("B5"));
667         // remove the %-items from the unit choice
668         pageLayoutModule->paperwidthUnitCO->noPercents();
669         pageLayoutModule->paperheightUnitCO->noPercents();
670         pageLayoutModule->paperheightLE->setValidator(unsignedLengthValidator(
671                 pageLayoutModule->paperheightLE));
672         pageLayoutModule->paperwidthLE->setValidator(unsignedLengthValidator(
673                 pageLayoutModule->paperwidthLE));
674
675
676         marginsModule = new UiWidget<Ui::MarginsUi>;
677         // margins
678         connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
679                 this, SLOT(setCustomMargins(bool)));
680         connect(marginsModule->marginCB, SIGNAL(clicked()),
681                 this, SLOT(change_adaptor()));
682         connect(marginsModule->topLE, SIGNAL(textChanged(const QString &)),
683                 this, SLOT(change_adaptor()));
684         connect(marginsModule->topUnit, SIGNAL(activated(int)),
685                 this, SLOT(change_adaptor()));
686         connect(marginsModule->bottomLE, SIGNAL(textChanged(const QString &)),
687                 this, SLOT(change_adaptor()));
688         connect(marginsModule->bottomUnit, SIGNAL(activated(int)),
689                 this, SLOT(change_adaptor()));
690         connect(marginsModule->innerLE, SIGNAL(textChanged(const QString &)),
691                 this, SLOT(change_adaptor()));
692         connect(marginsModule->innerUnit, SIGNAL(activated(int)),
693                 this, SLOT(change_adaptor()));
694         connect(marginsModule->outerLE, SIGNAL(textChanged(const QString &)),
695                 this, SLOT(change_adaptor()));
696         connect(marginsModule->outerUnit, SIGNAL(activated(int)),
697                 this, SLOT(change_adaptor()));
698         connect(marginsModule->headheightLE, SIGNAL(textChanged(const QString &)),
699                 this, SLOT(change_adaptor()));
700         connect(marginsModule->headheightUnit, SIGNAL(activated(int)),
701                 this, SLOT(change_adaptor()));
702         connect(marginsModule->headsepLE, SIGNAL(textChanged(const QString &)),
703                 this, SLOT(change_adaptor()));
704         connect(marginsModule->headsepUnit, SIGNAL(activated(int)),
705                 this, SLOT(change_adaptor()));
706         connect(marginsModule->footskipLE, SIGNAL(textChanged(const QString&)),
707                 this, SLOT(change_adaptor()));
708         connect(marginsModule->footskipUnit, SIGNAL(activated(int)),
709                 this, SLOT(change_adaptor()));
710         marginsModule->topLE->setValidator(unsignedLengthValidator(
711                 marginsModule->topLE));
712         marginsModule->bottomLE->setValidator(unsignedLengthValidator(
713                 marginsModule->bottomLE));
714         marginsModule->innerLE->setValidator(unsignedLengthValidator(
715                 marginsModule->innerLE));
716         marginsModule->outerLE->setValidator(unsignedLengthValidator(
717                 marginsModule->outerLE));
718         marginsModule->headsepLE->setValidator(unsignedLengthValidator(
719                 marginsModule->headsepLE));
720         marginsModule->headheightLE->setValidator(unsignedLengthValidator(
721                 marginsModule->headheightLE));
722         marginsModule->footskipLE->setValidator(unsignedLengthValidator(
723                 marginsModule->footskipLE));
724
725         bc().addCheckedLineEdit(marginsModule->topLE,
726                 marginsModule->topL);
727         bc().addCheckedLineEdit(marginsModule->bottomLE,
728                 marginsModule->bottomL);
729         bc().addCheckedLineEdit(marginsModule->innerLE,
730                 marginsModule->innerL);
731         bc().addCheckedLineEdit(marginsModule->outerLE,
732                 marginsModule->outerL);
733         bc().addCheckedLineEdit(marginsModule->headsepLE,
734                 marginsModule->headsepL);
735         bc().addCheckedLineEdit(marginsModule->headheightLE,
736                 marginsModule->headheightL);
737         bc().addCheckedLineEdit(marginsModule->footskipLE,
738                 marginsModule->footskipL);
739
740
741         langModule = new UiWidget<Ui::LanguageUi>;
742         // language & quote
743         connect(langModule->languageCO, SIGNAL(activated(int)),
744                 this, SLOT(change_adaptor()));
745         connect(langModule->defaultencodingRB, SIGNAL(clicked()),
746                 this, SLOT(change_adaptor()));
747         connect(langModule->otherencodingRB, SIGNAL(clicked()),
748                 this, SLOT(change_adaptor()));
749         connect(langModule->encodingCO, SIGNAL(activated(int)),
750                 this, SLOT(change_adaptor()));
751         connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
752                 this, SLOT(change_adaptor()));
753         // language & quotes
754         vector<LanguagePair> const langs = getLanguageData(false);
755         vector<LanguagePair>::const_iterator lit  = langs.begin();
756         vector<LanguagePair>::const_iterator lend = langs.end();
757         for (; lit != lend; ++lit) {
758                 langModule->languageCO->addItem(toqstr(lit->first));
759         }
760
761         // Always put the default encoding in the first position.
762         // It is special because the displayed text is translated.
763         langModule->encodingCO->addItem(qt_("LaTeX default"));
764         Encodings::const_iterator it = encodings.begin();
765         Encodings::const_iterator const end = encodings.end();
766         for (; it != end; ++it)
767                 langModule->encodingCO->addItem(toqstr(it->latexName()));
768
769         langModule->quoteStyleCO->addItem(qt_("``text''"));
770         langModule->quoteStyleCO->addItem(qt_("''text''"));
771         langModule->quoteStyleCO->addItem(qt_(",,text``"));
772         langModule->quoteStyleCO->addItem(qt_(",,text''"));
773         langModule->quoteStyleCO->addItem(qt_("<<text>>"));
774         langModule->quoteStyleCO->addItem(qt_(">>text<<"));
775
776
777
778         numberingModule = new UiWidget<Ui::NumberingUi>;
779         // numbering
780         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
781                 this, SLOT(change_adaptor()));
782         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
783                 this, SLOT(change_adaptor()));
784         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
785                 this, SLOT(updateNumbering()));
786         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
787                 this, SLOT(updateNumbering()));
788         numberingModule->tocTW->setColumnCount(3);
789         numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
790         numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
791         numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
792
793
794         biblioModule = new UiWidget<Ui::BiblioUi>;
795         connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
796                 biblioModule->citationStyleL, SLOT(setEnabled(bool)));
797         connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
798                 biblioModule->citeStyleCO, SLOT(setEnabled(bool)));
799         // biblio
800         connect(biblioModule->citeDefaultRB, SIGNAL(clicked()),
801                 this, SLOT(change_adaptor()));
802         connect(biblioModule->citeNatbibRB, SIGNAL(clicked()),
803                 this, SLOT(change_adaptor()));
804         connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
805                 this, SLOT(change_adaptor()));
806         connect(biblioModule->citeJurabibRB, SIGNAL(clicked()),
807                 this, SLOT(change_adaptor()));
808         connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
809                 this, SLOT(change_adaptor()));
810         // biblio
811         biblioModule->citeStyleCO->addItem(qt_("Author-year"));
812         biblioModule->citeStyleCO->addItem(qt_("Numerical"));
813         biblioModule->citeStyleCO->setCurrentIndex(0);
814
815
816         mathsModule = new UiWidget<Ui::MathsUi>;
817         connect(mathsModule->amsautoCB, SIGNAL(toggled(bool)),
818                 mathsModule->amsCB, SLOT(setDisabled(bool)));
819         connect(mathsModule->esintautoCB, SIGNAL(toggled(bool)),
820                 mathsModule->esintCB, SLOT(setDisabled(bool)));
821         // maths
822         connect(mathsModule->amsCB, SIGNAL(clicked()),
823                 this, SLOT(change_adaptor()));
824         connect(mathsModule->amsautoCB, SIGNAL(clicked()),
825                 this, SLOT(change_adaptor()));
826         connect(mathsModule->esintCB, SIGNAL(clicked()),
827                 this, SLOT(change_adaptor()));
828         connect(mathsModule->esintautoCB, SIGNAL(clicked()),
829                 this, SLOT(change_adaptor()));
830
831         latexModule = new UiWidget<Ui::LaTeXUi>;
832         // latex class
833         connect(latexModule->classCO, SIGNAL(activated(int)),
834                 this, SLOT(change_adaptor()));
835         connect(latexModule->optionsLE, SIGNAL(textChanged(const QString &)),
836                 this, SLOT(change_adaptor()));
837         connect(latexModule->psdriverCO, SIGNAL(activated(int)),
838                 this, SLOT(change_adaptor()));
839         connect(latexModule->classCO, SIGNAL(activated(int)),
840                 this, SLOT(classChanged()));
841         
842         selectionManager = 
843                 new ModuleSelMan(latexModule->availableLV, latexModule->selectedLV, 
844                         latexModule->addPB, latexModule->deletePB, 
845                         latexModule->upPB, latexModule->downPB, 
846                         availableModel(), selectedModel());
847         connect(selectionManager, SIGNAL(updateHook()),
848                 this, SLOT(updateModuleInfo()));
849         connect(selectionManager, SIGNAL(updateHook()),
850                 this, SLOT(change_adaptor()));
851         
852         // postscript drivers
853         for (int n = 0; tex_graphics[n][0]; ++n) {
854                 QString enc = qt_(tex_graphics_gui[n]);
855                 latexModule->psdriverCO->addItem(enc);
856         }
857         // latex classes
858         //FIXME This seems too involved with the kernel. Some of this
859         //should be moved to the kernel---which should perhaps just
860         //give us a list of entries or something of the sort.
861         for (TextClassList::const_iterator cit = textclasslist.begin();
862              cit != textclasslist.end(); ++cit) {
863                 if (cit->isTeXClassAvailable()) {
864                         latexModule->classCO->addItem(toqstr(cit->description()));
865                 } else {
866                         docstring item =
867                                 bformat(_("Unavailable: %1$s"), from_utf8(cit->description()));
868                         latexModule->classCO->addItem(toqstr(item));
869                 }
870         }
871
872         // branches
873         branchesModule = new GuiBranches;
874         connect(branchesModule, SIGNAL(changed()),
875                 this, SLOT(change_adaptor()));
876
877         // preamble
878         preambleModule = new PreambleModule;
879         connect(preambleModule, SIGNAL(changed()),
880                 this, SLOT(change_adaptor()));
881
882         // bullets
883         bulletsModule = new BulletsModule;
884         connect(bulletsModule, SIGNAL(changed()),
885                 this, SLOT(change_adaptor()));
886
887         // embedded files
888         embeddedFilesModule = new UiWidget<Ui::EmbeddedFilesUi>;
889         connect(embeddedFilesModule->bundleCB, SIGNAL(toggled(bool)),
890                 this, SLOT(change_adaptor()));
891         connect(embeddedFilesModule->addPB, SIGNAL(clicked()),
892                 this, SLOT(change_adaptor()));
893         connect(embeddedFilesModule->removePB, SIGNAL(clicked()),
894                 this, SLOT(change_adaptor()));
895
896         // PDF support
897         pdfSupportModule = new UiWidget<Ui::PDFSupportUi>;
898
899         connect(pdfSupportModule->use_hyperrefGB, SIGNAL(toggled(bool)),
900                 this, SLOT(change_adaptor()));
901         connect(pdfSupportModule->titleLE, SIGNAL(textChanged(const QString &)),
902                 this, SLOT(change_adaptor()));
903         connect(pdfSupportModule->authorLE, SIGNAL(textChanged(const QString &)),
904                 this, SLOT(change_adaptor()));
905         connect(pdfSupportModule->subjectLE, SIGNAL(textChanged(const QString &)),
906                 this, SLOT(change_adaptor()));
907         connect(pdfSupportModule->keywordsLE, SIGNAL(textChanged(const QString &)),
908                 this, SLOT(change_adaptor()));
909         connect(pdfSupportModule->bookmarksGB, SIGNAL(toggled(bool)),
910                 this, SLOT(change_adaptor()));
911         connect(pdfSupportModule->bookmarksnumberedCB, SIGNAL(toggled(bool)),
912                 this, SLOT(change_adaptor()));
913         connect(pdfSupportModule->bookmarksopenGB, SIGNAL(toggled(bool)),
914                 this, SLOT(change_adaptor()));
915         connect(pdfSupportModule->bookmarksopenlevelSB, SIGNAL(valueChanged(int)),
916                 this, SLOT(change_adaptor()));
917         connect(pdfSupportModule->breaklinksCB, SIGNAL(toggled(bool)),
918                 this, SLOT(change_adaptor()));
919         connect(pdfSupportModule->pdfborderCB, SIGNAL(toggled(bool)),
920                 this, SLOT(change_adaptor()));
921         connect(pdfSupportModule->colorlinksCB, SIGNAL(toggled(bool)),
922                 this, SLOT(change_adaptor()));
923         connect(pdfSupportModule->backrefCB, SIGNAL(toggled(bool)),
924                 this, SLOT(change_adaptor()));
925         connect(pdfSupportModule->pdfusetitleCB, SIGNAL(toggled(bool)),
926                 this, SLOT(change_adaptor()));
927         connect(pdfSupportModule->pagebackrefCB, SIGNAL(toggled(bool)),
928                 this, SLOT(change_adaptor()));
929         connect(pdfSupportModule->fullscreenCB, SIGNAL(toggled(bool)),
930                 this, SLOT(change_adaptor()));
931         connect(pdfSupportModule->optionsLE, SIGNAL(textChanged(const QString &)),
932                 this, SLOT(change_adaptor()));
933
934         // float
935         floatModule = new FloatPlacement;
936         connect(floatModule, SIGNAL(changed()),
937                 this, SLOT(change_adaptor()));
938
939         docPS->addPanel(latexModule, _("Document Class"));
940         docPS->addPanel(fontModule, _("Fonts"));
941         docPS->addPanel(textLayoutModule, _("Text Layout"));
942         docPS->addPanel(pageLayoutModule, _("Page Layout"));
943         docPS->addPanel(marginsModule, _("Page Margins"));
944         docPS->addPanel(langModule, _("Language"));
945         docPS->addPanel(numberingModule, _("Numbering & TOC"));
946         docPS->addPanel(biblioModule, _("Bibliography"));
947         docPS->addPanel(pdfSupportModule, _("PDF Properties"));
948         docPS->addPanel(mathsModule, _("Math Options"));
949         docPS->addPanel(floatModule, _("Float Placement"));
950         docPS->addPanel(bulletsModule, _("Bullets"));
951         docPS->addPanel(branchesModule, _("Branches"));
952         docPS->addPanel(embeddedFilesModule, _("Embedded Files"));
953         docPS->addPanel(preambleModule, _("LaTeX Preamble"));
954         docPS->setCurrentPanel(_("Document Class"));
955 // FIXME: hack to work around resizing bug in Qt >= 4.2
956 // bug verified with Qt 4.2.{0-3} (JSpitzm)
957 #if QT_VERSION >= 0x040200
958         docPS->updateGeometry();
959 #endif
960 }
961
962
963 void GuiDocument::showPreamble()
964 {
965         docPS->setCurrentPanel(_("LaTeX Preamble"));
966 }
967
968
969 void GuiDocument::saveDefaultClicked()
970 {
971         saveDocDefault();
972 }
973
974
975 void GuiDocument::useDefaultsClicked()
976 {
977         useClassDefaults();
978 }
979
980
981 void GuiDocument::change_adaptor()
982 {
983         changed();
984 }
985
986
987 docstring GuiDocument::validate_listings_params()
988 {
989         // use a cache here to avoid repeated validation
990         // of the same parameters
991         static string param_cache = string();
992         static docstring msg_cache = docstring();
993         
994         if (textLayoutModule->bypassCB->isChecked())
995                 return docstring();
996
997         string params = fromqstr(textLayoutModule->listingsED->toPlainText());
998         if (params != param_cache) {
999                 param_cache = params;
1000                 msg_cache = InsetListingsParams(params).validate();
1001         }
1002         return msg_cache;
1003 }
1004
1005
1006 void GuiDocument::set_listings_msg()
1007 {
1008         static bool isOK = true;
1009         docstring msg = validate_listings_params();
1010         if (msg.empty()) {
1011                 if (isOK)
1012                         return;
1013                 isOK = true;
1014                 // listingsTB->setTextColor("black");
1015                 textLayoutModule->listingsTB->setPlainText(
1016                         qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
1017         } else {
1018                 isOK = false;
1019                 // listingsTB->setTextColor("red");
1020                 textLayoutModule->listingsTB->setPlainText(toqstr(msg));
1021         }
1022 }
1023
1024
1025 void GuiDocument::closeEvent(QCloseEvent * e)
1026 {
1027         slotClose();
1028         e->accept();
1029 }
1030
1031
1032 void GuiDocument::setLSpacing(int item)
1033 {
1034         textLayoutModule->lspacingLE->setEnabled(item == 3);
1035 }
1036
1037
1038 void GuiDocument::setSkip(int item)
1039 {
1040         bool const enable = (item == 3);
1041         textLayoutModule->skipLE->setEnabled(enable);
1042         textLayoutModule->skipLengthCO->setEnabled(enable);
1043 }
1044
1045
1046 void GuiDocument::enableSkip(bool skip)
1047 {
1048         textLayoutModule->skipCO->setEnabled(skip);
1049         textLayoutModule->skipLE->setEnabled(skip);
1050         textLayoutModule->skipLengthCO->setEnabled(skip);
1051         if (skip)
1052                 setSkip(textLayoutModule->skipCO->currentIndex());
1053 }
1054
1055 void GuiDocument::portraitChanged()
1056 {
1057         setMargins(pageLayoutModule->papersizeCO->currentIndex());
1058 }
1059
1060 void GuiDocument::setMargins(bool custom)
1061 {
1062         marginsModule->marginCB->setChecked(custom);
1063         setCustomMargins(custom);
1064 }
1065
1066
1067 void GuiDocument::setCustomPapersize(int papersize)
1068 {
1069         bool const custom = (papersize == 1);
1070
1071         pageLayoutModule->paperwidthL->setEnabled(custom);
1072         pageLayoutModule->paperwidthLE->setEnabled(custom);
1073         pageLayoutModule->paperwidthUnitCO->setEnabled(custom);
1074         pageLayoutModule->paperheightL->setEnabled(custom);
1075         pageLayoutModule->paperheightLE->setEnabled(custom);
1076         pageLayoutModule->paperheightLE->setFocus();
1077         pageLayoutModule->paperheightUnitCO->setEnabled(custom);
1078 }
1079
1080
1081 void GuiDocument::setCustomMargins(bool custom)
1082 {
1083         marginsModule->topL->setEnabled(!custom);
1084         marginsModule->topLE->setEnabled(!custom);
1085         marginsModule->topUnit->setEnabled(!custom);
1086
1087         marginsModule->bottomL->setEnabled(!custom);
1088         marginsModule->bottomLE->setEnabled(!custom);
1089         marginsModule->bottomUnit->setEnabled(!custom);
1090
1091         marginsModule->innerL->setEnabled(!custom);
1092         marginsModule->innerLE->setEnabled(!custom);
1093         marginsModule->innerUnit->setEnabled(!custom);
1094
1095         marginsModule->outerL->setEnabled(!custom);
1096         marginsModule->outerLE->setEnabled(!custom);
1097         marginsModule->outerUnit->setEnabled(!custom);
1098
1099         marginsModule->headheightL->setEnabled(!custom);
1100         marginsModule->headheightLE->setEnabled(!custom);
1101         marginsModule->headheightUnit->setEnabled(!custom);
1102
1103         marginsModule->headsepL->setEnabled(!custom);
1104         marginsModule->headsepLE->setEnabled(!custom);
1105         marginsModule->headsepUnit->setEnabled(!custom);
1106
1107         marginsModule->footskipL->setEnabled(!custom);
1108         marginsModule->footskipLE->setEnabled(!custom);
1109         marginsModule->footskipUnit->setEnabled(!custom);
1110 }
1111
1112
1113 void GuiDocument::updateFontsize(string const & items, string const & sel)
1114 {
1115         fontModule->fontsizeCO->clear();
1116         fontModule->fontsizeCO->addItem(qt_("Default"));
1117
1118         for (int n = 0; !token(items,'|',n).empty(); ++n)
1119                 fontModule->fontsizeCO->
1120                         addItem(toqstr(token(items,'|',n)));
1121
1122         for (int n = 0; n < fontModule->fontsizeCO->count(); ++n) {
1123                 if (fromqstr(fontModule->fontsizeCO->itemText(n)) == sel) {
1124                         fontModule->fontsizeCO->setCurrentIndex(n);
1125                         break;
1126                 }
1127         }
1128 }
1129
1130
1131 void GuiDocument::romanChanged(int item)
1132 {
1133         string const font = tex_fonts_roman[item];
1134         fontModule->fontScCB->setEnabled(providesSC(font));
1135         fontModule->fontOsfCB->setEnabled(providesOSF(font));
1136 }
1137
1138
1139 void GuiDocument::sansChanged(int item)
1140 {
1141         string const font = tex_fonts_sans[item];
1142         bool scaleable = providesScale(font);
1143         fontModule->scaleSansSB->setEnabled(scaleable);
1144         fontModule->scaleSansLA->setEnabled(scaleable);
1145 }
1146
1147
1148 void GuiDocument::ttChanged(int item)
1149 {
1150         string const font = tex_fonts_monospaced[item];
1151         bool scaleable = providesScale(font);
1152         fontModule->scaleTypewriterSB->setEnabled(scaleable);
1153         fontModule->scaleTypewriterLA->setEnabled(scaleable);
1154 }
1155
1156
1157 void GuiDocument::updatePagestyle(string const & items, string const & sel)
1158 {
1159         pagestyles.clear();
1160         pageLayoutModule->pagestyleCO->clear();
1161         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
1162
1163         for (int n = 0; !token(items,'|',n).empty(); ++n) {
1164                 string style = token(items, '|', n);
1165                 docstring style_gui = _(style);
1166                 pagestyles.push_back(pair<string, docstring>(style, style_gui));
1167                 pageLayoutModule->pagestyleCO->addItem(toqstr(style_gui));
1168         }
1169
1170         if (sel == "default") {
1171                 pageLayoutModule->pagestyleCO->setCurrentIndex(0);
1172                 return;
1173         }
1174
1175         int nn = 0;
1176
1177         for (size_t i = 0; i < pagestyles.size(); ++i)
1178                 if (pagestyles[i].first == sel)
1179                         nn = pageLayoutModule->pagestyleCO->findText(
1180                                         toqstr(pagestyles[i].second));
1181
1182         if (nn > 0)
1183                 pageLayoutModule->pagestyleCO->setCurrentIndex(nn);
1184 }
1185
1186
1187 void GuiDocument::classChanged()
1188 {
1189         textclass_type const tc = latexModule->classCO->currentIndex();
1190         bp_.setBaseClass(tc);
1191         if (lyxrc.auto_reset_options)
1192                 bp_.useClassDefaults();
1193         updateContents();
1194 }
1195
1196
1197 namespace {
1198         //This is an insanely complicated attempt to make this sort of thing
1199         //work with RTL languages.
1200         docstring formatStrVec(vector<string> const & v, docstring const & s) 
1201         {
1202                 //this mess formats the list as "v[0], v[1], ..., [s] v[n]"
1203                 int const vSize = v.size();
1204                 if (v.size() == 0)
1205                         return docstring();
1206                 else if (v.size() == 1) 
1207                         return from_ascii(v[0]);
1208                 else if (v.size() == 2) {
1209                         docstring retval = _("%1$s and %2$s");
1210                         retval = subst(retval, _("and"), s);
1211                         return bformat(retval, from_ascii(v[0]), from_ascii(v[1]));
1212                 }
1213                 //The idea here is to format all but the last two items...
1214                 docstring t2 = _("%1$s, %2$s");
1215                 docstring retval = from_ascii(v[0]);
1216                 for (int i = 1; i < vSize - 2; ++i)
1217                         retval = bformat(t2, retval, from_ascii(v[i])); 
1218                 //...and then to  plug them, and the last two, into this schema
1219                 docstring t = _("%1$s, %2$s, and %3$s");
1220                 t = subst(t, _("and"), s);
1221                 return bformat(t, retval, from_ascii(v[vSize - 2]), from_ascii(v[vSize - 1]));
1222         }
1223 }
1224
1225
1226 void GuiDocument::updateModuleInfo()
1227 {
1228         selectionManager->update();
1229         //Module description
1230         QListView const * const lv = selectionManager->selectedFocused() ?
1231                                      latexModule->selectedLV :
1232                                      latexModule->availableLV;
1233         if (lv->selectionModel()->selectedIndexes().isEmpty())
1234                 latexModule->infoML->document()->clear();
1235         else {
1236                 QModelIndex const & idx = lv->selectionModel()->currentIndex();
1237                 string const modName = fromqstr(idx.data().toString());
1238                 docstring desc = getModuleDescription(modName);
1239
1240                 vector<string> pkgList = getPackageList(modName);
1241                 docstring pkgdesc = formatStrVec(pkgList, _("and"));
1242                 if (!pkgdesc.empty()) {
1243                         if (!desc.empty())
1244                                 desc += "\n";
1245                         desc += bformat(_("Package(s) required: %1$s."), pkgdesc);
1246                 }
1247
1248                 pkgList = getRequiredList(modName);
1249                 pkgdesc = formatStrVec(pkgList, _("or"));
1250                 if (!pkgdesc.empty()) {
1251                         if (!desc.empty())
1252                                 desc += "\n";
1253                         desc += bformat(_("Module required: %1$s."), pkgdesc);
1254                 }
1255
1256                 pkgList = getExcludedList(modName);
1257                 pkgdesc = formatStrVec(pkgList, _( "and"));
1258                 if (!pkgdesc.empty()) {
1259                         if (!desc.empty())
1260                                 desc += "\n";
1261                         desc += bformat(_("Modules excluded: %1$s."), pkgdesc);
1262                 }
1263
1264                 if (!isModuleAvailable(modName)) {
1265                         if (!desc.empty())
1266                                 desc += "\n";
1267                         desc += _("WARNING: Some packages are unavailable!");
1268                 }
1269                 latexModule->infoML->document()->setPlainText(toqstr(desc));
1270         }
1271 }
1272
1273
1274 void GuiDocument::updateEmbeddedFileList()
1275 {
1276         embeddedFilesModule->filesLW->clear();
1277         // add current embedded files
1278         EmbeddedFileList & files = buffer().embeddedFiles();
1279         files.update(buffer());
1280         EmbeddedFileList::iterator fit = files.begin();
1281         EmbeddedFileList::iterator fit_end = files.end();
1282         for (; fit != fit_end; ++fit) {
1283                 QString label = toqstr(fit->relFilename(buffer().filePath()));
1284                 if (fit->refCount() > 1)
1285                         label += " (" + QString::number(fit->refCount()) + ")";
1286                 QListWidgetItem * item = new QListWidgetItem(label);
1287                 item->setFlags(item->flags() | Qt::ItemIsSelectable
1288                         | Qt::ItemIsUserCheckable);
1289                 if(fit->embedded())
1290                         item->setCheckState(Qt::Checked);
1291                 else
1292                         item->setCheckState(Qt::Unchecked);
1293                 // index of the currently used ParConstIterator
1294                 embeddedFilesModule->filesLW->addItem(item);
1295         }
1296 }
1297
1298
1299 void GuiDocument::updateNumbering()
1300 {
1301         TextClass const & tclass = bp_.getTextClass();
1302
1303         numberingModule->tocTW->setUpdatesEnabled(false);
1304         numberingModule->tocTW->clear();
1305
1306         int const depth = numberingModule->depthSL->value();
1307         int const toc = numberingModule->tocSL->value();
1308         QString const no = qt_("No");
1309         QString const yes = qt_("Yes");
1310         TextClass::const_iterator end = tclass.end();
1311         TextClass::const_iterator cit = tclass.begin();
1312         QTreeWidgetItem * item = 0;
1313         for ( ; cit != end ; ++cit) {
1314                 int const toclevel = (*cit)->toclevel;
1315                 if (toclevel != Layout::NOT_IN_TOC
1316                     && (*cit)->labeltype == LABEL_COUNTER) {
1317                         item = new QTreeWidgetItem(numberingModule->tocTW);
1318                         item->setText(0, toqstr(translateIfPossible((*cit)->name())));
1319                         item->setText(1, (toclevel <= depth) ? yes : no);
1320                         item->setText(2, (toclevel <= toc) ? yes : no);
1321                 }
1322         }
1323
1324         numberingModule->tocTW->setUpdatesEnabled(true);
1325         numberingModule->tocTW->update();
1326 }
1327
1328
1329 void GuiDocument::apply(BufferParams & params)
1330 {
1331         // preamble
1332         preambleModule->apply(params);
1333
1334         // biblio
1335         params.setCiteEngine(biblio::ENGINE_BASIC);
1336
1337         if (biblioModule->citeNatbibRB->isChecked()) {
1338                 bool const use_numerical_citations =
1339                         biblioModule->citeStyleCO->currentIndex();
1340                 if (use_numerical_citations)
1341                         params.setCiteEngine(biblio::ENGINE_NATBIB_NUMERICAL);
1342                 else
1343                         params.setCiteEngine(biblio::ENGINE_NATBIB_AUTHORYEAR);
1344
1345         } else if (biblioModule->citeJurabibRB->isChecked())
1346                 params.setCiteEngine(biblio::ENGINE_JURABIB);
1347
1348         params.use_bibtopic =
1349                 biblioModule->bibtopicCB->isChecked();
1350
1351         // language & quotes
1352         if (langModule->defaultencodingRB->isChecked()) {
1353                 params.inputenc = "auto";
1354         } else {
1355                 int i = langModule->encodingCO->currentIndex();
1356                 if (i == 0)
1357                         params.inputenc = "default";
1358                 else
1359                         params.inputenc =
1360                                 fromqstr(langModule->encodingCO->currentText());
1361         }
1362
1363         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
1364         switch (langModule->quoteStyleCO->currentIndex()) {
1365         case 0:
1366                 lga = InsetQuotes::EnglishQ;
1367                 break;
1368         case 1:
1369                 lga = InsetQuotes::SwedishQ;
1370                 break;
1371         case 2:
1372                 lga = InsetQuotes::GermanQ;
1373                 break;
1374         case 3:
1375                 lga = InsetQuotes::PolishQ;
1376                 break;
1377         case 4:
1378                 lga = InsetQuotes::FrenchQ;
1379                 break;
1380         case 5:
1381                 lga = InsetQuotes::DanishQ;
1382                 break;
1383         }
1384         params.quotes_language = lga;
1385
1386         int const pos = langModule->languageCO->currentIndex();
1387         params.language = lyx::languages.getLanguage(lang_[pos]);
1388
1389         // numbering
1390         if (params.getTextClass().hasTocLevels()) {
1391                 params.tocdepth = numberingModule->tocSL->value();
1392                 params.secnumdepth = numberingModule->depthSL->value();
1393         }
1394
1395         // bullets
1396         params.user_defined_bullet(0) = bulletsModule->getBullet(0);
1397         params.user_defined_bullet(1) = bulletsModule->getBullet(1);
1398         params.user_defined_bullet(2) = bulletsModule->getBullet(2);
1399         params.user_defined_bullet(3) = bulletsModule->getBullet(3);
1400
1401         // packages
1402         params.graphicsDriver =
1403                 tex_graphics[latexModule->psdriverCO->currentIndex()];
1404         
1405         // text layout
1406         params.setBaseClass(latexModule->classCO->currentIndex());
1407
1408         // Modules
1409         params.clearLayoutModules();
1410         QStringList const selMods = selectedModel()->stringList();
1411         for (int i = 0; i != selMods.size(); ++i)
1412                 params.addLayoutModule(lyx::fromqstr(selMods[i]));
1413
1414         if (mathsModule->amsautoCB->isChecked()) {
1415                 params.use_amsmath = BufferParams::package_auto;
1416         } else {
1417                 if (mathsModule->amsCB->isChecked())
1418                         params.use_amsmath = BufferParams::package_on;
1419                 else
1420                         params.use_amsmath = BufferParams::package_off;
1421         }
1422
1423         if (mathsModule->esintautoCB->isChecked())
1424                 params.use_esint = BufferParams::package_auto;
1425         else {
1426                 if (mathsModule->esintCB->isChecked())
1427                         params.use_esint = BufferParams::package_on;
1428                 else
1429                         params.use_esint = BufferParams::package_off;
1430         }
1431
1432         if (pageLayoutModule->pagestyleCO->currentIndex() == 0)
1433                 params.pagestyle = "default";
1434         else {
1435                 docstring style_gui =
1436                         qstring_to_ucs4(pageLayoutModule->pagestyleCO->currentText());
1437                 for (size_t i = 0; i < pagestyles.size(); ++i)
1438                         if (pagestyles[i].second == style_gui)
1439                                 params.pagestyle = pagestyles[i].first;
1440         }
1441
1442         switch (textLayoutModule->lspacingCO->currentIndex()) {
1443         case 0:
1444                 params.spacing().set(Spacing::Single);
1445                 break;
1446         case 1:
1447                 params.spacing().set(Spacing::Onehalf);
1448                 break;
1449         case 2:
1450                 params.spacing().set(Spacing::Double);
1451                 break;
1452         case 3:
1453                 params.spacing().set(Spacing::Other,
1454                         fromqstr(textLayoutModule->lspacingLE->text()));
1455                 break;
1456         }
1457
1458         if (textLayoutModule->twoColumnCB->isChecked())
1459                 params.columns = 2;
1460         else
1461                 params.columns = 1;
1462
1463         // text should have passed validation
1464         params.listings_params =
1465                 InsetListingsParams(fromqstr(textLayoutModule->listingsED->toPlainText())).params();
1466
1467         if (textLayoutModule->indentRB->isChecked())
1468                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
1469         else
1470                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
1471
1472         switch (textLayoutModule->skipCO->currentIndex()) {
1473         case 0:
1474                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
1475                 break;
1476         case 1:
1477                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
1478                 break;
1479         case 2:
1480                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
1481                 break;
1482         case 3:
1483         {
1484                 VSpace vs = VSpace(
1485                         widgetsToLength(textLayoutModule->skipLE,
1486                                 textLayoutModule->skipLengthCO)
1487                         );
1488                 params.setDefSkip(vs);
1489                 break;
1490         }
1491         default:
1492                 // DocumentDefskipCB assures that this never happens
1493                 // so Assert then !!!  - jbl
1494                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
1495                 break;
1496         }
1497
1498         params.options =
1499                 fromqstr(latexModule->optionsLE->text());
1500
1501         params.float_placement = floatModule->get();
1502
1503         // fonts
1504         params.fontsRoman =
1505                 tex_fonts_roman[fontModule->fontsRomanCO->currentIndex()];
1506
1507         params.fontsSans =
1508                 tex_fonts_sans[fontModule->fontsSansCO->currentIndex()];
1509
1510         params.fontsTypewriter =
1511                 tex_fonts_monospaced[fontModule->fontsTypewriterCO->currentIndex()];
1512
1513         params.fontsSansScale = fontModule->scaleSansSB->value();
1514
1515         params.fontsTypewriterScale = fontModule->scaleTypewriterSB->value();
1516
1517         params.fontsSC = fontModule->fontScCB->isChecked();
1518
1519         params.fontsOSF = fontModule->fontOsfCB->isChecked();
1520
1521         params.fontsDefaultFamily = GuiDocument::fontfamilies[
1522                 fontModule->fontsDefaultCO->currentIndex()];
1523
1524         if (fontModule->fontsizeCO->currentIndex() == 0)
1525                 params.fontsize = "default";
1526         else
1527                 params.fontsize =
1528                         fromqstr(fontModule->fontsizeCO->currentText());
1529
1530         // paper
1531         params.papersize = PAPER_SIZE(
1532                 pageLayoutModule->papersizeCO->currentIndex());
1533
1534         // custom, A3, B3 and B4 paper sizes need geometry
1535         int psize = pageLayoutModule->papersizeCO->currentIndex();
1536         bool geom_papersize = (psize == 1 || psize == 5 || psize == 8 || psize == 9);
1537
1538         params.paperwidth = widgetsToLength(pageLayoutModule->paperwidthLE,
1539                 pageLayoutModule->paperwidthUnitCO);
1540
1541         params.paperheight = widgetsToLength(pageLayoutModule->paperheightLE,
1542                 pageLayoutModule->paperheightUnitCO);
1543
1544         if (pageLayoutModule->facingPagesCB->isChecked())
1545                 params.sides = TwoSides;
1546         else
1547                 params.sides = OneSide;
1548
1549         if (pageLayoutModule->landscapeRB->isChecked())
1550                 params.orientation = ORIENTATION_LANDSCAPE;
1551         else
1552                 params.orientation = ORIENTATION_PORTRAIT;
1553
1554         // margins
1555         params.use_geometry =
1556                 (!marginsModule->marginCB->isChecked()
1557                 || geom_papersize);
1558
1559         Ui::MarginsUi const * m(marginsModule);
1560
1561         params.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
1562         params.topmargin = widgetsToLength(m->topLE, m->topUnit);
1563         params.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
1564         params.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
1565         params.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
1566         params.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
1567         params.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
1568
1569         branchesModule->apply(params);
1570
1571         // PDF support
1572         PDFOptions & pdf = params.pdfoptions();
1573         pdf.use_hyperref = pdfSupportModule->use_hyperrefGB->isChecked();
1574         pdf.title = fromqstr(pdfSupportModule->titleLE->text());
1575         pdf.author = fromqstr(pdfSupportModule->authorLE->text());
1576         pdf.subject = fromqstr(pdfSupportModule->subjectLE->text());
1577         pdf.keywords = fromqstr(pdfSupportModule->keywordsLE->text());
1578
1579         pdf.bookmarks = pdfSupportModule->bookmarksGB->isChecked();
1580         pdf.bookmarksnumbered = pdfSupportModule->bookmarksnumberedCB->isChecked();
1581         pdf.bookmarksopen = pdfSupportModule->bookmarksopenGB->isChecked();
1582         pdf.bookmarksopenlevel = pdfSupportModule->bookmarksopenlevelSB->value();
1583
1584         pdf.breaklinks = pdfSupportModule->breaklinksCB->isChecked();
1585         pdf.pdfborder = pdfSupportModule->pdfborderCB->isChecked();
1586         pdf.pdfusetitle = pdfSupportModule->pdfusetitleCB->isChecked();
1587         pdf.colorlinks = pdfSupportModule->colorlinksCB->isChecked();
1588         pdf.backref = pdfSupportModule->backrefCB->isChecked();
1589         pdf.pagebackref = pdfSupportModule->pagebackrefCB->isChecked();
1590         if (pdfSupportModule->fullscreenCB->isChecked())
1591                 pdf.pagemode = pdf.pagemode_fullscreen;
1592         else
1593                 pdf.pagemode.clear();
1594         pdf.quoted_options = fromqstr(pdfSupportModule->optionsLE->text());
1595
1596         // Embedded files
1597         // FIXME
1598 }
1599
1600
1601 /** Return the position of val in the vector if found.
1602     If not found, return 0.
1603  */
1604 template<class A>
1605 static size_t findPos(vector<A> const & vec, A const & val)
1606 {
1607         typename vector<A>::const_iterator it =
1608                 find(vec.begin(), vec.end(), val);
1609         if (it == vec.end())
1610                 return 0;
1611         return distance(vec.begin(), it);
1612 }
1613
1614
1615 void GuiDocument::updateParams()
1616 {
1617         updateParams(bp_);
1618 }
1619
1620
1621 void GuiDocument::updateParams(BufferParams const & params)
1622 {
1623         // set the default unit
1624         Length::UNIT defaultUnit = Length::CM;
1625         switch (lyxrc.default_papersize) {
1626                 case PAPER_DEFAULT: break;
1627
1628                 case PAPER_USLETTER:
1629                 case PAPER_USLEGAL:
1630                 case PAPER_USEXECUTIVE:
1631                         defaultUnit = Length::IN;
1632                         break;
1633
1634                 case PAPER_A3:
1635                 case PAPER_A4:
1636                 case PAPER_A5:
1637                 case PAPER_B3:
1638                 case PAPER_B4:
1639                 case PAPER_B5:
1640                         defaultUnit = Length::CM;
1641                         break;
1642                 case PAPER_CUSTOM:
1643                         break;
1644         }
1645
1646         // preamble
1647         preambleModule->update(params, id());
1648
1649         // biblio
1650         biblioModule->citeDefaultRB->setChecked(
1651                 params.getEngine() == biblio::ENGINE_BASIC);
1652
1653         biblioModule->citeNatbibRB->setChecked(
1654                 params.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL ||
1655                 params.getEngine() == biblio::ENGINE_NATBIB_AUTHORYEAR);
1656
1657         biblioModule->citeStyleCO->setCurrentIndex(
1658                 params.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL);
1659
1660         biblioModule->citeJurabibRB->setChecked(
1661                 params.getEngine() == biblio::ENGINE_JURABIB);
1662
1663         biblioModule->bibtopicCB->setChecked(
1664                 params.use_bibtopic);
1665
1666         // language & quotes
1667         int const pos = int(findPos(lang_,
1668                                     params.language->lang()));
1669         langModule->languageCO->setCurrentIndex(pos);
1670
1671         langModule->quoteStyleCO->setCurrentIndex(
1672                 params.quotes_language);
1673
1674         bool default_enc = true;
1675         if (params.inputenc != "auto") {
1676                 default_enc = false;
1677                 if (params.inputenc == "default") {
1678                         langModule->encodingCO->setCurrentIndex(0);
1679                 } else {
1680                         int const i = langModule->encodingCO->findText(
1681                                         toqstr(params.inputenc));
1682                         if (i >= 0)
1683                                 langModule->encodingCO->setCurrentIndex(i);
1684                         else
1685                                 // unknown encoding. Set to default.
1686                                 default_enc = true;
1687                 }
1688         }
1689         langModule->defaultencodingRB->setChecked(default_enc);
1690         langModule->otherencodingRB->setChecked(!default_enc);
1691
1692         // numbering
1693         int const min_toclevel = textClass().min_toclevel();
1694         int const max_toclevel = textClass().max_toclevel();
1695         if (textClass().hasTocLevels()) {
1696                 numberingModule->setEnabled(true);
1697                 numberingModule->depthSL->setMinimum(min_toclevel - 1);
1698                 numberingModule->depthSL->setMaximum(max_toclevel);
1699                 numberingModule->depthSL->setValue(params.secnumdepth);
1700                 numberingModule->tocSL->setMaximum(min_toclevel - 1);
1701                 numberingModule->tocSL->setMaximum(max_toclevel);
1702                 numberingModule->tocSL->setValue(params.tocdepth);
1703                 updateNumbering();
1704         } else {
1705                 numberingModule->setEnabled(false);
1706                 numberingModule->tocTW->clear();
1707         }
1708
1709         // bullets
1710         bulletsModule->setBullet(0, params.user_defined_bullet(0));
1711         bulletsModule->setBullet(1, params.user_defined_bullet(1));
1712         bulletsModule->setBullet(2, params.user_defined_bullet(2));
1713         bulletsModule->setBullet(3, params.user_defined_bullet(3));
1714         bulletsModule->init();
1715
1716         // packages
1717         int nitem = findToken(tex_graphics, params.graphicsDriver);
1718         if (nitem >= 0)
1719                 latexModule->psdriverCO->setCurrentIndex(nitem);
1720         updateModuleInfo();
1721         
1722         mathsModule->amsCB->setChecked(
1723                 params.use_amsmath == BufferParams::package_on);
1724         mathsModule->amsautoCB->setChecked(
1725                 params.use_amsmath == BufferParams::package_auto);
1726
1727         mathsModule->esintCB->setChecked(
1728                 params.use_esint == BufferParams::package_on);
1729         mathsModule->esintautoCB->setChecked(
1730                 params.use_esint == BufferParams::package_auto);
1731
1732         switch (params.spacing().getSpace()) {
1733                 case Spacing::Other: nitem = 3; break;
1734                 case Spacing::Double: nitem = 2; break;
1735                 case Spacing::Onehalf: nitem = 1; break;
1736                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
1737         }
1738
1739         // text layout
1740         latexModule->classCO->setCurrentIndex(params.getBaseClass());
1741         
1742         updatePagestyle(textClass().opt_pagestyle(),
1743                                  params.pagestyle);
1744
1745         textLayoutModule->lspacingCO->setCurrentIndex(nitem);
1746         if (params.spacing().getSpace() == Spacing::Other) {
1747                 textLayoutModule->lspacingLE->setText(
1748                         toqstr(params.spacing().getValueAsString()));
1749         }
1750         setLSpacing(nitem);
1751
1752         if (params.paragraph_separation == BufferParams::PARSEP_INDENT)
1753                 textLayoutModule->indentRB->setChecked(true);
1754         else
1755                 textLayoutModule->skipRB->setChecked(true);
1756
1757         int skip = 0;
1758         switch (params.getDefSkip().kind()) {
1759         case VSpace::SMALLSKIP:
1760                 skip = 0;
1761                 break;
1762         case VSpace::MEDSKIP:
1763                 skip = 1;
1764                 break;
1765         case VSpace::BIGSKIP:
1766                 skip = 2;
1767                 break;
1768         case VSpace::LENGTH:
1769         {
1770                 skip = 3;
1771                 string const length = params.getDefSkip().asLyXCommand();
1772                 lengthToWidgets(textLayoutModule->skipLE,
1773                         textLayoutModule->skipLengthCO,
1774                         length, defaultUnit);
1775                 break;
1776         }
1777         default:
1778                 skip = 0;
1779                 break;
1780         }
1781         textLayoutModule->skipCO->setCurrentIndex(skip);
1782         setSkip(skip);
1783
1784         textLayoutModule->twoColumnCB->setChecked(
1785                 params.columns == 2);
1786
1787         // break listings_params to multiple lines
1788         string lstparams =
1789                 InsetListingsParams(params.listings_params).separatedParams();
1790         textLayoutModule->listingsED->setPlainText(toqstr(lstparams));
1791
1792         if (!params.options.empty()) {
1793                 latexModule->optionsLE->setText(
1794                         toqstr(params.options));
1795         } else {
1796                 latexModule->optionsLE->setText(QString());
1797         }
1798
1799         floatModule->set(params.float_placement);
1800
1801         // Fonts
1802         updateFontsize(textClass().opt_fontsize(),
1803                         params.fontsize);
1804
1805         int n = findToken(tex_fonts_roman, params.fontsRoman);
1806         if (n >= 0) {
1807                 fontModule->fontsRomanCO->setCurrentIndex(n);
1808                 romanChanged(n);
1809         }
1810
1811         n = findToken(tex_fonts_sans, params.fontsSans);
1812         if (n >= 0)     {
1813                 fontModule->fontsSansCO->setCurrentIndex(n);
1814                 sansChanged(n);
1815         }
1816
1817         n = findToken(tex_fonts_monospaced, params.fontsTypewriter);
1818         if (n >= 0) {
1819                 fontModule->fontsTypewriterCO->setCurrentIndex(n);
1820                 ttChanged(n);
1821         }
1822
1823         fontModule->fontScCB->setChecked(params.fontsSC);
1824         fontModule->fontOsfCB->setChecked(params.fontsOSF);
1825         fontModule->scaleSansSB->setValue(params.fontsSansScale);
1826         fontModule->scaleTypewriterSB->setValue(params.fontsTypewriterScale);
1827         n = findToken(GuiDocument::fontfamilies, params.fontsDefaultFamily);
1828         if (n >= 0)
1829                 fontModule->fontsDefaultCO->setCurrentIndex(n);
1830
1831         // paper
1832         int const psize = params.papersize;
1833         pageLayoutModule->papersizeCO->setCurrentIndex(psize);
1834         setCustomPapersize(psize);
1835
1836         bool const landscape =
1837                 params.orientation == ORIENTATION_LANDSCAPE;
1838         pageLayoutModule->landscapeRB->setChecked(landscape);
1839         pageLayoutModule->portraitRB->setChecked(!landscape);
1840
1841         pageLayoutModule->facingPagesCB->setChecked(
1842                 params.sides == TwoSides);
1843
1844
1845         lengthToWidgets(pageLayoutModule->paperwidthLE,
1846                 pageLayoutModule->paperwidthUnitCO, params.paperwidth, defaultUnit);
1847
1848         lengthToWidgets(pageLayoutModule->paperheightLE,
1849                 pageLayoutModule->paperheightUnitCO, params.paperheight, defaultUnit);
1850
1851         // margins
1852         Ui::MarginsUi * m = marginsModule;
1853
1854         setMargins(!params.use_geometry);
1855
1856         lengthToWidgets(m->topLE, m->topUnit,
1857                 params.topmargin, defaultUnit);
1858
1859         lengthToWidgets(m->bottomLE, m->bottomUnit,
1860                 params.bottommargin, defaultUnit);
1861
1862         lengthToWidgets(m->innerLE, m->innerUnit,
1863                 params.leftmargin, defaultUnit);
1864
1865         lengthToWidgets(m->outerLE, m->outerUnit,
1866                 params.rightmargin, defaultUnit);
1867
1868         lengthToWidgets(m->headheightLE, m->headheightUnit,
1869                 params.headheight, defaultUnit);
1870
1871         lengthToWidgets(m->headsepLE, m->headsepUnit,
1872                 params.headsep, defaultUnit);
1873
1874         lengthToWidgets(m->footskipLE, m->footskipUnit,
1875                 params.footskip, defaultUnit);
1876
1877         branchesModule->update(params);
1878
1879         // PDF support
1880         PDFOptions const & pdf = params.pdfoptions();
1881         pdfSupportModule->use_hyperrefGB->setChecked(pdf.use_hyperref);
1882         pdfSupportModule->titleLE->setText(toqstr(pdf.title));
1883         pdfSupportModule->authorLE->setText(toqstr(pdf.author));
1884         pdfSupportModule->subjectLE->setText(toqstr(pdf.subject));
1885         pdfSupportModule->keywordsLE->setText(toqstr(pdf.keywords));
1886
1887         pdfSupportModule->bookmarksGB->setChecked(pdf.bookmarks);
1888         pdfSupportModule->bookmarksnumberedCB->setChecked(pdf.bookmarksnumbered);
1889         pdfSupportModule->bookmarksopenGB->setChecked(pdf.bookmarksopen);
1890
1891         pdfSupportModule->bookmarksopenlevelSB->setValue(pdf.bookmarksopenlevel);
1892
1893         pdfSupportModule->breaklinksCB->setChecked(pdf.breaklinks);
1894         pdfSupportModule->pdfborderCB->setChecked(pdf.pdfborder);
1895         pdfSupportModule->pdfusetitleCB->setChecked(pdf.pdfusetitle);
1896         pdfSupportModule->colorlinksCB->setChecked(pdf.colorlinks);
1897         pdfSupportModule->backrefCB->setChecked(pdf.backref);
1898         pdfSupportModule->pagebackrefCB->setChecked(pdf.pagebackref);
1899         pdfSupportModule->fullscreenCB->setChecked
1900                 (pdf.pagemode == pdf.pagemode_fullscreen);
1901
1902         pdfSupportModule->optionsLE->setText(
1903                 toqstr(pdf.quoted_options));
1904
1905         // embedded files
1906         updateEmbeddedFileList();
1907 }
1908
1909
1910 void GuiDocument::applyView()
1911 {
1912         apply(params());
1913 }
1914
1915
1916 void GuiDocument::saveDocDefault()
1917 {
1918         // we have to apply the params first
1919         applyView();
1920         saveAsDefault();
1921 }
1922
1923
1924 void GuiDocument::updateContents()
1925 {
1926         //update list of available modules
1927         QStringList strlist;
1928         vector<string> const modNames = getModuleNames();
1929         vector<string>::const_iterator it = modNames.begin();
1930         for (; it != modNames.end(); ++it)
1931                 strlist.push_back(toqstr(*it));
1932         available_model_.setStringList(strlist);
1933         //and selected ones, too
1934         QStringList strlist2;
1935         vector<string> const & selMods = getSelectedModules();
1936         it = selMods.begin();
1937         for (; it != selMods.end(); ++it)
1938                 strlist2.push_back(toqstr(*it));
1939         //FIXME It'd be nice to make sure here that the selected
1940         //modules are consistent: That required modules are actually
1941         //selected, and that we don't have conflicts. If so, we could
1942         //at least pop up a warning.
1943         selected_model_.setStringList(strlist2);
1944
1945         updateParams(bp_);
1946 }
1947
1948 void GuiDocument::useClassDefaults()
1949 {
1950         bp_.setBaseClass(latexModule->classCO->currentIndex());
1951         bp_.useClassDefaults();
1952         updateContents();
1953 }
1954
1955
1956 bool GuiDocument::isValid()
1957 {
1958         return validate_listings_params().empty();
1959 }
1960
1961
1962 char const * const GuiDocument::fontfamilies[5] = {
1963         "default", "rmdefault", "sfdefault", "ttdefault", ""
1964 };
1965
1966
1967 char const * GuiDocument::fontfamilies_gui[5] = {
1968         N_("Default"), N_("Roman"), N_("Sans Serif"), N_("Typewriter"), ""
1969 };
1970
1971
1972 bool GuiDocument::initialiseParams(string const &)
1973 {
1974         bp_ = buffer().params();
1975         loadModuleNames();
1976         return true;
1977 }
1978
1979
1980 void GuiDocument::clearParams()
1981 {
1982         bp_ = BufferParams();
1983 }
1984
1985
1986 BufferId GuiDocument::id() const
1987 {
1988         return &buffer();
1989 }
1990
1991
1992 vector<string> const & GuiDocument::getModuleNames()
1993 {
1994         return moduleNames_;
1995 }
1996
1997
1998 vector<string> const & GuiDocument::getSelectedModules()
1999 {
2000         return params().getModules();
2001 }
2002
2003
2004 TextClass const & GuiDocument::textClass() const
2005 {
2006         return textclasslist[bp_.getBaseClass()];
2007 }
2008
2009
2010 static void dispatch_bufferparams(Dialog const & dialog,
2011         BufferParams const & bp, kb_action lfun)
2012 {
2013         ostringstream ss;
2014         ss << "\\begin_header\n";
2015         bp.writeFile(ss);
2016         ss << "\\end_header\n";
2017         dialog.dispatch(FuncRequest(lfun, ss.str()));
2018 }
2019
2020
2021 void GuiDocument::dispatchParams()
2022 {
2023         // This must come first so that a language change is correctly noticed
2024         setLanguage();
2025
2026         // Apply the BufferParams. Note that this will set the base class
2027         // and then update the buffer's layout.
2028         dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY);
2029
2030         // Generate the colours requested by each new branch.
2031         BranchList & branchlist = params().branchlist();
2032         if (!branchlist.empty()) {
2033                 BranchList::const_iterator it = branchlist.begin();
2034                 BranchList::const_iterator const end = branchlist.end();
2035                 for (; it != end; ++it) {
2036                         docstring const & current_branch = it->getBranch();
2037                         Branch const * branch = branchlist.find(current_branch);
2038                         string const x11hexname = X11hexname(branch->getColor());
2039                         // display the new color
2040                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
2041                         dispatch(FuncRequest(LFUN_SET_COLOR, str));
2042                 }
2043
2044                 // Open insets of selected branches, close deselected ones
2045                 dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
2046                         "assign branch"));
2047         }
2048         // FIXME: If we used an LFUN, we would not need those two lines:
2049         bufferview()->processUpdateFlags(Update::Force | Update::FitCursor);
2050 }
2051
2052
2053 void GuiDocument::setLanguage() const
2054 {
2055         Language const * const newL = bp_.language;
2056         if (buffer().params().language == newL)
2057                 return;
2058
2059         string const & lang_name = newL->lang();
2060         dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
2061 }
2062
2063
2064 void GuiDocument::saveAsDefault() const
2065 {
2066         dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
2067 }
2068
2069
2070 bool GuiDocument::isFontAvailable(string const & font) const
2071 {
2072         if (font == "default" || font == "cmr"
2073             || font == "cmss" || font == "cmtt")
2074                 // these are standard
2075                 return true;
2076         if (font == "lmodern" || font == "lmss" || font == "lmtt")
2077                 return LaTeXFeatures::isAvailable("lmodern");
2078         if (font == "times" || font == "palatino"
2079                  || font == "helvet" || font == "courier")
2080                 return LaTeXFeatures::isAvailable("psnfss");
2081         if (font == "cmbr" || font == "cmtl")
2082                 return LaTeXFeatures::isAvailable("cmbright");
2083         if (font == "utopia")
2084                 return LaTeXFeatures::isAvailable("utopia")
2085                         || LaTeXFeatures::isAvailable("fourier");
2086         if (font == "beraserif" || font == "berasans"
2087                 || font == "beramono")
2088                 return LaTeXFeatures::isAvailable("bera");
2089         return LaTeXFeatures::isAvailable(font);
2090 }
2091
2092
2093 bool GuiDocument::providesOSF(string const & font) const
2094 {
2095         if (font == "cmr")
2096                 return isFontAvailable("eco");
2097         if (font == "palatino")
2098                 return isFontAvailable("mathpazo");
2099         return false;
2100 }
2101
2102
2103 bool GuiDocument::providesSC(string const & font) const
2104 {
2105         if (font == "palatino")
2106                 return isFontAvailable("mathpazo");
2107         if (font == "utopia")
2108                 return isFontAvailable("fourier");
2109         return false;
2110 }
2111
2112
2113 bool GuiDocument::providesScale(string const & font) const
2114 {
2115         return font == "helvet" || font == "luximono"
2116                 || font == "berasans"  || font == "beramono";
2117 }
2118
2119
2120 void GuiDocument::loadModuleNames ()
2121 {
2122         moduleNames_.clear();
2123         LyXModuleList::const_iterator it = moduleList.begin();
2124         for (; it != moduleList.end(); ++it)
2125                 moduleNames_.push_back(it->getName());
2126         if (!moduleNames_.empty())
2127                 sort(moduleNames_.begin(), moduleNames_.end());
2128 }
2129
2130
2131 Dialog * createGuiDocument(GuiView & lv) { return new GuiDocument(lv); }
2132
2133
2134 } // namespace frontend
2135 } // namespace lyx
2136
2137 #include "GuiDocument_moc.cpp"