]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDocument.cpp
cosmetics
[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 "FloatPlacement.h"
17 #include "Layout.h"
18 #include "LengthCombo.h"
19 #include "PanelStack.h"
20 #include "qt_helpers.h"
21 #include "Validator.h"
22
23 // For the Branches module
24 #include "GuiBranches.h"
25
26 #include "GuiViewSource.h" // For latexHighlighter use in the preamble.
27
28 #include "frontend_helpers.h"
29 #include "BufferParams.h"
30 #include "Encoding.h"
31 #include "gettext.h"
32 #include "Language.h"
33 #include "LyXRC.h" // defaultUnit
34 #include "TextClassList.h"
35 #include "Spacing.h"
36 #include "PDFOptions.h"
37
38 #include "insets/InsetListingsParams.h"
39
40 #include "support/lstrings.h"
41
42 #include <boost/bind.hpp>
43
44 #include <QCloseEvent>
45 #include <QScrollBar>
46 #include <QTextCursor>
47
48 #include <algorithm>
49
50 using lyx::support::token;
51 using lyx::support::bformat;
52 using lyx::support::findToken;
53 using lyx::support::getVectorFromString;
54
55 using std::distance;
56 using std::make_pair;
57 using std::pair;
58 using std::vector;
59 using std::string;
60
61
62 ///
63 template<class Pair>
64 std::vector<typename Pair::second_type> const
65 getSecond(std::vector<Pair> const & pr)
66 {
67          std::vector<typename Pair::second_type> tmp(pr.size());
68          std::transform(pr.begin(), pr.end(), tmp.begin(),
69                                          boost::bind(&Pair::second, _1));
70          return tmp;
71 }
72
73 char const * const tex_graphics[] =
74 {
75         "default", "dvips", "dvitops", "emtex",
76         "ln", "oztex", "textures", "none", ""
77 };
78
79
80 char const * const tex_graphics_gui[] =
81 {
82         N_("Default"), "Dvips", "DVItoPS", "EmTeX",
83         "LN", "OzTeX", "Textures", N_("None"), ""
84 };
85
86
87 char const * const tex_fonts_roman[] =
88 {
89         "default", "cmr", "lmodern", "ae", "times", "palatino",
90         "charter", "newcent", "bookman", "utopia", "beraserif",
91         "ccfonts", "chancery", ""
92 };
93
94
95 char const * tex_fonts_roman_gui[] =
96 {
97         N_("Default"), N_("Computer Modern Roman"), N_("Latin Modern Roman"),
98         N_("AE (Almost European)"), N_("Times Roman"), N_("Palatino"),
99         N_("Bitstream Charter"), N_("New Century Schoolbook"), N_("Bookman"),
100         N_("Utopia"),  N_("Bera Serif"), N_("Concrete Roman"), N_("Zapf Chancery"),
101         ""
102 };
103
104
105 char const * const tex_fonts_sans[] =
106 {
107         "default", "cmss", "lmss", "helvet", "avant", "berasans", "cmbr", ""
108 };
109
110
111 char const * tex_fonts_sans_gui[] =
112 {
113         N_("Default"), N_("Computer Modern Sans"), N_("Latin Modern Sans"),
114         N_("Helvetica"), N_("Avant Garde"), N_("Bera Sans"), N_("CM Bright"), ""
115 };
116
117
118 char const * const tex_fonts_monospaced[] =
119 {
120         "default", "cmtt", "lmtt", "courier", "beramono", "luximono", "cmtl", ""
121 };
122
123
124 char const * tex_fonts_monospaced_gui[] =
125 {
126         N_("Default"), N_("Computer Modern Typewriter"),
127         N_("Latin Modern Typewriter"), N_("Courier"), N_("Bera Mono"),
128         N_("LuxiMono"), N_("CM Typewriter Light"), ""
129 };
130
131
132 vector<pair<string, lyx::docstring> > pagestyles;
133
134
135 namespace lyx {
136 namespace frontend {
137
138 /////////////////////////////////////////////////////////////////////
139 //
140 // PreambleModule
141 //
142 /////////////////////////////////////////////////////////////////////
143
144 PreambleModule::PreambleModule(): current_id_(0)
145 {
146         // This is not a memory leak. The object will be destroyed
147         // with this.
148         (void) new LaTeXHighlighter(preambleTE->document());
149         setFocusProxy(preambleTE);
150         connect(preambleTE, SIGNAL(textChanged()), this, SIGNAL(changed()));
151 }
152
153
154 void PreambleModule::update(BufferParams const & params, BufferId id)
155 {
156         QString preamble = toqstr(params.preamble);
157         // Nothing to do if the params and preamble are unchanged.
158         if (id == current_id_
159                 && preamble == preambleTE->document()->toPlainText())
160                 return;
161
162         QTextCursor cur = preambleTE->textCursor();
163         // Save the coords before switching to the new one.
164         preamble_coords_[current_id_] =
165                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
166
167         // Save the params address for further use.
168         current_id_ = id;
169         preambleTE->document()->setPlainText(preamble);
170         Coords::const_iterator it = preamble_coords_.find(current_id_);
171         if (it == preamble_coords_.end())
172                 // First time we open this one.
173                 preamble_coords_[current_id_] = make_pair(0,0);
174         else {
175                 // Restore saved coords.
176                 QTextCursor cur = preambleTE->textCursor();
177                 cur.setPosition(it->second.first);
178                 preambleTE->setTextCursor(cur);
179                 preambleTE->verticalScrollBar()->setValue(it->second.second);
180         }
181 }
182
183
184 void PreambleModule::apply(BufferParams & params)
185 {
186         params.preamble = fromqstr(preambleTE->document()->toPlainText());
187 }
188
189
190 void PreambleModule::closeEvent(QCloseEvent * e)
191 {
192         // Save the coords before closing.
193         QTextCursor cur = preambleTE->textCursor();
194         preamble_coords_[current_id_] =
195                 make_pair(cur.position(), preambleTE->verticalScrollBar()->value());
196         e->accept();
197 }
198
199
200 /////////////////////////////////////////////////////////////////////
201 //
202 // DocumentDialog
203 //
204 /////////////////////////////////////////////////////////////////////
205
206
207
208 GuiDocumentDialog::GuiDocumentDialog(LyXView & lv)
209         : GuiDialog(lv, "document")
210 {
211         setupUi(this);
212         setController(new ControlDocument(*this));
213         setViewTitle(_("Document Settings"));
214
215         lang_ = getSecond(getLanguageData(false));
216
217         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
218         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
219         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
220         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
221
222         connect(savePB, SIGNAL(clicked()), this, SLOT(saveDefaultClicked()));
223         connect(defaultPB, SIGNAL(clicked()), this, SLOT(useDefaultsClicked()));
224
225         // Manage the restore, ok, apply, restore and cancel/close buttons
226         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
227         bc().setOK(okPB);
228         bc().setApply(applyPB);
229         bc().setCancel(closePB);
230         bc().setRestore(restorePB);
231
232         textLayoutModule = new UiWidget<Ui::TextLayoutUi>;
233         // text layout
234         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
235                 this, SLOT(change_adaptor()));
236         connect(textLayoutModule->lspacingCO, SIGNAL(activated(int)),
237                 this, SLOT(setLSpacing(int)));
238         connect(textLayoutModule->lspacingLE, SIGNAL(textChanged(const QString&)),
239                 this, SLOT(change_adaptor()));
240         connect(textLayoutModule->skipRB, SIGNAL(clicked()),
241                 this, SLOT(change_adaptor()));
242         connect(textLayoutModule->indentRB, SIGNAL(clicked()),
243                 this, SLOT(change_adaptor()));
244         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
245                 this, SLOT(change_adaptor()));
246         connect(textLayoutModule->skipLE, SIGNAL(textChanged(const QString &)),
247                 this, SLOT(change_adaptor()));
248         connect(textLayoutModule->skipLengthCO, SIGNAL(activated(int)),
249                 this, SLOT(change_adaptor()));
250         connect(textLayoutModule->skipCO, SIGNAL(activated(int)),
251                 this, SLOT(setSkip(int)));
252         connect(textLayoutModule->skipRB, SIGNAL(toggled(bool)),
253                 this, SLOT(enableSkip(bool)));
254         connect(textLayoutModule->twoColumnCB, SIGNAL(clicked()),
255                 this, SLOT(change_adaptor()));
256         connect(textLayoutModule->listingsED, SIGNAL(textChanged()),
257                 this, SLOT(change_adaptor()));
258         connect(textLayoutModule->bypassCB, SIGNAL(clicked()), 
259                 this, SLOT(change_adaptor()));
260         connect(textLayoutModule->bypassCB, SIGNAL(clicked()), 
261                 this, SLOT(set_listings_msg()));
262         connect(textLayoutModule->listingsED, SIGNAL(textChanged()),
263                 this, SLOT(set_listings_msg()));
264         textLayoutModule->listingsTB->setPlainText(
265                 qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
266         textLayoutModule->lspacingLE->setValidator(new QDoubleValidator(
267                 textLayoutModule->lspacingLE));
268         textLayoutModule->skipLE->setValidator(unsignedLengthValidator(
269                 textLayoutModule->skipLE));
270
271         textLayoutModule->skipCO->addItem(qt_("SmallSkip"));
272         textLayoutModule->skipCO->addItem(qt_("MedSkip"));
273         textLayoutModule->skipCO->addItem(qt_("BigSkip"));
274         textLayoutModule->skipCO->addItem(qt_("Length"));
275         // remove the %-items from the unit choice
276         textLayoutModule->skipLengthCO->noPercents();
277         textLayoutModule->lspacingCO->insertItem(
278                 Spacing::Single, qt_("Single"));
279         textLayoutModule->lspacingCO->insertItem(
280                 Spacing::Onehalf, qt_("OneHalf"));
281         textLayoutModule->lspacingCO->insertItem(
282                 Spacing::Double, qt_("Double"));
283         textLayoutModule->lspacingCO->insertItem(
284                 Spacing::Other, qt_("Custom"));
285
286         // initialize the length validator
287         bc().addCheckedLineEdit(textLayoutModule->skipLE);
288
289         fontModule = new UiWidget<Ui::FontUi>;
290         // fonts
291         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
292                 this, SLOT(change_adaptor()));
293         connect(fontModule->fontsRomanCO, SIGNAL(activated(int)),
294                 this, SLOT(romanChanged(int)));
295         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
296                 this, SLOT(change_adaptor()));
297         connect(fontModule->fontsSansCO, SIGNAL(activated(int)),
298                 this, SLOT(sansChanged(int)));
299         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
300                 this, SLOT(change_adaptor()));
301         connect(fontModule->fontsTypewriterCO, SIGNAL(activated(int)),
302                 this, SLOT(ttChanged(int)));
303         connect(fontModule->fontsDefaultCO, SIGNAL(activated(int)),
304                 this, SLOT(change_adaptor()));
305         connect(fontModule->fontsizeCO, SIGNAL(activated(int)),
306                 this, SLOT(change_adaptor()));
307         connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)),
308                 this, SLOT(change_adaptor()));
309         connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)),
310                 this, SLOT(change_adaptor()));
311         connect(fontModule->fontScCB, SIGNAL(clicked()),
312                 this, SLOT(change_adaptor()));
313         connect(fontModule->fontOsfCB, SIGNAL(clicked()),
314                 this, SLOT(change_adaptor()));
315
316         for (int n = 0; tex_fonts_roman[n][0]; ++n) {
317                 QString font = qt_(tex_fonts_roman_gui[n]);
318                 if (!controller().isFontAvailable(tex_fonts_roman[n]))
319                         font += qt_(" (not installed)");
320                 fontModule->fontsRomanCO->addItem(font);
321         }
322         for (int n = 0; tex_fonts_sans[n][0]; ++n) {
323                 QString font = qt_(tex_fonts_sans_gui[n]);
324                 if (!controller().isFontAvailable(tex_fonts_sans[n]))
325                         font += qt_(" (not installed)");
326                 fontModule->fontsSansCO->addItem(font);
327         }
328         for (int n = 0; tex_fonts_monospaced[n][0]; ++n) {
329                 QString font = qt_(tex_fonts_monospaced_gui[n]);
330                 if (!controller().isFontAvailable(tex_fonts_monospaced[n]))
331                         font += qt_(" (not installed)");
332                 fontModule->fontsTypewriterCO->addItem(font);
333         }
334
335         fontModule->fontsizeCO->addItem(qt_("Default"));
336         fontModule->fontsizeCO->addItem(qt_("10"));
337         fontModule->fontsizeCO->addItem(qt_("11"));
338         fontModule->fontsizeCO->addItem(qt_("12"));
339
340         for (int n = 0; ControlDocument::fontfamilies_gui[n][0]; ++n)
341                 fontModule->fontsDefaultCO->addItem(
342                         qt_(ControlDocument::fontfamilies_gui[n]));
343
344
345         pageLayoutModule = new UiWidget<Ui::PageLayoutUi>;
346         // page layout
347         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
348                 this, SLOT(setCustomPapersize(int)));
349         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
350                 this, SLOT(setCustomPapersize(int)));
351         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
352                 this, SLOT(portraitChanged()));
353         connect(pageLayoutModule->papersizeCO, SIGNAL(activated(int)),
354                 this, SLOT(change_adaptor()));
355         connect(pageLayoutModule->paperheightLE, SIGNAL(textChanged(const QString &)),
356                 this, SLOT(change_adaptor()));
357         connect(pageLayoutModule->paperwidthLE, SIGNAL(textChanged(const QString &)),
358                 this, SLOT(change_adaptor()));
359         connect(pageLayoutModule->paperwidthUnitCO, SIGNAL(activated(int)),
360                 this, SLOT(change_adaptor()));
361         connect(pageLayoutModule->paperheightUnitCO, SIGNAL(activated(int)),
362                 this, SLOT(change_adaptor()));
363         connect(pageLayoutModule->portraitRB, SIGNAL(clicked()),
364                 this, SLOT(change_adaptor()));
365         connect(pageLayoutModule->landscapeRB, SIGNAL(clicked()),
366                 this, SLOT(change_adaptor()));
367         connect(pageLayoutModule->facingPagesCB, SIGNAL(clicked()),
368                 this, SLOT(change_adaptor()));
369         connect(pageLayoutModule->pagestyleCO, SIGNAL(activated(int)),
370                 this, SLOT(change_adaptor()));
371
372         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
373         pageLayoutModule->pagestyleCO->addItem(qt_("empty"));
374         pageLayoutModule->pagestyleCO->addItem(qt_("plain"));
375         pageLayoutModule->pagestyleCO->addItem(qt_("headings"));
376         pageLayoutModule->pagestyleCO->addItem(qt_("fancy"));
377         bc().addCheckedLineEdit(pageLayoutModule->paperheightLE,
378                 pageLayoutModule->paperheightL);
379         bc().addCheckedLineEdit(pageLayoutModule->paperwidthLE,
380                 pageLayoutModule->paperwidthL);
381
382         // paper
383         QComboBox * cb = pageLayoutModule->papersizeCO;
384         cb->addItem(qt_("Default"));
385         cb->addItem(qt_("Custom"));
386         cb->addItem(qt_("US letter"));
387         cb->addItem(qt_("US legal"));
388         cb->addItem(qt_("US executive"));
389         cb->addItem(qt_("A3"));
390         cb->addItem(qt_("A4"));
391         cb->addItem(qt_("A5"));
392         cb->addItem(qt_("B3"));
393         cb->addItem(qt_("B4"));
394         cb->addItem(qt_("B5"));
395         // remove the %-items from the unit choice
396         pageLayoutModule->paperwidthUnitCO->noPercents();
397         pageLayoutModule->paperheightUnitCO->noPercents();
398         pageLayoutModule->paperheightLE->setValidator(unsignedLengthValidator(
399                 pageLayoutModule->paperheightLE));
400         pageLayoutModule->paperwidthLE->setValidator(unsignedLengthValidator(
401                 pageLayoutModule->paperwidthLE));
402
403
404         marginsModule = new UiWidget<Ui::MarginsUi>;
405         // margins
406         connect(marginsModule->marginCB, SIGNAL(toggled(bool)),
407                 this, SLOT(setCustomMargins(bool)));
408         connect(marginsModule->marginCB, SIGNAL(clicked()),
409                 this, SLOT(change_adaptor()));
410         connect(marginsModule->topLE, SIGNAL(textChanged(const QString &)),
411                 this, SLOT(change_adaptor()));
412         connect(marginsModule->topUnit, SIGNAL(activated(int)),
413                 this, SLOT(change_adaptor()));
414         connect(marginsModule->bottomLE, SIGNAL(textChanged(const QString &)),
415                 this, SLOT(change_adaptor()));
416         connect(marginsModule->bottomUnit, SIGNAL(activated(int)),
417                 this, SLOT(change_adaptor()));
418         connect(marginsModule->innerLE, SIGNAL(textChanged(const QString &)),
419                 this, SLOT(change_adaptor()));
420         connect(marginsModule->innerUnit, SIGNAL(activated(int)),
421                 this, SLOT(change_adaptor()));
422         connect(marginsModule->outerLE, SIGNAL(textChanged(const QString &)),
423                 this, SLOT(change_adaptor()));
424         connect(marginsModule->outerUnit, SIGNAL(activated(int)),
425                 this, SLOT(change_adaptor()));
426         connect(marginsModule->headheightLE, SIGNAL(textChanged(const QString &)),
427                 this, SLOT(change_adaptor()));
428         connect(marginsModule->headheightUnit, SIGNAL(activated(int)),
429                 this, SLOT(change_adaptor()));
430         connect(marginsModule->headsepLE, SIGNAL(textChanged(const QString &)),
431                 this, SLOT(change_adaptor()));
432         connect(marginsModule->headsepUnit, SIGNAL(activated(int)),
433                 this, SLOT(change_adaptor()));
434         connect(marginsModule->footskipLE, SIGNAL(textChanged(const QString&)),
435                 this, SLOT(change_adaptor()));
436         connect(marginsModule->footskipUnit, SIGNAL(activated(int)),
437                 this, SLOT(change_adaptor()));
438         marginsModule->topLE->setValidator(unsignedLengthValidator(
439                 marginsModule->topLE));
440         marginsModule->bottomLE->setValidator(unsignedLengthValidator(
441                 marginsModule->bottomLE));
442         marginsModule->innerLE->setValidator(unsignedLengthValidator(
443                 marginsModule->innerLE));
444         marginsModule->outerLE->setValidator(unsignedLengthValidator(
445                 marginsModule->outerLE));
446         marginsModule->headsepLE->setValidator(unsignedLengthValidator(
447                 marginsModule->headsepLE));
448         marginsModule->headheightLE->setValidator(unsignedLengthValidator(
449                 marginsModule->headheightLE));
450         marginsModule->footskipLE->setValidator(unsignedLengthValidator(
451                 marginsModule->footskipLE));
452
453         bc().addCheckedLineEdit(marginsModule->topLE,
454                 marginsModule->topL);
455         bc().addCheckedLineEdit(marginsModule->bottomLE,
456                 marginsModule->bottomL);
457         bc().addCheckedLineEdit(marginsModule->innerLE,
458                 marginsModule->innerL);
459         bc().addCheckedLineEdit(marginsModule->outerLE,
460                 marginsModule->outerL);
461         bc().addCheckedLineEdit(marginsModule->headsepLE,
462                 marginsModule->headsepL);
463         bc().addCheckedLineEdit(marginsModule->headheightLE,
464                 marginsModule->headheightL);
465         bc().addCheckedLineEdit(marginsModule->footskipLE,
466                 marginsModule->footskipL);
467
468
469         langModule = new UiWidget<Ui::LanguageUi>;
470         // language & quote
471         connect(langModule->languageCO, SIGNAL(activated(int)),
472                 this, SLOT(change_adaptor()));
473         connect(langModule->defaultencodingRB, SIGNAL(clicked()),
474                 this, SLOT(change_adaptor()));
475         connect(langModule->otherencodingRB, SIGNAL(clicked()),
476                 this, SLOT(change_adaptor()));
477         connect(langModule->encodingCO, SIGNAL(activated(int)),
478                 this, SLOT(change_adaptor()));
479         connect(langModule->quoteStyleCO, SIGNAL(activated(int)),
480                 this, SLOT(change_adaptor()));
481         // language & quotes
482         vector<LanguagePair> const langs = getLanguageData(false);
483         vector<LanguagePair>::const_iterator lit  = langs.begin();
484         vector<LanguagePair>::const_iterator lend = langs.end();
485         for (; lit != lend; ++lit) {
486                 langModule->languageCO->addItem(toqstr(lit->first));
487         }
488
489         // Always put the default encoding in the first position.
490         // It is special because the displayed text is translated.
491         langModule->encodingCO->addItem(qt_("LaTeX default"));
492         Encodings::const_iterator it = encodings.begin();
493         Encodings::const_iterator const end = encodings.end();
494         for (; it != end; ++it)
495                 langModule->encodingCO->addItem(toqstr(it->latexName()));
496
497         langModule->quoteStyleCO->addItem(qt_("``text''"));
498         langModule->quoteStyleCO->addItem(qt_("''text''"));
499         langModule->quoteStyleCO->addItem(qt_(",,text``"));
500         langModule->quoteStyleCO->addItem(qt_(",,text''"));
501         langModule->quoteStyleCO->addItem(qt_("<<text>>"));
502         langModule->quoteStyleCO->addItem(qt_(">>text<<"));
503
504
505
506         numberingModule = new UiWidget<Ui::NumberingUi>;
507         // numbering
508         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
509                 this, SLOT(change_adaptor()));
510         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
511                 this, SLOT(change_adaptor()));
512         connect(numberingModule->depthSL, SIGNAL(valueChanged(int)),
513                 this, SLOT(updateNumbering()));
514         connect(numberingModule->tocSL, SIGNAL(valueChanged(int)),
515                 this, SLOT(updateNumbering()));
516         numberingModule->tocTW->setColumnCount(3);
517         numberingModule->tocTW->headerItem()->setText(0, qt_("Example"));
518         numberingModule->tocTW->headerItem()->setText(1, qt_("Numbered"));
519         numberingModule->tocTW->headerItem()->setText(2, qt_("Appears in TOC"));
520
521
522         biblioModule = new UiWidget<Ui::BiblioUi>;
523         connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
524                 biblioModule->citationStyleL, SLOT(setEnabled(bool)));
525         connect(biblioModule->citeNatbibRB, SIGNAL(toggled(bool)),
526                 biblioModule->citeStyleCO, SLOT(setEnabled(bool)));
527         // biblio
528         connect(biblioModule->citeDefaultRB, SIGNAL(clicked()),
529                 this, SLOT(change_adaptor()));
530         connect(biblioModule->citeNatbibRB, SIGNAL(clicked()),
531                 this, SLOT(change_adaptor()));
532         connect(biblioModule->citeStyleCO, SIGNAL(activated(int)),
533                 this, SLOT(change_adaptor()));
534         connect(biblioModule->citeJurabibRB, SIGNAL(clicked()),
535                 this, SLOT(change_adaptor()));
536         connect(biblioModule->bibtopicCB, SIGNAL(clicked()),
537                 this, SLOT(change_adaptor()));
538         // biblio
539         biblioModule->citeStyleCO->addItem(qt_("Author-year"));
540         biblioModule->citeStyleCO->addItem(qt_("Numerical"));
541         biblioModule->citeStyleCO->setCurrentIndex(0);
542
543
544         mathsModule = new UiWidget<Ui::MathsUi>;
545         connect(mathsModule->amsautoCB, SIGNAL(toggled(bool)),
546                 mathsModule->amsCB, SLOT(setDisabled(bool)));
547         connect(mathsModule->esintautoCB, SIGNAL(toggled(bool)),
548                 mathsModule->esintCB, SLOT(setDisabled(bool)));
549         // maths
550         connect(mathsModule->amsCB, SIGNAL(clicked()),
551                 this, SLOT(change_adaptor()));
552         connect(mathsModule->amsautoCB, SIGNAL(clicked()),
553                 this, SLOT(change_adaptor()));
554         connect(mathsModule->esintCB, SIGNAL(clicked()),
555                 this, SLOT(change_adaptor()));
556         connect(mathsModule->esintautoCB, SIGNAL(clicked()),
557                 this, SLOT(change_adaptor()));
558
559         latexModule = new UiWidget<Ui::LaTeXUi>;
560         // latex class
561         connect(latexModule->classCO, SIGNAL(activated(int)),
562                 this, SLOT(change_adaptor()));
563         connect(latexModule->optionsLE, SIGNAL(textChanged(const QString &)),
564                 this, SLOT(change_adaptor()));
565         connect(latexModule->psdriverCO, SIGNAL(activated(int)),
566                 this, SLOT(change_adaptor()));
567         connect(latexModule->classCO, SIGNAL(activated(int)),
568                 this, SLOT(classChanged()));
569         
570         selectionManager = 
571                 new GuiSelectionManager(latexModule->availableLV, latexModule->selectedLV, 
572                         latexModule->addPB, latexModule->deletePB, 
573                         latexModule->upPB, latexModule->downPB, 
574                         availableModel(), selectedModel());
575         connect(selectionManager, SIGNAL(updateHook()),
576                 this, SLOT(updateModuleInfo()));
577         connect(selectionManager, SIGNAL(updateHook()),
578                 this, SLOT(change_adaptor()));
579         
580         // postscript drivers
581         for (int n = 0; tex_graphics[n][0]; ++n) {
582                 QString enc = qt_(tex_graphics_gui[n]);
583                 latexModule->psdriverCO->addItem(enc);
584         }
585         // latex classes
586         //FIXME This seems too involved with the kernel. Some of this
587         //should be moved to the controller---which should perhaps just
588         //give us a list of entries or something of the sort.
589         for (TextClassList::const_iterator cit = textclasslist.begin();
590              cit != textclasslist.end(); ++cit) {
591                 if (cit->isTeXClassAvailable()) {
592                         latexModule->classCO->addItem(toqstr(cit->description()));
593                 } else {
594                         docstring item =
595                                 bformat(_("Unavailable: %1$s"), from_utf8(cit->description()));
596                         latexModule->classCO->addItem(toqstr(item));
597                 }
598         }
599
600         // branches
601         branchesModule = new GuiBranches;
602         connect(branchesModule, SIGNAL(changed()),
603                 this, SLOT(change_adaptor()));
604
605         // preamble
606         preambleModule = new PreambleModule;
607         connect(preambleModule, SIGNAL(changed()),
608                 this, SLOT(change_adaptor()));
609
610         // bullets
611         bulletsModule = new BulletsModule;
612         connect(bulletsModule, SIGNAL(changed()),
613                 this, SLOT(change_adaptor()));
614
615         // PDF support
616         pdfSupportModule = new UiWidget<Ui::PDFSupportUi>;
617
618         connect(pdfSupportModule->use_hyperrefGB, SIGNAL(toggled(bool)),
619                 this, SLOT(change_adaptor()));
620         connect(pdfSupportModule->titleLE, SIGNAL(textChanged(const QString &)),
621                 this, SLOT(change_adaptor()));
622         connect(pdfSupportModule->authorLE, SIGNAL(textChanged(const QString &)),
623                 this, SLOT(change_adaptor()));
624         connect(pdfSupportModule->subjectLE, SIGNAL(textChanged(const QString &)),
625                 this, SLOT(change_adaptor()));
626         connect(pdfSupportModule->keywordsLE, SIGNAL(textChanged(const QString &)),
627                 this, SLOT(change_adaptor()));
628         connect(pdfSupportModule->bookmarksGB, SIGNAL(toggled(bool)),
629                 this, SLOT(change_adaptor()));
630         connect(pdfSupportModule->bookmarksnumberedCB, SIGNAL(toggled(bool)),
631                 this, SLOT(change_adaptor()));
632         connect(pdfSupportModule->bookmarksopenGB, SIGNAL(toggled(bool)),
633                 this, SLOT(change_adaptor()));
634         connect(pdfSupportModule->bookmarksopenlevelSB, SIGNAL(valueChanged(int)),
635                 this, SLOT(change_adaptor()));
636         connect(pdfSupportModule->breaklinksCB, SIGNAL(toggled(bool)),
637                 this, SLOT(change_adaptor()));
638         connect(pdfSupportModule->pdfborderCB, SIGNAL(toggled(bool)),
639                 this, SLOT(change_adaptor()));
640         connect(pdfSupportModule->colorlinksCB, SIGNAL(toggled(bool)),
641                 this, SLOT(change_adaptor()));
642         connect(pdfSupportModule->backrefCB, SIGNAL(toggled(bool)),
643                 this, SLOT(change_adaptor()));
644         connect(pdfSupportModule->pagebackrefCB, SIGNAL(toggled(bool)),
645                 this, SLOT(change_adaptor()));
646         connect(pdfSupportModule->fullscreenCB, SIGNAL(toggled(bool)),
647                 this, SLOT(change_adaptor()));
648         connect(pdfSupportModule->optionsLE, SIGNAL(textChanged(const QString &)),
649                 this, SLOT(change_adaptor()));
650
651         // float
652         floatModule = new FloatPlacement;
653         connect(floatModule, SIGNAL(changed()),
654                 this, SLOT(change_adaptor()));
655
656         docPS->addPanel(latexModule, _("Document Class"));
657         docPS->addPanel(fontModule, _("Fonts"));
658         docPS->addPanel(textLayoutModule, _("Text Layout"));
659         docPS->addPanel(pageLayoutModule, _("Page Layout"));
660         docPS->addPanel(marginsModule, _("Page Margins"));
661         docPS->addPanel(langModule, _("Language"));
662         docPS->addPanel(numberingModule, _("Numbering & TOC"));
663         docPS->addPanel(biblioModule, _("Bibliography"));
664         docPS->addPanel(pdfSupportModule, _("PDF Properties"));
665         docPS->addPanel(mathsModule, _("Math Options"));
666         docPS->addPanel(floatModule, _("Float Placement"));
667         docPS->addPanel(bulletsModule, _("Bullets"));
668         docPS->addPanel(branchesModule, _("Branches"));
669         docPS->addPanel(preambleModule, _("LaTeX Preamble"));
670         docPS->setCurrentPanel(_("Document Class"));
671 // FIXME: hack to work around resizing bug in Qt >= 4.2
672 // bug verified with Qt 4.2.{0-3} (JSpitzm)
673 #if QT_VERSION >= 0x040200
674         docPS->updateGeometry();
675 #endif
676 }
677
678
679 ControlDocument & GuiDocumentDialog::controller()
680 {
681         return static_cast<ControlDocument &>(GuiDialog::controller());
682 }
683
684
685 void GuiDocumentDialog::showPreamble()
686 {
687         docPS->setCurrentPanel(_("LaTeX Preamble"));
688 }
689
690
691 void GuiDocumentDialog::saveDefaultClicked()
692 {
693         saveDocDefault();
694 }
695
696
697 void GuiDocumentDialog::useDefaultsClicked()
698 {
699         useClassDefaults();
700 }
701
702
703 void GuiDocumentDialog::change_adaptor()
704 {
705         changed();
706 }
707
708
709 docstring GuiDocumentDialog::validate_listings_params()
710 {
711         // use a cache here to avoid repeated validation
712         // of the same parameters
713         static string param_cache = string();
714         static docstring msg_cache = docstring();
715         
716         if (textLayoutModule->bypassCB->isChecked())
717                 return docstring();
718
719         string params = fromqstr(textLayoutModule->listingsED->toPlainText());
720         if (params != param_cache) {
721                 param_cache = params;
722                 msg_cache = InsetListingsParams(params).validate();
723         }
724         return msg_cache;
725 }
726
727
728 void GuiDocumentDialog::set_listings_msg()
729 {
730         static bool isOK = true;
731         docstring msg = validate_listings_params();
732         if (msg.empty()) {
733                 if (isOK)
734                         return;
735                 isOK = true;
736                 // listingsTB->setTextColor("black");
737                 textLayoutModule->listingsTB->setPlainText(
738                         qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
739         } else {
740                 isOK = false;
741                 // listingsTB->setTextColor("red");
742                 textLayoutModule->listingsTB->setPlainText(toqstr(msg));
743         }
744 }
745
746
747 void GuiDocumentDialog::closeEvent(QCloseEvent * e)
748 {
749         slotClose();
750         e->accept();
751 }
752
753
754 void GuiDocumentDialog::setLSpacing(int item)
755 {
756         textLayoutModule->lspacingLE->setEnabled(item == 3);
757 }
758
759
760 void GuiDocumentDialog::setSkip(int item)
761 {
762         bool const enable = (item == 3);
763         textLayoutModule->skipLE->setEnabled(enable);
764         textLayoutModule->skipLengthCO->setEnabled(enable);
765 }
766
767
768 void GuiDocumentDialog::enableSkip(bool skip)
769 {
770         textLayoutModule->skipCO->setEnabled(skip);
771         textLayoutModule->skipLE->setEnabled(skip);
772         textLayoutModule->skipLengthCO->setEnabled(skip);
773         if (skip)
774                 setSkip(textLayoutModule->skipCO->currentIndex());
775 }
776
777 void GuiDocumentDialog::portraitChanged()
778 {
779         setMargins(pageLayoutModule->papersizeCO->currentIndex());
780 }
781
782 void GuiDocumentDialog::setMargins(bool custom)
783 {
784         marginsModule->marginCB->setChecked(custom);
785         setCustomMargins(custom);
786 }
787
788
789 void GuiDocumentDialog::setCustomPapersize(int papersize)
790 {
791         bool const custom = (papersize == 1);
792
793         pageLayoutModule->paperwidthL->setEnabled(custom);
794         pageLayoutModule->paperwidthLE->setEnabled(custom);
795         pageLayoutModule->paperwidthUnitCO->setEnabled(custom);
796         pageLayoutModule->paperheightL->setEnabled(custom);
797         pageLayoutModule->paperheightLE->setEnabled(custom);
798         pageLayoutModule->paperheightLE->setFocus();
799         pageLayoutModule->paperheightUnitCO->setEnabled(custom);
800 }
801
802
803 void GuiDocumentDialog::setCustomMargins(bool custom)
804 {
805         marginsModule->topL->setEnabled(!custom);
806         marginsModule->topLE->setEnabled(!custom);
807         marginsModule->topUnit->setEnabled(!custom);
808
809         marginsModule->bottomL->setEnabled(!custom);
810         marginsModule->bottomLE->setEnabled(!custom);
811         marginsModule->bottomUnit->setEnabled(!custom);
812
813         marginsModule->innerL->setEnabled(!custom);
814         marginsModule->innerLE->setEnabled(!custom);
815         marginsModule->innerUnit->setEnabled(!custom);
816
817         marginsModule->outerL->setEnabled(!custom);
818         marginsModule->outerLE->setEnabled(!custom);
819         marginsModule->outerUnit->setEnabled(!custom);
820
821         marginsModule->headheightL->setEnabled(!custom);
822         marginsModule->headheightLE->setEnabled(!custom);
823         marginsModule->headheightUnit->setEnabled(!custom);
824
825         marginsModule->headsepL->setEnabled(!custom);
826         marginsModule->headsepLE->setEnabled(!custom);
827         marginsModule->headsepUnit->setEnabled(!custom);
828
829         marginsModule->footskipL->setEnabled(!custom);
830         marginsModule->footskipLE->setEnabled(!custom);
831         marginsModule->footskipUnit->setEnabled(!custom);
832 }
833
834
835 void GuiDocumentDialog::updateFontsize(string const & items, string const & sel)
836 {
837         fontModule->fontsizeCO->clear();
838         fontModule->fontsizeCO->addItem(qt_("Default"));
839
840         for (int n = 0; !token(items,'|',n).empty(); ++n)
841                 fontModule->fontsizeCO->
842                         addItem(toqstr(token(items,'|',n)));
843
844         for (int n = 0; n < fontModule->fontsizeCO->count(); ++n) {
845                 if (fromqstr(fontModule->fontsizeCO->itemText(n)) == sel) {
846                         fontModule->fontsizeCO->setCurrentIndex(n);
847                         break;
848                 }
849         }
850 }
851
852
853 void GuiDocumentDialog::romanChanged(int item)
854 {
855         string const font = tex_fonts_roman[item];
856         fontModule->fontScCB->setEnabled(controller().providesSC(font));
857         fontModule->fontOsfCB->setEnabled(controller().providesOSF(font));
858 }
859
860
861 void GuiDocumentDialog::sansChanged(int item)
862 {
863         string const font = tex_fonts_sans[item];
864         bool scaleable = controller().providesScale(font);
865         fontModule->scaleSansSB->setEnabled(scaleable);
866         fontModule->scaleSansLA->setEnabled(scaleable);
867 }
868
869
870 void GuiDocumentDialog::ttChanged(int item)
871 {
872         string const font = tex_fonts_monospaced[item];
873         bool scaleable = controller().providesScale(font);
874         fontModule->scaleTypewriterSB->setEnabled(scaleable);
875         fontModule->scaleTypewriterLA->setEnabled(scaleable);
876 }
877
878
879 void GuiDocumentDialog::updatePagestyle(string const & items, string const & sel)
880 {
881         pagestyles.clear();
882         pageLayoutModule->pagestyleCO->clear();
883         pageLayoutModule->pagestyleCO->addItem(qt_("Default"));
884
885         for (int n = 0; !token(items,'|',n).empty(); ++n) {
886                 string style = token(items, '|', n);
887                 docstring style_gui = _(style);
888                 pagestyles.push_back(pair<string, docstring>(style, style_gui));
889                 pageLayoutModule->pagestyleCO->addItem(toqstr(style_gui));
890         }
891
892         if (sel == "default") {
893                 pageLayoutModule->pagestyleCO->setCurrentIndex(0);
894                 return;
895         }
896
897         int nn = 0;
898
899         for (size_t i = 0; i < pagestyles.size(); ++i)
900                 if (pagestyles[i].first == sel)
901                         nn = pageLayoutModule->pagestyleCO->findText(
902                                         toqstr(pagestyles[i].second));
903
904         if (nn > 0)
905                 pageLayoutModule->pagestyleCO->setCurrentIndex(nn);
906 }
907
908
909 void GuiDocumentDialog::classChanged()
910 {
911         BufferParams & params = controller().params();
912         textclass_type const tc = latexModule->classCO->currentIndex();
913         params.setJustBaseClass(tc);
914         if (lyxrc.auto_reset_options)
915                 params.useClassDefaults();
916         updateContents();
917 }
918
919
920 void GuiDocumentDialog::updateModuleInfo()
921 {
922         selectionManager->update();
923         //Module description
924         QListView const * const lv = selectionManager->selectedFocused() ?
925                                      latexModule->selectedLV :
926                         latexModule->availableLV;
927         if (lv->selectionModel()->selectedIndexes().isEmpty())
928                 latexModule->infoML->document()->clear();
929         else {
930                 QModelIndex const idx = lv->selectionModel()->currentIndex();
931                 string const modName = fromqstr(idx.data().toString());
932                 string desc = controller().getModuleDescription(modName);
933                 vector<string> pkgList = controller().getPackageList(modName);
934                 string pkgdesc;
935                 //this mess formats the package list as "pkg1, pkg2, and pkg3"
936                 int const pkgListSize = pkgList.size();
937                 for (int i = 0; i < pkgListSize; ++i) {
938                         if (i == 1) {
939                                 if (i == pkgListSize - 1) //last element
940                                         pkgdesc += " and ";
941                                 else
942                                         pkgdesc += ", ";
943                         } else if (i > 1) {
944                                 if (i == pkgListSize - 1) //last element
945                                         pkgdesc += ", and ";
946                                 else
947                                         pkgdesc += ", ";
948                         }
949                         pkgdesc += pkgList[i];
950                 }
951                 if (!pkgdesc.empty())
952                         desc += " Requires " + pkgdesc + ".";
953                 latexModule->infoML->document()->setPlainText(toqstr(desc));
954         }
955 }
956
957
958 void GuiDocumentDialog::updateNumbering()
959 {
960         TextClass const & tclass = controller().params().getTextClass();
961
962         numberingModule->tocTW->setUpdatesEnabled(false);
963         numberingModule->tocTW->clear();
964
965         int const depth = numberingModule->depthSL->value();
966         int const toc = numberingModule->tocSL->value();
967         QString const no = qt_("No");
968         QString const yes = qt_("Yes");
969         TextClass::const_iterator end = tclass.end();
970         TextClass::const_iterator cit = tclass.begin();
971         QTreeWidgetItem * item = 0;
972         for ( ; cit != end ; ++cit) {
973                 int const toclevel = (*cit)->toclevel;
974                 if (toclevel != Layout::NOT_IN_TOC
975                     && (*cit)->labeltype == LABEL_COUNTER) {
976                         item = new QTreeWidgetItem(numberingModule->tocTW);
977                         item->setText(0, toqstr(translateIfPossible((*cit)->name())));
978                         item->setText(1, (toclevel <= depth) ? yes : no);
979                         item->setText(2, (toclevel <= toc) ? yes : no);
980                 }
981         }
982
983         numberingModule->tocTW->setUpdatesEnabled(true);
984         numberingModule->tocTW->update();
985 }
986
987
988 void GuiDocumentDialog::apply(BufferParams & params)
989 {
990         // preamble
991         preambleModule->apply(params);
992
993         // biblio
994         params.setCiteEngine(biblio::ENGINE_BASIC);
995
996         if (biblioModule->citeNatbibRB->isChecked()) {
997                 bool const use_numerical_citations =
998                         biblioModule->citeStyleCO->currentIndex();
999                 if (use_numerical_citations)
1000                         params.setCiteEngine(biblio::ENGINE_NATBIB_NUMERICAL);
1001                 else
1002                         params.setCiteEngine(biblio::ENGINE_NATBIB_AUTHORYEAR);
1003
1004         } else if (biblioModule->citeJurabibRB->isChecked())
1005                 params.setCiteEngine(biblio::ENGINE_JURABIB);
1006
1007         params.use_bibtopic =
1008                 biblioModule->bibtopicCB->isChecked();
1009
1010         // language & quotes
1011         if (langModule->defaultencodingRB->isChecked()) {
1012                 params.inputenc = "auto";
1013         } else {
1014                 int i = langModule->encodingCO->currentIndex();
1015                 if (i == 0)
1016                         params.inputenc = "default";
1017                 else
1018                         params.inputenc =
1019                                 fromqstr(langModule->encodingCO->currentText());
1020         }
1021
1022         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
1023         switch (langModule->quoteStyleCO->currentIndex()) {
1024         case 0:
1025                 lga = InsetQuotes::EnglishQ;
1026                 break;
1027         case 1:
1028                 lga = InsetQuotes::SwedishQ;
1029                 break;
1030         case 2:
1031                 lga = InsetQuotes::GermanQ;
1032                 break;
1033         case 3:
1034                 lga = InsetQuotes::PolishQ;
1035                 break;
1036         case 4:
1037                 lga = InsetQuotes::FrenchQ;
1038                 break;
1039         case 5:
1040                 lga = InsetQuotes::DanishQ;
1041                 break;
1042         }
1043         params.quotes_language = lga;
1044
1045         int const pos = langModule->languageCO->currentIndex();
1046         params.language = lyx::languages.getLanguage(lang_[pos]);
1047
1048         // numbering
1049         if (params.getTextClass().hasTocLevels()) {
1050                 params.tocdepth = numberingModule->tocSL->value();
1051                 params.secnumdepth = numberingModule->depthSL->value();
1052         }
1053
1054         // bullets
1055         params.user_defined_bullet(0) = bulletsModule->getBullet(0);
1056         params.user_defined_bullet(1) = bulletsModule->getBullet(1);
1057         params.user_defined_bullet(2) = bulletsModule->getBullet(2);
1058         params.user_defined_bullet(3) = bulletsModule->getBullet(3);
1059
1060         // packages
1061         params.graphicsDriver =
1062                 tex_graphics[latexModule->psdriverCO->currentIndex()];
1063         
1064         // Modules
1065         params.clearLayoutModules();
1066         QStringList const selMods = selectedModel()->stringList();
1067         for (int i = 0; i != selMods.size(); ++i)
1068                 params.addLayoutModule(lyx::fromqstr(selMods[i]));
1069
1070
1071         if (mathsModule->amsautoCB->isChecked()) {
1072                 params.use_amsmath = BufferParams::package_auto;
1073         } else {
1074                 if (mathsModule->amsCB->isChecked())
1075                         params.use_amsmath = BufferParams::package_on;
1076                 else
1077                         params.use_amsmath = BufferParams::package_off;
1078         }
1079
1080         if (mathsModule->esintautoCB->isChecked())
1081                 params.use_esint = BufferParams::package_auto;
1082         else {
1083                 if (mathsModule->esintCB->isChecked())
1084                         params.use_esint = BufferParams::package_on;
1085                 else
1086                         params.use_esint = BufferParams::package_off;
1087         }
1088
1089         // text layout
1090         params.setJustBaseClass(latexModule->classCO->currentIndex());
1091
1092         if (pageLayoutModule->pagestyleCO->currentIndex() == 0)
1093                 params.pagestyle = "default";
1094         else {
1095                 docstring style_gui =
1096                         qstring_to_ucs4(pageLayoutModule->pagestyleCO->currentText());
1097                 for (size_t i = 0; i < pagestyles.size(); ++i)
1098                         if (pagestyles[i].second == style_gui)
1099                                 params.pagestyle = pagestyles[i].first;
1100         }
1101
1102         switch (textLayoutModule->lspacingCO->currentIndex()) {
1103         case 0:
1104                 params.spacing().set(Spacing::Single);
1105                 break;
1106         case 1:
1107                 params.spacing().set(Spacing::Onehalf);
1108                 break;
1109         case 2:
1110                 params.spacing().set(Spacing::Double);
1111                 break;
1112         case 3:
1113                 params.spacing().set(Spacing::Other,
1114                         fromqstr(textLayoutModule->lspacingLE->text()));
1115                 break;
1116         }
1117
1118         if (textLayoutModule->twoColumnCB->isChecked())
1119                 params.columns = 2;
1120         else
1121                 params.columns = 1;
1122
1123         // text should have passed validation
1124         params.listings_params =
1125                 InsetListingsParams(fromqstr(textLayoutModule->listingsED->toPlainText())).params();
1126
1127         if (textLayoutModule->indentRB->isChecked())
1128                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
1129         else
1130                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
1131
1132         switch (textLayoutModule->skipCO->currentIndex()) {
1133         case 0:
1134                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
1135                 break;
1136         case 1:
1137                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
1138                 break;
1139         case 2:
1140                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
1141                 break;
1142         case 3:
1143         {
1144                 VSpace vs = VSpace(
1145                         widgetsToLength(textLayoutModule->skipLE,
1146                                 textLayoutModule->skipLengthCO)
1147                         );
1148                 params.setDefSkip(vs);
1149                 break;
1150         }
1151         default:
1152                 // DocumentDefskipCB assures that this never happens
1153                 // so Assert then !!!  - jbl
1154                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
1155                 break;
1156         }
1157
1158         params.options =
1159                 fromqstr(latexModule->optionsLE->text());
1160
1161         params.float_placement = floatModule->get();
1162
1163         // fonts
1164         params.fontsRoman =
1165                 tex_fonts_roman[fontModule->fontsRomanCO->currentIndex()];
1166
1167         params.fontsSans =
1168                 tex_fonts_sans[fontModule->fontsSansCO->currentIndex()];
1169
1170         params.fontsTypewriter =
1171                 tex_fonts_monospaced[fontModule->fontsTypewriterCO->currentIndex()];
1172
1173         params.fontsSansScale = fontModule->scaleSansSB->value();
1174
1175         params.fontsTypewriterScale = fontModule->scaleTypewriterSB->value();
1176
1177         params.fontsSC = fontModule->fontScCB->isChecked();
1178
1179         params.fontsOSF = fontModule->fontOsfCB->isChecked();
1180
1181         params.fontsDefaultFamily = ControlDocument::fontfamilies[
1182                 fontModule->fontsDefaultCO->currentIndex()];
1183
1184         if (fontModule->fontsizeCO->currentIndex() == 0)
1185                 params.fontsize = "default";
1186         else
1187                 params.fontsize =
1188                         fromqstr(fontModule->fontsizeCO->currentText());
1189
1190         // paper
1191         params.papersize = PAPER_SIZE(
1192                 pageLayoutModule->papersizeCO->currentIndex());
1193
1194         // custom, A3, B3 and B4 paper sizes need geometry
1195         int psize = pageLayoutModule->papersizeCO->currentIndex();
1196         bool geom_papersize = (psize == 1 || psize == 5 || psize == 8 || psize == 9);
1197
1198         params.paperwidth = widgetsToLength(pageLayoutModule->paperwidthLE,
1199                 pageLayoutModule->paperwidthUnitCO);
1200
1201         params.paperheight = widgetsToLength(pageLayoutModule->paperheightLE,
1202                 pageLayoutModule->paperheightUnitCO);
1203
1204         if (pageLayoutModule->facingPagesCB->isChecked())
1205                 params.sides = TextClass::TwoSides;
1206         else
1207                 params.sides = TextClass::OneSide;
1208
1209         if (pageLayoutModule->landscapeRB->isChecked())
1210                 params.orientation = ORIENTATION_LANDSCAPE;
1211         else
1212                 params.orientation = ORIENTATION_PORTRAIT;
1213
1214         // margins
1215         params.use_geometry =
1216                 (!marginsModule->marginCB->isChecked()
1217                 || geom_papersize);
1218
1219         Ui::MarginsUi const * m(marginsModule);
1220
1221         params.leftmargin = widgetsToLength(m->innerLE, m->innerUnit);
1222         params.topmargin = widgetsToLength(m->topLE, m->topUnit);
1223         params.rightmargin = widgetsToLength(m->outerLE, m->outerUnit);
1224         params.bottommargin = widgetsToLength(m->bottomLE, m->bottomUnit);
1225         params.headheight = widgetsToLength(m->headheightLE, m->headheightUnit);
1226         params.headsep = widgetsToLength(m->headsepLE, m->headsepUnit);
1227         params.footskip = widgetsToLength(m->footskipLE, m->footskipUnit);
1228
1229         branchesModule->apply(params);
1230
1231         // PDF support
1232         PDFOptions & pdf = params.pdfoptions();
1233         pdf.use_hyperref = pdfSupportModule->use_hyperrefGB->isChecked();
1234         pdf.title = fromqstr(pdfSupportModule->titleLE->text());
1235         pdf.author = fromqstr(pdfSupportModule->authorLE->text());
1236         pdf.subject = fromqstr(pdfSupportModule->subjectLE->text());
1237         pdf.keywords = fromqstr(pdfSupportModule->keywordsLE->text());
1238
1239         pdf.bookmarks = pdfSupportModule->bookmarksGB->isChecked();
1240         pdf.bookmarksnumbered = pdfSupportModule->bookmarksnumberedCB->isChecked();
1241         pdf.bookmarksopen = pdfSupportModule->bookmarksopenGB->isChecked();
1242         pdf.bookmarksopenlevel = pdfSupportModule->bookmarksopenlevelSB->value();
1243
1244         pdf.breaklinks = pdfSupportModule->breaklinksCB->isChecked();
1245         pdf.pdfborder = pdfSupportModule->pdfborderCB->isChecked();
1246         pdf.colorlinks = pdfSupportModule->colorlinksCB->isChecked();
1247         pdf.backref = pdfSupportModule->backrefCB->isChecked();
1248         pdf.pagebackref = pdfSupportModule->pagebackrefCB->isChecked();
1249         if (pdfSupportModule->fullscreenCB->isChecked())
1250                 pdf.pagemode = pdf.pagemode_fullscreen;
1251         else
1252                 pdf.pagemode.clear();
1253         pdf.quoted_options = fromqstr(pdfSupportModule->optionsLE->text());
1254         if (pdf.use_hyperref || !pdf.empty())
1255                 pdf.store_options = true;
1256 }
1257
1258
1259 /** Return the position of val in the vector if found.
1260     If not found, return 0.
1261  */
1262 template<class A>
1263 static size_t findPos(std::vector<A> const & vec, A const & val)
1264 {
1265         typename std::vector<A>::const_iterator it =
1266                 std::find(vec.begin(), vec.end(), val);
1267         if (it == vec.end())
1268                 return 0;
1269         return distance(vec.begin(), it);
1270 }
1271
1272
1273 void GuiDocumentDialog::updateParams()
1274 {
1275         BufferParams const & params = controller().params();
1276         updateParams(params);
1277 }
1278
1279
1280 void GuiDocumentDialog::updateParams(BufferParams const & params)
1281 {
1282         // set the default unit
1283         // FIXME: move to controller
1284         Length::UNIT defaultUnit = Length::CM;
1285         switch (lyxrc.default_papersize) {
1286                 case PAPER_DEFAULT: break;
1287
1288                 case PAPER_USLETTER:
1289                 case PAPER_USLEGAL:
1290                 case PAPER_USEXECUTIVE:
1291                         defaultUnit = Length::IN;
1292                         break;
1293
1294                 case PAPER_A3:
1295                 case PAPER_A4:
1296                 case PAPER_A5:
1297                 case PAPER_B3:
1298                 case PAPER_B4:
1299                 case PAPER_B5:
1300                         defaultUnit = Length::CM;
1301                         break;
1302                 case PAPER_CUSTOM:
1303                         break;
1304         }
1305
1306         // preamble
1307         preambleModule->update(params, controller().id());
1308
1309         // biblio
1310         biblioModule->citeDefaultRB->setChecked(
1311                 params.getEngine() == biblio::ENGINE_BASIC);
1312
1313         biblioModule->citeNatbibRB->setChecked(
1314                 params.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL ||
1315                 params.getEngine() == biblio::ENGINE_NATBIB_AUTHORYEAR);
1316
1317         biblioModule->citeStyleCO->setCurrentIndex(
1318                 params.getEngine() == biblio::ENGINE_NATBIB_NUMERICAL);
1319
1320         biblioModule->citeJurabibRB->setChecked(
1321                 params.getEngine() == biblio::ENGINE_JURABIB);
1322
1323         biblioModule->bibtopicCB->setChecked(
1324                 params.use_bibtopic);
1325
1326         // language & quotes
1327         int const pos = int(findPos(lang_,
1328                                     params.language->lang()));
1329         langModule->languageCO->setCurrentIndex(pos);
1330
1331         langModule->quoteStyleCO->setCurrentIndex(
1332                 params.quotes_language);
1333
1334         bool default_enc = true;
1335         if (params.inputenc != "auto") {
1336                 default_enc = false;
1337                 if (params.inputenc == "default") {
1338                         langModule->encodingCO->setCurrentIndex(0);
1339                 } else {
1340                         int const i = langModule->encodingCO->findText(
1341                                         toqstr(params.inputenc));
1342                         if (i >= 0)
1343                                 langModule->encodingCO->setCurrentIndex(i);
1344                         else
1345                                 // unknown encoding. Set to default.
1346                                 default_enc = true;
1347                 }
1348         }
1349         langModule->defaultencodingRB->setChecked(default_enc);
1350         langModule->otherencodingRB->setChecked(!default_enc);
1351
1352         // numbering
1353         int const min_toclevel = controller().textClass().min_toclevel();
1354         int const max_toclevel = controller().textClass().max_toclevel();
1355         if (controller().textClass().hasTocLevels()) {
1356                 numberingModule->setEnabled(true);
1357                 numberingModule->depthSL->setMinimum(min_toclevel - 1);
1358                 numberingModule->depthSL->setMaximum(max_toclevel);
1359                 numberingModule->depthSL->setValue(params.secnumdepth);
1360                 numberingModule->tocSL->setMaximum(min_toclevel - 1);
1361                 numberingModule->tocSL->setMaximum(max_toclevel);
1362                 numberingModule->tocSL->setValue(params.tocdepth);
1363                 updateNumbering();
1364         } else {
1365                 numberingModule->setEnabled(false);
1366                 numberingModule->tocTW->clear();
1367         }
1368
1369         // bullets
1370         bulletsModule->setBullet(0, params.user_defined_bullet(0));
1371         bulletsModule->setBullet(1, params.user_defined_bullet(1));
1372         bulletsModule->setBullet(2, params.user_defined_bullet(2));
1373         bulletsModule->setBullet(3, params.user_defined_bullet(3));
1374         bulletsModule->init();
1375
1376         // packages
1377         int nitem = findToken(tex_graphics, params.graphicsDriver);
1378         if (nitem >= 0)
1379                 latexModule->psdriverCO->setCurrentIndex(nitem);
1380         updateModuleInfo();
1381         
1382         mathsModule->amsCB->setChecked(
1383                 params.use_amsmath == BufferParams::package_on);
1384         mathsModule->amsautoCB->setChecked(
1385                 params.use_amsmath == BufferParams::package_auto);
1386
1387         mathsModule->esintCB->setChecked(
1388                 params.use_esint == BufferParams::package_on);
1389         mathsModule->esintautoCB->setChecked(
1390                 params.use_esint == BufferParams::package_auto);
1391
1392         switch (params.spacing().getSpace()) {
1393                 case Spacing::Other: nitem = 3; break;
1394                 case Spacing::Double: nitem = 2; break;
1395                 case Spacing::Onehalf: nitem = 1; break;
1396                 case Spacing::Default: case Spacing::Single: nitem = 0; break;
1397         }
1398
1399         // text layout
1400         latexModule->classCO->setCurrentIndex(params.getBaseClass());
1401         
1402         updatePagestyle(controller().textClass().opt_pagestyle(),
1403                                  params.pagestyle);
1404
1405         textLayoutModule->lspacingCO->setCurrentIndex(nitem);
1406         if (params.spacing().getSpace() == Spacing::Other) {
1407                 textLayoutModule->lspacingLE->setText(
1408                         toqstr(params.spacing().getValueAsString()));
1409         }
1410         setLSpacing(nitem);
1411
1412         if (params.paragraph_separation == BufferParams::PARSEP_INDENT)
1413                 textLayoutModule->indentRB->setChecked(true);
1414         else
1415                 textLayoutModule->skipRB->setChecked(true);
1416
1417         int skip = 0;
1418         switch (params.getDefSkip().kind()) {
1419         case VSpace::SMALLSKIP:
1420                 skip = 0;
1421                 break;
1422         case VSpace::MEDSKIP:
1423                 skip = 1;
1424                 break;
1425         case VSpace::BIGSKIP:
1426                 skip = 2;
1427                 break;
1428         case VSpace::LENGTH:
1429         {
1430                 skip = 3;
1431                 string const length = params.getDefSkip().asLyXCommand();
1432                 lengthToWidgets(textLayoutModule->skipLE,
1433                         textLayoutModule->skipLengthCO,
1434                         length, defaultUnit);
1435                 break;
1436         }
1437         default:
1438                 skip = 0;
1439                 break;
1440         }
1441         textLayoutModule->skipCO->setCurrentIndex(skip);
1442         setSkip(skip);
1443
1444         textLayoutModule->twoColumnCB->setChecked(
1445                 params.columns == 2);
1446
1447         // break listings_params to multiple lines
1448         string lstparams =
1449                 InsetListingsParams(params.listings_params).separatedParams();
1450         textLayoutModule->listingsED->setPlainText(toqstr(lstparams));
1451
1452         if (!params.options.empty()) {
1453                 latexModule->optionsLE->setText(
1454                         toqstr(params.options));
1455         } else {
1456                 latexModule->optionsLE->setText(QString());
1457         }
1458
1459         floatModule->set(params.float_placement);
1460
1461         // Fonts
1462         updateFontsize(controller().textClass().opt_fontsize(),
1463                         params.fontsize);
1464
1465         int n = findToken(tex_fonts_roman, params.fontsRoman);
1466         if (n >= 0) {
1467                 fontModule->fontsRomanCO->setCurrentIndex(n);
1468                 romanChanged(n);
1469         }
1470
1471         n = findToken(tex_fonts_sans, params.fontsSans);
1472         if (n >= 0)     {
1473                 fontModule->fontsSansCO->setCurrentIndex(n);
1474                 sansChanged(n);
1475         }
1476
1477         n = findToken(tex_fonts_monospaced, params.fontsTypewriter);
1478         if (n >= 0) {
1479                 fontModule->fontsTypewriterCO->setCurrentIndex(n);
1480                 ttChanged(n);
1481         }
1482
1483         fontModule->fontScCB->setChecked(params.fontsSC);
1484         fontModule->fontOsfCB->setChecked(params.fontsOSF);
1485         fontModule->scaleSansSB->setValue(params.fontsSansScale);
1486         fontModule->scaleTypewriterSB->setValue(params.fontsTypewriterScale);
1487         n = findToken(ControlDocument::fontfamilies, params.fontsDefaultFamily);
1488         if (n >= 0)
1489                 fontModule->fontsDefaultCO->setCurrentIndex(n);
1490
1491         // paper
1492         int const psize = params.papersize;
1493         pageLayoutModule->papersizeCO->setCurrentIndex(psize);
1494         setCustomPapersize(psize);
1495
1496         bool const landscape =
1497                 params.orientation == ORIENTATION_LANDSCAPE;
1498         pageLayoutModule->landscapeRB->setChecked(landscape);
1499         pageLayoutModule->portraitRB->setChecked(!landscape);
1500
1501         pageLayoutModule->facingPagesCB->setChecked(
1502                 params.sides == TextClass::TwoSides);
1503
1504
1505         lengthToWidgets(pageLayoutModule->paperwidthLE,
1506                 pageLayoutModule->paperwidthUnitCO, params.paperwidth, defaultUnit);
1507
1508         lengthToWidgets(pageLayoutModule->paperheightLE,
1509                 pageLayoutModule->paperheightUnitCO, params.paperheight, defaultUnit);
1510
1511         // margins
1512         Ui::MarginsUi * m = marginsModule;
1513
1514         setMargins(!params.use_geometry);
1515
1516         lengthToWidgets(m->topLE, m->topUnit,
1517                 params.topmargin, defaultUnit);
1518
1519         lengthToWidgets(m->bottomLE, m->bottomUnit,
1520                 params.bottommargin, defaultUnit);
1521
1522         lengthToWidgets(m->innerLE, m->innerUnit,
1523                 params.leftmargin, defaultUnit);
1524
1525         lengthToWidgets(m->outerLE, m->outerUnit,
1526                 params.rightmargin, defaultUnit);
1527
1528         lengthToWidgets(m->headheightLE, m->headheightUnit,
1529                 params.headheight, defaultUnit);
1530
1531         lengthToWidgets(m->headsepLE, m->headsepUnit,
1532                 params.headsep, defaultUnit);
1533
1534         lengthToWidgets(m->footskipLE, m->footskipUnit,
1535                 params.footskip, defaultUnit);
1536
1537         branchesModule->update(params);
1538
1539         // PDF support
1540         PDFOptions const & pdf = params.pdfoptions();
1541         pdfSupportModule->use_hyperrefGB->setChecked(pdf.use_hyperref);
1542         pdfSupportModule->titleLE->setText(toqstr(pdf.title));
1543         pdfSupportModule->authorLE->setText(toqstr(pdf.author));
1544         pdfSupportModule->subjectLE->setText(toqstr(pdf.subject));
1545         pdfSupportModule->keywordsLE->setText(toqstr(pdf.keywords));
1546
1547         pdfSupportModule->bookmarksGB->setChecked(pdf.bookmarks);
1548         pdfSupportModule->bookmarksnumberedCB->setChecked(pdf.bookmarksnumbered);
1549         pdfSupportModule->bookmarksopenGB->setChecked(pdf.bookmarksopen);
1550
1551         pdfSupportModule->bookmarksopenlevelSB->setValue(pdf.bookmarksopenlevel);
1552
1553         pdfSupportModule->breaklinksCB->setChecked(pdf.breaklinks);
1554         pdfSupportModule->pdfborderCB->setChecked(pdf.pdfborder);
1555         pdfSupportModule->colorlinksCB->setChecked(pdf.colorlinks);
1556         pdfSupportModule->backrefCB->setChecked(pdf.backref);
1557         pdfSupportModule->pagebackrefCB->setChecked(pdf.pagebackref);
1558         pdfSupportModule->fullscreenCB->setChecked
1559                 (pdf.pagemode == pdf.pagemode_fullscreen);
1560
1561         pdfSupportModule->optionsLE->setText(
1562                 toqstr(pdf.quoted_options));
1563 }
1564
1565
1566 void GuiDocumentDialog::applyView()
1567 {
1568         apply(controller().params());
1569 }
1570
1571
1572 void GuiDocumentDialog::saveDocDefault()
1573 {
1574         // we have to apply the params first
1575         applyView();
1576         controller().saveAsDefault();
1577 }
1578
1579
1580 void GuiDocumentDialog::updateContents()
1581 {
1582         //update list of available modules
1583         QStringList strlist;
1584         vector<string> const modNames = controller().getModuleNames();
1585         vector<string>::const_iterator it = modNames.begin();
1586         for (; it != modNames.end(); ++it)
1587                 strlist.push_back(toqstr(*it));
1588         available_model_.setStringList(strlist);
1589         //and selected ones, too
1590         QStringList strlist2;
1591         vector<string> const & selMods = controller().getSelectedModules();
1592         it = selMods.begin();
1593         for (; it != selMods.end(); ++it)
1594                 strlist2.push_back(toqstr(*it));
1595         selected_model_.setStringList(strlist2);
1596
1597         updateParams(controller().params());
1598 }
1599
1600 void GuiDocumentDialog::useClassDefaults()
1601 {
1602         BufferParams & params = controller().params();
1603
1604         params.setJustBaseClass(latexModule->classCO->currentIndex());
1605         params.useClassDefaults();
1606         updateContents();
1607 }
1608
1609
1610 bool GuiDocumentDialog::isValid()
1611 {
1612         return validate_listings_params().empty();
1613 }
1614
1615
1616 } // namespace frontend
1617 } // namespace lyx
1618
1619 #include "GuiDocument_moc.cpp"