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