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