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