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