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