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