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