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