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