]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormDocument.C
7d89cf98ecd68aa530a9e54cc4bc4e3e56e506c4
[lyx.git] / src / frontends / xforms / FormDocument.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000 The LyX Team.
7  *
8  *           @author Jürgen Vigna
9  *
10  *======================================================*/
11
12 #include <config.h>
13
14 #ifdef __GNUG_
15 #pragma implementation
16 #endif
17
18 #include "lyx_gui_misc.h"
19 #include FORMS_H_LOCATION
20 #include XPM_H_LOCATION
21
22 #include "FormDocument.h"
23 #include "form_document.h"
24 #include "Dialogs.h"
25 #include "layout.h"
26 #include "combox.h"
27 #include "tex-strings.h"
28 #include "bufferparams.h"
29 #include "insets/insetquotes.h"
30 #include "vspace.h"
31 #include "bmtable.h"
32 #include "support/filetools.h"
33 #include "language.h"
34 #include "LyXView.h"
35 #include "BufferView.h"
36 #include "buffer.h"
37 #include "Liason.h"
38 #include "CutAndPaste.h"
39 #include "bufferview_funcs.h"
40
41 #ifdef CXX_WORKING_NAMESPACES
42 using Liason::setMinibuffer;
43 #endif
44
45 #define USE_CLASS_COMBO 1
46
47 FormDocument::FormDocument(LyXView * lv, Dialogs * d)
48         : FormBase(lv, d, BUFFER_DEPENDENT, _("Document Layout"),
49                    new NoRepeatedApplyReadOnlyPolicy),
50           dialog_(0), paper_(0), class_(0), language_(0), options_(0),
51           bullets_(0), current_bullet_panel(0), current_bullet_depth(0),
52           fbullet(0), combo_language(0), combo_doc_class(0)
53 {
54     // let the popup be shown
55     // This is a permanent connection so we won't bother
56     // storing a copy because we won't be disconnecting.
57     d->showLayoutDocument.connect(slot(this, &FormDocument::show));
58 }
59
60
61 FormDocument::~FormDocument()
62 {
63 #ifdef USE_CLASS_COMBO
64     delete combo_doc_class;
65 #endif
66     delete class_;
67     delete paper_;
68     delete combo_language;
69     delete language_;
70     delete options_;
71     delete bullets_;
72     delete dialog_;
73 }
74
75
76 FL_FORM * FormDocument::form() const
77 {
78     if (dialog_) return dialog_->form;
79     return 0;
80 }
81
82
83 void FormDocument::build()
84 {
85     int n;
86
87     // the tabbed folder
88     dialog_ = build_tabbed_document();
89
90     // manage the restore, ok, apply and cancel/close buttons
91     bc_.setOK(dialog_->button_ok);
92     bc_.setApply(dialog_->button_apply);
93     bc_.setCancel(dialog_->button_cancel);
94     bc_.setUndoAll(dialog_->button_restore);
95     bc_.refresh();
96
97     // the document paper form
98     paper_ = build_doc_paper();
99     fl_addto_choice(paper_->choice_papersize2,
100                     _(" Default | Custom | USletter | USlegal "
101                       "| USexecutive | A3 | A4 | A5 | B3 | B4 | B5 "));
102     fl_addto_choice(paper_->choice_paperpackage,
103                     _(" None "
104                       "| A4 small Margins (only portrait) "
105                       "| A4 very small Margins (only portrait) "
106                       "| A4 very wide margins (only portrait) "));
107     fl_set_input_return(paper_->input_custom_width, FL_RETURN_ALWAYS);
108     fl_set_input_return(paper_->input_custom_height, FL_RETURN_ALWAYS);
109     fl_set_input_return(paper_->input_top_margin, FL_RETURN_ALWAYS);
110     fl_set_input_return(paper_->input_bottom_margin, FL_RETURN_ALWAYS);
111     fl_set_input_return(paper_->input_left_margin, FL_RETURN_ALWAYS);
112     fl_set_input_return(paper_->input_right_margin, FL_RETURN_ALWAYS);
113     fl_set_input_return(paper_->input_head_height, FL_RETURN_ALWAYS);
114     fl_set_input_return(paper_->input_head_sep, FL_RETURN_ALWAYS);
115     fl_set_input_return(paper_->input_foot_skip, FL_RETURN_ALWAYS);
116
117     bc_.addReadOnly (paper_->choice_paperpackage);
118     bc_.addReadOnly (paper_->greoup_radio_orientation);
119     bc_.addReadOnly (paper_->radio_portrait);
120     bc_.addReadOnly (paper_->radio_landscape);
121     bc_.addReadOnly (paper_->choice_papersize2);
122     bc_.addReadOnly (paper_->push_use_geometry);
123     bc_.addReadOnly (paper_->input_custom_width);
124     bc_.addReadOnly (paper_->input_custom_height);
125     bc_.addReadOnly (paper_->input_top_margin);
126     bc_.addReadOnly (paper_->input_bottom_margin);
127     bc_.addReadOnly (paper_->input_left_margin);
128     bc_.addReadOnly (paper_->input_right_margin);
129     bc_.addReadOnly (paper_->input_head_height);
130     bc_.addReadOnly (paper_->input_head_sep);
131     bc_.addReadOnly (paper_->input_foot_skip);
132
133     // the document class form
134     class_ = build_doc_class();
135
136     FL_OBJECT * obj;
137 #ifdef USE_CLASS_COMBO
138     // The language is a combo-box and has to be inserted manually
139     obj = class_->choice_doc_class;
140     fl_addto_form(class_->form);
141     combo_doc_class = new Combox(FL_COMBOX_DROPLIST);
142     combo_doc_class->add(obj->x, obj->y, obj->w, obj->h, 200);
143     combo_doc_class->shortcut("#C",1);
144     combo_doc_class->setcallback(ComboInputCB, this);
145     fl_end_form();
146     for (LyXTextClassList::const_iterator cit = textclasslist.begin();
147          cit != textclasslist.end(); ++cit)
148     {
149         combo_doc_class->addto((*cit).description().c_str());
150     }
151 #else
152     fl_clear_choice(class_->choice_doc_class);
153     for (LyXTextClassList::const_iterator cit = textclasslist.begin();
154          cit != textclasslist.end(); ++cit)
155     {
156         fl_addto_choice(class_->choice_doc_class,(*cit).description().c_str());
157     }
158 #endif
159     fl_addto_choice(class_->choice_doc_spacing,
160                     _(" Single | OneHalf | Double | Other "));
161     fl_addto_choice(class_->choice_doc_fontsize, "default|10|11|12");
162    for (n=0; tex_fonts[n][0]; ++n) {
163         fl_addto_choice(class_->choice_doc_fonts,tex_fonts[n]);
164     }
165     fl_addto_choice(class_->choice_doc_pagestyle,
166                     "default|empty|plain|headings|fancy");
167     fl_addto_choice(class_->choice_doc_skip,
168                     _(" Smallskip | Medskip | Bigskip | Length "));
169     fl_set_input_return(class_->input_doc_skip, FL_RETURN_ALWAYS);
170     fl_set_input_return(class_->input_doc_spacing, FL_RETURN_ALWAYS);
171
172     bc_.addReadOnly (class_->radio_doc_indent);
173     bc_.addReadOnly (class_->radio_doc_skip);
174 #ifndef USE_CLASS_COMBO
175     bc_.addReadOnly (class_->choice_doc_class);
176 #endif
177     bc_.addReadOnly (class_->choice_doc_pagestyle);
178     bc_.addReadOnly (class_->choice_doc_fonts);
179     bc_.addReadOnly (class_->choice_doc_fontsize);
180     bc_.addReadOnly (class_->radio_doc_sides_one);
181     bc_.addReadOnly (class_->radio_doc_sides_two);
182     bc_.addReadOnly (class_->radio_doc_columns_one);
183     bc_.addReadOnly (class_->radio_doc_columns_two);
184     bc_.addReadOnly (class_->input_doc_extra);
185     bc_.addReadOnly (class_->input_doc_skip);
186     bc_.addReadOnly (class_->choice_doc_skip);
187     bc_.addReadOnly (class_->choice_doc_spacing);
188     bc_.addReadOnly (class_->input_doc_spacing);
189
190     // the document language form
191     language_ = build_doc_language();
192     fl_addto_choice(language_->choice_inputenc,
193                     "default|auto|latin1|latin2|latin5"
194                     "|koi8-r|koi8-u|cp866|cp1251|iso88595");
195
196     // The language is a combo-box and has to be inserted manually
197     obj = language_->choice_language;
198     fl_addto_form(language_->form);
199     combo_language = new Combox(FL_COMBOX_DROPLIST);
200     combo_language->add(obj->x, obj->y, obj->w, obj->h, 200);
201     combo_language->shortcut("#L",1);
202     combo_language->setcallback(ComboInputCB, this);
203     fl_end_form();
204     // "default" is not part of the languages array any more.
205 #ifdef DO_USE_DEFAULT_LANGUAGE
206     combo_language->addto("default");
207 #endif
208     for(Languages::iterator cit = languages.begin();
209         cit != languages.end(); ++cit) {
210         combo_language->addto((*cit).second.lang().c_str());
211     }
212
213     fl_addto_choice(language_->choice_quotes_language,
214                     _(" ``text'' | ''text'' | ,,text`` | ,,text'' |"
215                       " «text» | »text« "));
216
217     bc_.addReadOnly (language_->choice_language);
218     bc_.addReadOnly (language_->choice_inputenc);
219
220     // the document options form
221     options_ = build_doc_options();
222     fl_set_counter_bounds(options_->slider_secnumdepth,-2,5);
223     fl_set_counter_bounds(options_->slider_tocdepth,-1,5);
224     fl_set_counter_step(options_->slider_secnumdepth,1,1);
225     fl_set_counter_step(options_->slider_tocdepth,1,1);
226     fl_set_counter_precision(options_->slider_secnumdepth, 0);
227     fl_set_counter_precision(options_->slider_tocdepth, 0);
228     for (n=0; tex_graphics[n][0]; ++n) {
229         fl_addto_choice(options_->choice_postscript_driver, tex_graphics[n]);
230     }
231
232     bc_.addReadOnly (options_->slider_secnumdepth);
233     bc_.addReadOnly (options_->slider_tocdepth);
234     bc_.addReadOnly (options_->check_use_amsmath);
235     bc_.addReadOnly (options_->input_float_placement);
236     bc_.addReadOnly (options_->choice_postscript_driver);
237
238     // the document bullets form
239     bullets_ = build_doc_bullet();
240     fl_addto_choice(bullets_->choice_bullet_size,
241                     _(" default | tiny | script | footnote | small |"
242                       " normal | large | Large | LARGE | huge | Huge"));
243     fl_set_choice(bullets_->choice_bullet_size, 1);
244     fl_set_input_return(bullets_->input_bullet_latex, FL_RETURN_CHANGED);
245     fl_set_input_maxchars(bullets_->input_bullet_latex, 80);
246
247     bc_.addReadOnly (bullets_->bmtable_bullet_panel);
248     bc_.addReadOnly (bullets_->choice_bullet_size);
249     bc_.addReadOnly (bullets_->input_bullet_latex);
250
251     fl_addto_tabfolder(dialog_->tabbed_folder,_("Document"),
252                        class_->form);
253     fl_addto_tabfolder(dialog_->tabbed_folder,_("Paper"),
254                        paper_->form);
255     fl_addto_tabfolder(dialog_->tabbed_folder,_("Language"),
256                        language_->form);
257     fl_addto_tabfolder(dialog_->tabbed_folder,_("Extra"),
258                        options_->form);
259     fbullet = fl_addto_tabfolder(dialog_->tabbed_folder,_("Bullets"),
260                                  bullets_->form);
261     if ((XpmVersion < 4) || (XpmVersion == 4 && XpmRevision < 7)) {
262             lyxerr << _("Your version of libXpm is older than 4.7.\n"
263                         "The `bullet' tab of the document popup "
264                         "has been disabled") << '\n';
265         fl_deactivate_object(fbullet);
266         fl_set_object_lcol(fbullet, FL_INACTIVE);
267     }
268 }
269
270
271 void FormDocument::apply()
272 {
273     if (!lv_->view()->available() || !dialog_)
274         return;
275
276     bool redo = class_apply();
277     paper_apply();
278     redo = language_apply() || redo;
279     redo = options_apply() || redo;
280     bullets_apply();
281
282     if (redo) {
283             lv_->view()->redoCurrentBuffer();
284     }
285     lv_->buffer()->markDirty();
286     setMinibuffer(lv_, _("Document layout set"));
287 }
288
289
290 void FormDocument::cancel()
291 {
292     // this avoids confusion when reopening
293     BufferParams & param = lv_->buffer()->params;
294     param.temp_bullets[0] = param.user_defined_bullets[0];
295     param.temp_bullets[1] = param.user_defined_bullets[1];
296     param.temp_bullets[2] = param.user_defined_bullets[2];
297     param.temp_bullets[3] = param.user_defined_bullets[3];
298     hide();
299 }
300
301
302 void FormDocument::update()
303 {
304     if (!dialog_)
305         return;
306
307     checkReadOnly();
308
309     BufferParams const & params = lv_->buffer()->params;
310
311     class_update(params);
312     paper_update(params);
313     language_update(params);
314     options_update(params);
315     bullets_update(params);
316 }
317
318
319 bool FormDocument::input( FL_OBJECT * ob, long data )
320 {
321         State cb = static_cast<State>( data );
322
323         switch( cb ) {
324         case CHECKCHOICECLASS:
325                 CheckChoiceClass(ob, 0);
326                 break;
327         case CHOICEBULLETSIZE:
328                 ChoiceBulletSize(ob, 0);
329                 break;
330         case INPUTBULLETLATEX:
331                 InputBulletLaTeX(ob, 0);
332                 break;
333         case BULLETDEPTH1:
334         case BULLETDEPTH2:
335         case BULLETDEPTH3:
336         case BULLETDEPTH4:
337                 BulletDepth(ob, cb);
338                 break;
339         case BULLETPANEL1:
340         case BULLETPANEL2:
341         case BULLETPANEL3:
342         case BULLETPANEL4:
343         case BULLETPANEL5:
344         case BULLETPANEL6:
345                 BulletPanel(ob, cb);
346                 break;
347         case BULLETBMTABLE:
348                 BulletBMTable(ob, 0);
349                 break;
350         default:
351                 break;
352         }
353         
354         switch( data ) {
355         case INPUT:
356         case CHECKCHOICECLASS:
357         case CHOICEBULLETSIZE:
358         case INPUTBULLETLATEX:
359         case BULLETBMTABLE:
360                 return CheckDocumentInput(ob, 0);
361         default:
362                 break;
363         }
364
365         return true;
366 }
367
368
369 void FormDocument::ComboInputCB(int, void * v, Combox * combox)
370 {
371     FormDocument * pre = static_cast<FormDocument*>(v);
372     if (combox == pre->combo_doc_class)
373         pre->CheckChoiceClass(0, 0);
374     pre->bc_.valid(pre->CheckDocumentInput(0,0));
375 }
376
377
378 bool FormDocument::class_apply()
379 {
380         bool redo = false;
381         BufferParams &params = lv_->buffer()->params;
382
383         // If default skip is a "Length" but there's no text in the
384         // input field, reset the kind to "Medskip", which is the default.
385         if ((fl_get_choice (class_->choice_doc_skip) == 4) &&
386             !*(fl_get_input (class_->input_doc_skip))) {
387                 fl_set_choice (class_->choice_doc_skip, 2);
388         }
389         params.fonts = fl_get_choice_text(class_->choice_doc_fonts);
390         params.fontsize = fl_get_choice_text(class_->choice_doc_fontsize);
391         params.pagestyle = fl_get_choice_text(class_->choice_doc_pagestyle);
392
393 #ifdef USE_CLASS_COMBO   
394         unsigned int new_class = combo_doc_class->get() - 1;
395 #else
396         unsigned int new_class = fl_get_choice(class_->choice_doc_class) - 1;
397 #endif
398         if (params.textclass != new_class) {
399                 // try to load new_class
400                 if (textclasslist.Load(new_class)) {
401                         // successfully loaded
402                         redo = true;
403                         setMinibuffer(lv_, _("Converting document to new document class..."));
404                         CutAndPaste cap;
405                         int ret = cap.SwitchLayoutsBetweenClasses(
406                             params.textclass, new_class,
407                             lv_->buffer()->paragraph);
408                         if (ret) {
409                                 string s;
410                                 if (ret==1) {
411                                         s = _("One paragraph couldn't be converted");
412                                 } else {
413                                         s += tostr(ret);
414                                         s += _(" paragraphs couldn't be converted");
415                                 }
416                                 WriteAlert(_("Conversion Errors!"),s,
417                                            _("into chosen document class"));
418                         }
419                         
420                         params.textclass = new_class;
421                 } else {
422                         // problem changing class -- warn user and retain old style
423                         WriteAlert(_("Conversion Errors!"),
424                                    _("Errors loading new document class."),
425                                    _("Reverting to original document class."));
426 #ifdef USE_CLASS_COMBO
427                         combo_doc_class->select(params.textclass + 1);
428 #else
429                         fl_set_choice(class_->choice_doc_class,
430                                       params.textclass + 1);
431 #endif
432                 }
433         }
434         char tmpsep = params.paragraph_separation;
435         if (fl_get_button(class_->radio_doc_indent))
436                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
437         else
438                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
439         if (tmpsep != params.paragraph_separation)
440                 redo = true;
441         
442         VSpace tmpdefskip = params.getDefSkip();
443         switch (fl_get_choice (class_->choice_doc_skip)) {
444         case 1:
445                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
446                 break;
447         case 2:
448                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
449                 break;
450     case 3:
451             params.setDefSkip(VSpace(VSpace::BIGSKIP));
452             break;
453         case 4:
454                 params.setDefSkip
455                         (VSpace(LyXGlueLength(fl_get_input(class_->input_doc_skip))));
456                 break;
457                 // DocumentDefskipCB assures that this never happens
458         default:
459                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
460                 break;
461         }
462         if (!(tmpdefskip == params.getDefSkip()))
463                 redo = true;
464         
465         if (fl_get_button(class_->radio_doc_columns_two))
466                 params.columns = 2;
467         else
468                 params.columns = 1;
469         if (fl_get_button(class_->radio_doc_sides_two))
470                 params.sides = LyXTextClass::TwoSides;
471         else
472                 params.sides = LyXTextClass::OneSide;
473         
474         Spacing tmpSpacing = params.spacing;
475         switch(fl_get_choice(class_->choice_doc_spacing)) {
476         case 1:
477                 lyxerr[Debug::INFO] << "Spacing: SINGLE\n";
478                 params.spacing.set(Spacing::Single);
479                 break;
480         case 2:
481                 lyxerr[Debug::INFO] << "Spacing: ONEHALF\n";
482                 params.spacing.set(Spacing::Onehalf);
483                 break;
484         case 3:
485                 lyxerr[Debug::INFO] << "Spacing: DOUBLE\n";
486                 params.spacing.set(Spacing::Double);
487                 break;
488         case 4:
489                 lyxerr[Debug::INFO] << "Spacing: OTHER\n";
490                 params.spacing.set(Spacing::Other, 
491                                    fl_get_input(class_->input_doc_spacing));
492                 break;
493         }
494         if (tmpSpacing != params.spacing)
495                 redo = true;
496         
497         params.options = fl_get_input(class_->input_doc_extra);
498         
499         return redo;
500 }
501
502
503 void FormDocument::paper_apply()
504 {
505     BufferParams & params = lv_->buffer()->params;
506     
507     params.papersize2 = fl_get_choice(paper_->choice_papersize2)-1;
508     params.paperpackage = fl_get_choice(paper_->choice_paperpackage)-1;
509     params.use_geometry = fl_get_button(paper_->push_use_geometry);
510     if (fl_get_button(paper_->radio_landscape))
511         params.orientation = BufferParams::ORIENTATION_LANDSCAPE;
512     else
513         params.orientation = BufferParams::ORIENTATION_PORTRAIT;
514     params.paperwidth = fl_get_input(paper_->input_custom_width);
515     params.paperheight = fl_get_input(paper_->input_custom_height);
516     params.leftmargin = fl_get_input(paper_->input_left_margin);
517     params.topmargin = fl_get_input(paper_->input_top_margin);
518     params.rightmargin = fl_get_input(paper_->input_right_margin);
519     params.bottommargin = fl_get_input(paper_->input_bottom_margin);
520     params.headheight = fl_get_input(paper_->input_head_height);
521     params.headsep = fl_get_input(paper_->input_head_sep);
522     params.footskip = fl_get_input(paper_->input_foot_skip);
523     lv_->buffer()->setPaperStuff();
524 }
525
526
527 bool FormDocument::language_apply()
528 {
529     BufferParams & params = lv_->buffer()->params;
530     InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
531     bool redo = false;
532
533     switch(fl_get_choice(language_->choice_quotes_language) - 1) {
534     case 0:
535         lga = InsetQuotes::EnglishQ;
536         break;
537     case 1:
538         lga = InsetQuotes::SwedishQ;
539         break;
540     case 2:
541         lga = InsetQuotes::GermanQ;
542         break;
543     case 3:
544         lga = InsetQuotes::PolishQ;
545                 break;
546     case 4:
547         lga = InsetQuotes::FrenchQ;
548         break;
549     case 5:
550         lga = InsetQuotes::DanishQ;
551         break;
552     }
553     params.quotes_language = lga;
554     if (fl_get_button(language_->radio_single))   
555         params.quotes_times = InsetQuotes::SingleQ;
556     else
557         params.quotes_times = InsetQuotes::DoubleQ;
558
559     Language const * old_language = params.language_info;
560     params.language = combo_language->getline();
561     Languages::iterator lit = languages.find(params.language);
562
563     Language const * new_language;
564     if (lit != languages.end()) 
565         new_language = &(*lit).second;
566     else
567         new_language = default_language;
568
569     if (old_language != new_language
570         && old_language->RightToLeft() == new_language->RightToLeft()
571         && !lv_->buffer()->isMultiLingual())
572         lv_->buffer()->ChangeLanguage(old_language, new_language);
573     if (old_language != new_language) {
574         redo = true;
575     }
576     params.language_info = new_language;
577     params.inputenc = fl_get_choice_text(language_->choice_inputenc);
578
579     return redo;
580 }
581
582
583 bool FormDocument::options_apply()
584 {
585     BufferParams & params = lv_->buffer()->params;
586     bool redo = false;
587
588     params.graphicsDriver =
589         fl_get_choice_text(options_->choice_postscript_driver);
590     params.use_amsmath = fl_get_button(options_->check_use_amsmath);
591
592     int tmpchar = int(fl_get_counter_value(options_->slider_secnumdepth));
593     if (params.secnumdepth != tmpchar)
594         redo = true;
595     params.secnumdepth = tmpchar;
596    
597     params.tocdepth = int(fl_get_counter_value(options_->slider_tocdepth));
598
599     params.float_placement =
600         fl_get_input(options_->input_float_placement);
601
602     return redo;
603 }
604
605
606 void FormDocument::bullets_apply()
607 {
608     /* update the bullet settings */
609     BufferParams & param = lv_->buffer()->params;
610     
611     // a little bit of loop unrolling
612     param.user_defined_bullets[0] = param.temp_bullets[0];
613     param.user_defined_bullets[1] = param.temp_bullets[1];
614     param.user_defined_bullets[2] = param.temp_bullets[2];
615     param.user_defined_bullets[3] = param.temp_bullets[3];
616 }
617
618
619 void FormDocument::class_update(BufferParams const & params)
620 {
621     if (!class_)
622         return;
623
624     LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
625
626 #ifdef USE_CLASS_COMBO
627     combo_doc_class->select_text(
628         textclasslist.DescOfClass(params.textclass).c_str());
629 #else   
630     fl_set_choice_text(class_->choice_doc_class, 
631                        textclasslist.DescOfClass(params.textclass).c_str());
632 #endif
633     fl_set_choice_text(class_->choice_doc_fonts, params.fonts.c_str());
634     fl_clear_choice(class_->choice_doc_fontsize);
635     fl_addto_choice(class_->choice_doc_fontsize, "default");
636     fl_addto_choice(class_->choice_doc_fontsize,
637                     tclass.opt_fontsize().c_str());
638     fl_set_choice(class_->choice_doc_fontsize,
639                   tokenPos(tclass.opt_fontsize(), '|', params.fontsize)+2);
640     fl_clear_choice(class_->choice_doc_pagestyle);
641     fl_addto_choice(class_->choice_doc_pagestyle, "default");
642     fl_addto_choice(class_->choice_doc_pagestyle,
643                     tclass.opt_pagestyle().c_str());
644     fl_set_choice(class_->choice_doc_pagestyle,
645                   tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle)+2);
646     fl_set_button(class_->radio_doc_indent, 0);
647     fl_set_button(class_->radio_doc_skip, 0);
648     if (params.paragraph_separation == BufferParams::PARSEP_INDENT)
649         fl_set_button(class_->radio_doc_indent, 1);
650     else
651         fl_set_button(class_->radio_doc_skip, 1);
652     switch (params.getDefSkip().kind()) {
653     case VSpace::SMALLSKIP: 
654         fl_set_choice (class_->choice_doc_skip, 1);
655         break;
656     case VSpace::MEDSKIP: 
657         fl_set_choice (class_->choice_doc_skip, 2);
658         break;
659     case VSpace::BIGSKIP: 
660         fl_set_choice (class_->choice_doc_skip, 3);
661         break;
662     case VSpace::LENGTH: 
663         fl_set_choice (class_->choice_doc_skip, 4);
664         fl_set_input (class_->input_doc_skip,
665                       params.getDefSkip().asLyXCommand().c_str());
666         break;
667     default:
668         fl_set_choice (class_->choice_doc_skip, 2);
669         break;
670     }
671     fl_set_button(class_->radio_doc_sides_one, 0);
672     fl_set_button(class_->radio_doc_sides_two, 0);
673     if (params.sides == 2)
674         fl_set_button(class_->radio_doc_sides_two, 1);
675     else
676         fl_set_button(class_->radio_doc_sides_one, 1);
677     fl_set_button(class_->radio_doc_columns_one, 0);
678     fl_set_button(class_->radio_doc_columns_two, 0);
679     if (params.columns == 2)
680         fl_set_button(class_->radio_doc_columns_two, 1);
681     else
682         fl_set_button(class_->radio_doc_columns_one, 1);
683     fl_set_input(class_->input_doc_spacing, "");
684     switch (params.spacing.getSpace()) {
685     case Spacing::Default: // nothing bad should happen with this
686     case Spacing::Single:
687         // \singlespacing
688         fl_set_choice(class_->choice_doc_spacing, 1);
689         break;
690     case Spacing::Onehalf:
691         // \onehalfspacing
692         fl_set_choice(class_->choice_doc_spacing, 2);
693         break;
694     case Spacing::Double:
695         // \doublespacing
696         fl_set_choice(class_->choice_doc_spacing, 3);
697         break;
698     case Spacing::Other:
699         {
700             fl_set_choice(class_->choice_doc_spacing, 4);
701             char sval[20];
702             sprintf(sval,"%g",params.spacing.getValue()); 
703             fl_set_input(class_->input_doc_spacing, sval);
704             break;
705         }
706     }
707     if (!params.options.empty())
708         fl_set_input(class_->input_doc_extra, params.options.c_str());
709     else
710         fl_set_input(class_->input_doc_extra, "");
711 }
712
713
714 void FormDocument::language_update(BufferParams const & params)
715 {
716     if (!language_)
717         return;
718
719     combo_language->select_text(params.language.c_str());
720     fl_set_choice_text(language_->choice_inputenc, params.inputenc.c_str());
721     fl_set_choice(language_->choice_quotes_language, params.quotes_language + 1);
722     fl_set_button(language_->radio_single, 0);
723     fl_set_button(language_->radio_double, 0);
724     if (params.quotes_times == InsetQuotes::SingleQ)
725         fl_set_button(language_->radio_single, 1);
726     else
727         fl_set_button(language_->radio_double, 1);
728 }
729
730
731 void FormDocument::options_update(BufferParams const & params)
732 {
733     if (!options_)
734         return;
735
736     fl_set_choice_text(options_->choice_postscript_driver,
737                        params.graphicsDriver.c_str());
738     fl_set_button(options_->check_use_amsmath, params.use_amsmath);
739     fl_set_counter_value(options_->slider_secnumdepth, params.secnumdepth);
740     fl_set_counter_value(options_->slider_tocdepth, params.tocdepth);
741     if (!params.float_placement.empty())
742         fl_set_input(options_->input_float_placement,
743                      params.float_placement.c_str());
744     else
745         fl_set_input(options_->input_float_placement, "");
746 }
747
748
749 void FormDocument::paper_update(BufferParams const & params)
750 {
751     if (!paper_)
752         return;
753
754     fl_set_choice(paper_->choice_papersize2, params.papersize2 + 1);
755     fl_set_choice(paper_->choice_paperpackage, params.paperpackage + 1);
756     fl_set_button(paper_->push_use_geometry, params.use_geometry);
757     fl_set_button(paper_->radio_portrait, 0);
758     fl_set_button(paper_->radio_landscape, 0);
759     if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
760         fl_set_button(paper_->radio_landscape, 1);
761     else
762         fl_set_button(paper_->radio_portrait, 1);
763     fl_set_input(paper_->input_custom_width, params.paperwidth.c_str());
764     fl_set_input(paper_->input_custom_height, params.paperheight.c_str());
765     fl_set_input(paper_->input_left_margin, params.leftmargin.c_str());
766     fl_set_input(paper_->input_top_margin, params.topmargin.c_str());
767     fl_set_input(paper_->input_right_margin, params.rightmargin.c_str());
768     fl_set_input(paper_->input_bottom_margin, params.bottommargin.c_str());
769     fl_set_input(paper_->input_head_height, params.headheight.c_str());
770     fl_set_input(paper_->input_head_sep, params.headsep.c_str());
771     fl_set_input(paper_->input_foot_skip, params.footskip.c_str());
772     fl_set_focus_object(paper_->form, paper_->choice_papersize2);
773 }
774
775
776 void FormDocument::bullets_update(BufferParams const & params)
777 {
778     if (!bullets_ || ((XpmVersion<4) || (XpmVersion==4 && XpmRevision<7)))
779         return;
780
781     if (lv_->buffer()->isLinuxDoc()) {
782         fl_deactivate_object(fbullet);
783         fl_set_object_lcol(fbullet, FL_INACTIVE);
784         return;
785     } else {
786         fl_activate_object(fbullet);
787         fl_set_object_lcol(fbullet, FL_BLACK);
788     }
789
790     fl_set_button(bullets_->radio_bullet_depth_1, 1);
791     fl_set_input(bullets_->input_bullet_latex,
792                  params.user_defined_bullets[0].getText().c_str());
793     fl_set_choice(bullets_->choice_bullet_size,
794                   params.user_defined_bullets[0].getSize() + 2);
795 }
796
797
798 void FormDocument::checkReadOnly()
799 {
800     if (bc_.readOnly(lv_->buffer()->isReadonly())) {
801         combo_doc_class->deactivate();
802         combo_language->deactivate();
803         fl_set_object_label(dialog_->text_warning,
804                             _("Document is read-only."
805                               " No changes to layout permitted."));
806         fl_show_object(dialog_->text_warning);
807     } else {
808         combo_doc_class->activate();
809         combo_language->activate();
810         fl_hide_object(dialog_->text_warning);
811     }   
812 }
813
814
815 void FormDocument::checkMarginValues()
816 {
817     int const allEmpty = (!strlen(fl_get_input(paper_->input_top_margin)) &&
818                 !strlen(fl_get_input(paper_->input_bottom_margin)) &&
819                 !strlen(fl_get_input(paper_->input_left_margin)) &&
820                 !strlen(fl_get_input(paper_->input_right_margin)) &&
821                 !strlen(fl_get_input(paper_->input_head_height)) &&
822                 !strlen(fl_get_input(paper_->input_head_sep)) &&
823                 !strlen(fl_get_input(paper_->input_foot_skip)) &&
824                 !strlen(fl_get_input(paper_->input_custom_width)) &&
825                 !strlen(fl_get_input(paper_->input_custom_height)));
826     if (!allEmpty)
827         fl_set_button(paper_->push_use_geometry, 1);
828 }
829
830 bool FormDocument::CheckDocumentInput(FL_OBJECT * ob, long)
831 {
832     string str;
833     char val;
834     bool ok = true;
835     char const * input;
836     
837     checkMarginValues();
838     if (ob == paper_->choice_papersize2) {
839         val = fl_get_choice(paper_->choice_papersize2)-1;
840         if (val == BufferParams::VM_PAPER_DEFAULT) {
841             fl_set_button(paper_->push_use_geometry, 0);
842             checkMarginValues();
843         } else {
844             if ((val != BufferParams::VM_PAPER_USLETTER) &&
845                 (val != BufferParams::VM_PAPER_USLEGAL) &&
846                 (val != BufferParams::VM_PAPER_USEXECUTIVE) &&
847                 (val != BufferParams::VM_PAPER_A4) &&
848                 (val != BufferParams::VM_PAPER_A5) &&
849                 (val != BufferParams::VM_PAPER_B5)) {
850                 fl_set_button(paper_->push_use_geometry, 1);
851             }
852             fl_set_choice(paper_->choice_paperpackage,
853                           BufferParams::PACKAGE_NONE + 1);
854         }
855     } else if (ob == paper_->choice_paperpackage) {
856         val = fl_get_choice(paper_->choice_paperpackage)-1;
857         if (val != BufferParams::PACKAGE_NONE) {
858             fl_set_choice(paper_->choice_papersize2,
859                           BufferParams::VM_PAPER_DEFAULT + 1);
860             fl_set_button(paper_->push_use_geometry, 0);
861         }
862     } else if (ob == class_->input_doc_spacing) {
863         input = fl_get_input(class_->input_doc_spacing);
864         if (!*input) {
865             fl_set_choice (class_->choice_doc_spacing, 1);
866         } else {
867             fl_set_choice(class_->choice_doc_spacing, 4);
868         }
869     }
870     // this has to be all out of if/elseif because it has to deactivate
871     // the document buttons and so the whole stuff has to be tested again.
872     str = fl_get_input(paper_->input_custom_width);
873     ok = ok && (str.empty() || isValidLength(str));
874     str = fl_get_input(paper_->input_custom_height);
875     ok = ok && (str.empty() || isValidLength(str));
876     str = fl_get_input(paper_->input_left_margin);
877     ok = ok && (str.empty() || isValidLength(str));
878     str = fl_get_input(paper_->input_right_margin);
879     ok = ok && (str.empty() || isValidLength(str));
880     str = fl_get_input(paper_->input_top_margin);
881     ok = ok && (str.empty() || isValidLength(str));
882     str = fl_get_input(paper_->input_bottom_margin);
883     ok = ok && (str.empty() || isValidLength(str));
884     str = fl_get_input(paper_->input_head_height);
885     ok = ok && (str.empty() || isValidLength(str));
886     str = fl_get_input(paper_->input_head_sep);
887     ok = ok && (str.empty() || isValidLength(str));
888     str = fl_get_input(paper_->input_foot_skip);
889     ok = ok && (str.empty() || isValidLength(str));
890     // "Synchronize" the choice and the input field, so that it
891     // is impossible to commit senseless data.
892     input = fl_get_input (class_->input_doc_skip);
893     if (ob == class_->input_doc_skip) {
894         if (!*input) {
895             fl_set_choice (class_->choice_doc_skip, 2);
896         } else if (isValidGlueLength (input)) {
897             fl_set_choice (class_->choice_doc_skip, 4);
898         } else {
899             fl_set_choice(class_->choice_doc_skip, 4);
900             ok = false;
901         }
902     } else {
903         if (*input && !isValidGlueLength(input))
904             ok = false;
905     }
906     if ((fl_get_choice(class_->choice_doc_skip) == 4) && !*input)
907         ok = false;
908     else if (fl_get_choice(class_->choice_doc_skip) != 4)
909         fl_set_input (class_->input_doc_skip, "");
910
911     input = fl_get_input(class_->input_doc_spacing);
912     if ((fl_get_choice(class_->choice_doc_spacing) == 4) && !*input)
913         ok = false;
914     else  if (fl_get_choice(class_->choice_doc_spacing) != 4)
915         fl_set_input (class_->input_doc_spacing, "");
916     return ok;
917 }
918
919
920 void FormDocument::ChoiceBulletSize(FL_OBJECT * ob, long /*data*/ )
921 {
922     BufferParams & param = lv_->buffer()->params;
923
924     // convert from 1-6 range to -1-4 
925     param.temp_bullets[current_bullet_depth].setSize(fl_get_choice(ob) - 2);
926     fl_set_input(bullets_->input_bullet_latex,
927                  param.temp_bullets[current_bullet_depth].getText().c_str());
928 }
929
930
931 void FormDocument::InputBulletLaTeX(FL_OBJECT *, long)
932 {
933     BufferParams & param = lv_->buffer()->params;
934
935     param.temp_bullets[current_bullet_depth].
936         setText(fl_get_input(bullets_->input_bullet_latex));
937 }
938
939
940 void FormDocument::BulletDepth(FL_OBJECT * ob, State cb)
941 {
942     /* Should I do the following:                                 */
943     /*  1. change to the panel that the current bullet belongs in */
944     /*  2. show that bullet as selected                           */
945     /*  3. change the size setting to the size of the bullet in Q.*/
946     /*  4. display the latex equivalent in the latex box          */
947     /*                                                            */
948     /* I'm inclined to just go with 3 and 4 at the moment and     */
949     /* maybe try to support the others later                      */
950     BufferParams & param = lv_->buffer()->params;
951
952     int data = 0;
953     if( cb == BULLETDEPTH1 )
954             data = 0;
955     else if ( cb == BULLETDEPTH2 )
956             data = 1;
957     else if ( cb == BULLETDEPTH3 )
958             data = 2;
959     else if ( cb == BULLETDEPTH4 )
960             data = 3;
961
962     switch (fl_get_button_numb(ob)) {
963     case 3:
964             // right mouse button resets to default
965         param.temp_bullets[data] = ITEMIZE_DEFAULTS[data];
966     default:
967         current_bullet_depth = data;
968         fl_set_input(bullets_->input_bullet_latex,
969                      param.temp_bullets[data].getText().c_str());
970         fl_set_choice(bullets_->choice_bullet_size,
971                       param.temp_bullets[data].getSize() + 2);
972     }
973 }
974
975
976 void FormDocument::BulletPanel(FL_OBJECT * /*ob*/, State cb)
977 {
978     /* Here we have to change the background pixmap to that selected */
979     /* by the user. (eg. standard.xpm, psnfss1.xpm etc...)           */
980     
981     int data = 0;
982     if( cb == BULLETPANEL1 )
983             data = 0;
984     else if ( cb == BULLETPANEL2 )
985             data = 1;
986     else if ( cb == BULLETPANEL3 )
987             data = 2;
988     else if ( cb == BULLETPANEL4 )
989             data = 3;
990     else if ( cb == BULLETPANEL5 )
991             data = 4;
992     else if ( cb == BULLETPANEL6 )
993             data = 5;
994
995     if (data != current_bullet_panel) {
996         fl_freeze_form(bullets_->form);
997         current_bullet_panel = data;
998
999         /* free the current pixmap */
1000         fl_free_bmtable_pixmap(bullets_->bmtable_bullet_panel);
1001         string new_panel;
1002         switch (cb) {
1003             /* display the new one */
1004         case BULLETPANEL1 :
1005             new_panel = "standard";
1006             break;
1007         case BULLETPANEL2 :
1008             new_panel = "amssymb";
1009             break;
1010         case BULLETPANEL3 :
1011             new_panel = "psnfss1";
1012             break;
1013         case BULLETPANEL4 :
1014             new_panel = "psnfss2";
1015             break;
1016         case BULLETPANEL5 :
1017             new_panel = "psnfss3";
1018             break;
1019         case BULLETPANEL6 :
1020             new_panel = "psnfss4";
1021             break;
1022         default :
1023             /* something very wrong happened */
1024             // play it safe for now but should be an exception
1025             current_bullet_panel = 0;  // standard panel
1026             new_panel = "standard";
1027             break;
1028         }
1029         new_panel += ".xpm";
1030         fl_set_bmtable_pixmap_file(bullets_->bmtable_bullet_panel, 6, 6,
1031                                    LibFileSearch("images", new_panel.c_str()).c_str());
1032         fl_redraw_object(bullets_->bmtable_bullet_panel);
1033         fl_unfreeze_form(bullets_->form);
1034     }
1035 }
1036
1037
1038 void FormDocument::BulletBMTable(FL_OBJECT * ob, long /*data*/ )
1039 {
1040     /* handle the user input by setting the current bullet depth's pixmap */
1041     /* to that extracted from the current chosen position of the BMTable  */
1042     /* Don't forget to free the button's old pixmap first.                */
1043
1044     BufferParams & param = lv_->buffer()->params;
1045     int bmtable_button = fl_get_bmtable(ob);
1046
1047     /* try to keep the button held down till another is pushed */
1048     /*  fl_set_bmtable(ob, 1, bmtable_button); */
1049     param.temp_bullets[current_bullet_depth].setFont(current_bullet_panel);
1050     param.temp_bullets[current_bullet_depth].setCharacter(bmtable_button);
1051     fl_set_input(bullets_->input_bullet_latex,
1052                  param.temp_bullets[current_bullet_depth].getText().c_str());
1053 }
1054
1055
1056 void FormDocument::CheckChoiceClass(FL_OBJECT * ob, long)
1057 {
1058     if (!ob)
1059         ob = class_->choice_doc_class;
1060
1061     ProhibitInput(lv_->view());
1062     int tc;
1063     string tct;
1064
1065 #ifdef USE_CLASS_COMBO
1066     tc = combo_doc_class->get() - 1;
1067     tct = combo_doc_class->getline();
1068 #else
1069     tc = fl_get_choice(ob) - 1;
1070     tct = fl_get_choice_text(ob);
1071 #endif
1072     if (textclasslist.Load(tc)) {
1073         if (AskQuestion(_("Should I set some parameters to"), tct,
1074                         _("the defaults of this document class?"))) {
1075             BufferParams & params = lv_->buffer()->params;
1076
1077             params.textclass = tc;
1078             params.useClassDefaults();
1079             UpdateLayoutDocument(params);
1080         }
1081     } else {
1082         // unable to load new style
1083         WriteAlert(_("Conversion Errors!"),
1084                    _("Unable to switch to new document class."),
1085                    _("Reverting to original document class."));
1086 #ifdef USE_CLASS_COMBO
1087         combo_doc_class->select(lv_->buffer()->params.textclass + 1);
1088 #else
1089         fl_set_choice(class_->choice_doc_class, 
1090                       lv_->buffer()->params.textclass + 1);
1091 #endif
1092     }
1093     AllowInput(lv_->view());
1094 }
1095
1096
1097 void FormDocument::UpdateLayoutDocument(BufferParams const & params)
1098 {
1099     if (!dialog_)
1100         return;
1101
1102     checkReadOnly();
1103     class_update(params);
1104     paper_update(params);
1105     language_update(params);
1106     options_update(params);
1107     bullets_update(params);
1108 }