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