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