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