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