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