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