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