]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormDocument.C
the qtclean-2 patch with some very small changes
[lyx.git] / src / frontends / xforms / FormDocument.C
1 /**
2  * \file FormDocument.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include FORMS_H_LOCATION
14 #include XPM_H_LOCATION
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "FormDocument.h"
21 #include "forms/form_document.h"
22 #include "Alert.h"
23 #include "lyxtextclasslist.h"
24 #include "combox.h"
25 #include "tex-strings.h"
26 #include "bufferparams.h"
27 #include "vspace.h"
28 #include "bmtable.h"
29 #include "language.h"
30 #include "frnt_lang.h"
31 #include "frontends/LyXView.h"
32 #include "lyxfunc.h"
33 #include "lyxrc.h"
34 #include "BufferView.h"
35 #include "buffer.h"
36 #include "CutAndPaste.h"
37 #include "bufferview_funcs.h"
38 #include "xforms_helpers.h"
39 #include "debug.h"
40 #include "input_validators.h" // fl_unsigned_float_filter
41 #include "helper_funcs.h"
42 #include "lyx_main.h" // for user_lyxdir
43
44 #include "insets/insetquotes.h"
45
46 #include "support/filetools.h"
47 #include "support/lstrings.h"
48
49 #include FORMS_H_LOCATION
50
51 #include <boost/bind.hpp>
52
53 #include <functional>
54
55 using std::bind2nd;
56 using std::vector;
57
58
59 FormDocument::FormDocument(LyXView & lv, Dialogs & d)
60         : FormBaseBD(lv, d, _("Document Layout")),
61           ActCell(0), Confirmed(0),
62           current_bullet_panel(0), current_bullet_depth(0), fbullet(0)
63 {
64 }
65
66
67 void FormDocument::redraw()
68 {
69         if (form() && form()->visible)
70                 fl_redraw_form(form());
71         else
72                 return;
73
74         FL_FORM * outer_form = fl_get_active_folder(dialog_->tabfolder);
75         if (outer_form && outer_form->visible)
76                 fl_redraw_form(outer_form);
77 }
78
79
80 FL_FORM * FormDocument::form() const
81 {
82         if (dialog_.get()) return dialog_->form;
83         return 0;
84 }
85
86
87 void FormDocument::build()
88 {
89         int n;
90
91         // the tabbed folder
92         dialog_.reset(build_document(this));
93
94         // Allow the base class to control messages
95         setMessageWidget(dialog_->text_warning);
96
97         // Manage the restore, ok, apply, restore and cancel/close buttons
98         bc().setOK(dialog_->button_ok);
99         bc().setApply(dialog_->button_apply);
100         bc().setCancel(dialog_->button_close);
101         bc().setRestore(dialog_->button_restore);
102         bc().addReadOnly (dialog_->button_save_defaults);
103         bc().addReadOnly (dialog_->button_reset_defaults);
104
105         // the document paper form
106         paper_.reset(build_document_paper(this));
107         fl_addto_choice(paper_->choice_papersize,
108                         _(" Default | Custom | US letter | US legal "
109                           "| US executive | A3 | A4 | A5 | B3 | B4 | B5 "));
110         fl_addto_choice(paper_->choice_paperpackage,
111                         _(" None "
112                           "| Small Margins "
113                           "| Very small Margins "
114                           "| Very wide Margins "));
115
116         fl_set_input_return(paper_->input_custom_width,  FL_RETURN_CHANGED);
117         fl_set_input_return(paper_->input_custom_height, FL_RETURN_CHANGED);
118         fl_set_input_return(paper_->input_top_margin,    FL_RETURN_CHANGED);
119         fl_set_input_return(paper_->input_bottom_margin, FL_RETURN_CHANGED);
120         fl_set_input_return(paper_->input_inner_margin,  FL_RETURN_CHANGED);
121         fl_set_input_return(paper_->input_outer_margin,  FL_RETURN_CHANGED);
122         fl_set_input_return(paper_->input_head_height,   FL_RETURN_CHANGED);
123         fl_set_input_return(paper_->input_head_sep,      FL_RETURN_CHANGED);
124         fl_set_input_return(paper_->input_foot_skip,     FL_RETURN_CHANGED);
125
126         setPrehandler(paper_->input_custom_width);
127         setPrehandler(paper_->input_custom_height);
128         setPrehandler(paper_->input_top_margin);
129         setPrehandler(paper_->input_bottom_margin);
130         setPrehandler(paper_->input_inner_margin);
131         setPrehandler(paper_->input_outer_margin);
132         setPrehandler(paper_->input_head_height);
133         setPrehandler(paper_->input_head_sep);
134         setPrehandler(paper_->input_foot_skip);
135
136         // Create the contents of the unit choices
137         // Don't include the "%" terms...
138         vector<string> units_vec = getLatexUnits();
139         vector<string>::iterator ret =
140                 std::remove_if(units_vec.begin(),
141                                units_vec.end(),
142                                bind2nd(contains_functor(), "%"));
143         units_vec.erase(ret, units_vec.end());
144
145         string units = getStringFromVector(units_vec, "|");
146
147         fl_addto_choice(paper_->choice_custom_width_units,  units.c_str());
148         fl_addto_choice(paper_->choice_custom_height_units, units.c_str());
149         fl_addto_choice(paper_->choice_top_margin_units,    units.c_str());
150         fl_addto_choice(paper_->choice_bottom_margin_units, units.c_str());
151         fl_addto_choice(paper_->choice_inner_margin_units,  units.c_str());
152         fl_addto_choice(paper_->choice_outer_margin_units,  units.c_str());
153         fl_addto_choice(paper_->choice_head_height_units,   units.c_str());
154         fl_addto_choice(paper_->choice_head_sep_units,      units.c_str());
155         fl_addto_choice(paper_->choice_foot_skip_units,     units.c_str());
156
157         bc().addReadOnly (paper_->choice_paperpackage);
158         bc().addReadOnly (paper_->radio_portrait);
159         bc().addReadOnly (paper_->radio_landscape);
160         bc().addReadOnly (paper_->choice_papersize);
161         bc().addReadOnly (paper_->check_use_geometry);
162         bc().addReadOnly (paper_->input_custom_width);
163         bc().addReadOnly (paper_->input_custom_height);
164         bc().addReadOnly (paper_->input_top_margin);
165         bc().addReadOnly (paper_->input_bottom_margin);
166         bc().addReadOnly (paper_->input_inner_margin);
167         bc().addReadOnly (paper_->input_outer_margin);
168         bc().addReadOnly (paper_->input_head_height);
169         bc().addReadOnly (paper_->input_head_sep);
170         bc().addReadOnly (paper_->input_foot_skip);
171
172         // the document class form
173         class_.reset(build_document_class(this));
174
175         FL_OBJECT * obj;
176         // The language is a combo-box and has to be inserted manually
177         obj = class_->choice_doc_class;
178         fl_deactivate_object(obj);
179         fl_addto_form(class_->form);
180         combo_doc_class.reset(new Combox(FL_COMBOX_DROPLIST));
181         combo_doc_class->add(obj->x, obj->y, obj->w, obj->h, 400);
182         combo_doc_class->shortcut("#C",1);
183         combo_doc_class->setcallback(ComboInputCB, this);
184         fl_end_form();
185         for (LyXTextClassList::const_iterator cit = textclasslist.begin();
186              cit != textclasslist.end(); ++cit) {
187                 combo_doc_class->addto(cit->description());
188         }
189
190         fl_addto_choice(class_->choice_doc_spacing,
191                         _(" Single | OneHalf | Double | Custom "));
192         fl_addto_choice(class_->choice_doc_fontsize, "default|10|11|12");
193         for (n=0; tex_fonts[n][0]; ++n) {
194                 fl_addto_choice(class_->choice_doc_fonts,tex_fonts[n]);
195         }
196         fl_addto_choice(class_->choice_doc_pagestyle,
197                         "default|empty|plain|headings|fancy");
198         fl_addto_choice(class_->choice_doc_skip,
199                         _(" Smallskip | Medskip | Bigskip | Length "));
200         fl_addto_choice(class_->choice_default_skip_units,  units.c_str());
201         fl_set_input_return(class_->input_doc_extra, FL_RETURN_CHANGED);
202         fl_set_input_return(class_->input_doc_skip, FL_RETURN_CHANGED);
203         fl_set_input_return(class_->input_doc_spacing, FL_RETURN_CHANGED);
204
205         setPrehandler(class_->input_doc_extra);
206         setPrehandler(class_->input_doc_skip);
207         setPrehandler(class_->input_doc_spacing);
208
209         // Set input filters on doc spacing to make it accept only
210         // unsigned numbers.
211         fl_set_input_filter(class_->input_doc_spacing, fl_unsigned_float_filter);
212
213         bc().addReadOnly (class_->radio_doc_indent);
214         bc().addReadOnly (class_->radio_doc_skip);
215
216         bc().addReadOnly (class_->choice_doc_pagestyle);
217         bc().addReadOnly (class_->choice_doc_fonts);
218         bc().addReadOnly (class_->choice_doc_fontsize);
219         bc().addReadOnly (class_->radio_doc_sides_one);
220         bc().addReadOnly (class_->radio_doc_sides_two);
221         bc().addReadOnly (class_->radio_doc_columns_one);
222         bc().addReadOnly (class_->radio_doc_columns_two);
223         bc().addReadOnly (class_->input_doc_extra);
224         bc().addReadOnly (class_->input_doc_skip);
225         bc().addReadOnly (class_->choice_doc_skip);
226         bc().addReadOnly (class_->choice_doc_spacing);
227         bc().addReadOnly (class_->input_doc_spacing);
228
229         // the document language form
230         language_.reset(build_document_language(this));
231         fl_addto_choice(language_->choice_inputenc,
232                         "default|auto|latin1|latin2|latin3|latin4|latin5|latin9"
233                         "|koi8-r|koi8-u|cp866|cp1251|iso88595|pt154");
234
235         vector<frnt::LanguagePair> const langs = frnt::getLanguageData(false);
236         // Store the identifiers for later
237         lang_ = getSecond(langs);
238
239         // The language is a combo-box and has to be inserted manually
240         obj = language_->choice_language;
241         fl_deactivate_object(obj);
242         fl_addto_form(language_->form);
243         combo_language.reset(new Combox(FL_COMBOX_DROPLIST));
244         combo_language->add(obj->x, obj->y, obj->w, obj->h, 400);
245         combo_language->shortcut("#L",1);
246         combo_language->setcallback(ComboInputCB, this);
247         fl_end_form();
248
249         vector<frnt::LanguagePair>::const_iterator lit  = langs.begin();
250         vector<frnt::LanguagePair>::const_iterator lend = langs.end();
251         for (; lit != lend; ++lit) {
252                 combo_language->addto(lit->first);
253         }
254         combo_language->select(1);
255
256         fl_addto_choice(language_->choice_quotes_language,
257                         _(" ``text'' | ''text'' | ,,text`` | ,,text'' |"
258                           " «text» | »text« "));
259
260         bc().addReadOnly (language_->choice_inputenc);
261         bc().addReadOnly (language_->choice_quotes_language);
262         bc().addReadOnly (language_->radio_single);
263         bc().addReadOnly (language_->radio_double);
264
265         // the document options form
266         options_.reset(build_document_options(this));
267         fl_set_input_return(options_->input_float_placement, FL_RETURN_CHANGED);
268         setPrehandler(options_->input_float_placement);
269
270         fl_set_counter_bounds(options_->counter_secnumdepth,-2,5);
271         fl_set_counter_bounds(options_->counter_tocdepth,-1,5);
272         fl_set_counter_step(options_->counter_secnumdepth,1,1);
273         fl_set_counter_step(options_->counter_tocdepth,1,1);
274         fl_set_counter_precision(options_->counter_secnumdepth, 0);
275         fl_set_counter_precision(options_->counter_tocdepth, 0);
276         for (n=0; tex_graphics[n][0]; ++n) {
277                 fl_addto_choice(options_->choice_postscript_driver,
278                                 tex_graphics[n]);
279         }
280         fl_addto_choice(options_->choice_citation_format,
281                         _(" Author-year | Numerical "));
282
283         bc_.addReadOnly (options_->counter_secnumdepth);
284         bc_.addReadOnly (options_->counter_tocdepth);
285         bc_.addReadOnly (options_->check_use_amsmath);
286         bc_.addReadOnly (options_->check_use_natbib);
287         bc_.addReadOnly (options_->choice_citation_format);
288         bc_.addReadOnly (options_->input_float_placement);
289         bc_.addReadOnly (options_->choice_postscript_driver);
290
291         // the document bullets form
292         bullets_.reset(build_document_bullet(this));
293         fl_addto_choice(bullets_->choice_bullet_size,
294                         _(" default | tiny | script | footnote | small |"
295                           " normal | large | Large | LARGE | huge | Huge"));
296         fl_set_choice(bullets_->choice_bullet_size, 1);
297         fl_set_input_return(bullets_->input_bullet_latex, FL_RETURN_CHANGED);
298         setPrehandler(bullets_->input_bullet_latex);
299         fl_set_input_maxchars(bullets_->input_bullet_latex, 80);
300
301         string const bmtablefile = LibFileSearch("images", "standard", "xpm");
302         fl_set_bmtable_pixmap_file(bullets_->bmtable_bullet_panel, 6, 6,
303                                    bmtablefile.c_str());
304
305         bc_.addReadOnly(bullets_->radio_bullet_depth_1);
306         bc_.addReadOnly(bullets_->radio_bullet_depth_2);
307         bc_.addReadOnly(bullets_->radio_bullet_depth_3);
308         bc_.addReadOnly(bullets_->radio_bullet_depth_4);
309         bc_.addReadOnly(bullets_->radio_bullet_panel_standard);
310         bc_.addReadOnly(bullets_->radio_bullet_panel_maths);
311         bc_.addReadOnly(bullets_->radio_bullet_panel_ding1);
312         bc_.addReadOnly(bullets_->radio_bullet_panel_ding2);
313         bc_.addReadOnly(bullets_->radio_bullet_panel_ding3);
314         bc_.addReadOnly(bullets_->radio_bullet_panel_ding4);
315
316         bc().addReadOnly (bullets_->bmtable_bullet_panel);
317         bc().addReadOnly (bullets_->choice_bullet_size);
318         bc().addReadOnly (bullets_->input_bullet_latex);
319
320         fl_addto_tabfolder(dialog_->tabfolder,_("Document"),
321                            class_->form);
322         fl_addto_tabfolder(dialog_->tabfolder,_("Paper"),
323                            paper_->form);
324         fl_addto_tabfolder(dialog_->tabfolder,_("Language"),
325                            language_->form);
326         fl_addto_tabfolder(dialog_->tabfolder,_("Extra"),
327                            options_->form);
328         fbullet = fl_addto_tabfolder(dialog_->tabfolder,_("Bullets"),
329                                      bullets_->form);
330
331         // work-around xforms bug re update of folder->x, folder->y coords.
332         setPrehandler(dialog_->tabfolder);
333
334         if ((XpmVersion < 4) || (XpmVersion == 4 && XpmRevision < 7)) {
335                 lyxerr << _("Your version of libXpm is older than 4.7.\n"
336                             "The `bullet' tab of the document dialog "
337                             "has been disabled") << '\n';
338                 fl_deactivate_object(fbullet);
339                 fl_set_object_lcol(fbullet, FL_INACTIVE);
340         }
341 }
342
343
344 void FormDocument::apply()
345 {
346         if (!lv_.view()->available() || !dialog_.get())
347                 return;
348
349         bool redo = class_apply();
350         paper_apply();
351         redo = language_apply() || redo;
352         redo = options_apply() || redo;
353         bullets_apply();
354
355         if (redo) {
356                 lv_.view()->redoCurrentBuffer();
357         }
358         lv_.buffer()->markDirty();
359         lv_.message(_("Document layout set"));
360 }
361
362
363 void FormDocument::cancel()
364 {
365         // this avoids confusion when reopening
366         BufferParams & param = lv_.buffer()->params;
367         param.temp_bullets[0] = param.user_defined_bullets[0];
368         param.temp_bullets[1] = param.user_defined_bullets[1];
369         param.temp_bullets[2] = param.user_defined_bullets[2];
370         param.temp_bullets[3] = param.user_defined_bullets[3];
371         hide();
372 }
373
374
375 void FormDocument::update()
376 {
377         if (!dialog_.get())
378                 return;
379
380         checkReadOnly();
381
382         BufferParams const & params = lv_.buffer()->params;
383
384         class_update(params);
385         paper_update(params);
386         language_update(params);
387         options_update(params);
388         bullets_update(params);
389 }
390
391
392 namespace {
393 // should this go elsewhere? Maybe a ControllerDocument? (JMarc)
394 /** Save the buffer's parameters as user default.
395     This function saves a file \c user_lyxdir/templates/defaults.lyx
396     which parameters are those of the current buffer. This file
397     is used as a default template when creating a new
398     file. Returns \c true on success.
399 */
400 bool saveParamsAsDefault(BufferParams const &params)
401 {
402         if (!Alert::askQuestion(_("Do you want to save the current settings"),
403                                 _("for the document layout as default?"),
404                                 _("(they will be valid for any new document)")))
405                 return false;
406
407         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
408                                      "defaults.lyx");
409         Buffer defaults(fname);
410         defaults.params = params;
411
412         // add an empty paragraph. Is this enough?
413         Paragraph * par = new Paragraph;
414         par->layout(params.getLyXTextClass().defaultLayout());
415         defaults.paragraphs.set(par);
416
417         return defaults.writeFile(defaults.fileName());
418 }
419
420 } //namespace
421
422
423 bool FormDocument::input(FL_OBJECT * ob, long)
424 {
425         if (ob == class_->choice_doc_class) {
426                 CheckChoiceClass(ob, 0);
427         } else if (ob == bullets_->choice_bullet_size) {
428                 ChoiceBulletSize(ob, 0);
429         } else if (ob == bullets_->input_bullet_latex) {
430                 InputBulletLaTeX(ob, 0);
431         } else if (ob == bullets_->radio_bullet_depth_1 ||
432                    ob == bullets_->radio_bullet_depth_2 ||
433                    ob == bullets_->radio_bullet_depth_3 ||
434                    ob == bullets_->radio_bullet_depth_4) {
435                 BulletDepth(ob);
436         } else if (ob == bullets_->radio_bullet_panel_standard ||
437                    ob == bullets_->radio_bullet_panel_maths ||
438                    ob == bullets_->radio_bullet_panel_ding1 ||
439                    ob == bullets_->radio_bullet_panel_ding2 ||
440                    ob == bullets_->radio_bullet_panel_ding3 ||
441                    ob == bullets_->radio_bullet_panel_ding4) {
442                 BulletPanel(ob);
443         } else if (ob == bullets_->bmtable_bullet_panel) {
444                 BulletBMTable(ob, 0);
445         }
446
447         bool const length_input = fl_get_choice(class_->choice_doc_skip) == 4;
448         if (ob == class_->choice_doc_skip) {
449                 setEnabled(class_->input_doc_skip, length_input);
450                 setEnabled(class_->choice_default_skip_units, length_input);
451         }
452
453         if (ob == class_->choice_doc_spacing)
454                 setEnabled(class_->input_doc_spacing,
455                            fl_get_choice(class_->choice_doc_spacing) == 4);
456
457         bool const skip_used = fl_get_button(class_->radio_doc_skip);
458         if (ob == class_->radio_doc_skip ||
459             ob == class_->radio_doc_indent) {
460                 setEnabled(class_->choice_doc_skip, skip_used);
461                 setEnabled(class_->input_doc_skip,
462                            skip_used && length_input);
463                 setEnabled(class_->choice_default_skip_units,
464                            skip_used && length_input);
465                 // Default unit choice is cm if metric, inches if US paper.
466                 // If papersize is default, check the lyxrc-settings
467                 int const paperchoice = fl_get_choice(paper_->choice_papersize);
468                 bool const metric = (paperchoice == 1 && lyxrc.default_papersize > 3)
469                         || paperchoice == 2 || paperchoice > 5;
470                 int const default_unit = metric ? 8 : 9;
471                 if (rtrim(fl_get_input(class_->input_doc_skip)).empty())
472                         fl_set_choice(class_->choice_default_skip_units,
473                                       default_unit);
474         }
475
476         if (ob == options_->check_use_natbib) {
477                 setEnabled(options_->choice_citation_format,
478                            fl_get_button(options_->check_use_natbib));
479         }
480
481         if (ob == dialog_->button_save_defaults) {
482                 BufferParams params;
483                 class_apply(params);
484                 paper_apply(params);
485                 language_apply(params);
486                 options_apply(params);
487                 bullets_apply(params);
488                 params.preamble = lv_.buffer()->params.preamble;
489                 saveParamsAsDefault(params);
490         }
491
492         if (ob == dialog_->button_reset_defaults) {
493                 BufferParams params = lv_.buffer()->params;
494                 params.textclass = combo_doc_class->get() - 1;
495                 params.useClassDefaults();
496                 UpdateLayoutDocument(params);
497         }
498
499         if (ob == paper_->radio_landscape)
500                 fl_set_choice(paper_->choice_paperpackage,
501                               BufferParams::PACKAGE_NONE + 1);
502
503         if (ob == paper_->choice_papersize) {
504                 int const paperchoice = fl_get_choice(paper_->choice_papersize);
505                 bool const defsize = paperchoice == 1;
506                 bool const custom = paperchoice == 2;
507                 bool const a3size = paperchoice == 6;
508                 bool const b3size = paperchoice == 9;
509                 bool const b4size = paperchoice == 10;
510
511                 if (custom)
512                         fl_set_button(paper_->radio_portrait, 1);
513
514                 bool const use_geom = (custom || a3size || b3size || b4size);
515
516                 fl_set_button(paper_->check_use_geometry, int(use_geom));
517
518                 setEnabled(paper_->input_top_margin,    use_geom);
519                 setEnabled(paper_->input_bottom_margin, use_geom);
520                 setEnabled(paper_->input_inner_margin,  use_geom);
521                 setEnabled(paper_->input_outer_margin,  use_geom);
522                 setEnabled(paper_->input_head_height,   use_geom);
523                 setEnabled(paper_->input_head_sep,      use_geom);
524                 setEnabled(paper_->input_foot_skip,     use_geom);
525                 setEnabled(paper_->choice_top_margin_units,    use_geom);
526                 setEnabled(paper_->choice_bottom_margin_units, use_geom);
527                 setEnabled(paper_->choice_inner_margin_units,  use_geom);
528                 setEnabled(paper_->choice_outer_margin_units,  use_geom);
529                 setEnabled(paper_->choice_head_height_units,   use_geom);
530                 setEnabled(paper_->choice_head_sep_units,      use_geom);
531                 setEnabled(paper_->choice_foot_skip_units,     use_geom);
532                 setEnabled(paper_->choice_custom_width_units,  use_geom);
533                 setEnabled(paper_->choice_custom_height_units, use_geom);
534
535                 setEnabled(paper_->input_custom_width,  custom);
536                 setEnabled(paper_->input_custom_height, custom);
537                 setEnabled(paper_->choice_custom_width_units,  custom);
538                 setEnabled(paper_->choice_custom_height_units, custom);
539                 setEnabled(paper_->radio_portrait,  !custom);
540                 setEnabled(paper_->radio_landscape, !custom);
541
542                 // Default unit choice is cm if metric, inches if US paper.
543                 // If papersize is default, use the lyxrc-settings
544                 bool const metric = (defsize && lyxrc.default_papersize > 3)
545                         || paperchoice == 2 || paperchoice > 5;
546                 int const default_unit = metric ? 8 : 9;
547                 if (rtrim(fl_get_input(paper_->input_custom_width)).empty())
548                         fl_set_choice(paper_->choice_custom_width_units,
549                                       default_unit);
550                 if (rtrim(fl_get_input(paper_->input_custom_height)).empty())
551                         fl_set_choice(paper_->choice_custom_height_units,
552                                       default_unit);
553                 if (rtrim(fl_get_input(paper_->input_top_margin)).empty())
554                         fl_set_choice(paper_->choice_top_margin_units,
555                                       default_unit);
556                 if (rtrim(fl_get_input(paper_->input_bottom_margin)).empty())
557                         fl_set_choice(paper_->choice_bottom_margin_units,
558                                       default_unit);
559                 if (rtrim(fl_get_input(paper_->input_inner_margin)).empty())
560                         fl_set_choice(paper_->choice_inner_margin_units,
561                                       default_unit);
562                 if (rtrim(fl_get_input(paper_->input_outer_margin)).empty())
563                         fl_set_choice(paper_->choice_outer_margin_units,
564                                       default_unit);
565                 if (rtrim(fl_get_input(paper_->input_head_height)).empty())
566                         fl_set_choice(paper_->choice_head_height_units,
567                                       default_unit);
568                 if (rtrim(fl_get_input(paper_->input_head_sep)).empty())
569                         fl_set_choice(paper_->choice_head_sep_units,
570                                       default_unit);
571                 if (rtrim(fl_get_input(paper_->input_foot_skip)).empty())
572                         fl_set_choice(paper_->choice_foot_skip_units,
573                                       default_unit);
574         }
575
576         if (ob == paper_->choice_papersize ||
577             ob == paper_->radio_portrait ||
578             ob == paper_->radio_landscape) {
579
580                 setEnabled(paper_->choice_paperpackage,
581                            //either default papersize (preferences)
582                            //or document papersize has to be A4
583                            (fl_get_choice(paper_->choice_papersize) == 7
584                             || fl_get_choice(paper_->choice_papersize) == 1
585                             && lyxrc.default_papersize == 5)
586                            && fl_get_button(paper_->radio_portrait));
587         }
588
589         if (ob == paper_->choice_paperpackage) {
590                 if (fl_get_choice(paper_->choice_paperpackage) != 1) {
591                         fl_set_button(paper_->check_use_geometry, 0);
592
593                         setEnabled(paper_->input_top_margin,    false);
594                         setEnabled(paper_->input_bottom_margin, false);
595                         setEnabled(paper_->input_inner_margin,  false);
596                         setEnabled(paper_->input_outer_margin,  false);
597                         setEnabled(paper_->input_head_height,   false);
598                         setEnabled(paper_->input_head_sep,      false);
599                         setEnabled(paper_->input_foot_skip,     false);
600                         setEnabled(paper_->choice_top_margin_units,    false);
601                         setEnabled(paper_->choice_bottom_margin_units, false);
602                         setEnabled(paper_->choice_inner_margin_units,  false);
603                         setEnabled(paper_->choice_outer_margin_units,  false);
604                         setEnabled(paper_->choice_head_height_units,   false);
605                         setEnabled(paper_->choice_head_sep_units,      false);
606                         setEnabled(paper_->choice_foot_skip_units,     false);
607                 }
608         }
609
610         if (ob == paper_->check_use_geometry) {
611                 //don't allow switching geometry off in custom papersize mode
612                 //nor in A3, B3, and B4
613                 int const choice = fl_get_choice(paper_->choice_papersize);
614                 if (choice == 2 || choice == 6 || choice == 9 || choice == 10)
615                         fl_set_button(paper_->check_use_geometry, 1);
616
617                 fl_set_choice(paper_->choice_paperpackage,
618                               BufferParams::PACKAGE_NONE + 1);
619
620                 bool const use_geom = fl_get_button(paper_->check_use_geometry);
621                 setEnabled(paper_->input_top_margin,    use_geom);
622                 setEnabled(paper_->input_bottom_margin, use_geom);
623                 setEnabled(paper_->input_inner_margin,  use_geom);
624                 setEnabled(paper_->input_outer_margin,  use_geom);
625                 setEnabled(paper_->input_head_height,   use_geom);
626                 setEnabled(paper_->input_head_sep,      use_geom);
627                 setEnabled(paper_->input_foot_skip,     use_geom);
628                 setEnabled(paper_->choice_top_margin_units,    use_geom);
629                 setEnabled(paper_->choice_bottom_margin_units, use_geom);
630                 setEnabled(paper_->choice_inner_margin_units,  use_geom);
631                 setEnabled(paper_->choice_outer_margin_units,  use_geom);
632                 setEnabled(paper_->choice_head_height_units,   use_geom);
633                 setEnabled(paper_->choice_head_sep_units,      use_geom);
634                 setEnabled(paper_->choice_foot_skip_units,     use_geom);
635         }
636
637         return CheckDocumentInput(ob, 0);
638 }
639
640
641 void FormDocument::ComboInputCB(int, void * v, Combox * combox)
642 {
643         FormDocument * pre = static_cast<FormDocument*>(v);
644         if (combox == pre->combo_doc_class.get())
645                 pre->CheckChoiceClass(0, 0);
646         pre->bc().valid(pre->CheckDocumentInput(0,0));
647 }
648
649
650 bool FormDocument::class_apply(BufferParams &params)
651 {
652         bool redo = false;
653
654         // If default skip is a "Length" but there's no text in the
655         // input field, reset the kind to "Medskip", which is the default.
656         if ((fl_get_choice (class_->choice_doc_skip) == 4) &&
657             !*(fl_get_input (class_->input_doc_skip))) {
658                 fl_set_choice (class_->choice_doc_skip, 2);
659         }
660         params.fonts = fl_get_choice_text(class_->choice_doc_fonts);
661         params.fontsize = fl_get_choice_text(class_->choice_doc_fontsize);
662         params.pagestyle = fl_get_choice_text(class_->choice_doc_pagestyle);
663
664         params.textclass = combo_doc_class->get() - 1;
665
666         BufferParams::PARSEP tmpsep = params.paragraph_separation;
667         if (fl_get_button(class_->radio_doc_indent))
668                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
669         else
670                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
671         if (tmpsep != params.paragraph_separation)
672                 redo = true;
673
674         VSpace tmpdefskip = params.getDefSkip();
675         switch (fl_get_choice (class_->choice_doc_skip)) {
676         case 1:
677                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
678                 break;
679         case 2:
680                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
681                 break;
682         case 3:
683                 params.setDefSkip(VSpace(VSpace::BIGSKIP));
684                 break;
685         case 4:
686         {
687                 string const length =
688                         getLengthFromWidgets(class_->input_doc_skip,
689                                              class_->choice_default_skip_units);
690
691                 params.setDefSkip(VSpace(LyXGlueLength(length)));
692                 break;
693         }
694         default:
695                 // DocumentDefskipCB assures that this never happens
696                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
697                 break;
698         }
699         if (!(tmpdefskip == params.getDefSkip()))
700                 redo = true;
701
702         if (fl_get_button(class_->radio_doc_columns_two))
703                 params.columns = 2;
704         else
705                 params.columns = 1;
706         if (fl_get_button(class_->radio_doc_sides_two))
707                 params.sides = LyXTextClass::TwoSides;
708         else
709                 params.sides = LyXTextClass::OneSide;
710
711         Spacing tmpSpacing = params.spacing;
712         switch (fl_get_choice(class_->choice_doc_spacing)) {
713         case 1:
714                 lyxerr[Debug::INFO] << "Spacing: SINGLE\n";
715                 params.spacing.set(Spacing::Single);
716                 break;
717         case 2:
718                 lyxerr[Debug::INFO] << "Spacing: ONEHALF\n";
719                 params.spacing.set(Spacing::Onehalf);
720                 break;
721         case 3:
722                 lyxerr[Debug::INFO] << "Spacing: DOUBLE\n";
723                 params.spacing.set(Spacing::Double);
724                 break;
725         case 4:
726                 lyxerr[Debug::INFO] << "Spacing: OTHER\n";
727                 params.spacing.set(Spacing::Other,
728                                    fl_get_input(class_->input_doc_spacing));
729                 break;
730         }
731         if (tmpSpacing != params.spacing)
732                 redo = true;
733
734         params.options = fl_get_input(class_->input_doc_extra);
735
736         return redo;
737 }
738
739
740 bool FormDocument::class_apply()
741 {
742         BufferParams &params = lv_.buffer()->params;
743
744         unsigned int const old_class = params.textclass;
745
746         bool redo = class_apply(params);
747
748         if (params.textclass != old_class) {
749                 // try to load new_class
750                 if (textclasslist[params.textclass].load()) {
751                         // successfully loaded
752                         redo = true;
753                         lv_.message(_("Converting document to new document class..."));
754                         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
755                                 old_class, params.textclass,
756                                 &*(lv_.buffer()->paragraphs.begin()),
757                                 lv_.buffer()->params);
758                         if (ret) {
759                                 string s;
760                                 if (ret == 1) {
761                                         s = _("One paragraph couldn't be converted");
762                                 } else {
763                                         s += tostr(ret);
764                                         s += _(" paragraphs couldn't be converted");
765                                 }
766                                 Alert::alert(_("Conversion Errors!"),s,
767                                              _("into chosen document class"));
768                         }
769
770                 } else {
771                         // problem changing class -- warn user and retain old style
772                         Alert::alert(_("Conversion Errors!"),
773                                      _("Errors loading new document class."),
774                                      _("Reverting to original document class."));
775                         combo_doc_class->select(int(old_class) + 1);
776                 }
777         }
778
779         return redo;
780 }
781
782
783 void FormDocument::paper_apply(BufferParams & params)
784 {
785         params.papersize2 = char(fl_get_choice(paper_->choice_papersize) - 1);
786
787         params.paperpackage =
788                 char(fl_get_choice(paper_->choice_paperpackage) - 1);
789
790         // set params.papersize from params.papersize2 and params.paperpackage
791         params.setPaperStuff();
792
793         params.use_geometry = fl_get_button(paper_->check_use_geometry);
794
795         if (fl_get_button(paper_->radio_landscape))
796                 params.orientation = BufferParams::ORIENTATION_LANDSCAPE;
797         else
798                 params.orientation = BufferParams::ORIENTATION_PORTRAIT;
799
800         params.paperwidth =
801                 getLengthFromWidgets(paper_->input_custom_width,
802                                      paper_->choice_custom_width_units);
803
804         params.paperheight =
805                 getLengthFromWidgets(paper_->input_custom_height,
806                                      paper_->choice_custom_height_units);
807
808         params.leftmargin =
809                 getLengthFromWidgets(paper_->input_inner_margin,
810                                      paper_->choice_inner_margin_units);
811
812         params.topmargin =
813                 getLengthFromWidgets(paper_->input_top_margin,
814                                      paper_->choice_top_margin_units);
815
816         params.rightmargin =
817                 getLengthFromWidgets(paper_->input_outer_margin,
818                                      paper_->choice_outer_margin_units);
819
820         params.bottommargin =
821                 getLengthFromWidgets(paper_->input_bottom_margin,
822                                      paper_->choice_bottom_margin_units);
823
824         params.headheight =
825                 getLengthFromWidgets(paper_->input_head_height,
826                                      paper_->choice_head_height_units);
827
828         params.headsep =
829                 getLengthFromWidgets(paper_->input_head_sep,
830                                      paper_->choice_head_sep_units);
831
832         params.footskip =
833                 getLengthFromWidgets(paper_->input_foot_skip,
834                                      paper_->choice_foot_skip_units);
835 }
836
837
838 void FormDocument::paper_apply()
839 {
840         paper_apply(lv_.buffer()->params);
841 }
842
843
844 bool FormDocument::language_apply(BufferParams & params)
845 {
846         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
847         bool redo = false;
848
849         switch (fl_get_choice(language_->choice_quotes_language) - 1) {
850         case 0:
851                 lga = InsetQuotes::EnglishQ;
852                 break;
853         case 1:
854                 lga = InsetQuotes::SwedishQ;
855                 break;
856         case 2:
857                 lga = InsetQuotes::GermanQ;
858                 break;
859         case 3:
860                 lga = InsetQuotes::PolishQ;
861                 break;
862         case 4:
863                 lga = InsetQuotes::FrenchQ;
864                 break;
865         case 5:
866                 lga = InsetQuotes::DanishQ;
867                 break;
868         }
869         params.quotes_language = lga;
870         if (fl_get_button(language_->radio_single))
871                 params.quotes_times = InsetQuotes::SingleQ;
872         else
873                 params.quotes_times = InsetQuotes::DoubleQ;
874
875         int const pos = combo_language->get();
876         Language const * old_language = params.language;
877         Language const * new_language = languages.getLanguage(lang_[pos-1]);
878         if (!new_language)
879                 new_language = default_language;
880
881         if (old_language != new_language
882             && old_language->RightToLeft() == new_language->RightToLeft()
883             && !lv_.buffer()->isMultiLingual())
884                 lv_.buffer()->changeLanguage(old_language, new_language);
885
886         if (old_language != new_language) {
887                 redo = true;
888         }
889
890         params.language = new_language;
891         params.inputenc = fl_get_choice_text(language_->choice_inputenc);
892
893         return redo;
894 }
895
896
897 bool FormDocument::language_apply()
898 {
899         return language_apply(lv_.buffer()->params);
900 }
901
902
903 bool FormDocument::options_apply(BufferParams & params)
904 {
905         bool redo = false;
906
907         params.graphicsDriver =
908                 fl_get_choice_text(options_->choice_postscript_driver);
909         params.use_amsmath = fl_get_button(options_->check_use_amsmath);
910         params.use_natbib  = fl_get_button(options_->check_use_natbib);
911         params.use_numerical_citations  =
912                 fl_get_choice(options_->choice_citation_format) - 1;
913
914         int tmpchar = int(fl_get_counter_value(options_->counter_secnumdepth));
915         if (params.secnumdepth != tmpchar)
916                 redo = true;
917         params.secnumdepth = tmpchar;
918
919         params.tocdepth = int(fl_get_counter_value(options_->counter_tocdepth));
920
921         params.float_placement =
922                 fl_get_input(options_->input_float_placement);
923
924         return redo;
925 }
926
927
928 bool FormDocument::options_apply()
929 {
930         return options_apply(lv_.buffer()->params);
931 }
932
933
934 void FormDocument::bullets_apply(BufferParams & params)
935 {
936         /* update the bullet settings */
937         BufferParams & buf_params = lv_.buffer()->params;
938
939         // a little bit of loop unrolling
940         params.user_defined_bullets[0] = buf_params.temp_bullets[0];
941         params.user_defined_bullets[1] = buf_params.temp_bullets[1];
942         params.user_defined_bullets[2] = buf_params.temp_bullets[2];
943         params.user_defined_bullets[3] = buf_params.temp_bullets[3];
944 }
945
946
947 void FormDocument::bullets_apply()
948 {
949         bullets_apply(lv_.buffer()->params);
950 }
951
952 void FormDocument::UpdateClassParams(BufferParams const & params)
953 {
954         // These are the params that have to be updated on any class change
955         // (even if the class defaults are not used) (JSpitzm 2002-04-08)
956
957         LyXTextClass const & tclass = textclasslist[params.textclass];
958
959         combo_doc_class->select(tclass.description());
960         fl_clear_choice(class_->choice_doc_fontsize);
961         fl_addto_choice(class_->choice_doc_fontsize, "default");
962         fl_addto_choice(class_->choice_doc_fontsize,
963                         tclass.opt_fontsize().c_str());
964         fl_set_choice_text(class_->choice_doc_fontsize,
965                         params.fontsize.c_str());
966         fl_clear_choice(class_->choice_doc_pagestyle);
967         fl_addto_choice(class_->choice_doc_pagestyle, "default");
968         fl_addto_choice(class_->choice_doc_pagestyle,
969                         tclass.opt_pagestyle().c_str());
970         fl_set_choice_text(class_->choice_doc_pagestyle,
971                         params.pagestyle.c_str());
972
973 }
974
975 void FormDocument::class_update(BufferParams const & params)
976 {
977         if (!class_.get())
978                 return;
979
980         UpdateClassParams(params);
981
982         fl_set_choice_text(class_->choice_doc_fonts,
983                 params.fonts.c_str());
984         fl_set_button(class_->radio_doc_indent, 0);
985         fl_set_button(class_->radio_doc_skip, 0);
986         if (params.paragraph_separation == BufferParams::PARSEP_INDENT)
987                 fl_set_button(class_->radio_doc_indent, 1);
988         else
989                 fl_set_button(class_->radio_doc_skip, 1);
990
991         bool const input_length = fl_get_choice(class_->choice_doc_skip) == 4;
992         setEnabled(class_->choice_default_skip_units, input_length);
993         setEnabled(class_->input_doc_skip, input_length);
994
995         switch (params.getDefSkip().kind()) {
996         case VSpace::SMALLSKIP:
997                 fl_set_choice (class_->choice_doc_skip, 1);
998                 break;
999         case VSpace::MEDSKIP:
1000                 fl_set_choice (class_->choice_doc_skip, 2);
1001                 break;
1002         case VSpace::BIGSKIP:
1003                 fl_set_choice (class_->choice_doc_skip, 3);
1004                 break;
1005         case VSpace::LENGTH:
1006         {
1007                 int const paperchoice = fl_get_choice(paper_->choice_papersize);
1008                 bool const metric = (paperchoice == 1 && lyxrc.default_papersize > 3)
1009                         || paperchoice == 2 || paperchoice > 5;
1010                 string const default_unit = metric ? "cm" : "in";
1011                 string const length = params.getDefSkip().asLyXCommand();
1012                 updateWidgetsFromLengthString(class_->input_doc_skip,
1013                                               class_->choice_default_skip_units,
1014                                               length, default_unit);
1015                 break;
1016         }
1017         default:
1018                 fl_set_choice (class_->choice_doc_skip, 2);
1019                 break;
1020         }
1021         fl_set_button(class_->radio_doc_sides_one, 0);
1022         fl_set_button(class_->radio_doc_sides_two, 0);
1023         setEnabled(class_->choice_doc_skip,
1024                    fl_get_button(class_->radio_doc_skip));
1025
1026         if (params.sides == LyXTextClass::TwoSides)
1027                 fl_set_button(class_->radio_doc_sides_two, 1);
1028         else
1029                 fl_set_button(class_->radio_doc_sides_one, 1);
1030         fl_set_button(class_->radio_doc_columns_one, 0);
1031         fl_set_button(class_->radio_doc_columns_two, 0);
1032         if (params.columns == 2)
1033                 fl_set_button(class_->radio_doc_columns_two, 1);
1034         else
1035                 fl_set_button(class_->radio_doc_columns_one, 1);
1036
1037         fl_set_input(class_->input_doc_spacing, "");
1038         setEnabled(class_->input_doc_spacing, false);
1039
1040         switch (params.spacing.getSpace()) {
1041         case Spacing::Default: // nothing bad should happen with this
1042         case Spacing::Single:
1043                 // \singlespacing
1044                 fl_set_choice(class_->choice_doc_spacing, 1);
1045                 break;
1046         case Spacing::Onehalf:
1047                 // \onehalfspacing
1048                 fl_set_choice(class_->choice_doc_spacing, 2);
1049                 break;
1050         case Spacing::Double:
1051                 // \doublespacing
1052                 fl_set_choice(class_->choice_doc_spacing, 3);
1053                 break;
1054         case Spacing::Other:
1055         {
1056                 fl_set_choice(class_->choice_doc_spacing, 4);
1057                 char sval[20];
1058                 sprintf(sval,"%g",params.spacing.getValue());
1059                 fl_set_input(class_->input_doc_spacing, sval);
1060                 setEnabled(class_->input_doc_spacing, true);
1061                 break;
1062         }
1063         }
1064         if (!params.options.empty())
1065                 fl_set_input(class_->input_doc_extra, params.options.c_str());
1066         else
1067                 fl_set_input(class_->input_doc_extra, "");
1068 }
1069
1070
1071 void FormDocument::language_update(BufferParams const & params)
1072 {
1073         if (!language_.get())
1074                 return;
1075
1076         int const pos = int(findPos(lang_, params.language->lang()));
1077         combo_language->select(pos+1);
1078
1079         fl_set_choice_text(language_->choice_inputenc, params.inputenc.c_str());
1080         fl_set_choice(language_->choice_quotes_language, params.quotes_language + 1);
1081         fl_set_button(language_->radio_single, 0);
1082         fl_set_button(language_->radio_double, 0);
1083         if (params.quotes_times == InsetQuotes::SingleQ)
1084                 fl_set_button(language_->radio_single, 1);
1085         else
1086                 fl_set_button(language_->radio_double, 1);
1087 }
1088
1089
1090 void FormDocument::options_update(BufferParams const & params)
1091 {
1092         if (!options_.get())
1093                 return;
1094
1095         fl_set_choice_text(options_->choice_postscript_driver,
1096                            params.graphicsDriver.c_str());
1097         fl_set_button(options_->check_use_amsmath, params.use_amsmath);
1098         fl_set_button(options_->check_use_natbib,  params.use_natbib);
1099         fl_set_choice(options_->choice_citation_format,
1100                       int(params.use_numerical_citations)+1);
1101         setEnabled(options_->choice_citation_format, params.use_natbib);
1102         fl_set_counter_value(options_->counter_secnumdepth, params.secnumdepth);
1103         fl_set_counter_value(options_->counter_tocdepth, params.tocdepth);
1104         if (!params.float_placement.empty())
1105                 fl_set_input(options_->input_float_placement,
1106                              params.float_placement.c_str());
1107         else
1108                 fl_set_input(options_->input_float_placement, "");
1109 }
1110
1111
1112 void FormDocument::paper_update(BufferParams const & params)
1113 {
1114         if (!paper_.get())
1115                 return;
1116
1117         fl_set_choice(paper_->choice_papersize, params.papersize2 + 1);
1118         fl_set_choice(paper_->choice_paperpackage, params.paperpackage + 1);
1119         fl_set_button(paper_->check_use_geometry, params.use_geometry);
1120
1121         int const paperchoice = fl_get_choice(paper_->choice_papersize);
1122         bool const useCustom = paperchoice == 2;
1123         bool const useGeom = fl_get_button(paper_->check_use_geometry);
1124
1125         fl_set_button(paper_->radio_portrait, 0);
1126         setEnabled(paper_->radio_portrait, !useCustom);
1127         fl_set_button(paper_->radio_landscape, 0);
1128         setEnabled(paper_->radio_landscape, !useCustom);
1129
1130         if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
1131                 fl_set_button(paper_->radio_landscape, 1);
1132         else
1133                 fl_set_button(paper_->radio_portrait, 1);
1134         setEnabled(paper_->choice_paperpackage,
1135                    //either default papersize (preferences)
1136                    //or document papersize has to be A4
1137                    (paperchoice == 7
1138                     || paperchoice == 1 && lyxrc.default_papersize == 5)
1139                    && fl_get_button(paper_->radio_portrait));
1140
1141         // Default unit choice is cm if metric, inches if US paper.
1142         bool const metric = (paperchoice == 1 && lyxrc.default_papersize > 3)
1143                 || paperchoice == 2 || paperchoice > 5;
1144         string const default_unit = metric ? "cm" : "in";
1145         updateWidgetsFromLengthString(paper_->input_custom_width,
1146                                       paper_->choice_custom_width_units,
1147                                       params.paperwidth, default_unit);
1148         setEnabled(paper_->input_custom_width, useCustom);
1149         setEnabled(paper_->choice_custom_width_units, useCustom);
1150
1151         updateWidgetsFromLengthString(paper_->input_custom_height,
1152                                       paper_->choice_custom_height_units,
1153                                       params.paperheight, default_unit);
1154         setEnabled(paper_->input_custom_height, useCustom);
1155         setEnabled(paper_->choice_custom_height_units, useCustom);
1156
1157         updateWidgetsFromLengthString(paper_->input_inner_margin,
1158                                       paper_->choice_inner_margin_units,
1159                                       params.leftmargin, default_unit);
1160         setEnabled(paper_->input_inner_margin, useGeom);
1161         setEnabled(paper_->choice_inner_margin_units, useGeom);
1162
1163         updateWidgetsFromLengthString(paper_->input_top_margin,
1164                                       paper_->choice_top_margin_units,
1165                                       params.topmargin, default_unit);
1166         setEnabled(paper_->input_top_margin, useGeom);
1167         setEnabled(paper_->choice_top_margin_units, useGeom);
1168
1169         updateWidgetsFromLengthString(paper_->input_outer_margin,
1170                                       paper_->choice_outer_margin_units,
1171                                       params.rightmargin, default_unit);
1172         setEnabled(paper_->input_outer_margin, useGeom);
1173         setEnabled(paper_->choice_outer_margin_units, useGeom);
1174
1175         updateWidgetsFromLengthString(paper_->input_bottom_margin,
1176                                       paper_->choice_bottom_margin_units,
1177                                       params.bottommargin, default_unit);
1178         setEnabled(paper_->input_bottom_margin, useGeom);
1179         setEnabled(paper_->choice_bottom_margin_units, useGeom);
1180
1181         updateWidgetsFromLengthString(paper_->input_head_height,
1182                                       paper_->choice_head_height_units,
1183                                       params.headheight, default_unit);
1184         setEnabled(paper_->input_head_height, useGeom);
1185         setEnabled(paper_->choice_head_height_units, useGeom);
1186
1187         updateWidgetsFromLengthString(paper_->input_head_sep,
1188                                       paper_->choice_head_sep_units,
1189                                       params.headsep, default_unit);
1190         setEnabled(paper_->input_head_sep, useGeom);
1191         setEnabled(paper_->choice_head_sep_units, useGeom);
1192
1193         updateWidgetsFromLengthString(paper_->input_foot_skip,
1194                                       paper_->choice_foot_skip_units,
1195                                       params.footskip, default_unit);
1196         setEnabled(paper_->input_foot_skip, useGeom);
1197         setEnabled(paper_->choice_foot_skip_units, useGeom);
1198
1199         fl_set_focus_object(paper_->form, paper_->choice_papersize);
1200 }
1201
1202
1203 void FormDocument::bullets_update(BufferParams const & params)
1204 {
1205         if (!bullets_.get() ||
1206             ((XpmVersion<4) ||
1207              (XpmVersion==4 && XpmRevision<7)))
1208                 return;
1209
1210         bool const isLinuxDoc = lv_.buffer()->isLinuxDoc();
1211         setEnabled(fbullet, !isLinuxDoc);
1212
1213         if (isLinuxDoc) return;
1214
1215         fl_set_button(bullets_->radio_bullet_depth_1, 1);
1216         fl_set_input(bullets_->input_bullet_latex,
1217                      params.user_defined_bullets[0].getText().c_str());
1218         fl_set_choice(bullets_->choice_bullet_size,
1219                       params.user_defined_bullets[0].getSize() + 2);
1220 }
1221
1222
1223 void FormDocument::checkReadOnly()
1224 {
1225         if (bc().readOnly(lv_.buffer()->isReadonly())) {
1226                 combo_doc_class->deactivate();
1227                 combo_language->deactivate();
1228                 postWarning(_("Document is read-only."
1229                               " No changes to layout permitted."));
1230         } else {
1231                 combo_doc_class->activate();
1232                 combo_language->activate();
1233                 clearMessage();
1234         }
1235 }
1236
1237
1238 bool FormDocument::CheckDocumentInput(FL_OBJECT * ob, long)
1239 {
1240         string str;
1241         bool ok = true;
1242         char const * input;
1243
1244         // this has to be all out of if/elseif because it has to deactivate
1245         // the document buttons and so the whole stuff has to be tested again.
1246         // disable OK/Apply if input is not valid
1247         str = fl_get_input(class_->input_doc_skip);
1248         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1249         str = fl_get_input(paper_->input_custom_width);
1250         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1251         str = fl_get_input(paper_->input_custom_height);
1252         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1253         str = fl_get_input(paper_->input_outer_margin);
1254         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1255         str = fl_get_input(paper_->input_inner_margin);
1256         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1257         str = fl_get_input(paper_->input_top_margin);
1258         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1259         str = fl_get_input(paper_->input_bottom_margin);
1260         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1261         str = fl_get_input(paper_->input_head_height);
1262         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1263         str = fl_get_input(paper_->input_head_sep);
1264         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1265         str = fl_get_input(paper_->input_foot_skip);
1266         ok = ok && (str.empty() || isValidLength(str) || isStrDbl(str));
1267
1268         //display warning if input is not valid
1269         if (ob == class_->input_doc_skip
1270             || ob == paper_->input_custom_width
1271             || ob == paper_->input_custom_height
1272             || ob == paper_->input_outer_margin
1273             || ob == paper_->input_inner_margin
1274             || ob == paper_->input_top_margin
1275             || ob == paper_->input_bottom_margin
1276             || ob == paper_->input_head_height
1277             || ob == paper_->input_head_sep
1278             || ob == paper_->input_foot_skip) {
1279                 if (!ok) {
1280                         postWarning(_("Invalid Length (valid example: 10mm)"));
1281                         return false;
1282                 } else {
1283                         clearMessage();
1284                         return true;
1285                 }
1286         }
1287
1288         // "Synchronize" the choice and the input field, so that it
1289         // is impossible to commit senseless data.
1290         input = fl_get_input (class_->input_doc_skip);
1291         if ((fl_get_choice(class_->choice_doc_skip) == 4) && !*input)
1292                 ok = false;
1293         else if (fl_get_choice(class_->choice_doc_skip) != 4)
1294                 fl_set_input (class_->input_doc_skip, "");
1295
1296         input = fl_get_input(class_->input_doc_spacing);
1297         if ((fl_get_choice(class_->choice_doc_spacing) == 4) && !*input)
1298                 ok = false;
1299         else if (fl_get_choice(class_->choice_doc_spacing) != 4)
1300                 fl_set_input (class_->input_doc_spacing, "");
1301         return ok;
1302 }
1303
1304
1305 void FormDocument::ChoiceBulletSize(FL_OBJECT * ob, long /*data*/)
1306 {
1307         BufferParams & param = lv_.buffer()->params;
1308
1309         // convert from 1-6 range to -1-4
1310         param.temp_bullets[current_bullet_depth].setSize(fl_get_choice(ob) - 2);
1311         fl_set_input(bullets_->input_bullet_latex,
1312                      param.temp_bullets[current_bullet_depth].getText().c_str());
1313 }
1314
1315
1316 void FormDocument::InputBulletLaTeX(FL_OBJECT *, long)
1317 {
1318         BufferParams & param = lv_.buffer()->params;
1319
1320         param.temp_bullets[current_bullet_depth].
1321                 setText(fl_get_input(bullets_->input_bullet_latex));
1322 }
1323
1324
1325 void FormDocument::BulletDepth(FL_OBJECT * ob)
1326 {
1327         /* Should I do the following:                                 */
1328         /*  1. change to the panel that the current bullet belongs in */
1329         /*  2. show that bullet as selected                           */
1330         /*  3. change the size setting to the size of the bullet in Q.*/
1331         /*  4. display the latex equivalent in the latex box          */
1332         /*                                                            */
1333         /* I'm inclined to just go with 3 and 4 at the moment and     */
1334         /* maybe try to support the others later                      */
1335         BufferParams & param = lv_.buffer()->params;
1336
1337         int data = 0;
1338         if (ob == bullets_->radio_bullet_depth_1)
1339                 data = 0;
1340         else if (ob == bullets_->radio_bullet_depth_2)
1341                 data = 1;
1342         else if (ob == bullets_->radio_bullet_depth_3)
1343                 data = 2;
1344         else if (ob == bullets_->radio_bullet_depth_4)
1345                 data = 3;
1346
1347         switch (fl_get_button_numb(ob)) {
1348         case 3:
1349                 // right mouse button resets to default
1350                 param.temp_bullets[data] = ITEMIZE_DEFAULTS[data];
1351         default:
1352                 current_bullet_depth = data;
1353                 fl_set_input(bullets_->input_bullet_latex,
1354                              param.temp_bullets[data].getText().c_str());
1355                 fl_set_choice(bullets_->choice_bullet_size,
1356                               param.temp_bullets[data].getSize() + 2);
1357         }
1358 }
1359
1360
1361 void FormDocument::BulletPanel(FL_OBJECT * ob)
1362 {
1363         /* Here we have to change the background pixmap to that selected */
1364         /* by the user. (eg. standard.xpm, psnfss1.xpm etc...)           */
1365
1366         int data = 0;
1367         if (ob == bullets_->radio_bullet_panel_standard)
1368                 data = 0;
1369         else if (ob == bullets_->radio_bullet_panel_maths)
1370                 data = 1;
1371         else if (ob == bullets_->radio_bullet_panel_ding2)
1372                 data = 2;
1373         else if (ob == bullets_->radio_bullet_panel_ding3)
1374                 data = 3;
1375         else if (ob == bullets_->radio_bullet_panel_ding4)
1376                 data = 4;
1377         else if (ob == bullets_->radio_bullet_panel_ding1)
1378                 data = 5;
1379
1380         if (data != current_bullet_panel) {
1381                 fl_freeze_form(bullets_->form);
1382                 current_bullet_panel = data;
1383
1384                 /* free the current pixmap */
1385                 fl_free_bmtable_pixmap(bullets_->bmtable_bullet_panel);
1386                 string new_panel;
1387                 if (ob == bullets_->radio_bullet_panel_standard) {
1388                         new_panel = "standard";
1389                 } else if (ob == bullets_->radio_bullet_panel_maths ) {
1390                         new_panel = "amssymb";
1391                 } else if (ob == bullets_->radio_bullet_panel_ding2) {
1392                         new_panel = "psnfss1";
1393                 } else if (ob == bullets_->radio_bullet_panel_ding3) {
1394                         new_panel = "psnfss2";
1395                 } else if (ob == bullets_->radio_bullet_panel_ding4) {
1396                         new_panel = "psnfss3";
1397                 } else if (ob == bullets_->radio_bullet_panel_ding1) {
1398                         new_panel = "psnfss4";
1399                 } else {
1400                         /* something very wrong happened */
1401                         // play it safe for now but should be an exception
1402                         current_bullet_panel = 0;  // standard panel
1403                         new_panel = "standard";
1404                 }
1405                 new_panel += ".xpm";
1406                 fl_set_bmtable_pixmap_file(bullets_->bmtable_bullet_panel, 6, 6,
1407                                            LibFileSearch("images", new_panel).c_str());
1408                 fl_redraw_object(bullets_->bmtable_bullet_panel);
1409                 fl_unfreeze_form(bullets_->form);
1410         }
1411 }
1412
1413
1414 void FormDocument::BulletBMTable(FL_OBJECT * ob, long /*data*/)
1415 {
1416         /* handle the user input by setting the current bullet depth's pixmap */
1417         /* to that extracted from the current chosen position of the BMTable  */
1418         /* Don't forget to free the button's old pixmap first.                */
1419
1420         BufferParams & param = lv_.buffer()->params;
1421         int bmtable_button = fl_get_bmtable(ob);
1422
1423         /* try to keep the button held down till another is pushed */
1424         /*  fl_set_bmtable(ob, 1, bmtable_button); */
1425         param.temp_bullets[current_bullet_depth].setFont(current_bullet_panel);
1426         param.temp_bullets[current_bullet_depth].setCharacter(bmtable_button);
1427         fl_set_input(bullets_->input_bullet_latex,
1428                      param.temp_bullets[current_bullet_depth].getText().c_str());
1429 }
1430
1431
1432 void FormDocument::CheckChoiceClass(FL_OBJECT * ob, long)
1433 {
1434         if (!ob)
1435                 ob = class_->choice_doc_class;
1436
1437         lv_.prohibitInput();
1438
1439         unsigned int tc = combo_doc_class->get() - 1;
1440         if (textclasslist[tc].load()) {
1441                 // we use a copy of the bufferparams because we do not
1442                 // want to modify them yet.
1443                 BufferParams params = lv_.buffer()->params;
1444
1445                 if (lyxrc.auto_reset_options) {
1446                         params.textclass = tc;
1447                         params.useClassDefaults();
1448                         UpdateLayoutDocument(params);
1449                 } else {
1450                         // update the params which are needed in any case
1451                         // (fontsizes, pagestyle)
1452                         params.textclass = tc;
1453                         UpdateClassParams(params);
1454                 }
1455
1456         } else {
1457                 // unable to load new style
1458                 Alert::alert(_("Conversion Errors!"),
1459                              _("Unable to switch to new document class."),
1460                              _("Reverting to original document class."));
1461                 combo_doc_class->select(int(lv_.buffer()->params.textclass) + 1);
1462         }
1463         lv_.allowInput();
1464 }
1465
1466
1467 void FormDocument::UpdateLayoutDocument(BufferParams const & params)
1468 {
1469         if (!dialog_.get())
1470                 return;
1471
1472         checkReadOnly();
1473         class_update(params);
1474         paper_update(params);
1475         language_update(params);
1476         options_update(params);
1477         bullets_update(params);
1478 }