]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiPrefs.cpp
Fix bug #6617: Allowing "compressed documents" be the default.
[features.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 defined(USE_ASPELL) || defined(USE_ENCHANT) || defined(USE_HUNSPELL)
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         #endif
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(saveCompressedCB, SIGNAL(clicked()),
2252                 this, SIGNAL(changed()));
2253         connect(lastfilesSB, SIGNAL(valueChanged(int)),
2254                 this, SIGNAL(changed()));
2255         connect(tooltipCB, SIGNAL(toggled(bool)),
2256                 this, SIGNAL(changed()));
2257         lastfilesSB->setMaximum(maxlastfiles);
2258 }
2259
2260
2261 void PrefUserInterface::apply(LyXRC & rc) const
2262 {
2263         rc.ui_file = internal_path(fromqstr(uiFileED->text()));
2264         rc.use_lastfilepos = restoreCursorCB->isChecked();
2265         rc.load_session = loadSessionCB->isChecked();
2266         rc.allow_geometry_session = allowGeometrySessionCB->isChecked();
2267         rc.autosave = autoSaveCB->isChecked() ?  autoSaveSB->value() * 60 : 0;
2268         rc.make_backup = backupCB->isChecked();
2269         rc.save_compressed = saveCompressedCB->isChecked();
2270         rc.num_lastfiles = lastfilesSB->value();
2271         rc.use_tooltip = tooltipCB->isChecked();
2272         rc.open_buffers_in_tabs = openDocumentsInTabsCB->isChecked();
2273         rc.single_close_tab_button = singleCloseTabButtonCB->isChecked();
2274 #if QT_VERSION < 0x040500
2275         rc.single_close_tab_button = true;
2276 #endif
2277 }
2278
2279
2280 void PrefUserInterface::update(LyXRC const & rc)
2281 {
2282         uiFileED->setText(toqstr(external_path(rc.ui_file)));
2283         restoreCursorCB->setChecked(rc.use_lastfilepos);
2284         loadSessionCB->setChecked(rc.load_session);
2285         allowGeometrySessionCB->setChecked(rc.allow_geometry_session);
2286         // convert to minutes
2287         bool autosave = rc.autosave > 0;
2288         int mins = rc.autosave / 60;
2289         if (!mins)
2290                 mins = 5;
2291         autoSaveSB->setValue(mins);
2292         autoSaveCB->setChecked(autosave);
2293         autoSaveSB->setEnabled(autosave);
2294         backupCB->setChecked(rc.make_backup);
2295         saveCompressedCB->setChecked(rc.save_compressed);
2296         lastfilesSB->setValue(rc.num_lastfiles);
2297         tooltipCB->setChecked(rc.use_tooltip);
2298         openDocumentsInTabsCB->setChecked(rc.open_buffers_in_tabs);
2299         singleCloseTabButtonCB->setChecked(rc.single_close_tab_button);
2300 }
2301
2302
2303 void PrefUserInterface::selectUi()
2304 {
2305         QString file = form_->browseUI(internalPath(uiFileED->text()));
2306         if (!file.isEmpty())
2307                 uiFileED->setText(file);
2308 }
2309
2310
2311 void PrefUserInterface::on_clearSessionPB_clicked()
2312 {
2313         guiApp->clearSession();
2314 }
2315
2316
2317
2318 /////////////////////////////////////////////////////////////////////
2319 //
2320 // PrefEdit
2321 //
2322 /////////////////////////////////////////////////////////////////////
2323
2324 PrefEdit::PrefEdit(GuiPreferences * form)
2325         : PrefModule(qt_(catEditing), qt_("Control"), form)
2326 {
2327         setupUi(this);
2328
2329         connect(cursorFollowsCB, SIGNAL(clicked()),
2330                 this, SIGNAL(changed()));
2331         connect(scrollBelowCB, SIGNAL(clicked()),
2332                 this, SIGNAL(changed()));
2333         connect(sortEnvironmentsCB, SIGNAL(clicked()),
2334                 this, SIGNAL(changed()));
2335         connect(groupEnvironmentsCB, SIGNAL(clicked()),
2336                 this, SIGNAL(changed()));
2337         connect(macroEditStyleCO, SIGNAL(activated(int)),
2338                 this, SIGNAL(changed()));
2339         connect(fullscreenLimitGB, SIGNAL(clicked()),
2340                 this, SIGNAL(changed()));
2341         connect(fullscreenWidthSB, SIGNAL(valueChanged(int)),
2342                 this, SIGNAL(changed()));
2343         connect(toggleTabbarCB, SIGNAL(toggled(bool)),
2344                 this, SIGNAL(changed()));
2345         connect(toggleMenubarCB, SIGNAL(toggled(bool)),
2346                 this, SIGNAL(changed()));
2347         connect(toggleScrollbarCB, SIGNAL(toggled(bool)),
2348                 this, SIGNAL(changed()));
2349         connect(toggleToolbarsCB, SIGNAL(toggled(bool)),
2350                 this, SIGNAL(changed()));
2351 }
2352
2353
2354 void PrefEdit::apply(LyXRC & rc) const
2355 {
2356         rc.cursor_follows_scrollbar = cursorFollowsCB->isChecked();
2357         rc.scroll_below_document = scrollBelowCB->isChecked();
2358         rc.sort_layouts = sortEnvironmentsCB->isChecked();
2359         rc.group_layouts = groupEnvironmentsCB->isChecked();
2360         switch (macroEditStyleCO->currentIndex()) {
2361                 case 0: rc.macro_edit_style = LyXRC::MACRO_EDIT_INLINE_BOX; break;
2362                 case 1: rc.macro_edit_style = LyXRC::MACRO_EDIT_INLINE; break;
2363                 case 2: rc.macro_edit_style = LyXRC::MACRO_EDIT_LIST;   break;
2364         }
2365         rc.full_screen_toolbars = toggleToolbarsCB->isChecked();
2366         rc.full_screen_scrollbar = toggleScrollbarCB->isChecked();
2367         rc.full_screen_tabbar = toggleTabbarCB->isChecked();
2368         rc.full_screen_menubar = toggleMenubarCB->isChecked();
2369         rc.full_screen_width = fullscreenWidthSB->value();
2370         rc.full_screen_limit = fullscreenLimitGB->isChecked();
2371 }
2372
2373
2374 void PrefEdit::update(LyXRC const & rc)
2375 {
2376         cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
2377         scrollBelowCB->setChecked(rc.scroll_below_document);
2378         sortEnvironmentsCB->setChecked(rc.sort_layouts);
2379         groupEnvironmentsCB->setChecked(rc.group_layouts);
2380         macroEditStyleCO->setCurrentIndex(rc.macro_edit_style);
2381         toggleScrollbarCB->setChecked(rc.full_screen_scrollbar);
2382         toggleToolbarsCB->setChecked(rc.full_screen_toolbars);
2383         toggleTabbarCB->setChecked(rc.full_screen_tabbar);
2384         toggleMenubarCB->setChecked(rc.full_screen_menubar);
2385         fullscreenWidthSB->setValue(rc.full_screen_width);
2386         fullscreenLimitGB->setChecked(rc.full_screen_limit);
2387 }
2388
2389
2390 /////////////////////////////////////////////////////////////////////
2391 //
2392 // PrefShortcuts
2393 //
2394 /////////////////////////////////////////////////////////////////////
2395
2396
2397 GuiShortcutDialog::GuiShortcutDialog(QWidget * parent) : QDialog(parent)
2398 {
2399         Ui::shortcutUi::setupUi(this);
2400         QDialog::setModal(true);
2401 }
2402
2403
2404 PrefShortcuts::PrefShortcuts(GuiPreferences * form)
2405         : PrefModule(qt_(catEditing), qt_("Shortcuts"), form)
2406 {
2407         setupUi(this);
2408
2409         shortcutsTW->setColumnCount(2);
2410         shortcutsTW->headerItem()->setText(0, qt_("Function"));
2411         shortcutsTW->headerItem()->setText(1, qt_("Shortcut"));
2412         shortcutsTW->setSortingEnabled(true);
2413         // Multi-selection can be annoying.
2414         // shortcutsTW->setSelectionMode(QAbstractItemView::MultiSelection);
2415
2416         connect(bindFilePB, SIGNAL(clicked()),
2417                 this, SLOT(selectBind()));
2418         connect(bindFileED, SIGNAL(textChanged(QString)),
2419                 this, SIGNAL(changed()));
2420         connect(removePB, SIGNAL(clicked()),
2421                 this, SIGNAL(changed()));
2422
2423         shortcut_ = new GuiShortcutDialog(this);
2424         shortcut_bc_.setPolicy(ButtonPolicy::OkCancelPolicy);
2425         shortcut_bc_.setOK(shortcut_->okPB);
2426         shortcut_bc_.setCancel(shortcut_->cancelPB);
2427
2428         connect(shortcut_->okPB, SIGNAL(clicked()),
2429                 shortcut_, SLOT(accept()));
2430         connect(shortcut_->okPB, SIGNAL(clicked()),
2431                 this, SIGNAL(changed()));
2432         connect(shortcut_->cancelPB, SIGNAL(clicked()),
2433                 shortcut_, SLOT(reject()));
2434         connect(shortcut_->clearPB, SIGNAL(clicked()),
2435                 this, SLOT(shortcutClearPressed()));
2436         connect(shortcut_->removePB, SIGNAL(clicked()),
2437                 this, SLOT(shortcutRemovePressed()));
2438         connect(shortcut_->okPB, SIGNAL(clicked()),
2439                 this, SLOT(shortcutOkPressed()));
2440         connect(shortcut_->cancelPB, SIGNAL(clicked()),
2441                 this, SLOT(shortcutCancelPressed()));
2442 }
2443
2444
2445 void PrefShortcuts::apply(LyXRC & rc) const
2446 {
2447         rc.bind_file = internal_path(fromqstr(bindFileED->text()));
2448         // write user_bind and user_unbind to .lyx/bind/user.bind
2449         FileName bind_dir(addPath(package().user_support().absFilename(), "bind"));
2450         if (!bind_dir.exists() && !bind_dir.createDirectory(0777)) {
2451                 lyxerr << "LyX could not create the user bind directory '"
2452                        << bind_dir << "'. All user-defined key bindings will be lost." << endl;
2453                 return;
2454         }
2455         if (!bind_dir.isDirWritable()) {
2456                 lyxerr << "LyX could not write to the user bind directory '"
2457                        << bind_dir << "'. All user-defined key bindings will be lost." << endl;
2458                 return;
2459         }
2460         FileName user_bind_file(bind_dir.absFilename() + "/user.bind");
2461         user_unbind_.write(user_bind_file.toFilesystemEncoding(), false, true);
2462         user_bind_.write(user_bind_file.toFilesystemEncoding(), true, false);
2463         // immediately apply the keybindings. Why this is not done before?
2464         // The good thing is that the menus are updated automatically.
2465         theTopLevelKeymap().clear();
2466         theTopLevelKeymap().read("site");
2467         theTopLevelKeymap().read(rc.bind_file, 0, KeyMap::Fallback);
2468         theTopLevelKeymap().read("user", 0, KeyMap::MissingOK);
2469 }
2470
2471
2472 void PrefShortcuts::update(LyXRC const & rc)
2473 {
2474         bindFileED->setText(toqstr(external_path(rc.bind_file)));
2475         //
2476         system_bind_.clear();
2477         user_bind_.clear();
2478         user_unbind_.clear();
2479         system_bind_.read("site");
2480         system_bind_.read(rc.bind_file);
2481         // \unbind in user.bind is added to user_unbind_
2482         user_bind_.read("user", &user_unbind_, KeyMap::MissingOK);
2483         updateShortcutsTW();
2484 }
2485
2486
2487 void PrefShortcuts::updateShortcutsTW()
2488 {
2489         shortcutsTW->clear();
2490
2491         editItem_ = new QTreeWidgetItem(shortcutsTW);
2492         editItem_->setText(0, qt_("Cursor, Mouse and Editing functions"));
2493         editItem_->setFlags(editItem_->flags() & ~Qt::ItemIsSelectable);
2494
2495         mathItem_ = new QTreeWidgetItem(shortcutsTW);
2496         mathItem_->setText(0, qt_("Mathematical Symbols"));
2497         mathItem_->setFlags(mathItem_->flags() & ~Qt::ItemIsSelectable);
2498
2499         bufferItem_ = new QTreeWidgetItem(shortcutsTW);
2500         bufferItem_->setText(0, qt_("Document and Window"));
2501         bufferItem_->setFlags(bufferItem_->flags() & ~Qt::ItemIsSelectable);
2502
2503         layoutItem_ = new QTreeWidgetItem(shortcutsTW);
2504         layoutItem_->setText(0, qt_("Font, Layouts and Textclasses"));
2505         layoutItem_->setFlags(layoutItem_->flags() & ~Qt::ItemIsSelectable);
2506
2507         systemItem_ = new QTreeWidgetItem(shortcutsTW);
2508         systemItem_->setText(0, qt_("System and Miscellaneous"));
2509         systemItem_->setFlags(systemItem_->flags() & ~Qt::ItemIsSelectable);
2510
2511         // listBindings(unbound=true) lists all bound and unbound lfuns
2512         // Items in this list is tagged by its source.
2513         KeyMap::BindingList bindinglist = system_bind_.listBindings(true,
2514                 KeyMap::System);
2515         KeyMap::BindingList user_bindinglist = user_bind_.listBindings(false,
2516                 KeyMap::UserBind);
2517         KeyMap::BindingList user_unbindinglist = user_unbind_.listBindings(false,
2518                 KeyMap::UserUnbind);
2519         bindinglist.insert(bindinglist.end(), user_bindinglist.begin(),
2520                         user_bindinglist.end());
2521         bindinglist.insert(bindinglist.end(), user_unbindinglist.begin(),
2522                         user_unbindinglist.end());
2523
2524         KeyMap::BindingList::const_iterator it = bindinglist.begin();
2525         KeyMap::BindingList::const_iterator it_end = bindinglist.end();
2526         for (; it != it_end; ++it)
2527                 insertShortcutItem(it->request, it->sequence, KeyMap::ItemType(it->tag));
2528
2529         shortcutsTW->sortItems(0, Qt::AscendingOrder);
2530         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2531         removePB->setEnabled(!items.isEmpty() && !items[0]->text(1).isEmpty());
2532         modifyPB->setEnabled(!items.isEmpty());
2533
2534         shortcutsTW->resizeColumnToContents(0);
2535 }
2536
2537
2538 void PrefShortcuts::setItemType(QTreeWidgetItem * item, KeyMap::ItemType tag)
2539 {
2540         item->setData(0, Qt::UserRole, QVariant(tag));
2541         QFont font;
2542
2543         switch (tag) {
2544         case KeyMap::System:
2545                 break;
2546         case KeyMap::UserBind:
2547                 font.setBold(true);
2548                 break;
2549         case KeyMap::UserUnbind:
2550                 font.setStrikeOut(true);
2551                 break;
2552         // this item is not displayed now.
2553         case KeyMap::UserExtraUnbind:
2554                 font.setStrikeOut(true);
2555                 break;
2556         }
2557
2558         item->setFont(1, font);
2559 }
2560
2561
2562 QTreeWidgetItem * PrefShortcuts::insertShortcutItem(FuncRequest const & lfun,
2563                 KeySequence const & seq, KeyMap::ItemType tag)
2564 {
2565         FuncCode action = lfun.action;
2566         string const action_name = lyxaction.getActionName(action);
2567         QString const lfun_name = toqstr(from_utf8(action_name)
2568                         + ' ' + lfun.argument());
2569         QString const shortcut = toqstr(seq.print(KeySequence::ForGui));
2570         KeyMap::ItemType item_tag = tag;
2571
2572         QTreeWidgetItem * newItem = 0;
2573         // for unbind items, try to find an existing item in the system bind list
2574         if (tag == KeyMap::UserUnbind) {
2575                 QList<QTreeWidgetItem*> const items = shortcutsTW->findItems(lfun_name,
2576                         Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive), 0);
2577                 for (int i = 0; i < items.size(); ++i) {
2578                         if (items[i]->text(1) == shortcut)
2579                                 newItem = items[i];
2580                                 break;
2581                         }
2582                 // if not found, this unbind item is KeyMap::UserExtraUnbind
2583                 // Such an item is not displayed to avoid confusion (what is
2584                 // unmatched removed?).
2585                 if (!newItem) {
2586                         item_tag = KeyMap::UserExtraUnbind;
2587                         return 0;
2588                 }
2589         }
2590         if (!newItem) {
2591                 switch(lyxaction.getActionType(action)) {
2592                 case LyXAction::Hidden:
2593                         return 0;
2594                 case LyXAction::Edit:
2595                         newItem = new QTreeWidgetItem(editItem_);
2596                         break;
2597                 case LyXAction::Math:
2598                         newItem = new QTreeWidgetItem(mathItem_);
2599                         break;
2600                 case LyXAction::Buffer:
2601                         newItem = new QTreeWidgetItem(bufferItem_);
2602                         break;
2603                 case LyXAction::Layout:
2604                         newItem = new QTreeWidgetItem(layoutItem_);
2605                         break;
2606                 case LyXAction::System:
2607                         newItem = new QTreeWidgetItem(systemItem_);
2608                         break;
2609                 default:
2610                         // this should not happen
2611                         newItem = new QTreeWidgetItem(shortcutsTW);
2612                 }
2613         }
2614
2615         newItem->setText(0, lfun_name);
2616         newItem->setText(1, shortcut);
2617         // record BindFile representation to recover KeySequence when needed.
2618         newItem->setData(1, Qt::UserRole, toqstr(seq.print(KeySequence::BindFile)));
2619         setItemType(newItem, item_tag);
2620         return newItem;
2621 }
2622
2623
2624 void PrefShortcuts::on_shortcutsTW_itemSelectionChanged()
2625 {
2626         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2627         removePB->setEnabled(!items.isEmpty() && !items[0]->text(1).isEmpty());
2628         modifyPB->setEnabled(!items.isEmpty());
2629         if (items.isEmpty())
2630                 return;
2631
2632         KeyMap::ItemType tag = 
2633                 static_cast<KeyMap::ItemType>(items[0]->data(0, Qt::UserRole).toInt());
2634         if (tag == KeyMap::UserUnbind)
2635                 removePB->setText(qt_("Res&tore"));
2636         else
2637                 removePB->setText(qt_("Remo&ve"));
2638 }
2639
2640
2641 void PrefShortcuts::on_shortcutsTW_itemDoubleClicked()
2642 {
2643         modifyShortcut();
2644 }
2645
2646
2647 void PrefShortcuts::modifyShortcut()
2648 {
2649         QTreeWidgetItem * item = shortcutsTW->currentItem();
2650         if (item->flags() & Qt::ItemIsSelectable) {
2651                 shortcut_->lfunLE->setText(item->text(0));
2652                 save_lfun_ = item->text(0).trimmed();
2653                 shortcut_->shortcutWG->setText(item->text(1));
2654                 KeySequence seq;
2655                 seq.parse(fromqstr(item->data(1, Qt::UserRole).toString()));
2656                 shortcut_->shortcutWG->setKeySequence(seq);
2657                 shortcut_->shortcutWG->setFocus();
2658                 shortcut_->exec();
2659         }
2660 }
2661
2662
2663 void PrefShortcuts::removeShortcut()
2664 {
2665         // it seems that only one item can be selected, but I am
2666         // removing all selected items anyway.
2667         QList<QTreeWidgetItem*> items = shortcutsTW->selectedItems();
2668         for (int i = 0; i < items.size(); ++i) {
2669                 string shortcut = fromqstr(items[i]->data(1, Qt::UserRole).toString());
2670                 string lfun = fromqstr(items[i]->text(0));
2671                 FuncRequest func = lyxaction.lookupFunc(lfun);
2672                 KeyMap::ItemType tag = 
2673                         static_cast<KeyMap::ItemType>(items[i]->data(0, Qt::UserRole).toInt());
2674
2675                 switch (tag) {
2676                 case KeyMap::System: {
2677                         // for system bind, we do not touch the item
2678                         // but add an user unbind item
2679                         user_unbind_.bind(shortcut, func);
2680                         setItemType(items[i], KeyMap::UserUnbind);
2681                         removePB->setText(qt_("Res&tore"));
2682                         break;
2683                 }
2684                 case KeyMap::UserBind: {
2685                         // for user_bind, we remove this bind
2686                         QTreeWidgetItem * parent = items[i]->parent();
2687                         int itemIdx = parent->indexOfChild(items[i]);
2688                         parent->takeChild(itemIdx);
2689                         if (itemIdx > 0)
2690                                 shortcutsTW->scrollToItem(parent->child(itemIdx - 1));
2691                         else
2692                                 shortcutsTW->scrollToItem(parent);
2693                         user_bind_.unbind(shortcut, func);
2694                         break;
2695                 }
2696                 case KeyMap::UserUnbind: {
2697                         // for user_unbind, we remove the unbind, and the item
2698                         // become KeyMap::System again.
2699                         user_unbind_.unbind(shortcut, func);
2700                         setItemType(items[i], KeyMap::System);
2701                         removePB->setText(qt_("Remo&ve"));
2702                         break;
2703                 }
2704                 case KeyMap::UserExtraUnbind: {
2705                         // for user unbind that is not in system bind file,
2706                         // remove this unbind file
2707                         QTreeWidgetItem * parent = items[i]->parent();
2708                         parent->takeChild(parent->indexOfChild(items[i]));
2709                         user_unbind_.unbind(shortcut, func);
2710                 }
2711                 }
2712         }
2713 }
2714
2715
2716 void PrefShortcuts::selectBind()
2717 {
2718         QString file = form_->browsebind(internalPath(bindFileED->text()));
2719         if (!file.isEmpty()) {
2720                 bindFileED->setText(file);
2721                 system_bind_ = KeyMap();
2722                 system_bind_.read(fromqstr(file));
2723                 updateShortcutsTW();
2724         }
2725 }
2726
2727
2728 void PrefShortcuts::on_modifyPB_pressed()
2729 {
2730         modifyShortcut();
2731 }
2732
2733
2734 void PrefShortcuts::on_newPB_pressed()
2735 {
2736         shortcut_->lfunLE->clear();
2737         shortcut_->shortcutWG->reset();
2738         save_lfun_ = QString();
2739         shortcut_->exec();
2740 }
2741
2742
2743 void PrefShortcuts::on_removePB_pressed()
2744 {
2745         removeShortcut();
2746 }
2747
2748
2749 void PrefShortcuts::on_searchLE_textEdited()
2750 {
2751         if (searchLE->text().isEmpty()) {
2752                 // show all hidden items
2753                 QTreeWidgetItemIterator it(shortcutsTW, QTreeWidgetItemIterator::Hidden);
2754                 while (*it)
2755                         shortcutsTW->setItemHidden(*it++, false);
2756                 return;
2757         }
2758         // search both columns
2759         QList<QTreeWidgetItem *> matched = shortcutsTW->findItems(searchLE->text(),
2760                 Qt::MatchFlags(Qt::MatchContains | Qt::MatchRecursive), 0);
2761         matched += shortcutsTW->findItems(searchLE->text(),
2762                 Qt::MatchFlags(Qt::MatchContains | Qt::MatchRecursive), 1);
2763
2764         // hide everyone (to avoid searching in matched QList repeatedly
2765         QTreeWidgetItemIterator it(shortcutsTW, QTreeWidgetItemIterator::Selectable);
2766         while (*it)
2767                 shortcutsTW->setItemHidden(*it++, true);
2768         // show matched items
2769         for (int i = 0; i < matched.size(); ++i) {
2770                 shortcutsTW->setItemHidden(matched[i], false);
2771         shortcutsTW->setItemExpanded(matched[i]->parent(), true);
2772         }
2773 }
2774
2775
2776 docstring makeCmdString(FuncRequest const & f)
2777 {
2778         docstring actionStr = from_ascii(lyxaction.getActionName(f.action));
2779         if (!f.argument().empty())
2780                 actionStr += " " + f.argument();
2781         return actionStr;
2782 }
2783
2784
2785 void PrefShortcuts::shortcutOkPressed()
2786 {
2787         QString const new_lfun = shortcut_->lfunLE->text();
2788         FuncRequest func = lyxaction.lookupFunc(fromqstr(new_lfun));
2789
2790         if (func.action == LFUN_UNKNOWN_ACTION) {
2791                 Alert::error(_("Failed to create shortcut"),
2792                         _("Unknown or invalid LyX function"));
2793                 return;
2794         }
2795
2796         KeySequence k = shortcut_->shortcutWG->getKeySequence();
2797         if (k.length() == 0) {
2798                 Alert::error(_("Failed to create shortcut"),
2799                         _("Invalid or empty key sequence"));
2800                 return;
2801         }
2802
2803         // check to see if there's been any change
2804         FuncRequest oldBinding = system_bind_.getBinding(k);
2805         if (oldBinding.action == LFUN_UNKNOWN_ACTION)
2806                 oldBinding = user_bind_.getBinding(k);
2807         if (oldBinding == func) {
2808                 docstring const actionStr = makeCmdString(func);
2809                 Alert::error(_("Failed to create shortcut"),
2810                         bformat(_("Shortcut `%1$s' is already bound to:\n%2$s"), 
2811                         k.print(KeySequence::ForGui), actionStr));
2812                 return;
2813         }
2814         
2815         // make sure this key isn't already bound---and, if so, not unbound
2816         FuncCode const unbind = user_unbind_.getBinding(k).action;
2817         docstring const action_string = makeCmdString(oldBinding);
2818         if (oldBinding.action != LFUN_UNKNOWN_ACTION && unbind == LFUN_UNKNOWN_ACTION
2819                   && save_lfun_ != toqstr(action_string)) {
2820                 // FIXME Perhaps we should offer to over-write the old shortcut?
2821                 // If so, we'll need to remove it from our list, etc.
2822                 Alert::error(_("Failed to create shortcut"),
2823                         bformat(_("Shortcut `%1$s' is already bound to:\n%2$s\n"
2824                           "You need to remove that binding before creating a new one."), 
2825                         k.print(KeySequence::ForGui), action_string));
2826                 return;
2827         }
2828
2829         if (!save_lfun_.isEmpty())
2830                 // real modification of the lfun's shortcut,
2831                 // so remove the previous one
2832                 removeShortcut();
2833
2834         QTreeWidgetItem * item = insertShortcutItem(func, k, KeyMap::UserBind);
2835         if (item) {
2836                 user_bind_.bind(&k, func);
2837                 shortcutsTW->sortItems(0, Qt::AscendingOrder);
2838                 shortcutsTW->setItemExpanded(item->parent(), true);
2839                 shortcutsTW->scrollToItem(item);
2840         } else {
2841                 Alert::error(_("Failed to create shortcut"),
2842                         _("Can not insert shortcut to the list"));
2843                 return;
2844         }
2845 }
2846
2847
2848 void PrefShortcuts::shortcutCancelPressed()
2849 {
2850         shortcut_->shortcutWG->reset();
2851 }
2852
2853
2854 void PrefShortcuts::shortcutClearPressed()
2855 {
2856         shortcut_->shortcutWG->reset();
2857 }
2858
2859
2860 void PrefShortcuts::shortcutRemovePressed()
2861 {
2862         shortcut_->shortcutWG->removeFromSequence();
2863 }
2864
2865
2866 /////////////////////////////////////////////////////////////////////
2867 //
2868 // PrefIdentity
2869 //
2870 /////////////////////////////////////////////////////////////////////
2871
2872 PrefIdentity::PrefIdentity(GuiPreferences * form)
2873         : PrefModule(QString(), qt_("Identity"), form)
2874 {
2875         setupUi(this);
2876
2877         connect(nameED, SIGNAL(textChanged(QString)),
2878                 this, SIGNAL(changed()));
2879         connect(emailED, SIGNAL(textChanged(QString)),
2880                 this, SIGNAL(changed()));
2881 }
2882
2883
2884 void PrefIdentity::apply(LyXRC & rc) const
2885 {
2886         rc.user_name = fromqstr(nameED->text());
2887         rc.user_email = fromqstr(emailED->text());
2888 }
2889
2890
2891 void PrefIdentity::update(LyXRC const & rc)
2892 {
2893         nameED->setText(toqstr(rc.user_name));
2894         emailED->setText(toqstr(rc.user_email));
2895 }
2896
2897
2898
2899 /////////////////////////////////////////////////////////////////////
2900 //
2901 // GuiPreferences
2902 //
2903 /////////////////////////////////////////////////////////////////////
2904
2905 GuiPreferences::GuiPreferences(GuiView & lv)
2906         : GuiDialog(lv, "prefs", qt_("Preferences")), update_screen_font_(false)
2907 {
2908         setupUi(this);
2909
2910         QDialog::setModal(false);
2911
2912         connect(savePB, SIGNAL(clicked()), this, SLOT(slotOK()));
2913         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
2914         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
2915         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
2916
2917         addModule(new PrefUserInterface(this));
2918         addModule(new PrefEdit(this));
2919         addModule(new PrefShortcuts(this));
2920         addModule(new PrefScreenFonts(this));
2921         addModule(new PrefColors(this));
2922         addModule(new PrefDisplay(this));
2923         addModule(new PrefInput(this));
2924         addModule(new PrefCompletion(this));
2925
2926         addModule(new PrefPaths(this));
2927
2928         addModule(new PrefIdentity(this));
2929
2930         addModule(new PrefLanguage(this));
2931         addModule(new PrefSpellchecker(this));
2932
2933         addModule(new PrefPrinter(this));
2934         PrefDate * dateFormat = new PrefDate(this);
2935         addModule(dateFormat);
2936         addModule(new PrefPlaintext(this));
2937         addModule(new PrefLatex(this));
2938
2939         PrefConverters * converters = new PrefConverters(this);
2940         PrefFileformats * formats = new PrefFileformats(this);
2941         connect(formats, SIGNAL(formatsChanged()),
2942                         converters, SLOT(updateGui()));
2943         addModule(converters);
2944         addModule(formats);
2945
2946         prefsPS->setCurrentPanel(qt_("User interface"));
2947 // FIXME: hack to work around resizing bug in Qt >= 4.2
2948 // bug verified with Qt 4.2.{0-3} (JSpitzm)
2949 #if QT_VERSION >= 0x040200
2950         prefsPS->updateGeometry();
2951 #endif
2952
2953         bc().setPolicy(ButtonPolicy::PreferencesPolicy);
2954         bc().setOK(savePB);
2955         bc().setApply(applyPB);
2956         bc().setCancel(closePB);
2957         bc().setRestore(restorePB);
2958
2959         // initialize the strftime validator
2960         bc().addCheckedLineEdit(dateFormat->DateED);
2961 }
2962
2963
2964 void GuiPreferences::addModule(PrefModule * module)
2965 {
2966         LASSERT(module, return);
2967         if (module->category().isEmpty())
2968                 prefsPS->addPanel(module, module->title());
2969         else
2970                 prefsPS->addPanel(module, module->title(), module->category());
2971         connect(module, SIGNAL(changed()), this, SLOT(change_adaptor()));
2972         modules_.push_back(module);
2973 }
2974
2975
2976 void GuiPreferences::change_adaptor()
2977 {
2978         changed();
2979 }
2980
2981
2982 void GuiPreferences::apply(LyXRC & rc) const
2983 {
2984         size_t end = modules_.size();
2985         for (size_t i = 0; i != end; ++i)
2986                 modules_[i]->apply(rc);
2987 }
2988
2989
2990 void GuiPreferences::updateRc(LyXRC const & rc)
2991 {
2992         size_t const end = modules_.size();
2993         for (size_t i = 0; i != end; ++i)
2994                 modules_[i]->update(rc);
2995 }
2996
2997
2998 void GuiPreferences::applyView()
2999 {
3000         apply(rc());
3001 }
3002
3003 bool GuiPreferences::initialiseParams(string const &)
3004 {
3005         rc_ = lyxrc;
3006         formats_ = lyx::formats;
3007         converters_ = theConverters();
3008         converters_.update(formats_);
3009         movers_ = theMovers();
3010         colors_.clear();
3011         update_screen_font_ = false;
3012         
3013         updateRc(rc_);
3014         // Make sure that the bc is in the INITIAL state  
3015         if (bc().policy().buttonStatus(ButtonPolicy::RESTORE))  
3016                 bc().restore();  
3017
3018         return true;
3019 }
3020
3021
3022 void GuiPreferences::dispatchParams()
3023 {
3024         ostringstream ss;
3025         rc_.write(ss, true);
3026         dispatch(FuncRequest(LFUN_LYXRC_APPLY, ss.str()));
3027         // FIXME: these need lfuns
3028         // FIXME UNICODE
3029         theBufferList().setCurrentAuthor(from_utf8(rc_.user_name), from_utf8(rc_.user_email));
3030
3031         lyx::formats = formats_;
3032
3033         theConverters() = converters_;
3034         theConverters().update(lyx::formats);
3035         theConverters().buildGraph();
3036
3037         theMovers() = movers_;
3038
3039         vector<string>::const_iterator it = colors_.begin();
3040         vector<string>::const_iterator const end = colors_.end();
3041         for (; it != end; ++it)
3042                 dispatch(FuncRequest(LFUN_SET_COLOR, *it));
3043         colors_.clear();
3044
3045         if (update_screen_font_) {
3046                 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
3047                 update_screen_font_ = false;
3048         }
3049
3050         // The Save button has been pressed
3051         if (isClosing())
3052                 dispatch(FuncRequest(LFUN_PREFERENCES_SAVE));
3053 }
3054
3055
3056 void GuiPreferences::setColor(ColorCode col, QString const & hex)
3057 {
3058         colors_.push_back(lcolor.getLyXName(col) + ' ' + fromqstr(hex));
3059 }
3060
3061
3062 void GuiPreferences::updateScreenFonts()
3063 {
3064         update_screen_font_ = true;
3065 }
3066
3067
3068 QString GuiPreferences::browsebind(QString const & file) const
3069 {
3070         return browseLibFile("bind", file, "bind", qt_("Choose bind file"),
3071                              QStringList(qt_("LyX bind files (*.bind)")));
3072 }
3073
3074
3075 QString GuiPreferences::browseUI(QString const & file) const
3076 {
3077         return browseLibFile("ui", file, "ui", qt_("Choose UI file"),
3078                              QStringList(qt_("LyX UI files (*.ui)")));
3079 }
3080
3081
3082 QString GuiPreferences::browsekbmap(QString const & file) const
3083 {
3084         return browseLibFile("kbd", file, "kmap", qt_("Choose keyboard map"),
3085                              QStringList(qt_("LyX keyboard maps (*.kmap)")));
3086 }
3087
3088
3089 QString GuiPreferences::browse(QString const & file,
3090         QString const & title) const
3091 {
3092         return browseFile(file, title, QStringList(), true);
3093 }
3094
3095
3096 // We support less paper sizes than the document dialog
3097 // Therefore this adjustment is needed.
3098 PAPER_SIZE GuiPreferences::toPaperSize(int i) const
3099 {
3100         switch (i) {
3101         case 0:
3102                 return PAPER_DEFAULT;
3103         case 1:
3104                 return PAPER_USLETTER;
3105         case 2:
3106                 return PAPER_USLEGAL;
3107         case 3:
3108                 return PAPER_USEXECUTIVE;
3109         case 4:
3110                 return PAPER_A3;
3111         case 5:
3112                 return PAPER_A4;
3113         case 6:
3114                 return PAPER_A5;
3115         case 7:
3116                 return PAPER_B5;
3117         default:
3118                 // should not happen
3119                 return PAPER_DEFAULT;
3120         }
3121 }
3122
3123
3124 int GuiPreferences::fromPaperSize(PAPER_SIZE papersize) const
3125 {
3126         switch (papersize) {
3127         case PAPER_DEFAULT:
3128                 return 0;
3129         case PAPER_USLETTER:
3130                 return 1;
3131         case PAPER_USLEGAL:
3132                 return 2;
3133         case PAPER_USEXECUTIVE:
3134                 return 3;
3135         case PAPER_A3:
3136                 return 4;
3137         case PAPER_A4:
3138                 return 5;
3139         case PAPER_A5:
3140                 return 6;
3141         case PAPER_B5:
3142                 return 7;
3143         default:
3144                 // should not happen
3145                 return 0;
3146         }
3147 }
3148
3149
3150 Dialog * createGuiPreferences(GuiView & lv) { return new GuiPreferences(lv); }
3151
3152
3153 } // namespace frontend
3154 } // namespace lyx
3155
3156 #include "moc_GuiPrefs.cpp"