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