]> git.lyx.org Git - lyx.git/blob - src/lyx_cb.C
Small fix.
[lyx.git] / src / lyx_cb.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich,
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <fstream>
14 #include <algorithm>
15 #include <iostream>
16
17 #include "LString.h"
18 #include FORMS_H_LOCATION
19 #include "lyx.h"
20 #include "layout_forms.h"
21 #include "bullet_forms.h"
22 #include "form1.h"
23 #include "lyx_main.h"
24 #include "lyx_cb.h"
25 #include "insets/insetref.h"
26 #include "insets/insetlabel.h"
27 #include "insets/figinset.h"
28 #include "lyxfunc.h"
29 #include "minibuffer.h"
30 #include "combox.h"
31 #include "bufferlist.h"
32 #include "filedlg.h"
33 #include "lyx_gui_misc.h"
34 #include "LyXView.h"
35 #include "BufferView.h"
36 #include "lastfiles.h"
37 #include "bufferview_funcs.h"
38 #include "support/FileInfo.h"
39 #include "support/syscall.h"
40 #include "support/filetools.h"
41 #include "support/path.h"
42 #include "lyxserver.h"
43 #include "FontLoader.h"
44 #include "lyxrc.h"
45 #include "lyxtext.h"
46 #include "CutAndPaste.h"
47 #include "TextCache.h"
48
49 using std::ifstream;
50 using std::copy;
51 using std::back_inserter;
52 using std::endl;
53 using std::cout;
54 using std::ios;
55 using std::istream_iterator;
56 using std::pair;
57 using std::vector;
58 using std::sort;
59 using std::equal;
60
61 extern Combox * combo_language;
62 extern Combox * combo_language2;
63 extern BufferList bufferlist;
64 extern void show_symbols_form();
65 extern FD_form_title * fd_form_title;
66 extern FD_form_paragraph * fd_form_paragraph;
67 extern FD_form_character * fd_form_character;
68 extern FD_form_document * fd_form_document;
69 extern FD_form_quotes * fd_form_quotes;
70 extern FD_form_preamble * fd_form_preamble;
71 extern FD_form_table * fd_form_table;
72 extern FD_form_figure * fd_form_figure;
73 extern FD_form_screen * fd_form_screen;
74 extern FD_form_toc * fd_form_toc;
75 extern FD_form_ref * fd_form_ref;
76 extern FD_form_bullet * fd_form_bullet;
77
78 extern BufferView * current_view; // called too many times in this file...
79
80 extern void DeleteSimpleCutBuffer(); /* for the cleanup when exiting */
81
82 extern bool send_fax(string const & fname, string const & sendcmd);
83 extern void MenuSendto();
84
85 extern LyXServer * lyxserver;
86
87 // this should be static, but I need it in buffer.C
88 bool quitting;  // flag, that we are quitting the program
89 extern bool finished; // all cleanup done just let it run through now.
90
91 char ascii_type; /* for selection notify callbacks */
92
93 bool scrolling = false;
94
95 // This is used to make the dreaded font toggle problem hopefully go
96 // away. Definitely not the best solution, but I think it sorta works.
97 bool toggleall = true;
98
99 /* 
100    This is the inset locking stuff needed for mathed --------------------
101
102    an inset can simple call LockInset in it's edit call and *ONLY* in it's
103    edit call.
104    Inset::Edit() can only be called by the main lyx module.
105
106    Then the inset may modify the menu's and/or iconbars. 
107
108    Unlocking is either done by LyX or the inset itself with a UnlockInset-call
109
110    During the lock, all button and keyboard events will be modified
111    and send to the inset through the following inset-features. Note that
112    Inset::InsetUnlock will be called from inside UnlockInset. It is meant
113    to contain the code for restoring the menus and things like this.
114
115    
116    virtual void InsetButtonPress(int x, int y, int button);
117    virtual void InsetButtonRelease(int x, int y, int button);
118    virtual void InsetKeyPress(XKeyEvent *ev);
119    virtual void InsetMotionNotify(int x, int y, int state);
120    virtual void InsetUnlock();
121
122    If a inset wishes any redraw and/or update it just has to call
123    UpdateInset(this).
124    It's is completly irrelevant, where the inset is. UpdateInset will
125    find it in any paragraph in any buffer. 
126    Of course the_locking_inset and the insets in the current paragraph/buffer
127    are checked first, so no performance problem should occur.
128    
129    Hope that's ok for the beginning, Alejandro,
130    sorry that I needed so much time,
131
132                   Matthias
133    */
134
135 //void UpdateInset(BufferView * bv, Inset * inset, bool mark_dirty = true);
136
137 /* these functions return 1 if an error occured, 
138    otherwise 0 */
139 // Now they work only for updatable insets. [Alejandro 080596]
140 //int LockInset(UpdatableInset * inset);
141 void ToggleLockedInsetCursor(long x, long y, int asc, int desc);
142 //void FitLockedInsetCursor(long x, long y, int asc, int desc);
143 //int UnlockInset(UpdatableInset * inset);
144 //void LockedInsetStoreUndo(Undo::undo_kind kind);
145
146 /* this is for asyncron updating. UpdateInsetUpdateList will be called
147    automatically from LyX. Just insert the Inset into the Updatelist */
148 //void UpdateInsetUpdateList();
149 //void PutInsetIntoInsetUpdateList(Inset * inset);
150
151 //InsetUpdateStruct * InsetUpdateList = 0;
152
153
154 /*
155   -----------------------------------------------------------------------
156  */
157
158 /* some function prototypes */
159
160 int RunLinuxDoc(BufferView *, int, string const &);
161 int RunDocBook(int, string const &);
162 void MenuWrite(Buffer * buf);
163 bool MenuWriteAs(Buffer * buffer);
164 void MenuReload(Buffer * buf);
165 void MenuLayoutSave();
166
167
168 void ShowMessage(Buffer * buf,
169                  string const & msg1,
170                  string const & msg2 = string(),
171                  string const & msg3 = string(), int delay = 6)
172 {
173         if (lyxrc.use_gui) {
174                 buf->getUser()->owner()->getMiniBuffer()->Set(msg1, msg2,
175                                                               msg3, delay);
176         } else {
177                 // can somebody think of something more clever? cerr?
178                 cout << msg1 << msg2 << msg3 << endl;
179         }
180 }
181
182
183 //
184 // Menu callbacks
185 //
186
187 //
188 // File menu
189 //
190
191 // should be moved to lyxfunc.C
192 void MenuWrite(Buffer * buffer)
193 {
194         XFlush(fl_display);
195         if (!buffer->save()) {
196                 string fname = buffer->fileName();
197                 string s = MakeAbsPath(fname);
198                 if (AskQuestion(_("Save failed. Rename and try again?"),
199                                 MakeDisplayPath(s, 50),
200                                 _("(If not, document is not saved.)"))) {
201                         MenuWriteAs(buffer);
202                 }
203         } else {
204                 lastfiles->newFile(buffer->fileName());
205         }
206 }
207
208
209 // should be moved to BufferView.C
210 // Half of this func should be in LyXView, the rest in BufferView.
211 bool MenuWriteAs(Buffer * buffer)
212 {
213         // Why do we require BufferView::text to be able to write a
214         // document? I see no point in that. (Lgb)
215         //if (!bv->text) return;
216
217         string fname = buffer->fileName();
218         string oldname = fname;
219         LyXFileDlg fileDlg;
220
221         ProhibitInput(current_view);
222         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
223         fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
224
225         if (!IsLyXFilename(fname))
226                 fname += ".lyx";
227
228         if (buffer->isUnnamed()) {
229                 fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
230                                        "",
231                                        "*.lyx", 
232                                        "");
233         } else {
234                 fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
235                                        OnlyPath(fname),
236                                        "*.lyx", 
237                                        OnlyFilename(fname));
238         }
239         AllowInput(current_view);
240
241         if (fname.empty()) {
242                 return false;
243         }
244         // Make sure the absolute filename ends with appropriate suffix
245         string s = MakeAbsPath(fname);
246         if (!IsLyXFilename(s))
247                 s += ".lyx";
248
249         // Same name as we have already?
250         if (s == oldname) {
251                 if (!AskQuestion(_("Same name as document already has:"),
252                                  MakeDisplayPath(s, 50),
253                                  _("Save anyway?")))
254                         return false;
255                 // Falls through to name change and save
256         } 
257         // No, but do we have another file with this name open?
258         else if (bufferlist.exists(s)) {
259                 if (AskQuestion(_("Another document with same name open!"),
260                                 MakeDisplayPath(s, 50),
261                                 _("Replace with current document?")))
262                         {
263                                 bufferlist.close(bufferlist.getBuffer(s));
264
265                                 // Ok, change the name of the buffer, but don't save!
266                                 buffer->fileName(s);
267                                 buffer->markDirty();
268
269                                 ShowMessage(buffer, _("Document renamed to '"),
270                                                 MakeDisplayPath(s), _("', but not saved..."));
271                         }
272                 return false;
273         } // Check whether the file exists
274         else {
275                 FileInfo myfile(s);
276                 if (myfile.isOK() && !AskQuestion(_("Document already exists:"), 
277                                                   MakeDisplayPath(s, 50),
278                                                   _("Replace file?")))
279                         return false;
280         }
281
282         // Ok, change the name of the buffer
283         buffer->fileName(s);
284         buffer->markDirty();
285         buffer->setUnnamed(false);
286         // And save
287         // Small bug: If the save fails, we have irreversible changed the name
288         // of the document.
289         MenuWrite(buffer);
290         return true;
291 }    
292
293
294 int MenuRunLaTeX(Buffer * buffer)
295 {
296         int ret = 0;
297
298         if (buffer->isLinuxDoc())
299                 ret = RunLinuxDoc(buffer->getUser(), 1, buffer->fileName());
300         else if (buffer->isLiterate())
301                 ret = buffer->runLiterate();
302         else if (buffer->isDocBook())
303                 ret = RunDocBook(1, buffer->fileName());
304         else
305                 ret = buffer->runLaTeX();
306    
307         if (ret > 0) {
308                 string s;
309                 string t;
310                 if (ret == 1) {
311                         s = _("One error detected");
312                         t = _("You should try to fix it.");
313                 } else {
314                         s += tostr(ret);
315                         s += _(" errors detected.");
316                         t = _("You should try to fix them.");
317                 }
318                 WriteAlert(_("There were errors during the LaTeX run."), s, t);
319         }
320         return ret;
321 }
322
323
324 int MenuBuildProg(Buffer * buffer)
325 {
326         int ret = 0;
327         
328         if (buffer->isLiterate())
329                 ret = buffer->buildProgram();
330         else {
331                 string s = _("Wrong type of document");
332                 string t = _("The Build operation is not allowed in this document");
333                 WriteAlert(_("There were errors during the Build process."), s, t);
334                 return 1;
335         }
336         
337         if (ret > 0) {
338                 string s;
339                 string t;
340                 if (ret == 1) {
341                         s = _("One error detected");
342                         t = _("You should try to fix it.");
343                 } else {
344                         s += tostr(ret);
345                         s += _(" errors detected.");
346                         t = _("You should try to fix them.");
347                 }
348                 WriteAlert(_("There were errors during the Build process."), s, t);
349         }
350         return ret;
351 }
352
353
354 int MenuRunChktex(Buffer * buffer)
355 {
356         int ret;
357
358         if (buffer->isSGML()) {
359                 WriteAlert(_("Chktex does not work with SGML derived documents."));
360                 return 0;
361         } else 
362                 ret = buffer->runChktex();
363    
364         if (ret >= 0) {
365                 string s;
366                 string t;
367                 if (ret == 0) {
368                         s = _("No warnings found.");
369                 } else if (ret == 1) {
370                         s = _("One warning found.");
371                         t = _("Use 'Edit->Go to Error' to find it.");
372                 } else {
373                         s += tostr(ret);
374                         s += _(" warnings found.");
375                         t = _("Use 'Edit->Go to Error' to find them.");
376                 }
377                 WriteAlert(_("Chktex run successfully"), s, t);
378         } else {
379                 WriteAlert(_("Error!"), _("It seems chktex does not work."));
380         }
381         return ret;
382 }
383
384  
385 int MakeLaTeXOutput(Buffer * buffer)
386 {
387         // Who cares?
388         //if (!bv->text)
389         //      return 1;
390         int ret = 0;
391         string path = OnlyPath(buffer->fileName());
392         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
393                 path = buffer->tmppath;
394         }
395                 
396     Path p(path);
397         ret = MenuRunLaTeX(buffer);
398         
399     return ret;
400 }
401
402
403 /* wait == false means don't wait for termination */
404 /* wait == true means wait for termination       */
405 // The bool should be placed last on the argument line. (Lgb)
406 // Returns false if we fail.
407 bool RunScript(Buffer * buffer, bool wait,
408                string const & command,
409                string const & orgname = string(),
410                bool need_shell = true)
411 {
412         string path;
413         string cmd;
414         string name = orgname;
415         int result = 0;
416         
417         if (MakeLaTeXOutput(buffer) > 0)
418                 return false;
419         /* get DVI-Filename */
420         if (name.empty())
421                 name = ChangeExtension(buffer->getLatexName(), ".dvi");
422
423         path = OnlyPath(name);
424         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
425                 path = buffer->tmppath;
426         }
427         Path p(path);
428         // At this point we check whether the command contains the
429         // filename parameter $$FName and if that's the case we
430         // substitute the real file name otherwise the filename is
431         // simply appended. rokrau 1/12/00
432         cmd = command;
433         string::size_type i;
434         if ( (i=command.find("$$FName")) != string::npos)
435         {
436                 cmd.replace(i,7,QuoteName(name));
437         }
438         else
439                 cmd = command + ' ' + QuoteName(name);
440
441         Systemcalls one;
442
443         if (need_shell) {
444 #ifndef __EMX__
445                 if (!wait)
446                         cmd += " &";
447 #else
448                 // OS/2 cmd.exe has another use for '&'
449                 if (!wait) {
450                         // This is not NLS safe, but it's OK, I think.
451                         string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
452                         if (sh.empty()) {
453                                 // COMSPEC is set, unless user unsets 
454                                 sh = OnlyFilename(GetEnvPath("COMSPEC"));
455                                 if (sh.empty())
456                                         sh = "cmd.exe";
457                         }
458                         sh = lowercase(sh);
459                         if (contains(sh, "cmd.exe")
460                             || contains(sh, "4os2.exe"))
461                                 cmd = "start /min/n " + cmd;
462                         else
463                                 cmd += " &";
464                 }
465 #endif
466                 // It seems that, if wait is false, we never get back
467                 // the return code of the command. This means that all
468                 // the code I added in PrintApplyCB is currently
469                 // useless...
470                 // CHECK What should we do here?
471                 ShowMessage(buffer, _("Executing command:"), cmd);
472                 result = one.startscript(Systemcalls::System, cmd);
473         } else {
474                 ShowMessage(buffer, _("Executing command:"), cmd);
475                 result = one.startscript(wait ? Systemcalls::Wait
476                                          : Systemcalls::DontWait, cmd);
477         }
478         return result == 0;
479 }
480
481
482 // Returns false if we fail
483 bool CreatePostscript(Buffer * buffer, bool wait = false)
484 {
485         // Who cares?
486         //if (!bv->text)
487         //      return false;
488
489         ProhibitInput(current_view);
490
491         // Generate dvi file
492         if (MakeLaTeXOutput(buffer) > 0) {
493                 AllowInput(current_view);
494                 return false;
495         }
496         // Generate postscript file
497         string psname = OnlyFilename(ChangeExtension (buffer->fileName(),
498                                          ".ps_tmp"));
499
500         string paper;
501
502         // Wrong type
503         char real_papersize = buffer->params.papersize;
504         if (real_papersize == BufferParams::PAPER_DEFAULT)
505                 real_papersize = lyxrc.default_papersize;
506
507         switch (real_papersize) {
508         case BufferParams::PAPER_USLETTER:
509                 paper = "letter";
510                 break;
511         case BufferParams::PAPER_A3PAPER:
512                 paper = "a3";
513                 break;
514         case BufferParams::PAPER_A4PAPER:
515                 paper = "a4";
516                 break;
517         case BufferParams::PAPER_A5PAPER:
518                 paper = "a5";
519                 break;
520         case BufferParams::PAPER_B5PAPER:
521                 paper = "b5";
522                 break;
523         case BufferParams::PAPER_EXECUTIVEPAPER:
524                 paper = "foolscap";
525                 break;
526         case BufferParams::PAPER_LEGALPAPER:
527                 paper = "legal";
528                 break;
529         default: /* If nothing else fits, keep an empty value... */
530                 break;
531         }
532
533         // Make postscript file.
534         string command = lyxrc.dvi_to_ps_command + ' ' + lyxrc.print_to_file + ' ';
535         command += QuoteName(psname);
536         if (buffer->params.use_geometry
537             && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
538             && !lyxrc.print_paper_dimension_flag.empty()
539             && !buffer->params.paperwidth.empty()
540             && !buffer->params.paperheight.empty()) {
541                 // using a custom papersize
542                 command += ' ';
543                 command += lyxrc.print_paper_dimension_flag + ' ';
544                 command += buffer->params.paperwidth + ',';
545                 command += buffer->params.paperheight;
546         } else if (!paper.empty()
547                    && (real_papersize != BufferParams::PAPER_USLETTER ||
548                        buffer->params.orientation == BufferParams::ORIENTATION_PORTRAIT)) {
549                 // dvips won't accept -t letter -t landscape.  In all other
550                 // cases, include the paper size explicitly.
551                 command += ' ';
552                 command += lyxrc.print_paper_flag + ' ' + paper;
553         }
554         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE) {
555                 command += ' ';
556                 command += lyxrc.print_landscape_flag;
557         }
558         // push directorypath, if necessary 
559         string path = OnlyPath(buffer->fileName());
560         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)){
561                 path = buffer->tmppath;
562         }
563         Path p(path);
564         bool ret = RunScript(buffer, wait, command);
565         AllowInput(current_view);
566         return ret;
567 }
568
569
570 // Returns false if we fail
571 //bool MenuPreviewPS(Buffer * buffer)
572 bool PreviewPostscript(Buffer * buffer)
573 {
574         // Who cares?
575         //if (!bv->text)
576         //      return false;
577
578         // Generate postscript file
579         if (!CreatePostscript(buffer, true)) {
580                 return false;
581         }
582
583         // Start postscript viewer
584         ProhibitInput(current_view);
585         string ps = OnlyFilename(ChangeExtension (buffer->fileName(),
586                                      ".ps_tmp"));
587         // push directorypath, if necessary 
588         string path = OnlyPath(buffer->fileName());
589         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)){
590                 path = buffer->tmppath;
591         }
592         Path p(path);
593         bool ret = RunScript(buffer, false, lyxrc.view_ps_command, ps);
594         AllowInput(current_view);
595         return ret;
596 }
597
598
599 void MenuFax(Buffer * buffer)
600 {
601         // Who cares?
602         //if (!bv->text)
603         //      return;
604
605         // Generate postscript file
606         if (!CreatePostscript(buffer, true)) {
607                 return;
608         }
609
610         // Send fax
611         string ps = OnlyFilename(ChangeExtension (buffer->fileName(), 
612                                                   ".ps_tmp"));
613         string path = OnlyPath (buffer->fileName());
614         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
615                 path = buffer->tmppath;
616         }
617         Path p(path);
618         if (!lyxrc.fax_program.empty()) {
619                 string help2 = subst(lyxrc.fax_program, "$$FName", ps);
620                 help2 += " &";
621                 Systemcalls one(Systemcalls::System, help2);
622         } else
623                 send_fax(ps, lyxrc.fax_command);
624 }
625
626
627 // Returns false if we fail
628 bool PreviewDVI(Buffer * buffer)
629 {
630         // Who cares?
631         //if (!bv->text)
632         //      return false;
633
634         string paper = lyxrc.view_dvi_paper_option;
635         if (!paper.empty()) {
636                 // wrong type
637                 char real_papersize = buffer->params.papersize;
638                 if (real_papersize == BufferParams::PAPER_DEFAULT)
639                         real_papersize = lyxrc.default_papersize;
640   
641                 switch (real_papersize) {
642                 case BufferParams::PAPER_USLETTER:
643                         paper += " us";
644                         break;
645                 case BufferParams::PAPER_A3PAPER:
646                         paper += " a3";
647                         break;
648                 case BufferParams::PAPER_A4PAPER:
649                         paper += " a4";
650                         break;
651                 case BufferParams::PAPER_A5PAPER:
652                         paper += " a5";
653                         break;
654                 case BufferParams::PAPER_B5PAPER:
655                         paper += " b5";
656                         break;
657                 case BufferParams::PAPER_EXECUTIVEPAPER:
658                         paper += " foolscap";
659                         break;
660                 case BufferParams::PAPER_LEGALPAPER:
661                         paper += " legal";
662                         break;
663                 default: /* If nothing else fits, keep the empty value */
664                         break;
665                 }
666                 if (real_papersize==' ') {
667                         //      if (paper.empty()) {
668                         if (buffer->params.orientation 
669                             == BufferParams::ORIENTATION_LANDSCAPE)
670                           // we HAVE to give a size when the page is in
671                           // landscape, so use USletter.          
672                                 paper = " -paper usr";
673                 } else {
674                         // paper = " -paper " + paper;
675                         if (buffer->params.orientation 
676                             == BufferParams::ORIENTATION_LANDSCAPE)
677                                paper+= 'r';
678                 }
679         }
680         // push directorypath, if necessary 
681         string path = OnlyPath(buffer->fileName());
682         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
683                 path = buffer->tmppath;
684         }
685         Path p(path);
686         // Run dvi-viewer
687         string command = lyxrc.view_dvi_command + " " + paper;
688         bool ret = RunScript(buffer, false, command);
689         return ret;
690 }
691
692
693 bool AskOverwrite(Buffer * buffer, string const & s)
694 {
695         if (lyxrc.use_gui) {
696                 // be friendly if there is a gui
697                 FileInfo fi(s);
698                 if (fi.readable() &&
699                                 !AskQuestion(_("File already exists:"), 
700                                  MakeDisplayPath(s, 50),
701                                  _("Do you want to overwrite the file?"))) {
702                         ShowMessage(buffer, _("Canceled"));
703                         return false;
704                 }
705         }
706         return true;
707 }
708
709
710 void MenuMakeLaTeX(Buffer * buffer)
711 {
712         // Why care about this?
713         //if (!bv->text)
714         //      return;
715         
716         // Get LaTeX-Filename
717         string s = buffer->getLatexName(false);
718         
719         if (!AskOverwrite(buffer, s))
720                 return; 
721         
722         if (buffer->isDocBook())
723                 ShowMessage(buffer, _("DocBook does not have a latex backend"));
724         else {
725                 if (buffer->isLinuxDoc())
726                         RunLinuxDoc(buffer->getUser(), 0, buffer->fileName());
727                 else
728                         buffer->makeLaTeXFile(s, string(), true);
729                 ShowMessage(buffer, _("Nice LaTeX file saved as"), 
730                             MakeDisplayPath(s));
731         }
732 }
733
734
735 void MenuMakeLinuxDoc(Buffer * buffer)
736 {
737         // Who cares?
738         //if (!bv->text) return;
739         
740         if (!buffer->isLinuxDoc()) {
741                 WriteAlert(_("Error!"), _("Document class must be linuxdoc."));
742                 return;
743         }
744         
745         // Get LinuxDoc-Filename
746         string s = ChangeExtension(buffer->fileName(), ".sgml");
747
748         if (!AskOverwrite(buffer, s))
749                 return;
750         
751         ShowMessage(buffer, _("Building LinuxDoc SGML file `"),
752                                           MakeDisplayPath(s),"'...");
753         
754         buffer->makeLinuxDocFile(s, true);
755         buffer->redraw();
756         ShowMessage(buffer, _("LinuxDoc SGML file save as"),
757                                           MakeDisplayPath(s)); 
758 }
759
760
761 void MenuMakeDocBook(Buffer * buffer)
762 {
763         // Who cares?
764         //if (!bv->text) return;
765         
766         if (!buffer->isDocBook()) {
767                 WriteAlert(_("Error!"),
768                            _("Document class must be docbook."));
769                 return;
770         }
771         
772         // Get DocBook-Filename
773         string s = ChangeExtension(buffer->fileName(), ".sgml");
774
775         if (!AskOverwrite(buffer, s))
776                 return;
777         
778         ShowMessage(buffer, _("Building DocBook SGML file `"),
779                                           MakeDisplayPath(s), "'..."); 
780         
781         buffer->makeDocBookFile(s, true);
782         buffer->redraw();
783         ShowMessage(buffer, _("DocBook SGML file save as"),
784                                           MakeDisplayPath(s)); 
785 }
786
787
788 void MenuMakeAscii(Buffer * buffer)
789 {
790         // Who cares?
791         //if (!bv->text) return;
792         
793         /* get LaTeX-Filename */
794         string s = ChangeExtension (buffer->fileName(), ".txt");
795         
796
797         if (!AskOverwrite(buffer, s))
798                 return;
799         
800         buffer->writeFileAscii(s, lyxrc.ascii_linelen);
801         
802         ShowMessage(buffer, _("Ascii file saved as"), MakeDisplayPath(s));
803 }
804
805
806 void MenuMakeHTML(Buffer * buffer)
807 {
808         // First, create LaTeX file
809         MenuMakeLaTeX(buffer);
810
811         // And now, run the converter
812         string file = buffer->fileName();
813         Path path(OnlyPath(file));
814         // the tex file name has to be correct for
815         // latex, but the html file name can be
816         // anything.
817         string result = ChangeExtension(file, ".html");
818         string infile = buffer->getLatexName(false);
819         string tmp = lyxrc.html_command;
820         tmp = subst(tmp, "$$FName", infile);
821         tmp = subst(tmp, "$$OutName", result);
822         Systemcalls one;
823         int res = one.startscript(Systemcalls::System, tmp);
824         if (res == 0) {
825                 ShowMessage(buffer, _("Document exported as HTML to file `")
826                                                   + MakeDisplayPath(result) +'\'');
827         } else {
828                 ShowMessage(buffer, _("Unable to convert to HTML the file `")
829                                                   + MakeDisplayPath(infile) 
830                                                   + '\'');
831         }
832
833 }
834
835
836 void MenuMakeHTML_LinuxDoc(Buffer * buffer)
837 {
838         // First, create LinuxDoc file
839         MenuMakeLinuxDoc(buffer);
840
841         // And now, run the converter
842         string file = buffer->fileName();
843
844         string result = ChangeExtension(file, ".html");
845         string infile = ChangeExtension(file, ".sgml");
846         string tmp = lyxrc.linuxdoc_to_html_command;
847         tmp = subst(tmp, "$$FName", infile);
848         tmp = subst(tmp, "$$OutName", result);
849         Systemcalls one;
850         int res = one.startscript(Systemcalls::System, tmp);
851         if (res == 0) {
852                 ShowMessage(buffer,_("Document exported as HTML to file `")
853                                                   + MakeDisplayPath(result) +'\'');
854         } else {
855                 ShowMessage(buffer,_("Unable to convert to HTML the file `")
856                                                   + MakeDisplayPath(infile) 
857                                                   + '\'');
858         }
859
860 }
861
862 void MenuMakeHTML_DocBook(Buffer * buffer)
863 {
864         // First, create LaTeX file
865         MenuMakeDocBook(buffer);
866
867         // And now, run the converter
868         string file = buffer->fileName();
869         string result = ChangeExtension(file, ".html");
870         string infile = ChangeExtension(file, ".sgml");
871         string tmp = lyxrc.docbook_to_html_command;
872         tmp = subst(tmp, "$$FName", infile);
873         tmp = subst(tmp, "$$OutName", result);
874         Systemcalls one;
875         int res = one.startscript(Systemcalls::System, tmp);
876         if (res == 0) {
877                 ShowMessage(buffer,_("Document exported as HTML to file `")
878                                                   + MakeDisplayPath(result) +'\'');
879         } else {
880                 ShowMessage(buffer,_("Unable to convert to HTML the file `")
881                                                   + MakeDisplayPath(infile) 
882                                                   + '\'');
883         }
884
885 }
886
887
888 void MenuExport(Buffer * buffer, string const & extyp) 
889 {
890         // latex
891         if (extyp == "latex") {
892                 // make sure that this buffer is not linuxdoc
893                 MenuMakeLaTeX(buffer);
894         }
895         // linuxdoc
896         else if (extyp == "linuxdoc") {
897                 // make sure that this buffer is not latex
898                 MenuMakeLinuxDoc(buffer);
899         }
900         // docbook
901         else if (extyp == "docbook") {
902                 // make sure that this buffer is not latex or linuxdoc
903                 MenuMakeDocBook(buffer);
904         }
905         // dvi
906         else if (extyp == "dvi") {
907                 // Run LaTeX as "Update dvi..." Bernhard.
908                 // We want the dvi in the current directory. This
909                 // is achieved by temporarily disabling use of
910                 // temp directory. As a side-effect, we get
911                 // *.log and *.aux files also. (Asger)
912                 bool flag = lyxrc.use_tempdir;
913                 lyxrc.use_tempdir = false;
914                 MenuRunLaTeX(buffer);
915                 lyxrc.use_tempdir = flag;
916         }
917         // postscript
918         else if (extyp == "postscript") {
919                 // Start Print-dialog. Not as good as dvi... Bernhard.
920         //should start lyxview->getDialogs()->showPrint();
921         // to get same as before
922         //              MenuPrint(buffer);
923                 // Since the MenuPrint is a pop-up, we can't use
924                 // the same trick as above. (Asger)
925                 // MISSING: Move of ps-file :-(
926                 // And MenuPrint should not be used for this at all...
927         }
928         // ascii
929         else if (extyp == "ascii") {
930                 MenuMakeAscii(buffer);
931         }
932         else if (extyp == "custom") {
933                 MenuSendto();
934         }
935         // HTML
936         else if (extyp == "html") {
937                 if (buffer->isLinuxDoc())
938                         MenuMakeHTML_LinuxDoc(buffer);
939                 else if (buffer->isDocBook())
940                         MenuMakeHTML_DocBook(buffer);
941                 else
942                         MenuMakeHTML(buffer);
943         }
944         else {
945                 ShowMessage(buffer, _("Unknown export type: ") + extyp);
946         }
947 }
948
949
950 void QuitLyX()
951 {
952         lyxerr.debug() << "Running QuitLyX." << endl;
953
954         if (!bufferlist.QwriteAll())
955                 return;
956
957         lastfiles->writeFile(lyxrc.lastfiles);
958
959         // Set a flag that we do quitting from the program,
960         // so no refreshes are necessary.
961         quitting = true;
962
963         // close buffers first
964         bufferlist.closeAll();
965
966         // do any other cleanup procedures now
967         lyxerr.debug() << "Deleting tmp dir " << system_tempdir << endl;
968
969         DestroyLyXTmpDir(system_tempdir);
970
971         finished = true;
972 }
973
974
975
976 void AutoSave(BufferView * bv)
977         // should probably be moved into BufferList (Lgb)
978         // Perfect target for a thread...
979 {
980         if (!bv->available())
981                 return;
982
983         if (bv->buffer()->isBakClean() ||
984             bv->buffer()->isReadonly() || bv->buffer()->isUnnamed()) {
985                 // We don't save now, but we'll try again later
986                 bv->owner()->resetAutosaveTimer();
987                 return;
988         }
989
990         bv->owner()->getMiniBuffer()->Set(_("Autosaving current document..."));
991         
992         // create autosave filename
993         string fname =  OnlyPath(bv->buffer()->fileName());
994         fname += "#";
995         fname += OnlyFilename(bv->buffer()->fileName());
996         fname += "#";
997         
998         // tmp_ret will be located (usually) in /tmp
999         // will that be a problem?
1000         string tmp_ret = tmpnam(0);
1001         
1002         pid_t pid = fork(); // If you want to debug the autosave
1003         // you should set pid to -1, and comment out the
1004         // fork.
1005         if (pid == 0 || pid == -1) {
1006                 // pid = -1 signifies that lyx was unable
1007                 // to fork. But we will do the save
1008                 // anyway.
1009                 bool failed = false;
1010                 if (!tmp_ret.empty()) {
1011                         bv->buffer()->writeFile(tmp_ret, 1);
1012                         // assume successful write of tmp_ret
1013                         if (rename(tmp_ret.c_str(), fname.c_str()) == -1) {
1014                                 failed = true;
1015                                 // most likely couldn't move between filesystems
1016                                 // unless write of tmp_ret failed
1017                                 // so remove tmp file (if it exists)
1018                                 remove(tmp_ret.c_str());
1019                         }
1020                 } else {
1021                         failed = true;
1022                 }
1023                 
1024                 if (failed) {
1025                         // failed to write/rename tmp_ret so try writing direct
1026                         if (!bv->buffer()->writeFile(fname, 1)) {
1027                                 // It is dangerous to do this in the child,
1028                                 // but safe in the parent, so...
1029                                 if (pid == -1)
1030                                         bv->owner()->getMiniBuffer()->Set(_("Autosave Failed!"));
1031                         }
1032                 }
1033                 if (pid == 0) { // we are the child so...
1034                         _exit(0);
1035                 }
1036         }
1037         
1038         bv->buffer()->markBakClean();
1039         bv->owner()->resetAutosaveTimer();
1040 }
1041
1042
1043 //
1044 // Copyright CHT Software Service GmbH
1045 // Uwe C. Schroeder
1046 //
1047 // create new file with template
1048 // SERVERCMD !
1049 //
1050 Buffer * NewLyxFile(string const & filename)
1051 {
1052         // Split argument by :
1053         string name;
1054         string tmpname = split(filename, name, ':');
1055 #ifdef __EMX__ // Fix me! lyx_cb.C may not be low level enough to allow this.
1056         if (name.length() == 1
1057             && isalpha(static_cast<unsigned char>(name[0]))
1058             && (prefixIs(tmpname, "/") || prefixIs(tmpname, "\\"))) {
1059                 name += ':';
1060                 name += token(tmpname, ':', 0);
1061                 tmpname = split(tmpname, ':');
1062         }
1063 #endif
1064         lyxerr.debug() << "Arg is " << filename
1065                        << "\nName is " << name
1066                        << "\nTemplate is " << tmpname << endl;
1067
1068         // find a free buffer 
1069         Buffer * tmpbuf = bufferlist.newFile(name, tmpname);
1070         if (tmpbuf)
1071                 lastfiles->newFile(tmpbuf->fileName());
1072         return tmpbuf;
1073 }
1074
1075
1076 // Insert ascii file (if filename is empty, prompt for one)
1077 void InsertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
1078 {
1079         string fname = f;
1080         LyXFileDlg fileDlg;
1081  
1082         if (!bv->available()) return;
1083      
1084         if (fname.empty()) {
1085                 ProhibitInput(bv);
1086                 fname = fileDlg.Select(_("File to Insert"), 
1087                                        bv->owner()->buffer()->filepath,
1088                                        "*");
1089                 AllowInput(bv);
1090                 if (fname.empty()) return;
1091         }
1092
1093         FileInfo fi(fname);
1094
1095         if (!fi.readable()) {
1096                 WriteFSAlert(_("Error! Specified file is unreadable: "),
1097                              MakeDisplayPath(fname, 50));
1098                 return;
1099         }
1100
1101         ifstream ifs(fname.c_str());
1102         if (!ifs) {
1103                 WriteFSAlert(_("Error! Cannot open specified file: "),
1104                              MakeDisplayPath(fname, 50));
1105                 return;
1106         }
1107
1108         ifs.unsetf(ios::skipws);
1109         istream_iterator<char> ii(ifs);
1110         istream_iterator<char> end;
1111 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
1112         // We use this until the compilers get better...
1113         vector<char> tmp;
1114         copy(ii, end, back_inserter(tmp));
1115         string tmpstr(tmp.begin(), tmp.end());
1116 #else
1117         // This is what we want to use and what we will use once the
1118         // compilers get good enough. 
1119         //string tmpstr(ii, end); // yet a reason for using std::string
1120         // alternate approach to get the file into a string:
1121         string tmpstr;
1122         copy(ii, end, back_inserter(tmpstr));
1123 #endif
1124         // insert the string
1125         current_view->hideCursor();
1126         
1127         // clear the selection
1128         bv->beforeChange();
1129         if (!asParagraph)
1130                 bv->text->InsertStringA(bv, tmpstr);
1131         else
1132                 bv->text->InsertStringB(bv, tmpstr);
1133         bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1134 }
1135
1136
1137 void MenuShowTableOfContents()
1138 {
1139         static int ow = -1, oh;
1140
1141         TocUpdateCB(0, 0);
1142         if (fd_form_toc->form_toc->visible) {
1143                 fl_raise_form(fd_form_toc->form_toc);
1144         } else {
1145                 fl_show_form(fd_form_toc->form_toc,
1146                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
1147                              _("Table Of Contents"));
1148                 if (ow < 0) {
1149                         ow = fd_form_toc->form_toc->w;
1150                         oh = fd_form_toc->form_toc->h;
1151                 }
1152                 fl_set_form_minsize(fd_form_toc->form_toc, ow, oh);
1153         }
1154 }
1155
1156
1157 void MenuInsertLabel(char const * arg)
1158 {
1159         string label = arg;
1160         ProhibitInput(current_view);
1161         if (label.empty()) {
1162                 pair<bool, string>
1163                         result = askForText(_("Enter new label to insert:"));
1164                 if (result.first) {
1165                         label = frontStrip(strip(result.second));
1166                 }
1167         }
1168         if (!label.empty()) {
1169                 InsetLabel * new_inset = new InsetLabel;
1170                 new_inset->setContents(label);
1171                 current_view->insertInset(new_inset);
1172         }
1173         AllowInput(current_view);
1174 }
1175
1176
1177 void MenuInsertRef()
1178 {
1179         static int ow = -1, oh;
1180
1181         RefUpdateCB(0, 0);
1182         if (fd_form_ref->form_ref->visible) {
1183                 fl_raise_form(fd_form_ref->form_ref);
1184         } else {
1185                 fl_show_form(fd_form_ref->form_ref,
1186                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
1187                              _("Insert Reference"));
1188                 if (ow < 0) {
1189                         ow = fd_form_ref->form_ref->w;
1190                         oh = fd_form_ref->form_ref->h;
1191                 }
1192                 fl_set_form_minsize(fd_form_ref->form_ref, ow, oh);
1193         }
1194 }
1195
1196
1197 // candidate for move to LyXView
1198 // This is only used in toolbar.C
1199 void LayoutsCB(int sel, void *)
1200 {
1201         string tmp = tostr(sel);
1202         current_view->owner()->getLyXFunc()->Dispatch(LFUN_LAYOUTNO,
1203                                                       tmp.c_str());
1204 }
1205
1206
1207 /*
1208  * SGML Linuxdoc support:
1209  * (flag == 0) make TeX output
1210  * (flag == 1) make dvi output
1211  */
1212 int RunLinuxDoc(BufferView * bv, int flag, string const & filename)
1213 {
1214         string s2;
1215         string add_flags;
1216
1217         int errorcode = 0;
1218
1219         /* generate a path-less extension name */
1220         string name = OnlyFilename(ChangeExtension (filename, ".sgml"));
1221         string path = OnlyPath (filename);
1222         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
1223                 path = bv->buffer()->tmppath;
1224         }
1225         Path p(path);
1226         
1227         if (!bv->available())
1228                 return 0;
1229         bv->buffer()->makeLinuxDocFile(name, false);
1230
1231         // CHECK remove this once we have a proper geometry class
1232
1233         BufferParams::PAPER_SIZE ps =
1234                 static_cast<BufferParams::PAPER_SIZE>(bv->buffer()->params.papersize);
1235         switch (ps) {
1236         case BufferParams::PAPER_A4PAPER:
1237                 add_flags = "-p a4";
1238                 break;
1239         case BufferParams::PAPER_USLETTER:
1240                 add_flags = "-p letter";
1241                 break;
1242         default: /* nothing to be done yet ;-) */     break; 
1243         }
1244         
1245         ProhibitInput(bv);
1246         
1247         Systemcalls one;
1248         switch (flag) {
1249         case 0: /* TeX output asked */
1250               bv->owner()->getMiniBuffer()->Set(_("Converting LinuxDoc SGML to TeX file..."));
1251                 s2 = lyxrc.linuxdoc_to_latex_command + ' ' + add_flags + " -o tex " + ' ' + name;
1252                 if (one.startscript(Systemcalls::System, s2)) 
1253                         errorcode = 1;
1254                 break;
1255         case 1: /* dvi output asked */
1256                 bv->owner()->getMiniBuffer()->Set(_("Converting LinuxDoc SGML to dvi file..."));
1257                 s2 = lyxrc.linuxdoc_to_latex_command + ' ' + add_flags + " -o dvi " + ' ' + name;
1258                 if (one.startscript(Systemcalls::System, s2)) {
1259                         errorcode = 1;
1260                 } 
1261                 break;
1262         default: /* unknown output */
1263                 break;
1264         }
1265         
1266         AllowInput(bv);
1267
1268         bv->buffer()->redraw();
1269         return errorcode;
1270 }
1271
1272
1273 /*
1274  * SGML DocBook support:
1275  * (flag == 1) make dvi output
1276  */
1277 int RunDocBook(int flag, string const & filename)
1278 {
1279         /* generate a path-less extension name */
1280         string name = OnlyFilename(ChangeExtension (filename, ".sgml"));
1281         string path = OnlyPath (filename);
1282         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
1283                 path = current_view->buffer()->tmppath;
1284         }
1285         Path p(path);
1286
1287         if (!current_view->available())
1288                 return 0;
1289         
1290         current_view->buffer()->makeDocBookFile(name, false);
1291
1292         // Shall this code go or should it stay? (Lgb)
1293         // This code is a placeholder for future implementation. (Jose')
1294 //      string add_flags;
1295 //      LYX_PAPER_SIZE ps = (LYX_PAPER_SIZE) current_view->buffer()->params.papersize;
1296 //      switch (ps) {
1297 //      case BufferParams::PAPER_A4PAPER:  add_flags = "-p a4";     break;
1298 //      case BufferParams::PAPER_USLETTER: add_flags = "-p letter"; break;
1299 //      default: /* nothing to be done yet ;-) */     break; 
1300 //      }
1301         ProhibitInput(current_view);
1302         
1303         int errorcode = 0;
1304         Systemcalls one;
1305         switch (flag) {
1306         case 1: /* dvi output asked */
1307         {
1308                 current_view->owner()->getMiniBuffer()->Set(_("Converting DocBook SGML to dvi file..."));
1309                 string s2 = lyxrc.docbook_to_dvi_command + ' ' + name;
1310                 if (one.startscript(Systemcalls::System, s2)) {
1311                         errorcode = 1;
1312                 }
1313         }
1314         break;
1315         default: /* unknown output */
1316                 break;
1317         }
1318         
1319         AllowInput(current_view);
1320
1321         current_view->buffer()->redraw();
1322         return errorcode;
1323 }
1324
1325
1326 void MenuLayoutCharacter()
1327 {
1328         static int ow = -1, oh;
1329
1330         if (fd_form_character->form_character->visible) {
1331                 fl_raise_form(fd_form_character->form_character);
1332         } else {
1333                 fl_show_form(fd_form_character->form_character,
1334                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
1335                              _("Character Style"));
1336                 if (ow < 0) {
1337                         ow = fd_form_character->form_character->w;
1338                         oh = fd_form_character->form_character->h;
1339                 }
1340                 fl_set_form_minsize(fd_form_character->form_character, ow, oh);
1341         }
1342 }
1343
1344
1345 inline
1346 void DeactivateParagraphButtons ()
1347 {
1348         fl_deactivate_object (fd_form_paragraph->button_ok);
1349         fl_deactivate_object (fd_form_paragraph->button_apply);
1350         fl_set_object_lcol (fd_form_paragraph->button_ok, FL_INACTIVE);
1351         fl_set_object_lcol (fd_form_paragraph->button_apply, FL_INACTIVE);
1352 }
1353
1354
1355 inline
1356 void ActivateParagraphButtons ()
1357 {
1358         fl_activate_object (fd_form_paragraph->button_ok);
1359         fl_activate_object (fd_form_paragraph->button_apply);
1360         fl_set_object_lcol (fd_form_paragraph->button_ok, FL_BLACK);
1361         fl_set_object_lcol (fd_form_paragraph->button_apply, FL_BLACK);
1362 }
1363
1364
1365 inline
1366 void DisableParagraphLayout ()
1367 {
1368         DeactivateParagraphButtons();
1369         fl_deactivate_object (fd_form_paragraph->input_labelwidth);
1370         fl_deactivate_object (fd_form_paragraph->check_lines_top);
1371         fl_deactivate_object (fd_form_paragraph->check_lines_bottom);
1372         fl_deactivate_object (fd_form_paragraph->check_pagebreaks_top);
1373         fl_deactivate_object (fd_form_paragraph->check_pagebreaks_bottom);
1374         fl_deactivate_object (fd_form_paragraph->check_noindent);
1375         fl_deactivate_object (fd_form_paragraph->group_radio_alignment);
1376         fl_deactivate_object (fd_form_paragraph->radio_align_right);
1377         fl_deactivate_object (fd_form_paragraph->radio_align_left);
1378         fl_deactivate_object (fd_form_paragraph->radio_align_block);
1379         fl_deactivate_object (fd_form_paragraph->radio_align_center);
1380         fl_deactivate_object (fd_form_paragraph->input_space_above);
1381         fl_deactivate_object (fd_form_paragraph->input_space_below);
1382         fl_deactivate_object (fd_form_paragraph->choice_space_above);
1383         fl_deactivate_object (fd_form_paragraph->choice_space_below);
1384         fl_deactivate_object (fd_form_paragraph->check_space_above);
1385         fl_deactivate_object (fd_form_paragraph->check_space_below);
1386 }
1387
1388
1389 inline
1390 void EnableParagraphLayout ()
1391 {
1392         ActivateParagraphButtons();
1393         fl_activate_object (fd_form_paragraph->input_labelwidth);
1394         fl_activate_object (fd_form_paragraph->check_lines_top);
1395         fl_activate_object (fd_form_paragraph->check_lines_bottom);
1396         fl_activate_object (fd_form_paragraph->check_pagebreaks_top);
1397         fl_activate_object (fd_form_paragraph->check_pagebreaks_bottom);
1398         fl_activate_object (fd_form_paragraph->check_noindent);
1399         fl_activate_object (fd_form_paragraph->group_radio_alignment);
1400         fl_activate_object (fd_form_paragraph->radio_align_right);
1401         fl_activate_object (fd_form_paragraph->radio_align_left);
1402         fl_activate_object (fd_form_paragraph->radio_align_block);
1403         fl_activate_object (fd_form_paragraph->radio_align_center);
1404         fl_activate_object (fd_form_paragraph->input_space_above);
1405         fl_activate_object (fd_form_paragraph->input_space_below);
1406         fl_activate_object (fd_form_paragraph->choice_space_above);
1407         fl_activate_object (fd_form_paragraph->choice_space_below);
1408         fl_activate_object (fd_form_paragraph->check_space_above);
1409         fl_activate_object (fd_form_paragraph->check_space_below);
1410 }
1411
1412
1413 bool UpdateLayoutParagraph()
1414 {
1415         if (!current_view->available()) {
1416                 if (fd_form_paragraph->form_paragraph->visible) 
1417                         fl_hide_form(fd_form_paragraph->form_paragraph);
1418                 return false;
1419         }
1420
1421         Buffer * buf = current_view->buffer();
1422         LyXText * text = 0;
1423         if (current_view->the_locking_inset)
1424             text = current_view->the_locking_inset->getLyXText(current_view);
1425         if (!text)
1426             text = current_view->text;
1427
1428         fl_set_input(fd_form_paragraph->input_labelwidth,
1429                      text->cursor.par()->GetLabelWidthString().c_str());
1430         fl_set_button(fd_form_paragraph->radio_align_right, 0);
1431         fl_set_button(fd_form_paragraph->radio_align_left, 0);
1432         fl_set_button(fd_form_paragraph->radio_align_center, 0);
1433         fl_set_button(fd_form_paragraph->radio_align_block, 0);
1434
1435         int align = text->cursor.par()->GetAlign();
1436         if (align == LYX_ALIGN_LAYOUT)
1437                 align = textclasslist.Style(buf->params.textclass,
1438                                             text->cursor.par()->GetLayout()).align;
1439          
1440         switch (align) {
1441         case LYX_ALIGN_RIGHT:
1442                 fl_set_button(fd_form_paragraph->radio_align_right, 1);
1443                 break;
1444         case LYX_ALIGN_LEFT:
1445                 fl_set_button(fd_form_paragraph->radio_align_left, 1);
1446                 break;
1447         case LYX_ALIGN_CENTER:
1448                 fl_set_button(fd_form_paragraph->radio_align_center, 1);
1449                 break;
1450         default:
1451                 fl_set_button(fd_form_paragraph->radio_align_block, 1);
1452                 break;
1453         }
1454
1455 #ifndef NEW_INSETS
1456         fl_set_button(fd_form_paragraph->check_lines_top,
1457                       text->cursor.par()->FirstPhysicalPar()->line_top);
1458
1459         fl_set_button(fd_form_paragraph->check_lines_bottom,
1460                       text->cursor.par()->FirstPhysicalPar()->line_bottom);
1461
1462         fl_set_button(fd_form_paragraph->check_pagebreaks_top,
1463                       text->cursor.par()->FirstPhysicalPar()->pagebreak_top);
1464
1465         fl_set_button(fd_form_paragraph->check_pagebreaks_bottom,
1466                       text->cursor.par()->FirstPhysicalPar()->pagebreak_bottom);
1467         fl_set_button(fd_form_paragraph->check_noindent,
1468                       text->cursor.par()->FirstPhysicalPar()->noindent);
1469 #else
1470         fl_set_button(fd_form_paragraph->check_lines_top,
1471                       text->cursor.par()->line_top);
1472         fl_set_button(fd_form_paragraph->check_lines_bottom,
1473                       text->cursor.par()->line_bottom);
1474         fl_set_button(fd_form_paragraph->check_pagebreaks_top,
1475                       text->cursor.par()->pagebreak_top);
1476         fl_set_button(fd_form_paragraph->check_pagebreaks_bottom,
1477                       text->cursor.par()->pagebreak_bottom);
1478         fl_set_button(fd_form_paragraph->check_noindent,
1479                       text->cursor.par()->noindent);
1480 #endif
1481         fl_set_input (fd_form_paragraph->input_space_above, "");
1482
1483 #ifndef NEW_INSETS
1484         switch (text->cursor.par()->FirstPhysicalPar()->added_space_top.kind()) {
1485 #else
1486         switch (text->cursor.par()->added_space_top.kind()) {
1487 #endif
1488
1489         case VSpace::NONE:
1490                 fl_set_choice (fd_form_paragraph->choice_space_above, 1);
1491                 break;
1492         case VSpace::DEFSKIP:
1493                 fl_set_choice (fd_form_paragraph->choice_space_above, 2);
1494                 break;
1495         case VSpace::SMALLSKIP:
1496                 fl_set_choice (fd_form_paragraph->choice_space_above, 3);
1497                 break;
1498         case VSpace::MEDSKIP:
1499                 fl_set_choice (fd_form_paragraph->choice_space_above, 4);
1500                 break;
1501         case VSpace::BIGSKIP:
1502                 fl_set_choice (fd_form_paragraph->choice_space_above, 5);
1503                 break;
1504         case VSpace::VFILL:
1505                 fl_set_choice (fd_form_paragraph->choice_space_above, 6);
1506                 break;
1507         case VSpace::LENGTH:
1508                 fl_set_choice (fd_form_paragraph->choice_space_above, 7);
1509 #ifndef NEW_INSETS
1510                 fl_set_input  (fd_form_paragraph->input_space_above, 
1511                                text->cursor.par()->FirstPhysicalPar()->added_space_top.length().asString().c_str());
1512 #else
1513                 fl_set_input  (fd_form_paragraph->input_space_above, 
1514                                text->cursor.par()->added_space_top.length().asString().c_str());
1515 #endif
1516                 break;
1517         }
1518 #ifndef NEW_INSETS
1519         fl_set_button (fd_form_paragraph->check_space_above,
1520                        text->cursor.par()->FirstPhysicalPar()->added_space_top.keep());
1521         fl_set_input (fd_form_paragraph->input_space_below, "");
1522         switch (text->cursor.par()->FirstPhysicalPar()->added_space_bottom.kind()) {
1523 #else
1524         fl_set_button (fd_form_paragraph->check_space_above,
1525                        text->cursor.par()->added_space_top.keep());
1526         fl_set_input (fd_form_paragraph->input_space_below, "");
1527         switch (text->cursor.par()->added_space_bottom.kind()) {
1528 #endif
1529         case VSpace::NONE:
1530                 fl_set_choice (fd_form_paragraph->choice_space_below,
1531                                1);
1532                 break;
1533         case VSpace::DEFSKIP:
1534                 fl_set_choice (fd_form_paragraph->choice_space_below,
1535                                2);
1536                 break;
1537         case VSpace::SMALLSKIP:
1538                 fl_set_choice (fd_form_paragraph->choice_space_below,
1539                                3);
1540                 break;
1541         case VSpace::MEDSKIP:
1542                 fl_set_choice (fd_form_paragraph->choice_space_below,
1543                                4);
1544                 break;
1545         case VSpace::BIGSKIP:
1546                 fl_set_choice (fd_form_paragraph->choice_space_below,
1547                                5);
1548                 break;
1549         case VSpace::VFILL:
1550                 fl_set_choice (fd_form_paragraph->choice_space_below,
1551                                6);
1552                 break;
1553         case VSpace::LENGTH:
1554                 fl_set_choice (fd_form_paragraph->choice_space_below,
1555                                7);
1556 #ifndef NEW_INSETS
1557                 fl_set_input  (fd_form_paragraph->input_space_below, 
1558                                text->cursor.par()->FirstPhysicalPar()->added_space_bottom.length().asString().c_str());
1559                 break;
1560         }
1561         fl_set_button (fd_form_paragraph->check_space_below,
1562                        text->cursor.par()->FirstPhysicalPar()->added_space_bottom.keep());
1563
1564         fl_set_button(fd_form_paragraph->check_noindent,
1565                       text->cursor.par()->FirstPhysicalPar()->noindent);
1566 #else
1567                 fl_set_input  (fd_form_paragraph->input_space_below, 
1568                                text->cursor.par()->added_space_bottom.length().asString().c_str());
1569                 break;
1570         }
1571         fl_set_button (fd_form_paragraph->check_space_below,
1572                        text->cursor.par()->added_space_bottom.keep());
1573
1574         fl_set_button(fd_form_paragraph->check_noindent,
1575                       text->cursor.par()->noindent);
1576 #endif
1577         if (current_view->buffer()->isReadonly()) {
1578                 DisableParagraphLayout();
1579         } else {
1580                 EnableParagraphLayout();
1581         }
1582         return true;
1583 }
1584
1585
1586 void MenuLayoutParagraph()
1587 {
1588         if (UpdateLayoutParagraph()) {
1589                 if (fd_form_paragraph->form_paragraph->visible) {
1590                         fl_raise_form(fd_form_paragraph->form_paragraph);
1591                 } else {
1592                         fl_show_form(fd_form_paragraph->form_paragraph,
1593                                      FL_PLACE_MOUSE, FL_FULLBORDER,
1594                                      _("Paragraph Environment"));
1595                 }
1596         }
1597 }
1598
1599
1600 inline
1601 void DeactivateDocumentButtons ()
1602 {
1603         fl_deactivate_object (fd_form_document->button_ok);
1604         fl_deactivate_object (fd_form_document->button_apply);
1605         fl_set_object_lcol (fd_form_document->button_ok, FL_INACTIVE);
1606         fl_set_object_lcol (fd_form_document->button_apply, FL_INACTIVE);
1607 }
1608
1609
1610 inline
1611 void ActivateDocumentButtons ()
1612 {
1613         fl_activate_object (fd_form_document->button_ok);
1614         fl_activate_object (fd_form_document->button_apply);
1615         fl_set_object_lcol (fd_form_document->button_ok, FL_BLACK);
1616         fl_set_object_lcol (fd_form_document->button_apply, FL_BLACK);
1617 }
1618
1619
1620 inline
1621 void DisableDocumentLayout ()
1622 {
1623         DeactivateDocumentButtons ();
1624         fl_deactivate_object (fd_form_document->group_radio_separation);
1625         fl_deactivate_object (fd_form_document->radio_indent);
1626         fl_deactivate_object (fd_form_document->radio_skip);
1627         fl_deactivate_object (fd_form_document->choice_class);
1628         fl_deactivate_object (fd_form_document->choice_pagestyle);
1629         fl_deactivate_object (fd_form_document->choice_fonts);
1630         fl_deactivate_object (fd_form_document->choice_fontsize);
1631         fl_deactivate_object (fd_form_document->input_float_placement);
1632         fl_deactivate_object (fd_form_document->choice_postscript_driver);
1633         fl_deactivate_object (fd_form_document->choice_inputenc);
1634         fl_deactivate_object (fd_form_document->group_radio_sides);
1635         fl_deactivate_object (fd_form_document->radio_sides_one);
1636         fl_deactivate_object (fd_form_document->radio_sides_two);
1637         fl_deactivate_object (fd_form_document->group_radio_columns);
1638         fl_deactivate_object (fd_form_document->radio_columns_one);
1639         fl_deactivate_object (fd_form_document->radio_columns_two);
1640         fl_deactivate_object (fd_form_document->input_extra);
1641         fl_deactivate_object (fd_form_document->choice_language);
1642         combo_language->deactivate();
1643         fl_deactivate_object (fd_form_document->input_default_skip);
1644         fl_deactivate_object (fd_form_document->choice_default_skip);
1645         fl_deactivate_object (fd_form_document->slider_secnumdepth);
1646         fl_deactivate_object (fd_form_document->slider_tocdepth);
1647         fl_deactivate_object (fd_form_document->choice_spacing);
1648         fl_deactivate_object (fd_form_document->input_spacing);
1649         fl_deactivate_object (fd_form_document->check_use_amsmath);
1650 }
1651
1652
1653 inline
1654 void EnableDocumentLayout ()
1655 {
1656         ActivateDocumentButtons ();
1657         fl_activate_object (fd_form_document->group_radio_separation);
1658         fl_activate_object (fd_form_document->radio_indent);
1659         fl_activate_object (fd_form_document->radio_skip);
1660         fl_activate_object (fd_form_document->choice_class);
1661         fl_activate_object (fd_form_document->choice_pagestyle);
1662         fl_activate_object (fd_form_document->choice_fonts);
1663         fl_activate_object (fd_form_document->choice_fontsize);
1664         fl_activate_object (fd_form_document->input_float_placement);
1665         fl_activate_object (fd_form_document->choice_postscript_driver);
1666         fl_activate_object (fd_form_document->choice_inputenc);
1667         fl_activate_object (fd_form_document->group_radio_sides);
1668         fl_activate_object (fd_form_document->radio_sides_one);
1669         fl_activate_object (fd_form_document->radio_sides_two);
1670         fl_activate_object (fd_form_document->group_radio_columns);
1671         fl_activate_object (fd_form_document->radio_columns_one);
1672         fl_activate_object (fd_form_document->radio_columns_two);
1673         fl_activate_object (fd_form_document->input_extra);
1674         fl_activate_object (fd_form_document->choice_language);
1675         combo_language->activate();
1676         fl_activate_object (fd_form_document->input_default_skip);
1677         fl_activate_object (fd_form_document->choice_default_skip);
1678         fl_activate_object (fd_form_document->slider_secnumdepth);
1679         fl_activate_object (fd_form_document->slider_tocdepth);
1680         fl_activate_object (fd_form_document->choice_spacing);
1681         fl_activate_object (fd_form_document->input_spacing);
1682         fl_activate_object (fd_form_document->check_use_amsmath);
1683 }
1684
1685
1686 bool UpdateLayoutDocument(BufferParams * params)
1687 {
1688         if (!current_view->available()) {
1689                 if (fd_form_document->form_document->visible) 
1690                         fl_hide_form(fd_form_document->form_document);
1691                 return false;
1692         }               
1693
1694         if (params == 0)
1695                 params = &current_view->buffer()->params;
1696         LyXTextClass const & tclass = textclasslist.TextClass(params->textclass);
1697         
1698         fl_set_choice_text(fd_form_document->choice_class, 
1699                            textclasslist.DescOfClass(params->textclass).c_str());
1700         combo_language->select_text(params->language.c_str());
1701         
1702         fl_set_choice_text(fd_form_document->choice_fonts, 
1703                            params->fonts.c_str());
1704         fl_set_choice_text(fd_form_document->choice_inputenc, 
1705                            params->inputenc.c_str());
1706         fl_set_choice_text(fd_form_document->choice_postscript_driver, 
1707                            params->graphicsDriver.c_str());
1708
1709         // ale970405+lasgoutt970513
1710         fl_clear_choice(fd_form_document->choice_fontsize);
1711         fl_addto_choice(fd_form_document->choice_fontsize, "default");
1712         fl_addto_choice(fd_form_document->choice_fontsize, 
1713                         tclass.opt_fontsize().c_str());
1714         fl_set_choice(fd_form_document->choice_fontsize, 
1715                       tokenPos(tclass.opt_fontsize(), '|', params->fontsize) + 2);
1716
1717         // ale970405+lasgoutt970513
1718         fl_clear_choice(fd_form_document->choice_pagestyle);
1719         fl_addto_choice(fd_form_document->choice_pagestyle, "default");
1720         fl_addto_choice(fd_form_document->choice_pagestyle, 
1721                         tclass.opt_pagestyle().c_str());
1722     
1723         fl_set_choice(fd_form_document->choice_pagestyle,
1724                       tokenPos(tclass.opt_pagestyle(), '|', params->pagestyle) + 2);
1725
1726         fl_set_button(fd_form_document->radio_indent, 0);
1727         fl_set_button(fd_form_document->radio_skip, 0);
1728     
1729         
1730         fl_set_button(fd_form_document->check_use_amsmath, params->use_amsmath);
1731
1732         if (params->paragraph_separation == BufferParams::PARSEP_INDENT)
1733                 fl_set_button(fd_form_document->radio_indent, 1);
1734         else
1735                 fl_set_button(fd_form_document->radio_skip, 1);
1736
1737         switch (params->getDefSkip().kind()) {
1738         case VSpace::SMALLSKIP: 
1739                 fl_set_choice (fd_form_document->choice_default_skip, 1);
1740                 break;
1741         case VSpace::MEDSKIP: 
1742                 fl_set_choice (fd_form_document->choice_default_skip, 2);
1743                 break;
1744         case VSpace::BIGSKIP: 
1745                 fl_set_choice (fd_form_document->choice_default_skip, 3);
1746                 break;
1747         case VSpace::LENGTH: 
1748                 fl_set_choice (fd_form_document->choice_default_skip, 4);
1749                 fl_set_input (fd_form_document->input_default_skip,
1750                               params->getDefSkip().asLyXCommand().c_str());
1751                 break;
1752         default:
1753                 fl_set_choice (fd_form_document->choice_default_skip, 2);
1754                 break;
1755         }
1756    
1757         fl_set_button(fd_form_document->radio_sides_one, 0);
1758         fl_set_button(fd_form_document->radio_sides_two, 0);
1759    
1760         switch (params->sides) {
1761         case LyXTextClass::OneSide:
1762                 fl_set_button(fd_form_document->radio_sides_one, 1);
1763                 break;
1764         case LyXTextClass::TwoSides:
1765                 fl_set_button(fd_form_document->radio_sides_two, 1);
1766                 break;
1767         }
1768    
1769         fl_set_button(fd_form_document->radio_columns_one, 0);
1770         fl_set_button(fd_form_document->radio_columns_two, 0);
1771    
1772         if (params->columns == 2)
1773                 fl_set_button(fd_form_document->radio_columns_two, 1);
1774         else
1775                 fl_set_button(fd_form_document->radio_columns_one, 1);
1776    
1777         fl_set_input(fd_form_document->input_spacing, "");
1778         switch (params->spacing.getSpace()) {
1779         case Spacing::Default: // nothing bad should happen with this
1780         case Spacing::Single:
1781         {
1782                 // \singlespacing
1783                 fl_set_choice(fd_form_document->choice_spacing, 1);
1784                 break;
1785         }
1786         case Spacing::Onehalf:
1787         {
1788                 // \onehalfspacing
1789                 fl_set_choice(fd_form_document->choice_spacing, 2);
1790                 break;
1791         }
1792         case Spacing::Double:
1793         {
1794                 // \ doublespacing
1795                 fl_set_choice(fd_form_document->choice_spacing, 3);
1796                 break;
1797         }
1798         case Spacing::Other:
1799         {
1800                 fl_set_choice(fd_form_document->choice_spacing, 4);
1801                 //char sval[20];
1802                 //sprintf(sval, "%g", params->spacing.getValue()); 
1803 #ifdef HAVE_SSTREAM
1804                 std::ostringstream sval;
1805                 sval << params->spacing.getValue(); // setw?
1806                 fl_set_input(fd_form_document->input_spacing,
1807                              sval.str().c_str());
1808 #else
1809                 char tval[20];
1810                 ostrstream sval(tval, 20);
1811                 sval << params->spacing.getValue() << '\0'; // setw?
1812                 fl_set_input(fd_form_document->input_spacing, sval.str());
1813 #endif
1814                 break;
1815         }
1816         }
1817
1818
1819         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
1820                              params->secnumdepth);
1821         fl_set_counter_value(fd_form_document->slider_tocdepth, 
1822                              params->tocdepth);
1823         if (!params->float_placement.empty()) { // buffer local (Lgb)
1824                 fl_set_input(fd_form_document->input_float_placement,
1825                              params->float_placement.c_str());
1826         } else {
1827                 fl_set_input(fd_form_document->input_float_placement, "");
1828         }
1829         if (!params->options.empty())
1830                 fl_set_input(fd_form_document->input_extra,
1831                              params->options.c_str());
1832         else
1833                 fl_set_input(fd_form_document->input_extra, "");
1834
1835         if (current_view->buffer()->isSGML()) {
1836                 // bullets not used in SGML derived documents
1837                 fl_deactivate_object(fd_form_document->button_bullets);
1838                 fl_set_object_lcol(fd_form_document->button_bullets,
1839                                    FL_INACTIVE);
1840         } else {
1841                 fl_activate_object(fd_form_document->button_bullets);
1842                 fl_set_object_lcol(fd_form_document->button_bullets,
1843                                    FL_BLACK);
1844         }
1845
1846         if (current_view->buffer()->isReadonly()) {
1847                 DisableDocumentLayout();
1848         } else {
1849                 EnableDocumentLayout();
1850         }
1851
1852         return true;
1853 }
1854
1855
1856 void MenuLayoutDocument()
1857 {
1858         if (UpdateLayoutDocument()) {
1859                 if (fd_form_document->form_document->visible) {
1860                         fl_raise_form(fd_form_document->form_document);
1861                 } else {
1862                         fl_show_form(fd_form_document->form_document,
1863                                      FL_PLACE_MOUSE, FL_FULLBORDER,
1864                                      _("Document Layout"));
1865                 }
1866         }
1867 }
1868
1869
1870 bool UpdateLayoutQuotes()
1871 {
1872         bool update = true;
1873         if (!current_view->available()
1874             || current_view->buffer()->isReadonly())
1875                 update = false;
1876         
1877         if (update) {
1878                 fl_set_choice(fd_form_quotes->choice_quotes_language,
1879                               current_view->buffer()->params.quotes_language + 1);
1880                 fl_set_button(fd_form_quotes->radio_single, 0);
1881                 fl_set_button(fd_form_quotes->radio_double, 0);
1882         
1883                 if (current_view->buffer()->params.quotes_times == InsetQuotes::SingleQ)
1884                         fl_set_button(fd_form_quotes->radio_single, 1);
1885                 else
1886                         fl_set_button(fd_form_quotes->radio_double, 1);
1887         } else if (fd_form_quotes->form_quotes->visible) {
1888                 fl_hide_form(fd_form_quotes->form_quotes);
1889         }
1890         return update;
1891 }
1892
1893
1894 void MenuLayoutQuotes()
1895 {
1896         if (UpdateLayoutQuotes()) {
1897                 if (fd_form_quotes->form_quotes->visible) {
1898                         fl_raise_form(fd_form_quotes->form_quotes);
1899                 } else {
1900                         fl_show_form(fd_form_quotes->form_quotes,
1901                                      FL_PLACE_MOUSE, FL_FULLBORDER,
1902                                      _("Quotes"));
1903                 }
1904         }
1905 }
1906
1907
1908 bool UpdateLayoutPreamble()
1909 {
1910         bool update = true;
1911         if (!current_view->available())
1912                 update = false;
1913
1914         if (update) {
1915                 fl_set_input(fd_form_preamble->input_preamble,
1916                              current_view->buffer()->params.preamble.c_str());
1917
1918                 if (current_view->buffer()->isReadonly()) {
1919                         fl_deactivate_object(fd_form_preamble->input_preamble);
1920                         fl_deactivate_object(fd_form_preamble->button_ok);
1921                         fl_deactivate_object(fd_form_preamble->button_apply);
1922                         fl_set_object_lcol(fd_form_preamble->button_ok, FL_INACTIVE);
1923                         fl_set_object_lcol(fd_form_preamble->button_apply, FL_INACTIVE);
1924                 }
1925                 else {
1926                         fl_activate_object(fd_form_preamble->input_preamble);
1927                         fl_activate_object(fd_form_preamble->button_ok);
1928                         fl_activate_object(fd_form_preamble->button_apply);
1929                         fl_set_object_lcol(fd_form_preamble->button_ok, FL_BLACK);
1930                         fl_set_object_lcol(fd_form_preamble->button_apply, FL_BLACK);
1931                 }
1932         } else if (fd_form_preamble->form_preamble->visible) {
1933                 fl_hide_form(fd_form_preamble->form_preamble);
1934         }
1935         return update;
1936 }
1937
1938
1939 void MenuLayoutPreamble()
1940 {
1941         static int ow = -1, oh;
1942
1943         if (UpdateLayoutPreamble()) {
1944                 if (fd_form_preamble->form_preamble->visible) {
1945                         fl_raise_form(fd_form_preamble->form_preamble);
1946                 } else {
1947                         fl_show_form(fd_form_preamble->form_preamble,
1948                                      FL_PLACE_MOUSE | FL_FREE_SIZE,
1949                                      FL_FULLBORDER,
1950                                      _("LaTeX Preamble"));
1951                         if (ow < 0) {
1952                                 ow = fd_form_preamble->form_preamble->w;
1953                                 oh = fd_form_preamble->form_preamble->h;
1954                         }
1955                         fl_set_form_minsize(fd_form_preamble->form_preamble,
1956                                             ow, oh);
1957                 }
1958         }
1959 }
1960
1961
1962 void MenuLayoutSave()
1963 {
1964         if (!current_view->available())
1965                 return;
1966
1967         if (AskQuestion(_("Do you want to save the current settings"),
1968                         _("for Character, Document, Paper and Quotes"),
1969                         _("as default for new documents?")))
1970                 current_view->buffer()->saveParamsAsDefaults();
1971 }
1972
1973
1974 // This is both GUI and LyXFont dependent. Don't know where to put it. (Asger)
1975 // Well, it's mostly GUI dependent, so I guess it will stay here. (Asger)
1976 LyXFont UserFreeFont(BufferParams const & params)
1977 {
1978         LyXFont font(LyXFont::ALL_IGNORE);
1979
1980         int pos = fl_get_choice(fd_form_character->choice_family);
1981         switch(pos) {
1982         case 1: font.setFamily(LyXFont::IGNORE_FAMILY); break;
1983         case 2: font.setFamily(LyXFont::ROMAN_FAMILY); break;
1984         case 3: font.setFamily(LyXFont::SANS_FAMILY); break;
1985         case 4: font.setFamily(LyXFont::TYPEWRITER_FAMILY); break;
1986         case 5: font.setFamily(LyXFont::INHERIT_FAMILY); break;
1987         }
1988
1989         pos = fl_get_choice(fd_form_character->choice_series);
1990         switch(pos) {
1991         case 1: font.setSeries(LyXFont::IGNORE_SERIES); break;
1992         case 2: font.setSeries(LyXFont::MEDIUM_SERIES); break;
1993         case 3: font.setSeries(LyXFont::BOLD_SERIES); break;
1994         case 4: font.setSeries(LyXFont::INHERIT_SERIES); break;
1995         }
1996
1997         pos = fl_get_choice(fd_form_character->choice_shape);
1998         switch(pos) {
1999         case 1: font.setShape(LyXFont::IGNORE_SHAPE); break;
2000         case 2: font.setShape(LyXFont::UP_SHAPE); break;
2001         case 3: font.setShape(LyXFont::ITALIC_SHAPE); break;
2002         case 4: font.setShape(LyXFont::SLANTED_SHAPE); break;
2003         case 5: font.setShape(LyXFont::SMALLCAPS_SHAPE); break;
2004         case 6: font.setShape(LyXFont::INHERIT_SHAPE); break;
2005         }
2006
2007         pos = fl_get_choice(fd_form_character->choice_size);
2008         switch(pos) {
2009         case 1: font.setSize(LyXFont::IGNORE_SIZE); break;
2010         case 2: font.setSize(LyXFont::SIZE_TINY); break;
2011         case 3: font.setSize(LyXFont::SIZE_SCRIPT); break;
2012         case 4: font.setSize(LyXFont::SIZE_FOOTNOTE); break;
2013         case 5: font.setSize(LyXFont::SIZE_SMALL); break;
2014         case 6: font.setSize(LyXFont::SIZE_NORMAL); break;
2015         case 7: font.setSize(LyXFont::SIZE_LARGE); break;
2016         case 8: font.setSize(LyXFont::SIZE_LARGER); break;
2017         case 9: font.setSize(LyXFont::SIZE_LARGEST); break;
2018         case 10: font.setSize(LyXFont::SIZE_HUGE); break;
2019         case 11: font.setSize(LyXFont::SIZE_HUGER); break;
2020         case 12: font.setSize(LyXFont::INCREASE_SIZE); break;
2021         case 13: font.setSize(LyXFont::DECREASE_SIZE); break;
2022         case 14: font.setSize(LyXFont::INHERIT_SIZE); break;
2023         }
2024
2025         pos = fl_get_choice(fd_form_character->choice_bar);
2026         switch(pos) {
2027         case 1: font.setEmph(LyXFont::IGNORE);
2028                 font.setUnderbar(LyXFont::IGNORE);
2029                 font.setNoun(LyXFont::IGNORE);
2030                 font.setLatex(LyXFont::IGNORE);
2031                 break;
2032         case 2: font.setEmph(LyXFont::TOGGLE); break;
2033         case 3: font.setUnderbar(LyXFont::TOGGLE); break;
2034         case 4: font.setNoun(LyXFont::TOGGLE); break;
2035         case 5: font.setLatex(LyXFont::TOGGLE); break;
2036         case 6: font.setEmph(LyXFont::INHERIT);
2037                 font.setUnderbar(LyXFont::INHERIT);
2038                 font.setNoun(LyXFont::INHERIT);
2039                 font.setLatex(LyXFont::INHERIT);
2040                 break;
2041         }
2042
2043         pos = fl_get_choice(fd_form_character->choice_color);
2044         switch(pos) {
2045         case 1: font.setColor(LColor::ignore); break;
2046         case 2: font.setColor(LColor::none); break;
2047         case 3: font.setColor(LColor::black); break;
2048         case 4: font.setColor(LColor::white); break;
2049         case 5: font.setColor(LColor::red); break;
2050         case 6: font.setColor(LColor::green); break;
2051         case 7: font.setColor(LColor::blue); break;
2052         case 8: font.setColor(LColor::cyan); break;
2053         case 9: font.setColor(LColor::magenta); break;
2054         case 10: font.setColor(LColor::yellow); break;
2055         case 11: font.setColor(LColor::inherit); break;
2056         }
2057
2058         int choice = combo_language2->get();
2059         if (choice == 1)
2060                 font.setLanguage(ignore_language);
2061         else if (choice == 2)
2062                 font.setLanguage(params.language_info);
2063         else
2064                 font.setLanguage(&languages[combo_language2->getline()]);
2065
2066         return font; 
2067 }
2068
2069
2070 /* callbacks for form form_title */
2071 extern "C" void TimerCB(FL_OBJECT *, long)
2072 {
2073         // only if the form still exists
2074         if (lyxrc.show_banner && fd_form_title->form_title != 0) {
2075                 if (fd_form_title->form_title->visible) {
2076                         fl_hide_form(fd_form_title->form_title);
2077                 }
2078                 fl_free_form(fd_form_title->form_title);
2079                 fd_form_title->form_title = 0;
2080         }
2081 }
2082
2083
2084 /* callbacks for form form_paragraph */
2085
2086 extern "C" void ParagraphVSpaceCB(FL_OBJECT * obj, long )
2087 {
2088         // "Synchronize" the choices and input fields, making it
2089         // impossible to commit senseless data.
2090
2091         FD_form_paragraph const * fp = fd_form_paragraph;
2092
2093         if (obj == fp->choice_space_above) {
2094                 if (fl_get_choice (fp->choice_space_above) != 7) {
2095                         fl_set_input (fp->input_space_above, "");
2096                         ActivateParagraphButtons();
2097                 }
2098         } else if (obj == fp->choice_space_below) {
2099                 if (fl_get_choice (fp->choice_space_below) != 7) {
2100                         fl_set_input (fp->input_space_below, "");
2101                         ActivateParagraphButtons();
2102                 }
2103         } else if (obj == fp->input_space_above) {
2104                 string input = fl_get_input (fp->input_space_above);
2105
2106                 if (input.empty()) {
2107                         fl_set_choice (fp->choice_space_above, 1);
2108                         ActivateParagraphButtons();
2109                 }
2110                 else if (isValidGlueLength (input)) {
2111                         fl_set_choice (fp->choice_space_above, 7);
2112                         ActivateParagraphButtons();
2113                 }
2114                 else {
2115                         fl_set_choice (fp->choice_space_above, 7);
2116                         DeactivateParagraphButtons();
2117                 }
2118         } else if (obj == fp->input_space_below) {
2119                 string input = fl_get_input (fp->input_space_below);
2120
2121                 if (input.empty()) {
2122                         fl_set_choice (fp->choice_space_below, 1);
2123                         ActivateParagraphButtons();
2124                 }
2125                 else if (isValidGlueLength (input)) {
2126                         fl_set_choice (fp->choice_space_below, 7);
2127                         ActivateParagraphButtons();
2128                 }
2129                 else {
2130                         fl_set_choice (fp->choice_space_below, 7);
2131                         DeactivateParagraphButtons();
2132                 }
2133         }
2134 }
2135
2136
2137 extern "C" void ParagraphApplyCB(FL_OBJECT *, long)
2138 {
2139         if (!current_view->available())
2140                 return;
2141         
2142         VSpace space_top, space_bottom;
2143         LyXAlignment align;
2144         string labelwidthstring;
2145         bool noindent;
2146
2147         // If a vspace kind is "Length" but there's no text in
2148         // the input field, reset the kind to "None". 
2149         if (fl_get_choice (fd_form_paragraph->choice_space_above) == 7
2150             && !*(fl_get_input (fd_form_paragraph->input_space_above))) {
2151                 fl_set_choice (fd_form_paragraph->choice_space_above, 1);
2152         }
2153         if (fl_get_choice (fd_form_paragraph->choice_space_below) == 7
2154             && !*(fl_get_input (fd_form_paragraph->input_space_below))) {
2155                 fl_set_choice (fd_form_paragraph->choice_space_below, 1);
2156         }
2157    
2158         bool line_top = fl_get_button(fd_form_paragraph->check_lines_top);
2159         bool line_bottom = fl_get_button(fd_form_paragraph->check_lines_bottom);
2160         bool pagebreak_top = fl_get_button(fd_form_paragraph->check_pagebreaks_top);
2161         bool pagebreak_bottom = fl_get_button(fd_form_paragraph->check_pagebreaks_bottom);
2162         switch (fl_get_choice (fd_form_paragraph->choice_space_above)) {
2163         case 1: space_top = VSpace(VSpace::NONE); break;
2164         case 2: space_top = VSpace(VSpace::DEFSKIP); break;
2165         case 3: space_top = VSpace(VSpace::SMALLSKIP); break;
2166         case 4: space_top = VSpace(VSpace::MEDSKIP); break;
2167         case 5: space_top = VSpace(VSpace::BIGSKIP); break;
2168         case 6: space_top = VSpace(VSpace::VFILL); break;
2169         case 7: space_top = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_above))); break;
2170         }
2171         if (fl_get_button (fd_form_paragraph->check_space_above))
2172                 space_top.setKeep (true);
2173         switch (fl_get_choice (fd_form_paragraph->choice_space_below)) {
2174         case 1: space_bottom = VSpace(VSpace::NONE); break;
2175         case 2: space_bottom = VSpace(VSpace::DEFSKIP); break;
2176         case 3: space_bottom = VSpace(VSpace::SMALLSKIP); break;
2177         case 4: space_bottom = VSpace(VSpace::MEDSKIP); break;
2178         case 5: space_bottom = VSpace(VSpace::BIGSKIP); break;
2179         case 6: space_bottom = VSpace(VSpace::VFILL); break;
2180         case 7: space_bottom = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_below))); break;
2181         }
2182         if (fl_get_button (fd_form_paragraph->check_space_below))
2183                 space_bottom.setKeep (true);
2184
2185         if (fl_get_button(fd_form_paragraph->radio_align_left))
2186                 align = LYX_ALIGN_LEFT;
2187         else if (fl_get_button(fd_form_paragraph->radio_align_right))
2188                 align = LYX_ALIGN_RIGHT;
2189         else if (fl_get_button(fd_form_paragraph->radio_align_center))
2190                 align = LYX_ALIGN_CENTER;
2191         else 
2192                 align = LYX_ALIGN_BLOCK;
2193    
2194         labelwidthstring = fl_get_input(fd_form_paragraph->input_labelwidth);
2195         noindent = fl_get_button(fd_form_paragraph->check_noindent);
2196
2197         LyXText * text = 0;
2198         if (current_view->the_locking_inset)
2199             text = current_view->the_locking_inset->getLyXText(current_view);
2200         if (!text)
2201             text = current_view->text;
2202         text->SetParagraph(current_view,
2203                            line_top,
2204                            line_bottom,
2205                            pagebreak_top,
2206                            pagebreak_bottom,
2207                            space_top,
2208                            space_bottom,
2209                            align, 
2210                            labelwidthstring,
2211                            noindent);
2212         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2213         current_view->owner()->getMiniBuffer()->Set(_("Paragraph layout set"));
2214 }
2215
2216
2217 extern "C" void ParagraphCancelCB(FL_OBJECT *, long)
2218 {
2219         fl_hide_form(fd_form_paragraph->form_paragraph);
2220 }
2221
2222
2223 extern "C" void ParagraphOKCB(FL_OBJECT *ob, long data)
2224 {
2225         ParagraphApplyCB(ob, data);
2226         ParagraphCancelCB(ob, data);
2227 }
2228
2229
2230 /* callbacks for form form_character */
2231
2232 extern "C" void CharacterApplyCB(FL_OBJECT *, long)
2233 {
2234         // we set toggleall locally here, since it should be true for
2235         // all other uses of ToggleAndShow() (JMarc)
2236         toggleall = fl_get_button(fd_form_character->check_toggle_all);
2237         ToggleAndShow(current_view, UserFreeFont(current_view->buffer()->params));
2238         current_view->setState();
2239         toggleall = true;
2240 }
2241
2242
2243 extern "C" void CharacterCloseCB(FL_OBJECT *, long)
2244 {
2245         fl_hide_form(fd_form_character->form_character);
2246 }
2247
2248
2249 extern "C" void CharacterOKCB(FL_OBJECT *ob, long data)
2250 {
2251         CharacterApplyCB(ob, data);
2252         CharacterCloseCB(ob, data);
2253 }
2254
2255
2256 /* callbacks for form form_document */
2257
2258 void UpdateDocumentButtons(BufferParams const & params) 
2259 {
2260         fl_set_choice(fd_form_document->choice_pagestyle, 1);
2261
2262         switch (params.sides) {
2263         case LyXTextClass::OneSide:
2264                 fl_set_button(fd_form_document->radio_sides_one, 1);
2265                 break;
2266         case LyXTextClass::TwoSides:
2267                 fl_set_button(fd_form_document->radio_sides_two, 1);
2268                 break;
2269         }
2270    
2271         if (params.columns == 2)
2272                 fl_set_button(fd_form_document->radio_columns_two, 1);
2273         else
2274                 fl_set_button(fd_form_document->radio_columns_one, 1);
2275         
2276         fl_set_input(fd_form_document->input_extra, params.options.c_str());
2277         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
2278                              params.secnumdepth);
2279         fl_set_counter_value(fd_form_document->slider_tocdepth, 
2280                              params.tocdepth);
2281         
2282 }
2283
2284 extern "C" void ChoiceClassCB(FL_OBJECT * ob, long)
2285 {
2286         ProhibitInput(current_view);
2287         if (textclasslist.Load(fl_get_choice(ob)-1)) {
2288                 if (AskQuestion(_("Should I set some parameters to"),
2289                                 fl_get_choice_text(ob),
2290                                 _("the defaults of this document class?"))) {
2291                         BufferParams params = BufferParams();
2292                         params.textclass = fl_get_choice(ob)-1;
2293                         params.useClassDefaults();
2294                         UpdateLayoutDocument(&params);
2295                         UpdateDocumentButtons(params);
2296                 }
2297         } else {
2298                 // unable to load new style
2299                 WriteAlert(_("Conversion Errors!"),
2300                            _("Unable to switch to new document class."),
2301                            _("Reverting to original document class."));
2302                 fl_set_choice(fd_form_document->choice_class, 
2303                               current_view->buffer()->params.textclass + 1);
2304         }
2305         AllowInput(current_view);
2306 }
2307
2308
2309 extern "C" void DocumentDefskipCB(FL_OBJECT * obj, long)
2310 {
2311         // "Synchronize" the choice and the input field, so that it
2312         // is impossible to commit senseless data.
2313         FD_form_document const * fd = fd_form_document;
2314
2315         if (obj == fd->choice_default_skip) {
2316                 if (fl_get_choice (fd->choice_default_skip) != 4) {
2317                         fl_set_input (fd->input_default_skip, "");
2318                         ActivateDocumentButtons();
2319                 }
2320         } else if (obj == fd->input_default_skip) {
2321
2322                 char const * input = fl_get_input (fd->input_default_skip);
2323
2324                 if (!*input) {
2325                         fl_set_choice (fd->choice_default_skip, 2);
2326                         ActivateDocumentButtons();
2327                 } else if (isValidGlueLength (input)) {
2328                         fl_set_choice (fd->choice_default_skip, 4);
2329                         ActivateDocumentButtons();
2330                 } else {
2331                         fl_set_choice (fd->choice_default_skip, 4);
2332                         DeactivateDocumentButtons();
2333                 }
2334         }
2335 }
2336
2337
2338 extern "C" void DocumentSpacingCB(FL_OBJECT * obj, long)
2339 {
2340         // "Synchronize" the choice and the input field, so that it
2341         // is impossible to commit senseless data.
2342         FD_form_document const * fd = fd_form_document;
2343
2344         if (obj == fd->choice_spacing
2345             && fl_get_choice (fd->choice_spacing) != 4) {
2346                 fl_set_input(fd->input_spacing, "");
2347         } else if (obj == fd->input_spacing) {
2348
2349                 const char* input = fl_get_input (fd->input_spacing);
2350
2351                 if (!*input) {
2352                         fl_set_choice (fd->choice_spacing, 1);
2353                 } else {
2354                         fl_set_choice (fd->choice_spacing, 4);
2355                 }
2356         }
2357 }
2358
2359
2360 extern "C" void DocumentApplyCB(FL_OBJECT *, long)
2361 {
2362         bool redo = false;
2363         BufferParams * params = &(current_view->buffer()->params);
2364
2365         Language const * old_language = params->language_info;
2366         params->language = combo_language->getline();
2367         Languages::iterator lit = languages.find(params->language);
2368
2369         Language const * new_language;
2370         if (lit != languages.end()) 
2371                 new_language = &(*lit).second;
2372         else
2373                 new_language = default_language;
2374
2375         if (current_view->available()) {
2376                 if (old_language != new_language
2377                     && old_language->RightToLeft() == new_language->RightToLeft()
2378                     && !current_view->buffer()->isMultiLingual())
2379                         current_view->buffer()->ChangeLanguage(old_language,
2380                                                                new_language);
2381                 if (old_language != new_language) {
2382                                 //current_view->buffer()->redraw();
2383                         redo = true;
2384                 }
2385         }
2386         params->language_info = new_language;
2387
2388         // If default skip is a "Length" but there's no text in the
2389         // input field, reset the kind to "Medskip", which is the default.
2390         if (fl_get_choice (fd_form_document->choice_default_skip) == 4
2391             && !*(fl_get_input (fd_form_document->input_default_skip))) {
2392                 fl_set_choice (fd_form_document->choice_default_skip, 2);
2393         }
2394
2395         /* this shouldn't be done automatically IMO. For example I write german
2396          * documents with an american keyboard very often. Matthias */
2397    
2398         /* ChangeKeymap(buffer->parameters.language, TRUE, false,
2399            fl_get_choice(fd_form_document->choice_language)); */
2400         params->fonts = 
2401                 fl_get_choice_text(fd_form_document->choice_fonts);
2402         params->inputenc = 
2403                 fl_get_choice_text(fd_form_document->choice_inputenc);
2404         params->fontsize = 
2405                 fl_get_choice_text(fd_form_document->choice_fontsize);
2406         params->pagestyle = 
2407                 fl_get_choice_text(fd_form_document->choice_pagestyle);
2408         params->graphicsDriver = 
2409                 fl_get_choice_text(fd_form_document->choice_postscript_driver);
2410         params->use_amsmath = 
2411                 fl_get_button(fd_form_document->check_use_amsmath);
2412    
2413         if (!current_view->available())
2414                 return;
2415
2416         current_view->text->SetCursor(current_view,
2417                                       current_view->text->cursor.par(),
2418                                       current_view->text->cursor.pos());
2419         current_view->setState();
2420
2421         LyXTextClassList::ClassList::size_type new_class =
2422                 fl_get_choice(fd_form_document->choice_class) - 1;
2423
2424         if (params->textclass != new_class) {
2425                 // try to load new_class
2426                 if (textclasslist.Load(new_class)) {
2427                         // successfully loaded
2428                         redo = true;
2429                         current_view->owner()->getMiniBuffer()->
2430                                 Set(_("Converting document to new document class..."));
2431                         CutAndPaste cap;
2432                         int ret = cap.SwitchLayoutsBetweenClasses(
2433                                 current_view->buffer()->params.textclass,
2434                                 new_class,
2435                                 current_view->buffer()->paragraph);
2436
2437                         if (ret) {
2438                                 string s;
2439                                 if (ret == 1)
2440                                         s = _("One paragraph couldn't be converted");
2441                                 else {
2442                                         s += tostr(ret);
2443                                         s += _(" paragraphs couldn't be converted");
2444                                 }
2445                                 WriteAlert(_("Conversion Errors!"), s,
2446                                            _("into chosen document class"));
2447                         }
2448
2449                         params->textclass = new_class;
2450                 } else {
2451                         // problem changing class -- warn user and retain old style
2452                         WriteAlert(_("Conversion Errors!"),
2453                                    _("Unable to switch to new document class."),
2454                                    _("Reverting to original document class."));
2455                         fl_set_choice(fd_form_document->choice_class, params->textclass + 1);
2456                 }
2457         }
2458
2459         char tmpsep = params->paragraph_separation;
2460         if (fl_get_button(fd_form_document->radio_indent))
2461                 params->paragraph_separation = BufferParams::PARSEP_INDENT;
2462         else
2463                 params->paragraph_separation = BufferParams::PARSEP_SKIP;
2464         if (tmpsep != params->paragraph_separation)
2465                 redo = true;
2466    
2467         VSpace tmpdefskip = params->getDefSkip();
2468         switch (fl_get_choice (fd_form_document->choice_default_skip)) {
2469         case 1: params->setDefSkip(VSpace(VSpace::SMALLSKIP)); break;
2470         case 2: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2471         case 3: params->setDefSkip(VSpace(VSpace::BIGSKIP)); break;
2472         case 4: params->setDefSkip( 
2473                 VSpace (LyXGlueLength (fl_get_input 
2474                                        (fd_form_document->input_default_skip))));
2475         break;
2476         // DocumentDefskipCB assures that this never happens
2477         default: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2478         }
2479         if (!(tmpdefskip == params->getDefSkip()))
2480                 redo = true;
2481
2482         if (fl_get_button(fd_form_document->radio_columns_two))
2483                 params->columns = 2;
2484         else
2485                 params->columns = 1;
2486         if (fl_get_button(fd_form_document->radio_sides_two))
2487                 params->sides = LyXTextClass::TwoSides;
2488         else
2489                 params->sides = LyXTextClass::OneSide;
2490
2491         Spacing tmpSpacing = params->spacing;
2492         switch(fl_get_choice(fd_form_document->choice_spacing)) {
2493         case 1:
2494                 lyxerr.debug() << "Spacing: SINGLE" << endl;
2495                 params->spacing.set(Spacing::Single);
2496                 break;
2497         case 2:
2498                 lyxerr.debug() << "Spacing: ONEHALF" << endl;
2499                 params->spacing.set(Spacing::Onehalf);
2500                 break;
2501         case 3:
2502                 lyxerr.debug() << "Spacing: DOUBLE" << endl;
2503                 params->spacing.set(Spacing::Double);
2504                 break;
2505         case 4:
2506                 lyxerr.debug() << "Spacing: OTHER" << endl;
2507                 params->spacing.set(Spacing::Other, 
2508                                     fl_get_input(fd_form_document->input_spacing));
2509                 break;
2510         }
2511         if (tmpSpacing != params->spacing)
2512                 redo = true;
2513         
2514         signed char tmpchar =  
2515                 static_cast<signed char>(fl_get_counter_value(fd_form_document->slider_secnumdepth));
2516         if (params->secnumdepth != tmpchar)
2517                 redo = true;
2518         params->secnumdepth = tmpchar;
2519    
2520         params->tocdepth =  
2521                 static_cast<int>(fl_get_counter_value(fd_form_document->slider_tocdepth));
2522
2523         params->float_placement = 
2524                 fl_get_input(fd_form_document->input_float_placement);
2525
2526         // More checking should be done to ensure the string doesn't have
2527         // spaces or illegal placement characters in it. (thornley)
2528
2529         if (redo)
2530                 current_view->redoCurrentBuffer();
2531    
2532         current_view->owner()->getMiniBuffer()->Set(_("Document layout set"));
2533         current_view->buffer()->markDirty();
2534         
2535         params->options = 
2536                 fl_get_input(fd_form_document->input_extra);
2537 }
2538
2539
2540 extern "C" void DocumentCancelCB(FL_OBJECT *, long)
2541 {
2542         fl_hide_form(fd_form_document->form_document);
2543 }
2544
2545
2546 extern "C" void DocumentOKCB(FL_OBJECT * ob, long data)
2547 {
2548         DocumentCancelCB(ob, data);
2549         DocumentApplyCB(ob, data);
2550 }
2551
2552
2553 extern "C" void DocumentBulletsCB(FL_OBJECT *, long)
2554 {
2555         bulletForm();
2556         // bullet callbacks etc. in bullet_panel.C -- ARRae
2557 }
2558
2559
2560 /* callbacks for form form_quotes */
2561
2562 extern "C" void QuotesApplyCB(FL_OBJECT *, long)
2563 {
2564         if (!current_view->available())
2565                 return;
2566         
2567         current_view->owner()->getMiniBuffer()->Set(_("Quotes type set"));
2568         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
2569         switch(fl_get_choice(fd_form_quotes->choice_quotes_language) - 1) {
2570         case 0:
2571                 lga = InsetQuotes::EnglishQ;
2572                 break;
2573         case 1:
2574                 lga = InsetQuotes::SwedishQ;
2575                 break;
2576         case 2:
2577                 lga = InsetQuotes::GermanQ;
2578                 break;
2579         case 3:
2580                 lga = InsetQuotes::PolishQ;
2581                 break;
2582         case 4:
2583                 lga = InsetQuotes::FrenchQ;
2584                 break;
2585         case 5:
2586                 lga = InsetQuotes::DanishQ;
2587                 break;
2588         }
2589         current_view->buffer()->params.quotes_language = lga;
2590         if (fl_get_button(fd_form_quotes->radio_single))   
2591                 current_view->buffer()->
2592                         params.quotes_times = InsetQuotes::SingleQ;
2593         else
2594                 current_view->buffer()->
2595                         params.quotes_times = InsetQuotes::DoubleQ;
2596 }
2597
2598
2599 extern "C" void QuotesCancelCB(FL_OBJECT *, long)
2600 {
2601         fl_hide_form(fd_form_quotes->form_quotes);
2602 }
2603
2604
2605 extern "C" void QuotesOKCB(FL_OBJECT * ob, long data)
2606 {
2607         QuotesApplyCB(ob, data);
2608         QuotesCancelCB(ob, data);
2609 }
2610
2611
2612
2613 /* callbacks for form form_preamble */
2614
2615 extern "C" void PreambleCancelCB(FL_OBJECT *, long)
2616 {
2617         fl_hide_form(fd_form_preamble->form_preamble);
2618 }
2619
2620
2621 extern "C" void PreambleApplyCB(FL_OBJECT *, long)
2622 {
2623         if (!current_view->available())
2624                 return;
2625         
2626         current_view->buffer()->params.preamble = 
2627                 fl_get_input(fd_form_preamble->input_preamble);
2628         current_view->buffer()->markDirty();
2629         current_view->owner()->getMiniBuffer()->Set(_("LaTeX preamble set"));
2630 }
2631
2632    
2633 extern "C" void PreambleOKCB(FL_OBJECT * ob, long data)
2634 {
2635         PreambleApplyCB(ob, data);
2636         PreambleCancelCB(ob, data);
2637 }
2638
2639
2640 /* callbacks for form form_table */
2641
2642 extern "C"
2643 void TableApplyCB(FL_OBJECT *, long)
2644 {
2645         if (!current_view->available())
2646                 return;
2647
2648 #ifndef NEW_TABULAR
2649         // check for tables in tables
2650         if (current_view->text->cursor.par()->table){
2651                 WriteAlert(_("Impossible Operation!"),
2652                            _("Cannot insert table in table."),
2653                            _("Sorry."));
2654                 return;
2655         }
2656 #endif
2657         current_view->owner()->getMiniBuffer()->Set(_("Inserting table..."));
2658
2659         int ysize = int(fl_get_slider_value(fd_form_table->slider_columns) + 0.5);
2660         int xsize = int(fl_get_slider_value(fd_form_table->slider_rows) + 0.5);
2661    
2662    
2663         current_view->hideCursor();
2664         current_view->beforeChange();
2665         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2666    
2667         current_view->text->SetCursorParUndo(current_view->buffer()); 
2668         current_view->text->FreezeUndo();
2669
2670         current_view->text->BreakParagraph(current_view);
2671         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2672    
2673         if (current_view->text->cursor.par()->Last()) {
2674                 current_view->text->CursorLeft(current_view);
2675       
2676                 current_view->text->BreakParagraph(current_view);
2677                 current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2678         }
2679
2680         current_view->text->current_font.setLatex(LyXFont::OFF);
2681         //if (!fl_get_button(fd_form_table->check_latex)){
2682         // insert the new wysiwy table
2683         current_view->text->SetLayout(current_view, 0); // standard layout
2684 #ifndef NEW_INSETS
2685         if (current_view->text->cursor.par()->footnoteflag == 
2686             LyXParagraph::NO_FOOTNOTE) {
2687 #endif
2688                 current_view->text
2689                         ->SetParagraph(current_view, 0, 0,
2690                                        0, 0,
2691                                        VSpace (0.3 * current_view->buffer()->
2692                                                params.spacing.getValue(),
2693                                                LyXLength::CM),
2694                                        VSpace (0.3 * current_view->buffer()->
2695                                                params.spacing.getValue(),
2696                                                LyXLength::CM),
2697                                        LYX_ALIGN_CENTER,
2698                                        string(),
2699                                        0);
2700 #ifndef NEW_INSETS
2701         } else {
2702                 current_view->text
2703                         ->SetParagraph(current_view, 0, 0,
2704                                        0, 0,
2705                                        VSpace(VSpace::NONE),
2706                                        VSpace(VSpace::NONE),
2707                                        LYX_ALIGN_CENTER, 
2708                                        string(),
2709                                        0);
2710         }
2711 #endif
2712 #ifndef NEW_TABULAR
2713         current_view->text->cursor.par()->table =
2714                 new LyXTable(xsize, ysize);
2715 #endif
2716         Language const * lang = 
2717                 current_view->text->cursor.par()->getParLanguage(current_view->buffer()->params);
2718         LyXFont font(LyXFont::ALL_INHERIT, lang);
2719         for (int i = 0; i < xsize * ysize - 1; ++i) {
2720                 current_view->text->cursor.par()
2721                         ->InsertChar(0, LyXParagraph::META_NEWLINE, font);
2722         }
2723         current_view->text->RedoParagraph(current_view);
2724    
2725         current_view->text->UnFreezeUndo();
2726      
2727         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2728         current_view->owner()->getMiniBuffer()->Set(_("Table inserted"));
2729         current_view->setState();
2730 }
2731
2732
2733 extern "C"
2734 void TableCancelCB(FL_OBJECT *, long)
2735 {
2736         fl_hide_form(fd_form_table->form_table);
2737 }
2738
2739
2740 extern "C"
2741 void TableOKCB(FL_OBJECT * ob, long data)
2742 {
2743         TableApplyCB(ob, data);
2744         TableCancelCB(ob, data);
2745 }
2746
2747
2748 void Figure()
2749 {
2750         if (fd_form_figure->form_figure->visible) {
2751                 fl_raise_form(fd_form_figure->form_figure);
2752         } else {
2753                 fl_show_form(fd_form_figure->form_figure,
2754                              FL_PLACE_MOUSE, FL_FULLBORDER,
2755                              _("Insert Figure"));
2756         }
2757 }
2758
2759
2760 void Table()
2761 {
2762         if (fd_form_table->form_table->visible) {
2763                 fl_raise_form(fd_form_table->form_table);
2764         } else {
2765                 fl_show_form(fd_form_table->form_table,
2766                              FL_PLACE_MOUSE, FL_FULLBORDER,
2767                              _("Insert Table"));
2768         }
2769 }
2770
2771
2772 /* callbacks for form form_figure */
2773 extern "C"
2774 void FigureApplyCB(FL_OBJECT *, long)
2775 {
2776         if (!current_view->available())
2777                 return;
2778
2779         Buffer * buffer = current_view->buffer();
2780         if(buffer->isReadonly()) // paranoia
2781                 return;
2782         
2783         current_view->owner()->getMiniBuffer()->Set(_("Inserting figure..."));
2784         if (fl_get_button(fd_form_figure->radio_inline)
2785 #ifndef NEW_TABULAR
2786             || current_view->text->cursor.par()->table
2787 #endif
2788                 ) {
2789                 InsetFig * new_inset = new InsetFig(100, 20, buffer);
2790                 current_view->insertInset(new_inset);
2791                 current_view->owner()->getMiniBuffer()->Set(_("Figure inserted"));
2792                 new_inset->Edit(current_view, 0, 0, 0);
2793                 return;
2794         }
2795         
2796         current_view->hideCursor();
2797         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2798         current_view->beforeChange();
2799       
2800         current_view->text->SetCursorParUndo(current_view->buffer()); 
2801         current_view->text->FreezeUndo();
2802
2803         current_view->text->BreakParagraph(current_view);
2804         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2805       
2806         if (current_view->text->cursor.par()->Last()) {
2807                 current_view->text->CursorLeft(current_view);
2808          
2809                 current_view->text->BreakParagraph(current_view);
2810                 current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2811         }
2812
2813         // The standard layout should always be numer 0;
2814         current_view->text->SetLayout(current_view, 0);
2815
2816 #ifndef NEW_INSETS
2817         if (current_view->text->cursor.par()->footnoteflag == 
2818             LyXParagraph::NO_FOOTNOTE) {
2819 #endif
2820                 current_view->text->
2821                         SetParagraph(current_view, 0, 0,
2822                                      0, 0,
2823                                      VSpace (0.3 * buffer->params.spacing.getValue(),
2824                                              LyXLength::CM),
2825                                      VSpace (0.3 *
2826                                              buffer->params.spacing.getValue(),
2827                                              LyXLength::CM),
2828                                      LYX_ALIGN_CENTER, string(), 0);
2829 #ifndef NEW_INSETS
2830         } else {
2831                 current_view->text->SetParagraph(current_view, 0, 0,
2832                                                  0, 0,
2833                                                  VSpace(VSpace::NONE),
2834                                                  VSpace(VSpace::NONE),
2835                                                  LYX_ALIGN_CENTER, 
2836                                                  string(),
2837                                                  0);
2838         }
2839 #endif
2840         
2841         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2842       
2843         Inset * new_inset = new InsetFig(100, 100, buffer);
2844         current_view->insertInset(new_inset);
2845         new_inset->Edit(current_view, 0, 0, 0);
2846         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2847         current_view->owner()->getMiniBuffer()->Set(_("Figure inserted"));
2848         current_view->text->UnFreezeUndo();
2849         current_view->setState();
2850 }
2851
2852
2853 extern "C" void FigureCancelCB(FL_OBJECT *, long)
2854 {
2855         fl_hide_form(fd_form_figure->form_figure);
2856 }
2857
2858
2859 extern "C" void FigureOKCB(FL_OBJECT * ob, long data)
2860 {
2861         FigureApplyCB(ob, data);
2862         FigureCancelCB(ob, data);
2863 }
2864
2865
2866 extern "C" void ScreenApplyCB(FL_OBJECT *, long)
2867 {
2868         lyxrc.roman_font_name = fl_get_input(fd_form_screen->input_roman);
2869         lyxrc.sans_font_name = fl_get_input(fd_form_screen->input_sans);
2870         lyxrc.typewriter_font_name = fl_get_input(fd_form_screen->input_typewriter);
2871         lyxrc.font_norm = fl_get_input(fd_form_screen->input_font_norm);
2872         lyxrc.set_font_norm_type();
2873         lyxrc.zoom = atoi(fl_get_input(fd_form_screen->intinput_size));
2874         fontloader.update();
2875
2876         // Of course we should only to the resize and the textcahce.clear
2877         // if values really changed...but not very important right now. (Lgb)
2878         
2879         // All buffers will need resize
2880         bufferlist.resize();
2881         // We also need to empty the textcache so that
2882         // the buffer will be formatted correctly after
2883         // a zoom change.
2884         textcache.clear();
2885         
2886         current_view->owner()->getMiniBuffer()->Set(_("Screen options set"));
2887 }
2888
2889
2890 extern "C" void ScreenCancelCB(FL_OBJECT *, long)
2891 {
2892         fl_hide_form(fd_form_screen->form_screen);
2893 }
2894
2895
2896 extern "C" void ScreenOKCB(FL_OBJECT * ob, long data)
2897 {
2898         ScreenCancelCB(ob, data);
2899         ScreenApplyCB(ob, data);
2900 }
2901
2902
2903 // This function runs "configure" and then rereads lyx.defaults to
2904 // reconfigure the automatic settings.
2905 void Reconfigure(BufferView * bv)
2906 {
2907         bv->owner()->getMiniBuffer()->Set(_("Running configure..."));
2908
2909         // Run configure in user lyx directory
2910         Path p(user_lyxdir);
2911         Systemcalls one(Systemcalls::System, 
2912                         AddName(system_lyxdir, "configure"));
2913         p.pop();
2914         bv->owner()->getMiniBuffer()->Set(_("Reloading configuration..."));
2915         lyxrc.read(LibFileSearch(string(), "lyxrc.defaults"));
2916         WriteAlert(_("The system has been reconfigured."), 
2917                    _("You need to restart LyX to make use of any"),
2918                    _("updated document class specifications."));
2919 }
2920
2921
2922 //
2923 // Table of Contents
2924 //
2925
2926 static vector<Buffer::TocItem> toclist;
2927
2928
2929 extern "C" void TocSelectCB(FL_OBJECT * ob, long)
2930 {
2931         if (!current_view->available())
2932                 return;
2933    
2934         TocUpdateCB(0, 0);
2935         unsigned int choice = fl_get_browser(ob);
2936         if (0 < choice && choice - 1 < toclist.size()) {
2937                 current_view->beforeChange();
2938                 current_view->text->SetCursor(current_view, toclist[choice-1].par, 0);
2939                 current_view->text->sel_cursor = 
2940                         current_view->text->cursor;
2941                 current_view->update(BufferView::SELECT|BufferView::FITCUR);
2942         } else {
2943                 WriteAlert(_("Error"), 
2944                            _("Couldn't find this label"), 
2945                            _("in current document."));
2946         }
2947 }
2948
2949
2950 extern "C" void TocCancelCB(FL_OBJECT *, long)
2951 {
2952         fl_hide_form(fd_form_toc->form_toc);
2953 }
2954
2955
2956 extern "C"
2957 void TocUpdateCB(FL_OBJECT *, long)
2958 {
2959         if (!current_view->available()) {
2960                 toclist.clear();
2961                 fl_clear_browser(fd_form_toc->browser_toc);
2962                 fl_add_browser_line(fd_form_toc->browser_toc,
2963                                     _("*** No Document ***"));
2964                 return;
2965         }
2966
2967         vector<vector<Buffer::TocItem> > tmp =
2968                 current_view->buffer()->getTocList();
2969         int type = fl_get_choice(fd_form_toc->toctype)-1;
2970         //if (toclist == tmp[type])
2971         //      return;
2972         if (toclist.size() == tmp[type].size()) {
2973                 // Check if all elements are the same.
2974                 unsigned int i = 0;
2975                 for (; i < toclist.size(); ++i) {
2976                         if (toclist[i] !=  tmp[type][i])
2977                                 break;
2978                 }
2979                 if (i >= toclist.size()) return;
2980         }
2981
2982         toclist = tmp[type];
2983
2984         static Buffer * buffer = 0;
2985         int topline = 0;
2986         int line = 0;
2987         if (buffer == current_view->buffer()) {
2988                 topline = fl_get_browser_topline(fd_form_toc->browser_toc);
2989                 line = fl_get_browser(fd_form_toc->browser_toc);
2990         } else
2991                 buffer = current_view->buffer();
2992
2993         fl_clear_browser(fd_form_toc->browser_toc);
2994         fl_hide_object(fd_form_toc->browser_toc);
2995
2996         for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
2997              it != toclist.end(); ++it)
2998                 fl_add_browser_line(fd_form_toc->browser_toc,
2999                                     (string(4*(*it).depth,' ')+
3000                                      (*it).str).c_str());
3001
3002         fl_set_browser_topline(fd_form_toc->browser_toc, topline);
3003         fl_select_browser_line(fd_form_toc->browser_toc, line);
3004         fl_show_object(fd_form_toc->browser_toc);
3005 }
3006
3007
3008 /* callbacks for form form_ref */
3009 extern "C" void RefSelectCB(FL_OBJECT *, long data)
3010 {
3011         if (!current_view->available())
3012                 return;
3013
3014         string s = 
3015                 fl_get_browser_line(fd_form_ref->browser_ref,
3016                                     fl_get_browser(fd_form_ref->browser_ref));
3017         string u = frontStrip(strip(fl_get_input(fd_form_ref->ref_name)));
3018
3019         if (s.empty())
3020                 return;
3021
3022         if (data == 5) {
3023                 current_view->owner()->getLyXFunc()->Dispatch(LFUN_REFGOTO, s.c_str());
3024                 if (!current_view->NoSavedPositions()) {
3025                         fl_activate_object(fd_form_ref->back);
3026                         fl_set_object_lcol(fd_form_ref->back, FL_BLACK);
3027                 }
3028                 return;
3029         } else if (data >= 6) {
3030                 current_view->owner()->getLyXFunc()->Dispatch(LFUN_REFBACK);
3031                 if (current_view->NoSavedPositions()) {
3032                         fl_deactivate_object(fd_form_ref->back);
3033                         fl_set_object_lcol(fd_form_ref->back, FL_INACTIVE);
3034                 }
3035                 return;
3036         }       
3037
3038         static string const commands[5]
3039                 = { "\\ref", "\\pageref", "\\vref", "\\vpageref",
3040                     "\\prettyref"};
3041         string t = commands[data];
3042
3043         if (current_view->buffer()->isSGML())
3044                 t += "[" + u + "]" + "{" + s + "}";
3045         else
3046                 t += "{" + s + "}";
3047
3048         Inset * new_inset = 
3049                 new InsetRef(t, current_view->buffer());
3050         current_view->insertInset(new_inset);
3051 }
3052
3053
3054 extern "C" void RefUpdateCB(FL_OBJECT *, long)
3055 {
3056         if (!current_view->available()) {
3057                 fl_clear_browser(fd_form_ref->browser_ref);
3058                 return;
3059         }
3060
3061         FL_OBJECT * brow = fd_form_ref->browser_ref;
3062
3063         // Get the current line, in order to restore it later
3064         char const * const btmp = fl_get_browser_line(brow,
3065                                                       fl_get_browser(brow));
3066         string currentstr = btmp ? btmp : "";
3067
3068         fl_clear_browser(brow);
3069         fl_hide_object(brow);
3070
3071         vector<string> refs = current_view->buffer()->getLabelList();
3072         if (fl_get_button(fd_form_ref->sort))
3073                 sort(refs.begin(),refs.end());
3074         for (vector<string>::const_iterator it = refs.begin();
3075              it != refs.end(); ++it)
3076                 fl_add_browser_line(brow, (*it).c_str());
3077
3078         int topline = 1;
3079         int total_lines = fl_get_browser_maxline(brow);
3080         for (int i = 1; i <= total_lines ; ++i) {
3081                 if (fl_get_browser_line(brow, i) == currentstr) {
3082                         topline = i;
3083                         break;
3084                 }
3085         }
3086         fl_set_browser_topline(brow, topline);
3087
3088         bool empty = refs.empty();
3089         bool sgml = current_view->buffer()->isSGML();
3090         bool readonly = current_view->buffer()->isReadonly();
3091
3092         if (current_view->NoSavedPositions()) {
3093                 fl_deactivate_object(fd_form_ref->back);
3094                 fl_set_object_lcol(fd_form_ref->back, FL_INACTIVE);
3095         } else {
3096                 fl_activate_object(fd_form_ref->back);
3097                 fl_set_object_lcol(fd_form_ref->back, FL_BLACK);
3098         }
3099
3100         if (empty) {
3101                 fl_add_browser_line(brow, 
3102                                     _("*** No labels found in document ***"));
3103                 fl_deactivate_object(brow);
3104                 fl_deactivate_object(fd_form_ref->gotoref);
3105                 fl_set_object_lcol(fd_form_ref->gotoref, FL_INACTIVE);
3106         } else {
3107                 fl_select_browser_line(brow, topline);
3108                 fl_activate_object(brow);
3109                 fl_activate_object(fd_form_ref->gotoref);
3110                 fl_set_object_lcol(fd_form_ref->gotoref, FL_BLACK);
3111         }
3112
3113         if (empty || readonly) {
3114                 fl_deactivate_object(fd_form_ref->ref);
3115                 fl_set_object_lcol(fd_form_ref->ref, FL_INACTIVE);
3116                 fl_deactivate_object(fd_form_ref->pageref);
3117                 fl_set_object_lcol(fd_form_ref->pageref, FL_INACTIVE);
3118         } else {
3119                 fl_activate_object(fd_form_ref->ref);
3120                 fl_set_object_lcol(fd_form_ref->ref, FL_BLACK);
3121                 fl_activate_object(fd_form_ref->pageref);
3122                 fl_set_object_lcol(fd_form_ref->pageref, FL_BLACK);
3123         }
3124         
3125         if (empty || readonly || sgml) {
3126                 fl_deactivate_object(fd_form_ref->vref);
3127                 fl_set_object_lcol(fd_form_ref->vref, FL_INACTIVE);
3128                 fl_deactivate_object(fd_form_ref->vpageref);
3129                 fl_set_object_lcol(fd_form_ref->vpageref, FL_INACTIVE);
3130                 fl_deactivate_object(fd_form_ref->prettyref);
3131                 fl_set_object_lcol(fd_form_ref->prettyref, FL_INACTIVE);
3132         } else {
3133                 fl_activate_object(fd_form_ref->vref);
3134                 fl_set_object_lcol(fd_form_ref->vref, FL_BLACK);
3135                 fl_activate_object(fd_form_ref->vpageref);
3136                 fl_set_object_lcol(fd_form_ref->vpageref, FL_BLACK);
3137                 fl_activate_object(fd_form_ref->prettyref);
3138                 fl_set_object_lcol(fd_form_ref->prettyref, FL_BLACK);
3139         }
3140
3141         if (sgml) {
3142                 fl_activate_object(fd_form_ref->ref_name);
3143                 fl_set_object_lcol(fd_form_ref->ref_name, FL_BLACK);
3144         } else {
3145                 fl_deactivate_object(fd_form_ref->ref_name);
3146                 fl_set_object_lcol(fd_form_ref->ref_name, FL_INACTIVE);
3147         }
3148
3149         fl_show_object(brow);
3150 }
3151
3152
3153 extern "C" void RefHideCB(FL_OBJECT *, long)
3154 {
3155         fl_hide_form(fd_form_ref->form_ref);
3156 }