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