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