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