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