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