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