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