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