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