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