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