]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrefs.cpp
A little cleanup while perusing TocWidget....
[lyx.git] / src / frontends / qt4 / GuiPrefs.cpp
1 /**
2  * \file GuiPrefs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiPrefs.h"
15
16 #include "ColorCache.h"
17 #include "FileDialog.h"
18 #include "GuiApplication.h"
19 #include "GuiFontExample.h"
20 #include "GuiFontLoader.h"
21 #include "GuiKeySymbol.h"
22 #include "qt_helpers.h"
23
24 #include "BufferList.h"
25 #include "Color.h"
26 #include "ColorSet.h"
27 #include "ConverterCache.h"
28 #include "FontEnums.h"
29 #include "FuncRequest.h"
30 #include "KeyMap.h"
31 #include "KeySequence.h"
32 #include "Language.h"
33 #include "LyXAction.h"
34 #include "PanelStack.h"
35 #include "paper.h"
36 #include "Session.h"
37 #include "SpellChecker.h"
38
39 #include "support/debug.h"
40 #include "support/FileName.h"
41 #include "support/filetools.h"
42 #include "support/foreach.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
45 #include "support/os.h"
46 #include "support/Package.h"
47
48 #include "graphics/GraphicsTypes.h"
49
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52 #include "frontends/FontLoader.h"
53
54 #include <QAbstractItemModel>
55 #include <QCheckBox>
56 #include <QColorDialog>
57 #include <QFontDatabase>
58 #include <QHeaderView>
59 #include <QLineEdit>
60 #include <QMessageBox>
61 #include <QPixmapCache>
62 #include <QPushButton>
63 #include <QSpinBox>
64 #include <QString>
65 #include <QTreeWidget>
66 #include <QTreeWidgetItem>
67 #include <QValidator>
68
69 #include <iomanip>
70 #include <sstream>
71 #include <algorithm>
72
73 using namespace Ui;
74
75 using namespace std;
76 using namespace lyx::support;
77 using namespace lyx::support::os;
78
79 namespace lyx {
80 namespace frontend {
81
82 /////////////////////////////////////////////////////////////////////
83 //
84 // Browser Helpers
85 //
86 /////////////////////////////////////////////////////////////////////
87
88 /** Launch a file dialog and return the chosen file.
89         filename: a suggested filename.
90         title: the title of the dialog.
91         pattern: *.ps etc.
92         dir1 = (name, dir), dir2 = (name, dir): extra buttons on the dialog.
93 */
94 QString browseFile(QString const & filename,
95         QString const & title,
96         QStringList const & filters,
97         bool save = false,
98         QString const & label1 = QString(),
99         QString const & dir1 = QString(),
100         QString const & label2 = QString(),
101         QString const & dir2 = QString(),
102         QString const & fallback_dir = QString())
103 {
104         QString lastPath = ".";
105         if (!filename.isEmpty())
106                 lastPath = onlyPath(filename);
107         else if(!fallback_dir.isEmpty())
108                 lastPath = fallback_dir;
109
110         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
111         dlg.setButton2(label1, dir1);
112         dlg.setButton2(label2, dir2);
113
114         FileDialog::Result result;
115
116         if (save)
117                 result = dlg.save(lastPath, filters, onlyFilename(filename));
118         else
119                 result = dlg.open(lastPath, filters, onlyFilename(filename));
120
121         return result.second;
122 }
123
124
125 /** Wrapper around browseFile which tries to provide a filename
126 *  relative to the user or system directory. The dir, name and ext
127 *  parameters have the same meaning as in the
128 *  support::LibFileSearch function.
129 */
130 QString browseLibFile(QString const & dir,
131         QString const & name,
132         QString const & ext,
133         QString const & title,
134         QStringList const & filters)
135 {
136         // FIXME UNICODE
137         QString const label1 = qt_("System files|#S#s");
138         QString const dir1 =
139                 toqstr(addName(package().system_support().absFilename(), fromqstr(dir)));
140
141         QString const label2 = qt_("User files|#U#u");
142         QString const dir2 =
143                 toqstr(addName(package().user_support().absFilename(), fromqstr(dir)));
144
145         QString const result = browseFile(toqstr(
146                 libFileSearch(dir, name, ext).absFilename()),
147                 title, filters, false, dir1, dir2, QString(), QString(), dir1);
148
149         // remove the extension if it is the default one
150         QString noextresult;
151         if (getExtension(result) == ext)
152                 noextresult = removeExtension(result);
153         else
154                 noextresult = result;
155
156         // remove the directory, if it is the default one
157         QString const file = onlyFilename(noextresult);
158         if (toqstr(libFileSearch(dir, file, ext).absFilename()) == result)
159                 return file;
160         else
161                 return noextresult;
162 }
163
164
165 /** Launch a file dialog and return the chosen directory.
166         pathname: a suggested pathname.
167         title: the title of the dialog.
168         dir1 = (name, dir), dir2 = (name, dir): extra buttons on the dialog.
169 */
170 QString browseDir(QString const & pathname,
171         QString const & title,
172         QString const & label1 = QString(),
173         QString const & dir1 = QString(),
174         QString const & label2 = QString(),
175         QString const & dir2 = QString())
176 {
177         QString lastPath = ".";
178         if (!pathname.isEmpty())
179                 lastPath = onlyPath(pathname);
180
181         FileDialog dlg(title, LFUN_SELECT_FILE_SYNC);
182         dlg.setButton1(label1, dir1);
183         dlg.setButton2(label2, dir2);
184
185         FileDialog::Result const result =
186                 dlg.opendir(lastPath, onlyFilename(pathname));
187
188         return result.second;
189 }
190
191
192 } // namespace frontend
193
194
195 QString browseRelFile(QString const & filename, QString const & refpath,
196         QString const & title, QStringList const & filters, bool save,
197         QString const & label1, QString const & dir1,
198         QString const & label2, QString const & dir2)
199 {
200         QString const fname = makeAbsPath(filename, refpath);
201
202
203         QString const outname =
204                 frontend::browseFile(fname, title, filters, save, label1, dir1, label2, dir2);
205
206         QString const reloutname =
207                 toqstr(makeRelPath(qstring_to_ucs4(outname), qstring_to_ucs4(refpath)));
208
209         if (reloutname.startsWith("../"))
210                 return outname;
211         else
212                 return reloutname;
213 }
214
215
216
217 /////////////////////////////////////////////////////////////////////
218 //
219 // Helpers
220 //
221 /////////////////////////////////////////////////////////////////////
222
223 namespace frontend {
224
225 string const catLookAndFeel = N_("Look & Feel");
226 string const catEditing = N_("Editing");
227 string const catLanguage = N_("Language Settings");
228 string const catOutput = N_("Output");
229 string const catFiles = N_("File Handling");
230
231 static void parseFontName(QString const & mangled0,
232         string & name, string & foundry)
233 {
234         string mangled = fromqstr(mangled0);
235         size_t const idx = mangled.find('[');
236         if (idx == string::npos || idx == 0) {
237                 name = mangled;
238                 foundry.clear();
239         } else {
240                 name = mangled.substr(0, idx - 1),
241                 foundry = mangled.substr(idx + 1, mangled.size() - idx - 2);
242         }
243 }
244
245
246 static void setComboxFont(QComboBox * cb, string const & family,
247         string const & foundry)
248 {
249         QString fontname = toqstr(family);
250         if (!foundry.empty())
251                 fontname += " [" + toqstr(foundry) + ']';
252
253         for (int i = 0; i != cb->count(); ++i) {
254                 if (cb->itemText(i) == fontname) {
255                         cb->setCurrentIndex(i);
256                         return;
257                 }
258         }
259
260         // Try matching without foundry name
261
262         // We count in reverse in order to prefer the Xft foundry
263         for (int i = cb->count(); --i >= 0;) {
264                 string name, foundry;
265                 parseFontName(cb->itemText(i), name, foundry);
266                 if (compare_ascii_no_case(name, family) == 0) {
267                         cb->setCurrentIndex(i);
268                         return;
269                 }
270         }
271
272         // family alone can contain e.g. "Helvetica [Adobe]"
273         string tmpname, tmpfoundry;
274         parseFontName(toqstr(family), tmpname, tmpfoundry);
275
276         // We count in reverse in order to prefer the Xft foundry
277         for (int i = cb->count(); --i >= 0; ) {
278                 string name, foundry;
279                 parseFontName(cb->itemText(i), name, foundry);
280                 if (compare_ascii_no_case(name, foundry) == 0) {
281                         cb->setCurrentIndex(i);
282                         return;
283                 }
284         }
285
286         // Bleh, default fonts, and the names couldn't be found. Hack
287         // for bug 1063.
288
289         QFont font;
290         font.setKerning(false);
291
292         QString const font_family = toqstr(family);
293         if (font_family == guiApp->romanFontName()) {
294                 font.setStyleHint(QFont::Serif);
295                 font.setFamily(font_family);
296         } else if (font_family == guiApp->sansFontName()) {
297                 font.setStyleHint(QFont::SansSerif);
298                 font.setFamily(font_family);
299         } else if (font_family == guiApp->typewriterFontName()) {
300                 font.setStyleHint(QFont::TypeWriter);
301                 font.setFamily(font_family);
302         } else {
303                 LYXERR0("FAILED to find the default font: '"
304                        << foundry << "', '" << family << '\'');
305                 return;
306         }
307
308         QFontInfo info(font);
309         string default_font_name, dummyfoundry;
310         parseFontName(info.family(), default_font_name, dummyfoundry);
311         LYXERR0("Apparent font is " << default_font_name);
312
313         for (int i = 0; i < cb->count(); ++i) {
314                 LYXERR0("Looking at " << cb->itemText(i));
315                 if (compare_ascii_no_case(fromqstr(cb->itemText(i)),
316                                     default_font_name) == 0) {
317                         cb->setCurrentIndex(i);
318                         return;
319                 }
320         }
321
322         LYXERR0("FAILED to find the font: '"
323                << foundry << "', '" << family << '\'');
324 }
325
326
327
328 /////////////////////////////////////////////////////////////////////
329 //
330 // PrefPlaintext
331 //
332 /////////////////////////////////////////////////////////////////////
333
334 PrefPlaintext::PrefPlaintext(GuiPreferences * form)
335         : PrefModule(qt_(catOutput), qt_("Plain text"), form)
336 {
337         setupUi(this);
338         connect(plaintextLinelengthSB, SIGNAL(valueChanged(int)),
339                 this, SIGNAL(changed()));
340 }
341
342
343 void PrefPlaintext::apply(LyXRC & rc) const
344 {
345         rc.plaintext_linelen = plaintextLinelengthSB->value();
346 }
347
348
349 void PrefPlaintext::update(LyXRC const & rc)
350 {
351         plaintextLinelengthSB->setValue(rc.plaintext_linelen);
352 }
353
354
355 /////////////////////////////////////////////////////////////////////
356 //
357 // StrftimeValidator
358 //
359 /////////////////////////////////////////////////////////////////////
360
361 class StrftimeValidator : public QValidator
362 {
363 public:
364         StrftimeValidator(QWidget *);
365         QValidator::State validate(QString & input, int & pos) const;
366 };
367
368
369 StrftimeValidator::StrftimeValidator(QWidget * parent)
370         : QValidator(parent)
371 {
372 }
373
374
375 QValidator::State StrftimeValidator::validate(QString & input, int & /*pos*/) const
376 {
377         if (is_valid_strftime(fromqstr(input)))
378                 return QValidator::Acceptable;
379         else
380                 return QValidator::Intermediate;
381 }
382
383
384 /////////////////////////////////////////////////////////////////////
385 //
386 // PrefDate
387 //
388 /////////////////////////////////////////////////////////////////////
389
390 PrefDate::PrefDate(GuiPreferences * form)
391         : PrefModule(qt_(catOutput), qt_("Date format"), form)
392 {
393         setupUi(this);
394         DateED->setValidator(new StrftimeValidator(DateED));
395         connect(DateED, SIGNAL(textChanged(QString)),
396                 this, SIGNAL(changed()));
397 }
398
399
400 void PrefDate::on_DateED_textChanged(const QString &)
401 {
402         QString t = DateED->text();
403         int p = 0;
404         bool valid = DateED->validator()->validate(t, p)
405                      == QValidator::Acceptable;
406         setValid(DateLA, valid);
407 }
408
409
410 void PrefDate::apply(LyXRC & rc) const
411 {
412         rc.date_insert_format = fromqstr(DateED->text());
413 }
414
415
416 void PrefDate::update(LyXRC const & rc)
417 {
418         DateED->setText(toqstr(rc.date_insert_format));
419 }
420
421
422 /////////////////////////////////////////////////////////////////////
423 //
424 // PrefInput
425 //
426 /////////////////////////////////////////////////////////////////////
427
428 PrefInput::PrefInput(GuiPreferences * form)
429         : PrefModule(qt_(catEditing), qt_("Keyboard/Mouse"), form)
430 {
431         setupUi(this);
432
433         connect(keymapCB, SIGNAL(clicked()),
434                 this, SIGNAL(changed()));
435         connect(firstKeymapED, SIGNAL(textChanged(QString)),
436                 this, SIGNAL(changed()));
437         connect(secondKeymapED, SIGNAL(textChanged(QString)),
438                 this, SIGNAL(changed()));
439         connect(mouseWheelSpeedSB, SIGNAL(valueChanged(double)),
440                 this, SIGNAL(changed()));
441 }
442
443
444 void PrefInput::apply(LyXRC & rc) const
445 {
446         // FIXME: can derive CB from the two EDs
447         rc.use_kbmap = keymapCB->isChecked();
448         rc.primary_kbmap = internal_path(fromqstr(firstKeymapED->text()));
449         rc.secondary_kbmap = internal_path(fromqstr(secondKeymapED->text()));
450         rc.mouse_wheel_speed = mouseWheelSpeedSB->value();
451 }
452
453
454 void PrefInput::update(LyXRC const & rc)
455 {
456         // FIXME: can derive CB from the two EDs
457         keymapCB->setChecked(rc.use_kbmap);
458         firstKeymapED->setText(toqstr(external_path(rc.primary_kbmap)));
459         secondKeymapED->setText(toqstr(external_path(rc.secondary_kbmap)));
460         mouseWheelSpeedSB->setValue(rc.mouse_wheel_speed);
461 }
462
463
464 QString PrefInput::testKeymap(QString const & keymap)
465 {
466         return form_->browsekbmap(internalPath(keymap));
467 }
468
469
470 void PrefInput::on_firstKeymapPB_clicked(bool)
471 {
472         QString const file = testKeymap(firstKeymapED->text());
473         if (!file.isEmpty())
474                 firstKeymapED->setText(file);
475 }
476
477
478 void PrefInput::on_secondKeymapPB_clicked(bool)
479 {
480         QString const file = testKeymap(secondKeymapED->text());
481         if (!file.isEmpty())
482                 secondKeymapED->setText(file);
483 }
484
485
486 void PrefInput::on_keymapCB_toggled(bool keymap)
487 {
488         firstKeymapLA->setEnabled(keymap);
489         secondKeymapLA->setEnabled(keymap);
490         firstKeymapED->setEnabled(keymap);
491         secondKeymapED->setEnabled(keymap);
492         firstKeymapPB->setEnabled(keymap);
493         secondKeymapPB->setEnabled(keymap);
494 }
495
496
497 /////////////////////////////////////////////////////////////////////
498 //
499 // PrefCompletion
500 //
501 /////////////////////////////////////////////////////////////////////
502
503 PrefCompletion::PrefCompletion(GuiPreferences * form)
504         : PrefModule(qt_(catEditing), qt_("Input Completion"), form)
505 {
506         setupUi(this);
507
508         connect(inlineDelaySB, SIGNAL(valueChanged(double)),
509                 this, SIGNAL(changed()));
510         connect(inlineMathCB, SIGNAL(clicked()),
511                 this, SIGNAL(changed()));
512         connect(inlineTextCB, SIGNAL(clicked()),
513                 this, SIGNAL(changed()));
514         connect(inlineDotsCB, SIGNAL(clicked()),
515                 this, SIGNAL(changed()));
516         connect(popupDelaySB, SIGNAL(valueChanged(double)),
517                 this, SIGNAL(changed()));
518         connect(popupMathCB, SIGNAL(clicked()),
519                 this, SIGNAL(changed()));
520         connect(autocorrectionCB, SIGNAL(clicked()),
521                 this, SIGNAL(changed()));
522         connect(popupTextCB, SIGNAL(clicked()),
523                 this, SIGNAL(changed()));
524         connect(popupAfterCompleteCB, SIGNAL(clicked()),
525                 this, SIGNAL(changed()));
526         connect(cursorTextCB, SIGNAL(clicked()),
527                 this, SIGNAL(changed()));
528 }
529
530
531 void PrefCompletion::on_inlineTextCB_clicked()
532 {
533         enableCB();
534 }
535
536
537 void PrefCompletion::on_popupTextCB_clicked()
538 {
539         enableCB();
540 }
541
542
543 void PrefCompletion::enableCB()
544 {
545         cursorTextCB->setEnabled(
546                 popupTextCB->isChecked() || inlineTextCB->isChecked());
547 }
548
549
550 void PrefCompletion::apply(LyXRC & rc) const
551 {
552         rc.completion_inline_delay = inlineDelaySB->value();
553         rc.completion_inline_math = inlineMathCB->isChecked();
554         rc.completion_inline_text = inlineTextCB->isChecked();
555         rc.completion_inline_dots = inlineDotsCB->isChecked() ? 13 : -1;
556         rc.completion_popup_delay = popupDelaySB->value();
557         rc.completion_popup_math = popupMathCB->isChecked();
558         rc.autocorrection_math = autocorrectionCB->isChecked();
559         rc.completion_popup_text = popupTextCB->isChecked();
560         rc.completion_cursor_text = cursorTextCB->isChecked();
561         rc.completion_popup_after_complete =
562                 popupAfterCompleteCB->isChecked();
563 }
564
565
566 void PrefCompletion::update(LyXRC const & rc)
567 {
568         inlineDelaySB->setValue(rc.completion_inline_delay);
569         inlineMathCB->setChecked(rc.completion_inline_math);
570         inlineTextCB->setChecked(rc.completion_inline_text);
571         inlineDotsCB->setChecked(rc.completion_inline_dots != -1);
572         popupDelaySB->setValue(rc.completion_popup_delay);
573         popupMathCB->setChecked(rc.completion_popup_math);
574         autocorrectionCB->setChecked(rc.autocorrection_math);
575         popupTextCB->setChecked(rc.completion_popup_text);
576         cursorTextCB->setChecked(rc.completion_cursor_text);
577         popupAfterCompleteCB->setChecked(rc.completion_popup_after_complete);
578         enableCB();
579 }
580
581
582
583 /////////////////////////////////////////////////////////////////////
584 //
585 // PrefLatex
586 //
587 /////////////////////////////////////////////////////////////////////
588
589 PrefLatex::PrefLatex(GuiPreferences * form)
590         : PrefModule(qt_(catOutput), qt_("LaTeX"), form)
591 {
592         setupUi(this);
593         connect(latexEncodingCB, SIGNAL(clicked()),
594                 this, SIGNAL(changed()));
595         connect(latexEncodingED, SIGNAL(textChanged(QString)),
596                 this, SIGNAL(changed()));
597         connect(latexChecktexED, SIGNAL(textChanged(QString)),
598                 this, SIGNAL(changed()));
599         connect(latexBibtexCO, SIGNAL(activated(int)),
600                 this, SIGNAL(changed()));
601         connect(latexBibtexED, SIGNAL(textChanged(QString)),
602                 this, SIGNAL(changed()));
603         connect(latexJBibtexED, SIGNAL(textChanged(QString)),
604                 this, SIGNAL(changed()));
605         connect(latexIndexCO, SIGNAL(activated(int)),
606                 this, SIGNAL(changed()));
607         connect(latexIndexED, SIGNAL(textChanged(QString)),
608                 this, SIGNAL(changed()));
609         connect(latexJIndexED, SIGNAL(textChanged(QString)),
610                 this, SIGNAL(changed()));
611         connect(latexAutoresetCB, SIGNAL(clicked()),
612                 this, SIGNAL(changed()));
613         connect(latexDviPaperED, SIGNAL(textChanged(QString)),
614                 this, SIGNAL(changed()));
615         connect(latexPaperSizeCO, SIGNAL(activated(int)),
616                 this, SIGNAL(changed()));
617
618 #if defined(__CYGWIN__) || defined(_WIN32)
619         pathCB->setVisible(true);
620         connect(pathCB, SIGNAL(clicked()),
621                 this, SIGNAL(changed()));
622 #else
623         pathCB->setVisible(false);
624 #endif
625 }
626
627
628 void PrefLatex::on_latexEncodingCB_stateChanged(int state)
629 {
630         latexEncodingED->setEnabled(state == Qt::Checked);
631 }
632
633
634 void PrefLatex::on_latexBibtexCO_activated(int n)
635 {
636         QString const bibtex = latexBibtexCO->itemData(n).toString();
637         if (bibtex.isEmpty()) {
638                 latexBibtexED->clear();
639                 latexBibtexOptionsLA->setText(qt_("Co&mmand:"));
640                 return;
641         }
642         for (LyXRC::CommandSet::const_iterator it = bibtex_alternatives.begin();
643              it != bibtex_alternatives.end(); ++it) {
644                 QString const bib = toqstr(*it);
645                 int ind = bib.indexOf(" ");
646                 QString sel_command = bib.left(ind);
647                 QString sel_options = ind < 0 ? QString() : bib.mid(ind + 1);
648                 if (bibtex == sel_command) {
649                         if (ind < 0)
650                                 latexBibtexED->clear();
651                         else
652                                 latexBibtexED->setText(sel_options.trimmed());
653                 }
654         }
655         latexBibtexOptionsLA->setText(qt_("&Options:"));
656 }
657
658
659 void PrefLatex::on_latexIndexCO_activated(int n)
660 {
661         QString const index = latexIndexCO->itemData(n).toString();
662         if (index.isEmpty()) {
663                 latexIndexED->clear();
664                 latexIndexOptionsLA->setText(qt_("Co&mmand:"));
665                 return;
666         }
667         for (LyXRC::CommandSet::const_iterator it = index_alternatives.begin();
668              it != index_alternatives.end(); ++it) {
669                 QString const idx = toqstr(*it);
670                 int ind = idx.indexOf(" ");
671                 QString sel_command = idx.left(ind);
672                 QString sel_options = ind < 0 ? QString() : idx.mid(ind + 1);
673                 if (index == sel_command) {
674                         if (ind < 0)
675                                 latexIndexED->clear();
676                         else
677                                 latexIndexED->setText(sel_options.trimmed());
678                 }
679         }
680         latexIndexOptionsLA->setText(qt_("Op&tions:"));
681 }
682
683
684 void PrefLatex::apply(LyXRC & rc) const
685 {
686         // If bibtex is not empty, bibopt contains the options, otherwise
687         // it is a customized bibtex command with options.
688         QString const bibtex = latexBibtexCO->itemData(
689                 latexBibtexCO->currentIndex()).toString();
690         QString const bibopt = latexBibtexED->text();
691         if (bibtex.isEmpty())
692                 rc.bibtex_command = fromqstr(bibopt);
693         else if (bibopt.isEmpty())
694                 rc.bibtex_command = fromqstr(bibtex);
695         else
696                 rc.bibtex_command = fromqstr(bibtex) + " " + fromqstr(bibopt);
697
698         // If index is not empty, idxopt contains the options, otherwise
699         // it is a customized index command with options.
700         QString const index = latexIndexCO->itemData(
701                 latexIndexCO->currentIndex()).toString();
702         QString const idxopt = latexIndexED->text();
703         if (index.isEmpty())
704                 rc.index_command = fromqstr(idxopt);
705         else if (idxopt.isEmpty())
706                 rc.index_command = fromqstr(index);
707         else
708                 rc.index_command = fromqstr(index) + " " + fromqstr(idxopt);
709
710         if (latexEncodingCB->isChecked())
711                 rc.fontenc = fromqstr(latexEncodingED->text());
712         else
713                 rc.fontenc = "default";
714         rc.chktex_command = fromqstr(latexChecktexED->text());
715         rc.jbibtex_command = fromqstr(latexJBibtexED->text());
716         rc.jindex_command = fromqstr(latexJIndexED->text());
717         rc.nomencl_command = fromqstr(latexNomenclED->text());
718         rc.auto_reset_options = latexAutoresetCB->isChecked();
719         rc.view_dvi_paper_option = fromqstr(latexDviPaperED->text());
720         rc.default_papersize =
721                 form_->toPaperSize(latexPaperSizeCO->currentIndex());
722 #if defined(__CYGWIN__) || defined(_WIN32)
723         rc.windows_style_tex_paths = pathCB->isChecked();
724 #endif
725 }
726
727
728 void PrefLatex::update(LyXRC const & rc)
729 {
730         latexBibtexCO->clear();
731
732         latexBibtexCO->addItem(qt_("Custom"), QString());
733         for (LyXRC::CommandSet::const_iterator it = rc.bibtex_alternatives.begin();
734                              it != rc.bibtex_alternatives.end(); ++it) {
735                 QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
736                 latexBibtexCO->addItem(command, command);
737         }
738
739         bibtex_alternatives = rc.bibtex_alternatives;
740
741         QString const bib = toqstr(rc.bibtex_command);
742         int ind = bib.indexOf(" ");
743         QString sel_command = bib.left(ind);
744         QString sel_options = ind < 0 ? QString() : bib.mid(ind + 1);
745
746         int pos = latexBibtexCO->findData(sel_command);
747         if (pos != -1) {
748                 latexBibtexCO->setCurrentIndex(pos);
749                 latexBibtexED->setText(sel_options.trimmed());
750                 latexBibtexOptionsLA->setText(qt_("&Options:"));
751         } else {
752                 latexBibtexED->setText(toqstr(rc.bibtex_command));
753                 latexBibtexCO->setCurrentIndex(0);
754                 latexBibtexOptionsLA->setText(qt_("Co&mmand:"));
755         }
756
757         latexIndexCO->clear();
758
759         latexIndexCO->addItem(qt_("Custom"), QString());
760         for (LyXRC::CommandSet::const_iterator it = rc.index_alternatives.begin();
761                              it != rc.index_alternatives.end(); ++it) {
762                 QString const command = toqstr(*it).left(toqstr(*it).indexOf(" "));
763                 latexIndexCO->addItem(command, command);
764         }
765
766         index_alternatives = rc.index_alternatives;
767
768         QString const idx = toqstr(rc.index_command);
769         ind = idx.indexOf(" ");
770         sel_command = idx.left(ind);
771         sel_options = ind < 0 ? QString() : idx.mid(ind + 1);
772
773         pos = latexIndexCO->findData(sel_command);
774         if (pos != -1) {
775                 latexIndexCO->setCurrentIndex(pos);
776                 latexIndexED->setText(sel_options.trimmed());
777                 latexIndexOptionsLA->setText(qt_("Op&tions:"));
778         } else {
779                 latexIndexED->setText(toqstr(rc.index_command));
780                 latexIndexCO->setCurrentIndex(0);
781                 latexIndexOptionsLA->setText(qt_("Co&mmand:"));
782         }
783
784         if (rc.fontenc == "default") {
785                 latexEncodingCB->setChecked(false);
786                 latexEncodingED->setEnabled(false);
787         } else {
788                 latexEncodingCB->setChecked(true);
789                 latexEncodingED->setEnabled(true);
790                 latexEncodingED->setText(toqstr(rc.fontenc));
791         }
792         latexChecktexED->setText(toqstr(rc.chktex_command));
793         latexJBibtexED->setText(toqstr(rc.jbibtex_command));
794         latexJIndexED->setText(toqstr(rc.jindex_command));
795         latexNomenclED->setText(toqstr(rc.nomencl_command));
796         latexAutoresetCB->setChecked(rc.auto_reset_options);
797         latexDviPaperED->setText(toqstr(rc.view_dvi_paper_option));
798         latexPaperSizeCO->setCurrentIndex(
799                 form_->fromPaperSize(rc.default_papersize));
800 #if defined(__CYGWIN__) || defined(_WIN32)
801         pathCB->setChecked(rc.windows_style_tex_paths);
802 #endif
803 }
804
805
806 /////////////////////////////////////////////////////////////////////
807 //
808 // PrefScreenFonts
809 //
810 /////////////////////////////////////////////////////////////////////
811
812 PrefScreenFonts::PrefScreenFonts(GuiPreferences * form)
813         : PrefModule(qt_(catLookAndFeel), qt_("Screen fonts"), form)
814 {
815         setupUi(this);
816
817         connect(screenRomanCO, SIGNAL(activated(QString)),
818                 this, SLOT(selectRoman(QString)));
819         connect(screenSansCO, SIGNAL(activated(QString)),
820                 this, SLOT(selectSans(QString)));
821         connect(screenTypewriterCO, SIGNAL(activated(QString)),
822                 this, SLOT(selectTypewriter(QString)));
823
824         QFontDatabase fontdb;
825         QStringList families(fontdb.families());
826         for (QStringList::Iterator it = families.begin(); it != families.end(); ++it) {
827                 screenRomanCO->addItem(*it);
828                 screenSansCO->addItem(*it);
829                 screenTypewriterCO->addItem(*it);
830         }
831         connect(screenRomanCO, SIGNAL(activated(QString)),
832                 this, SIGNAL(changed()));
833         connect(screenSansCO, SIGNAL(activated(QString)),
834                 this, SIGNAL(changed()));
835         connect(screenTypewriterCO, SIGNAL(activated(QString)),
836                 this, SIGNAL(changed()));
837         connect(screenZoomSB, SIGNAL(valueChanged(int)),
838                 this, SIGNAL(changed()));
839         connect(screenDpiSB, SIGNAL(valueChanged(int)),
840                 this, SIGNAL(changed()));
841         connect(screenTinyED, SIGNAL(textChanged(QString)),
842                 this, SIGNAL(changed()));
843         connect(screenSmallestED, SIGNAL(textChanged(QString)),
844                 this, SIGNAL(changed()));
845         connect(screenSmallerED, SIGNAL(textChanged(QString)),
846                 this, SIGNAL(changed()));
847         connect(screenSmallED, SIGNAL(textChanged(QString)),
848                 this, SIGNAL(changed()));
849         connect(screenNormalED, SIGNAL(textChanged(QString)),
850                 this, SIGNAL(changed()));
851         connect(screenLargeED, SIGNAL(textChanged(QString)),
852                 this, SIGNAL(changed()));
853         connect(screenLargerED, SIGNAL(textChanged(QString)),
854                 this, SIGNAL(changed()));
855         connect(screenLargestED, SIGNAL(textChanged(QString)),
856                 this, SIGNAL(changed()));
857         connect(screenHugeED, SIGNAL(textChanged(QString)),
858                 this, SIGNAL(changed()));
859         connect(screenHugerED, SIGNAL(textChanged(QString)),
860                 this, SIGNAL(changed()));
861         connect(pixmapCacheCB, SIGNAL(toggled(bool)),
862                 this, SIGNAL(changed()));
863
864         screenTinyED->setValidator(new QDoubleValidator(screenTinyED));
865         screenSmallestED->setValidator(new QDoubleValidator(screenSmallestED));
866         screenSmallerED->setValidator(new QDoubleValidator(screenSmallerED));
867         screenSmallED->setValidator(new QDoubleValidator(screenSmallED));
868         screenNormalED->setValidator(new QDoubleValidator(screenNormalED));
869         screenLargeED->setValidator(new QDoubleValidator(screenLargeED));
870         screenLargerED->setValidator(new QDoubleValidator(screenLargerED));
871         screenLargestED->setValidator(new QDoubleValidator(screenLargestED));
872         screenHugeED->setValidator(new QDoubleValidator(screenHugeED));
873         screenHugerED->setValidator(new QDoubleValidator(screenHugerED));
874 }
875
876
877 void PrefScreenFonts::apply(LyXRC & rc) const
878 {
879         LyXRC const oldrc = rc;
880
881         parseFontName(screenRomanCO->currentText(),
882                 rc.roman_font_name, rc.roman_font_foundry);
883         parseFontName(screenSansCO->currentText(),
884                 rc.sans_font_name, rc.sans_font_foundry);
885         parseFontName(screenTypewriterCO->currentText(),
886                 rc.typewriter_font_name, rc.typewriter_font_foundry);
887
888         rc.zoom = screenZoomSB->value();
889         rc.dpi = screenDpiSB->value();
890         rc.font_sizes[FONT_SIZE_TINY] = widgetToDoubleStr(screenTinyED);
891         rc.font_sizes[FONT_SIZE_SCRIPT] = widgetToDoubleStr(screenSmallestED);
892         rc.font_sizes[FONT_SIZE_FOOTNOTE] = widgetToDoubleStr(screenSmallerED);
893         rc.font_sizes[FONT_SIZE_SMALL] = widgetToDoubleStr(screenSmallED);
894         rc.font_sizes[FONT_SIZE_NORMAL] = widgetToDoubleStr(screenNormalED);
895         rc.font_sizes[FONT_SIZE_LARGE] = widgetToDoubleStr(screenLargeED);
896         rc.font_sizes[FONT_SIZE_LARGER] = widgetToDoubleStr(screenLargerED);
897         rc.font_sizes[FONT_SIZE_LARGEST] = widgetToDoubleStr(screenLargestED);
898         rc.font_sizes[FONT_SIZE_HUGE] = widgetToDoubleStr(screenHugeED);
899         rc.font_sizes[FONT_SIZE_HUGER] = widgetToDoubleStr(screenHugerED);
900         rc.use_pixmap_cache = pixmapCacheCB->isChecked();
901
902         if (rc.font_sizes != oldrc.font_sizes
903                 || rc.roman_font_name != oldrc.roman_font_name
904                 || rc.sans_font_name != oldrc.sans_font_name
905                 || rc.typewriter_font_name != oldrc.typewriter_font_name
906                 || rc.zoom != oldrc.zoom || rc.dpi != oldrc.dpi) {
907                 // The global QPixmapCache is used in GuiPainter to cache text
908                 // painting so we must reset it in case any of the above
909                 // parameter is changed.
910                 QPixmapCache::clear();
911                 guiApp->fontLoader().update();
912                 form_->updateScreenFonts();
913         }
914 }
915
916
917 void PrefScreenFonts::update(LyXRC const & rc)
918 {
919         setComboxFont(screenRomanCO, rc.roman_font_name,
920                         rc.roman_font_foundry);
921         setComboxFont(screenSansCO, rc.sans_font_name,
922                         rc.sans_font_foundry);
923         setComboxFont(screenTypewriterCO, rc.typewriter_font_name,
924                         rc.typewriter_font_foundry);
925
926         selectRoman(screenRomanCO->currentText());
927         selectSans(screenSansCO->currentText());
928         selectTypewriter(screenTypewriterCO->currentText());
929
930         screenZoomSB->setValue(rc.zoom);
931         screenDpiSB->setValue(rc.dpi);
932         doubleToWidget(screenTinyED, rc.font_sizes[FONT_SIZE_TINY]);
933         doubleToWidget(screenSmallestED, rc.font_sizes[FONT_SIZE_SCRIPT]);
934         doubleToWidget(screenSmallerED, rc.font_sizes[FONT_SIZE_FOOTNOTE]);
935         doubleToWidget(screenSmallED, rc.font_sizes[FONT_SIZE_SMALL]);
936         doubleToWidget(screenNormalED, rc.font_sizes[FONT_SIZE_NORMAL]);
937         doubleToWidget(screenLargeED, rc.font_sizes[FONT_SIZE_LARGE]);
938         doubleToWidget(screenLargerED, rc.font_sizes[FONT_SIZE_LARGER]);
939         doubleToWidget(screenLargestED, rc.font_sizes[FONT_SIZE_LARGEST]);
940         doubleToWidget(screenHugeED, rc.font_sizes[FONT_SIZE_HUGE]);
941         doubleToWidget(screenHugerED, rc.font_sizes[FONT_SIZE_HUGER]);
942
943         pixmapCacheCB->setChecked(rc.use_pixmap_cache);
944 #if defined(Q_WS_X11)
945         pixmapCacheCB->setEnabled(false);
946 #endif
947
948 }
949
950
951 void PrefScreenFonts::selectRoman(const QString & name)
952 {
953         screenRomanFE->set(QFont(name), name);
954 }
955
956
957 void PrefScreenFonts::selectSans(const QString & name)
958 {
959         screenSansFE->set(QFont(name), name);
960 }
961
962
963 void PrefScreenFonts::selectTypewriter(const QString & name)
964 {
965         screenTypewriterFE->set(QFont(name), name);
966 }
967
968
969 /////////////////////////////////////////////////////////////////////
970 //
971 // PrefColors
972 //
973 /////////////////////////////////////////////////////////////////////
974
975 namespace {
976
977 struct ColorSorter
978 {
979         bool operator()(ColorCode lhs, ColorCode rhs) const {
980                 return 
981                         compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
982         }
983 };
984
985 } // namespace anon
986
987 PrefColors::PrefColors(GuiPreferences * form)
988         : PrefModule(qt_(catLookAndFeel), qt_("Colors"), form)
989 {
990         setupUi(this);
991
992         // FIXME: all of this initialization should be put into the controller.
993         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg113301.html
994         // for some discussion of why that is not trivial.
995         QPixmap icon(32, 32);
996         for (int i = 0; i < Color_ignore; ++i) {
997                 ColorCode lc = static_cast<ColorCode>(i);
998                 if (lc == Color_none
999                         || lc == Color_black
1000                         || lc == Color_white
1001                         || lc == Color_red
1002                         || lc == Color_green
1003                         || lc == Color_blue
1004                         || lc == Color_cyan
1005                         || lc == Color_magenta
1006                         || lc == Color_yellow
1007                         || lc == Color_inherit
1008                         || lc == Color_ignore) continue;
1009
1010                 lcolors_.push_back(lc);
1011         }
1012         sort(lcolors_.begin(), lcolors_.end(), ColorSorter());
1013         vector<ColorCode>::const_iterator cit = lcolors_.begin();
1014         vector<ColorCode>::const_iterator const end = lcolors_.end();
1015         for (; cit != end; ++cit) {
1016                         (void) new QListWidgetItem(QIcon(icon),
1017                         toqstr(lcolor.getGUIName(*cit)), lyxObjectsLW);
1018         }
1019         curcolors_.resize(lcolors_.size());
1020         newcolors_.resize(lcolors_.size());
1021         // End initialization
1022
1023         connect(colorChangePB, SIGNAL(clicked()),
1024                 this, SLOT(changeColor()));
1025         connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()),
1026                 this, SLOT(changeLyxObjectsSelection()));
1027         connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)),
1028                 this, SLOT(changeColor()));
1029 }
1030
1031
1032 void PrefColors::apply(LyXRC & /*rc*/) const
1033 {
1034         for (unsigned int i = 0; i < lcolors_.size(); ++i)
1035                 if (curcolors_[i] != newcolors_[i])
1036                         form_->setColor(lcolors_[i], newcolors_[i]);
1037 }
1038
1039
1040 void PrefColors::update(LyXRC const & /*rc*/)
1041 {
1042         for (unsigned int i = 0; i < lcolors_.size(); ++i) {
1043                 QColor color = QColor(guiApp->colorCache().get(lcolors_[i]));
1044                 QPixmap coloritem(32, 32);
1045                 coloritem.fill(color);
1046                 lyxObjectsLW->item(i)->setIcon(QIcon(coloritem));
1047                 newcolors_[i] = curcolors_[i] = color.name();
1048         }
1049         changeLyxObjectsSelection();
1050 }
1051
1052
1053 void PrefColors::changeColor()
1054 {
1055         int const row = lyxObjectsLW->currentRow();
1056
1057         // just to be sure
1058         if (row < 0)
1059                 return;
1060
1061         QString const color = newcolors_[row];
1062         QColor c = QColorDialog::getColor(QColor(color), qApp->focusWidget());
1063
1064         if (c.isValid() && c.name() != color) {
1065                 newcolors_[row] = c.name();
1066                 QPixmap coloritem(32, 32);
1067                 coloritem.fill(c);
1068                 lyxObjectsLW->currentItem()->setIcon(QIcon(coloritem));
1069                 // emit signal
1070                 changed();
1071         }
1072 }
1073
1074 void PrefColors::changeLyxObjectsSelection()
1075 {
1076         colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0);
1077 }
1078
1079
1080 /////////////////////////////////////////////////////////////////////
1081 //
1082 // PrefDisplay
1083 //
1084 /////////////////////////////////////////////////////////////////////
1085
1086 PrefDisplay::PrefDisplay(GuiPreferences * form)
1087         : PrefModule(qt_(catLookAndFeel), qt_("Display"), form)
1088 {
1089         setupUi(this);
1090         connect(displayGraphicsCB, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
1091         connect(instantPreviewCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
1092         connect(previewSizeSB, SIGNAL(valueChanged(double)), this, SIGNAL(changed()));
1093         connect(paragraphMarkerCB, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
1094         if (instantPreviewCO->currentIndex() == 0)
1095                 previewSizeSB->setEnabled(false);
1096         else
1097                 previewSizeSB->setEnabled(true);
1098 }
1099
1100
1101 void PrefDisplay::on_instantPreviewCO_currentIndexChanged(int index)
1102 {
1103         if (index == 0)
1104                 previewSizeSB->setEnabled(false);
1105         else
1106                 previewSizeSB->setEnabled(true);
1107 }
1108
1109
1110 void PrefDisplay::apply(LyXRC & rc) const
1111 {
1112         switch (instantPreviewCO->currentIndex()) {
1113                 case 0: rc.preview = LyXRC::PREVIEW_OFF; break;
1114                 case 1: rc.preview = LyXRC::PREVIEW_NO_MATH; break;
1115                 case 2: rc.preview = LyXRC::PREVIEW_ON; break;
1116         }
1117
1118         rc.display_graphics = displayGraphicsCB->isChecked();
1119         rc.preview_scale_factor = previewSizeSB->value();
1120         rc.paragraph_markers = paragraphMarkerCB->isChecked();
1121
1122         // FIXME!! The graphics cache no longer has a changeDisplay method.
1123 #if 0
1124         if (old_value != rc.display_graphics) {
1125                 graphics::GCache & gc = graphics::GCache::get();
1126                 gc.changeDisplay();
1127         }
1128 #endif
1129 }
1130
1131
1132 void PrefDisplay::update(LyXRC const & rc)
1133 {
1134         switch (rc.preview) {
1135         case LyXRC::PREVIEW_OFF:
1136                 instantPreviewCO->setCurrentIndex(0);
1137                 break;
1138         case LyXRC::PREVIEW_NO_MATH :
1139                 instantPreviewCO->setCurrentIndex(1);
1140                 break;
1141         case LyXRC::PREVIEW_ON :
1142                 instantPreviewCO->setCurrentIndex(2);
1143                 break;
1144         }
1145
1146         displayGraphicsCB->setChecked(rc.display_graphics);
1147         instantPreviewCO->setEnabled(rc.display_graphics);
1148         previewSizeSB->setValue(rc.preview_scale_factor);
1149         paragraphMarkerCB->setChecked(rc.paragraph_markers);
1150 }
1151
1152
1153 /////////////////////////////////////////////////////////////////////
1154 //
1155 // PrefPaths
1156 //
1157 /////////////////////////////////////////////////////////////////////
1158
1159 PrefPaths::PrefPaths(GuiPreferences * form)
1160         : PrefModule(QString(), qt_("Paths"), form)
1161 {
1162         setupUi(this);
1163
1164         connect(workingDirPB, SIGNAL(clicked()), this, SLOT(selectWorkingdir()));
1165         connect(workingDirED, SIGNAL(textChanged(QString)),
1166                 this, SIGNAL(changed()));
1167
1168         connect(templateDirPB, SIGNAL(clicked()), this, SLOT(selectTemplatedir()));
1169         connect(templateDirED, SIGNAL(textChanged(QString)),
1170                 this, SIGNAL(changed()));
1171
1172         connect(exampleDirPB, SIGNAL(clicked()), this, SLOT(selectExampledir()));
1173         connect(exampleDirED, SIGNAL(textChanged(QString)),
1174                 this, SIGNAL(changed()));
1175
1176         connect(backupDirPB, SIGNAL(clicked()), this, SLOT(selectBackupdir()));
1177         connect(backupDirED, SIGNAL(textChanged(QString)),
1178                 this, SIGNAL(changed()));
1179
1180         connect(lyxserverDirPB, SIGNAL(clicked()), this, SLOT(selectLyxPipe()));
1181         connect(lyxserverDirED, SIGNAL(textChanged(QString)),
1182                 this, SIGNAL(changed()));
1183
1184         connect(thesaurusDirPB, SIGNAL(clicked()), this, SLOT(selectThesaurusdir()));
1185         connect(thesaurusDirED, SIGNAL(textChanged(QString)),
1186                 this, SIGNAL(changed()));
1187
1188         connect(tempDirPB, SIGNAL(clicked()), this, SLOT(selectTempdir()));
1189         connect(tempDirED, SIGNAL(textChanged(QString)),
1190                 this, SIGNAL(changed()));
1191
1192         connect(hunspellDirPB, SIGNAL(clicked()), this, SLOT(selectHunspelldir()));
1193         connect(hunspellDirED, SIGNAL(textChanged(QString)),
1194                 this, SIGNAL(changed()));
1195
1196         connect(pathPrefixED, SIGNAL(textChanged(QString)),
1197                 this, SIGNAL(changed()));
1198 }
1199
1200
1201 void PrefPaths::apply(LyXRC & rc) const
1202 {
1203         rc.document_path = internal_path(fromqstr(workingDirED->text()));
1204         rc.example_path = internal_path(fromqstr(exampleDirED->text()));
1205         rc.template_path = internal_path(fromqstr(templateDirED->text()));
1206         rc.backupdir_path = internal_path(fromqstr(backupDirED->text()));
1207         rc.tempdir_path = internal_path(fromqstr(tempDirED->text()));
1208         rc.thesaurusdir_path = internal_path(fromqstr(thesaurusDirED->text()));
1209         rc.hunspelldir_path = internal_path(fromqstr(hunspellDirED->text()));
1210         rc.path_prefix = internal_path_list(fromqstr(pathPrefixED->text()));
1211         // FIXME: should be a checkbox only
1212         rc.lyxpipes = internal_path(fromqstr(lyxserverDirED->text()));
1213 }
1214
1215
1216 void PrefPaths::update(LyXRC const & rc)
1217 {
1218         workingDirED->setText(toqstr(external_path(rc.document_path)));
1219         exampleDirED->setText(toqstr(external_path(rc.example_path)));
1220         templateDirED->setText(toqstr(external_path(rc.template_path)));
1221         backupDirED->setText(toqstr(external_path(rc.backupdir_path)));
1222         tempDirED->setText(toqstr(external_path(rc.tempdir_path)));
1223         thesaurusDirED->setText(toqstr(external_path(rc.thesaurusdir_path)));
1224         hunspellDirED->setText(toqstr(external_path(rc.hunspelldir_path)));
1225         pathPrefixED->setText(toqstr(external_path_list(rc.path_prefix)));
1226         // FIXME: should be a checkbox only
1227         lyxserverDirED->setText(toqstr(external_path(rc.lyxpipes)));
1228 }
1229
1230
1231 void PrefPaths::selectExampledir()
1232 {
1233         QString file = browseDir(internalPath(exampleDirED->text()),
1234                 qt_("Select directory for example files"));
1235         if (!file.isEmpty())
1236                 exampleDirED->setText(file);
1237 }
1238
1239
1240 void PrefPaths::selectTemplatedir()
1241 {
1242         QString file = browseDir(internalPath(templateDirED->text()),
1243                 qt_("Select a document templates directory"));
1244         if (!file.isEmpty())
1245                 templateDirED->setText(file);
1246 }
1247
1248
1249 void PrefPaths::selectTempdir()
1250 {
1251         QString file = browseDir(internalPath(tempDirED->text()),
1252                 qt_("Select a temporary directory"));
1253         if (!file.isEmpty())
1254                 tempDirED->setText(file);
1255 }
1256
1257
1258 void PrefPaths::selectBackupdir()
1259 {
1260         QString file = browseDir(internalPath(backupDirED->text()),
1261                 qt_("Select a backups directory"));
1262         if (!file.isEmpty())
1263                 backupDirED->setText(file);
1264 }
1265
1266
1267 void PrefPaths::selectWorkingdir()
1268 {
1269         QString file = browseDir(internalPath(workingDirED->text()),
1270                 qt_("Select a document directory"));
1271         if (!file.isEmpty())
1272                 workingDirED->setText(file);
1273 }
1274
1275
1276 void PrefPaths::selectThesaurusdir()
1277 {
1278         QString file = browseDir(internalPath(thesaurusDirED->text()),
1279                 qt_("Set the path to the thesaurus dictionaries"));
1280         if (!file.isEmpty())
1281                 thesaurusDirED->setText(file);
1282 }
1283
1284
1285 void PrefPaths::selectHunspelldir()
1286 {
1287         QString file = browseDir(internalPath(hunspellDirED->text()),
1288                 qt_("Set the path to the Hunspell dictionaries"));
1289         if (!file.isEmpty())
1290                 hunspellDirED->setText(file);
1291 }
1292
1293
1294 void PrefPaths::selectLyxPipe()
1295 {
1296         QString file = form_->browse(internalPath(lyxserverDirED->text()),
1297                 qt_("Give a filename for the LyX server pipe"));
1298         if (!file.isEmpty())
1299                 lyxserverDirED->setText(file);
1300 }
1301
1302
1303 /////////////////////////////////////////////////////////////////////
1304 //
1305 // PrefSpellchecker
1306 //
1307 /////////////////////////////////////////////////////////////////////
1308
1309 PrefSpellchecker::PrefSpellchecker(GuiPreferences * form)
1310         : PrefModule(qt_(catLanguage), qt_("Spellchecker"), form)
1311 {
1312         setupUi(this);
1313
1314 #if defined(USE_ASPELL)
1315         spellcheckerCB->addItem(qt_("aspell"), QString("aspell"));
1316 #endif
1317 #if defined(USE_ENCHANT)
1318         spellcheckerCB->addItem(qt_("enchant"), QString("enchant"));
1319 #endif
1320 #if defined(USE_HUNSPELL)
1321         spellcheckerCB->addItem(qt_("hunspell"), QString("hunspell"));
1322 #endif
1323
1324         if (theSpellChecker()) {
1325                 connect(spellcheckerCB, SIGNAL(currentIndexChanged(int)),
1326                         this, SIGNAL(changed()));
1327                 connect(altLanguageED, SIGNAL(textChanged(QString)),
1328                         this, SIGNAL(changed()));
1329                 connect(escapeCharactersED, SIGNAL(textChanged(QString)),
1330                         this, SIGNAL(changed()));
1331                 connect(compoundWordCB, SIGNAL(clicked()),
1332                         this, SIGNAL(changed()));
1333                 connect(spellcheckContinuouslyCB, SIGNAL(clicked()),
1334                         this, SIGNAL(changed()));
1335                 connect(spellcheckNotesCB, SIGNAL(clicked()),
1336                         this, SIGNAL(changed()));
1337         } else {
1338                 spellcheckerCB->setEnabled(false);
1339                 altLanguageED->setEnabled(false);
1340                 escapeCharactersED->setEnabled(false);
1341                 compoundWordCB->setEnabled(false);
1342                 spellcheckContinuouslyCB->setEnabled(false);
1343                 spellcheckNotesCB->setEnabled(false);
1344         }
1345 }
1346
1347
1348 void PrefSpellchecker::apply(LyXRC & rc) const
1349 {
1350         rc.spellchecker = fromqstr(spellcheckerCB->itemData(
1351                         spellcheckerCB->currentIndex()).toString());
1352         rc.spellchecker_alt_lang = fromqstr(altLanguageED->text());
1353         rc.spellchecker_esc_chars = fromqstr(escapeCharactersED->text());
1354         rc.spellchecker_accept_compound = compoundWordCB->isChecked();
1355         rc.spellcheck_continuously = spellcheckContinuouslyCB->isChecked();
1356         rc.spellcheck_notes = spellcheckNotesCB->isChecked();
1357 }
1358
1359
1360 void PrefSpellchecker::update(LyXRC const & rc)
1361 {
1362         spellcheckerCB->setCurrentIndex(
1363                 spellcheckerCB->findData(toqstr(rc.spellchecker)));
1364         altLanguageED->setText(toqstr(rc.spellchecker_alt_lang));
1365         escapeCharactersED->setText(toqstr(rc.spellchecker_esc_chars));
1366         compoundWordCB->setChecked(rc.spellchecker_accept_compound);
1367         spellcheckContinuouslyCB->setChecked(rc.spellcheck_continuously);
1368         spellcheckNotesCB->setChecked(rc.spellcheck_notes);
1369 }
1370
1371
1372
1373 /////////////////////////////////////////////////////////////////////
1374 //
1375 // PrefConverters
1376 //
1377 /////////////////////////////////////////////////////////////////////
1378
1379
1380 PrefConverters::PrefConverters(GuiPreferences * form)
1381         : PrefModule(qt_(catFiles), qt_("Converters"), form)
1382 {
1383         setupUi(this);
1384
1385         connect(converterNewPB, SIGNAL(clicked()),
1386                 this, SLOT(updateConverter()));
1387         connect(converterRemovePB, SIGNAL(clicked()),
1388                 this, SLOT(removeConverter()));
1389         connect(converterModifyPB, SIGNAL(clicked()),
1390                 this, SLOT(updateConverter()));
1391         connect(convertersLW, SIGNAL(currentRowChanged(int)),
1392                 this, SLOT(switchConverter()));
1393         connect(converterFromCO, SIGNAL(activated(QString)),
1394                 this, SLOT(changeConverter()));
1395         connect(converterToCO, SIGNAL(activated(QString)),
1396                 this, SLOT(changeConverter()));
1397         connect(converterED, SIGNAL(textEdited(QString)),
1398                 this, SLOT(changeConverter()));
1399         connect(converterFlagED, SIGNAL(textEdited(QString)),
1400                 this, SLOT(changeConverter()));
1401         connect(converterNewPB, SIGNAL(clicked()),
1402                 this, SIGNAL(changed()));
1403         connect(converterRemovePB, SIGNAL(clicked()),
1404                 this, SIGNAL(changed()));
1405         connect(converterModifyPB, SIGNAL(clicked()),
1406                 this, SIGNAL(changed()));
1407         connect(maxAgeLE, SIGNAL(textEdited(QString)),
1408                 this, SIGNAL(changed()));
1409
1410         maxAgeLE->setValidator(new QDoubleValidator(maxAgeLE));
1411         //converterDefGB->setFocusProxy(convertersLW);
1412 }
1413
1414
1415 void PrefConverters::apply(LyXRC & rc) const
1416 {
1417         rc.use_converter_cache = cacheCB->isChecked();
1418         rc.converter_cache_maxage = int(widgetToDouble(maxAgeLE) * 86400.0);
1419 }
1420
1421
1422 void PrefConverters::update(LyXRC const & rc)
1423 {
1424         cacheCB->setChecked(rc.use_converter_cache);
1425         QString max_age;
1426         doubleToWidget(maxAgeLE, (double(rc.converter_cache_maxage) / 86400.0), 'g', 6);
1427         updateGui();
1428 }
1429
1430
1431 void PrefConverters::updateGui()
1432 {
1433         form_->formats().sort();
1434         form_->converters().update(form_->formats());
1435         // save current selection
1436         QString current = converterFromCO->currentText()
1437                 + " -> " + converterToCO->currentText();
1438
1439         converterFromCO->clear();
1440         converterToCO->clear();
1441
1442         Formats::const_iterator cit = form_->formats().begin();
1443         Formats::const_iterator end = form_->formats().end();
1444         for (; cit != end; ++cit) {
1445                 converterFromCO->addItem(qt_(cit->prettyname()));
1446                 converterToCO->addItem(qt_(cit->prettyname()));
1447         }
1448
1449         // currentRowChanged(int) is also triggered when updating the listwidget
1450         // block signals to avoid unnecessary calls to switchConverter()
1451         convertersLW->blockSignals(true);
1452         convertersLW->clear();
1453
1454         Converters::const_iterator ccit = form_->converters().begin();
1455         Converters::const_iterator cend = form_->converters().end();
1456         for (; ccit != cend; ++ccit) {
1457                 QString const name =
1458                         qt_(ccit->From->prettyname()) + " -> " + qt_(ccit->To->prettyname());
1459                 int type = form_->converters().getNumber(ccit->From->name(), ccit->To->name());
1460                 new QListWidgetItem(name, convertersLW, type);
1461         }
1462         convertersLW->sortItems(Qt::AscendingOrder);
1463         convertersLW->blockSignals(false);
1464
1465         // restore selection
1466         if (!current.isEmpty()) {
1467                 QList<QListWidgetItem *> const item =
1468                         convertersLW->findItems(current, Qt::MatchExactly);
1469                 if (!item.isEmpty())
1470                         convertersLW->setCurrentItem(item.at(0));
1471         }
1472
1473         // select first element if restoring failed
1474         if (convertersLW->currentRow() == -1)
1475                 convertersLW->setCurrentRow(0);
1476
1477         updateButtons();
1478 }
1479
1480
1481 void PrefConverters::switchConverter()
1482 {
1483         int const cnr = convertersLW->currentItem()->type();
1484         Converter const & c(form_->converters().get(cnr));
1485         converterFromCO->setCurrentIndex(form_->formats().getNumber(c.from));
1486         converterToCO->setCurrentIndex(form_->formats().getNumber(c.to));
1487         converterED->setText(toqstr(c.command));
1488         converterFlagED->setText(toqstr(c.flags));
1489
1490         updateButtons();
1491 }
1492
1493
1494 void PrefConverters::changeConverter()
1495 {
1496         updateButtons();
1497 }
1498
1499
1500 void PrefConverters::updateButtons()
1501 {
1502         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1503         Format const & to = form_->formats().get(converterToCO->currentIndex());
1504         int const sel = form_->converters().getNumber(from.name(), to.name());
1505         bool const known = sel >= 0;
1506         bool const valid = !(converterED->text().isEmpty()
1507                 || from.name() == to.name());
1508
1509         int const cnr = convertersLW->currentItem()->type();
1510         Converter const & c = form_->converters().get(cnr);
1511         string const old_command = c.command;
1512         string const old_flag = c.flags;
1513         string const new_command = fromqstr(converterED->text());
1514         string const new_flag = fromqstr(converterFlagED->text());
1515
1516         bool modified = (old_command != new_command || old_flag != new_flag);
1517
1518         converterModifyPB->setEnabled(valid && known && modified);
1519         converterNewPB->setEnabled(valid && !known);
1520         converterRemovePB->setEnabled(known);
1521
1522         maxAgeLE->setEnabled(cacheCB->isChecked());
1523         maxAgeLA->setEnabled(cacheCB->isChecked());
1524 }
1525
1526
1527 // FIXME: user must
1528 // specify unique from/to or it doesn't appear. This is really bad UI
1529 // this is why we can use the same function for both new and modify
1530 void PrefConverters::updateConverter()
1531 {
1532         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1533         Format const & to = form_->formats().get(converterToCO->currentIndex());
1534         string const flags = fromqstr(converterFlagED->text());
1535         string const command = fromqstr(converterED->text());
1536
1537         Converter const * old =
1538                 form_->converters().getConverter(from.name(), to.name());
1539         form_->converters().add(from.name(), to.name(), command, flags);
1540
1541         if (!old)
1542                 form_->converters().updateLast(form_->formats());
1543
1544         updateGui();
1545
1546         // Remove all files created by this converter from the cache, since
1547         // the modified converter might create different files.
1548         ConverterCache::get().remove_all(from.name(), to.name());
1549 }
1550
1551
1552 void PrefConverters::removeConverter()
1553 {
1554         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1555         Format const & to = form_->formats().get(converterToCO->currentIndex());
1556         form_->converters().erase(from.name(), to.name());
1557
1558         updateGui();
1559
1560         // Remove all files created by this converter from the cache, since
1561         // a possible new converter might create different files.
1562         ConverterCache::get().remove_all(from.name(), to.name());
1563 }
1564
1565
1566 void PrefConverters::on_cacheCB_stateChanged(int state)
1567 {
1568         maxAgeLE->setEnabled(state == Qt::Checked);
1569         maxAgeLA->setEnabled(state == Qt::Checked);
1570         changed();
1571 }
1572
1573
1574 /////////////////////////////////////////////////////////////////////
1575 //
1576 // FormatValidator
1577 //
1578 /////////////////////////////////////////////////////////////////////
1579
1580 class FormatValidator : public QValidator
1581 {
1582 public:
1583         FormatValidator(QWidget *, Formats const & f);
1584         void fixup(QString & input) const;
1585         QValidator::State validate(QString & input, int & pos) const;
1586 private:
1587         virtual QString toString(Format const & format) const = 0;
1588         int nr() const;
1589         Formats const & formats_;
1590 };
1591
1592
1593 FormatValidator::FormatValidator(QWidget * parent, Formats const & f)
1594         : QValidator(parent), formats_(f)
1595 {
1596 }
1597
1598
1599 void FormatValidator::fixup(QString & input) const
1600 {
1601         Formats::const_iterator cit = formats_.begin();
1602         Formats::const_iterator end = formats_.end();
1603         for (; cit != end; ++cit) {
1604                 QString const name = toString(*cit);
1605                 if (distance(formats_.begin(), cit) == nr()) {
1606                         input = name;
1607                         return;
1608                 }
1609         }
1610 }
1611
1612
1613 QValidator::State FormatValidator::validate(QString & input, int & /*pos*/) const
1614 {
1615         Formats::const_iterator cit = formats_.begin();
1616         Formats::const_iterator end = formats_.end();
1617         bool unknown = true;
1618         for (; unknown && cit != end; ++cit) {
1619                 QString const name = toString(*cit);
1620                 if (distance(formats_.begin(), cit) != nr())
1621                         unknown = name != input;
1622         }
1623
1624         if (unknown && !input.isEmpty())
1625                 return QValidator::Acceptable;
1626         else
1627                 return QValidator::Intermediate;
1628 }
1629
1630
1631 int FormatValidator::nr() const
1632 {
1633         QComboBox * p = qobject_cast<QComboBox *>(parent());
1634         return p->itemData(p->currentIndex()).toInt();
1635 }
1636
1637
1638 /////////////////////////////////////////////////////////////////////
1639 //
1640 // FormatNameValidator
1641 //
1642 /////////////////////////////////////////////////////////////////////
1643
1644 class FormatNameValidator : public FormatValidator
1645 {
1646 public:
1647         FormatNameValidator(QWidget * parent, Formats const & f)
1648                 : FormatValidator(parent, f)
1649         {}
1650 private:
1651         QString toString(Format const & format) const
1652         {
1653                 return toqstr(format.name());
1654         }
1655 };
1656
1657
1658 /////////////////////////////////////////////////////////////////////
1659 //
1660 // FormatPrettynameValidator
1661 //
1662 /////////////////////////////////////////////////////////////////////
1663
1664 class FormatPrettynameValidator : public FormatValidator
1665 {
1666 public:
1667         FormatPrettynameValidator(QWidget * parent, Formats const & f)
1668                 : FormatValidator(parent, f)
1669         {}
1670 private:
1671         QString toString(Format const & format) const
1672         {
1673                 return qt_(format.prettyname());
1674         }
1675 };
1676
1677
1678 /////////////////////////////////////////////////////////////////////
1679 //
1680 // PrefFileformats
1681 //
1682 /////////////////////////////////////////////////////////////////////
1683
1684 PrefFileformats::PrefFileformats(GuiPreferences * form)
1685         : PrefModule(qt_(catFiles), qt_("File formats"), form)
1686 {
1687         setupUi(this);
1688         formatED->setValidator(new FormatNameValidator(formatsCB, form_->formats()));
1689         formatsCB->setValidator(new FormatPrettynameValidator(formatsCB, form_->formats()));
1690
1691         connect(documentCB, SIGNAL(clicked()),
1692                 this, SLOT(setFlags()));
1693         connect(vectorCB, SIGNAL(clicked()),
1694                 this, SLOT(setFlags()));
1695         connect(formatsCB->lineEdit(), SIGNAL(editingFinished()),
1696                 this, SLOT(updatePrettyname()));
1697         connect(formatsCB->lineEdit(), SIGNAL(textEdited(QString)),
1698                 this, SIGNAL(changed()));
1699         connect(defaultFormatCB, SIGNAL(activated(QString)),
1700                 this, SIGNAL(changed()));
1701         connect(viewerCO, SIGNAL(activated(int)),
1702                 this, SIGNAL(changed()));
1703         connect(editorCO, SIGNAL(activated(int)),
1704                 this, SIGNAL(changed()));
1705 }
1706
1707
1708 namespace {
1709
1710 string const l10n_shortcut(string const prettyname, string const shortcut)
1711 {
1712         if (shortcut.empty())
1713                 return string();
1714
1715         string l10n_format =
1716                 to_utf8(_(prettyname + '|' + shortcut));
1717         return split(l10n_format, '|');
1718 }
1719
1720 }; // namespace anon
1721
1722
1723 void PrefFileformats::apply(LyXRC & rc) const
1724 {
1725         QString const default_format = defaultFormatCB->itemData(
1726                 defaultFormatCB->currentIndex()).toString();
1727         rc.default_view_format = fromqstr(default_format);
1728 }
1729
1730
1731 void PrefFileformats::update(LyXRC const & rc)
1732 {
1733         viewer_alternatives = rc.viewer_alternatives;
1734         editor_alternatives = rc.editor_alternatives;
1735         bool const init = defaultFormatCB->currentText().isEmpty();
1736         updateView();
1737         if (init) {
1738                 int const pos =
1739                         defaultFormatCB->findData(toqstr(rc.default_view_format));
1740                 defaultFormatCB->setCurrentIndex(pos);
1741         }
1742 }
1743
1744
1745 void PrefFileformats::updateView()
1746 {
1747         QString const current = formatsCB->currentText();
1748         QString const current_def = defaultFormatCB->currentText();
1749
1750         // update comboboxes with formats
1751         formatsCB->blockSignals(true);
1752         defaultFormatCB->blockSignals(true);
1753         formatsCB->clear();
1754         defaultFormatCB->clear();
1755         form_->formats().sort();
1756         Formats::const_iterator cit = form_->formats().begin();
1757         Formats::const_iterator end = form_->formats().end();
1758         for (; cit != end; ++cit) {
1759                 formatsCB->addItem(qt_(cit->prettyname()),
1760                                 QVariant(form_->formats().getNumber(cit->name())));
1761                 if (form_->converters().isReachable("latex", cit->name())
1762                     || form_->converters().isReachable("pdflatex", cit->name()))
1763                         defaultFormatCB->addItem(qt_(cit->prettyname()),
1764                                         QVariant(toqstr(cit->name())));
1765         }
1766
1767         // restore selection
1768         int item = formatsCB->findText(current, Qt::MatchExactly);
1769         formatsCB->setCurrentIndex(item < 0 ? 0 : item);
1770         on_formatsCB_currentIndexChanged(item < 0 ? 0 : item);
1771         item = defaultFormatCB->findText(current_def, Qt::MatchExactly);
1772         defaultFormatCB->setCurrentIndex(item < 0 ? 0 : item);
1773         formatsCB->blockSignals(false);
1774         defaultFormatCB->blockSignals(false);
1775 }
1776
1777
1778 void PrefFileformats::on_formatsCB_currentIndexChanged(int i)
1779 {
1780         int const nr = formatsCB->itemData(i).toInt();
1781         Format const f = form_->formats().get(nr);
1782
1783         formatED->setText(toqstr(f.name()));
1784         copierED->setText(toqstr(form_->movers().command(f.name())));
1785         extensionED->setText(toqstr(f.extension()));
1786         shortcutED->setText(
1787                 toqstr(l10n_shortcut(f.prettyname(), f.shortcut())));
1788         documentCB->setChecked((f.documentFormat()));
1789         vectorCB->setChecked((f.vectorFormat()));
1790         updateViewers();
1791         updateEditors();
1792 }
1793
1794
1795 void PrefFileformats::setFlags()
1796 {
1797         int flags = Format::none;
1798         if (documentCB->isChecked())
1799                 flags |= Format::document;
1800         if (vectorCB->isChecked())
1801                 flags |= Format::vector;
1802         currentFormat().setFlags(flags);
1803         changed();
1804 }
1805
1806
1807 void PrefFileformats::on_copierED_textEdited(const QString & s)
1808 {
1809         string const fmt = fromqstr(formatED->text());
1810         form_->movers().set(fmt, fromqstr(s));
1811         changed();
1812 }
1813
1814
1815 void PrefFileformats::on_extensionED_textEdited(const QString & s)
1816 {
1817         currentFormat().setExtension(fromqstr(s));
1818         changed();
1819 }
1820
1821 void PrefFileformats::on_viewerED_textEdited(const QString & s)
1822 {
1823         currentFormat().setViewer(fromqstr(s));
1824         changed();
1825 }
1826
1827
1828 void PrefFileformats::on_editorED_textEdited(const QString & s)
1829 {
1830         currentFormat().setEditor(fromqstr(s));
1831         changed();
1832 }
1833
1834
1835 void PrefFileformats::on_shortcutED_textEdited(const QString & s)
1836 {
1837         string const new_shortcut = fromqstr(s);
1838         if (new_shortcut == l10n_shortcut(currentFormat().prettyname(),
1839                                           currentFormat().shortcut()))
1840                 return;
1841         currentFormat().setShortcut(new_shortcut);
1842         changed();
1843 }
1844
1845
1846 void PrefFileformats::on_formatED_editingFinished()
1847 {
1848         string const newname = fromqstr(formatED->displayText());
1849         if (newname == currentFormat().name())
1850                 return;
1851
1852         currentFormat().setName(newname);
1853         changed();
1854 }
1855
1856
1857 void PrefFileformats::on_formatED_textChanged(const QString &)
1858 {
1859         QString t = formatED->text();
1860         int p = 0;
1861         bool valid = formatED->validator()->validate(t, p) == QValidator::Acceptable;
1862         setValid(formatLA, valid);
1863 }
1864
1865
1866 void PrefFileformats::on_formatsCB_editTextChanged(const QString &)
1867 {
1868         QString t = formatsCB->currentText();
1869         int p = 0;
1870         bool valid = formatsCB->validator()->validate(t, p) == QValidator::Acceptable;
1871         setValid(formatsLA, valid);
1872 }
1873
1874
1875 void PrefFileformats::updatePrettyname()
1876 {
1877         QString const newname = formatsCB->currentText();
1878         if (newname == qt_(currentFormat().prettyname()))
1879                 return;
1880
1881         currentFormat().setPrettyname(fromqstr(newname));
1882         formatsChanged();
1883         updateView();
1884         changed();
1885 }
1886
1887
1888 namespace {
1889         void updateComboBox(LyXRC::Alternatives const & alts,
1890                             string const & fmt, QComboBox * combo)
1891         {
1892                 LyXRC::Alternatives::const_iterator it = 
1893                                 alts.find(fmt);
1894                 if (it != alts.end()) {
1895                         LyXRC::CommandSet const & cmds = it->second;
1896                         LyXRC::CommandSet::const_iterator sit = 
1897                                         cmds.begin();
1898                         LyXRC::CommandSet::const_iterator const sen = 
1899                                         cmds.end();
1900                         for (; sit != sen; ++sit) {
1901                                 QString const qcmd = toqstr(*sit);
1902                                 combo->addItem(qcmd, qcmd);
1903                         }
1904                 }
1905         }
1906 }
1907
1908
1909 void PrefFileformats::updateViewers()
1910 {
1911         Format const f = currentFormat();
1912         viewerCO->blockSignals(true);
1913         viewerCO->clear();
1914         viewerCO->addItem(qt_("None"), QString());
1915         updateComboBox(viewer_alternatives, f.name(), viewerCO);
1916         viewerCO->addItem(qt_("Custom"), QString("custom viewer"));
1917         viewerCO->blockSignals(false);
1918
1919         int pos = viewerCO->findData(toqstr(f.viewer()));
1920         if (pos != -1) {
1921                 viewerED->clear();
1922                 viewerED->setEnabled(false);
1923                 viewerCO->setCurrentIndex(pos);
1924         } else {
1925                 viewerED->setEnabled(true);
1926                 viewerED->setText(toqstr(f.viewer()));
1927                 viewerCO->setCurrentIndex(viewerCO->findData(toqstr("custom viewer")));
1928         }
1929 }
1930
1931
1932 void PrefFileformats::updateEditors()
1933 {
1934         Format const f = currentFormat();
1935         editorCO->blockSignals(true);
1936         editorCO->clear();
1937         editorCO->addItem(qt_("None"), QString());
1938         updateComboBox(editor_alternatives, f.name(), editorCO);
1939         editorCO->addItem(qt_("Custom"), QString("custom editor"));
1940         editorCO->blockSignals(false);
1941
1942         int pos = editorCO->findData(toqstr(f.editor()));
1943         if (pos != -1) {
1944                 editorED->clear();
1945                 editorED->setEnabled(false);
1946                 editorCO->setCurrentIndex(pos);
1947         } else {
1948                 editorED->setEnabled(true);
1949                 editorED->setText(toqstr(f.editor()));
1950                 editorCO->setCurrentIndex(editorCO->findData(toqstr("custom editor")));
1951         }
1952 }
1953
1954
1955 void PrefFileformats::on_viewerCO_currentIndexChanged(int i)
1956 {
1957         bool const custom = viewerCO->itemData(i).toString() == "custom viewer";
1958         viewerED->setEnabled(custom);
1959         if (!custom)
1960                 currentFormat().setViewer(fromqstr(viewerCO->itemData(i).toString()));
1961 }
1962
1963
1964 void PrefFileformats::on_editorCO_currentIndexChanged(int i)
1965 {
1966         bool const custom = editorCO->itemData(i).toString() == "custom editor";
1967         editorED->setEnabled(custom);
1968         if (!custom)
1969                 currentFormat().setEditor(fromqstr(editorCO->itemData(i).toString()));
1970 }
1971
1972
1973 Format & PrefFileformats::currentFormat()
1974 {
1975         int const i = formatsCB->currentIndex();
1976         int const nr = formatsCB->itemData(i).toInt();
1977         return form_->formats().get(nr);
1978 }
1979
1980
1981 void PrefFileformats::on_formatNewPB_clicked()
1982 {
1983         form_->formats().add("", "", "", "", "", "", Format::none);
1984         updateView();
1985         formatsCB->setCurrentIndex(0);
1986         formatsCB->setFocus(Qt::OtherFocusReason);
1987 }
1988
1989
1990 void PrefFileformats::on_formatRemovePB_clicked()
1991 {
1992         int const i = formatsCB->currentIndex();
1993         int const nr = formatsCB->itemData(i).toInt();
1994         string const current_text = form_->formats().get(nr).name();
1995         if (form_->converters().formatIsUsed(current_text)) {
1996                 Alert::error(_("Format in use"),
1997                              _("Cannot remove a Format used by a Converter. "
1998                                             "Remove the converter first."));
1999                 return;
2000         }
2001
2002         form_->formats().erase(current_text);
2003         formatsChanged();
2004         updateView();
2005         on_formatsCB_editTextChanged(formatsCB->currentText());
2006         changed();
2007 }
2008
2009
2010 /////////////////////////////////////////////////////////////////////
2011 //
2012 // PrefLanguage
2013 //
2014 /////////////////////////////////////////////////////////////////////
2015
2016 PrefLanguage::PrefLanguage(GuiPreferences * form)
2017         : PrefModule(qt_(catLanguage), qt_("Language"), form)
2018 {
2019         setupUi(this);
2020
2021         connect(rtlGB, SIGNAL(clicked()),
2022                 this, SIGNAL(changed()));
2023         connect(visualCursorRB, SIGNAL(clicked()),
2024                 this, SIGNAL(changed()));
2025         connect(logicalCursorRB, SIGNAL(clicked()),
2026                 this, SIGNAL(changed()));
2027         connect(markForeignCB, SIGNAL(clicked()),
2028                 this, SIGNAL(changed()));
2029         connect(autoBeginCB, SIGNAL(clicked()),
2030                 this, SIGNAL(changed()));
2031         connect(autoEndCB, SIGNAL(clicked()),
2032                 this, SIGNAL(changed()));
2033         connect(useBabelCB, SIGNAL(clicked()),
2034                 this, SIGNAL(changed()));
2035         connect(globalCB, SIGNAL(clicked()),
2036                 this, SIGNAL(changed()));
2037         connect(languagePackageED, SIGNAL(textChanged(QString)),
2038                 this, SIGNAL(changed()));
2039         connect(startCommandED, SIGNAL(textChanged(QString)),
2040                 this, SIGNAL(changed()));
2041         connect(endCommandED, SIGNAL(textChanged(QString)),
2042                 this, SIGNAL(changed()));
2043         connect(uiLanguageCO, SIGNAL(activated(int)),
2044                 this, SIGNAL(changed()));
2045
2046         uiLanguageCO->clear();
2047
2048         QAbstractItemModel * language_model = guiApp->languageModel();
2049         // FIXME: it would be nice if sorting was enabled/disabled via a checkbox.
2050         language_model->sort(0);
2051
2052         // FIXME: This is wrong, we need filter this list based on the available
2053         // translation.
2054         uiLanguageCO->blockSignals(true);
2055         uiLanguageCO->addItem(qt_("Default"), toqstr("auto"));
2056         for (int i = 0; i != language_model->rowCount(); ++i) {
2057                 QModelIndex index = language_model->index(i, 0);
2058                 uiLanguageCO->addItem(index.data(Qt::DisplayRole).toString(),
2059                         index.data(Qt::UserRole).toString());
2060         }
2061         uiLanguageCO->blockSignals(false);
2062 }
2063
2064
2065 void PrefLanguage::on_uiLanguageCO_currentIndexChanged(int)
2066 {
2067          QMessageBox::information(this, qt_("LyX needs to be restarted!"),
2068                  qt_("The change of user interface language will be fully "
2069                  "effective only after a restart."));
2070 }
2071
2072
2073 void PrefLanguage::apply(LyXRC & rc) const
2074 {
2075         // FIXME: remove rtl_support bool
2076         rc.rtl_support = rtlGB->isChecked();
2077         rc.visual_cursor = rtlGB->isChecked() && visualCursorRB->isChecked();
2078         rc.mark_foreign_language = markForeignCB->isChecked();
2079         rc.language_auto_begin = autoBeginCB->isChecked();
2080         rc.language_auto_end = autoEndCB->isChecked();
2081         rc.language_use_babel = useBabelCB->isChecked();
2082         rc.language_global_options = globalCB->isChecked();
2083         rc.language_package = fromqstr(languagePackageED->text());
2084         rc.language_command_begin = fromqstr(startCommandED->text());
2085         rc.language_command_end = fromqstr(endCommandED->text());
2086         rc.gui_language = fromqstr(
2087                 uiLanguageCO->itemData(uiLanguageCO->currentIndex()).toString());
2088 }
2089
2090
2091 void PrefLanguage::update(LyXRC const & rc)
2092 {
2093         // FIXME: remove rtl_support bool
2094         rtlGB->setChecked(rc.rtl_support);
2095         if (rc.visual_cursor)
2096                 visualCursorRB->setChecked(true);
2097         else
2098                 logicalCursorRB->setChecked(true);
2099         markForeignCB->setChecked(rc.mark_foreign_language);
2100         autoBeginCB->setChecked(rc.language_auto_begin);
2101         autoEndCB->setChecked(rc.language_auto_end);
2102         useBabelCB->setChecked(rc.language_use_babel);
2103         globalCB->setChecked(rc.language_global_options);
2104         languagePackageED->setText(toqstr(rc.language_package));
2105         startCommandED->setText(toqstr(rc.language_command_begin));
2106         endCommandED->setText(toqstr(rc.language_command_end));
2107
2108         int pos = uiLanguageCO->findData(toqstr(rc.gui_language));
2109         uiLanguageCO->blockSignals(true);
2110         uiLanguageCO->setCurrentIndex(pos);
2111         uiLanguageCO->blockSignals(false);
2112 }
2113
2114
2115 /////////////////////////////////////////////////////////////////////
2116 //
2117 // PrefPrinter
2118 //
2119 /////////////////////////////////////////////////////////////////////
2120
2121 PrefPrinter::PrefPrinter(GuiPreferences * form)
2122         : PrefModule(qt_(catOutput), qt_("Printer"), form)
2123 {
2124         setupUi(this);
2125
2126         connect(printerAdaptCB, SIGNAL(clicked()),
2127                 this, SIGNAL(changed()));
2128         connect(printerCommandED, SIGNAL(textChanged(QString)),
2129                 this, SIGNAL(changed()));
2130         connect(printerNameED, SIGNAL(textChanged(QString)),
2131                 this, SIGNAL(changed()));
2132         connect(printerPageRangeED, SIGNAL(textChanged(QString)),
2133                 this, SIGNAL(changed()));
2134         connect(printerCopiesED, SIGNAL(textChanged(QString)),
2135                 this, SIGNAL(changed()));
2136         connect(printerReverseED, SIGNAL(textChanged(QString)),
2137                 this, SIGNAL(changed()));
2138         connect(printerToPrinterED, SIGNAL(textChanged(QString)),
2139                 this, SIGNAL(changed()));
2140         connect(printerExtensionED, SIGNAL(textChanged(QString)),
2141                 this, SIGNAL(changed()));
2142         connect(printerSpoolCommandED, SIGNAL(textChanged(QString)),
2143                 this, SIGNAL(changed()));
2144         connect(printerPaperTypeED, SIGNAL(textChanged(QString)),
2145                 this, SIGNAL(changed()));
2146         connect(printerEvenED, SIGNAL(textChanged(QString)),
2147                 this, SIGNAL(changed()));
2148         connect(printerOddED, SIGNAL(textChanged(QString)),
2149                 this, SIGNAL(changed()));
2150         connect(printerCollatedED, SIGNAL(textChanged(QString)),
2151                 this, SIGNAL(changed()));
2152         connect(printerLandscapeED, SIGNAL(textChanged(QString)),
2153                 this, SIGNAL(changed()));
2154         connect(printerToFileED, SIGNAL(textChanged(QString)),
2155                 this, SIGNAL(changed()));
2156         connect(printerExtraED, SIGNAL(textChanged(QString)),
2157                 this, SIGNAL(changed()));
2158         connect(printerSpoolPrefixED, SIGNAL(textChanged(QString)),
2159                 this, SIGNAL(changed()));
2160         connect(printerPaperSizeED, SIGNAL(textChanged(QString)),
2161                 this, SIGNAL(changed()));
2162 }
2163
2164
2165 void PrefPrinter::apply(LyXRC & rc) const
2166 {
2167         rc.print_adapt_output = printerAdaptCB->isChecked();
2168         rc.print_command = fromqstr(printerCommandED->text());
2169         rc.printer = fromqstr(printerNameED->text());
2170
2171         rc.print_pagerange_flag = fromqstr(printerPageRangeED->text());
2172         rc.print_copies_flag = fromqstr(printerCopiesED->text());
2173         rc.print_reverse_flag = fromqstr(printerReverseED->text());
2174         rc.print_to_printer = fromqstr(printerToPrinterED->text());
2175         rc.print_file_extension = fromqstr(printerExtensionED->text());
2176         rc.print_spool_command = fromqstr(printerSpoolCommandED->text());
2177         rc.print_paper_flag = fromqstr(printerPaperTypeED->text());
2178         rc.print_evenpage_flag = fromqstr(printerEvenED->text());
2179         rc.print_oddpage_flag = fromqstr(printerOddED->text());
2180         rc.print_collcopies_flag = fromqstr(printerCollatedED->text());
2181         rc.print_landscape_flag = fromqstr(printerLandscapeED->text());
2182         rc.print_to_file = internal_path(fromqstr(printerToFileED->text()));
2183         rc.print_extra_options = fromqstr(printerExtraED->text());
2184         rc.print_spool_printerprefix = fromqstr(printerSpoolPrefixED->text());
2185         rc.print_paper_dimension_flag = fromqstr(printerPaperSizeED->text());
2186 }
2187
2188
2189 void PrefPrinter::update(LyXRC const & rc)
2190 {
2191         printerAdaptCB->setChecked(rc.print_adapt_output);
2192         printerCommandED->setText(toqstr(rc.print_command));
2193         printerNameED->setText(toqstr(rc.printer));
2194
2195         printerPageRangeED->setText(toqstr(rc.print_pagerange_flag));
2196         printerCopiesED->setText(toqstr(rc.print_copies_flag));
2197         printerReverseED->setText(toqstr(rc.print_reverse_flag));
2198         printerToPrinterED->setText(toqstr(rc.print_to_printer));
2199         printerExtensionED->setText(toqstr(rc.print_file_extension));
2200         printerSpoolCommandED->setText(toqstr(rc.print_spool_command));
2201         printerPaperTypeED->setText(toqstr(rc.print_paper_flag));
2202         printerEvenED->setText(toqstr(rc.print_evenpage_flag));
2203         printerOddED->setText(toqstr(rc.print_oddpage_flag));
2204         printerCollatedED->setText(toqstr(rc.print_collcopies_flag));
2205         printerLandscapeED->setText(toqstr(rc.print_landscape_flag));
2206         printerToFileED->setText(toqstr(external_path(rc.print_to_file)));
2207         printerExtraED->setText(toqstr(rc.print_extra_options));
2208         printerSpoolPrefixED->setText(toqstr(rc.print_spool_printerprefix));
2209         printerPaperSizeED->setText(toqstr(rc.print_paper_dimension_flag));
2210 }
2211
2212
2213 /////////////////////////////////////////////////////////////////////
2214 //
2215 // PrefUserInterface
2216 //
2217 /////////////////////////////////////////////////////////////////////
2218
2219 PrefUserInterface::PrefUserInterface(GuiPreferences * form)
2220         : PrefModule(qt_(catLookAndFeel), qt_("User interface"), form)
2221 {
2222         setupUi(this);
2223
2224         connect(autoSaveCB, SIGNAL(toggled(bool)),
2225                 autoSaveSB, SLOT(setEnabled(bool)));
2226         connect(autoSaveCB, SIGNAL(toggled(bool)),
2227                 TextLabel1, SLOT(setEnabled(bool)));
2228         connect(openDocumentsInTabsCB, SIGNAL(clicked()),
2229                 this, SIGNAL(changed()));
2230 #if QT_VERSION < 0x040500
2231         singleCloseTabButtonCB->setEnabled(false);
2232 #endif
2233         connect(singleCloseTabButtonCB, SIGNAL(clicked()),
2234                 this, SIGNAL(changed()));
2235         connect(uiFilePB, SIGNAL(clicked()),
2236                 this, SLOT(selectUi()));
2237         connect(uiFileED, SIGNAL(textChanged(QString)),
2238                 this, SIGNAL(changed()));
2239         connect(restoreCursorCB, SIGNAL(clicked()),
2240                 this, SIGNAL(changed()));
2241         connect(loadSessionCB, SIGNAL(clicked()),
2242                 this, SIGNAL(changed()));
2243         connect(allowGeometrySessionCB, SIGNAL(clicked()),
2244                 this, SIGNAL(changed()));
2245         connect(autoSaveSB, SIGNAL(valueChanged(int)),
2246                 this, SIGNAL(changed()));
2247         connect(autoSaveCB, SIGNAL(clicked()),
2248                 this, SIGNAL(changed()));
2249         connect(backupCB, SIGNAL(clicked()),
2250                 this, SIGNAL(changed()));
2251         connect(lastfilesSB, SIGNAL(valueChanged(int)),
2252                 this, SIGNAL(changed()));
2253         connect(tooltipCB, SIGNAL(toggled(bool)),
2254                 this, SIGNAL(changed()));
2255         lastfilesSB->setMaximum(maxlastfiles);
2256 }
2257
2258
2259 void PrefUserInterface::apply(LyXRC & rc) const
2260 {
2261         rc.ui_file = internal_path(fromqstr(uiFileED->text()));
2262         rc.use_lastfilepos = restoreCursorCB->isChecked();
2263         rc.load_session = loadSessionCB->isChecked();
2264         rc.allow_geometry_session = allowGeometrySessionCB->isChecked();
2265         rc.autosave = autoSaveCB->isChecked()?  autoSaveSB->value() * 60 : 0;
2266         rc.make_backup = backupCB->isChecked();
2267         rc.num_lastfiles = lastfilesSB->value();
2268         rc.use_tooltip = tooltipCB->isChecked();
2269         rc.open_buffers_in_tabs = openDocumentsInTabsCB->isChecked();
2270         rc.single_close_tab_button = singleCloseTabButtonCB->isChecked();
2271 #if QT_VERSION < 0x040500
2272         rc.single_close_tab_button = true;
2273 #endif
2274 }
2275
2276
2277 void PrefUserInterface::update(LyXRC const & rc)
2278 {
2279         uiFileED->setText(toqstr(external_path(rc.ui_file)));
2280         restoreCursorCB->setChecked(rc.use_lastfilepos);
2281         loadSessionCB->setChecked(rc.load_session);
2282         allowGeometrySessionCB->setChecked(rc.allow_geometry_session);
2283         // convert to minutes
2284         bool autosave = rc.autosave > 0;
2285         int mins = rc.autosave / 60;
2286         if (!mins)
2287                 mins = 5;
2288         autoSaveSB->setValue(mins);
2289         autoSaveCB->setChecked(autosave);
2290         autoSaveSB->setEnabled(autosave);
2291         backupCB->setChecked(rc.make_backup);
2292         lastfilesSB->setValue(rc.num_lastfiles);
2293         tooltipCB->setChecked(rc.use_tooltip);
2294         openDocumentsInTabsCB->setChecked(rc.open_buffers_in_tabs);
2295         singleCloseTabButtonCB->setChecked(rc.single_close_tab_button);
2296 }
2297
2298
2299 void PrefUserInterface::selectUi()
2300 {
2301         QString file = form_->browseUI(internalPath(uiFileED->text()));
2302         if (!file.isEmpty())
2303                 uiFileED->setText(file);
2304 }
2305
2306
2307 void PrefUserInterface::on_clearSessionPB_clicked()
2308 {
2309         guiApp->clearSession();
2310 }
2311
2312
2313
2314 /////////////////////////////////////////////////////////////////////
2315 //
2316 // PrefEdit
2317 //
2318 /////////////////////////////////////////////////////////////////////
2319
2320 PrefEdit::PrefEdit(GuiPreferences * form)
2321         : PrefModule(qt_(catEditing), qt_("Control"), form)
2322 {
2323         setupUi(this);
2324
2325         connect(cursorFollowsCB, SIGNAL(clicked()),
2326                 this, SIGNAL(changed()));
2327         connect(scrollBelowCB, SIGNAL(clicked()),
2328                 this, SIGNAL(changed()));
2329         connect(sortEnvironmentsCB, SIGNAL(clicked()),
2330                 this, SIGNAL(changed()));
2331         connect(groupEnvironmentsCB, SIGNAL(clicked()),
2332                 this, SIGNAL(changed()));
2333         connect(macroEditStyleCO, SIGNAL(activated(int)),
2334                 this, SIGNAL(changed()));
2335         connect(fullscreenLimitGB, SIGNAL(clicked()),
2336                 this, SIGNAL(changed()));
2337         connect(fullscreenWidthSB, SIGNAL(valueChanged(int)),
2338                 this, SIGNAL(changed()));
2339         connect(toggleTabbarCB, SIGNAL(toggled(bool)),
2340                 this, SIGNAL(changed()));
2341         connect(toggleMenubarCB, SIGNAL(toggled(bool)),
2342                 this, SIGNAL(changed()));
2343         connect(toggleScrollbarCB, SIGNAL(toggled(bool)),
2344                 this, SIGNAL(changed()));
2345         connect(toggleToolbarsCB, SIGNAL(toggled(bool)),
2346                 this, SIGNAL(changed()));
2347 }
2348
2349
2350 void PrefEdit::apply(LyXRC & rc) const
2351 {
2352         rc.cursor_follows_scrollbar = cursorFollowsCB->isChecked();
2353         rc.scroll_below_document = scrollBelowCB->isChecked();
2354         rc.sort_layouts = sortEnvironmentsCB->isChecked();
2355         rc.group_layouts = groupEnvironmentsCB->isChecked();
2356         switch (macroEditStyleCO->currentIndex()) {
2357                 case 0: rc.macro_edit_style = LyXRC::MACRO_EDIT_INLINE_BOX; break;
2358                 case 1: rc.macro_edit_style = LyXRC::MACRO_EDIT_INLINE; break;
2359                 case 2: rc.macro_edit_style = LyXRC::MACRO_EDIT_LIST;   break;
2360         }
2361         rc.full_screen_toolbars = toggleToolbarsCB->isChecked();
2362         rc.full_screen_scrollbar = toggleScrollbarCB->isChecked();
2363         rc.full_screen_tabbar = toggleTabbarCB->isChecked();
2364         rc.full_screen_menubar = toggleMenubarCB->isChecked();
2365         rc.full_screen_width = fullscreenWidthSB->value();
2366         rc.full_screen_limit = fullscreenLimitGB->isChecked();
2367 }
2368
2369
2370 void PrefEdit::update(LyXRC const & rc)
2371 {
2372         cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
2373         scrollBelowCB->setChecked(rc.scroll_below_document);
2374         sortEnvironmentsCB->setChecked(rc.sort_layouts);
2375         groupEnvironmentsCB->setChecked(rc.group_layouts);
2376         macroEditStyleCO->setCurrentIndex(rc.macro_edit_style);
2377         toggleScrollbarCB->setChecked(rc.full_screen_scrollbar);
2378         toggleToolbarsCB->setChecked(rc.full_screen_toolbars);
2379         toggleTabbarCB->setChecked(rc.full_screen_tabbar);
2380         toggleMenubarCB->setChecked(rc.full_screen_menubar);
2381         fullscreenWidthSB->setValue(rc.full_screen_width);
2382         fullscreenLimitGB->setChecked(rc.full_screen_limit);
2383 }
2384
2385
2386 /////////////////////////////////////////////////////////////////////
2387 //
2388 // PrefShortcuts
2389 //
2390 /////////////////////////////////////////////////////////////////////
2391
2392
2393 GuiShortcutDialog::GuiShortcutDialog(QWidget * parent) : QDialog(parent)
2394 {
2395         Ui::shortcutUi::setupUi(this);
2396         QDialog::setModal(true);
2397 }
2398
2399
2400 PrefShortcuts::PrefShortcuts(GuiPreferences * form)
2401         : PrefModule(qt_(catEditing), qt_("Shortcuts"), form)
2402 {
2403         setupUi(this);
2404
2405         shortcutsTW->setColumnCount(2);
2406         shortcutsTW->headerItem()->setText(0, qt_("Function"));
2407         shortcutsTW->headerItem()->setText(1, qt_("Shortcut"));
2408         shortcutsTW->setSortingEnabled(true);
2409         // Multi-selection can be annoying.
2410         // shortcutsTW->setSelectionMode(QAbstractItemView::MultiSelection);
2411
2412         connect(bindFilePB, SIGNAL(clicked()),
2413                 this, SLOT(selectBind()));
2414         connect(bindFileED, SIGNAL(textChanged(QString)),
2415                 this, SIGNAL(changed()));
2416         connect(removePB, SIGNAL(clicked()),
2417                 this, SIGNAL(changed()));
2418
2419         shortcut_ = new GuiShortcutDialog(this);
2420         shortcut_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
2421         shortcut_bc_.setOK(shortcut_->okPB);
2422         shortcut_bc_.setCancel(shortcut_->cancelPB);
2423
2424         connect(shortcut_->okPB, SIGNAL(clicked()),
2425                 shortcut_, SLOT(accept()));
2426         connect(shortcut_->okPB, SIGNAL(clicked()),
2427                 this, SIGNAL(changed()));
2428         connect(shortcut_->cancelPB, SIGNAL(clicked()),
2429                 shortcut_, SLOT(reject()));
2430         connect(shortcut_->clearPB, SIGNAL(clicked()),
2431                 this, SLOT(shortcutClearPressed()));
2432         connect(shortcut_->removePB, SIGNAL(clicked()),
2433                 this, SLOT(shortcutRemovePressed()));
2434         connect(shortcut_->okPB, SIGNAL(clicked()),
2435                 this, SLOT(shortcutOkPressed()));
2436         connect(shortcut_->cancelPB, SIGNAL(clicked()),
2437                 this, SLOT(shortcutCancelPressed()));
2438 }
2439
2440
2441 void PrefShortcuts::apply(LyXRC & rc) const
2442 {
2443         rc.bind_file = internal_path(fromqstr(bindFileED->text()));
2444         // write user_bind and user_unbind to .lyx/bind/user.bind
2445         FileName bind_dir(addPath(package().user_support().absFilename(), "bind"));
2446         if (!bind_dir.exists() && !bind_dir.createDirectory(0777)) {
2447                 lyxerr << "LyX could not create the user bind directory '"
2448                        << bind_dir << "'. All user-defined key bindings will be lost." << endl;
2449                 return;
2450         }
2451         if (!bind_dir.isDirWritable()) {
2452                 lyxerr << "LyX could not write to the user bind directory '"
2453                        << bind_dir << "'. All user-defined key bindings will be lost." << endl;
2454                 return;
2455         }
2456         FileName user_bind_file(bind_dir.absFilename() + "/user.bind");
2457         user_unbind_.write(user_bind_file.toFilesystemEncoding(), false, true);
2458         user_bind_.write(user_bind_file.toFilesystemEncoding(), true, false);
2459         // immediately apply the keybindings. Why this is not done before?
2460         // The good thing is that the menus are updated automatically.
2461         theTopLevelKeymap().clear();
2462         theTopLevelKeymap().read("site");
2463         theTopLevelKeymap().read(rc.bind_file, 0, KeyMap::Fallback);
2464         theTopLevelKeymap().read("user", 0, KeyMap::MissingOK);
2465 }
2466
2467
2468 void PrefShortcuts::update(LyXRC const & rc)
2469 {
2470         bindFileED->setText(toqstr(external_path(rc.bind_file)));
2471         //
2472         system_bind_.clear();
2473         user_bind_.clear();
2474         user_unbind_.clear();
2475         system_bind_.read("site");
2476         system_bind_.read(rc.bind_file);
2477         // \unbind in user.bind is added to user_unbind_
2478         user_bind_.read("user", &user_unbind_, KeyMap::MissingOK);
2479         updateShortcutsTW();
2480 }
2481
2482
2483 void PrefShortcuts::updateShortcutsTW()
2484 {
2485         shortcutsTW->clear();
2486
2487         editItem_ = new QTreeWidgetItem(shortcutsTW);
2488         editItem_->setText(0, qt_("Cursor, Mouse and Editing functions"));
2489         editItem_->setFlags(editItem_->flags() & ~Qt::ItemIsSelectable);
2490
2491         mathItem_ = new QTreeWidgetItem(shortcutsTW);
2492         mathItem_->setText(0, qt_("Mathematical Symbols"));
2493         mathItem_->setFlags(mathItem_->flags() & ~Qt::ItemIsSelectable);
2494
2495         bufferItem_ = new QTreeWidgetItem(shortcutsTW);
2496         bufferItem_->setText(0, qt_("Document and Window"));
2497         bufferItem_->setFlags(bufferItem_->flags() & ~Qt::ItemIsSelectable);
2498
2499         layoutItem_ = new QTreeWidgetItem(shortcutsTW);
2500         layoutItem_->setText(0, qt_("Font, Layouts and Textclasses"));
2501         layoutItem_->setFlags(layoutItem_->flags() & ~Qt::ItemIsSelectable);
2502
2503         systemItem_ = new QTreeWidgetItem(shortcutsTW);
2504         systemItem_->setText(0, qt_("System and Miscellaneous"));
2505         systemItem_->setFlags(systemItem_->flags() & ~Qt::ItemIsSelectable);
2506
2507         // listBindings(unbound=true) lists all bound and unbound lfuns
2508         // Items in this list is tagged by its source.
2509         KeyMap::BindingList bindinglist = system_bind_.listBindings(true,
2510                 KeyMap::System);
2511         KeyMap::BindingList user_bindinglist = user_bind_.listBindings(false,
2512                 KeyMap::UserBind);
2513         KeyMap::BindingList user_unbindinglist = user_unbind_.listBindings(false,
2514                 KeyMap::UserUnbind);
2515         bindinglist.insert(bindinglist.end(), user_bindinglist.begin(),
2516                         user_bindinglist.end());
2517         bindinglist.insert(bindinglist.end(), user_unbindinglist.begin(),
2518                         user_unbindinglist.end());
2519
2520         KeyMap::BindingList::const_iterator it = bindinglist.begin();
2521         KeyMap::BindingList::const_iterator it_end = bindinglist.end();
2522         for (; it != it_end; ++it)
2523                 insertShortcutItem(it->request, it->sequence, KeyMap::ItemType(it->tag));
2524
2525         shortcutsTW->sortItems(0, Qt::AscendingOrder);
2526         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2527         removePB->setEnabled(!items.isEmpty() && !items[0]->text(1).isEmpty());
2528         modifyPB->setEnabled(!items.isEmpty());
2529
2530         shortcutsTW->resizeColumnToContents(0);
2531 }
2532
2533
2534 void PrefShortcuts::setItemType(QTreeWidgetItem * item, KeyMap::ItemType tag)
2535 {
2536         item->setData(0, Qt::UserRole, QVariant(tag));
2537         QFont font;
2538
2539         switch (tag) {
2540         case KeyMap::System:
2541                 break;
2542         case KeyMap::UserBind:
2543                 font.setBold(true);
2544                 break;
2545         case KeyMap::UserUnbind:
2546                 font.setStrikeOut(true);
2547                 break;
2548         // this item is not displayed now.
2549         case KeyMap::UserExtraUnbind:
2550                 font.setStrikeOut(true);
2551                 break;
2552         }
2553
2554         item->setFont(1, font);
2555 }
2556
2557
2558 QTreeWidgetItem * PrefShortcuts::insertShortcutItem(FuncRequest const & lfun,
2559                 KeySequence const & seq, KeyMap::ItemType tag)
2560 {
2561         FuncCode action = lfun.action;
2562         string const action_name = lyxaction.getActionName(action);
2563         QString const lfun_name = toqstr(from_utf8(action_name)
2564                         + ' ' + lfun.argument());
2565         QString const shortcut = toqstr(seq.print(KeySequence::ForGui));
2566         KeyMap::ItemType item_tag = tag;
2567
2568         QTreeWidgetItem * newItem = 0;
2569         // for unbind items, try to find an existing item in the system bind list
2570         if (tag == KeyMap::UserUnbind) {
2571                 QList<QTreeWidgetItem*> const items = shortcutsTW->findItems(lfun_name,
2572                         Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive), 0);
2573                 for (int i = 0; i < items.size(); ++i) {
2574                         if (items[i]->text(1) == shortcut)
2575                                 newItem = items[i];
2576                                 break;
2577                         }
2578                 // if not found, this unbind item is KeyMap::UserExtraUnbind
2579                 // Such an item is not displayed to avoid confusion (what is
2580                 // unmatched removed?).
2581                 if (!newItem) {
2582                         item_tag = KeyMap::UserExtraUnbind;
2583                         return 0;
2584                 }
2585         }
2586         if (!newItem) {
2587                 switch(lyxaction.getActionType(action)) {
2588                 case LyXAction::Hidden:
2589                         return 0;
2590                 case LyXAction::Edit:
2591                         newItem = new QTreeWidgetItem(editItem_);
2592                         break;
2593                 case LyXAction::Math:
2594                         newItem = new QTreeWidgetItem(mathItem_);
2595                         break;
2596                 case LyXAction::Buffer:
2597                         newItem = new QTreeWidgetItem(bufferItem_);
2598                         break;
2599                 case LyXAction::Layout:
2600                         newItem = new QTreeWidgetItem(layoutItem_);
2601                         break;
2602                 case LyXAction::System:
2603                         newItem = new QTreeWidgetItem(systemItem_);
2604                         break;
2605                 default:
2606                         // this should not happen
2607                         newItem = new QTreeWidgetItem(shortcutsTW);
2608                 }
2609         }
2610
2611         newItem->setText(0, lfun_name);
2612         newItem->setText(1, shortcut);
2613         // record BindFile representation to recover KeySequence when needed.
2614         newItem->setData(1, Qt::UserRole, toqstr(seq.print(KeySequence::BindFile)));
2615         setItemType(newItem, item_tag);
2616         return newItem;
2617 }
2618
2619
2620 void PrefShortcuts::on_shortcutsTW_itemSelectionChanged()
2621 {
2622         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2623         removePB->setEnabled(!items.isEmpty() && !items[0]->text(1).isEmpty());
2624         modifyPB->setEnabled(!items.isEmpty());
2625         if (items.isEmpty())
2626                 return;
2627
2628         KeyMap::ItemType tag = 
2629                 static_cast<KeyMap::ItemType>(items[0]->data(0, Qt::UserRole).toInt());
2630         if (tag == KeyMap::UserUnbind)
2631                 removePB->setText(qt_("Res&tore"));
2632         else
2633                 removePB->setText(qt_("Remo&ve"));
2634 }
2635
2636
2637 void PrefShortcuts::on_shortcutsTW_itemDoubleClicked()
2638 {
2639         modifyShortcut();
2640 }
2641
2642
2643 void PrefShortcuts::modifyShortcut()
2644 {
2645         QTreeWidgetItem * item = shortcutsTW->currentItem();
2646         if (item->flags() & Qt::ItemIsSelectable) {
2647                 shortcut_->lfunLE->setText(item->text(0));
2648                 save_lfun_ = item->text(0);
2649                 shortcut_->shortcutWG->setText(item->text(1));
2650                 KeySequence seq;
2651                 seq.parse(fromqstr(item->data(1, Qt::UserRole).toString()));
2652                 shortcut_->shortcutWG->setKeySequence(seq);
2653                 shortcut_->shortcutWG->setFocus();
2654                 shortcut_->exec();
2655         }
2656 }
2657
2658
2659 void PrefShortcuts::removeShortcut()
2660 {
2661         // it seems that only one item can be selected, but I am
2662         // removing all selected items anyway.
2663         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2664         for (int i = 0; i < items.size(); ++i) {
2665                 string shortcut = fromqstr(items[i]->data(1, Qt::UserRole).toString());
2666                 string lfun = fromqstr(items[i]->text(0));
2667                 FuncRequest func = lyxaction.lookupFunc(lfun);
2668                 KeyMap::ItemType tag = 
2669                         static_cast<KeyMap::ItemType>(items[i]->data(0, Qt::UserRole).toInt());
2670
2671                 switch (tag) {
2672                 case KeyMap::System: {
2673                         // for system bind, we do not touch the item
2674                         // but add an user unbind item
2675                         user_unbind_.bind(shortcut, func);
2676                         setItemType(items[i], KeyMap::UserUnbind);
2677                         removePB->setText(qt_("Res&tore"));
2678                         break;
2679                 }
2680                 case KeyMap::UserBind: {
2681                         // for user_bind, we remove this bind
2682                         QTreeWidgetItem * parent = items[i]->parent();
2683                         int itemIdx = parent->indexOfChild(items[i]);
2684                         parent->takeChild(itemIdx);
2685                         if (itemIdx > 0)
2686                                 shortcutsTW->scrollToItem(parent->child(itemIdx - 1));
2687                         else
2688                                 shortcutsTW->scrollToItem(parent);
2689                         user_bind_.unbind(shortcut, func);
2690                         break;
2691                 }
2692                 case KeyMap::UserUnbind: {
2693                         // for user_unbind, we remove the unbind, and the item
2694                         // become KeyMap::System again.
2695                         user_unbind_.unbind(shortcut, func);
2696                         setItemType(items[i], KeyMap::System);
2697                         removePB->setText(qt_("Remo&ve"));
2698                         break;
2699                 }
2700                 case KeyMap::UserExtraUnbind: {
2701                         // for user unbind that is not in system bind file,
2702                         // remove this unbind file
2703                         QTreeWidgetItem * parent = items[i]->parent();
2704                         parent->takeChild(parent->indexOfChild(items[i]));
2705                         user_unbind_.unbind(shortcut, func);
2706                 }
2707                 }
2708         }
2709 }
2710
2711
2712 void PrefShortcuts::selectBind()
2713 {
2714         QString file = form_->browsebind(internalPath(bindFileED->text()));
2715         if (!file.isEmpty()) {
2716                 bindFileED->setText(file);
2717                 system_bind_ = KeyMap();
2718                 system_bind_.read(fromqstr(file));
2719                 updateShortcutsTW();
2720         }
2721 }
2722
2723
2724 void PrefShortcuts::on_modifyPB_pressed()
2725 {
2726         modifyShortcut();
2727 }
2728
2729
2730 void PrefShortcuts::on_newPB_pressed()
2731 {
2732         shortcut_->lfunLE->clear();
2733         shortcut_->shortcutWG->reset();
2734         save_lfun_ = QString();
2735         shortcut_->exec();
2736 }
2737
2738
2739 void PrefShortcuts::on_removePB_pressed()
2740 {
2741         removeShortcut();
2742 }
2743
2744
2745 void PrefShortcuts::on_searchLE_textEdited()
2746 {
2747         if (searchLE->text().isEmpty()) {
2748                 // show all hidden items
2749                 QTreeWidgetItemIterator it(shortcutsTW, QTreeWidgetItemIterator::Hidden);
2750                 while (*it)
2751                         shortcutsTW->setItemHidden(*it++, false);
2752                 return;
2753         }
2754         // search both columns
2755         QList<QTreeWidgetItem *> matched = shortcutsTW->findItems(searchLE->text(),
2756                 Qt::MatchFlags(Qt::MatchContains | Qt::MatchRecursive), 0);
2757         matched += shortcutsTW->findItems(searchLE->text(),
2758                 Qt::MatchFlags(Qt::MatchContains | Qt::MatchRecursive), 1);
2759
2760         // hide everyone (to avoid searching in matched QList repeatedly
2761         QTreeWidgetItemIterator it(shortcutsTW, QTreeWidgetItemIterator::Selectable);
2762         while (*it)
2763                 shortcutsTW->setItemHidden(*it++, true);
2764         // show matched items
2765         for (int i = 0; i < matched.size(); ++i) {
2766                 shortcutsTW->setItemHidden(matched[i], false);
2767         shortcutsTW->setItemExpanded(matched[i]->parent(), true);
2768         }
2769 }
2770
2771
2772 docstring makeCmdString(FuncRequest const & f)
2773 {
2774         docstring actionStr = from_ascii(lyxaction.getActionName(f.action));
2775         if (!f.argument().empty())
2776                 actionStr += " " + f.argument();
2777         return actionStr;
2778 }
2779
2780
2781 void PrefShortcuts::shortcutOkPressed()
2782 {
2783         QString const new_lfun = shortcut_->lfunLE->text();
2784         FuncRequest func = lyxaction.lookupFunc(fromqstr(new_lfun));
2785
2786         if (func.action == LFUN_UNKNOWN_ACTION) {
2787                 Alert::error(_("Failed to create shortcut"),
2788                         _("Unknown or invalid LyX function"));
2789                 return;
2790         }
2791
2792         KeySequence k = shortcut_->shortcutWG->getKeySequence();
2793         if (k.length() == 0) {
2794                 Alert::error(_("Failed to create shortcut"),
2795                         _("Invalid or empty key sequence"));
2796                 return;
2797         }
2798
2799         // check to see if there's been any change
2800         FuncRequest oldBinding = system_bind_.getBinding(k);
2801         if (oldBinding.action == LFUN_UNKNOWN_ACTION)
2802                 oldBinding = user_bind_.getBinding(k);
2803         if (oldBinding == func) {
2804                 docstring const actionStr = makeCmdString(func);
2805                 Alert::error(_("Failed to create shortcut"),
2806                         bformat(_("Shortcut `%1$s' is already bound to:\n%2$s"), 
2807                         k.print(KeySequence::ForGui), actionStr));
2808                 return;
2809         }
2810         
2811         // make sure this key isn't already bound---and, if so, not unbound
2812         FuncCode const unbind = user_unbind_.getBinding(k).action;
2813         if (oldBinding.action != LFUN_UNKNOWN_ACTION && unbind == LFUN_UNKNOWN_ACTION)
2814         {
2815                 // FIXME Perhaps we should offer to over-write the old shortcut?
2816                 // If so, we'll need to remove it from our list, etc.
2817                 docstring const actionStr = makeCmdString(oldBinding);
2818                 Alert::error(_("Failed to create shortcut"),
2819                         bformat(_("Shortcut `%1$s' is already bound to:\n%2$s\n"
2820                           "You need to remove that binding before creating a new one."), 
2821                         k.print(KeySequence::ForGui), actionStr));
2822                 return;
2823         }
2824
2825         if (!save_lfun_.isEmpty() && new_lfun == save_lfun_)
2826                 // real modification of the lfun's shortcut,
2827                 // so remove the previous one
2828                 removeShortcut();
2829
2830         QTreeWidgetItem * item = insertShortcutItem(func, k, KeyMap::UserBind);
2831         if (item) {
2832                 user_bind_.bind(&k, func);
2833                 shortcutsTW->sortItems(0, Qt::AscendingOrder);
2834                 shortcutsTW->setItemExpanded(item->parent(), true);
2835                 shortcutsTW->scrollToItem(item);
2836         } else {
2837                 Alert::error(_("Failed to create shortcut"),
2838                         _("Can not insert shortcut to the list"));
2839                 return;
2840         }
2841 }
2842
2843
2844 void PrefShortcuts::shortcutCancelPressed()
2845 {
2846         shortcut_->shortcutWG->reset();
2847 }
2848
2849
2850 void PrefShortcuts::shortcutClearPressed()
2851 {
2852         shortcut_->shortcutWG->reset();
2853 }
2854
2855
2856 void PrefShortcuts::shortcutRemovePressed()
2857 {
2858         shortcut_->shortcutWG->removeFromSequence();
2859 }
2860
2861
2862 /////////////////////////////////////////////////////////////////////
2863 //
2864 // PrefIdentity
2865 //
2866 /////////////////////////////////////////////////////////////////////
2867
2868 PrefIdentity::PrefIdentity(GuiPreferences * form)
2869         : PrefModule(QString(), qt_("Identity"), form)
2870 {
2871         setupUi(this);
2872
2873         connect(nameED, SIGNAL(textChanged(QString)),
2874                 this, SIGNAL(changed()));
2875         connect(emailED, SIGNAL(textChanged(QString)),
2876                 this, SIGNAL(changed()));
2877 }
2878
2879
2880 void PrefIdentity::apply(LyXRC & rc) const
2881 {
2882         rc.user_name = fromqstr(nameED->text());
2883         rc.user_email = fromqstr(emailED->text());
2884 }
2885
2886
2887 void PrefIdentity::update(LyXRC const & rc)
2888 {
2889         nameED->setText(toqstr(rc.user_name));
2890         emailED->setText(toqstr(rc.user_email));
2891 }
2892
2893
2894
2895 /////////////////////////////////////////////////////////////////////
2896 //
2897 // GuiPreferences
2898 //
2899 /////////////////////////////////////////////////////////////////////
2900
2901 GuiPreferences::GuiPreferences(GuiView & lv)
2902         : GuiDialog(lv, "prefs", qt_("Preferences")), update_screen_font_(false)
2903 {
2904         setupUi(this);
2905
2906         QDialog::setModal(false);
2907
2908         connect(savePB, SIGNAL(clicked()), this, SLOT(slotOK()));
2909         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
2910         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
2911         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
2912
2913         addModule(new PrefUserInterface(this));
2914         addModule(new PrefEdit(this));
2915         addModule(new PrefShortcuts(this));
2916         addModule(new PrefScreenFonts(this));
2917         addModule(new PrefColors(this));
2918         addModule(new PrefDisplay(this));
2919         addModule(new PrefInput(this));
2920         addModule(new PrefCompletion(this));
2921
2922         addModule(new PrefPaths(this));
2923
2924         addModule(new PrefIdentity(this));
2925
2926         addModule(new PrefLanguage(this));
2927         addModule(new PrefSpellchecker(this));
2928
2929         addModule(new PrefPrinter(this));
2930         PrefDate * dateFormat = new PrefDate(this);
2931         addModule(dateFormat);
2932         addModule(new PrefPlaintext(this));
2933         addModule(new PrefLatex(this));
2934
2935         PrefConverters * converters = new PrefConverters(this);
2936         PrefFileformats * formats = new PrefFileformats(this);
2937         connect(formats, SIGNAL(formatsChanged()),
2938                         converters, SLOT(updateGui()));
2939         addModule(converters);
2940         addModule(formats);
2941
2942         prefsPS->setCurrentPanel(qt_("User interface"));
2943 // FIXME: hack to work around resizing bug in Qt >= 4.2
2944 // bug verified with Qt 4.2.{0-3} (JSpitzm)
2945 #if QT_VERSION >= 0x040200
2946         prefsPS->updateGeometry();
2947 #endif
2948
2949         bc().setPolicy(ButtonPolicy::PreferencesPolicy);
2950         bc().setOK(savePB);
2951         bc().setApply(applyPB);
2952         bc().setCancel(closePB);
2953         bc().setRestore(restorePB);
2954
2955         // initialize the strftime validator
2956         bc().addCheckedLineEdit(dateFormat->DateED);
2957 }
2958
2959
2960 void GuiPreferences::addModule(PrefModule * module)
2961 {
2962         LASSERT(module, return);
2963         if (module->category().isEmpty())
2964                 prefsPS->addPanel(module, module->title());
2965         else
2966                 prefsPS->addPanel(module, module->title(), module->category());
2967         connect(module, SIGNAL(changed()), this, SLOT(change_adaptor()));
2968         modules_.push_back(module);
2969 }
2970
2971
2972 void GuiPreferences::change_adaptor()
2973 {
2974         changed();
2975 }
2976
2977
2978 void GuiPreferences::apply(LyXRC & rc) const
2979 {
2980         size_t end = modules_.size();
2981         for (size_t i = 0; i != end; ++i)
2982                 modules_[i]->apply(rc);
2983 }
2984
2985
2986 void GuiPreferences::updateRc(LyXRC const & rc)
2987 {
2988         size_t const end = modules_.size();
2989         for (size_t i = 0; i != end; ++i)
2990                 modules_[i]->update(rc);
2991 }
2992
2993
2994 void GuiPreferences::applyView()
2995 {
2996         apply(rc());
2997 }
2998
2999 bool GuiPreferences::initialiseParams(string const &)
3000 {
3001         rc_ = lyxrc;
3002         formats_ = lyx::formats;
3003         converters_ = theConverters();
3004         converters_.update(formats_);
3005         movers_ = theMovers();
3006         colors_.clear();
3007         update_screen_font_ = false;
3008         
3009         updateRc(rc_);
3010         // Make sure that the bc is in the INITIAL state  
3011         if (bc().policy().buttonStatus(ButtonPolicy::RESTORE))  
3012                 bc().restore();  
3013
3014         return true;
3015 }
3016
3017
3018 void GuiPreferences::dispatchParams()
3019 {
3020         ostringstream ss;
3021         rc_.write(ss, true);
3022         dispatch(FuncRequest(LFUN_LYXRC_APPLY, ss.str()));
3023         // FIXME: these need lfuns
3024         // FIXME UNICODE
3025         theBufferList().setCurrentAuthor(from_utf8(rc_.user_name), from_utf8(rc_.user_email));
3026
3027         lyx::formats = formats_;
3028
3029         theConverters() = converters_;
3030         theConverters().update(lyx::formats);
3031         theConverters().buildGraph();
3032
3033         theMovers() = movers_;
3034
3035         vector<string>::const_iterator it = colors_.begin();
3036         vector<string>::const_iterator const end = colors_.end();
3037         for (; it != end; ++it)
3038                 dispatch(FuncRequest(LFUN_SET_COLOR, *it));
3039         colors_.clear();
3040
3041         if (update_screen_font_) {
3042                 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
3043                 update_screen_font_ = false;
3044         }
3045
3046         // The Save button has been pressed
3047         if (isClosing())
3048                 dispatch(FuncRequest(LFUN_PREFERENCES_SAVE));
3049 }
3050
3051
3052 void GuiPreferences::setColor(ColorCode col, QString const & hex)
3053 {
3054         colors_.push_back(lcolor.getLyXName(col) + ' ' + fromqstr(hex));
3055 }
3056
3057
3058 void GuiPreferences::updateScreenFonts()
3059 {
3060         update_screen_font_ = true;
3061 }
3062
3063
3064 QString GuiPreferences::browsebind(QString const & file) const
3065 {
3066         return browseLibFile("bind", file, "bind", qt_("Choose bind file"),
3067                              QStringList(qt_("LyX bind files (*.bind)")));
3068 }
3069
3070
3071 QString GuiPreferences::browseUI(QString const & file) const
3072 {
3073         return browseLibFile("ui", file, "ui", qt_("Choose UI file"),
3074                              QStringList(qt_("LyX UI files (*.ui)")));
3075 }
3076
3077
3078 QString GuiPreferences::browsekbmap(QString const & file) const
3079 {
3080         return browseLibFile("kbd", file, "kmap", qt_("Choose keyboard map"),
3081                              QStringList(qt_("LyX keyboard maps (*.kmap)")));
3082 }
3083
3084
3085 QString GuiPreferences::browse(QString const & file,
3086         QString const & title) const
3087 {
3088         return browseFile(file, title, QStringList(), true);
3089 }
3090
3091
3092 // We support less paper sizes than the document dialog
3093 // Therefore this adjustment is needed.
3094 PAPER_SIZE GuiPreferences::toPaperSize(int i) const
3095 {
3096         switch (i) {
3097         case 0:
3098                 return PAPER_DEFAULT;
3099         case 1:
3100                 return PAPER_USLETTER;
3101         case 2:
3102                 return PAPER_USLEGAL;
3103         case 3:
3104                 return PAPER_USEXECUTIVE;
3105         case 4:
3106                 return PAPER_A3;
3107         case 5:
3108                 return PAPER_A4;
3109         case 6:
3110                 return PAPER_A5;
3111         case 7:
3112                 return PAPER_B5;
3113         default:
3114                 // should not happen
3115                 return PAPER_DEFAULT;
3116         }
3117 }
3118
3119
3120 int GuiPreferences::fromPaperSize(PAPER_SIZE papersize) const
3121 {
3122         switch (papersize) {
3123         case PAPER_DEFAULT:
3124                 return 0;
3125         case PAPER_USLETTER:
3126                 return 1;
3127         case PAPER_USLEGAL:
3128                 return 2;
3129         case PAPER_USEXECUTIVE:
3130                 return 3;
3131         case PAPER_A3:
3132                 return 4;
3133         case PAPER_A4:
3134                 return 5;
3135         case PAPER_A5:
3136                 return 6;
3137         case PAPER_B5:
3138                 return 7;
3139         default:
3140                 // should not happen
3141                 return 0;
3142         }
3143 }
3144
3145
3146 Dialog * createGuiPreferences(GuiView & lv) { return new GuiPreferences(lv); }
3147
3148
3149 } // namespace frontend
3150 } // namespace lyx
3151
3152 #include "moc_GuiPrefs.cpp"