]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormDocument.C
b790ff2e5fa10cd69864bda7c857ca946f19cc47
[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     // 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, 400);
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());
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, 400);
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::const_iterator cit = languages.begin();
209         cit != languages.end(); ++cit) {
210         combo_language->addto((*cit).second.lang());
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 // we can safely ignore the parameter because we can always update
303 void FormDocument::update(bool)
304 {
305     if (!dialog_)
306         return;
307
308     checkReadOnly();
309
310     BufferParams const & params = lv_->buffer()->params;
311
312     class_update(params);
313     paper_update(params);
314     language_update(params);
315     options_update(params);
316     bullets_update(params);
317 }
318
319
320 bool FormDocument::input( FL_OBJECT * ob, long data )
321 {
322         State cb = static_cast<State>( data );
323
324         switch( cb ) {
325         case CHECKCHOICECLASS:
326                 CheckChoiceClass(ob, 0);
327                 break;
328         case CHOICEBULLETSIZE:
329                 ChoiceBulletSize(ob, 0);
330                 break;
331         case INPUTBULLETLATEX:
332                 InputBulletLaTeX(ob, 0);
333                 break;
334         case BULLETDEPTH1:
335         case BULLETDEPTH2:
336         case BULLETDEPTH3:
337         case BULLETDEPTH4:
338                 BulletDepth(ob, cb);
339                 break;
340         case BULLETPANEL1:
341         case BULLETPANEL2:
342         case BULLETPANEL3:
343         case BULLETPANEL4:
344         case BULLETPANEL5:
345         case BULLETPANEL6:
346                 BulletPanel(ob, cb);
347                 break;
348         case BULLETBMTABLE:
349                 BulletBMTable(ob, 0);
350                 break;
351         default:
352                 break;
353         }
354         
355         switch( data ) {
356         case INPUT:
357         case CHECKCHOICECLASS:
358         case CHOICEBULLETSIZE:
359         case INPUTBULLETLATEX:
360         case BULLETBMTABLE:
361                 return CheckDocumentInput(ob, 0);
362         default:
363                 break;
364         }
365
366         return true;
367 }
368
369
370 void FormDocument::ComboInputCB(int, void * v, Combox * combox)
371 {
372     FormDocument * pre = static_cast<FormDocument*>(v);
373     if (combox == pre->combo_doc_class)
374         pre->CheckChoiceClass(0, 0);
375     pre->bc_.valid(pre->CheckDocumentInput(0,0));
376 }
377
378
379 bool FormDocument::class_apply()
380 {
381         bool redo = false;
382         BufferParams &params = lv_->buffer()->params;
383
384         // If default skip is a "Length" but there's no text in the
385         // input field, reset the kind to "Medskip", which is the default.
386         if ((fl_get_choice (class_->choice_doc_skip) == 4) &&
387             !*(fl_get_input (class_->input_doc_skip))) {
388                 fl_set_choice (class_->choice_doc_skip, 2);
389         }
390         params.fonts = fl_get_choice_text(class_->choice_doc_fonts);
391         params.fontsize = fl_get_choice_text(class_->choice_doc_fontsize);
392         params.pagestyle = fl_get_choice_text(class_->choice_doc_pagestyle);
393
394 #ifdef USE_CLASS_COMBO   
395         unsigned int const new_class = combo_doc_class->get() - 1;
396 #else
397         unsigned int const new_class = fl_get_choice(class_->choice_doc_class) - 1;
398 #endif
399         if (params.textclass != new_class) {
400                 // try to load new_class
401                 if (textclasslist.Load(new_class)) {
402                         // successfully loaded
403                         redo = true;
404                         setMinibuffer(lv_, _("Converting document to new document class..."));
405                         CutAndPaste cap;
406                         int ret = cap.SwitchLayoutsBetweenClasses(
407                             params.textclass, new_class,
408                             lv_->buffer()->paragraph);
409                         if (ret) {
410                                 string s;
411                                 if (ret==1) {
412                                         s = _("One paragraph couldn't be converted");
413                                 } else {
414                                         s += tostr(ret);
415                                         s += _(" paragraphs couldn't be converted");
416                                 }
417                                 WriteAlert(_("Conversion Errors!"),s,
418                                            _("into chosen document class"));
419                         }
420                         
421                         params.textclass = new_class;
422                 } else {
423                         // problem changing class -- warn user and retain old style
424                         WriteAlert(_("Conversion Errors!"),
425                                    _("Errors loading new document class."),
426                                    _("Reverting to original document class."));
427 #ifdef USE_CLASS_COMBO
428                         combo_doc_class->select(params.textclass + 1);
429 #else
430                         fl_set_choice(class_->choice_doc_class,
431                                       params.textclass + 1);
432 #endif
433                 }
434         }
435         char tmpsep = params.paragraph_separation;
436         if (fl_get_button(class_->radio_doc_indent))
437                 params.paragraph_separation = BufferParams::PARSEP_INDENT;
438         else
439                 params.paragraph_separation = BufferParams::PARSEP_SKIP;
440         if (tmpsep != params.paragraph_separation)
441                 redo = true;
442         
443         VSpace tmpdefskip = params.getDefSkip();
444         switch (fl_get_choice (class_->choice_doc_skip)) {
445         case 1:
446                 params.setDefSkip(VSpace(VSpace::SMALLSKIP));
447                 break;
448         case 2:
449                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
450                 break;
451     case 3:
452             params.setDefSkip(VSpace(VSpace::BIGSKIP));
453             break;
454         case 4:
455                 params.setDefSkip
456                         (VSpace(LyXGlueLength(fl_get_input(class_->input_doc_skip))));
457                 break;
458                 // DocumentDefskipCB assures that this never happens
459         default:
460                 params.setDefSkip(VSpace(VSpace::MEDSKIP));
461                 break;
462         }
463         if (!(tmpdefskip == params.getDefSkip()))
464                 redo = true;
465         
466         if (fl_get_button(class_->radio_doc_columns_two))
467                 params.columns = 2;
468         else
469                 params.columns = 1;
470         if (fl_get_button(class_->radio_doc_sides_two))
471                 params.sides = LyXTextClass::TwoSides;
472         else
473                 params.sides = LyXTextClass::OneSide;
474         
475         Spacing tmpSpacing = params.spacing;
476         switch(fl_get_choice(class_->choice_doc_spacing)) {
477         case 1:
478                 lyxerr[Debug::INFO] << "Spacing: SINGLE\n";
479                 params.spacing.set(Spacing::Single);
480                 break;
481         case 2:
482                 lyxerr[Debug::INFO] << "Spacing: ONEHALF\n";
483                 params.spacing.set(Spacing::Onehalf);
484                 break;
485         case 3:
486                 lyxerr[Debug::INFO] << "Spacing: DOUBLE\n";
487                 params.spacing.set(Spacing::Double);
488                 break;
489         case 4:
490                 lyxerr[Debug::INFO] << "Spacing: OTHER\n";
491                 params.spacing.set(Spacing::Other, 
492                                    fl_get_input(class_->input_doc_spacing));
493                 break;
494         }
495         if (tmpSpacing != params.spacing)
496                 redo = true;
497         
498         params.options = fl_get_input(class_->input_doc_extra);
499         
500         return redo;
501 }
502
503
504 void FormDocument::paper_apply()
505 {
506     BufferParams & params = lv_->buffer()->params;
507     
508     params.papersize2 = fl_get_choice(paper_->choice_papersize2)-1;
509     params.paperpackage = fl_get_choice(paper_->choice_paperpackage)-1;
510     params.use_geometry = fl_get_button(paper_->push_use_geometry);
511     if (fl_get_button(paper_->radio_landscape))
512         params.orientation = BufferParams::ORIENTATION_LANDSCAPE;
513     else
514         params.orientation = BufferParams::ORIENTATION_PORTRAIT;
515     params.paperwidth = fl_get_input(paper_->input_custom_width);
516     params.paperheight = fl_get_input(paper_->input_custom_height);
517     params.leftmargin = fl_get_input(paper_->input_left_margin);
518     params.topmargin = fl_get_input(paper_->input_top_margin);
519     params.rightmargin = fl_get_input(paper_->input_right_margin);
520     params.bottommargin = fl_get_input(paper_->input_bottom_margin);
521     params.headheight = fl_get_input(paper_->input_head_height);
522     params.headsep = fl_get_input(paper_->input_head_sep);
523     params.footskip = fl_get_input(paper_->input_foot_skip);
524     lv_->buffer()->setPaperStuff();
525 }
526
527
528 bool FormDocument::language_apply()
529 {
530     BufferParams & params = lv_->buffer()->params;
531     InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
532     bool redo = false;
533
534     switch(fl_get_choice(language_->choice_quotes_language) - 1) {
535     case 0:
536         lga = InsetQuotes::EnglishQ;
537         break;
538     case 1:
539         lga = InsetQuotes::SwedishQ;
540         break;
541     case 2:
542         lga = InsetQuotes::GermanQ;
543         break;
544     case 3:
545         lga = InsetQuotes::PolishQ;
546                 break;
547     case 4:
548         lga = InsetQuotes::FrenchQ;
549         break;
550     case 5:
551         lga = InsetQuotes::DanishQ;
552         break;
553     }
554     params.quotes_language = lga;
555     if (fl_get_button(language_->radio_single))   
556         params.quotes_times = InsetQuotes::SingleQ;
557     else
558         params.quotes_times = InsetQuotes::DoubleQ;
559
560     Language const * old_language = params.language;
561     Language const * new_language = 
562             languages.getLanguage(combo_language->getline());
563     if (!new_language)
564         new_language = default_language;
565
566     if (old_language != new_language
567         && old_language->RightToLeft() == new_language->RightToLeft()
568         && !lv_->buffer()->isMultiLingual())
569         lv_->buffer()->ChangeLanguage(old_language, new_language);
570     if (old_language != new_language) {
571         redo = true;
572     }
573     params.language = new_language;
574     params.inputenc = fl_get_choice_text(language_->choice_inputenc);
575
576     return redo;
577 }
578
579
580 bool FormDocument::options_apply()
581 {
582     BufferParams & params = lv_->buffer()->params;
583     bool redo = false;
584
585     params.graphicsDriver =
586         fl_get_choice_text(options_->choice_postscript_driver);
587     params.use_amsmath = fl_get_button(options_->check_use_amsmath);
588
589     int tmpchar = int(fl_get_counter_value(options_->slider_secnumdepth));
590     if (params.secnumdepth != tmpchar)
591         redo = true;
592     params.secnumdepth = tmpchar;
593    
594     params.tocdepth = int(fl_get_counter_value(options_->slider_tocdepth));
595
596     params.float_placement =
597         fl_get_input(options_->input_float_placement);
598
599     return redo;
600 }
601
602
603 void FormDocument::bullets_apply()
604 {
605     /* update the bullet settings */
606     BufferParams & param = lv_->buffer()->params;
607     
608     // a little bit of loop unrolling
609     param.user_defined_bullets[0] = param.temp_bullets[0];
610     param.user_defined_bullets[1] = param.temp_bullets[1];
611     param.user_defined_bullets[2] = param.temp_bullets[2];
612     param.user_defined_bullets[3] = param.temp_bullets[3];
613 }
614
615
616 void FormDocument::class_update(BufferParams const & params)
617 {
618     if (!class_)
619         return;
620
621     LyXTextClass const & tclass = textclasslist.TextClass(params.textclass);
622
623 #ifdef USE_CLASS_COMBO
624     combo_doc_class->select_text(
625         textclasslist.DescOfClass(params.textclass));
626 #else   
627     fl_set_choice_text(class_->choice_doc_class, 
628                        textclasslist.DescOfClass(params.textclass).c_str());
629 #endif
630     fl_set_choice_text(class_->choice_doc_fonts, params.fonts.c_str());
631     fl_clear_choice(class_->choice_doc_fontsize);
632     fl_addto_choice(class_->choice_doc_fontsize, "default");
633     fl_addto_choice(class_->choice_doc_fontsize,
634                     tclass.opt_fontsize().c_str());
635     fl_set_choice(class_->choice_doc_fontsize,
636                   tokenPos(tclass.opt_fontsize(), '|', params.fontsize)+2);
637     fl_clear_choice(class_->choice_doc_pagestyle);
638     fl_addto_choice(class_->choice_doc_pagestyle, "default");
639     fl_addto_choice(class_->choice_doc_pagestyle,
640                     tclass.opt_pagestyle().c_str());
641     fl_set_choice(class_->choice_doc_pagestyle,
642                   tokenPos(tclass.opt_pagestyle(), '|', params.pagestyle)+2);
643     fl_set_button(class_->radio_doc_indent, 0);
644     fl_set_button(class_->radio_doc_skip, 0);
645     if (params.paragraph_separation == BufferParams::PARSEP_INDENT)
646         fl_set_button(class_->radio_doc_indent, 1);
647     else
648         fl_set_button(class_->radio_doc_skip, 1);
649     switch (params.getDefSkip().kind()) {
650     case VSpace::SMALLSKIP: 
651         fl_set_choice (class_->choice_doc_skip, 1);
652         break;
653     case VSpace::MEDSKIP: 
654         fl_set_choice (class_->choice_doc_skip, 2);
655         break;
656     case VSpace::BIGSKIP: 
657         fl_set_choice (class_->choice_doc_skip, 3);
658         break;
659     case VSpace::LENGTH: 
660         fl_set_choice (class_->choice_doc_skip, 4);
661         fl_set_input (class_->input_doc_skip,
662                       params.getDefSkip().asLyXCommand().c_str());
663         break;
664     default:
665         fl_set_choice (class_->choice_doc_skip, 2);
666         break;
667     }
668     fl_set_button(class_->radio_doc_sides_one, 0);
669     fl_set_button(class_->radio_doc_sides_two, 0);
670     if (params.sides == 2)
671         fl_set_button(class_->radio_doc_sides_two, 1);
672     else
673         fl_set_button(class_->radio_doc_sides_one, 1);
674     fl_set_button(class_->radio_doc_columns_one, 0);
675     fl_set_button(class_->radio_doc_columns_two, 0);
676     if (params.columns == 2)
677         fl_set_button(class_->radio_doc_columns_two, 1);
678     else
679         fl_set_button(class_->radio_doc_columns_one, 1);
680     fl_set_input(class_->input_doc_spacing, "");
681     switch (params.spacing.getSpace()) {
682     case Spacing::Default: // nothing bad should happen with this
683     case Spacing::Single:
684         // \singlespacing
685         fl_set_choice(class_->choice_doc_spacing, 1);
686         break;
687     case Spacing::Onehalf:
688         // \onehalfspacing
689         fl_set_choice(class_->choice_doc_spacing, 2);
690         break;
691     case Spacing::Double:
692         // \doublespacing
693         fl_set_choice(class_->choice_doc_spacing, 3);
694         break;
695     case Spacing::Other:
696         {
697             fl_set_choice(class_->choice_doc_spacing, 4);
698             char sval[20];
699             sprintf(sval,"%g",params.spacing.getValue()); 
700             fl_set_input(class_->input_doc_spacing, sval);
701             break;
702         }
703     }
704     if (!params.options.empty())
705         fl_set_input(class_->input_doc_extra, params.options.c_str());
706     else
707         fl_set_input(class_->input_doc_extra, "");
708 }
709
710
711 void FormDocument::language_update(BufferParams const & params)
712 {
713     if (!language_)
714         return;
715
716     combo_language->select_text(params.language->lang());
717     fl_set_choice_text(language_->choice_inputenc, params.inputenc.c_str());
718     fl_set_choice(language_->choice_quotes_language, params.quotes_language + 1);
719     fl_set_button(language_->radio_single, 0);
720     fl_set_button(language_->radio_double, 0);
721     if (params.quotes_times == InsetQuotes::SingleQ)
722         fl_set_button(language_->radio_single, 1);
723     else
724         fl_set_button(language_->radio_double, 1);
725 }
726
727
728 void FormDocument::options_update(BufferParams const & params)
729 {
730     if (!options_)
731         return;
732
733     fl_set_choice_text(options_->choice_postscript_driver,
734                        params.graphicsDriver.c_str());
735     fl_set_button(options_->check_use_amsmath, params.use_amsmath);
736     fl_set_counter_value(options_->slider_secnumdepth, params.secnumdepth);
737     fl_set_counter_value(options_->slider_tocdepth, params.tocdepth);
738     if (!params.float_placement.empty())
739         fl_set_input(options_->input_float_placement,
740                      params.float_placement.c_str());
741     else
742         fl_set_input(options_->input_float_placement, "");
743 }
744
745
746 void FormDocument::paper_update(BufferParams const & params)
747 {
748     if (!paper_)
749         return;
750
751     fl_set_choice(paper_->choice_papersize2, params.papersize2 + 1);
752     fl_set_choice(paper_->choice_paperpackage, params.paperpackage + 1);
753     fl_set_button(paper_->push_use_geometry, params.use_geometry);
754     fl_set_button(paper_->radio_portrait, 0);
755     fl_set_button(paper_->radio_landscape, 0);
756     if (params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
757         fl_set_button(paper_->radio_landscape, 1);
758     else
759         fl_set_button(paper_->radio_portrait, 1);
760     fl_set_input(paper_->input_custom_width, params.paperwidth.c_str());
761     fl_set_input(paper_->input_custom_height, params.paperheight.c_str());
762     fl_set_input(paper_->input_left_margin, params.leftmargin.c_str());
763     fl_set_input(paper_->input_top_margin, params.topmargin.c_str());
764     fl_set_input(paper_->input_right_margin, params.rightmargin.c_str());
765     fl_set_input(paper_->input_bottom_margin, params.bottommargin.c_str());
766     fl_set_input(paper_->input_head_height, params.headheight.c_str());
767     fl_set_input(paper_->input_head_sep, params.headsep.c_str());
768     fl_set_input(paper_->input_foot_skip, params.footskip.c_str());
769     fl_set_focus_object(paper_->form, paper_->choice_papersize2);
770 }
771
772
773 void FormDocument::bullets_update(BufferParams const & params)
774 {
775     if (!bullets_ || ((XpmVersion<4) || (XpmVersion==4 && XpmRevision<7)))
776         return;
777
778     if (lv_->buffer()->isLinuxDoc()) {
779         fl_deactivate_object(fbullet);
780         fl_set_object_lcol(fbullet, FL_INACTIVE);
781         return;
782     } else {
783         fl_activate_object(fbullet);
784         fl_set_object_lcol(fbullet, FL_BLACK);
785     }
786
787     fl_set_button(bullets_->radio_bullet_depth_1, 1);
788     fl_set_input(bullets_->input_bullet_latex,
789                  params.user_defined_bullets[0].getText().c_str());
790     fl_set_choice(bullets_->choice_bullet_size,
791                   params.user_defined_bullets[0].getSize() + 2);
792 }
793
794
795 void FormDocument::checkReadOnly()
796 {
797     if (bc_.readOnly(lv_->buffer()->isReadonly())) {
798         combo_doc_class->deactivate();
799         combo_language->deactivate();
800         fl_set_object_label(dialog_->text_warning,
801                             _("Document is read-only."
802                               " No changes to layout permitted."));
803         fl_show_object(dialog_->text_warning);
804     } else {
805         combo_doc_class->activate();
806         combo_language->activate();
807         fl_hide_object(dialog_->text_warning);
808     }   
809 }
810
811
812 void FormDocument::checkMarginValues()
813 {
814     int const allEmpty = (!strlen(fl_get_input(paper_->input_top_margin)) &&
815                 !strlen(fl_get_input(paper_->input_bottom_margin)) &&
816                 !strlen(fl_get_input(paper_->input_left_margin)) &&
817                 !strlen(fl_get_input(paper_->input_right_margin)) &&
818                 !strlen(fl_get_input(paper_->input_head_height)) &&
819                 !strlen(fl_get_input(paper_->input_head_sep)) &&
820                 !strlen(fl_get_input(paper_->input_foot_skip)) &&
821                 !strlen(fl_get_input(paper_->input_custom_width)) &&
822                 !strlen(fl_get_input(paper_->input_custom_height)));
823     if (!allEmpty)
824         fl_set_button(paper_->push_use_geometry, 1);
825 }
826
827 bool FormDocument::CheckDocumentInput(FL_OBJECT * ob, long)
828 {
829     string str;
830     char val;
831     bool ok = true;
832     char const * input;
833     
834     checkMarginValues();
835     if (ob == paper_->choice_papersize2) {
836         val = fl_get_choice(paper_->choice_papersize2)-1;
837         if (val == BufferParams::VM_PAPER_DEFAULT) {
838             fl_set_button(paper_->push_use_geometry, 0);
839             checkMarginValues();
840         } else {
841             if ((val != BufferParams::VM_PAPER_USLETTER) &&
842                 (val != BufferParams::VM_PAPER_USLEGAL) &&
843                 (val != BufferParams::VM_PAPER_USEXECUTIVE) &&
844                 (val != BufferParams::VM_PAPER_A4) &&
845                 (val != BufferParams::VM_PAPER_A5) &&
846                 (val != BufferParams::VM_PAPER_B5)) {
847                 fl_set_button(paper_->push_use_geometry, 1);
848             }
849             fl_set_choice(paper_->choice_paperpackage,
850                           BufferParams::PACKAGE_NONE + 1);
851         }
852     } else if (ob == paper_->choice_paperpackage) {
853         val = fl_get_choice(paper_->choice_paperpackage)-1;
854         if (val != BufferParams::PACKAGE_NONE) {
855             fl_set_choice(paper_->choice_papersize2,
856                           BufferParams::VM_PAPER_DEFAULT + 1);
857             fl_set_button(paper_->push_use_geometry, 0);
858         }
859     } else if (ob == class_->input_doc_spacing) {
860         input = fl_get_input(class_->input_doc_spacing);
861         if (!*input) {
862             fl_set_choice (class_->choice_doc_spacing, 1);
863         } else {
864             fl_set_choice(class_->choice_doc_spacing, 4);
865         }
866     }
867     // this has to be all out of if/elseif because it has to deactivate
868     // the document buttons and so the whole stuff has to be tested again.
869     str = fl_get_input(paper_->input_custom_width);
870     ok = ok && (str.empty() || isValidLength(str));
871     str = fl_get_input(paper_->input_custom_height);
872     ok = ok && (str.empty() || isValidLength(str));
873     str = fl_get_input(paper_->input_left_margin);
874     ok = ok && (str.empty() || isValidLength(str));
875     str = fl_get_input(paper_->input_right_margin);
876     ok = ok && (str.empty() || isValidLength(str));
877     str = fl_get_input(paper_->input_top_margin);
878     ok = ok && (str.empty() || isValidLength(str));
879     str = fl_get_input(paper_->input_bottom_margin);
880     ok = ok && (str.empty() || isValidLength(str));
881     str = fl_get_input(paper_->input_head_height);
882     ok = ok && (str.empty() || isValidLength(str));
883     str = fl_get_input(paper_->input_head_sep);
884     ok = ok && (str.empty() || isValidLength(str));
885     str = fl_get_input(paper_->input_foot_skip);
886     ok = ok && (str.empty() || isValidLength(str));
887     // "Synchronize" the choice and the input field, so that it
888     // is impossible to commit senseless data.
889     input = fl_get_input (class_->input_doc_skip);
890     if (ob == class_->input_doc_skip) {
891         if (!*input) {
892             fl_set_choice (class_->choice_doc_skip, 2);
893         } else if (isValidGlueLength (input)) {
894             fl_set_choice (class_->choice_doc_skip, 4);
895         } else {
896             fl_set_choice(class_->choice_doc_skip, 4);
897             ok = false;
898         }
899     } else {
900         if (*input && !isValidGlueLength(input))
901             ok = false;
902     }
903     if ((fl_get_choice(class_->choice_doc_skip) == 4) && !*input)
904         ok = false;
905     else if (fl_get_choice(class_->choice_doc_skip) != 4)
906         fl_set_input (class_->input_doc_skip, "");
907
908     input = fl_get_input(class_->input_doc_spacing);
909     if ((fl_get_choice(class_->choice_doc_spacing) == 4) && !*input)
910         ok = false;
911     else  if (fl_get_choice(class_->choice_doc_spacing) != 4)
912         fl_set_input (class_->input_doc_spacing, "");
913     return ok;
914 }
915
916
917 void FormDocument::ChoiceBulletSize(FL_OBJECT * ob, long /*data*/ )
918 {
919     BufferParams & param = lv_->buffer()->params;
920
921     // convert from 1-6 range to -1-4 
922     param.temp_bullets[current_bullet_depth].setSize(fl_get_choice(ob) - 2);
923     fl_set_input(bullets_->input_bullet_latex,
924                  param.temp_bullets[current_bullet_depth].getText().c_str());
925 }
926
927
928 void FormDocument::InputBulletLaTeX(FL_OBJECT *, long)
929 {
930     BufferParams & param = lv_->buffer()->params;
931
932     param.temp_bullets[current_bullet_depth].
933         setText(fl_get_input(bullets_->input_bullet_latex));
934 }
935
936
937 void FormDocument::BulletDepth(FL_OBJECT * ob, State cb)
938 {
939     /* Should I do the following:                                 */
940     /*  1. change to the panel that the current bullet belongs in */
941     /*  2. show that bullet as selected                           */
942     /*  3. change the size setting to the size of the bullet in Q.*/
943     /*  4. display the latex equivalent in the latex box          */
944     /*                                                            */
945     /* I'm inclined to just go with 3 and 4 at the moment and     */
946     /* maybe try to support the others later                      */
947     BufferParams & param = lv_->buffer()->params;
948
949     int data = 0;
950     if( cb == BULLETDEPTH1 )
951             data = 0;
952     else if ( cb == BULLETDEPTH2 )
953             data = 1;
954     else if ( cb == BULLETDEPTH3 )
955             data = 2;
956     else if ( cb == BULLETDEPTH4 )
957             data = 3;
958
959     switch (fl_get_button_numb(ob)) {
960     case 3:
961             // right mouse button resets to default
962         param.temp_bullets[data] = ITEMIZE_DEFAULTS[data];
963     default:
964         current_bullet_depth = data;
965         fl_set_input(bullets_->input_bullet_latex,
966                      param.temp_bullets[data].getText().c_str());
967         fl_set_choice(bullets_->choice_bullet_size,
968                       param.temp_bullets[data].getSize() + 2);
969     }
970 }
971
972
973 void FormDocument::BulletPanel(FL_OBJECT * /*ob*/, State cb)
974 {
975     /* Here we have to change the background pixmap to that selected */
976     /* by the user. (eg. standard.xpm, psnfss1.xpm etc...)           */
977     
978     int data = 0;
979     if( cb == BULLETPANEL1 )
980             data = 0;
981     else if ( cb == BULLETPANEL2 )
982             data = 1;
983     else if ( cb == BULLETPANEL3 )
984             data = 2;
985     else if ( cb == BULLETPANEL4 )
986             data = 3;
987     else if ( cb == BULLETPANEL5 )
988             data = 4;
989     else if ( cb == BULLETPANEL6 )
990             data = 5;
991
992     if (data != current_bullet_panel) {
993         fl_freeze_form(bullets_->form);
994         current_bullet_panel = data;
995
996         /* free the current pixmap */
997         fl_free_bmtable_pixmap(bullets_->bmtable_bullet_panel);
998         string new_panel;
999         switch (cb) {
1000             /* display the new one */
1001         case BULLETPANEL1 :
1002             new_panel = "standard";
1003             break;
1004         case BULLETPANEL2 :
1005             new_panel = "amssymb";
1006             break;
1007         case BULLETPANEL3 :
1008             new_panel = "psnfss1";
1009             break;
1010         case BULLETPANEL4 :
1011             new_panel = "psnfss2";
1012             break;
1013         case BULLETPANEL5 :
1014             new_panel = "psnfss3";
1015             break;
1016         case BULLETPANEL6 :
1017             new_panel = "psnfss4";
1018             break;
1019         default :
1020             /* something very wrong happened */
1021             // play it safe for now but should be an exception
1022             current_bullet_panel = 0;  // standard panel
1023             new_panel = "standard";
1024             break;
1025         }
1026         new_panel += ".xpm";
1027         fl_set_bmtable_pixmap_file(bullets_->bmtable_bullet_panel, 6, 6,
1028                                    LibFileSearch("images", new_panel).c_str());
1029         fl_redraw_object(bullets_->bmtable_bullet_panel);
1030         fl_unfreeze_form(bullets_->form);
1031     }
1032 }
1033
1034
1035 void FormDocument::BulletBMTable(FL_OBJECT * ob, long /*data*/ )
1036 {
1037     /* handle the user input by setting the current bullet depth's pixmap */
1038     /* to that extracted from the current chosen position of the BMTable  */
1039     /* Don't forget to free the button's old pixmap first.                */
1040
1041     BufferParams & param = lv_->buffer()->params;
1042     int bmtable_button = fl_get_bmtable(ob);
1043
1044     /* try to keep the button held down till another is pushed */
1045     /*  fl_set_bmtable(ob, 1, bmtable_button); */
1046     param.temp_bullets[current_bullet_depth].setFont(current_bullet_panel);
1047     param.temp_bullets[current_bullet_depth].setCharacter(bmtable_button);
1048     fl_set_input(bullets_->input_bullet_latex,
1049                  param.temp_bullets[current_bullet_depth].getText().c_str());
1050 }
1051
1052
1053 void FormDocument::CheckChoiceClass(FL_OBJECT * ob, long)
1054 {
1055     if (!ob)
1056         ob = class_->choice_doc_class;
1057
1058     ProhibitInput(lv_->view());
1059
1060 #ifdef USE_CLASS_COMBO
1061     int tc = combo_doc_class->get() - 1;
1062     string tct = combo_doc_class->getline();
1063 #else
1064     int tc = fl_get_choice(ob) - 1;
1065     string tct = fl_get_choice_text(ob);
1066 #endif
1067     if (textclasslist.Load(tc)) {
1068         if (AskQuestion(_("Should I set some parameters to"), tct,
1069                         _("the defaults of this document class?"))) {
1070             BufferParams & params = lv_->buffer()->params;
1071
1072             params.textclass = tc;
1073             params.useClassDefaults();
1074             UpdateLayoutDocument(params);
1075         }
1076     } else {
1077         // unable to load new style
1078         WriteAlert(_("Conversion Errors!"),
1079                    _("Unable to switch to new document class."),
1080                    _("Reverting to original document class."));
1081 #ifdef USE_CLASS_COMBO
1082         combo_doc_class->select(lv_->buffer()->params.textclass + 1);
1083 #else
1084         fl_set_choice(class_->choice_doc_class, 
1085                       lv_->buffer()->params.textclass + 1);
1086 #endif
1087     }
1088     AllowInput(lv_->view());
1089 }
1090
1091
1092 void FormDocument::UpdateLayoutDocument(BufferParams const & params)
1093 {
1094     if (!dialog_)
1095         return;
1096
1097     checkReadOnly();
1098     class_update(params);
1099     paper_update(params);
1100     language_update(params);
1101     options_update(params);
1102     bullets_update(params);
1103 }