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