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