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