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