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