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