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