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