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