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