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