]> git.lyx.org Git - lyx.git/blob - src/lyx_cb.C
003643240b140efcb5727fa13465126159d9bb41
[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                 std::ostringstream sval;
1780                 sval << params->spacing.getValue(); // setw?
1781                 fl_set_input(fd_form_document->input_spacing,
1782                              sval.str().c_str());
1783                 break;
1784         }
1785         }
1786
1787
1788         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
1789                              params->secnumdepth);
1790         fl_set_counter_value(fd_form_document->slider_tocdepth, 
1791                              params->tocdepth);
1792         if (!params->float_placement.empty()) { // buffer local (Lgb)
1793                 fl_set_input(fd_form_document->input_float_placement,
1794                              params->float_placement.c_str());
1795         } else {
1796                 fl_set_input(fd_form_document->input_float_placement, "");
1797         }
1798         if (!params->options.empty())
1799                 fl_set_input(fd_form_document->input_extra,
1800                              params->options.c_str());
1801         else
1802                 fl_set_input(fd_form_document->input_extra, "");
1803
1804         if (current_view->buffer()->isSGML()) {
1805                 // bullets not used in SGML derived documents
1806                 fl_deactivate_object(fd_form_document->button_bullets);
1807                 fl_set_object_lcol(fd_form_document->button_bullets,
1808                                    FL_INACTIVE);
1809         } else {
1810                 fl_activate_object(fd_form_document->button_bullets);
1811                 fl_set_object_lcol(fd_form_document->button_bullets,
1812                                    FL_BLACK);
1813         }
1814
1815         if (current_view->buffer()->isReadonly()) {
1816                 DisableDocumentLayout();
1817         } else {
1818                 EnableDocumentLayout();
1819         }
1820
1821         return true;
1822 }
1823
1824
1825 void MenuLayoutDocument()
1826 {
1827         if (UpdateLayoutDocument()) {
1828                 if (fd_form_document->form_document->visible) {
1829                         fl_raise_form(fd_form_document->form_document);
1830                 } else {
1831                         fl_show_form(fd_form_document->form_document,
1832                                      FL_PLACE_MOUSE, FL_FULLBORDER,
1833                                      _("Document Layout"));
1834                 }
1835         }
1836 }
1837
1838
1839 bool UpdateLayoutQuotes()
1840 {
1841         bool update = true;
1842         if (!current_view->available()
1843             || current_view->buffer()->isReadonly())
1844                 update = false;
1845         
1846         if (update) {
1847                 fl_set_choice(fd_form_quotes->choice_quotes_language,
1848                               current_view->buffer()->params.quotes_language + 1);
1849                 fl_set_button(fd_form_quotes->radio_single, 0);
1850                 fl_set_button(fd_form_quotes->radio_double, 0);
1851         
1852                 if (current_view->buffer()->params.quotes_times == InsetQuotes::SingleQ)
1853                         fl_set_button(fd_form_quotes->radio_single, 1);
1854                 else
1855                         fl_set_button(fd_form_quotes->radio_double, 1);
1856         } else if (fd_form_quotes->form_quotes->visible) {
1857                 fl_hide_form(fd_form_quotes->form_quotes);
1858         }
1859         return update;
1860 }
1861
1862
1863 void MenuLayoutQuotes()
1864 {
1865         if (UpdateLayoutQuotes()) {
1866                 if (fd_form_quotes->form_quotes->visible) {
1867                         fl_raise_form(fd_form_quotes->form_quotes);
1868                 } else {
1869                         fl_show_form(fd_form_quotes->form_quotes,
1870                                      FL_PLACE_MOUSE, FL_FULLBORDER,
1871                                      _("Quotes"));
1872                 }
1873         }
1874 }
1875 #endif
1876
1877
1878 bool UpdateLayoutPreamble()
1879 {
1880         bool update = true;
1881         if (!current_view->available())
1882                 update = false;
1883
1884         if (update) {
1885                 fl_set_input(fd_form_preamble->input_preamble,
1886                              current_view->buffer()->params.preamble.c_str());
1887
1888                 if (current_view->buffer()->isReadonly()) {
1889                         fl_deactivate_object(fd_form_preamble->input_preamble);
1890                         fl_deactivate_object(fd_form_preamble->button_ok);
1891                         fl_deactivate_object(fd_form_preamble->button_apply);
1892                         fl_set_object_lcol(fd_form_preamble->button_ok, FL_INACTIVE);
1893                         fl_set_object_lcol(fd_form_preamble->button_apply, FL_INACTIVE);
1894                 }
1895                 else {
1896                         fl_activate_object(fd_form_preamble->input_preamble);
1897                         fl_activate_object(fd_form_preamble->button_ok);
1898                         fl_activate_object(fd_form_preamble->button_apply);
1899                         fl_set_object_lcol(fd_form_preamble->button_ok, FL_BLACK);
1900                         fl_set_object_lcol(fd_form_preamble->button_apply, FL_BLACK);
1901                 }
1902         } else if (fd_form_preamble->form_preamble->visible) {
1903                 fl_hide_form(fd_form_preamble->form_preamble);
1904         }
1905         return update;
1906 }
1907
1908
1909 void MenuLayoutPreamble()
1910 {
1911         static int ow = -1, oh;
1912
1913         if (UpdateLayoutPreamble()) {
1914                 if (fd_form_preamble->form_preamble->visible) {
1915                         fl_raise_form(fd_form_preamble->form_preamble);
1916                 } else {
1917                         fl_show_form(fd_form_preamble->form_preamble,
1918                                      FL_PLACE_MOUSE | FL_FREE_SIZE,
1919                                      FL_FULLBORDER,
1920                                      _("LaTeX Preamble"));
1921                         if (ow < 0) {
1922                                 ow = fd_form_preamble->form_preamble->w;
1923                                 oh = fd_form_preamble->form_preamble->h;
1924                         }
1925                         fl_set_form_minsize(fd_form_preamble->form_preamble,
1926                                             ow, oh);
1927                 }
1928         }
1929 }
1930
1931
1932 void MenuLayoutSave()
1933 {
1934         if (!current_view->available())
1935                 return;
1936
1937         if (AskQuestion(_("Do you want to save the current settings"),
1938                         _("for Character, Document, Paper and Quotes"),
1939                         _("as default for new documents?")))
1940                 current_view->buffer()->saveParamsAsDefaults();
1941 }
1942
1943
1944 // This is both GUI and LyXFont dependent. Don't know where to put it. (Asger)
1945 // Well, it's mostly GUI dependent, so I guess it will stay here. (Asger)
1946 LyXFont const UserFreeFont(BufferParams const & params)
1947 {
1948         LyXFont font(LyXFont::ALL_IGNORE);
1949
1950         int pos = fl_get_choice(fd_form_character->choice_family);
1951         switch(pos) {
1952         case 1: font.setFamily(LyXFont::IGNORE_FAMILY); break;
1953         case 2: font.setFamily(LyXFont::ROMAN_FAMILY); break;
1954         case 3: font.setFamily(LyXFont::SANS_FAMILY); break;
1955         case 4: font.setFamily(LyXFont::TYPEWRITER_FAMILY); break;
1956         case 5: font.setFamily(LyXFont::INHERIT_FAMILY); break;
1957         }
1958
1959         pos = fl_get_choice(fd_form_character->choice_series);
1960         switch(pos) {
1961         case 1: font.setSeries(LyXFont::IGNORE_SERIES); break;
1962         case 2: font.setSeries(LyXFont::MEDIUM_SERIES); break;
1963         case 3: font.setSeries(LyXFont::BOLD_SERIES); break;
1964         case 4: font.setSeries(LyXFont::INHERIT_SERIES); break;
1965         }
1966
1967         pos = fl_get_choice(fd_form_character->choice_shape);
1968         switch(pos) {
1969         case 1: font.setShape(LyXFont::IGNORE_SHAPE); break;
1970         case 2: font.setShape(LyXFont::UP_SHAPE); break;
1971         case 3: font.setShape(LyXFont::ITALIC_SHAPE); break;
1972         case 4: font.setShape(LyXFont::SLANTED_SHAPE); break;
1973         case 5: font.setShape(LyXFont::SMALLCAPS_SHAPE); break;
1974         case 6: font.setShape(LyXFont::INHERIT_SHAPE); break;
1975         }
1976
1977         pos = fl_get_choice(fd_form_character->choice_size);
1978         switch(pos) {
1979         case 1: font.setSize(LyXFont::IGNORE_SIZE); break;
1980         case 2: font.setSize(LyXFont::SIZE_TINY); break;
1981         case 3: font.setSize(LyXFont::SIZE_SCRIPT); break;
1982         case 4: font.setSize(LyXFont::SIZE_FOOTNOTE); break;
1983         case 5: font.setSize(LyXFont::SIZE_SMALL); break;
1984         case 6: font.setSize(LyXFont::SIZE_NORMAL); break;
1985         case 7: font.setSize(LyXFont::SIZE_LARGE); break;
1986         case 8: font.setSize(LyXFont::SIZE_LARGER); break;
1987         case 9: font.setSize(LyXFont::SIZE_LARGEST); break;
1988         case 10: font.setSize(LyXFont::SIZE_HUGE); break;
1989         case 11: font.setSize(LyXFont::SIZE_HUGER); break;
1990         case 12: font.setSize(LyXFont::INCREASE_SIZE); break;
1991         case 13: font.setSize(LyXFont::DECREASE_SIZE); break;
1992         case 14: font.setSize(LyXFont::INHERIT_SIZE); break;
1993         }
1994
1995         pos = fl_get_choice(fd_form_character->choice_bar);
1996         switch(pos) {
1997         case 1: font.setEmph(LyXFont::IGNORE);
1998                 font.setUnderbar(LyXFont::IGNORE);
1999                 font.setNoun(LyXFont::IGNORE);
2000                 font.setLatex(LyXFont::IGNORE);
2001                 break;
2002         case 2: font.setEmph(LyXFont::TOGGLE); break;
2003         case 3: font.setUnderbar(LyXFont::TOGGLE); break;
2004         case 4: font.setNoun(LyXFont::TOGGLE); break;
2005         case 5: font.setLatex(LyXFont::TOGGLE); break;
2006         case 6: font.setEmph(LyXFont::INHERIT);
2007                 font.setUnderbar(LyXFont::INHERIT);
2008                 font.setNoun(LyXFont::INHERIT);
2009                 font.setLatex(LyXFont::INHERIT);
2010                 break;
2011         }
2012
2013         pos = fl_get_choice(fd_form_character->choice_color);
2014         switch(pos) {
2015         case 1: font.setColor(LColor::ignore); break;
2016         case 2: font.setColor(LColor::none); break;
2017         case 3: font.setColor(LColor::black); break;
2018         case 4: font.setColor(LColor::white); break;
2019         case 5: font.setColor(LColor::red); break;
2020         case 6: font.setColor(LColor::green); break;
2021         case 7: font.setColor(LColor::blue); break;
2022         case 8: font.setColor(LColor::cyan); break;
2023         case 9: font.setColor(LColor::magenta); break;
2024         case 10: font.setColor(LColor::yellow); break;
2025         case 11: font.setColor(LColor::inherit); break;
2026         }
2027
2028         int choice = combo_language2->get();
2029         if (choice == 1)
2030                 font.setLanguage(ignore_language);
2031         else if (choice == 2)
2032                 font.setLanguage(params.language_info);
2033         else
2034                 font.setLanguage(&languages[combo_language2->getline()]);
2035
2036         return font; 
2037 }
2038
2039
2040 /* callbacks for form form_title */
2041 extern "C" void TimerCB(FL_OBJECT *, long)
2042 {
2043         // only if the form still exists
2044         if (lyxrc.show_banner && fd_form_title->form_title != 0) {
2045                 if (fd_form_title->form_title->visible) {
2046                         fl_hide_form(fd_form_title->form_title);
2047                 }
2048                 fl_free_form(fd_form_title->form_title);
2049                 fd_form_title->form_title = 0;
2050         }
2051 }
2052
2053
2054 /* callbacks for form form_paragraph */
2055
2056 extern "C" void ParagraphVSpaceCB(FL_OBJECT * obj, long )
2057 {
2058         // "Synchronize" the choices and input fields, making it
2059         // impossible to commit senseless data.
2060
2061         FD_form_paragraph const * fp = fd_form_paragraph;
2062
2063         if (obj == fp->choice_space_above) {
2064                 if (fl_get_choice (fp->choice_space_above) != 7) {
2065                         fl_set_input (fp->input_space_above, "");
2066                         ActivateParagraphButtons();
2067                 }
2068         } else if (obj == fp->choice_space_below) {
2069                 if (fl_get_choice (fp->choice_space_below) != 7) {
2070                         fl_set_input (fp->input_space_below, "");
2071                         ActivateParagraphButtons();
2072                 }
2073         } else if (obj == fp->input_space_above) {
2074                 string input = fl_get_input (fp->input_space_above);
2075
2076                 if (input.empty()) {
2077                         fl_set_choice (fp->choice_space_above, 1);
2078                         ActivateParagraphButtons();
2079                 }
2080                 else if (isValidGlueLength (input)) {
2081                         fl_set_choice (fp->choice_space_above, 7);
2082                         ActivateParagraphButtons();
2083                 }
2084                 else {
2085                         fl_set_choice (fp->choice_space_above, 7);
2086                         DeactivateParagraphButtons();
2087                 }
2088         } else if (obj == fp->input_space_below) {
2089                 string input = fl_get_input (fp->input_space_below);
2090
2091                 if (input.empty()) {
2092                         fl_set_choice (fp->choice_space_below, 1);
2093                         ActivateParagraphButtons();
2094                 }
2095                 else if (isValidGlueLength (input)) {
2096                         fl_set_choice (fp->choice_space_below, 7);
2097                         ActivateParagraphButtons();
2098                 }
2099                 else {
2100                         fl_set_choice (fp->choice_space_below, 7);
2101                         DeactivateParagraphButtons();
2102                 }
2103         }
2104 }
2105
2106
2107 extern "C" void ParagraphApplyCB(FL_OBJECT *, long)
2108 {
2109         if (!current_view->available())
2110                 return;
2111         
2112         VSpace space_top, space_bottom;
2113         LyXAlignment align;
2114         string labelwidthstring;
2115         bool noindent;
2116
2117         // If a vspace kind is "Length" but there's no text in
2118         // the input field, reset the kind to "None". 
2119         if (fl_get_choice (fd_form_paragraph->choice_space_above) == 7
2120             && !*(fl_get_input (fd_form_paragraph->input_space_above))) {
2121                 fl_set_choice (fd_form_paragraph->choice_space_above, 1);
2122         }
2123         if (fl_get_choice (fd_form_paragraph->choice_space_below) == 7
2124             && !*(fl_get_input (fd_form_paragraph->input_space_below))) {
2125                 fl_set_choice (fd_form_paragraph->choice_space_below, 1);
2126         }
2127    
2128         bool line_top = fl_get_button(fd_form_paragraph->check_lines_top);
2129         bool line_bottom = fl_get_button(fd_form_paragraph->check_lines_bottom);
2130         bool pagebreak_top = fl_get_button(fd_form_paragraph->check_pagebreaks_top);
2131         bool pagebreak_bottom = fl_get_button(fd_form_paragraph->check_pagebreaks_bottom);
2132         switch (fl_get_choice (fd_form_paragraph->choice_space_above)) {
2133         case 1: space_top = VSpace(VSpace::NONE); break;
2134         case 2: space_top = VSpace(VSpace::DEFSKIP); break;
2135         case 3: space_top = VSpace(VSpace::SMALLSKIP); break;
2136         case 4: space_top = VSpace(VSpace::MEDSKIP); break;
2137         case 5: space_top = VSpace(VSpace::BIGSKIP); break;
2138         case 6: space_top = VSpace(VSpace::VFILL); break;
2139         case 7: space_top = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_above))); break;
2140         }
2141         if (fl_get_button (fd_form_paragraph->check_space_above))
2142                 space_top.setKeep (true);
2143         switch (fl_get_choice (fd_form_paragraph->choice_space_below)) {
2144         case 1: space_bottom = VSpace(VSpace::NONE); break;
2145         case 2: space_bottom = VSpace(VSpace::DEFSKIP); break;
2146         case 3: space_bottom = VSpace(VSpace::SMALLSKIP); break;
2147         case 4: space_bottom = VSpace(VSpace::MEDSKIP); break;
2148         case 5: space_bottom = VSpace(VSpace::BIGSKIP); break;
2149         case 6: space_bottom = VSpace(VSpace::VFILL); break;
2150         case 7: space_bottom = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_below))); break;
2151         }
2152         if (fl_get_button (fd_form_paragraph->check_space_below))
2153                 space_bottom.setKeep (true);
2154
2155         if (fl_get_button(fd_form_paragraph->radio_align_left))
2156                 align = LYX_ALIGN_LEFT;
2157         else if (fl_get_button(fd_form_paragraph->radio_align_right))
2158                 align = LYX_ALIGN_RIGHT;
2159         else if (fl_get_button(fd_form_paragraph->radio_align_center))
2160                 align = LYX_ALIGN_CENTER;
2161         else 
2162                 align = LYX_ALIGN_BLOCK;
2163    
2164         labelwidthstring = fl_get_input(fd_form_paragraph->input_labelwidth);
2165         noindent = fl_get_button(fd_form_paragraph->check_noindent);
2166
2167         LyXText * text = 0;
2168         if (current_view->the_locking_inset)
2169             text = current_view->the_locking_inset->getLyXText(current_view);
2170         if (!text)
2171             text = current_view->text;
2172         text->SetParagraph(current_view,
2173                            line_top,
2174                            line_bottom,
2175                            pagebreak_top,
2176                            pagebreak_bottom,
2177                            space_top,
2178                            space_bottom,
2179                            align, 
2180                            labelwidthstring,
2181                            noindent);
2182         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2183         current_view->owner()->getMiniBuffer()->Set(_("Paragraph layout set"));
2184 }
2185
2186
2187 extern "C" void ParagraphCancelCB(FL_OBJECT *, long)
2188 {
2189         fl_hide_form(fd_form_paragraph->form_paragraph);
2190 }
2191
2192
2193 extern "C" void ParagraphOKCB(FL_OBJECT *ob, long data)
2194 {
2195         ParagraphApplyCB(ob, data);
2196         ParagraphCancelCB(ob, data);
2197 }
2198
2199
2200 /* callbacks for form form_character */
2201
2202 extern "C" void CharacterApplyCB(FL_OBJECT *, long)
2203 {
2204         // we set toggleall locally here, since it should be true for
2205         // all other uses of ToggleAndShow() (JMarc)
2206         toggleall = fl_get_button(fd_form_character->check_toggle_all);
2207         ToggleAndShow(current_view, UserFreeFont(current_view->buffer()->params));
2208         current_view->setState();
2209         toggleall = true;
2210 }
2211
2212
2213 extern "C" void CharacterCloseCB(FL_OBJECT *, long)
2214 {
2215         fl_hide_form(fd_form_character->form_character);
2216 }
2217
2218
2219 extern "C" void CharacterOKCB(FL_OBJECT *ob, long data)
2220 {
2221         CharacterApplyCB(ob, data);
2222         CharacterCloseCB(ob, data);
2223 }
2224
2225
2226 #ifdef USE_OLD_DOCUMENT_LAYOUT
2227 /* callbacks for form form_document */
2228 void UpdateDocumentButtons(BufferParams const & params) 
2229 {
2230         fl_set_choice(fd_form_document->choice_pagestyle, 1);
2231
2232         switch (params.sides) {
2233         case LyXTextClass::OneSide:
2234                 fl_set_button(fd_form_document->radio_sides_one, 1);
2235                 break;
2236         case LyXTextClass::TwoSides:
2237                 fl_set_button(fd_form_document->radio_sides_two, 1);
2238                 break;
2239         }
2240    
2241         if (params.columns == 2)
2242                 fl_set_button(fd_form_document->radio_columns_two, 1);
2243         else
2244                 fl_set_button(fd_form_document->radio_columns_one, 1);
2245         
2246         fl_set_input(fd_form_document->input_extra, params.options.c_str());
2247         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
2248                              params.secnumdepth);
2249         fl_set_counter_value(fd_form_document->slider_tocdepth, 
2250                              params.tocdepth);
2251         
2252 }
2253
2254 extern "C" void ChoiceClassCB(FL_OBJECT * ob, long)
2255 {
2256         ProhibitInput(current_view);
2257         if (textclasslist.Load(fl_get_choice(ob)-1)) {
2258                 if (AskQuestion(_("Should I set some parameters to"),
2259                                 fl_get_choice_text(ob),
2260                                 _("the defaults of this document class?"))) {
2261                         BufferParams params = BufferParams();
2262                         params.textclass = fl_get_choice(ob)-1;
2263                         params.useClassDefaults();
2264                         UpdateLayoutDocument(&params);
2265                         UpdateDocumentButtons(params);
2266                 }
2267         } else {
2268                 // unable to load new style
2269                 WriteAlert(_("Conversion Errors!"),
2270                            _("Unable to switch to new document class."),
2271                            _("Reverting to original document class."));
2272                 fl_set_choice(fd_form_document->choice_class, 
2273                               current_view->buffer()->params.textclass + 1);
2274         }
2275         AllowInput(current_view);
2276 }
2277
2278
2279 extern "C" void DocumentDefskipCB(FL_OBJECT * obj, long)
2280 {
2281         // "Synchronize" the choice and the input field, so that it
2282         // is impossible to commit senseless data.
2283         FD_form_document const * fd = fd_form_document;
2284
2285         if (obj == fd->choice_default_skip) {
2286                 if (fl_get_choice (fd->choice_default_skip) != 4) {
2287                         fl_set_input (fd->input_default_skip, "");
2288                         ActivateDocumentButtons();
2289                 }
2290         } else if (obj == fd->input_default_skip) {
2291
2292                 char const * input = fl_get_input (fd->input_default_skip);
2293
2294                 if (!*input) {
2295                         fl_set_choice (fd->choice_default_skip, 2);
2296                         ActivateDocumentButtons();
2297                 } else if (isValidGlueLength (input)) {
2298                         fl_set_choice (fd->choice_default_skip, 4);
2299                         ActivateDocumentButtons();
2300                 } else {
2301                         fl_set_choice (fd->choice_default_skip, 4);
2302                         DeactivateDocumentButtons();
2303                 }
2304         }
2305 }
2306
2307
2308 extern "C" void DocumentSpacingCB(FL_OBJECT * obj, long)
2309 {
2310         // "Synchronize" the choice and the input field, so that it
2311         // is impossible to commit senseless data.
2312         FD_form_document const * fd = fd_form_document;
2313
2314         if (obj == fd->choice_spacing
2315             && fl_get_choice (fd->choice_spacing) != 4) {
2316                 fl_set_input(fd->input_spacing, "");
2317         } else if (obj == fd->input_spacing) {
2318
2319                 const char* input = fl_get_input (fd->input_spacing);
2320
2321                 if (!*input) {
2322                         fl_set_choice (fd->choice_spacing, 1);
2323                 } else {
2324                         fl_set_choice (fd->choice_spacing, 4);
2325                 }
2326         }
2327 }
2328
2329
2330 extern "C" void DocumentApplyCB(FL_OBJECT *, long)
2331 {
2332         bool redo = false;
2333         BufferParams * params = &(current_view->buffer()->params);
2334
2335         Language const * old_language = params->language_info;
2336         params->language = combo_language->getline();
2337         Languages::iterator lit = languages.find(params->language);
2338
2339         Language const * new_language;
2340         if (lit != languages.end()) 
2341                 new_language = &(*lit).second;
2342         else
2343                 new_language = default_language;
2344
2345         if (current_view->available()) {
2346                 if (old_language != new_language
2347                     && old_language->RightToLeft() == new_language->RightToLeft()
2348                     && !current_view->buffer()->isMultiLingual())
2349                         current_view->buffer()->ChangeLanguage(old_language,
2350                                                                new_language);
2351                 if (old_language != new_language) {
2352                                 //current_view->buffer()->redraw();
2353                         redo = true;
2354                 }
2355         }
2356         params->language_info = new_language;
2357
2358         // If default skip is a "Length" but there's no text in the
2359         // input field, reset the kind to "Medskip", which is the default.
2360         if (fl_get_choice (fd_form_document->choice_default_skip) == 4
2361             && !*(fl_get_input (fd_form_document->input_default_skip))) {
2362                 fl_set_choice (fd_form_document->choice_default_skip, 2);
2363         }
2364
2365         /* this shouldn't be done automatically IMO. For example I write german
2366          * documents with an american keyboard very often. Matthias */
2367    
2368         /* ChangeKeymap(buffer->parameters.language, TRUE, false,
2369            fl_get_choice(fd_form_document->choice_language)); */
2370         params->fonts = 
2371                 fl_get_choice_text(fd_form_document->choice_fonts);
2372         params->inputenc = 
2373                 fl_get_choice_text(fd_form_document->choice_inputenc);
2374         params->fontsize = 
2375                 fl_get_choice_text(fd_form_document->choice_fontsize);
2376         params->pagestyle = 
2377                 fl_get_choice_text(fd_form_document->choice_pagestyle);
2378         params->graphicsDriver = 
2379                 fl_get_choice_text(fd_form_document->choice_postscript_driver);
2380         params->use_amsmath = 
2381                 fl_get_button(fd_form_document->check_use_amsmath);
2382    
2383         if (!current_view->available())
2384                 return;
2385
2386         current_view->text->SetCursor(current_view,
2387                                       current_view->text->cursor.par(),
2388                                       current_view->text->cursor.pos());
2389         current_view->setState();
2390
2391         LyXTextClassList::ClassList::size_type new_class =
2392                 fl_get_choice(fd_form_document->choice_class) - 1;
2393
2394         if (params->textclass != new_class) {
2395                 // try to load new_class
2396                 if (textclasslist.Load(new_class)) {
2397                         // successfully loaded
2398                         redo = true;
2399                         current_view->owner()->getMiniBuffer()->
2400                                 Set(_("Converting document to new document class..."));
2401                         CutAndPaste cap;
2402                         int ret = cap.SwitchLayoutsBetweenClasses(
2403                                 current_view->buffer()->params.textclass,
2404                                 new_class,
2405                                 current_view->buffer()->paragraph);
2406
2407                         if (ret) {
2408                                 string s;
2409                                 if (ret == 1)
2410                                         s = _("One paragraph couldn't be converted");
2411                                 else {
2412                                         s += tostr(ret);
2413                                         s += _(" paragraphs couldn't be converted");
2414                                 }
2415                                 WriteAlert(_("Conversion Errors!"), s,
2416                                            _("into chosen document class"));
2417                         }
2418
2419                         params->textclass = new_class;
2420                 } else {
2421                         // problem changing class -- warn user and retain old style
2422                         WriteAlert(_("Conversion Errors!"),
2423                                    _("Unable to switch to new document class."),
2424                                    _("Reverting to original document class."));
2425                         fl_set_choice(fd_form_document->choice_class, params->textclass + 1);
2426                 }
2427         }
2428
2429         char tmpsep = params->paragraph_separation;
2430         if (fl_get_button(fd_form_document->radio_indent))
2431                 params->paragraph_separation = BufferParams::PARSEP_INDENT;
2432         else
2433                 params->paragraph_separation = BufferParams::PARSEP_SKIP;
2434         if (tmpsep != params->paragraph_separation)
2435                 redo = true;
2436    
2437         VSpace tmpdefskip = params->getDefSkip();
2438         switch (fl_get_choice (fd_form_document->choice_default_skip)) {
2439         case 1: params->setDefSkip(VSpace(VSpace::SMALLSKIP)); break;
2440         case 2: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2441         case 3: params->setDefSkip(VSpace(VSpace::BIGSKIP)); break;
2442         case 4: params->setDefSkip( 
2443                 VSpace (LyXGlueLength (fl_get_input 
2444                                        (fd_form_document->input_default_skip))));
2445         break;
2446         // DocumentDefskipCB assures that this never happens
2447         default: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2448         }
2449         if (!(tmpdefskip == params->getDefSkip()))
2450                 redo = true;
2451
2452         if (fl_get_button(fd_form_document->radio_columns_two))
2453                 params->columns = 2;
2454         else
2455                 params->columns = 1;
2456         if (fl_get_button(fd_form_document->radio_sides_two))
2457                 params->sides = LyXTextClass::TwoSides;
2458         else
2459                 params->sides = LyXTextClass::OneSide;
2460
2461         Spacing tmpSpacing = params->spacing;
2462         switch(fl_get_choice(fd_form_document->choice_spacing)) {
2463         case 1:
2464                 lyxerr.debug() << "Spacing: SINGLE" << endl;
2465                 params->spacing.set(Spacing::Single);
2466                 break;
2467         case 2:
2468                 lyxerr.debug() << "Spacing: ONEHALF" << endl;
2469                 params->spacing.set(Spacing::Onehalf);
2470                 break;
2471         case 3:
2472                 lyxerr.debug() << "Spacing: DOUBLE" << endl;
2473                 params->spacing.set(Spacing::Double);
2474                 break;
2475         case 4:
2476                 lyxerr.debug() << "Spacing: OTHER" << endl;
2477                 params->spacing.set(Spacing::Other, 
2478                                     fl_get_input(fd_form_document->input_spacing));
2479                 break;
2480         }
2481         if (tmpSpacing != params->spacing)
2482                 redo = true;
2483         
2484         signed char tmpchar =  
2485                 static_cast<signed char>(fl_get_counter_value(fd_form_document->slider_secnumdepth));
2486         if (params->secnumdepth != tmpchar)
2487                 redo = true;
2488         params->secnumdepth = tmpchar;
2489    
2490         params->tocdepth =  
2491                 static_cast<int>(fl_get_counter_value(fd_form_document->slider_tocdepth));
2492
2493         params->float_placement = 
2494                 fl_get_input(fd_form_document->input_float_placement);
2495
2496         // More checking should be done to ensure the string doesn't have
2497         // spaces or illegal placement characters in it. (thornley)
2498
2499         if (redo)
2500                 current_view->redoCurrentBuffer();
2501    
2502         current_view->owner()->getMiniBuffer()->Set(_("Document layout set"));
2503         current_view->buffer()->markDirty();
2504         
2505         params->options = 
2506                 fl_get_input(fd_form_document->input_extra);
2507 }
2508
2509
2510 extern "C" void DocumentCancelCB(FL_OBJECT *, long)
2511 {
2512         fl_hide_form(fd_form_document->form_document);
2513 }
2514
2515
2516 extern "C" void DocumentOKCB(FL_OBJECT * ob, long data)
2517 {
2518         DocumentCancelCB(ob, data);
2519         DocumentApplyCB(ob, data);
2520 }
2521
2522
2523 extern "C" void DocumentBulletsCB(FL_OBJECT *, long)
2524 {
2525         bulletForm();
2526         // bullet callbacks etc. in bullet_panel.C -- ARRae
2527 }
2528
2529 /* callbacks for form form_quotes */
2530
2531 extern "C" void QuotesApplyCB(FL_OBJECT *, long)
2532 {
2533         if (!current_view->available())
2534                 return;
2535         
2536         current_view->owner()->getMiniBuffer()->Set(_("Quotes type set"));
2537         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
2538         switch(fl_get_choice(fd_form_quotes->choice_quotes_language) - 1) {
2539         case 0:
2540                 lga = InsetQuotes::EnglishQ;
2541                 break;
2542         case 1:
2543                 lga = InsetQuotes::SwedishQ;
2544                 break;
2545         case 2:
2546                 lga = InsetQuotes::GermanQ;
2547                 break;
2548         case 3:
2549                 lga = InsetQuotes::PolishQ;
2550                 break;
2551         case 4:
2552                 lga = InsetQuotes::FrenchQ;
2553                 break;
2554         case 5:
2555                 lga = InsetQuotes::DanishQ;
2556                 break;
2557         }
2558         current_view->buffer()->params.quotes_language = lga;
2559         if (fl_get_button(fd_form_quotes->radio_single))   
2560                 current_view->buffer()->
2561                         params.quotes_times = InsetQuotes::SingleQ;
2562         else
2563                 current_view->buffer()->
2564                         params.quotes_times = InsetQuotes::DoubleQ;
2565 }
2566
2567
2568 extern "C" void QuotesCancelCB(FL_OBJECT *, long)
2569 {
2570         fl_hide_form(fd_form_quotes->form_quotes);
2571 }
2572
2573
2574 extern "C" void QuotesOKCB(FL_OBJECT * ob, long data)
2575 {
2576         QuotesApplyCB(ob, data);
2577         QuotesCancelCB(ob, data);
2578 }
2579 #else
2580 // this is needed for now!
2581 extern "C" void ChoiceClassCB(FL_OBJECT *, long) {}
2582 extern "C" void DocumentDefskipCB(FL_OBJECT *, long) {}
2583 extern "C" void DocumentSpacingCB(FL_OBJECT *, long) {}
2584 extern "C" void DocumentApplyCB(FL_OBJECT *, long) {}
2585 extern "C" void DocumentCancelCB(FL_OBJECT *, long) {}
2586 extern "C" void DocumentOKCB(FL_OBJECT *, long) {}
2587 extern "C" void DocumentBulletsCB(FL_OBJECT *, long) {}
2588 extern "C" void QuotesApplyCB(FL_OBJECT *, long) {}
2589 extern "C" void QuotesCancelCB(FL_OBJECT *, long) {}
2590 extern "C" void QuotesOKCB(FL_OBJECT *, long) {}
2591 #endif
2592
2593
2594 /* callbacks for form form_preamble */
2595
2596 extern "C" void PreambleCancelCB(FL_OBJECT *, long)
2597 {
2598         fl_hide_form(fd_form_preamble->form_preamble);
2599 }
2600
2601
2602 extern "C" void PreambleApplyCB(FL_OBJECT *, long)
2603 {
2604         if (!current_view->available())
2605                 return;
2606         
2607         current_view->buffer()->params.preamble = 
2608                 fl_get_input(fd_form_preamble->input_preamble);
2609         current_view->buffer()->markDirty();
2610         current_view->owner()->getMiniBuffer()->Set(_("LaTeX preamble set"));
2611 }
2612
2613    
2614 extern "C" void PreambleOKCB(FL_OBJECT * ob, long data)
2615 {
2616         PreambleApplyCB(ob, data);
2617         PreambleCancelCB(ob, data);
2618 }
2619
2620
2621 /* callbacks for form form_table */
2622
2623 extern "C"
2624 void TableApplyCB(FL_OBJECT *, long)
2625 {
2626         if (!current_view->available())
2627                 return;
2628
2629 #ifndef NEW_TABULAR
2630         // check for tables in tables
2631         if (current_view->text->cursor.par()->table){
2632                 WriteAlert(_("Impossible Operation!"),
2633                            _("Cannot insert table in table."),
2634                            _("Sorry."));
2635                 return;
2636         }
2637 #endif
2638         current_view->owner()->getMiniBuffer()->Set(_("Inserting table..."));
2639
2640         int ysize = int(fl_get_slider_value(fd_form_table->slider_columns) + 0.5);
2641         int xsize = int(fl_get_slider_value(fd_form_table->slider_rows) + 0.5);
2642    
2643    
2644         current_view->hideCursor();
2645         current_view->beforeChange();
2646         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2647    
2648         current_view->text->SetCursorParUndo(current_view->buffer()); 
2649         current_view->text->FreezeUndo();
2650
2651         current_view->text->BreakParagraph(current_view);
2652         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2653    
2654         if (current_view->text->cursor.par()->Last()) {
2655                 current_view->text->CursorLeft(current_view);
2656       
2657                 current_view->text->BreakParagraph(current_view);
2658                 current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2659         }
2660
2661         current_view->text->current_font.setLatex(LyXFont::OFF);
2662         //if (!fl_get_button(fd_form_table->check_latex)){
2663         // insert the new wysiwy table
2664         current_view->text->SetLayout(current_view, 0); // standard layout
2665 #ifndef NEW_INSETS
2666         if (current_view->text->cursor.par()->footnoteflag == 
2667             LyXParagraph::NO_FOOTNOTE) {
2668 #endif
2669                 current_view->text
2670                         ->SetParagraph(current_view, 0, 0,
2671                                        0, 0,
2672                                        VSpace (0.3 * current_view->buffer()->
2673                                                params.spacing.getValue(),
2674                                                LyXLength::CM),
2675                                        VSpace (0.3 * current_view->buffer()->
2676                                                params.spacing.getValue(),
2677                                                LyXLength::CM),
2678                                        LYX_ALIGN_CENTER,
2679                                        string(),
2680                                        0);
2681 #ifndef NEW_INSETS
2682         } else {
2683                 current_view->text
2684                         ->SetParagraph(current_view, 0, 0,
2685                                        0, 0,
2686                                        VSpace(VSpace::NONE),
2687                                        VSpace(VSpace::NONE),
2688                                        LYX_ALIGN_CENTER, 
2689                                        string(),
2690                                        0);
2691         }
2692 #endif
2693 #ifndef NEW_TABULAR
2694         current_view->text->cursor.par()->table =
2695                 new LyXTable(xsize, ysize);
2696 #endif
2697         Language const * lang = 
2698                 current_view->text->cursor.par()->getParLanguage(current_view->buffer()->params);
2699         LyXFont font(LyXFont::ALL_INHERIT, lang);
2700         for (int i = 0; i < xsize * ysize - 1; ++i) {
2701                 current_view->text->cursor.par()
2702                         ->InsertChar(0, LyXParagraph::META_NEWLINE, font);
2703         }
2704         current_view->text->RedoParagraph(current_view);
2705    
2706         current_view->text->UnFreezeUndo();
2707      
2708         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2709         current_view->owner()->getMiniBuffer()->Set(_("Table inserted"));
2710         current_view->setState();
2711 }
2712
2713
2714 extern "C"
2715 void TableCancelCB(FL_OBJECT *, long)
2716 {
2717         fl_hide_form(fd_form_table->form_table);
2718 }
2719
2720
2721 extern "C"
2722 void TableOKCB(FL_OBJECT * ob, long data)
2723 {
2724         TableApplyCB(ob, data);
2725         TableCancelCB(ob, data);
2726 }
2727
2728
2729 void Figure()
2730 {
2731         if (fd_form_figure->form_figure->visible) {
2732                 fl_raise_form(fd_form_figure->form_figure);
2733         } else {
2734                 fl_show_form(fd_form_figure->form_figure,
2735                              FL_PLACE_MOUSE, FL_FULLBORDER,
2736                              _("Insert Figure"));
2737         }
2738 }
2739
2740
2741 void Table()
2742 {
2743         if (fd_form_table->form_table->visible) {
2744                 fl_raise_form(fd_form_table->form_table);
2745         } else {
2746                 fl_show_form(fd_form_table->form_table,
2747                              FL_PLACE_MOUSE, FL_FULLBORDER,
2748                              _("Insert Table"));
2749         }
2750 }
2751
2752
2753 /* callbacks for form form_figure */
2754 extern "C"
2755 void FigureApplyCB(FL_OBJECT *, long)
2756 {
2757         if (!current_view->available())
2758                 return;
2759
2760         Buffer * buffer = current_view->buffer();
2761         if(buffer->isReadonly()) // paranoia
2762                 return;
2763         
2764         current_view->owner()->getMiniBuffer()->Set(_("Inserting figure..."));
2765         if (fl_get_button(fd_form_figure->radio_inline)
2766 #ifndef NEW_TABULAR
2767             || current_view->text->cursor.par()->table
2768 #endif
2769                 ) {
2770                 InsetFig * new_inset = new InsetFig(100, 20, buffer);
2771                 current_view->insertInset(new_inset);
2772                 current_view->owner()->getMiniBuffer()->Set(_("Figure inserted"));
2773                 new_inset->Edit(current_view, 0, 0, 0);
2774                 return;
2775         }
2776         
2777         current_view->hideCursor();
2778         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2779         current_view->beforeChange();
2780       
2781         current_view->text->SetCursorParUndo(current_view->buffer()); 
2782         current_view->text->FreezeUndo();
2783
2784         current_view->text->BreakParagraph(current_view);
2785         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2786       
2787         if (current_view->text->cursor.par()->Last()) {
2788                 current_view->text->CursorLeft(current_view);
2789          
2790                 current_view->text->BreakParagraph(current_view);
2791                 current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2792         }
2793
2794         // The standard layout should always be numer 0;
2795         current_view->text->SetLayout(current_view, 0);
2796
2797 #ifndef NEW_INSETS
2798         if (current_view->text->cursor.par()->footnoteflag == 
2799             LyXParagraph::NO_FOOTNOTE) {
2800 #endif
2801                 current_view->text->
2802                         SetParagraph(current_view, 0, 0,
2803                                      0, 0,
2804                                      VSpace (0.3 * buffer->params.spacing.getValue(),
2805                                              LyXLength::CM),
2806                                      VSpace (0.3 *
2807                                              buffer->params.spacing.getValue(),
2808                                              LyXLength::CM),
2809                                      LYX_ALIGN_CENTER, string(), 0);
2810 #ifndef NEW_INSETS
2811         } else {
2812                 current_view->text->SetParagraph(current_view, 0, 0,
2813                                                  0, 0,
2814                                                  VSpace(VSpace::NONE),
2815                                                  VSpace(VSpace::NONE),
2816                                                  LYX_ALIGN_CENTER, 
2817                                                  string(),
2818                                                  0);
2819         }
2820 #endif
2821         
2822         current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
2823       
2824         Inset * new_inset = new InsetFig(100, 100, buffer);
2825         current_view->insertInset(new_inset);
2826         new_inset->Edit(current_view, 0, 0, 0);
2827         current_view->update(BufferView::SELECT|BufferView::FITCUR);
2828         current_view->owner()->getMiniBuffer()->Set(_("Figure inserted"));
2829         current_view->text->UnFreezeUndo();
2830         current_view->setState();
2831 }
2832
2833
2834 extern "C" void FigureCancelCB(FL_OBJECT *, long)
2835 {
2836         fl_hide_form(fd_form_figure->form_figure);
2837 }
2838
2839
2840 extern "C" void FigureOKCB(FL_OBJECT * ob, long data)
2841 {
2842         FigureApplyCB(ob, data);
2843         FigureCancelCB(ob, data);
2844 }
2845
2846
2847 // This function runs "configure" and then rereads lyx.defaults to
2848 // reconfigure the automatic settings.
2849 void Reconfigure(BufferView * bv)
2850 {
2851         bv->owner()->getMiniBuffer()->Set(_("Running configure..."));
2852
2853         // Run configure in user lyx directory
2854         Path p(user_lyxdir);
2855         Systemcalls one(Systemcalls::System, 
2856                         AddName(system_lyxdir, "configure"));
2857         p.pop();
2858         bv->owner()->getMiniBuffer()->Set(_("Reloading configuration..."));
2859         lyxrc.read(LibFileSearch(string(), "lyxrc.defaults"));
2860         WriteAlert(_("The system has been reconfigured."), 
2861                    _("You need to restart LyX to make use of any"),
2862                    _("updated document class specifications."));
2863 }