]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrefs.cpp
PrefShortcuts: add a simple shortcut dialog (does nothing now), adjust prefShortcuts...
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiPrefs.h"
14
15 #include "qt_helpers.h"
16 #include "GuiApplication.h"
17 #include "GuiFontLoader.h"
18
19 #include "BufferList.h"
20 #include "Color.h"
21 #include "ConverterCache.h"
22 #include "debug.h"
23 #include "Font.h"
24 #include "FuncRequest.h"
25 #include "gettext.h"
26 #include "GuiFontExample.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/lstrings.h"
35 #include "support/os.h"
36 #include "support/FileFilterList.h"
37
38 #include "frontend_helpers.h"
39
40 #include "frontends/alert.h"
41 #include "frontends/Application.h"
42
43 #include <QCheckBox>
44 #include <QColorDialog>
45 #include <QFontDatabase>
46 #include <QHeaderView>
47 #include <QLineEdit>
48 #include <QPixmapCache>
49 #include <QPushButton>
50 #include <QSpinBox>
51 #include <QString>
52 #include <QTreeWidget>
53 #include <QTreeWidgetItem>
54 #include <QValidator>
55 #include <QCloseEvent>
56
57 #include <iomanip>
58 #include <sstream>
59 #include <algorithm>
60 #include <utility>
61
62 using namespace Ui;
63
64 using std::endl;
65 using std::ostringstream;
66 using std::pair;
67 using std::string;
68 using std::vector;
69
70 namespace lyx {
71 namespace frontend {
72
73 using support::compare_ascii_no_case;
74 using support::os::external_path;
75 using support::os::external_path_list;
76 using support::os::internal_path;
77 using support::os::internal_path_list;
78 using support::FileFilterList;
79
80
81 /////////////////////////////////////////////////////////////////////
82 //
83 // Helpers
84 //
85 /////////////////////////////////////////////////////////////////////
86
87 template<class A>
88 static size_t findPos_helper(std::vector<A> const & vec, A const & val)
89 {
90         typedef typename std::vector<A>::const_iterator Cit;
91
92         Cit it = std::find(vec.begin(), vec.end(), val);
93         if (it == vec.end())
94                 return 0;
95         return std::distance(vec.begin(), it);
96 }
97
98
99 static std::pair<string, string> parseFontName(string const & name)
100 {
101         string::size_type const idx = name.find('[');
102         if (idx == string::npos || idx == 0)
103                 return make_pair(name, string());
104         return make_pair(name.substr(0, idx - 1),
105                          name.substr(idx + 1, name.size() - idx - 2));
106 }
107
108
109 static void setComboxFont(QComboBox * cb, string const & family,
110         string const & foundry)
111 {
112         QString fontname = toqstr(family);
113         if (!foundry.empty())
114                 fontname += " [" + toqstr(foundry) + ']';
115
116         for (int i = 0; i < cb->count(); ++i) {
117                 if (cb->itemText(i) == fontname) {
118                         cb->setCurrentIndex(i);
119                         return;
120                 }
121         }
122
123         // Try matching without foundry name
124
125         // We count in reverse in order to prefer the Xft foundry
126         for (int i = cb->count(); --i >= 0;) {
127                 pair<string, string> tmp = parseFontName(fromqstr(cb->itemText(i)));
128                 if (compare_ascii_no_case(tmp.first, family) == 0) {
129                         cb->setCurrentIndex(i);
130                         return;
131                 }
132         }
133
134         // family alone can contain e.g. "Helvetica [Adobe]"
135         pair<string, string> tmpfam = parseFontName(family);
136
137         // We count in reverse in order to prefer the Xft foundry
138         for (int i = cb->count() - 1; i >= 0; --i) {
139                 pair<string, string> tmp = parseFontName(fromqstr(cb->itemText(i)));
140                 if (compare_ascii_no_case(tmp.first, tmpfam.first) == 0) {
141                         cb->setCurrentIndex(i);
142                         return;
143                 }
144         }
145
146         // Bleh, default fonts, and the names couldn't be found. Hack
147         // for bug 1063.
148
149         QFont font;
150         font.setKerning(false);
151
152         if (family == theApp()->romanFontName()) {
153                 font.setStyleHint(QFont::Serif);
154                 font.setFamily(family.c_str());
155         } else if (family == theApp()->sansFontName()) {
156                 font.setStyleHint(QFont::SansSerif);
157                 font.setFamily(family.c_str());
158         } else if (family == theApp()->typewriterFontName()) {
159                 font.setStyleHint(QFont::TypeWriter);
160                 font.setFamily(family.c_str());
161         } else {
162                 lyxerr << "FAILED to find the default font: '"
163                        << foundry << "', '" << family << '\''<< endl;
164                 return;
165         }
166
167         QFontInfo info(font);
168         pair<string, string> tmp = parseFontName(fromqstr(info.family()));
169         string const & default_font_name = tmp.first;
170         lyxerr << "Apparent font is " << default_font_name << endl;
171
172         for (int i = 0; i < cb->count(); ++i) {
173                 lyxerr << "Looking at " << fromqstr(cb->itemText(i)) << endl;
174                 if (compare_ascii_no_case(fromqstr(cb->itemText(i)),
175                                     default_font_name) == 0) {
176                         cb->setCurrentIndex(i);
177                         return;
178                 }
179         }
180
181         lyxerr << "FAILED to find the font: '"
182                << foundry << "', '" << family << '\'' <<endl;
183 }
184
185
186 /////////////////////////////////////////////////////////////////////
187 //
188 // PrefPlaintext
189 //
190 /////////////////////////////////////////////////////////////////////
191
192 PrefPlaintext::PrefPlaintext(QWidget * parent)
193         : PrefModule(_("Plain text"), 0, parent)
194 {
195         setupUi(this);
196         connect(plaintextLinelengthSB, SIGNAL(valueChanged(int)),
197                 this, SIGNAL(changed()));
198         connect(plaintextRoffED, SIGNAL(textChanged(const QString&)),
199                 this, SIGNAL(changed()));
200 }
201
202
203 void PrefPlaintext::apply(LyXRC & rc) const
204 {
205         rc.plaintext_linelen = plaintextLinelengthSB->value();
206         rc.plaintext_roff_command = fromqstr(plaintextRoffED->text());
207 }
208
209
210 void PrefPlaintext::update(LyXRC const & rc)
211 {
212         plaintextLinelengthSB->setValue(rc.plaintext_linelen);
213         plaintextRoffED->setText(toqstr(rc.plaintext_roff_command));
214 }
215
216
217 /////////////////////////////////////////////////////////////////////
218 //
219 // PrefDate
220 //
221 /////////////////////////////////////////////////////////////////////
222
223 PrefDate::PrefDate(QWidget * parent)
224         : PrefModule(_("Date format"), 0, parent)
225 {
226         setupUi(this);
227         connect(DateED, SIGNAL(textChanged(const QString &)),
228                 this, SIGNAL(changed()));
229 }
230
231
232 void PrefDate::apply(LyXRC & rc) const
233 {
234         rc.date_insert_format = fromqstr(DateED->text());
235 }
236
237
238 void PrefDate::update(LyXRC const & rc)
239 {
240         DateED->setText(toqstr(rc.date_insert_format));
241 }
242
243
244 /////////////////////////////////////////////////////////////////////
245 //
246 // PrefKeyboard
247 //
248 /////////////////////////////////////////////////////////////////////
249
250 PrefKeyboard::PrefKeyboard(GuiPreferences * form, QWidget * parent)
251         : PrefModule(_("Keyboard"), form, parent)
252 {
253         setupUi(this);
254
255         connect(keymapCB, SIGNAL(clicked()),
256                 this, SIGNAL(changed()));
257         connect(firstKeymapED, SIGNAL(textChanged(const QString&)),
258                 this, SIGNAL(changed()));
259         connect(secondKeymapED, SIGNAL(textChanged(const QString&)),
260                 this, SIGNAL(changed()));
261 }
262
263
264 void PrefKeyboard::apply(LyXRC & rc) const
265 {
266         // FIXME: can derive CB from the two EDs
267         rc.use_kbmap = keymapCB->isChecked();
268         rc.primary_kbmap = internal_path(fromqstr(firstKeymapED->text()));
269         rc.secondary_kbmap = internal_path(fromqstr(secondKeymapED->text()));
270 }
271
272
273 void PrefKeyboard::update(LyXRC const & rc)
274 {
275         // FIXME: can derive CB from the two EDs
276         keymapCB->setChecked(rc.use_kbmap);
277         firstKeymapED->setText(toqstr(external_path(rc.primary_kbmap)));
278         secondKeymapED->setText(toqstr(external_path(rc.secondary_kbmap)));
279 }
280
281
282 QString PrefKeyboard::testKeymap(QString keymap)
283 {
284         return toqstr(form_->browsekbmap(from_utf8(internal_path(fromqstr(keymap)))));
285 }
286
287
288 void PrefKeyboard::on_firstKeymapPB_clicked(bool)
289 {
290         QString const file = testKeymap(firstKeymapED->text());
291         if (!file.isEmpty())
292                 firstKeymapED->setText(file);
293 }
294
295
296 void PrefKeyboard::on_secondKeymapPB_clicked(bool)
297 {
298         QString const file = testKeymap(secondKeymapED->text());
299         if (!file.isEmpty())
300                 secondKeymapED->setText(file);
301 }
302
303
304 void PrefKeyboard::on_keymapCB_toggled(bool keymap)
305 {
306         firstKeymapLA->setEnabled(keymap);
307         secondKeymapLA->setEnabled(keymap);
308         firstKeymapED->setEnabled(keymap);
309         secondKeymapED->setEnabled(keymap);
310         firstKeymapPB->setEnabled(keymap);
311         secondKeymapPB->setEnabled(keymap);
312 }
313
314
315 /////////////////////////////////////////////////////////////////////
316 //
317 // PrefLatex
318 //
319 /////////////////////////////////////////////////////////////////////
320
321 PrefLatex::PrefLatex(GuiPreferences * form, QWidget * parent)
322         : PrefModule(_("LaTeX"), form, parent)
323 {
324         setupUi(this);
325         connect(latexEncodingED, SIGNAL(textChanged(const QString&)),
326                 this, SIGNAL(changed()));
327         connect(latexChecktexED, SIGNAL(textChanged(const QString&)),
328                 this, SIGNAL(changed()));
329         connect(latexBibtexED, SIGNAL(textChanged(const QString&)),
330                 this, SIGNAL(changed()));
331         connect(latexIndexED, SIGNAL(textChanged(const QString&)),
332                 this, SIGNAL(changed()));
333         connect(latexAutoresetCB, SIGNAL(clicked()),
334                 this, SIGNAL(changed()));
335         connect(latexDviPaperED, SIGNAL(textChanged(const QString&)),
336                 this, SIGNAL(changed()));
337         connect(latexPaperSizeCO, SIGNAL(activated(int)),
338                 this, SIGNAL(changed()));
339
340 #if defined(__CYGWIN__) || defined(_WIN32)
341         pathCB->setVisible(true);
342         connect(pathCB, SIGNAL(clicked()), 
343                 this, SIGNAL(changed()));
344 #else
345         pathCB->setVisible(false);
346 #endif
347 }
348
349
350 void PrefLatex::apply(LyXRC & rc) const
351 {
352         rc.fontenc = fromqstr(latexEncodingED->text());
353         rc.chktex_command = fromqstr(latexChecktexED->text());
354         rc.bibtex_command = fromqstr(latexBibtexED->text());
355         rc.index_command = fromqstr(latexIndexED->text());
356         rc.auto_reset_options = latexAutoresetCB->isChecked();
357         rc.view_dvi_paper_option = fromqstr(latexDviPaperED->text());
358         rc.default_papersize =
359                 form_->toPaperSize(latexPaperSizeCO->currentIndex());
360 #if defined(__CYGWIN__) || defined(_WIN32)
361         rc.windows_style_tex_paths = pathCB->isChecked();
362 #endif
363 }
364
365
366 void PrefLatex::update(LyXRC const & rc)
367 {
368         latexEncodingED->setText(toqstr(rc.fontenc));
369         latexChecktexED->setText(toqstr(rc.chktex_command));
370         latexBibtexED->setText(toqstr(rc.bibtex_command));
371         latexIndexED->setText(toqstr(rc.index_command));
372         latexAutoresetCB->setChecked(rc.auto_reset_options);
373         latexDviPaperED->setText(toqstr(rc.view_dvi_paper_option));
374         latexPaperSizeCO->setCurrentIndex(
375                 form_->fromPaperSize(rc.default_papersize));
376 #if defined(__CYGWIN__) || defined(_WIN32)
377         pathCB->setChecked(rc.windows_style_tex_paths);
378 #endif
379 }
380
381
382 /////////////////////////////////////////////////////////////////////
383 //
384 // PrefScreenFonts
385 //
386 /////////////////////////////////////////////////////////////////////
387
388 PrefScreenFonts::PrefScreenFonts(GuiPreferences * form, QWidget * parent)
389         : PrefModule(_("Screen fonts"), form, parent)
390 {
391         setupUi(this);
392
393         connect(screenRomanCO, SIGNAL(activated(const QString&)),
394                 this, SLOT(select_roman(const QString&)));
395         connect(screenSansCO, SIGNAL(activated(const QString&)),
396                 this, SLOT(select_sans(const QString&)));
397         connect(screenTypewriterCO, SIGNAL(activated(const QString&)),
398                 this, SLOT(select_typewriter(const QString&)));
399
400         QFontDatabase fontdb;
401         QStringList families(fontdb.families());
402         for (QStringList::Iterator it = families.begin(); it != families.end(); ++it) {
403                 screenRomanCO->addItem(*it);
404                 screenSansCO->addItem(*it);
405                 screenTypewriterCO->addItem(*it);
406         }
407         connect(screenRomanCO, SIGNAL(activated(const QString&)),
408                 this, SIGNAL(changed()));
409         connect(screenSansCO, SIGNAL(activated(const QString&)),
410                 this, SIGNAL(changed()));
411         connect(screenTypewriterCO, SIGNAL(activated(const QString&)),
412                 this, SIGNAL(changed()));
413         connect(screenZoomSB, SIGNAL(valueChanged(int)),
414                 this, SIGNAL(changed()));
415         connect(screenDpiSB, SIGNAL(valueChanged(int)),
416                 this, SIGNAL(changed()));
417         connect(screenTinyED, SIGNAL(textChanged(const QString&)),
418                 this, SIGNAL(changed()));
419         connect(screenSmallestED, SIGNAL(textChanged(const QString&)),
420                 this, SIGNAL(changed()));
421         connect(screenSmallerED, SIGNAL(textChanged(const QString&)),
422                 this, SIGNAL(changed()));
423         connect(screenSmallED, SIGNAL(textChanged(const QString&)),
424                 this, SIGNAL(changed()));
425         connect(screenNormalED, SIGNAL(textChanged(const QString&)),
426                 this, SIGNAL(changed()));
427         connect(screenLargeED, SIGNAL(textChanged(const QString&)),
428                 this, SIGNAL(changed()));
429         connect(screenLargerED, SIGNAL(textChanged(const QString&)),
430                 this, SIGNAL(changed()));
431         connect(screenLargestED, SIGNAL(textChanged(const QString&)),
432                 this, SIGNAL(changed()));
433         connect(screenHugeED, SIGNAL(textChanged(const QString&)),
434                 this, SIGNAL(changed()));
435         connect(screenHugerED, SIGNAL(textChanged(const QString&)),
436                 this, SIGNAL(changed()));
437
438         screenTinyED->setValidator(new QDoubleValidator(screenTinyED));
439         screenSmallestED->setValidator(new QDoubleValidator(screenSmallestED));
440         screenSmallerED->setValidator(new QDoubleValidator(screenSmallerED));
441         screenSmallED->setValidator(new QDoubleValidator(screenSmallED));
442         screenNormalED->setValidator(new QDoubleValidator(screenNormalED));
443         screenLargeED->setValidator(new QDoubleValidator(screenLargeED));
444         screenLargerED->setValidator(new QDoubleValidator(screenLargerED));
445         screenLargestED->setValidator(new QDoubleValidator(screenLargestED));
446         screenHugeED->setValidator(new QDoubleValidator(screenHugeED));
447         screenHugerED->setValidator(new QDoubleValidator(screenHugerED));
448 }
449
450
451 void PrefScreenFonts::apply(LyXRC & rc) const
452 {
453         LyXRC const oldrc(rc);
454
455         boost::tie(rc.roman_font_name, rc.roman_font_foundry)
456                 = parseFontName(fromqstr(screenRomanCO->currentText()));
457         boost::tie(rc.sans_font_name, rc.sans_font_foundry) =
458                 parseFontName(fromqstr(screenSansCO->currentText()));
459         boost::tie(rc.typewriter_font_name, rc.typewriter_font_foundry) =
460                 parseFontName(fromqstr(screenTypewriterCO->currentText()));
461
462         rc.zoom = screenZoomSB->value();
463         rc.dpi = screenDpiSB->value();
464         rc.font_sizes[Font::SIZE_TINY] = fromqstr(screenTinyED->text());
465         rc.font_sizes[Font::SIZE_SCRIPT] = fromqstr(screenSmallestED->text());
466         rc.font_sizes[Font::SIZE_FOOTNOTE] = fromqstr(screenSmallerED->text());
467         rc.font_sizes[Font::SIZE_SMALL] = fromqstr(screenSmallED->text());
468         rc.font_sizes[Font::SIZE_NORMAL] = fromqstr(screenNormalED->text());
469         rc.font_sizes[Font::SIZE_LARGE] = fromqstr(screenLargeED->text());
470         rc.font_sizes[Font::SIZE_LARGER] = fromqstr(screenLargerED->text());
471         rc.font_sizes[Font::SIZE_LARGEST] = fromqstr(screenLargestED->text());
472         rc.font_sizes[Font::SIZE_HUGE] = fromqstr(screenHugeED->text());
473         rc.font_sizes[Font::SIZE_HUGER] = fromqstr(screenHugerED->text());
474
475         if (rc.font_sizes != oldrc.font_sizes
476                 || rc.roman_font_name != oldrc.roman_font_name
477                 || rc.sans_font_name != oldrc.sans_font_name
478                 || rc.typewriter_font_name != oldrc.typewriter_font_name
479                 || rc.zoom != oldrc.zoom || rc.dpi != oldrc.dpi) {
480                 // The global QPixmapCache is used in GuiPainter to cache text
481                 // painting so we must reset it in case any of the above
482                 // parameter is changed.
483                 QPixmapCache::clear();
484                 guiApp->fontLoader().update();
485                 form_->updateScreenFonts();
486         }
487 }
488
489
490 void PrefScreenFonts::update(LyXRC const & rc)
491 {
492         setComboxFont(screenRomanCO, rc.roman_font_name,
493                         rc.roman_font_foundry);
494         setComboxFont(screenSansCO, rc.sans_font_name,
495                         rc.sans_font_foundry);
496         setComboxFont(screenTypewriterCO, rc.typewriter_font_name,
497                         rc.typewriter_font_foundry);
498
499         select_roman(screenRomanCO->currentText());
500         select_sans(screenSansCO->currentText());
501         select_typewriter(screenTypewriterCO->currentText());
502
503         screenZoomSB->setValue(rc.zoom);
504         screenDpiSB->setValue(rc.dpi);
505         screenTinyED->setText(toqstr(rc.font_sizes[Font::SIZE_TINY]));
506         screenSmallestED->setText(toqstr(rc.font_sizes[Font::SIZE_SCRIPT]));
507         screenSmallerED->setText(toqstr(rc.font_sizes[Font::SIZE_FOOTNOTE]));
508         screenSmallED->setText(toqstr(rc.font_sizes[Font::SIZE_SMALL]));
509         screenNormalED->setText(toqstr(rc.font_sizes[Font::SIZE_NORMAL]));
510         screenLargeED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGE]));
511         screenLargerED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGER]));
512         screenLargestED->setText(toqstr(rc.font_sizes[Font::SIZE_LARGEST]));
513         screenHugeED->setText(toqstr(rc.font_sizes[Font::SIZE_HUGE]));
514         screenHugerED->setText(toqstr(rc.font_sizes[Font::SIZE_HUGER]));
515 }
516
517 void PrefScreenFonts::select_roman(const QString& name)
518 {
519         screenRomanFE->set(QFont(name), name);
520 }
521
522
523 void PrefScreenFonts::select_sans(const QString& name)
524 {
525         screenSansFE->set(QFont(name), name);
526 }
527
528
529 void PrefScreenFonts::select_typewriter(const QString& name)
530 {
531         screenTypewriterFE->set(QFont(name), name);
532 }
533
534
535 /////////////////////////////////////////////////////////////////////
536 //
537 // PrefColors
538 //
539 /////////////////////////////////////////////////////////////////////
540
541 namespace {
542
543 struct ColorSorter
544 {
545         bool operator()(Color::color const & lhs, Color::color const & rhs) const {
546                 return lcolor.getGUIName(lhs) < lcolor.getGUIName(rhs);
547         }
548 };
549
550 } // namespace anon
551
552 PrefColors::PrefColors(GuiPreferences * form, QWidget * parent)
553         : PrefModule( _("Colors"), form, parent)
554 {
555         setupUi(this);
556
557         // FIXME: all of this initialization should be put into the controller.
558         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg113301.html
559         // for some discussion of why that is not trivial.
560         QPixmap icon(32, 32);
561         for (int i = 0; i < Color::ignore; ++i) {
562                 Color::color lc = static_cast<Color::color>(i);
563                 if (lc == Color::none
564                         || lc == Color::black
565                         || lc == Color::white
566                         || lc == Color::red
567                         || lc == Color::green
568                         || lc == Color::blue
569                         || lc == Color::cyan
570                         || lc == Color::magenta
571                         || lc == Color::yellow
572                         || lc == Color::inherit
573                         || lc == Color::ignore) continue;
574
575                 lcolors_.push_back(lc);
576         }
577         std::sort(lcolors_.begin(), lcolors_.end(), ColorSorter());
578         vector<Color_color>::const_iterator cit = lcolors_.begin();
579         vector<Color_color>::const_iterator const end = lcolors_.end();
580         for (; cit != end; ++cit) {
581                         (void) new QListWidgetItem(QIcon(icon),
582                         toqstr(lcolor.getGUIName(*cit)), lyxObjectsLW);
583         }
584         curcolors_.resize(lcolors_.size());
585         newcolors_.resize(lcolors_.size());
586         // End initialization
587
588         connect(colorChangePB, SIGNAL(clicked()),
589                 this, SLOT(change_color()));
590         connect(lyxObjectsLW, SIGNAL(itemSelectionChanged()),
591                 this, SLOT(change_lyxObjects_selection()));
592         connect(lyxObjectsLW, SIGNAL(itemActivated(QListWidgetItem*)),
593                 this, SLOT(change_color()));
594 }
595
596
597 void PrefColors::apply(LyXRC & /*rc*/) const
598 {
599         for (unsigned int i = 0; i < lcolors_.size(); ++i)
600                 if (curcolors_[i] != newcolors_[i])
601                         form_->setColor(lcolors_[i], fromqstr(newcolors_[i]));
602 }
603
604
605 void PrefColors::update(LyXRC const & /*rc*/)
606 {
607         for (unsigned int i = 0; i < lcolors_.size(); ++i) {
608                 QColor color = QColor(guiApp->colorCache().get(lcolors_[i]));
609                 QPixmap coloritem(32, 32);
610                 coloritem.fill(color);
611                 lyxObjectsLW->item(i)->setIcon(QIcon(coloritem));
612                 newcolors_[i] = curcolors_[i] = color.name();
613         }
614         change_lyxObjects_selection();
615 }
616
617 void PrefColors::change_color()
618 {
619         int const row = lyxObjectsLW->currentRow();
620
621         // just to be sure
622         if (row < 0)
623                 return;
624
625         QString const color = newcolors_[row];
626         QColor c = QColorDialog::getColor(QColor(color), qApp->focusWidget());
627
628         if (c.isValid() && c.name() != color) {
629                 newcolors_[row] = c.name();
630                 QPixmap coloritem(32, 32);
631                 coloritem.fill(c);
632                 lyxObjectsLW->currentItem()->setIcon(QIcon(coloritem));
633                 // emit signal
634                 changed();
635         }
636 }
637
638 void PrefColors::change_lyxObjects_selection()
639 {
640         colorChangePB->setDisabled(lyxObjectsLW->currentRow() < 0);
641 }
642
643
644 /////////////////////////////////////////////////////////////////////
645 //
646 // PrefDisplay
647 //
648 /////////////////////////////////////////////////////////////////////
649
650 PrefDisplay::PrefDisplay(QWidget * parent)
651         : PrefModule(_("Graphics"), 0, parent)
652 {
653         setupUi(this);
654         connect(instantPreviewCO, SIGNAL(activated(int)),
655                 this, SIGNAL(changed()));
656         connect(displayGraphicsCO, SIGNAL(activated(int)),
657                 this, SIGNAL(changed()));
658 }
659
660
661 void PrefDisplay::apply(LyXRC & rc) const
662 {
663         switch (instantPreviewCO->currentIndex()) {
664                 case 0: rc.preview = LyXRC::PREVIEW_OFF; break;
665                 case 1: rc.preview = LyXRC::PREVIEW_NO_MATH; break;
666                 case 2: rc.preview = LyXRC::PREVIEW_ON; break;
667         }
668
669         lyx::graphics::DisplayType dtype;
670         switch (displayGraphicsCO->currentIndex()) {
671                 case 3: dtype = lyx::graphics::NoDisplay; break;
672                 case 2: dtype = lyx::graphics::ColorDisplay; break;
673                 case 1: dtype = lyx::graphics::GrayscaleDisplay;        break;
674                 case 0: dtype = lyx::graphics::MonochromeDisplay; break;
675                 default: dtype = lyx::graphics::GrayscaleDisplay;
676         }
677         rc.display_graphics = dtype;
678
679         // FIXME!! The graphics cache no longer has a changeDisplay method.
680 #if 0
681         if (old_value != rc.display_graphics) {
682                 lyx::graphics::GCache & gc = lyx::graphics::GCache::get();
683                 gc.changeDisplay();
684         }
685 #endif
686 }
687
688
689 void PrefDisplay::update(LyXRC const & rc)
690 {
691         switch (rc.preview) {
692         case LyXRC::PREVIEW_OFF:
693                 instantPreviewCO->setCurrentIndex(0);
694                 break;
695         case LyXRC::PREVIEW_NO_MATH :
696                 instantPreviewCO->setCurrentIndex(1);
697                 break;
698         case LyXRC::PREVIEW_ON :
699                 instantPreviewCO->setCurrentIndex(2);
700                 break;
701         }
702
703         int item = 2;
704         switch (rc.display_graphics) {
705                 case lyx::graphics::NoDisplay:          item = 3; break;
706                 case lyx::graphics::ColorDisplay:       item = 2; break;
707                 case lyx::graphics::GrayscaleDisplay:   item = 1; break;
708                 case lyx::graphics::MonochromeDisplay:  item = 0; break;
709                 default: break;
710         }
711         displayGraphicsCO->setCurrentIndex(item);
712 }
713
714
715 /////////////////////////////////////////////////////////////////////
716 //
717 // PrefPaths
718 //
719 /////////////////////////////////////////////////////////////////////
720
721 PrefPaths::PrefPaths(GuiPreferences * form, QWidget * parent)
722         : PrefModule(_("Paths"), form, parent)
723 {
724         setupUi(this);
725         connect(templateDirPB, SIGNAL(clicked()), this, SLOT(select_templatedir()));
726         connect(tempDirPB, SIGNAL(clicked()), this, SLOT(select_tempdir()));
727         connect(backupDirPB, SIGNAL(clicked()), this, SLOT(select_backupdir()));
728         connect(workingDirPB, SIGNAL(clicked()), this, SLOT(select_workingdir()));
729         connect(lyxserverDirPB, SIGNAL(clicked()), this, SLOT(select_lyxpipe()));
730         connect(workingDirED, SIGNAL(textChanged(const QString&)),
731                 this, SIGNAL(changed()));
732         connect(templateDirED, SIGNAL(textChanged(const QString&)),
733                 this, SIGNAL(changed()));
734         connect(backupDirED, SIGNAL(textChanged(const QString&)),
735                 this, SIGNAL(changed()));
736         connect(tempDirED, SIGNAL(textChanged(const QString&)),
737                 this, SIGNAL(changed()));
738         connect(lyxserverDirED, SIGNAL(textChanged(const QString&)),
739                 this, SIGNAL(changed()));
740         connect(pathPrefixED, SIGNAL(textChanged(const QString&)),
741                 this, SIGNAL(changed()));
742 }
743
744
745 void PrefPaths::apply(LyXRC & rc) const
746 {
747         rc.document_path = internal_path(fromqstr(workingDirED->text()));
748         rc.template_path = internal_path(fromqstr(templateDirED->text()));
749         rc.backupdir_path = internal_path(fromqstr(backupDirED->text()));
750         rc.tempdir_path = internal_path(fromqstr(tempDirED->text()));
751         rc.path_prefix = internal_path_list(fromqstr(pathPrefixED->text()));
752         // FIXME: should be a checkbox only
753         rc.lyxpipes = internal_path(fromqstr(lyxserverDirED->text()));
754 }
755
756
757 void PrefPaths::update(LyXRC const & rc)
758 {
759         workingDirED->setText(toqstr(external_path(rc.document_path)));
760         templateDirED->setText(toqstr(external_path(rc.template_path)));
761         backupDirED->setText(toqstr(external_path(rc.backupdir_path)));
762         tempDirED->setText(toqstr(external_path(rc.tempdir_path)));
763         pathPrefixED->setText(toqstr(external_path_list(rc.path_prefix)));
764         // FIXME: should be a checkbox only
765         lyxserverDirED->setText(toqstr(external_path(rc.lyxpipes)));
766 }
767
768
769 void PrefPaths::select_templatedir()
770 {
771         docstring file(form_->browsedir(
772                 from_utf8(internal_path(fromqstr(templateDirED->text()))),
773                 _("Select a document templates directory")));
774         if (!file.empty())
775                 templateDirED->setText(toqstr(file));
776 }
777
778
779 void PrefPaths::select_tempdir()
780 {
781         docstring file(form_->browsedir(
782                 from_utf8(internal_path(fromqstr(tempDirED->text()))),
783                 _("Select a temporary directory")));
784         if (!file.empty())
785                 tempDirED->setText(toqstr(file));
786 }
787
788
789 void PrefPaths::select_backupdir()
790 {
791         docstring file(form_->browsedir(
792                 from_utf8(internal_path(fromqstr(backupDirED->text()))),
793                 _("Select a backups directory")));
794         if (!file.empty())
795                 backupDirED->setText(toqstr(file));
796 }
797
798
799 void PrefPaths::select_workingdir()
800 {
801         docstring file(form_->browsedir(
802                 from_utf8(internal_path(fromqstr(workingDirED->text()))),
803                 _("Select a document directory")));
804         if (!file.empty())
805                 workingDirED->setText(toqstr(file));
806 }
807
808
809 void PrefPaths::select_lyxpipe()
810 {
811         docstring file(form_->browse(
812                 from_utf8(internal_path(fromqstr(lyxserverDirED->text()))),
813                 _("Give a filename for the LyX server pipe")));
814         if (!file.empty())
815                 lyxserverDirED->setText(toqstr(file));
816 }
817
818
819 /////////////////////////////////////////////////////////////////////
820 //
821 // PrefSpellchecker
822 //
823 /////////////////////////////////////////////////////////////////////
824
825 PrefSpellchecker::PrefSpellchecker(GuiPreferences * form, QWidget * parent)
826         : PrefModule(_("Spellchecker"), form, parent)
827 {
828         setupUi(this);
829
830         connect(persDictionaryPB, SIGNAL(clicked()), this, SLOT(select_dict()));
831 #if defined (USE_ISPELL)
832         connect(spellCommandCO, SIGNAL(activated(int)),
833                 this, SIGNAL(changed()));
834 #else
835         spellCommandCO->setEnabled(false);
836 #endif
837         connect(altLanguageED, SIGNAL(textChanged(const QString&)),
838                 this, SIGNAL(changed()));
839         connect(escapeCharactersED, SIGNAL(textChanged(const QString&)),
840                 this, SIGNAL(changed()));
841         connect(persDictionaryED, SIGNAL(textChanged(const QString&)),
842                 this, SIGNAL(changed()));
843         connect(compoundWordCB, SIGNAL(clicked()),
844                 this, SIGNAL(changed()));
845         connect(inputEncodingCB, SIGNAL(clicked()),
846                 this, SIGNAL(changed()));
847
848         spellCommandCO->addItem(qt_("ispell"));
849         spellCommandCO->addItem(qt_("aspell"));
850         spellCommandCO->addItem(qt_("hspell"));
851 #ifdef USE_PSPELL
852         spellCommandCO->addItem(qt_("pspell (library)"));
853 #else
854 #ifdef USE_ASPELL
855         spellCommandCO->addItem(qt_("aspell (library)"));
856 #endif
857 #endif
858 }
859
860
861 void PrefSpellchecker::apply(LyXRC & rc) const
862 {
863         switch (spellCommandCO->currentIndex()) {
864                 case 0:
865                 case 1:
866                 case 2:
867                         rc.use_spell_lib = false;
868                         rc.isp_command = fromqstr(spellCommandCO->currentText());
869                         break;
870                 case 3:
871                         rc.use_spell_lib = true;
872                         break;
873         }
874
875         // FIXME: remove isp_use_alt_lang
876         rc.isp_alt_lang = fromqstr(altLanguageED->text());
877         rc.isp_use_alt_lang = !rc.isp_alt_lang.empty();
878         // FIXME: remove isp_use_esc_chars
879         rc.isp_esc_chars = fromqstr(escapeCharactersED->text());
880         rc.isp_use_esc_chars = !rc.isp_esc_chars.empty();
881         // FIXME: remove isp_use_pers_dict
882         rc.isp_pers_dict = internal_path(fromqstr(persDictionaryED->text()));
883         rc.isp_use_pers_dict = !rc.isp_pers_dict.empty();
884         rc.isp_accept_compound = compoundWordCB->isChecked();
885         rc.isp_use_input_encoding = inputEncodingCB->isChecked();
886 }
887
888
889 void PrefSpellchecker::update(LyXRC const & rc)
890 {
891         spellCommandCO->setCurrentIndex(0);
892
893         if (rc.isp_command == "ispell") {
894                 spellCommandCO->setCurrentIndex(0);
895         } else if (rc.isp_command == "aspell") {
896                 spellCommandCO->setCurrentIndex(1);
897         } else if (rc.isp_command == "hspell") {
898                 spellCommandCO->setCurrentIndex(2);
899         }
900
901         if (rc.use_spell_lib) {
902 #if defined(USE_ASPELL) || defined(USE_PSPELL)
903                 spellCommandCO->setCurrentIndex(3);
904 #endif
905         }
906
907         // FIXME: remove isp_use_alt_lang
908         altLanguageED->setText(toqstr(rc.isp_alt_lang));
909         // FIXME: remove isp_use_esc_chars
910         escapeCharactersED->setText(toqstr(rc.isp_esc_chars));
911         // FIXME: remove isp_use_pers_dict
912         persDictionaryED->setText(toqstr(external_path(rc.isp_pers_dict)));
913         compoundWordCB->setChecked(rc.isp_accept_compound);
914         inputEncodingCB->setChecked(rc.isp_use_input_encoding);
915 }
916
917
918 void PrefSpellchecker::select_dict()
919 {
920         docstring file(form_->browsedict(
921                 from_utf8(internal_path(fromqstr(persDictionaryED->text())))));
922         if (!file.empty())
923                 persDictionaryED->setText(toqstr(file));
924 }
925
926
927
928 /////////////////////////////////////////////////////////////////////
929 //
930 // PrefConverters
931 //
932 /////////////////////////////////////////////////////////////////////
933
934
935 PrefConverters::PrefConverters(GuiPreferences * form, QWidget * parent)
936         : PrefModule(_("Converters"), form, parent)
937 {
938         setupUi(this);
939
940         connect(converterNewPB, SIGNAL(clicked()),
941                 this, SLOT(update_converter()));
942         connect(converterRemovePB, SIGNAL(clicked()),
943                 this, SLOT(remove_converter()));
944         connect(converterModifyPB, SIGNAL(clicked()),
945                 this, SLOT(update_converter()));
946         connect(convertersLW, SIGNAL(currentRowChanged(int)),
947                 this, SLOT(switch_converter()));
948         connect(converterFromCO, SIGNAL(activated(const QString&)),
949                 this, SLOT(converter_changed()));
950         connect(converterToCO, SIGNAL(activated(const QString&)),
951                 this, SLOT(converter_changed()));
952         connect(converterED, SIGNAL(textEdited(const QString&)),
953                 this, SLOT(converter_changed()));
954         connect(converterFlagED, SIGNAL(textEdited(const QString&)),
955                 this, SLOT(converter_changed()));
956         connect(converterNewPB, SIGNAL(clicked()),
957                 this, SIGNAL(changed()));
958         connect(converterRemovePB, SIGNAL(clicked()),
959                 this, SIGNAL(changed()));
960         connect(converterModifyPB, SIGNAL(clicked()),
961                 this, SIGNAL(changed()));
962         connect(maxAgeLE, SIGNAL(textEdited(const QString&)),
963                 this, SIGNAL(changed()));
964
965         maxAgeLE->setValidator(new QDoubleValidator(maxAgeLE));
966         //converterDefGB->setFocusProxy(convertersLW);
967 }
968
969
970 void PrefConverters::apply(LyXRC & rc) const
971 {
972         rc.use_converter_cache = cacheCB->isChecked();
973         rc.converter_cache_maxage = int(maxAgeLE->text().toDouble() * 86400.0);
974 }
975
976
977 void PrefConverters::update(LyXRC const & rc)
978 {
979         cacheCB->setChecked(rc.use_converter_cache);
980         QString max_age;
981         max_age.setNum(double(rc.converter_cache_maxage) / 86400.0, 'g', 6);
982         maxAgeLE->setText(max_age);
983         updateGui();
984 }
985
986
987 void PrefConverters::updateGui()
988 {
989         form_->formats().sort();
990         form_->converters().update(form_->formats());
991         // save current selection
992         QString current = converterFromCO->currentText()
993                 + " -> " + converterToCO->currentText();
994
995         converterFromCO->clear();
996         converterToCO->clear();
997
998         Formats::const_iterator cit = form_->formats().begin();
999         Formats::const_iterator end = form_->formats().end();
1000         for (; cit != end; ++cit) {
1001                 converterFromCO->addItem(toqstr(cit->prettyname()));
1002                 converterToCO->addItem(toqstr(cit->prettyname()));
1003         }
1004
1005         // currentRowChanged(int) is also triggered when updating the listwidget
1006         // block signals to avoid unnecessary calls to switch_converter()
1007         convertersLW->blockSignals(true);
1008         convertersLW->clear();
1009
1010         Converters::const_iterator ccit = form_->converters().begin();
1011         Converters::const_iterator cend = form_->converters().end();
1012         for (; ccit != cend; ++ccit) {
1013                 std::string const name =
1014                         ccit->From->prettyname() + " -> " + ccit->To->prettyname();
1015                 int type = form_->converters().getNumber(ccit->From->name(), ccit->To->name());
1016                 new QListWidgetItem(toqstr(name), convertersLW, type);
1017         }
1018         convertersLW->sortItems(Qt::AscendingOrder);
1019         convertersLW->blockSignals(false);
1020
1021         // restore selection
1022         if (!current.isEmpty()) {
1023                 QList<QListWidgetItem *> const item =
1024                         convertersLW->findItems(current, Qt::MatchExactly);
1025                 if (!item.isEmpty())
1026                         convertersLW->setCurrentItem(item.at(0));
1027         }
1028
1029         // select first element if restoring failed
1030         if (convertersLW->currentRow() == -1)
1031                 convertersLW->setCurrentRow(0);
1032
1033         updateButtons();
1034 }
1035
1036
1037 void PrefConverters::switch_converter()
1038 {
1039         int const cnr = convertersLW->currentItem()->type();
1040         Converter const & c(form_->converters().get(cnr));
1041         converterFromCO->setCurrentIndex(form_->formats().getNumber(c.from));
1042         converterToCO->setCurrentIndex(form_->formats().getNumber(c.to));
1043         converterED->setText(toqstr(c.command));
1044         converterFlagED->setText(toqstr(c.flags));
1045
1046         updateButtons();
1047 }
1048
1049
1050 void PrefConverters::converter_changed()
1051 {
1052         updateButtons();
1053 }
1054
1055
1056 void PrefConverters::updateButtons()
1057 {
1058         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1059         Format const & to = form_->formats().get(converterToCO->currentIndex());
1060         int const sel = form_->converters().getNumber(from.name(), to.name());
1061         bool const known = !(sel < 0);
1062         bool const valid = !(converterED->text().isEmpty()
1063                 || from.name() == to.name());
1064
1065         int const cnr = convertersLW->currentItem()->type();
1066         Converter const & c(form_->converters().get(cnr));
1067         string const old_command = c.command;
1068         string const old_flag = c.flags;
1069         string const new_command(fromqstr(converterED->text()));
1070         string const new_flag(fromqstr(converterFlagED->text()));
1071
1072         bool modified = ((old_command != new_command) || (old_flag != new_flag));
1073
1074         converterModifyPB->setEnabled(valid && known && modified);
1075         converterNewPB->setEnabled(valid && !known);
1076         converterRemovePB->setEnabled(known);
1077
1078         maxAgeLE->setEnabled(cacheCB->isChecked());
1079         maxAgeLA->setEnabled(cacheCB->isChecked());
1080 }
1081
1082
1083 // FIXME: user must
1084 // specify unique from/to or it doesn't appear. This is really bad UI
1085 // this is why we can use the same function for both new and modify
1086 void PrefConverters::update_converter()
1087 {
1088         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1089         Format const & to = form_->formats().get(converterToCO->currentIndex());
1090         string const flags = fromqstr(converterFlagED->text());
1091         string const command = fromqstr(converterED->text());
1092
1093         Converter const * old =
1094                 form_->converters().getConverter(from.name(), to.name());
1095         form_->converters().add(from.name(), to.name(), command, flags);
1096
1097         if (!old)
1098                 form_->converters().updateLast(form_->formats());
1099
1100         updateGui();
1101
1102         // Remove all files created by this converter from the cache, since
1103         // the modified converter might create different files.
1104         ConverterCache::get().remove_all(from.name(), to.name());
1105 }
1106
1107
1108 void PrefConverters::remove_converter()
1109 {
1110         Format const & from = form_->formats().get(converterFromCO->currentIndex());
1111         Format const & to = form_->formats().get(converterToCO->currentIndex());
1112         form_->converters().erase(from.name(), to.name());
1113
1114         updateGui();
1115
1116         // Remove all files created by this converter from the cache, since
1117         // a possible new converter might create different files.
1118         ConverterCache::get().remove_all(from.name(), to.name());
1119 }
1120
1121
1122 void PrefConverters::on_cacheCB_stateChanged(int state)
1123 {
1124         maxAgeLE->setEnabled(state == Qt::Checked);
1125         maxAgeLA->setEnabled(state == Qt::Checked);
1126         changed();
1127 }
1128
1129
1130 /////////////////////////////////////////////////////////////////////
1131 //
1132 // PrefFileformats
1133 //
1134 /////////////////////////////////////////////////////////////////////
1135 FormatValidator::FormatValidator(QWidget * parent, Formats const & f) 
1136         : QValidator(parent), formats_(f)
1137 {
1138 }
1139
1140
1141 void FormatValidator::fixup(QString & input) const
1142 {
1143         Formats::const_iterator cit = formats_.begin();
1144         Formats::const_iterator end = formats_.end();
1145         for (; cit != end; ++cit) {
1146                 string const name = str(cit);
1147                 if (distance(formats_.begin(), cit) == nr()) {
1148                         input = toqstr(name);
1149                         return;
1150
1151                 }
1152         }
1153 }
1154
1155
1156 QValidator::State FormatValidator::validate(QString & input, int & /*pos*/) const
1157 {
1158         Formats::const_iterator cit = formats_.begin();
1159         Formats::const_iterator end = formats_.end();
1160         bool unknown = true;
1161         for (; unknown && cit != end; ++cit) {
1162                 string const name = str(cit);
1163                 if (distance(formats_.begin(), cit) != nr())
1164                         unknown = toqstr(name) != input;
1165         }
1166
1167         if (unknown && !input.isEmpty()) 
1168                 return QValidator::Acceptable;
1169         else
1170                 return QValidator::Intermediate;
1171 }
1172
1173
1174 int FormatValidator::nr() const
1175 {
1176         QComboBox * p = qobject_cast<QComboBox *>(parent());
1177         return p->itemData(p->currentIndex()).toInt();
1178 }
1179
1180
1181 FormatNameValidator::FormatNameValidator(QWidget * parent, Formats const & f) 
1182         : FormatValidator(parent, f)
1183 {
1184 }
1185
1186 std::string FormatNameValidator::str(Formats::const_iterator it) const
1187 {
1188         return it->name();
1189 }
1190
1191
1192 FormatPrettynameValidator::FormatPrettynameValidator(QWidget * parent, Formats const & f) 
1193         : FormatValidator(parent, f)
1194 {
1195 }
1196
1197
1198 std::string FormatPrettynameValidator::str(Formats::const_iterator it) const
1199 {
1200         return it->prettyname();
1201 }
1202
1203
1204 PrefFileformats::PrefFileformats(GuiPreferences * form, QWidget * parent)
1205         : PrefModule(_("File formats"), form, parent)
1206 {
1207         setupUi(this);
1208         formatED->setValidator(new FormatNameValidator(formatsCB, form_->formats()));
1209         formatsCB->setValidator(new FormatPrettynameValidator(formatsCB, form_->formats()));
1210
1211         connect(documentCB, SIGNAL(clicked()),
1212                 this, SLOT(setFlags()));
1213         connect(vectorCB, SIGNAL(clicked()),
1214                 this, SLOT(setFlags()));
1215         connect(formatsCB->lineEdit(), SIGNAL(editingFinished()),
1216                 this, SLOT(updatePrettyname()));
1217         connect(formatsCB->lineEdit(), SIGNAL(textEdited(const QString &)),
1218                 this, SIGNAL(changed()));
1219 }
1220
1221
1222 void PrefFileformats::apply(LyXRC & /*rc*/) const
1223 {
1224 }
1225
1226
1227 void PrefFileformats::update(LyXRC const & /*rc*/)
1228 {
1229         updateView();
1230 }
1231
1232
1233 void PrefFileformats::updateView()
1234 {
1235         QString const current = formatsCB->currentText();
1236
1237         // update combobox with formats
1238         formatsCB->blockSignals(true);
1239         formatsCB->clear();
1240         form_->formats().sort();
1241         Formats::const_iterator cit = form_->formats().begin();
1242         Formats::const_iterator end = form_->formats().end();
1243         for (; cit != end; ++cit)
1244                 formatsCB->addItem(toqstr(cit->prettyname()),
1245                                                         QVariant(form_->formats().getNumber(cit->name())) );
1246
1247         // restore selection
1248         int const item = formatsCB->findText(current, Qt::MatchExactly);
1249         formatsCB->setCurrentIndex(item < 0 ? 0 : item);
1250         on_formatsCB_currentIndexChanged(item < 0 ? 0 : item);
1251         formatsCB->blockSignals(false);
1252 }
1253
1254
1255 void PrefFileformats::on_formatsCB_currentIndexChanged(int i)
1256 {
1257         int const nr = formatsCB->itemData(i).toInt();
1258         Format const f = form_->formats().get(nr);
1259
1260         formatED->setText(toqstr(f.name()));
1261         copierED->setText(toqstr(form_->movers().command(f.name())));
1262         extensionED->setText(toqstr(f.extension()));
1263         shortcutED->setText(toqstr(f.shortcut()));
1264         viewerED->setText(toqstr(f.viewer()));
1265         editorED->setText(toqstr(f.editor()));
1266         documentCB->setChecked((f.documentFormat()));
1267         vectorCB->setChecked((f.vectorFormat()));
1268 }
1269
1270
1271 void PrefFileformats::setFlags()
1272 {
1273         int flags = Format::none;
1274         if (documentCB->isChecked())
1275                 flags |= Format::document;
1276         if (vectorCB->isChecked())
1277                 flags |= Format::vector;
1278         currentFormat().setFlags(flags);
1279         changed();
1280 }
1281
1282
1283 void PrefFileformats::on_copierED_textEdited(const QString & s)
1284 {
1285         string const fmt = fromqstr(formatED->text());
1286         form_->movers().set(fmt, fromqstr(s));
1287         changed();
1288 }
1289
1290
1291 void PrefFileformats::on_extensionED_textEdited(const QString & s)
1292 {
1293         currentFormat().setExtension(fromqstr(s));
1294         changed();
1295 }
1296
1297 void PrefFileformats::on_viewerED_textEdited(const QString & s)
1298 {
1299         currentFormat().setViewer(fromqstr(s));
1300         changed();
1301 }
1302
1303
1304 void PrefFileformats::on_editorED_textEdited(const QString & s)
1305 {
1306         currentFormat().setEditor(fromqstr(s));
1307         changed();
1308 }
1309
1310
1311 void PrefFileformats::on_shortcutED_textEdited(const QString & s)
1312 {
1313         currentFormat().setShortcut(fromqstr(s));
1314         changed();
1315 }
1316
1317
1318 void PrefFileformats::on_formatED_editingFinished()
1319 {
1320         string const newname = fromqstr(formatED->displayText());
1321         if (newname == currentFormat().name())
1322                 return;
1323
1324         currentFormat().setName(newname);
1325         changed();
1326 }
1327
1328
1329 void PrefFileformats::on_formatED_textChanged(const QString &)
1330 {
1331         QString t = formatED->text();
1332         int p = 0;
1333         bool valid = formatED->validator()->validate(t, p) == QValidator::Acceptable;
1334         setValid(formatLA, valid);
1335 }
1336
1337
1338 void PrefFileformats::on_formatsCB_editTextChanged(const QString &)
1339 {
1340         QString t = formatsCB->currentText();
1341         int p = 0;
1342         bool valid = formatsCB->validator()->validate(t, p) == QValidator::Acceptable;
1343         setValid(formatsLA, valid);
1344 }
1345
1346
1347 void PrefFileformats::updatePrettyname()
1348 {
1349         string const newname = fromqstr(formatsCB->currentText());
1350         if (newname == currentFormat().prettyname())
1351                 return;
1352
1353         currentFormat().setPrettyname(newname);
1354         formatsChanged();
1355         updateView();
1356         changed();
1357 }
1358
1359
1360 Format & PrefFileformats::currentFormat()
1361 {
1362         int const i = formatsCB->currentIndex();
1363         int const nr = formatsCB->itemData(i).toInt();
1364         return form_->formats().get(nr);
1365 }
1366
1367
1368 void PrefFileformats::on_formatNewPB_clicked()
1369 {
1370         form_->formats().add("", "", "", "", "", "", Format::none);
1371         updateView();
1372         formatsCB->setCurrentIndex(0);
1373         formatsCB->setFocus(Qt::OtherFocusReason);
1374 }
1375
1376
1377 void PrefFileformats::on_formatRemovePB_clicked()
1378 {
1379         int const i = formatsCB->currentIndex();
1380         int const nr = formatsCB->itemData(i).toInt();
1381         string const current_text = form_->formats().get(nr).name();
1382         if (form_->converters().formatIsUsed(current_text)) {
1383                 Alert::error(_("Format in use"),
1384                              _("Cannot remove a Format used by a Converter. "
1385                                             "Remove the converter first."));
1386                 return;
1387         }
1388
1389         form_->formats().erase(current_text);
1390         formatsChanged();
1391         updateView();
1392         on_formatsCB_editTextChanged(formatsCB->currentText());
1393         changed();
1394 }
1395
1396
1397 /////////////////////////////////////////////////////////////////////
1398 //
1399 // PrefLanguage
1400 //
1401 /////////////////////////////////////////////////////////////////////
1402
1403 PrefLanguage::PrefLanguage(QWidget * parent)
1404         : PrefModule(_("Language"), 0, parent)
1405 {
1406         setupUi(this);
1407
1408         connect(rtlCB, SIGNAL(clicked()),
1409                 this, SIGNAL(changed()));
1410         connect(markForeignCB, SIGNAL(clicked()),
1411                 this, SIGNAL(changed()));
1412         connect(autoBeginCB, SIGNAL(clicked()),
1413                 this, SIGNAL(changed()));
1414         connect(autoEndCB, SIGNAL(clicked()),
1415                 this, SIGNAL(changed()));
1416         connect(useBabelCB, SIGNAL(clicked()),
1417                 this, SIGNAL(changed()));
1418         connect(globalCB, SIGNAL(clicked()),
1419                 this, SIGNAL(changed()));
1420         connect(languagePackageED, SIGNAL(textChanged(const QString&)),
1421                 this, SIGNAL(changed()));
1422         connect(startCommandED, SIGNAL(textChanged(const QString&)),
1423                 this, SIGNAL(changed()));
1424         connect(endCommandED, SIGNAL(textChanged(const QString&)),
1425                 this, SIGNAL(changed()));
1426         connect(defaultLanguageCO, SIGNAL(activated(int)),
1427                 this, SIGNAL(changed()));
1428
1429         defaultLanguageCO->clear();
1430
1431         // store the lang identifiers for later
1432         std::vector<LanguagePair> const langs = frontend::getLanguageData(false);
1433         std::vector<LanguagePair>::const_iterator lit  = langs.begin();
1434         std::vector<LanguagePair>::const_iterator lend = langs.end();
1435         lang_.clear();
1436         for (; lit != lend; ++lit) {
1437                 defaultLanguageCO->addItem(toqstr(lit->first));
1438                 lang_.push_back(lit->second);
1439         }
1440 }
1441
1442
1443 void PrefLanguage::apply(LyXRC & rc) const
1444 {
1445         // FIXME: remove rtl_support bool
1446         rc.rtl_support = rtlCB->isChecked();
1447         rc.mark_foreign_language = markForeignCB->isChecked();
1448         rc.language_auto_begin = autoBeginCB->isChecked();
1449         rc.language_auto_end = autoEndCB->isChecked();
1450         rc.language_use_babel = useBabelCB->isChecked();
1451         rc.language_global_options = globalCB->isChecked();
1452         rc.language_package = fromqstr(languagePackageED->text());
1453         rc.language_command_begin = fromqstr(startCommandED->text());
1454         rc.language_command_end = fromqstr(endCommandED->text());
1455         rc.default_language = lang_[defaultLanguageCO->currentIndex()];
1456 }
1457
1458
1459 void PrefLanguage::update(LyXRC const & rc)
1460 {
1461         // FIXME: remove rtl_support bool
1462         rtlCB->setChecked(rc.rtl_support);
1463         markForeignCB->setChecked(rc.mark_foreign_language);
1464         autoBeginCB->setChecked(rc.language_auto_begin);
1465         autoEndCB->setChecked(rc.language_auto_end);
1466         useBabelCB->setChecked(rc.language_use_babel);
1467         globalCB->setChecked(rc.language_global_options);
1468         languagePackageED->setText(toqstr(rc.language_package));
1469         startCommandED->setText(toqstr(rc.language_command_begin));
1470         endCommandED->setText(toqstr(rc.language_command_end));
1471
1472         int const pos = int(findPos_helper(lang_, rc.default_language));
1473         defaultLanguageCO->setCurrentIndex(pos);
1474 }
1475
1476
1477 /////////////////////////////////////////////////////////////////////
1478 //
1479 // PrefPrinter
1480 //
1481 /////////////////////////////////////////////////////////////////////
1482
1483 PrefPrinter::PrefPrinter(QWidget * parent)
1484         : PrefModule(_("Printer"), 0, parent)
1485 {
1486         setupUi(this);
1487
1488         connect(printerAdaptCB, SIGNAL(clicked()),
1489                 this, SIGNAL(changed()));
1490         connect(printerCommandED, SIGNAL(textChanged(const QString&)),
1491                 this, SIGNAL(changed()));
1492         connect(printerNameED, SIGNAL(textChanged(const QString&)),
1493                 this, SIGNAL(changed()));
1494         connect(printerPageRangeED, SIGNAL(textChanged(const QString&)),
1495                 this, SIGNAL(changed()));
1496         connect(printerCopiesED, SIGNAL(textChanged(const QString&)),
1497                 this, SIGNAL(changed()));
1498         connect(printerReverseED, SIGNAL(textChanged(const QString&)),
1499                 this, SIGNAL(changed()));
1500         connect(printerToPrinterED, SIGNAL(textChanged(const QString&)),
1501                 this, SIGNAL(changed()));
1502         connect(printerExtensionED, SIGNAL(textChanged(const QString&)),
1503                 this, SIGNAL(changed()));
1504         connect(printerSpoolCommandED, SIGNAL(textChanged(const QString&)),
1505                 this, SIGNAL(changed()));
1506         connect(printerPaperTypeED, SIGNAL(textChanged(const QString&)),
1507                 this, SIGNAL(changed()));
1508         connect(printerEvenED, SIGNAL(textChanged(const QString&)),
1509                 this, SIGNAL(changed()));
1510         connect(printerOddED, SIGNAL(textChanged(const QString&)),
1511                 this, SIGNAL(changed()));
1512         connect(printerCollatedED, SIGNAL(textChanged(const QString&)),
1513                 this, SIGNAL(changed()));
1514         connect(printerLandscapeED, SIGNAL(textChanged(const QString&)),
1515                 this, SIGNAL(changed()));
1516         connect(printerToFileED, SIGNAL(textChanged(const QString&)),
1517                 this, SIGNAL(changed()));
1518         connect(printerExtraED, SIGNAL(textChanged(const QString&)),
1519                 this, SIGNAL(changed()));
1520         connect(printerSpoolPrefixED, SIGNAL(textChanged(const QString&)),
1521                 this, SIGNAL(changed()));
1522         connect(printerPaperSizeED, SIGNAL(textChanged(const QString&)),
1523                 this, SIGNAL(changed()));
1524 }
1525
1526
1527 void PrefPrinter::apply(LyXRC & rc) const
1528 {
1529         rc.print_adapt_output = printerAdaptCB->isChecked();
1530         rc.print_command = fromqstr(printerCommandED->text());
1531         rc.printer = fromqstr(printerNameED->text());
1532
1533         rc.print_pagerange_flag = fromqstr(printerPageRangeED->text());
1534         rc.print_copies_flag = fromqstr(printerCopiesED->text());
1535         rc.print_reverse_flag = fromqstr(printerReverseED->text());
1536         rc.print_to_printer = fromqstr(printerToPrinterED->text());
1537         rc.print_file_extension = fromqstr(printerExtensionED->text());
1538         rc.print_spool_command = fromqstr(printerSpoolCommandED->text());
1539         rc.print_paper_flag = fromqstr(printerPaperTypeED->text());
1540         rc.print_evenpage_flag = fromqstr(printerEvenED->text());
1541         rc.print_oddpage_flag = fromqstr(printerOddED->text());
1542         rc.print_collcopies_flag = fromqstr(printerCollatedED->text());
1543         rc.print_landscape_flag = fromqstr(printerLandscapeED->text());
1544         rc.print_to_file = internal_path(fromqstr(printerToFileED->text()));
1545         rc.print_extra_options = fromqstr(printerExtraED->text());
1546         rc.print_spool_printerprefix = fromqstr(printerSpoolPrefixED->text());
1547         rc.print_paper_dimension_flag = fromqstr(printerPaperSizeED->text());
1548 }
1549
1550
1551 void PrefPrinter::update(LyXRC const & rc)
1552 {
1553         printerAdaptCB->setChecked(rc.print_adapt_output);
1554         printerCommandED->setText(toqstr(rc.print_command));
1555         printerNameED->setText(toqstr(rc.printer));
1556
1557         printerPageRangeED->setText(toqstr(rc.print_pagerange_flag));
1558         printerCopiesED->setText(toqstr(rc.print_copies_flag));
1559         printerReverseED->setText(toqstr(rc.print_reverse_flag));
1560         printerToPrinterED->setText(toqstr(rc.print_to_printer));
1561         printerExtensionED->setText(toqstr(rc.print_file_extension));
1562         printerSpoolCommandED->setText(toqstr(rc.print_spool_command));
1563         printerPaperTypeED->setText(toqstr(rc.print_paper_flag));
1564         printerEvenED->setText(toqstr(rc.print_evenpage_flag));
1565         printerOddED->setText(toqstr(rc.print_oddpage_flag));
1566         printerCollatedED->setText(toqstr(rc.print_collcopies_flag));
1567         printerLandscapeED->setText(toqstr(rc.print_landscape_flag));
1568         printerToFileED->setText(toqstr(external_path(rc.print_to_file)));
1569         printerExtraED->setText(toqstr(rc.print_extra_options));
1570         printerSpoolPrefixED->setText(toqstr(rc.print_spool_printerprefix));
1571         printerPaperSizeED->setText(toqstr(rc.print_paper_dimension_flag));
1572 }
1573
1574
1575 /////////////////////////////////////////////////////////////////////
1576 //
1577 // PrefUserInterface
1578 //
1579 /////////////////////////////////////////////////////////////////////
1580
1581 PrefUserInterface::PrefUserInterface(GuiPreferences * form, QWidget * parent)
1582         : PrefModule(_("User interface"), form, parent)
1583 {
1584         setupUi(this);
1585
1586         connect(autoSaveCB, SIGNAL(toggled(bool)),
1587                 autoSaveSB, SLOT(setEnabled(bool)));
1588         connect(autoSaveCB, SIGNAL(toggled(bool)),
1589                 TextLabel1, SLOT(setEnabled(bool)));
1590         connect(uiFilePB, SIGNAL(clicked()),
1591                 this, SLOT(select_ui()));
1592         connect(uiFileED, SIGNAL(textChanged(const QString &)),
1593                 this, SIGNAL(changed()));
1594         connect(restoreCursorCB, SIGNAL(clicked()),
1595                 this, SIGNAL(changed()));
1596         connect(loadSessionCB, SIGNAL(clicked()),
1597                 this, SIGNAL(changed()));
1598         connect(loadWindowSizeCB, SIGNAL(clicked()),
1599                 this, SIGNAL(changed()));
1600         connect(loadWindowLocationCB, SIGNAL(clicked()),
1601                 this, SIGNAL(changed()));
1602         connect(windowWidthSB, SIGNAL(valueChanged(int)),
1603                 this, SIGNAL(changed()));
1604         connect(windowHeightSB, SIGNAL(valueChanged(int)),
1605                 this, SIGNAL(changed()));
1606         connect(cursorFollowsCB, SIGNAL(clicked()),
1607                 this, SIGNAL(changed()));
1608         connect(autoSaveSB, SIGNAL(valueChanged(int)),
1609                 this, SIGNAL(changed()));
1610         connect(autoSaveCB, SIGNAL(clicked()),
1611                 this, SIGNAL(changed()));
1612         connect(lastfilesSB, SIGNAL(valueChanged(int)),
1613                 this, SIGNAL(changed()));
1614         lastfilesSB->setMaximum(maxlastfiles);
1615 }
1616
1617
1618 void PrefUserInterface::apply(LyXRC & rc) const
1619 {
1620         rc.ui_file = internal_path(fromqstr(uiFileED->text()));
1621         rc.use_lastfilepos = restoreCursorCB->isChecked();
1622         rc.load_session = loadSessionCB->isChecked();
1623         if (loadWindowSizeCB->isChecked()) {
1624                 rc.geometry_width = 0;
1625                 rc.geometry_height = 0;
1626         } else {
1627                 rc.geometry_width = windowWidthSB->value();
1628                 rc.geometry_height = windowHeightSB->value();
1629         }
1630         rc.geometry_xysaved = loadWindowLocationCB->isChecked();
1631         rc.cursor_follows_scrollbar = cursorFollowsCB->isChecked();
1632         rc.autosave = autoSaveSB->value() * 60;
1633         rc.make_backup = autoSaveCB->isChecked();
1634         rc.num_lastfiles = lastfilesSB->value();
1635 }
1636
1637
1638 void PrefUserInterface::update(LyXRC const & rc)
1639 {
1640         uiFileED->setText(toqstr(external_path(rc.ui_file)));
1641         restoreCursorCB->setChecked(rc.use_lastfilepos);
1642         loadSessionCB->setChecked(rc.load_session);
1643         bool loadWindowSize = rc.geometry_width == 0 && rc.geometry_height == 0;
1644         loadWindowSizeCB->setChecked(loadWindowSize);
1645         if (!loadWindowSize) {
1646                 windowWidthSB->setValue(rc.geometry_width);
1647                 windowHeightSB->setValue(rc.geometry_height);
1648         }
1649         loadWindowLocationCB->setChecked(rc.geometry_xysaved);
1650         cursorFollowsCB->setChecked(rc.cursor_follows_scrollbar);
1651         // convert to minutes
1652         int mins(rc.autosave / 60);
1653         if (rc.autosave && !mins)
1654                 mins = 1;
1655         autoSaveSB->setValue(mins);
1656         autoSaveCB->setChecked(rc.make_backup);
1657         lastfilesSB->setValue(rc.num_lastfiles);
1658 }
1659
1660
1661 void PrefUserInterface::select_ui()
1662 {
1663         docstring const name =
1664                 from_utf8(internal_path(fromqstr(uiFileED->text())));
1665         docstring file = form_->browseUI(name);
1666         if (!file.empty())
1667                 uiFileED->setText(toqstr(file));
1668 }
1669
1670
1671 void PrefUserInterface::on_loadWindowSizeCB_toggled(bool loadwindowsize)
1672 {
1673         windowWidthLA->setDisabled(loadwindowsize);
1674         windowHeightLA->setDisabled(loadwindowsize);
1675         windowWidthSB->setDisabled(loadwindowsize);
1676         windowHeightSB->setDisabled(loadwindowsize);
1677 }
1678
1679
1680 /////////////////////////////////////////////////////////////////////
1681 //
1682 // PrefShortcuts
1683 //
1684 /////////////////////////////////////////////////////////////////////
1685
1686 PrefShortcuts::PrefShortcuts(GuiPreferences * form, QWidget * parent)
1687         : PrefModule(_("Shortcuts"), form, parent)
1688 {
1689         setupUi(this);
1690
1691         shortcutsTW->setColumnCount(2);
1692         shortcutsTW->headerItem()->setText(0, qt_("Function"));
1693         shortcutsTW->headerItem()->setText(1, qt_("Shortcut"));
1694         shortcutsTW->setSortingEnabled(true);
1695         shortcutsTW->header()->resizeSection(0, 200);
1696
1697         connect(bindFilePB, SIGNAL(clicked()),
1698                 this, SLOT(select_bind()));
1699         connect(bindFileED, SIGNAL(textChanged(const QString &)),
1700                 this, SIGNAL(changed()));
1701         
1702         shortcut_ = new GuiShortcutDialog(this);
1703 }
1704
1705
1706 void PrefShortcuts::apply(LyXRC & rc) const
1707 {
1708         rc.bind_file = internal_path(fromqstr(bindFileED->text()));
1709 }
1710
1711
1712 void PrefShortcuts::update(LyXRC const & rc)
1713 {
1714         bindFileED->setText(toqstr(external_path(rc.bind_file)));
1715         shortcutsTW->clear();
1716
1717         QTreeWidgetItem * editItem = new QTreeWidgetItem(shortcutsTW);
1718         editItem->setText(0, toqstr("Cursor, Mouse and Editing functions"));
1719
1720         // first insert a few categories
1721         QTreeWidgetItem * mathItem = new QTreeWidgetItem(shortcutsTW);
1722         mathItem->setText(0, toqstr("Mathematical Symbols"));
1723         
1724         QTreeWidgetItem * bufferItem = new QTreeWidgetItem(shortcutsTW);
1725         bufferItem->setText(0, toqstr("Buffer and Window"));
1726         
1727         QTreeWidgetItem * layoutItem = new QTreeWidgetItem(shortcutsTW);
1728         layoutItem->setText(0, toqstr("Font, Layouts and Textclasses"));
1729
1730         QTreeWidgetItem * systemItem = new QTreeWidgetItem(shortcutsTW);
1731         systemItem->setText(0, toqstr("System and Miscellaneous"));
1732
1733         // listBindings(unbound=true) lists all bound and unbound lfuns
1734         KeyMap::BindingList const bindinglist = theTopLevelKeymap().listBindings(true);
1735
1736         KeyMap::BindingList::const_iterator it = bindinglist.begin();
1737         KeyMap::BindingList::const_iterator it_end = bindinglist.end();
1738         for (; it != it_end; ++it) {
1739                 kb_action action = it->first.action;
1740                 string const action_name = lyxaction.getActionName(action);
1741                 QString const lfun = toqstr(from_utf8(action_name) 
1742                         + " " + it->first.argument());
1743                 QString const shortcut = toqstr(it->second.print(false));
1744                 
1745                 QTreeWidgetItem * newItem = NULL;
1746                 // if an item with the same lfun already exists, insert as a
1747                 // child item to that item.
1748                 // WARNING: this can be slow.
1749                 QList<QTreeWidgetItem*> items = shortcutsTW->findItems(lfun, 
1750                         Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive), 0);
1751                 if (!items.empty())
1752                         newItem = new QTreeWidgetItem(items[0]);
1753                 else {
1754                         switch(lyxaction.getActionType(action)) {
1755                         case LyXAction::Hidden:
1756                                 continue;
1757                         case LyXAction::Edit:
1758                                 newItem = new QTreeWidgetItem(editItem);
1759                                 break;
1760                         case LyXAction::Math:
1761                                 newItem = new QTreeWidgetItem(mathItem);
1762                                 break;
1763                         case LyXAction::Buffer:
1764                                 newItem = new QTreeWidgetItem(bufferItem);
1765                                 break;
1766                         case LyXAction::Layout:
1767                                 newItem = new QTreeWidgetItem(layoutItem);
1768                                 break;
1769                         case LyXAction::System:
1770                                 newItem = new QTreeWidgetItem(systemItem);
1771                                 break;
1772                         default:
1773                                 // this should not happen
1774                                 newItem = new QTreeWidgetItem(shortcutsTW);
1775                         }
1776                 }
1777
1778                 newItem->setText(0, lfun);
1779                 newItem->setText(1, shortcut);
1780                 // FIXME: TreeItem can not be user-checkable?
1781                 newItem->setFlags(newItem->flags() | Qt::ItemIsEditable
1782                         | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
1783         }
1784         shortcutsTW->sortItems(0, Qt::AscendingOrder);
1785 }
1786
1787
1788 void PrefShortcuts::select_bind()
1789 {
1790         docstring const name =
1791                 from_utf8(internal_path(fromqstr(bindFileED->text())));
1792         docstring file = form_->browsebind(name);
1793         if (!file.empty())
1794                 bindFileED->setText(toqstr(file));
1795 }
1796
1797
1798 void PrefShortcuts::on_newPB_pressed()
1799 {
1800         // FIXME
1801         shortcut_->exec();
1802 }
1803
1804
1805 void PrefShortcuts::on_modifyPB_pressed()
1806 {
1807         // FIXME
1808         shortcut_->exec();
1809 }
1810
1811
1812 void PrefShortcuts::on_removePB_pressed()
1813 {
1814         //
1815 }
1816
1817
1818 void PrefShortcuts::on_searchPB_pressed()
1819 {
1820         //
1821 }
1822
1823
1824 PrefIdentity::PrefIdentity(QWidget * parent)
1825         : PrefModule(_("Identity"), 0, parent)
1826 {
1827         setupUi(this);
1828
1829         connect(nameED, SIGNAL(textChanged(const QString&)),
1830                 this, SIGNAL(changed()));
1831         connect(emailED, SIGNAL(textChanged(const QString&)),
1832                 this, SIGNAL(changed()));
1833 }
1834
1835
1836 void PrefIdentity::apply(LyXRC & rc) const
1837 {
1838         rc.user_name = fromqstr(nameED->text());
1839         rc.user_email = fromqstr(emailED->text());
1840 }
1841
1842
1843 void PrefIdentity::update(LyXRC const & rc)
1844 {
1845         nameED->setText(toqstr(rc.user_name));
1846         emailED->setText(toqstr(rc.user_email));
1847 }
1848
1849
1850
1851 /////////////////////////////////////////////////////////////////////
1852 //
1853 // GuiPreferences
1854 //
1855 /////////////////////////////////////////////////////////////////////
1856
1857 GuiPreferences::GuiPreferences(LyXView & lv)
1858         : GuiDialog(lv, "prefs"), update_screen_font_(false)
1859 {
1860         setupUi(this);
1861         setViewTitle(_("Preferences"));
1862
1863         QDialog::setModal(false);
1864
1865         connect(savePB, SIGNAL(clicked()), this, SLOT(slotOK()));
1866         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
1867         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
1868         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
1869
1870         add(new PrefUserInterface(this));
1871         add(new PrefShortcuts(this));
1872         add(new PrefScreenFonts(this));
1873         add(new PrefColors(this));
1874         add(new PrefDisplay);
1875         add(new PrefKeyboard(this));
1876
1877         add(new PrefPaths(this));
1878
1879         add(new PrefIdentity);
1880
1881         add(new PrefLanguage);
1882         add(new PrefSpellchecker(this));
1883
1884         add(new PrefPrinter);
1885         add(new PrefDate);
1886         add(new PrefPlaintext);
1887         add(new PrefLatex(this));
1888
1889         PrefConverters * converters = new PrefConverters(this);
1890         PrefFileformats * formats = new PrefFileformats(this);
1891         connect(formats, SIGNAL(formatsChanged()),
1892                         converters, SLOT(updateGui()));
1893         add(converters);
1894         add(formats);
1895
1896         prefsPS->setCurrentPanel(_("User interface"));
1897 // FIXME: hack to work around resizing bug in Qt >= 4.2
1898 // bug verified with Qt 4.2.{0-3} (JSpitzm)
1899 #if QT_VERSION >= 0x040200
1900         prefsPS->updateGeometry();
1901 #endif
1902
1903         bc().setPolicy(ButtonPolicy::PreferencesPolicy);
1904         bc().setOK(savePB);
1905         bc().setApply(applyPB);
1906         bc().setCancel(closePB);
1907         bc().setRestore(restorePB);
1908 }
1909
1910
1911 void GuiPreferences::add(PrefModule * module)
1912 {
1913         BOOST_ASSERT(module);
1914         prefsPS->addPanel(module, module->title());
1915         connect(module, SIGNAL(changed()), this, SLOT(change_adaptor()));
1916         modules_.push_back(module);
1917 }
1918
1919
1920 void GuiPreferences::closeEvent(QCloseEvent * e)
1921 {
1922         slotClose();
1923         e->accept();
1924 }
1925
1926
1927 void GuiPreferences::change_adaptor()
1928 {
1929         changed();
1930 }
1931
1932
1933 void GuiPreferences::apply(LyXRC & rc) const
1934 {
1935         size_t end = modules_.size();
1936         for (size_t i = 0; i != end; ++i)
1937                 modules_[i]->apply(rc);
1938 }
1939
1940
1941 void GuiPreferences::updateRc(LyXRC const & rc)
1942 {
1943         size_t const end = modules_.size();
1944         for (size_t i = 0; i != end; ++i)
1945                 modules_[i]->update(rc);
1946 }
1947
1948
1949 void GuiPreferences::applyView()
1950 {
1951         apply(rc());
1952 }
1953
1954
1955 void GuiPreferences::updateContents()
1956 {
1957         updateRc(rc());
1958 }
1959
1960
1961 bool GuiPreferences::initialiseParams(std::string const &)
1962 {
1963         rc_ = lyxrc;
1964         formats_ = lyx::formats;
1965         converters_ = theConverters();
1966         converters_.update(formats_);
1967         movers_ = theMovers();
1968         colors_.clear();
1969         update_screen_font_ = false;
1970
1971         return true;
1972 }
1973
1974
1975 void GuiPreferences::dispatchParams()
1976 {
1977         ostringstream ss;
1978         rc_.write(ss, true);
1979         dispatch(FuncRequest(LFUN_LYXRC_APPLY, ss.str())); 
1980         // FIXME: these need lfuns
1981         // FIXME UNICODE
1982         theBufferList().setCurrentAuthor(from_utf8(rc_.user_name), from_utf8(rc_.user_email));
1983
1984         lyx::formats = formats_;
1985
1986         theConverters() = converters_;
1987         theConverters().update(lyx::formats);
1988         theConverters().buildGraph();
1989
1990         theMovers() = movers_;
1991
1992         vector<string>::const_iterator it = colors_.begin();
1993         vector<string>::const_iterator const end = colors_.end();
1994         for (; it != end; ++it)
1995                 dispatch(FuncRequest(LFUN_SET_COLOR, *it));
1996         colors_.clear();
1997
1998         if (update_screen_font_) {
1999                 dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
2000                 update_screen_font_ = false;
2001         }
2002
2003         // The Save button has been pressed
2004         if (isClosing())
2005                 dispatch(FuncRequest(LFUN_PREFERENCES_SAVE));
2006 }
2007
2008
2009 void GuiPreferences::setColor(Color_color col, string const & hex)
2010 {
2011         colors_.push_back(lcolor.getLyXName(col) + ' ' + hex);
2012 }
2013
2014
2015 void GuiPreferences::updateScreenFonts()
2016 {
2017         update_screen_font_ = true;
2018 }
2019
2020
2021 docstring const GuiPreferences::browsebind(docstring const & file) const
2022 {
2023         return browseLibFile(from_ascii("bind"), file, from_ascii("bind"),
2024                              _("Choose bind file"),
2025                              FileFilterList(_("LyX bind files (*.bind)")));
2026 }
2027
2028
2029 docstring const GuiPreferences::browseUI(docstring const & file) const
2030 {
2031         return browseLibFile(from_ascii("ui"), file, from_ascii("ui"),
2032                              _("Choose UI file"),
2033                              FileFilterList(_("LyX UI files (*.ui)")));
2034 }
2035
2036
2037 docstring const GuiPreferences::browsekbmap(docstring const & file) const
2038 {
2039         return browseLibFile(from_ascii("kbd"), file, from_ascii("kmap"),
2040                              _("Choose keyboard map"),
2041                              FileFilterList(_("LyX keyboard maps (*.kmap)")));
2042 }
2043
2044
2045 docstring const GuiPreferences::browsedict(docstring const & file) const
2046 {
2047         if (lyxrc.use_spell_lib)
2048                 return browseFile(file,
2049                                   _("Choose personal dictionary"),
2050                                   FileFilterList(_("*.pws")));
2051         else
2052                 return browseFile(file,
2053                                   _("Choose personal dictionary"),
2054                                   FileFilterList(_("*.ispell")));
2055 }
2056
2057
2058 docstring const GuiPreferences::browse(docstring const & file,
2059                                   docstring const & title) const
2060 {
2061         return browseFile(file, title, FileFilterList(), true);
2062 }
2063
2064
2065 docstring const GuiPreferences::browsedir(docstring const & path,
2066                                      docstring const & title) const
2067 {
2068         return browseDir(path, title);
2069 }
2070
2071
2072 // We support less paper sizes than the document dialog
2073 // Therefore this adjustment is needed.
2074 PAPER_SIZE GuiPreferences::toPaperSize(int i) const
2075 {
2076         switch (i) {
2077         case 0:
2078                 return PAPER_DEFAULT;
2079         case 1:
2080                 return PAPER_USLETTER;
2081         case 2:
2082                 return PAPER_USLEGAL;
2083         case 3:
2084                 return PAPER_USEXECUTIVE;
2085         case 4:
2086                 return PAPER_A3;
2087         case 5:
2088                 return PAPER_A4;
2089         case 6:
2090                 return PAPER_A5;
2091         case 7:
2092                 return PAPER_B5;
2093         default:
2094                 // should not happen
2095                 return PAPER_DEFAULT;
2096         }
2097 }
2098
2099
2100 int GuiPreferences::fromPaperSize(PAPER_SIZE papersize) const
2101 {
2102         switch (papersize) {
2103         case PAPER_DEFAULT:
2104                 return 0;
2105         case PAPER_USLETTER:
2106                 return 1;
2107         case PAPER_USLEGAL:
2108                 return 2;
2109         case PAPER_USEXECUTIVE:
2110                 return 3;
2111         case PAPER_A3:
2112                 return 4;
2113         case PAPER_A4:
2114                 return 5;
2115         case PAPER_A5:
2116                 return 6;
2117         case PAPER_B5:
2118                 return 7;
2119         default:
2120                 // should not happen
2121                 return 0;
2122         }
2123 }
2124
2125
2126 Dialog * createGuiPreferences(LyXView & lv) { return new GuiPreferences(lv); }
2127
2128
2129 } // namespace frontend
2130 } // namespace lyx
2131
2132 #include "GuiPrefs_moc.cpp"