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