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