]> git.lyx.org Git - lyx.git/blob - src/lyx_cb.C
Initial revision
[lyx.git] / src / lyx_cb.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1995 Matthias Ettrich,
7 *           Copyright (C) 1995-1998 The LyX Team.
8 *
9 *======================================================*/
10
11 #include <config.h>
12
13 #include <ctype.h>
14 #include <unistd.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <stdlib.h>
18
19 #include "LString.h"
20 #include "lyx_main.h"
21 #include FORMS_H_LOCATION
22 #include "lyx.h"
23 #include "layout_forms.h"
24 #include "bullet_forms.h"
25 #include "print_form.h"
26 #include "form1.h"
27 #include "spellchecker.h"
28 #include "version.h"
29 #include "lyx_cb.h"
30 #include "credits.h"
31 #include "insets/insetref.h"
32 #include "insets/insetquotes.h"
33 #include "insets/insetlatex.h"
34 #include "insets/insetlabel.h"
35 #include "insets/insetinfo.h"
36 #include "insets/insetspecialchar.h"
37 #include "insets/figinset.h"
38 #include "lyxfunc.h"
39 #include "latexoptions.h"
40 #include "lyxfont.h"
41 #include "minibuffer.h"
42 #include "combox.h"
43 #include "bufferlist.h"
44 #include "filetools.h"
45 #include "pathstack.h"
46 #include "filedlg.h"
47 #include "lyx_gui_misc.h"
48 #include "LyXView.h" // only because of form_main
49 #include "lastfiles.h"
50 #include "FileInfo.h"
51 #include "lyxscreen.h"
52 #include "error.h"
53 #include "syscall.h"
54 #include "lyxlib.h"
55 #include "lyxserver.h"
56 #include "FontLoader.h"
57 #include "lyxrc.h"
58 #include "lyxtext.h"
59 #include "gettext.h"
60
61 //      $Id: lyx_cb.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $    
62
63 #if !defined(lint) && !defined(WITH_WARNINGS)
64 static char vcid[] = "$Id: lyx_cb.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
65 #endif /* lint */
66
67 extern MiniBuffer *minibuffer;
68 extern Combox *combo_language;
69 extern BufferList bufferlist;
70 extern void show_symbols_form();
71 extern FD_form_main *fd_form_main;
72 extern FD_form_title *fd_form_title;
73 extern FD_form_paragraph *fd_form_paragraph;
74 extern FD_form_character *fd_form_character;
75 extern FD_form_document *fd_form_document;
76 extern FD_form_quotes *fd_form_quotes;
77 extern FD_form_preamble *fd_form_preamble;
78 extern FD_form_table *fd_form_table;
79 extern FD_form_print *fd_form_print;
80 extern FD_form_figure *fd_form_figure;
81 extern FD_form_screen *fd_form_screen;
82 extern FD_form_toc *fd_form_toc;
83 extern FD_form_ref *fd_form_ref;
84 extern FD_LaTeXOptions *fd_latex_options;
85 extern FD_form_bullet *fd_form_bullet;
86
87 extern BufferView *current_view; // called too many times in this file...
88
89 extern void DeleteSimpleCutBuffer(); /* for the cleanup when exiting */
90
91 extern bool send_fax(LString const &fname, LString const &sendcmd);
92
93 extern LyXServer *lyxserver;
94 extern FontLoader fontloader;
95
96 // this should be static, but I need it in buffer.C
97 bool quitting;  // flag, that we are quitting the program
98 extern bool finished; // all cleanup done just let it run through now.
99
100 char ascii_type; /* for selection notify callbacks */
101
102 bool scrolling = false;
103
104 char updatetimer = 0;
105
106 /* whether the work area should get callbacks */ 
107 bool input_prohibited = false;
108
109 /* the selection possible is needed, that only motion events are 
110 * used, where the bottom press event was on the drawing area too */
111 bool selection_possible = false;
112
113 // This is used to make the dreaded font toggle problem hopefully go
114 // away. Definitely not the best solution, but I think it sorta works.
115 bool toggleall = true;
116
117 void InsertCorrectQuote();
118
119
120 /* 
121    This is the inset locking stuff needed for mathed --------------------
122
123    an inset can simple call LockInset in it's edit call and *ONLY* in it's
124    edit call.
125    Inset::Edit() can only be called by the main lyx module.
126
127    Then the inset may modify the menu's and/or iconbars. 
128
129    Unlocking is either done by LyX or the inset itself with a UnlockInset-call
130
131    During the lock, all button and keyboard events will be modified
132    and send to the inset through the following inset-features. Note that
133    Inset::InsetUnlock will be called from inside UnlockInset. It is meant
134    to contain the code for restoring the menus and things like this.
135
136    
137    virtual void InsetButtonPress(int x, int y, int button);
138    virtual void InsetButtonRelease(int x, int y, int button);
139    virtual void InsetKeyPress(XKeyEvent *ev);
140    virtual void InsetMotionNotify(int x, int y, int state);
141    virtual void InsetUnlock();
142
143    If a inset wishes any redraw and/or update it just has to call
144    UpdateInset(this).
145    It's is completly irrelevant, where the inset is. UpdateInset will
146    find it in any paragraph in any buffer. 
147    Of course the_locking_inset and the insets in the current paragraph/buffer
148    are checked first, so no performance problem should occur.
149    
150    Hope that's ok for the beginning, Alejandro,
151    sorry that I needed so much time,
152
153                   Matthias
154    */
155
156 void UpdateInset(Inset* inset, bool mark_dirty = true);
157 /* these functions return 1 if an error occured, 
158    otherwise 0 */
159 // Now they work only for updatable insets. [Alejandro 080596]
160 int LockInset(UpdatableInset* inset);
161 void ToggleLockedInsetCursor(long x, long y, int asc, int desc);
162 void FitLockedInsetCursor(long x, long y, int asc, int desc);
163 int UnlockInset(UpdatableInset* inset);
164 void LockedInsetStoreUndo(Undo::undo_kind kind);
165
166 /* this is for asyncron updating. UpdateInsetUpdateList will be called
167    automatically from LyX. Just insert the Inset into the Updatelist */
168 void UpdateInsetUpdateList();
169 void PutInsetIntoInsetUpdateList(Inset* inset);
170
171 InsetUpdateStruct *InsetUpdateList = NULL;
172
173
174 /*
175   -----------------------------------------------------------------------
176  */
177
178 /* some function prototypes */
179
180 void GotoNote();
181 void OpenStuff();
182 void ToggleFloat();
183 void MenuUndo();
184 void MenuRedo();
185 void HyphenationPoint();
186 void MenuSeparator();
187 void HFill();
188 void Newline();
189 void ProtectedBlank();
190 void CopyCB();
191 int RunLinuxDoc(int, LString const &);
192 int RunDocBook(int, LString const &);
193 void MenuWrite(Buffer* buf);
194 void MenuWriteAs(Buffer *buffer);
195 void MenuReload(Buffer *buf);
196 void MenuLayoutSave();
197
198 unsigned char GetCurrentTextClass()
199         // Who are we asking?
200         // Shouldn't this question be directed to the buffer?
201         // Indeed it should. Asger.
202 {
203         return current_view->currentBuffer()->params.textclass;
204 }
205
206
207 // How should this actually work? Should it prohibit input in all BufferViews,
208 // or just in the current one? If "just the current one", then it should be
209 // placed in BufferView. If "all BufferViews" then LyXGUI (I think) should
210 // run "ProhibitInput" on all LyXViews which will run prohibitInput on all
211 // BufferViews. Or is it perhaps just the (input in) BufferViews in the
212 // current LyxView that should be prohibited (Lgb) (This applies to
213 // "AllowInput" as well.)
214 void ProhibitInput()
215 {
216         input_prohibited = true;
217         if (current_view->getScreen())
218                 current_view->getScreen()->HideCursor();
219
220         static Cursor cursor;
221         static bool cursor_undefined = true;
222    
223         if (cursor_undefined){
224                 cursor = XCreateFontCursor(fl_display, XC_watch);
225                 XFlush(fl_display);
226                 cursor_undefined = false;
227         }
228    
229         /* set the cursor to the watch for all forms and the canvas */ 
230         XDefineCursor(fl_display, fd_form_main->form_main->window, cursor);
231         if (fd_form_paragraph->form_paragraph->visible)
232                 XDefineCursor(fl_display,
233                               fd_form_paragraph->form_paragraph->window,
234                               cursor);
235         if (fd_form_character->form_character->visible)
236                 XDefineCursor(fl_display,
237                               fd_form_character->form_character->window,
238                               cursor);
239
240         XFlush(fl_display);
241         fl_deactivate_all_forms();
242 }
243
244
245 // Should find a way to move this into BufferView.C
246 void SetXtermCursor(Window win)
247 {
248         static Cursor cursor;
249         static char cursor_undefined = 1;
250         if (cursor_undefined){
251                 cursor = XCreateFontCursor(fl_display, XC_xterm);
252                 XFlush(fl_display);
253                 cursor_undefined = 0;
254         }
255         XDefineCursor(fl_display, win, cursor);
256         XFlush(fl_display);
257 }
258
259
260 void AllowInput()
261 {
262         input_prohibited = false;
263
264         /* reset the cursor from the watch for all forms and the canvas */
265    
266         XUndefineCursor(fl_display, fd_form_main->form_main->window);
267         if (fd_form_paragraph->form_paragraph->visible)
268                 XUndefineCursor(fl_display,
269                                 fd_form_paragraph->form_paragraph->window);
270         if (fd_form_character->form_character->visible)
271                 XUndefineCursor(fl_display,
272                                 fd_form_character->form_character->window);
273         if (current_view->getWorkArea()->belowmouse)
274                 SetXtermCursor(fd_form_main->form_main->window);
275
276         XFlush(fl_display);
277         fl_activate_all_forms();
278 }
279
280
281 void FreeUpdateTimer()
282 {
283         /* a real free timer would be better but I don't know 
284          * how to do this with xforms */
285         updatetimer = 0;
286 }
287
288
289 void SetUpdateTimer(float time)
290 {
291         fl_set_timer(fd_form_main->timer_update, time);
292         updatetimer = 1;
293 }
294
295
296 void BeforeChange()
297 {
298         current_view->getScreen()->ToggleSelection();
299         current_view->currentBuffer()->text->ClearSelection();
300         FreeUpdateTimer();
301 }
302
303
304 void SmallUpdate(signed char f)
305 {
306         current_view->getScreen()->SmallUpdate();
307         if (current_view->getScreen()->TopCursorVisible()
308             != current_view->getScreen()->first){
309                 current_view->currentBuffer()->update(f);
310                 return;
311         }
312
313         current_view->fitCursor();
314         current_view->updateScrollbar();
315    
316         if (!current_view->currentBuffer()->text->selection)
317                 current_view->currentBuffer()->text->sel_cursor = 
318                         current_view->currentBuffer()->text->cursor;
319
320         if (f==1 || f==-1) {
321                 if (current_view->currentBuffer()->isLyxClean()) {
322                         current_view->currentBuffer()->markDirty();
323                         minibuffer->setTimer(4);
324                 }
325                 else {
326                         current_view->currentBuffer()->markDirty();
327                 }
328         }
329 }
330
331
332 //
333 // Menu callbacks
334 //
335
336 //
337 // File menu
338 //
339
340 // should be moved to lyxfunc.C
341 void MenuWrite(Buffer* buf)
342 {
343         XFlush(fl_display);
344         if (!bufferlist.write(buf)) {
345                 LString fname = buf->getFileName();
346                 LString s = MakeAbsPath(fname);
347                 if (AskQuestion(_("Save failed. Rename and try again?"),
348                                  MakeDisplayPath(s,50),
349                                  _("(If not, document is not saved.)"))) {
350                         MenuWriteAs(buf);
351                 }
352         } else {
353                 lastfiles->newFile(buf->getFileName());
354         }
355 }
356
357
358 // should be moved to BufferView.C
359 void MenuWriteAs(Buffer *buffer)
360 {
361         if (!buffer->text) return;
362
363         LString fname = buffer->getFileName();
364         LString oldname = fname;
365         LyXFileDlg fileDlg;
366
367         ProhibitInput();
368         fileDlg.SetButton(0, _("Documents"), lyxrc->document_path);
369         fileDlg.SetButton(1, _("Templates"), lyxrc->template_path);
370
371         if (!IsLyXFilename(fname))
372                 fname += ".lyx";
373
374         fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
375                                OnlyPath(fname),
376                                "*.lyx", 
377                                OnlyFilename(fname));
378         AllowInput();
379
380         if (fname.empty()) {
381                 minibuffer->Set(_("Canceled."));
382                 return;
383         }
384
385         // Make sure the absolute filename ends with appropriate suffix
386         LString s= MakeAbsPath(fname);
387         if (!IsLyXFilename(s))
388                 s += ".lyx";
389
390         // Same name as we have already?
391         if (s == oldname) {
392                 if (!AskQuestion(_("Same name as document already has:"),
393                                  MakeDisplayPath(s,50),
394                                  _("Save anyway?")))
395                         return;
396                 // Falls through to name change and save
397         } 
398         // No, but do we have another file with this name open?
399         else if (bufferlist.exists(s)) {
400                 if (AskQuestion(_("Another document with same name open!"),
401                                 MakeDisplayPath(s,50),
402                                 _("Replace with current document?")))
403                         {
404                                 bufferlist.close(bufferlist.getBuffer(s));
405
406                                 // Ok, change the name of the buffer, but don't save!
407                                 buffer->setFileName(s);
408                                 buffer->markDirty();
409
410                                 minibuffer->Set(_("Document renamed to '"),
411                                                 MakeDisplayPath(s),
412                                                 _("', but not saved..."));
413                         }
414                 return;
415         } // Check whether the file exists
416         else {
417                 FileInfo myfile(s);
418                 if (myfile.isOK() && !AskQuestion(_("Document already exists:"), 
419                                                   MakeDisplayPath(s,50),
420                                                   _("Replace file?")))
421                         return;
422         }
423
424         // Ok, change the name of the buffer
425         buffer->setFileName(s);
426         buffer->markDirty();
427         // And save
428         // Small bug: If the save fails, we have irreversible changed the name
429         // of the document.
430         MenuWrite(buffer);
431 }    
432
433 #if 0
434 extern bool gsworking();
435 #endif
436
437 int MenuRunLaTeX(Buffer *buffer)
438 {
439         int ret = 0;
440
441 #if 0
442         if (gsworking()) {
443                 WriteAlert(_("Sorry, can't do this while pictures are being rendered."),
444                            _("Please wait a few seconds for this to finish and try again."),
445                            _("(or kill runaway gs processes by hand and try again.)"));
446                 return 1;
447         }
448         extern pid_t isp_pid; // from spellchecker.C
449         if(isp_pid != -1)
450         {
451           WriteAlert(_("Can't do this while the spellchecker is running."),
452             _("Stop the spellchecker first."));
453           return 1;
454         }
455 #endif
456         
457         if (buffer->isLinuxDoc())
458                 ret = RunLinuxDoc(1, buffer->getFileName());
459         else if (buffer->isLiterate())
460                 ret = buffer->runLiterate();
461         else if (buffer->isDocBook())
462                 ret = RunDocBook(1, buffer->getFileName());
463         else
464                 ret = buffer->runLaTeX();
465    
466         if (ret > 0) {
467                 LString s;
468                 LString t;
469                 if (ret == 1) {
470                         s = _("One error detected");
471                         t = _("You should try to fix it.");
472                 } else {
473                         s += ret;
474                         s += _(" errors detected.");
475                         t = _("You should try to fix them.");
476                 }
477                 WriteAlert(_("There were errors during the LaTeX run."), s, t);
478         }
479         return ret;
480 }
481
482
483 int MenuBuildProg(Buffer *buffer)
484 {
485        int ret = 0;
486
487        if (buffer->isLiterate())
488                ret = buffer->buildProgram();
489        else {
490                LString s;
491                LString t;
492                s = _("Wrong type of document");
493                t = _("The Build operation is not allowed in this document");
494                WriteAlert(_("There were errors during the Build process."), s, t);
495                return 1;
496        }
497    
498        if (ret > 0) {
499                LString s;
500                LString t;
501                if (ret == 1) {
502                        s = _("One error detected");
503                        t = _("You should try to fix it.");
504                } else {
505                        s += ret;
506                        s += _(" errors detected.");
507                        t = _("You should try to fix them.");
508                }
509                WriteAlert(_("There were errors during the Build process."), s, t);
510        }
511        return ret;
512 }
513
514
515 int MenuRunChktex(Buffer *buffer)
516 {
517         int ret;
518
519         if (buffer->isSGML()) {
520                 WriteAlert(_("Chktex does not work with SGML derived documents."));
521                 return 0;
522         } else 
523                 ret = buffer->runChktex();
524    
525         if (ret >= 0) {
526                 LString s;
527                 LString t;
528                 if (ret == 0) {
529                         s = _("No warnings found.");
530                 } else if (ret == 1) {
531                         s = _("One warning found.");
532                         t = _("Use 'Edit->Go to Error' to find it.");
533                 } else {
534                         s += ret;
535                         s += _(" warnings found.");
536                         t = _("Use 'Edit->Go to Error' to find them.");
537                 }
538                 WriteAlert(_("Chktex run successfully"), s, t);
539         } else {
540                 WriteAlert(_("Error!"),_("It seems chktex does not work."));
541         }
542         return ret;
543 }
544
545  
546 int MakeDVIOutput(Buffer *buffer)
547 {
548         if (!(buffer->text))
549                 return 1;
550
551         int ret = 0;
552
553         LString path = OnlyPath(buffer->getFileName());
554         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
555                 path = buffer->tmppath;
556         }
557         if (!buffer->isDviClean()) {
558                 PathPush(path);
559                 ret = MenuRunLaTeX(buffer);
560                 PathPop();
561         }
562         return ret;
563 }
564
565
566 /* wait == false means don't wait for termination */
567 /* wait == true means wait for termination       */
568 // The bool should be placed last on the argument line. (Lgb)
569 // Returns false if we fail.
570 bool RunScript(Buffer *buffer, bool wait,
571                LString const & command, LString const & orgname = LString(),
572                bool need_shell=true)
573 {
574         LString path;
575         LString cmd;
576         LString name= orgname;
577         int result = 0;
578         
579         if (MakeDVIOutput(buffer) > 0)
580                 return false;
581         /* get DVI-Filename */
582         if (name.empty())
583                 name = ChangeExtension(buffer->getFileName(),
584                                        ".dvi", true);
585
586         path = OnlyPath(name);
587         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
588                 path = buffer->tmppath;
589         }
590         PathPush(path);
591
592         cmd = command + ' ' + SpaceLess(name);
593         Systemcalls one;
594
595         if (need_shell) {
596 #ifndef __EMX__
597                 if (!wait)
598                         cmd += " &";
599 #else
600                 // OS/2 cmd.exe has another use for '&'
601                 if (!wait) {
602                         // This is not NLS safe, but it's OK, I think.
603                         LString sh = OnlyFilename(getEnvPath("EMXSHELL"));
604                         if (sh.empty()) {
605                                 // COMSPEC is set, unless user unsets 
606                                 sh = OnlyFilename(getEnvPath("COMSPEC"));
607                                 if (sh.empty())
608                                         sh = "cmd.exe";
609                         }
610                         sh.lowercase();
611                         if (sh.contains("cmd.exe") || sh.contains("4os2.exe"))
612                                 cmd = "start /min/n " + cmd;
613                         else
614                                 cmd += " &";
615                 }
616 #endif
617                 // It seems that, if wait is false, we never get back
618                 // the return code of the command. This means that all
619                 // the code I added in PrintApplyCB is currently
620                 // useless...
621 #ifdef WITH_WARNINGS
622 #warning What should we do here?
623 #endif          
624                 minibuffer->Set(_("Executing command:"), cmd);
625                 result = one.Startscript(Systemcalls::System, cmd);
626         } else {
627                 minibuffer->Set(_("Executing command:"), cmd);
628                 result = one.Startscript(wait ? Systemcalls::Wait
629                                         : Systemcalls::DontWait, cmd);
630         }
631         PathPop();
632         return (result==0);
633 }
634
635
636 // Returns false if we fail
637 bool MenuRunDvips(Buffer *buffer, bool wait=false)
638 {
639         if (!buffer->text)
640                 return false;
641
642         ProhibitInput();
643
644         // Generate dvi file
645         if (MakeDVIOutput(buffer) > 0) {
646                 AllowInput();
647                 return false;
648         }
649         // Generate postscript file
650         LString ps = ChangeExtension (buffer->getFileName(),
651                                       ".ps_tmp", true);
652
653         LString paper;
654         
655         char real_papersize = buffer->params.papersize;
656         if (real_papersize == PAPER_DEFAULT)
657                 real_papersize = lyxrc->default_papersize;
658
659         switch (real_papersize) {
660         case PAPER_USLETTER:
661                 paper = "letter";
662                 break;
663         case PAPER_A3PAPER:
664                 paper = "a3";
665                 break;
666         case PAPER_A4PAPER:
667                 paper = "a4";
668                 break;
669         case PAPER_A5PAPER:
670                 paper = "a5";
671                 break;
672         case PAPER_B5PAPER:
673                 paper = "b5";
674                 break;
675         case PAPER_EXECUTIVEPAPER:
676                 paper = "foolscap";
677                 break;
678         case PAPER_LEGALPAPER:
679                 paper = "legal";
680                 break;
681         default: /* If nothing else fits, keep an empty value... */
682                 break;
683         }
684
685         // Make postscript file.
686         LString command = "dvips " + lyxrc->print_to_file + ' ';
687         command += SpaceLess(ps);
688         if (buffer->params.use_geometry
689             && buffer->params.papersize2 == VM_PAPER_CUSTOM
690             && !lyxrc->print_paper_dimension_flag.empty()
691             && !buffer->params.paperwidth.empty()
692             && !buffer->params.paperheight.empty()) {
693                 // using a custom papersize
694                 command += ' ';
695                 command += lyxrc->print_paper_dimension_flag + ' ';
696                 command += buffer->params.paperwidth + ',';
697                 command += buffer->params.paperheight;
698         } else if (!paper.empty()
699                    && (real_papersize != PAPER_USLETTER ||
700                        buffer->params.orientation == ORIENTATION_PORTRAIT)) {
701                 // dvips won't accept -t letter -t landscape.  In all other
702                 // cases, include the paper size explicitly.
703                 command += ' ';
704                 command += lyxrc->print_paper_flag + ' ' + paper;
705         }
706         if (buffer->params.orientation == ORIENTATION_LANDSCAPE) {
707                 command += ' ';
708                 command += lyxrc->print_landscape_flag;
709         }
710         // push directorypath, if necessary 
711         LString path = OnlyPath(buffer->getFileName());
712         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)){
713                 path = buffer->tmppath;
714         }
715         PathPush(path);
716         bool ret = RunScript(buffer, wait, command);
717         AllowInput();
718         PathPop();
719         return ret;
720 }
721
722
723 // Returns false if we fail
724 bool MenuPreviewPS(Buffer *buffer)
725 {
726         if (!buffer->text)
727                 return false;
728
729         // Generate postscript file
730         if (!MenuRunDvips(buffer, true)) {
731                 return false;
732         }
733
734         // Start postscript viewer
735         ProhibitInput();
736         LString ps = ChangeExtension (buffer->getFileName(),
737                                       ".ps_tmp", true);
738         // push directorypath, if necessary 
739         LString path = OnlyPath(buffer->getFileName());
740         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)){
741                 path = buffer->tmppath;
742         }
743         PathPush(path);
744         bool ret = RunScript(buffer, false, lyxrc->view_ps_command, ps);
745         PathPop();
746         AllowInput();
747         return ret;
748 }
749
750
751 void MenuFax(Buffer *buffer)
752 {
753         if (!buffer->text)
754                 return;
755
756         // Generate postscript file
757         if (!MenuRunDvips(buffer, true)) {
758                 return;
759         }
760
761         // Send fax
762         LString ps = ChangeExtension (buffer->getFileName(), ".ps_tmp", true);
763         LString path = OnlyPath (buffer->getFileName());
764         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
765                 path = buffer->tmppath;
766         }
767         PathPush(path);
768         if (!lyxrc->fax_program.empty()) {
769                 LString help2 = lyxrc->fax_program;
770                 help2.subst("$$FName",ps);
771                 help2 += " &";
772                 Systemcalls one(Systemcalls::System, help2);
773         } else
774                 send_fax(ps,lyxrc->fax_command);
775         PathPop();
776 }
777
778
779 // Returns false if we fail
780 bool MenuPreview(Buffer *buffer)
781 {
782         if (!buffer->text)
783                 return false;
784    
785         LString paper;
786         
787         char real_papersize = buffer->params.papersize;
788         if (real_papersize == PAPER_DEFAULT)
789                 real_papersize = lyxrc->default_papersize;
790    
791         switch (real_papersize) {
792         case PAPER_USLETTER:
793                 paper = "us";
794                 break;
795         case PAPER_A3PAPER:
796                 paper = "a3";
797                 break;
798         case PAPER_A4PAPER:
799                 paper = "a4";
800                 break;
801         case PAPER_A5PAPER:
802                 paper = "a5";
803                 break;
804         case PAPER_B5PAPER:
805                 paper = "b5";
806                 break;
807         case PAPER_EXECUTIVEPAPER:
808                 paper = "foolscap";
809                 break;
810         case PAPER_LEGALPAPER:
811                 paper = "legal";
812                 break;
813         default: /* If nothing else fits, keep the empty value */
814                 break;
815         }
816    
817         if (paper.empty()) {
818                 if (buffer->params.orientation == ORIENTATION_LANDSCAPE)
819                         // we HAVE to give a size when the page is in
820                         // landscape, so use USletter.          
821                         paper = " -paper usr";
822         } else {
823                 paper = " -paper " + paper;
824                 if (buffer->params.orientation == ORIENTATION_LANDSCAPE)
825                         paper+='r';
826         }
827
828         // push directorypath, if necessary 
829         LString path = OnlyPath(buffer->getFileName());
830         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)){
831                 path = buffer->tmppath;
832         }
833         PathPush(path);
834         // Run dvi-viewer
835         LString command = lyxrc->view_dvi_command + paper ;
836         bool ret = RunScript(buffer, false, command);
837         PathPop();
838         return ret;
839 }
840
841
842 void MenuMakeLaTeX(Buffer *buffer)
843 {
844         if (buffer->text) {
845                 // Get LaTeX-Filename
846                 LString s = SpaceLess(ChangeExtension(
847                                                 buffer->getFileName(),
848                                                 ".tex", false));
849
850                 FilePtr myfile(s, FilePtr::read);
851                 if (myfile() &&
852                     !AskQuestion(_("File already exists:"), 
853                                 MakeDisplayPath(s,50),
854                                 _("Do you want to overwrite the file?"))) {
855                         minibuffer->Set(_("Canceled"));
856                         return;
857                 }
858
859                 if (buffer->isDocBook())
860                         minibuffer->Set(_("DocBook does not have a latex backend"));
861                 else {
862                         if (buffer->isLinuxDoc())
863                                 RunLinuxDoc(0, buffer->getFileName());
864                         else
865                                 buffer->makeLaTeXFile(s, LString(), true);
866                         minibuffer->Set(_("Nice LaTeX file saved as"),
867                                         MakeDisplayPath(s));
868                         buffer->markDviDirty();
869                 }
870         }
871 }
872
873
874 void MenuMakeLinuxDoc(Buffer *buffer)
875 {
876         if (buffer->text) {
877
878                 if (!buffer->isLinuxDoc()) {
879                         WriteAlert(_("Error!"), _("Document class must be linuxdoc."));
880                         return;
881                 }
882
883                 // Get LinuxDoc-Filename
884                 LString s = ChangeExtension (buffer->getFileName(), 
885                                              ".sgml", false);
886
887                 FilePtr myfile(s, FilePtr::read);
888                 if (myfile() &&
889                     !AskQuestion(_("File already exists:"), 
890                                 MakeDisplayPath(s,50),
891                                 _("Do you want to overwrite the file?"))) {
892                         minibuffer->Set(_("Canceled"));
893                         return;
894                 }
895
896                 minibuffer->Set(_("Building LinuxDoc SGML file `"),
897                                 MakeDisplayPath(s), "'..."); 
898
899                 buffer->makeLinuxDocFile(s, 65);
900                 buffer->redraw();
901                 minibuffer->Set(_("LinuxDoc SGML file save as"),
902                                 MakeDisplayPath(s)); 
903         }
904 }
905
906
907 void MenuMakeDocBook(Buffer *buffer)
908 {
909         if (buffer->text) {
910
911                 if (!buffer->isDocBook()) {
912                         WriteAlert(_("Error!"), _("Document class must be docbook."));
913                         return;
914                 }
915
916                 // Get DocBook-Filename
917                 LString s = ChangeExtension (buffer->getFileName(), 
918                                              ".sgml", false);
919
920                 FilePtr myfile(s, FilePtr::read);
921                 if (myfile() &&
922                     !AskQuestion(_("File already exists:"), 
923                                 MakeDisplayPath(s,50),
924                                 _("Do you want to overwrite the file?"))) {
925                         minibuffer->Set(_("Canceled"));
926                         return;
927                 }
928
929                 minibuffer->Set(_("Building DocBook SGML file `"),
930                                 MakeDisplayPath(s), "'..."); 
931
932                 buffer->makeDocBookFile(s, 65);
933                 buffer->redraw();
934                 minibuffer->Set(_("DocBook SGML file save as"),
935                                 MakeDisplayPath(s)); 
936         }
937 }
938
939
940 void MenuMakeAscii(Buffer *buffer)
941 {
942         if (buffer->text) {
943                 /* get LaTeX-Filename */
944                 LString s = ChangeExtension (buffer->getFileName(),
945                                              ".txt", false);
946
947                 FilePtr myfile(s, FilePtr::read);
948                 if (myfile() &&
949                     !AskQuestion(_("File already exists:"), 
950                                 MakeDisplayPath(s,50),
951                                 _("Do you want to overwrite the file?"))) {
952                         minibuffer->Set(_("Canceled"));
953                         return;
954                 }
955
956                 buffer->writeFileAscii(s, lyxrc->ascii_linelen);
957
958                 minibuffer->Set(_("Ascii file saved as"), MakeDisplayPath(s));
959         }
960 }
961
962
963 void MenuPrint(Buffer *buffer)
964 {
965         LString input_file;
966
967         if (!buffer->text) 
968                 return;
969         
970         input_file = ChangeExtension(buffer->getFileName(),
971                                      lyxrc->print_file_extension,
972                                      true);
973         fl_set_input(fd_form_print->input_file, input_file.c_str());
974         
975         if (fd_form_print->form_print->visible) {
976                 fl_raise_form(fd_form_print->form_print);
977         } 
978         else {
979                 fl_show_form(fd_form_print->form_print,
980                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
981                              _("Print"));
982         }
983 }
984
985
986 void QuitLyX()
987 {
988         lyxerr.debug("Running QuitLyX.");
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);
1004
1005         DestroyLyXTmpDir(system_tempdir);
1006
1007         finished = true;
1008 }
1009
1010
1011
1012 void AutoSave()
1013         // should probably be moved into BufferList (Lgb)
1014         // Perfect target for a thread...
1015 {
1016         if (!current_view->getScreen() || !current_view->available())
1017                 return;
1018
1019         if (current_view->currentBuffer()->isBakClean()
1020             || current_view->currentBuffer()->isReadonly()) {
1021                 // We don't save now, but we'll try again later
1022                 current_view->getOwner()->resetAutosaveTimer();
1023                 return;
1024         }
1025
1026         minibuffer->Set(_("Autosaving current document..."));
1027         
1028         // create autosave filename
1029         LString fname = OnlyPath(current_view->currentBuffer()->getFileName());
1030         fname += "#";
1031         fname += OnlyFilename(current_view->currentBuffer()->getFileName());
1032         fname += "#";
1033         
1034         // tmp_ret will be located (usually) in /tmp
1035         // will that be a problem?
1036         LString tmp_ret = tmpnam(NULL);
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                         current_view->currentBuffer()->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 (!current_view->currentBuffer()->writeFile(fname,
1063                                                                       1)) {
1064                                 // It is dangerous to do this in the child,
1065                                 // but safe in the parent, so...
1066                                 if (pid == -1)
1067                                         minibuffer->Set(_("Autosave Failed!"));
1068                         }
1069                 }
1070                 if (pid == 0) { // we are the child so...
1071                         _exit(0);
1072                 }
1073         }
1074         
1075         current_view->currentBuffer()->markBakClean();
1076         current_view->getOwner()->resetAutosaveTimer();
1077 }
1078
1079
1080 //
1081 // (c) CHT Software Service GmbH
1082 // Uwe C. Schroeder
1083 //
1084 // create new file with template
1085 // SERVERCMD !
1086 //
1087 Buffer * NewLyxFile(LString const & filename)
1088 {
1089         // Split argument by :
1090         LString tmpname = filename;
1091         LString name;
1092         tmpname.split(name, ':');
1093 #ifdef __EMX__ // Fix me! lyx_cb.C may not be low level enough to allow this.
1094         if (name.length() == 1 && isalpha((unsigned char) name[0]) &&
1095             (tmpname.prefixIs("/") || tmpname.prefixIs("\\"))) {
1096                 name += ':';
1097                 name += tmpname.token(':');
1098                 tmpname.split(':');
1099         }
1100 #endif
1101         lyxerr.debug("Arg is " + filename);
1102         lyxerr.debug("Name is " + name);
1103         lyxerr.debug("Template is " + tmpname);
1104
1105         // find a free buffer 
1106         Buffer *tmpbuf = bufferlist.newFile(name,tmpname);
1107         if (tmpbuf)
1108                 lastfiles->newFile(tmpbuf->getFileName());
1109         return tmpbuf;
1110 }
1111
1112
1113 // Insert ascii file (if filename is empty, prompt for one)
1114 void InsertAsciiFile(LString const & f, bool asParagraph)
1115 {
1116         LString fname = f;
1117         LyXParagraph *tmppar;
1118         LyXFileDlg fileDlg;
1119  
1120         if (!current_view->getScreen()) return;
1121      
1122         if (fname.empty()) {
1123                 ProhibitInput();
1124                 fname = fileDlg.Select(_("File to Insert"), 
1125                                        current_view->getOwner()->currentBuffer()->filepath,
1126                                        "*");
1127                 AllowInput();
1128                 if (fname.empty()) return;
1129         }
1130
1131         FileInfo fi(fname);
1132         FilePtr myfile(fname, FilePtr::read);
1133
1134         if (!fi.exist() || !fi.readable() || !myfile()) {
1135                 WriteFSAlert(_("Error! Cannot open specified file:"),
1136                              MakeDisplayPath(fname,50));
1137                 return;
1138         }
1139         
1140         tmppar = new LyXParagraph();
1141         tmppar->readSimpleWholeFile(myfile);
1142         
1143         // set the end of the string
1144         tmppar->InsertChar(tmppar->last-1,'\0');
1145       
1146         // insert the string
1147         current_view->getScreen()->HideCursor();
1148       
1149         // clear the selection
1150         BeforeChange();
1151         if (!asParagraph)
1152                 current_view->currentBuffer()->text->InsertStringA(tmppar->text);
1153         else
1154                 current_view->currentBuffer()->text->InsertStringB(tmppar->text);
1155         delete tmppar;
1156         current_view->currentBuffer()->update(1);
1157 }
1158
1159
1160 void MenuShowTableOfContents()
1161 {
1162         TocUpdateCB(NULL, 0);
1163         if (fd_form_toc->form_toc->visible) {
1164                 fl_raise_form(fd_form_toc->form_toc);
1165         } else {
1166                 fl_show_form(fd_form_toc->form_toc,
1167                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
1168                              _("Table Of Contents"));
1169         }
1170 }
1171
1172
1173 void MenuInsertLabel(const char *arg)
1174 {
1175         LString label = arg;
1176         ProhibitInput();
1177         //LString label = fl_show_input(_("Enter new label to insert:"),"");
1178         if (label.empty())
1179                 label = askForText(_("Enter new label to insert:"));
1180         label.strip();
1181         label.frontStrip();
1182         if (!label.empty()) {
1183                 InsetLabel *new_inset = new InsetLabel;
1184                 new_inset->setContents(label);
1185                 current_view->currentBuffer()->insertInset(new_inset);
1186         }
1187         AllowInput();
1188 }
1189
1190
1191 void MenuInsertRef()
1192 {
1193         RefUpdateCB(NULL, 0);
1194         if (fd_form_ref->form_ref->visible) {
1195                 fl_raise_form(fd_form_ref->form_ref);
1196         } else {
1197                 fl_show_form(fd_form_ref->form_ref,
1198                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
1199                              _("Insert Reference"));
1200         }
1201 }
1202
1203
1204 void MenuPasteSelection(char at)
1205 {
1206         if (!current_view->getScreen())
1207                 return;
1208
1209         ascii_type = at;
1210   
1211         Atom data_prop = XInternAtom(fl_display, 
1212                                      "LyX_Primary",
1213                                      false);
1214         if (data_prop == None) 
1215                 return;
1216         XConvertSelection(fl_display,
1217                           XA_PRIMARY, XA_STRING, data_prop, 
1218                           fd_form_main->form_main->window, 0);
1219         XFlush(fl_display);
1220 }
1221
1222
1223 void FootCB(FL_OBJECT*, long)
1224 {
1225         if (!current_view->available()) 
1226                 return;
1227         
1228         minibuffer->Set(_("Inserting Footnote..."));
1229         current_view->getScreen()->HideCursor();
1230         current_view->currentBuffer()->update(-2);
1231         current_view->currentBuffer()->text->InsertFootnoteEnvironment(LyXParagraph::FOOTNOTE);
1232         current_view->currentBuffer()->update(1);
1233 }
1234
1235
1236 void LayoutsCB(int sel, void *)
1237 {
1238         LString tmp;
1239         tmp += sel;
1240         current_view->getOwner()->getLyXFunc()->Dispatch(LFUN_LAYOUTNO,
1241                                                          tmp.c_str());
1242 }
1243
1244
1245 /*
1246  * SGML Linuxdoc support:
1247  * (flag == -1) import SGML file
1248  * (flag == 0) make TeX output
1249  * (flag == 1) make dvi output
1250  */
1251 int RunLinuxDoc(int flag, LString const & filename)
1252 {
1253         LString name;
1254         LString s2;
1255         LString path;
1256         LString add_flags;
1257
1258         int errorcode = 0;
1259
1260         /* generate a path-less extension name */
1261         name = ChangeExtension (filename, ".sgml", true);
1262         path = OnlyPath (filename);
1263         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
1264                 path = current_view->currentBuffer()->tmppath;
1265         }
1266         PathPush (path);
1267         
1268         if (flag != -1) {
1269                 if (!current_view->available())
1270                         return 0;
1271                 current_view->currentBuffer()->makeLinuxDocFile(name,0);
1272                 LYX_PAPER_SIZE ps = (LYX_PAPER_SIZE) current_view->currentBuffer()->params.papersize;
1273                 switch (ps) {
1274                 case PAPER_A4PAPER:  add_flags = "-p a4";     break;
1275                 case PAPER_USLETTER: add_flags = "-p letter"; break;
1276                 default: /* nothing to be done yet ;-) */     break; 
1277                 }
1278         }
1279         
1280         ProhibitInput();
1281         
1282         Systemcalls one;
1283         switch (flag) {
1284         case -1: /* Import file */
1285                 minibuffer->Set(_("Importing LinuxDoc SGML file `"), 
1286                                 MakeDisplayPath(filename), "'...");
1287                 s2 = "sgml2lyx " + lyxrc->sgml_extra_options + ' ' 
1288                         + name;
1289                 if (one.Startscript(Systemcalls::System, s2)) 
1290                         errorcode = 1;
1291                 break;
1292         case 0: /* TeX output asked */
1293                 minibuffer->Set(_("Converting LinuxDoc SGML to TeX file..."));
1294                 s2 = "sgml2latex " + add_flags + " -o tex "
1295                         + lyxrc->sgml_extra_options + ' ' + name;
1296                 if (one.Startscript(Systemcalls::System, s2)) 
1297                         errorcode = 1;
1298                 break;
1299         case 1: /* dvi output asked */
1300                 minibuffer->Set(_("Converting LinuxDoc SGML to dvi file..."));
1301                 s2 = "sgml2latex " + add_flags + " -o dvi "
1302                         + lyxrc->sgml_extra_options + ' ' + name;
1303                 if (one.Startscript(Systemcalls::System, s2)) {
1304                         errorcode = 1;
1305                 } else
1306                         current_view->currentBuffer()->markDviClean();
1307                 break;
1308         default: /* unknown output */
1309                 break;
1310         }
1311         
1312         PathPop();
1313         AllowInput();
1314
1315         current_view->currentBuffer()->redraw();
1316         return errorcode;
1317 }
1318
1319
1320 /*
1321  * SGML DocBook support:
1322  * (flag == 1) make dvi output
1323  */
1324 int RunDocBook(int flag, LString const & filename)
1325 {
1326         LString name;
1327         LString s2;
1328         LString path;
1329
1330         int errorcode = 0;
1331
1332         /* generate a path-less extension name */
1333         name = ChangeExtension (filename, ".sgml", true);
1334         path = OnlyPath (filename);
1335         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)) {
1336                 path = current_view->currentBuffer()->tmppath;
1337         }
1338         PathPush (path);
1339
1340         if (!current_view->available())
1341                 return 0;
1342         
1343         current_view->currentBuffer()->makeDocBookFile(name,0);
1344 #if 0
1345         LString add_flags;
1346         LYX_PAPER_SIZE ps = (LYX_PAPER_SIZE) current_view->currentBuffer()->params.papersize;
1347         switch (ps) {
1348         case PAPER_A4PAPER:  add_flags = "-p a4";     break;
1349         case PAPER_USLETTER: add_flags = "-p letter"; break;
1350         default: /* nothing to be done yet ;-) */     break; 
1351         }
1352 #endif  
1353         ProhibitInput();
1354         
1355         Systemcalls one;
1356         switch (flag) {
1357         case 1: /* dvi output asked */
1358                 minibuffer->Set(_("Converting DocBook SGML to dvi file..."));
1359                 s2 = "sgmltools --backend dvi " + name;
1360                 if (one.Startscript(Systemcalls::System, s2)) {
1361                         errorcode = 1;
1362                 } else
1363                         current_view->currentBuffer()->markDviClean();
1364                 break;
1365         default: /* unknown output */
1366                 break;
1367         }
1368         
1369         PathPop();
1370         AllowInput();
1371
1372         current_view->currentBuffer()->redraw();
1373         return errorcode;
1374 }
1375
1376
1377 void AllFloats(char flag, char figmar)
1378 {
1379         if (!current_view->available())
1380                 return;
1381    
1382         LyXCursor cursor = current_view->currentBuffer()->text->cursor;
1383
1384         if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
1385             && ((figmar 
1386                  && cursor.par->footnotekind != LyXParagraph::FOOTNOTE 
1387                  && cursor.par->footnotekind != LyXParagraph::MARGIN)
1388                 || (!figmar
1389                     && cursor.par->footnotekind != LyXParagraph::FIG 
1390                     && cursor.par->footnotekind != LyXParagraph::TAB
1391                     && cursor.par->footnotekind != LyXParagraph::WIDE_FIG 
1392                     && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
1393                     && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
1394                 ToggleFloat();
1395         else
1396                 BeforeChange();
1397
1398         LyXCursor tmpcursor = cursor;
1399         cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
1400         cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
1401
1402         LyXParagraph *par = current_view->currentBuffer()->paragraph;
1403         while (par) {
1404                 if (flag) {
1405                         if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
1406                             && (
1407                                     (figmar 
1408                                      &&
1409                                      par->footnotekind != LyXParagraph::FOOTNOTE 
1410                                      &&
1411                                      par->footnotekind !=  LyXParagraph::MARGIN
1412                                             )
1413                                     ||
1414                                     (!figmar
1415                                      &&
1416                                      par->footnotekind != LyXParagraph::FIG 
1417                                      &&
1418                                      par->footnotekind != LyXParagraph::TAB
1419                                      &&
1420                                      par->footnotekind != LyXParagraph::WIDE_FIG 
1421                                      &&
1422                                      par->footnotekind != LyXParagraph::WIDE_TAB
1423                                      &&
1424                                      par->footnotekind != LyXParagraph::ALGORITHM
1425                                             )
1426                                     )
1427                                 ){
1428                                 if (par->previous
1429                                     && par->previous->footnoteflag !=
1430                                     LyXParagraph::CLOSED_FOOTNOTE){ /* should be */ 
1431                                         current_view->currentBuffer()->text->SetCursorIntern(par->previous,
1432                                                                       0);
1433                                         current_view->currentBuffer()->text->OpenFootnote();
1434                                 }
1435                         }
1436                 }
1437                 else  {
1438                         if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
1439                             && (
1440                                     (figmar 
1441                                      &&
1442                                      par->footnotekind != LyXParagraph::FOOTNOTE 
1443                                      &&
1444                                      par->footnotekind !=  LyXParagraph::MARGIN
1445                                             )
1446                                     ||
1447                                     (!figmar
1448                                      &&
1449                                      par->footnotekind != LyXParagraph::FIG 
1450                                      &&
1451                                      par->footnotekind != LyXParagraph::TAB
1452                                      &&
1453                                      par->footnotekind != LyXParagraph::WIDE_FIG 
1454                                      &&
1455                                      par->footnotekind != LyXParagraph::WIDE_TAB
1456                                      &&
1457                                      par->footnotekind != LyXParagraph::ALGORITHM
1458                                             )
1459                                     )
1460                                 ){
1461                                 current_view->currentBuffer()->text->SetCursorIntern(par, 0);
1462                                 current_view->currentBuffer()->text->CloseFootnote();
1463                         }
1464                 }
1465                 par = par->next;
1466         }
1467
1468         current_view->currentBuffer()->text->SetCursorIntern(cursor.par, cursor.pos);
1469         current_view->redraw();
1470         current_view->fitCursor();
1471         current_view->updateScrollbar();
1472 }
1473
1474
1475 void MenuLayoutCharacter()
1476 {
1477         if (fd_form_character->form_character->visible) {
1478                 fl_raise_form(fd_form_character->form_character);
1479         } else {
1480                 fl_show_form(fd_form_character->form_character,
1481                              FL_PLACE_MOUSE | FL_FREE_SIZE,FL_FULLBORDER,
1482                              _("Character Style"));
1483         }
1484 }
1485
1486
1487 inline void DeactivateParagraphButtons ()
1488 {
1489         fl_deactivate_object (fd_form_paragraph->button_ok);
1490         fl_deactivate_object (fd_form_paragraph->button_apply);
1491         fl_set_object_lcol (fd_form_paragraph->button_ok, FL_INACTIVE);
1492         fl_set_object_lcol (fd_form_paragraph->button_apply, FL_INACTIVE);
1493 }
1494
1495 inline void ActivateParagraphButtons ()
1496 {
1497         fl_activate_object (fd_form_paragraph->button_ok);
1498         fl_activate_object (fd_form_paragraph->button_apply);
1499         fl_set_object_lcol (fd_form_paragraph->button_ok, FL_BLACK);
1500         fl_set_object_lcol (fd_form_paragraph->button_apply, FL_BLACK);
1501 }
1502
1503 inline void DisableParagraphLayout ()
1504 {
1505         DeactivateParagraphButtons();
1506         fl_deactivate_object (fd_form_paragraph->input_labelwidth);
1507         fl_deactivate_object (fd_form_paragraph->check_lines_top);
1508         fl_deactivate_object (fd_form_paragraph->check_lines_bottom);
1509         fl_deactivate_object (fd_form_paragraph->check_pagebreaks_top);
1510         fl_deactivate_object (fd_form_paragraph->check_pagebreaks_bottom);
1511         fl_deactivate_object (fd_form_paragraph->check_noindent);
1512         fl_deactivate_object (fd_form_paragraph->group_radio_alignment);
1513         fl_deactivate_object (fd_form_paragraph->radio_align_right);
1514         fl_deactivate_object (fd_form_paragraph->radio_align_left);
1515         fl_deactivate_object (fd_form_paragraph->radio_align_block);
1516         fl_deactivate_object (fd_form_paragraph->radio_align_center);
1517         fl_deactivate_object (fd_form_paragraph->input_space_above);
1518         fl_deactivate_object (fd_form_paragraph->input_space_below);
1519         fl_deactivate_object (fd_form_paragraph->choice_space_above);
1520         fl_deactivate_object (fd_form_paragraph->choice_space_below);
1521         fl_deactivate_object (fd_form_paragraph->check_space_above);
1522         fl_deactivate_object (fd_form_paragraph->check_space_below);
1523 }
1524
1525 inline void EnableParagraphLayout ()
1526 {
1527         ActivateParagraphButtons();
1528         fl_activate_object (fd_form_paragraph->input_labelwidth);
1529         fl_activate_object (fd_form_paragraph->check_lines_top);
1530         fl_activate_object (fd_form_paragraph->check_lines_bottom);
1531         fl_activate_object (fd_form_paragraph->check_pagebreaks_top);
1532         fl_activate_object (fd_form_paragraph->check_pagebreaks_bottom);
1533         fl_activate_object (fd_form_paragraph->check_noindent);
1534         fl_activate_object (fd_form_paragraph->group_radio_alignment);
1535         fl_activate_object (fd_form_paragraph->radio_align_right);
1536         fl_activate_object (fd_form_paragraph->radio_align_left);
1537         fl_activate_object (fd_form_paragraph->radio_align_block);
1538         fl_activate_object (fd_form_paragraph->radio_align_center);
1539         fl_activate_object (fd_form_paragraph->input_space_above);
1540         fl_activate_object (fd_form_paragraph->input_space_below);
1541         fl_activate_object (fd_form_paragraph->choice_space_above);
1542         fl_activate_object (fd_form_paragraph->choice_space_below);
1543         fl_activate_object (fd_form_paragraph->check_space_above);
1544         fl_activate_object (fd_form_paragraph->check_space_below);
1545 }
1546
1547 bool UpdateLayoutParagraph()
1548 {
1549         if (!current_view->getScreen() || !current_view->available()) {
1550                 if (fd_form_paragraph->form_paragraph->visible) 
1551                         fl_hide_form(fd_form_paragraph->form_paragraph);
1552                 return false;
1553         }
1554
1555         Buffer * buf = current_view->currentBuffer();
1556
1557         fl_set_input(fd_form_paragraph->input_labelwidth,
1558                      buf->text->cursor.par->GetLabelWidthString().c_str());
1559         fl_set_button(fd_form_paragraph->radio_align_right, 0);
1560         fl_set_button(fd_form_paragraph->radio_align_left, 0);
1561         fl_set_button(fd_form_paragraph->radio_align_center, 0);
1562         fl_set_button(fd_form_paragraph->radio_align_block, 0);
1563
1564         int align = buf->text->cursor.par->GetAlign();
1565         if (align == LYX_ALIGN_LAYOUT)
1566                 align = lyxstyle.Style(buf->params.textclass,
1567                                        buf->text->cursor.par->GetLayout())->align;
1568          
1569         switch (align) {
1570         case LYX_ALIGN_RIGHT:
1571                 fl_set_button(fd_form_paragraph->radio_align_right, 1);
1572                 break;
1573         case LYX_ALIGN_LEFT:
1574                 fl_set_button(fd_form_paragraph->radio_align_left, 1);
1575                 break;
1576         case LYX_ALIGN_CENTER:
1577                 fl_set_button(fd_form_paragraph->radio_align_center, 1);
1578                 break;
1579         default:
1580                 fl_set_button(fd_form_paragraph->radio_align_block, 1);
1581                 break;
1582         }
1583          
1584         fl_set_button(fd_form_paragraph->check_lines_top,
1585                       buf->text->cursor.par->FirstPhysicalPar()->line_top);
1586         fl_set_button(fd_form_paragraph->check_lines_bottom,
1587                       buf->text->cursor.par->FirstPhysicalPar()->line_bottom);
1588         fl_set_button(fd_form_paragraph->check_pagebreaks_top,
1589                       buf->text->cursor.par->FirstPhysicalPar()->pagebreak_top);
1590         fl_set_button(fd_form_paragraph->check_pagebreaks_bottom,
1591                       buf->text->cursor.par->FirstPhysicalPar()->pagebreak_bottom);
1592         fl_set_button(fd_form_paragraph->check_noindent,
1593                       buf->text->cursor.par->FirstPhysicalPar()->noindent);
1594         fl_set_input (fd_form_paragraph->input_space_above, "");
1595         
1596         switch (buf->text->cursor.par->FirstPhysicalPar()->added_space_top.kind()) {
1597         case VSpace::NONE:
1598                 fl_set_choice (fd_form_paragraph->choice_space_above,
1599                                1);
1600                 break;
1601         case VSpace::DEFSKIP:
1602                 fl_set_choice (fd_form_paragraph->choice_space_above,
1603                                2);
1604                 break;
1605         case VSpace::SMALLSKIP:
1606                 fl_set_choice (fd_form_paragraph->choice_space_above,
1607                                3);
1608                 break;
1609         case VSpace::MEDSKIP:
1610                 fl_set_choice (fd_form_paragraph->choice_space_above,
1611                                4);
1612                 break;
1613         case VSpace::BIGSKIP:
1614                 fl_set_choice (fd_form_paragraph->choice_space_above,
1615                                5);
1616                 break;
1617         case VSpace::VFILL:
1618                 fl_set_choice (fd_form_paragraph->choice_space_above,
1619                                6);
1620                 break;
1621         case VSpace::LENGTH:
1622                 fl_set_choice (fd_form_paragraph->choice_space_above,
1623                                7); 
1624                 fl_set_input  (fd_form_paragraph->input_space_above, 
1625                                buf->text->cursor.par->FirstPhysicalPar()->added_space_top.length().asString().c_str());
1626                 break;
1627         }
1628         fl_set_button (fd_form_paragraph->check_space_above,
1629                        buf->text->cursor.par->FirstPhysicalPar()->added_space_top.keep());
1630         fl_set_input (fd_form_paragraph->input_space_below, "");
1631         switch (buf->text->cursor.par->FirstPhysicalPar()->added_space_bottom.kind()) {
1632         case VSpace::NONE:
1633                 fl_set_choice (fd_form_paragraph->choice_space_below,
1634                                1);
1635                 break;
1636         case VSpace::DEFSKIP:
1637                 fl_set_choice (fd_form_paragraph->choice_space_below,
1638                                2);
1639                 break;
1640         case VSpace::SMALLSKIP:
1641                 fl_set_choice (fd_form_paragraph->choice_space_below,
1642                                3);
1643                 break;
1644         case VSpace::MEDSKIP:
1645                 fl_set_choice (fd_form_paragraph->choice_space_below,
1646                                4);
1647                 break;
1648         case VSpace::BIGSKIP:
1649                 fl_set_choice (fd_form_paragraph->choice_space_below,
1650                                5);
1651                 break;
1652         case VSpace::VFILL:
1653                 fl_set_choice (fd_form_paragraph->choice_space_below,
1654                                6);
1655                 break;
1656         case VSpace::LENGTH:
1657                 fl_set_choice (fd_form_paragraph->choice_space_below,
1658                                7); 
1659                 fl_set_input  (fd_form_paragraph->input_space_below, 
1660                                buf->text->cursor.par->FirstPhysicalPar()->added_space_bottom.length().asString().c_str());
1661                 break;
1662         }
1663         fl_set_button (fd_form_paragraph->check_space_below,
1664                        buf->text->cursor.par->FirstPhysicalPar()->added_space_bottom.keep());
1665
1666         fl_set_button(fd_form_paragraph->check_noindent,
1667                       buf->text->cursor.par->FirstPhysicalPar()->noindent);
1668
1669         if (current_view->currentBuffer()->isReadonly()) {
1670                 DisableParagraphLayout();
1671         } else {
1672                 EnableParagraphLayout();
1673         }
1674         return true;
1675 }
1676
1677 void MenuLayoutParagraph()
1678 {
1679         if (UpdateLayoutParagraph()) {
1680                 if (fd_form_paragraph->form_paragraph->visible) {
1681                         fl_raise_form(fd_form_paragraph->form_paragraph);
1682                 } else {
1683                         fl_show_form(fd_form_paragraph->form_paragraph,
1684                                      FL_PLACE_MOUSE | FL_FREE_SIZE,FL_FULLBORDER,
1685                                      _("Paragraph Environment"));
1686                 }
1687         }
1688 }
1689
1690
1691 inline
1692 void DeactivateDocumentButtons ()
1693 {
1694         fl_deactivate_object (fd_form_document->button_ok);
1695         fl_deactivate_object (fd_form_document->button_apply);
1696         fl_set_object_lcol (fd_form_document->button_ok, FL_INACTIVE);
1697         fl_set_object_lcol (fd_form_document->button_apply, FL_INACTIVE);
1698 }
1699
1700
1701 inline
1702 void ActivateDocumentButtons ()
1703 {
1704         fl_activate_object (fd_form_document->button_ok);
1705         fl_activate_object (fd_form_document->button_apply);
1706         fl_set_object_lcol (fd_form_document->button_ok, FL_BLACK);
1707         fl_set_object_lcol (fd_form_document->button_apply, FL_BLACK);
1708 }
1709
1710 inline
1711 void DisableDocumentLayout ()
1712 {
1713         DeactivateDocumentButtons ();
1714         fl_deactivate_object (fd_form_document->group_radio_separation);
1715         fl_deactivate_object (fd_form_document->radio_indent);
1716         fl_deactivate_object (fd_form_document->radio_skip);
1717         fl_deactivate_object (fd_form_document->choice_class);
1718         fl_deactivate_object (fd_form_document->choice_pagestyle);
1719         fl_deactivate_object (fd_form_document->choice_fonts);
1720         fl_deactivate_object (fd_form_document->choice_fontsize);
1721         fl_deactivate_object (fd_form_document->input_float_placement);
1722         fl_deactivate_object (fd_form_document->choice_postscript_driver);
1723         fl_deactivate_object (fd_form_document->choice_inputenc);
1724         fl_deactivate_object (fd_form_document->group_radio_sides);
1725         fl_deactivate_object (fd_form_document->radio_sides_one);
1726         fl_deactivate_object (fd_form_document->radio_sides_two);
1727         fl_deactivate_object (fd_form_document->group_radio_columns);
1728         fl_deactivate_object (fd_form_document->radio_columns_one);
1729         fl_deactivate_object (fd_form_document->radio_columns_two);
1730         fl_deactivate_object (fd_form_document->input_extra);
1731         fl_deactivate_object (fd_form_document->choice_language);
1732         combo_language->deactivate();
1733         fl_deactivate_object (fd_form_document->input_default_skip);
1734         fl_deactivate_object (fd_form_document->choice_default_skip);
1735         fl_deactivate_object (fd_form_document->slider_secnumdepth);
1736         fl_deactivate_object (fd_form_document->slider_tocdepth);
1737         fl_deactivate_object (fd_form_document->choice_spacing);
1738         fl_deactivate_object (fd_form_document->input_spacing);
1739         fl_deactivate_object (fd_form_document->check_use_amsmath);
1740 }
1741
1742 inline
1743 void EnableDocumentLayout ()
1744 {
1745         ActivateDocumentButtons ();
1746         fl_activate_object (fd_form_document->group_radio_separation);
1747         fl_activate_object (fd_form_document->radio_indent);
1748         fl_activate_object (fd_form_document->radio_skip);
1749         fl_activate_object (fd_form_document->choice_class);
1750         fl_activate_object (fd_form_document->choice_pagestyle);
1751         fl_activate_object (fd_form_document->choice_fonts);
1752         fl_activate_object (fd_form_document->choice_fontsize);
1753         fl_activate_object (fd_form_document->input_float_placement);
1754         fl_activate_object (fd_form_document->choice_postscript_driver);
1755         fl_activate_object (fd_form_document->choice_inputenc);
1756         fl_activate_object (fd_form_document->group_radio_sides);
1757         fl_activate_object (fd_form_document->radio_sides_one);
1758         fl_activate_object (fd_form_document->radio_sides_two);
1759         fl_activate_object (fd_form_document->group_radio_columns);
1760         fl_activate_object (fd_form_document->radio_columns_one);
1761         fl_activate_object (fd_form_document->radio_columns_two);
1762         fl_activate_object (fd_form_document->input_extra);
1763         fl_activate_object (fd_form_document->choice_language);
1764         combo_language->activate();
1765         fl_activate_object (fd_form_document->input_default_skip);
1766         fl_activate_object (fd_form_document->choice_default_skip);
1767         fl_activate_object (fd_form_document->slider_secnumdepth);
1768         fl_activate_object (fd_form_document->slider_tocdepth);
1769         fl_activate_object (fd_form_document->choice_spacing);
1770         fl_activate_object (fd_form_document->input_spacing);
1771         fl_activate_object (fd_form_document->check_use_amsmath);
1772 }
1773
1774 bool UpdateLayoutDocument(BufferParams *params)
1775 {
1776         if (!current_view->getScreen() || !current_view->available()) {
1777                 if (fd_form_document->form_document->visible) 
1778                         fl_hide_form(fd_form_document->form_document);
1779                 return false;
1780         }               
1781
1782         if (params == NULL)
1783                 params = &current_view->currentBuffer()->params;
1784         LyXTextClass *tclass = lyxstyle.TextClass(params->textclass);
1785         
1786         fl_set_choice_text(fd_form_document->choice_class, 
1787                            lyxstyle.DescOfClass(params->textclass).c_str());
1788         combo_language->select_text(params->language.c_str());
1789         
1790         fl_set_choice_text(fd_form_document->choice_fonts, 
1791                            params->fonts.c_str());
1792         fl_set_choice_text(fd_form_document->choice_inputenc, 
1793                            params->inputenc.c_str());
1794         fl_set_choice_text(fd_form_document->choice_postscript_driver, 
1795                            params->graphicsDriver.c_str());
1796
1797         // ale970405+lasgoutt970513
1798         fl_clear_choice(fd_form_document->choice_fontsize);
1799         fl_addto_choice(fd_form_document->choice_fontsize, "default");
1800         fl_addto_choice(fd_form_document->choice_fontsize, 
1801                         tclass->opt_fontsize.c_str());
1802         fl_set_choice(fd_form_document->choice_fontsize, 
1803                       tclass->opt_fontsize.tokenPos('|', params->fontsize) + 2);
1804
1805         // ale970405+lasgoutt970513
1806         fl_clear_choice(fd_form_document->choice_pagestyle);
1807         fl_addto_choice(fd_form_document->choice_pagestyle, "default");
1808         fl_addto_choice(fd_form_document->choice_pagestyle, 
1809                         tclass->opt_pagestyle.c_str());
1810     
1811         fl_set_choice(fd_form_document->choice_pagestyle,
1812                       tclass->opt_pagestyle.tokenPos('|', params->pagestyle) + 2);
1813
1814         fl_set_button(fd_form_document->radio_indent, 0);
1815         fl_set_button(fd_form_document->radio_skip, 0);
1816     
1817         
1818         fl_set_button(fd_form_document->check_use_amsmath, params->use_amsmath);
1819
1820         if (params->paragraph_separation == LYX_PARSEP_INDENT)
1821                 fl_set_button(fd_form_document->radio_indent, 1);
1822         else
1823                 fl_set_button(fd_form_document->radio_skip, 1);
1824
1825         switch (params->getDefSkip().kind()) {
1826         case VSpace::SMALLSKIP: 
1827                 fl_set_choice (fd_form_document->choice_default_skip, 1);
1828                 break;
1829         case VSpace::MEDSKIP: 
1830                 fl_set_choice (fd_form_document->choice_default_skip, 2);
1831                 break;
1832         case VSpace::BIGSKIP: 
1833                 fl_set_choice (fd_form_document->choice_default_skip, 3);
1834                 break;
1835         case VSpace::LENGTH: 
1836                 fl_set_choice (fd_form_document->choice_default_skip, 4);
1837                 fl_set_input (fd_form_document->input_default_skip,
1838                               params->getDefSkip().asLyXCommand().c_str());
1839                 break;
1840         default:
1841                 fl_set_choice (fd_form_document->choice_default_skip, 2);
1842                 break;
1843         }
1844    
1845         fl_set_button(fd_form_document->radio_sides_one, 0);
1846         fl_set_button(fd_form_document->radio_sides_two, 0);
1847    
1848         if (params->sides == 2)
1849                 fl_set_button(fd_form_document->radio_sides_two, 1);
1850         else
1851                 fl_set_button(fd_form_document->radio_sides_one, 1);
1852    
1853         fl_set_button(fd_form_document->radio_columns_one, 0);
1854         fl_set_button(fd_form_document->radio_columns_two, 0);
1855    
1856         if (params->columns == 2)
1857                 fl_set_button(fd_form_document->radio_columns_two, 1);
1858         else
1859                 fl_set_button(fd_form_document->radio_columns_one, 1);
1860    
1861         fl_set_input(fd_form_document->input_spacing, "");
1862         switch (params->spacing.getSpace()) {
1863         case Spacing::Single:
1864                 {
1865                         // \singlespacing
1866                         fl_set_choice(fd_form_document->choice_spacing, 1);
1867                         break;
1868                 }
1869         case Spacing::Onehalf:
1870                 {
1871                         // \onehalfspacing
1872                         fl_set_choice(fd_form_document->choice_spacing, 2);
1873                         break;
1874                 }
1875         case Spacing::Double:
1876                 {
1877                         // \ doublespacing
1878                         fl_set_choice(fd_form_document->choice_spacing, 3);
1879                         break;
1880                 }
1881         case Spacing::Other:
1882                 {
1883                         fl_set_choice(fd_form_document->choice_spacing, 4);
1884                         char sval[20];
1885                         sprintf(sval,"%g",params->spacing.getValue()); 
1886                         fl_set_input(fd_form_document->input_spacing, sval);
1887                         break;
1888                 }
1889         }
1890
1891
1892         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
1893                              params->secnumdepth);
1894         fl_set_counter_value(fd_form_document->slider_tocdepth, 
1895                              params->tocdepth);
1896         if (!params->float_placement.empty()) { // buffer local (Lgb)
1897                 fl_set_input(fd_form_document->input_float_placement,
1898                              params->float_placement.c_str());
1899         } else {
1900                 fl_set_input(fd_form_document->input_float_placement, "");
1901         }
1902         if (!params->options.empty())
1903                 fl_set_input(fd_form_document->input_extra,
1904                              params->options.c_str());
1905         else
1906                 fl_set_input(fd_form_document->input_extra, "");
1907
1908         if (current_view->currentBuffer()->isSGML()) {
1909                 // bullets not used in SGML derived documents
1910                 fl_deactivate_object(fd_form_document->button_bullets);
1911                 fl_set_object_lcol(fd_form_document->button_bullets,
1912                                    FL_INACTIVE);
1913         } else {
1914                 fl_activate_object(fd_form_document->button_bullets);
1915                 fl_set_object_lcol(fd_form_document->button_bullets,
1916                                    FL_BLACK);
1917         }
1918
1919         if (current_view->currentBuffer()->isReadonly()) {
1920                 DisableDocumentLayout();
1921         } else {
1922                 EnableDocumentLayout();
1923         }
1924
1925         return true;
1926 }
1927
1928 void MenuLayoutDocument()
1929 {
1930         if (UpdateLayoutDocument()) {
1931                 if (fd_form_document->form_document->visible) {
1932                         fl_raise_form(fd_form_document->form_document);
1933                 } else {
1934                         fl_show_form(fd_form_document->form_document,
1935                                      FL_PLACE_MOUSE | FL_FREE_SIZE,
1936                                      FL_FULLBORDER,
1937                                      _("Document Layout"));
1938                 }
1939         }
1940 }
1941
1942
1943 bool UpdateLayoutQuotes()
1944 {
1945         bool update = true;
1946         if (!current_view->getScreen()
1947             || !current_view->available()
1948             || current_view->currentBuffer()->isReadonly())
1949                 update = false;
1950         
1951         if (update) {
1952                 fl_set_choice(fd_form_quotes->choice_quotes_language,
1953                       current_view->currentBuffer()->params.quotes_language + 1);
1954                 fl_set_button(fd_form_quotes->radio_single, 0);
1955                 fl_set_button(fd_form_quotes->radio_double, 0);
1956         
1957                 if (current_view->currentBuffer()->params.quotes_times == InsetQuotes::SingleQ)
1958                         fl_set_button(fd_form_quotes->radio_single, 1);
1959                 else
1960                         fl_set_button(fd_form_quotes->radio_double, 1);
1961         } else if (fd_form_quotes->form_quotes->visible) {
1962                 fl_hide_form(fd_form_quotes->form_quotes);
1963         }
1964         return update;
1965 }
1966
1967 void MenuLayoutQuotes()
1968 {
1969         if (UpdateLayoutQuotes()) {
1970                 if (fd_form_quotes->form_quotes->visible) {
1971                         fl_raise_form(fd_form_quotes->form_quotes);
1972                 } else {
1973                         fl_show_form(fd_form_quotes->form_quotes,
1974                                      FL_PLACE_MOUSE | FL_FREE_SIZE,
1975                                      FL_FULLBORDER,
1976                                      _("Quotes"));
1977                 }
1978         }
1979 }
1980
1981
1982 bool UpdateLayoutPreamble()
1983 {
1984         bool update = true;
1985         if (!current_view->getScreen() || ! current_view->available())
1986             update = false;
1987
1988         if (update) {
1989                 fl_set_input(fd_form_preamble->input_preamble,
1990                      current_view->currentBuffer()->params.preamble.c_str());
1991
1992                 if (current_view->currentBuffer()->isReadonly()) {
1993                   fl_deactivate_object(fd_form_preamble->input_preamble);
1994                   fl_deactivate_object(fd_form_preamble->button_ok);
1995                   fl_deactivate_object(fd_form_preamble->button_apply);
1996                   fl_set_object_lcol(fd_form_preamble->button_ok, FL_INACTIVE);
1997                   fl_set_object_lcol(fd_form_preamble->button_apply, FL_INACTIVE);
1998                 }
1999                 else {
2000                   fl_activate_object(fd_form_preamble->input_preamble);
2001                   fl_activate_object(fd_form_preamble->button_ok);
2002                   fl_activate_object(fd_form_preamble->button_apply);
2003                   fl_set_object_lcol(fd_form_preamble->button_ok, FL_BLACK);
2004                   fl_set_object_lcol(fd_form_preamble->button_apply, FL_BLACK);
2005                 }
2006         } else if (fd_form_preamble->form_preamble->visible) {
2007                 fl_hide_form(fd_form_preamble->form_preamble);
2008         }
2009         return update;
2010 }
2011
2012 void MenuLayoutPreamble()
2013 {
2014         if (UpdateLayoutPreamble()) {
2015                 if (fd_form_preamble->form_preamble->visible) {
2016                         fl_raise_form(fd_form_preamble->form_preamble);
2017                 } else {
2018                         fl_show_form(fd_form_preamble->form_preamble,
2019                                      FL_PLACE_MOUSE | FL_FREE_SIZE,
2020                                      FL_FULLBORDER,
2021                                      _("LaTeX Preamble"));
2022                 }
2023         }
2024 }
2025
2026 void MenuLayoutSave()
2027 {
2028         if (!current_view->getScreen() || ! current_view->available())
2029             return;
2030
2031         if (AskQuestion(_("Do you want to save the current settings"),
2032                         _("for Character, Document, Paper and Quotes"),
2033                         _("as default for new documents?")))
2034                 current_view->currentBuffer()->saveParamsAsDefaults();
2035 }
2036
2037
2038 void NoteCB()
2039 {
2040         InsetInfo *new_inset = new InsetInfo();
2041         current_view->currentBuffer()->insertInset(new_inset);
2042         new_inset->Edit(0,0);
2043         //current_view->currentBuffer()->update(-1);
2044 }
2045
2046
2047 void OpenStuff()
2048 {
2049         if (current_view->available()) {
2050                 minibuffer->Set(_("Open/Close..."));
2051                 current_view->getScreen()->HideCursor();
2052                 BeforeChange();
2053                 current_view->currentBuffer()->update(-2);
2054                 current_view->currentBuffer()->text->OpenStuff();
2055                 current_view->currentBuffer()->update(0);
2056         }
2057 }
2058
2059 void ToggleFloat()
2060 {
2061         if (current_view->available()) {
2062                 minibuffer->Set(_("Open/Close..."));
2063                 current_view->getScreen()->HideCursor();
2064                 BeforeChange();
2065                 current_view->currentBuffer()->update(-2);
2066                 current_view->currentBuffer()->text->ToggleFootnote();
2067                 current_view->currentBuffer()->update(0);
2068         }
2069 }
2070
2071
2072 void MenuUndo()
2073 {
2074 /*      if (current_view->currentBuffer()->the_locking_inset) {
2075                 minibuffer->Set(_("Undo not yet supported in math mode"));
2076                 return;
2077         }*/
2078    
2079         if (current_view->available()) {
2080                 minibuffer->Set(_("Undo"));
2081                 current_view->getScreen()->HideCursor();
2082                 BeforeChange();
2083                 current_view->currentBuffer()->update(-2);
2084                 if (!current_view->currentBuffer()->text->TextUndo())
2085                         minibuffer->Set(_("No further undo information"));
2086                 else
2087                         current_view->currentBuffer()->update(-1);
2088         }
2089 }
2090
2091
2092 void MenuRedo()
2093 {
2094         if (current_view->currentBuffer()->the_locking_inset) {
2095                 minibuffer->Set(_("Redo not yet supported in math mode"));
2096                 return;
2097         }    
2098    
2099         if (current_view->available()) {
2100                 minibuffer->Set(_("Redo"));
2101                 current_view->getScreen()->HideCursor();
2102                 BeforeChange();
2103                 current_view->currentBuffer()->update(-2);
2104                 if (!current_view->currentBuffer()->text->TextRedo())
2105                         minibuffer->Set(_("No further redo information"));
2106                 else
2107                         current_view->currentBuffer()->update(-1);
2108         }
2109 }
2110
2111
2112 void HyphenationPoint()
2113 {
2114         if (current_view->available())  {
2115                 current_view->getScreen()->HideCursor();
2116                 current_view->currentBuffer()->update(-2);
2117                 InsetSpecialChar *new_inset = 
2118                         new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
2119                 current_view->currentBuffer()->insertInset(new_inset);
2120                 //current_view->currentBuffer()->update(-1);
2121         }
2122 }
2123
2124
2125 void Ldots()
2126 {
2127         if (current_view->available())  {
2128                 current_view->getScreen()->HideCursor();
2129                 current_view->currentBuffer()->update(-2);
2130                 InsetSpecialChar *new_inset = 
2131                         new InsetSpecialChar(InsetSpecialChar::LDOTS);
2132                 current_view->currentBuffer()->insertInset(new_inset);
2133         }
2134 }
2135
2136
2137 void EndOfSentenceDot()
2138 {
2139         if (current_view->available())  {
2140                 current_view->getScreen()->HideCursor();
2141                 current_view->currentBuffer()->update(-2);
2142                 InsetSpecialChar *new_inset = 
2143                         new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
2144                 current_view->currentBuffer()->insertInset(new_inset);
2145         }
2146 }
2147
2148
2149 void MenuSeparator()
2150 {
2151         if (current_view->available())  {
2152                 current_view->getScreen()->HideCursor();
2153                 current_view->currentBuffer()->update(-2);
2154                 InsetSpecialChar *new_inset = 
2155                         new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
2156                 current_view->currentBuffer()->insertInset(new_inset);
2157                 //current_view->currentBuffer()->update(-1);
2158         }
2159 }
2160
2161
2162 void Newline()
2163 {
2164         if (current_view->available())  {
2165                 current_view->getScreen()->HideCursor();
2166                 current_view->currentBuffer()->update(-2);
2167                 current_view->currentBuffer()->text->InsertChar(LYX_META_NEWLINE);
2168                 current_view->currentBuffer()->update(-1);
2169         }
2170 }
2171
2172
2173 void ProtectedBlank()
2174 {
2175         if (current_view->available())  {
2176                 current_view->getScreen()->HideCursor();
2177                 current_view->currentBuffer()->update(-2);
2178                 current_view->currentBuffer()->text->InsertChar(LYX_META_PROTECTED_SEPARATOR);
2179                 current_view->currentBuffer()->update(-1);
2180         }
2181 }
2182
2183
2184 void HFill()
2185 {
2186         if (current_view->available())  {
2187                 current_view->getScreen()->HideCursor();
2188                 current_view->currentBuffer()->update(-2);
2189                 current_view->currentBuffer()->text->InsertChar(LYX_META_HFILL);
2190                 current_view->currentBuffer()->update(-1);
2191         }
2192 }
2193
2194
2195 /* -------> These CB's use ToggleFree() as the (one and only?) font-changer. 
2196                         They also show the current font state. */
2197
2198 static
2199 void ToggleAndShow(LyXFont const &);
2200
2201
2202 void FontSizeCB(LString const & size)
2203 {
2204         LyXFont font(LyXFont::ALL_IGNORE);
2205         font.setGUISize(size);
2206         ToggleAndShow(font);
2207 }
2208
2209
2210 void EmphCB()
2211 {
2212         LyXFont font(LyXFont::ALL_IGNORE);
2213         font.setEmph(LyXFont::TOGGLE);
2214         ToggleAndShow(font);
2215 }
2216
2217
2218 void NounCB()
2219 {
2220         LyXFont font(LyXFont::ALL_IGNORE);
2221         font.setNoun(LyXFont::TOGGLE);
2222         ToggleAndShow(font);
2223 }
2224
2225
2226 void BoldCB()
2227 {
2228         LyXFont font(LyXFont::ALL_IGNORE);
2229         font.setSeries(LyXFont::BOLD_SERIES);
2230         ToggleAndShow(font);
2231 }
2232
2233
2234 void UnderlineCB()
2235 {
2236         LyXFont font(LyXFont::ALL_IGNORE);
2237         font.setUnderbar(LyXFont::TOGGLE);
2238         ToggleAndShow(font);
2239 }
2240
2241
2242 void CodeCB()
2243 {
2244         LyXFont font(LyXFont::ALL_IGNORE);
2245         font.setFamily(LyXFont::TYPEWRITER_FAMILY); // no good
2246         ToggleAndShow(font);
2247 }
2248
2249
2250 void SansCB()
2251 {
2252         LyXFont font(LyXFont::ALL_IGNORE);
2253         font.setFamily(LyXFont::SANS_FAMILY);
2254         ToggleAndShow(font);
2255 }
2256
2257
2258 void RomanCB()
2259 {
2260         LyXFont font(LyXFont::ALL_IGNORE);
2261         font.setFamily(LyXFont::ROMAN_FAMILY);
2262         ToggleAndShow(font);
2263 }
2264
2265
2266 void TexCB()
2267 {
2268         LyXFont font(LyXFont::ALL_IGNORE);
2269         font.setLatex (LyXFont::TOGGLE);
2270         ToggleAndShow(font);
2271 }
2272
2273
2274 void StyleResetCB()
2275 {
2276         LyXFont font(LyXFont::ALL_INHERIT);
2277         ToggleAndShow(font);
2278 }
2279
2280
2281 /* -------> Returns the current font and depth by printing a message. In the
2282  * future perhaps we could try to implement a callback to the button-bar.
2283  * That is, `light' the bold button when the font is currently bold, etc.
2284  */
2285 LString CurrentState()
2286 {
2287         LString state;
2288         if (current_view->available()) { 
2289                 // I think we should only show changes from the default
2290                 // font. (Asger)
2291                 Buffer * buffer = current_view->currentBuffer();
2292                 LyXFont font = buffer->text->real_current_font;
2293                 LyXFont defaultfont = lyxstyle.TextClass(buffer->
2294                                                          params.textclass)->defaultfont;
2295                 font.reduce(defaultfont);
2296                 state = _("Font: ") + font.stateText();
2297
2298                 int depth = buffer->text->GetDepth();
2299                 if (depth>0) 
2300                         state += LString(_(", Depth: ")) + depth;
2301         }
2302         return state;
2303 }
2304
2305
2306 /* -------> Does the actual toggle job of the XxxCB() calls above.
2307  * Also shows the current font state.
2308  */
2309 static
2310 void ToggleAndShow(LyXFont const & font)
2311 {
2312         if (current_view->available()) { 
2313                 current_view->getScreen()->HideCursor();
2314                 current_view->currentBuffer()->update(-2);
2315                 current_view->currentBuffer()->text->ToggleFree(font, toggleall);
2316                 current_view->currentBuffer()->update(1);
2317         }
2318         // removed since it overrides the ToggleFree Message about the style
2319         // Since Styles are more "High Level" than raw fonts I think the user
2320         // prefers it like this               Matthias
2321         // FontStateShowCB( 0, 0 );
2322 }
2323
2324
2325 void MarginCB(FL_OBJECT *, long)
2326 {
2327         if (current_view->available()) {
2328                 minibuffer->Set(_("Inserting margin note..."));
2329                 current_view->getScreen()->HideCursor();
2330                 current_view->currentBuffer()->update(-2);
2331                 current_view->currentBuffer()->text->InsertFootnoteEnvironment(LyXParagraph::MARGIN);
2332                 current_view->currentBuffer()->update(1);
2333         }
2334 }
2335
2336
2337 void FigureCB(FL_OBJECT *, long)
2338 {
2339         if (fd_form_figure->form_figure->visible) {
2340                 fl_raise_form(fd_form_figure->form_figure);
2341         } else {
2342                 fl_show_form(fd_form_figure->form_figure,
2343                              FL_PLACE_MOUSE, FL_FULLBORDER,
2344                              _("Insert Figure"));
2345         }
2346 }
2347
2348
2349 void TableCB(FL_OBJECT *, long)
2350 {
2351         if (fd_form_table->form_table->visible) {
2352                 fl_raise_form(fd_form_table->form_table);
2353         } else {
2354                 fl_show_form(fd_form_table->form_table,
2355                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
2356                              _("Insert Table"));
2357         }
2358 }
2359
2360
2361 void CopyEnvironmentCB()
2362 {
2363         if (current_view->available()) {
2364                 current_view->currentBuffer()->text->copyEnvironmentType();
2365                 /* clear the selection, even if mark_set */ 
2366                 current_view->getScreen()->ToggleSelection();
2367                 current_view->currentBuffer()->text->ClearSelection();
2368                 current_view->currentBuffer()->update(-2);
2369                 minibuffer->Set(_("Paragraph environment type copied"));
2370         }
2371 }
2372
2373
2374 void PasteEnvironmentCB()
2375 {
2376         if (current_view->available()) {
2377                 current_view->currentBuffer()->text->pasteEnvironmentType();
2378                 minibuffer->Set(_("Paragraph environment type set"));
2379                 current_view->currentBuffer()->update(1);
2380         }
2381 }
2382
2383
2384 void CopyCB()
2385 {
2386         if (current_view->available()) {
2387                 current_view->currentBuffer()->text->CopySelection();
2388                 /* clear the selection, even if mark_set */ 
2389                 current_view->getScreen()->ToggleSelection();
2390                 current_view->currentBuffer()->text->ClearSelection();
2391                 current_view->currentBuffer()->update(-2);
2392                 minibuffer->Set(_("Copy"));
2393         }
2394 }
2395
2396
2397 void CutCB()
2398 {
2399         if (current_view->available()) {
2400                 current_view->getScreen()->HideCursor();
2401                 current_view->currentBuffer()->update(-2);
2402                 current_view->currentBuffer()->text->CutSelection();
2403                 current_view->currentBuffer()->update(1);
2404                 minibuffer->Set(_("Cut"));
2405         }
2406 }
2407
2408
2409 void PasteCB()
2410 {
2411         if (!current_view->available()) return;
2412         
2413         minibuffer->Set(_("Paste"));
2414         current_view->getScreen()->HideCursor();
2415         /* clear the selection */ 
2416         current_view->getScreen()->ToggleSelection();
2417         current_view->currentBuffer()->text->ClearSelection();
2418         current_view->currentBuffer()->update(-2);
2419         
2420         /* paste */ 
2421         current_view->currentBuffer()->text->PasteSelection();
2422         current_view->currentBuffer()->update(1);
2423         
2424         /* clear the selection */ 
2425         current_view->getScreen()->ToggleSelection();
2426         current_view->currentBuffer()->text->ClearSelection();
2427         current_view->currentBuffer()->update(-2);
2428 }
2429
2430
2431 void MeltCB(FL_OBJECT *, long)
2432 {
2433         if (!current_view->available()) return;
2434         
2435         minibuffer->Set(_("Melt"));
2436         current_view->getScreen()->HideCursor();
2437         BeforeChange();
2438         current_view->currentBuffer()->update(-2);
2439         current_view->currentBuffer()->text->MeltFootnoteEnvironment();
2440         current_view->currentBuffer()->update(1);
2441 }
2442
2443
2444 // Change environment depth.
2445 // if decInc == 0, depth change taking mouse button number into account
2446 // if decInc == 1, increment depth
2447 // if decInc == -1, decrement depth
2448 void DepthCB(FL_OBJECT *ob, long decInc)
2449 {
2450         int button = 1;
2451
2452         /* When decInc != 0, fake a mouse button. This allows us to
2453            implement depth-plus and depth-min commands. RVDK_PATCH_5. */
2454         /* check out wether ob is defined, too (Matthias) */ 
2455         if ( decInc < 0 )
2456                 button = 0;
2457         else if (!decInc && ob) {
2458                 button = fl_get_button_numb(ob);
2459         }
2460   
2461         if (current_view->available()) {
2462                 current_view->getScreen()->HideCursor();
2463                 current_view->currentBuffer()->update(-2);
2464                 if (button == 1)
2465                         current_view->currentBuffer()->text->IncDepth();
2466                 else
2467                         current_view->currentBuffer()->text->DecDepth();
2468                 current_view->currentBuffer()->update(1);
2469                 minibuffer->Set(_("Changed environment depth"
2470                                   " (in possible range, maybe not)"));
2471         }
2472 }
2473
2474
2475 // This is both GUI and LyXFont dependent. Don't know where to put it. (Asger)
2476 // Well, it's mostly GUI dependent, so I guess it will stay here. (Asger)
2477 LyXFont UserFreeFont()
2478 {
2479         LyXFont font(LyXFont::ALL_IGNORE);
2480         int pos;
2481
2482         pos = fl_get_choice(fd_form_character->choice_family);
2483         switch(pos) {
2484         case 1: font.setFamily(LyXFont::IGNORE_FAMILY); break;
2485         case 2: font.setFamily(LyXFont::ROMAN_FAMILY); break;
2486         case 3: font.setFamily(LyXFont::SANS_FAMILY); break;
2487         case 4: font.setFamily(LyXFont::TYPEWRITER_FAMILY); break;
2488         case 5: font.setFamily(LyXFont::INHERIT_FAMILY); break;
2489         }
2490
2491         pos = fl_get_choice(fd_form_character->choice_series);
2492         switch(pos) {
2493         case 1: font.setSeries(LyXFont::IGNORE_SERIES); break;
2494         case 2: font.setSeries(LyXFont::MEDIUM_SERIES); break;
2495         case 3: font.setSeries(LyXFont::BOLD_SERIES); break;
2496         case 4: font.setSeries(LyXFont::INHERIT_SERIES); break;
2497         }
2498
2499         pos = fl_get_choice(fd_form_character->choice_shape);
2500         switch(pos) {
2501         case 1: font.setShape(LyXFont::IGNORE_SHAPE); break;
2502         case 2: font.setShape(LyXFont::UP_SHAPE); break;
2503         case 3: font.setShape(LyXFont::ITALIC_SHAPE); break;
2504         case 4: font.setShape(LyXFont::SLANTED_SHAPE); break;
2505         case 5: font.setShape(LyXFont::SMALLCAPS_SHAPE); break;
2506         case 6: font.setShape(LyXFont::INHERIT_SHAPE); break;
2507         }
2508
2509         pos = fl_get_choice(fd_form_character->choice_size);
2510         switch(pos) {
2511         case 1: font.setSize(LyXFont::IGNORE_SIZE); break;
2512         case 2: font.setSize(LyXFont::SIZE_TINY); break;
2513         case 3: font.setSize(LyXFont::SIZE_SCRIPT); break;
2514         case 4: font.setSize(LyXFont::SIZE_FOOTNOTE); break;
2515         case 5: font.setSize(LyXFont::SIZE_SMALL); break;
2516         case 6: font.setSize(LyXFont::SIZE_NORMAL); break;
2517         case 7: font.setSize(LyXFont::SIZE_LARGE); break;
2518         case 8: font.setSize(LyXFont::SIZE_LARGER); break;
2519         case 9: font.setSize(LyXFont::SIZE_LARGEST); break;
2520         case 10: font.setSize(LyXFont::SIZE_HUGE); break;
2521         case 11: font.setSize(LyXFont::SIZE_HUGER); break;
2522         case 12: font.setSize(LyXFont::INCREASE_SIZE); break;
2523         case 13: font.setSize(LyXFont::DECREASE_SIZE); break;
2524         case 14: font.setSize(LyXFont::INHERIT_SIZE); break;
2525         }
2526
2527         pos = fl_get_choice(fd_form_character->choice_bar);
2528         switch(pos) {
2529         case 1: font.setEmph(LyXFont::IGNORE);
2530                 font.setUnderbar(LyXFont::IGNORE);
2531                 font.setNoun(LyXFont::IGNORE);
2532                 font.setLatex(LyXFont::IGNORE);
2533                 break;
2534         case 2: font.setEmph(LyXFont::TOGGLE); break;
2535         case 3: font.setUnderbar(LyXFont::TOGGLE); break;
2536         case 4: font.setNoun(LyXFont::TOGGLE); break;
2537         case 5: font.setLatex(LyXFont::TOGGLE); break;
2538         case 6: font.setEmph(LyXFont::INHERIT);
2539                 font.setUnderbar(LyXFont::INHERIT);
2540                 font.setNoun(LyXFont::INHERIT);
2541                 font.setLatex(LyXFont::INHERIT);
2542                 break;
2543         }
2544
2545         pos = fl_get_choice(fd_form_character->choice_color);
2546         switch(pos) {
2547         case 1: font.setColor(LyXFont::IGNORE_COLOR); break;
2548         case 2: font.setColor(LyXFont::NONE); break;
2549         case 3: font.setColor(LyXFont::BLACK); break;
2550         case 4: font.setColor(LyXFont::WHITE); break;
2551         case 5: font.setColor(LyXFont::RED); break;
2552         case 6: font.setColor(LyXFont::GREEN); break;
2553         case 7: font.setColor(LyXFont::BLUE); break;
2554         case 8: font.setColor(LyXFont::CYAN); break;
2555         case 9: font.setColor(LyXFont::MAGENTA); break;
2556         case 10: font.setColor(LyXFont::YELLOW); break;
2557         case 11: font.setColor(LyXFont::INHERIT_COLOR); break;
2558         }
2559
2560         return font; 
2561 }
2562
2563
2564 void FreeCB()
2565 {
2566         ToggleAndShow(UserFreeFont());
2567 }
2568
2569
2570 /* callbacks for form form_title */
2571 void TimerCB(FL_OBJECT *, long)
2572 {
2573         // only if the form still exists
2574         if (fd_form_title->form_title != NULL) {
2575                 if (fd_form_title->form_title->visible) {
2576                         fl_hide_form(fd_form_title->form_title);
2577                 }
2578                 fl_free_form(fd_form_title->form_title);
2579                 fd_form_title->form_title = NULL;
2580         }
2581 }
2582
2583
2584 /* callbacks for form form_paragraph */
2585
2586 void ParagraphVSpaceCB(FL_OBJECT* obj, long )
2587 {
2588         // "Synchronize" the choices and input fields, making it
2589         // impossible to commit senseless data.
2590
2591         const FD_form_paragraph* fp = fd_form_paragraph;
2592
2593         if (obj == fp->choice_space_above) {
2594                 if (fl_get_choice (fp->choice_space_above) != 7) {
2595                         fl_set_input (fp->input_space_above, "");
2596                         ActivateParagraphButtons();
2597                 }
2598         } else if (obj == fp->choice_space_below) {
2599                 if (fl_get_choice (fp->choice_space_below) != 7) {
2600                         fl_set_input (fp->input_space_below, "");
2601                         ActivateParagraphButtons();
2602                 }
2603         } else if (obj == fp->input_space_above) {
2604                 LString input = fl_get_input (fp->input_space_above);
2605
2606                 if (input.empty()) {
2607                         fl_set_choice (fp->choice_space_above, 1);
2608                         ActivateParagraphButtons();
2609                 }
2610                 else if (isValidGlueLength (input)) {
2611                         fl_set_choice (fp->choice_space_above, 7);
2612                         ActivateParagraphButtons();
2613                 }
2614                 else {
2615                         fl_set_choice (fp->choice_space_above, 7);
2616                         DeactivateParagraphButtons();
2617                 }
2618         } else if (obj == fp->input_space_below) {
2619                 LString input = fl_get_input (fp->input_space_below);
2620
2621                 if (input.empty()) {
2622                         fl_set_choice (fp->choice_space_below, 1);
2623                         ActivateParagraphButtons();
2624                 }
2625                 else if (isValidGlueLength (input)) {
2626                         fl_set_choice (fp->choice_space_below, 7);
2627                         ActivateParagraphButtons();
2628                 }
2629                 else {
2630                         fl_set_choice (fp->choice_space_below, 7);
2631                         DeactivateParagraphButtons();
2632                 }
2633         }
2634 }
2635
2636
2637 void ParagraphApplyCB(FL_OBJECT *, long)
2638 {
2639         if (!current_view->available())
2640                 return;
2641         
2642         bool line_top, line_bottom;
2643         bool pagebreak_top, pagebreak_bottom;
2644         VSpace space_top, space_bottom;
2645         char align;
2646         LString labelwidthstring;
2647         bool noindent;
2648
2649         // If a vspace kind is "Length" but there's no text in
2650         // the input field, reset the kind to "None". 
2651         if (fl_get_choice (fd_form_paragraph->choice_space_above) == 7
2652             && !*(fl_get_input (fd_form_paragraph->input_space_above))) {
2653                 fl_set_choice (fd_form_paragraph->choice_space_above, 1);
2654         }
2655         if (fl_get_choice (fd_form_paragraph->choice_space_below) == 7
2656             && !*(fl_get_input (fd_form_paragraph->input_space_below))) {
2657                 fl_set_choice (fd_form_paragraph->choice_space_below, 1);
2658         }
2659    
2660         line_top = fl_get_button(fd_form_paragraph->check_lines_top);
2661         line_bottom = fl_get_button(fd_form_paragraph->check_lines_bottom);
2662         pagebreak_top = fl_get_button(fd_form_paragraph->check_pagebreaks_top);
2663         pagebreak_bottom = fl_get_button(fd_form_paragraph->check_pagebreaks_bottom);
2664         switch (fl_get_choice (fd_form_paragraph->choice_space_above)) {
2665         case 1: space_top = VSpace(VSpace::NONE); break;
2666         case 2: space_top = VSpace(VSpace::DEFSKIP); break;
2667         case 3: space_top = VSpace(VSpace::SMALLSKIP); break;
2668         case 4: space_top = VSpace(VSpace::MEDSKIP); break;
2669         case 5: space_top = VSpace(VSpace::BIGSKIP); break;
2670         case 6: space_top = VSpace(VSpace::VFILL); break;
2671         case 7: space_top = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_above))); break;
2672         }
2673         if (fl_get_button (fd_form_paragraph->check_space_above))
2674           space_top.setKeep (true);
2675         switch (fl_get_choice (fd_form_paragraph->choice_space_below)) {
2676         case 1: space_bottom = VSpace(VSpace::NONE); break;
2677         case 2: space_bottom = VSpace(VSpace::DEFSKIP); break;
2678         case 3: space_bottom = VSpace(VSpace::SMALLSKIP); break;
2679         case 4: space_bottom = VSpace(VSpace::MEDSKIP); break;
2680         case 5: space_bottom = VSpace(VSpace::BIGSKIP); break;
2681         case 6: space_bottom = VSpace(VSpace::VFILL); break;
2682         case 7: space_bottom = VSpace(LyXGlueLength (fl_get_input (fd_form_paragraph->input_space_below))); break;
2683         }
2684         if (fl_get_button (fd_form_paragraph->check_space_below))
2685           space_bottom.setKeep (true);
2686
2687         if (fl_get_button(fd_form_paragraph->radio_align_left))
2688                 align = LYX_ALIGN_LEFT;
2689         else if (fl_get_button(fd_form_paragraph->radio_align_right))
2690                 align = LYX_ALIGN_RIGHT;
2691         else if (fl_get_button(fd_form_paragraph->radio_align_center))
2692                 align = LYX_ALIGN_CENTER;
2693         else 
2694                 align = LYX_ALIGN_BLOCK;
2695    
2696         labelwidthstring = fl_get_input(fd_form_paragraph->input_labelwidth);
2697         noindent = fl_get_button(fd_form_paragraph->check_noindent);
2698    
2699         current_view->currentBuffer()->text->SetParagraph(line_top,
2700                                                           line_bottom,
2701                                                           pagebreak_top,
2702                                                           pagebreak_bottom,
2703                                                           space_top,
2704                                                           space_bottom,
2705                                                           align, 
2706                                                           labelwidthstring,
2707                                                           noindent);
2708         current_view->currentBuffer()->update(1);
2709         minibuffer->Set(_("Paragraph layout set"));
2710 }
2711
2712
2713 void ParagraphCancelCB(FL_OBJECT *, long)
2714 {
2715         fl_hide_form(fd_form_paragraph->form_paragraph);
2716 }
2717
2718
2719 void ParagraphOKCB(FL_OBJECT *ob, long data)
2720 {
2721         ParagraphApplyCB(ob, data);
2722         ParagraphCancelCB(ob, data);
2723 }
2724
2725
2726 /* callbacks for form form_character */
2727
2728 void CharacterApplyCB(FL_OBJECT *, long)
2729 {
2730         // we set toggleall locally here, since it should be true for
2731         // all other uses of ToggleAndShow() (JMarc)
2732         toggleall = fl_get_button(fd_form_character->check_toggle_all);
2733         ToggleAndShow( UserFreeFont());
2734         toggleall = true;
2735 }
2736
2737
2738 void CharacterCloseCB(FL_OBJECT *, long)
2739 {
2740         fl_hide_form(fd_form_character->form_character);
2741 }
2742
2743
2744 void CharacterOKCB(FL_OBJECT *ob, long data)
2745 {
2746         CharacterApplyCB(ob,data);
2747         CharacterCloseCB(ob,data);
2748 }
2749
2750
2751 /* callbacks for form form_document */
2752
2753 void UpdateDocumentButtons(BufferParams const &params) 
2754 {
2755         fl_set_choice(fd_form_document->choice_pagestyle, 1);
2756         
2757         if (params.sides == 2)
2758                 fl_set_button(fd_form_document->radio_sides_two, 1);
2759         else
2760                 fl_set_button(fd_form_document->radio_sides_one, 1);
2761         
2762         if (params.columns == 2)
2763                 fl_set_button(fd_form_document->radio_columns_two, 1);
2764         else
2765                 fl_set_button(fd_form_document->radio_columns_one, 1);
2766         
2767         fl_set_input(fd_form_document->input_extra, params.options.c_str());
2768         fl_set_counter_value(fd_form_document->slider_secnumdepth, 
2769                              params.secnumdepth);
2770         fl_set_counter_value(fd_form_document->slider_tocdepth, 
2771                              params.tocdepth);
2772         
2773 }
2774
2775 void ChoiceClassCB(FL_OBJECT *ob, long)
2776 {
2777         ProhibitInput();
2778         if (lyxstyle.Load(fl_get_choice(ob)-1)) {
2779                 if (AskQuestion(_("Should I set some parameters to"),
2780                                 fl_get_choice_text(ob),
2781                                 _("the defaults of this document class?"))) {
2782                         BufferParams params = BufferParams();
2783                         params.textclass = fl_get_choice(ob)-1;
2784                         params.useClassDefaults();
2785                         UpdateLayoutDocument(&params);
2786                         UpdateDocumentButtons(params);
2787                 }
2788         } else {
2789                 // unable to load new style
2790                 WriteAlert(_("Conversion Errors!"),
2791                            _("Unable to switch to new document class."),
2792                            _("Reverting to original document class."));
2793                 fl_set_choice(fd_form_document->choice_class, 
2794                               GetCurrentTextClass() + 1);
2795         }
2796         AllowInput();
2797 }
2798
2799
2800 void DocumentDefskipCB(FL_OBJECT *obj, long)
2801 {
2802         // "Synchronize" the choice and the input field, so that it
2803         // is impossible to commit senseless data.
2804         const FD_form_document* fd = fd_form_document;
2805
2806         if (obj == fd->choice_default_skip) {
2807                 if (fl_get_choice (fd->choice_default_skip) != 4) {
2808                         fl_set_input (fd->input_default_skip, "");
2809                         ActivateDocumentButtons();
2810                 }
2811         } else if (obj == fd->input_default_skip) {
2812
2813                 const char* input = fl_get_input (fd->input_default_skip);
2814
2815                 if (!*input) {
2816                         fl_set_choice (fd->choice_default_skip, 2);
2817                         ActivateDocumentButtons();
2818                 } else if (isValidGlueLength (input)) {
2819                         fl_set_choice (fd->choice_default_skip, 4);
2820                         ActivateDocumentButtons();
2821                 } else {
2822                         fl_set_choice (fd->choice_default_skip, 4);
2823                         DeactivateDocumentButtons();
2824                 }
2825         }
2826 }
2827
2828
2829 void DocumentSpacingCB(FL_OBJECT *obj, long)
2830 {
2831         // "Synchronize" the choice and the input field, so that it
2832         // is impossible to commit senseless data.
2833         const FD_form_document* fd = fd_form_document;
2834
2835         if (obj == fd->choice_spacing
2836             && fl_get_choice (fd->choice_spacing) != 4) {
2837                 fl_set_input(fd->input_spacing, "");
2838         } else if (obj == fd->input_spacing) {
2839
2840                 const char* input = fl_get_input (fd->input_spacing);
2841
2842                 if (!*input) {
2843                         fl_set_choice (fd->choice_spacing, 1);
2844                 } else {
2845                         fl_set_choice (fd->choice_spacing, 4);
2846                 }
2847         }
2848 }
2849
2850
2851 void DocumentApplyCB(FL_OBJECT *, long)
2852 {
2853         bool redo = false;
2854         BufferParams *params = &(current_view->currentBuffer()->params);
2855         current_view->currentBuffer()->params.language =
2856                 combo_language->getline();
2857
2858         // If default skip is a "Length" but there's no text in the
2859         // input field, reset the kind to "Medskip", which is the default.
2860         if (fl_get_choice (fd_form_document->choice_default_skip) == 4
2861             && !*(fl_get_input (fd_form_document->input_default_skip))) {
2862                 fl_set_choice (fd_form_document->choice_default_skip, 2);
2863         }
2864
2865         /* this shouldn't be done automatically IMO. For example I write german
2866          * documents with an american keyboard very often. Matthias */
2867    
2868         /* ChangeKeymap(buffer->parameters.language, TRUE, false,
2869            fl_get_choice(fd_form_document->choice_language)); */
2870         params->fonts =
2871                 fl_get_choice_text(fd_form_document->choice_fonts);
2872         params->inputenc =
2873                 fl_get_choice_text(fd_form_document->choice_inputenc);
2874         params->fontsize =
2875                 fl_get_choice_text(fd_form_document->choice_fontsize);
2876         params->pagestyle =
2877                 fl_get_choice_text(fd_form_document->choice_pagestyle);
2878         params->graphicsDriver =
2879                 fl_get_choice_text(fd_form_document->choice_postscript_driver);
2880         params->use_amsmath =
2881                 fl_get_button(fd_form_document->check_use_amsmath);
2882    
2883         if (!current_view->available())
2884                 return;
2885
2886         char new_class = fl_get_choice(fd_form_document->choice_class) - 1;
2887         if (params->textclass != new_class) {
2888                 // try to load new_class
2889                 if (lyxstyle.Load(new_class)) {
2890                         // successfully loaded
2891                         redo = true;
2892                         minibuffer->Set(_("Converting document to new document class..."));
2893                         int ret = current_view->currentBuffer()->
2894                                 text->
2895                                 SwitchLayoutsBetweenClasses(current_view->currentBuffer()->
2896                                                             params.textclass,
2897                                                             new_class,
2898                                                             current_view->currentBuffer()->
2899                                                             paragraph);
2900
2901                         if (ret){
2902                                 LString s;
2903                                 if (ret==1)
2904                                         s= _("One paragraph couldn't be converted");
2905                                 else {
2906                                         s += ret;
2907                                         s += _(" paragraphs couldn't be converted");
2908                                 }
2909                                 WriteAlert(_("Conversion Errors!"),s,
2910                                            _("into chosen document class"));
2911                         }
2912
2913                         params->textclass = new_class;
2914                 } else {
2915                         // problem changing class -- warn user and retain old style
2916                         WriteAlert(_("Conversion Errors!"),
2917                                    _("Unable to switch to new document class."),
2918                                    _("Reverting to original document class."));
2919                         fl_set_choice(fd_form_document->choice_class, params->textclass + 1);
2920                 }
2921         }
2922
2923         char tmpsep = params->paragraph_separation;
2924         if (fl_get_button(fd_form_document->radio_indent))
2925                 params->paragraph_separation = LYX_PARSEP_INDENT;
2926         else
2927                 params->paragraph_separation = LYX_PARSEP_SKIP;
2928         if (tmpsep != params->paragraph_separation)
2929                 redo = true;
2930    
2931         VSpace tmpdefskip = params->getDefSkip();
2932         switch (fl_get_choice (fd_form_document->choice_default_skip)) {
2933         case 1: params->setDefSkip(VSpace(VSpace::SMALLSKIP)); break;
2934         case 2: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2935         case 3: params->setDefSkip(VSpace(VSpace::BIGSKIP)); break;
2936         case 4: params->setDefSkip( 
2937                 VSpace (LyXGlueLength (fl_get_input 
2938                                        (fd_form_document->input_default_skip))));
2939         break;
2940         // DocumentDefskipCB assures that this never happens
2941         default: params->setDefSkip(VSpace(VSpace::MEDSKIP)); break;
2942         }
2943         if (!(tmpdefskip == params->getDefSkip()))
2944                 redo = true;
2945
2946         if (fl_get_button(fd_form_document->radio_columns_two))
2947                 params->columns = 2;
2948         else
2949                 params->columns = 1;
2950         if (fl_get_button(fd_form_document->radio_sides_two))
2951                 params->sides = 2;
2952         else
2953                 params->sides = 1;
2954
2955         Spacing tmpSpacing = params->spacing;
2956         switch(fl_get_choice(fd_form_document->choice_spacing)) {
2957         case 1:
2958                 lyxerr.debug("Spacing: SINGLE");
2959                 params->spacing.set(Spacing::Single);
2960                 break;
2961         case 2:
2962                 lyxerr.debug("Spacing: ONEHALF");
2963                 params->spacing.set(Spacing::Onehalf);
2964                 break;
2965         case 3:
2966                 lyxerr.debug("Spacing: DOUBLE");
2967                 params->spacing.set(Spacing::Double);
2968                 break;
2969         case 4:
2970                 lyxerr.debug("Spacing: OTHER");
2971                 params->spacing.set(Spacing::Other, 
2972                                     fl_get_input(fd_form_document->input_spacing));
2973                 break;
2974         }
2975         if (tmpSpacing != params->spacing)
2976                 redo = true;
2977         
2978         signed char tmpchar = (signed char) 
2979                 fl_get_counter_value(fd_form_document->slider_secnumdepth);;
2980         if (params->secnumdepth != tmpchar)
2981                 redo = true;
2982         params->secnumdepth = tmpchar;
2983    
2984         params->tocdepth = (signed char) 
2985                 fl_get_counter_value(fd_form_document->slider_tocdepth);;
2986
2987         params->float_placement =
2988                 fl_get_input(fd_form_document->input_float_placement);
2989
2990         // More checking should be done to ensure the string doesn't have
2991         // spaces or illegal placement characters in it. (thornley)
2992
2993         if (redo)
2994                 current_view->redoCurrentBuffer();
2995    
2996         minibuffer->Set(_("Document layout set"));
2997         current_view->currentBuffer()->markDirty();
2998
2999         params->options =
3000                 fl_get_input(fd_form_document->input_extra);
3001    
3002 }
3003
3004
3005 void DocumentCancelCB(FL_OBJECT *, long)
3006 {
3007         fl_hide_form(fd_form_document->form_document);
3008 }
3009
3010
3011 void DocumentOKCB(FL_OBJECT *ob, long data)
3012 {
3013         DocumentCancelCB(ob,data);
3014         DocumentApplyCB(ob,data);
3015 }
3016
3017
3018 void DocumentBulletsCB(FL_OBJECT *, long)
3019 {
3020         bulletForm();
3021         // bullet callbacks etc. in bullet_panel.C -- ARRae
3022 }
3023
3024
3025 void GotoNote()
3026 {
3027         if (!current_view->getScreen())
3028                 return;
3029    
3030         current_view->getScreen()->HideCursor();
3031         BeforeChange();
3032         current_view->currentBuffer()->update(-2);
3033         LyXCursor tmp;
3034    
3035         if (!current_view->currentBuffer()->text->GotoNextNote()) {
3036                 if (current_view->currentBuffer()->text->cursor.pos 
3037                     || current_view->currentBuffer()->text->cursor.par !=
3038                     current_view->currentBuffer()->text->FirstParagraph())
3039                         {
3040                                 tmp = current_view->currentBuffer()->text->cursor;
3041                                 current_view->currentBuffer()->text->cursor.par =
3042                                         current_view->currentBuffer()->text->FirstParagraph();
3043                                 current_view->currentBuffer()->text->cursor.pos = 0;
3044                                 if (!current_view->currentBuffer()->text->GotoNextNote()) {
3045                                         current_view->currentBuffer()->text->cursor = tmp;
3046                                         minibuffer->Set(_("No more notes"));
3047                                         LyXBell();
3048                                 }
3049                         } else {
3050                                 minibuffer->Set(_("No more notes"));
3051                                 LyXBell();
3052                         }
3053         }
3054         current_view->currentBuffer()->update(0);
3055         current_view->currentBuffer()->text->sel_cursor =
3056                 current_view->currentBuffer()->text->cursor;
3057 }
3058
3059
3060 void InsertCorrectQuote()
3061 {
3062         Buffer *cbuffer = current_view->currentBuffer();
3063         char c;
3064
3065         if  (cbuffer->text->cursor.pos )
3066                 c = cbuffer->text->cursor.par->GetChar(cbuffer->text->cursor.pos - 1);
3067         else 
3068                 c = ' ';
3069
3070         cbuffer->insertInset(new InsetQuotes(c, cbuffer->params));
3071 }
3072
3073
3074 /* callbacks for form form_quotes */
3075
3076 void QuotesApplyCB(FL_OBJECT *, long)
3077 {
3078         if (!current_view->available())
3079                 return;
3080         
3081         minibuffer->Set(_("Quotes type set"));
3082         //current_view->currentBuffer()->params.quotes_language =
3083         //      fl_get_choice(fd_form_quotes->choice_quotes_language) - 1;
3084         InsetQuotes::quote_language lga = InsetQuotes::EnglishQ;
3085         switch(fl_get_choice(fd_form_quotes->choice_quotes_language) - 1) {
3086         case 0:
3087                 lga = InsetQuotes::EnglishQ;
3088                 break;
3089         case 1:
3090                 lga = InsetQuotes::SwedishQ;
3091                 break;
3092         case 2:
3093                 lga = InsetQuotes::GermanQ;
3094                 break;
3095         case 3:
3096                 lga = InsetQuotes::PolishQ;
3097                 break;
3098         case 4:
3099                 lga = InsetQuotes::FrenchQ;
3100                 break;
3101         case 5:
3102                 lga = InsetQuotes::DanishQ;
3103                 break;
3104         }
3105         current_view->currentBuffer()->params.quotes_language = lga;
3106         if (fl_get_button(fd_form_quotes->radio_single))   
3107                 current_view->currentBuffer()->
3108                         params.quotes_times = InsetQuotes::SingleQ;
3109         else
3110                 current_view->currentBuffer()->
3111                         params.quotes_times = InsetQuotes::DoubleQ;
3112 }
3113
3114
3115 void QuotesCancelCB(FL_OBJECT *, long)
3116 {
3117         fl_hide_form(fd_form_quotes->form_quotes);
3118 }
3119
3120
3121 void QuotesOKCB(FL_OBJECT *ob, long data)
3122 {
3123         QuotesApplyCB(ob, data);
3124         QuotesCancelCB(ob, data);
3125 }
3126
3127
3128
3129 /* callbacks for form form_preamble */
3130
3131 void PreambleCancelCB(FL_OBJECT *, long)
3132 {
3133         fl_hide_form(fd_form_preamble->form_preamble);
3134 }
3135
3136
3137 void PreambleApplyCB(FL_OBJECT *, long)
3138 {
3139         if (!current_view->available())
3140                 return;
3141         
3142         current_view->currentBuffer()->params.preamble =
3143                 fl_get_input(fd_form_preamble->input_preamble);
3144         current_view->currentBuffer()->markDirty();
3145         minibuffer->Set(_("LaTeX preamble set"));
3146 }
3147
3148    
3149 void PreambleOKCB(FL_OBJECT *ob, long data)
3150 {
3151         PreambleApplyCB(ob, data);
3152         PreambleCancelCB(ob, data);
3153 }
3154
3155
3156 /* callbacks for form form_table */
3157
3158 void TableApplyCB(FL_OBJECT *, long)
3159 {
3160         int xsize,ysize;
3161         if (!current_view->getScreen())
3162                 return;
3163    
3164         // check for tables in tables
3165         if (current_view->currentBuffer()->text->cursor.par->table){
3166                 WriteAlert(_("Impossible Operation!"),
3167                            _("Cannot insert table in table."),
3168                            _("Sorry."));
3169                 return;
3170         }
3171  
3172         minibuffer->Set(_("Inserting table..."));
3173
3174         ysize = (int)(fl_get_slider_value(fd_form_table->slider_columns) + 0.5);
3175         xsize = (int)(fl_get_slider_value(fd_form_table->slider_rows) + 0.5);
3176    
3177    
3178         current_view->getScreen()->HideCursor();
3179         BeforeChange();
3180         current_view->currentBuffer()->update(-2);
3181    
3182         current_view->currentBuffer()->text->SetCursorParUndo(); 
3183         current_view->currentBuffer()->text->FreezeUndo();
3184
3185         current_view->currentBuffer()->text->BreakParagraph();
3186         current_view->currentBuffer()->update(-1);
3187    
3188         if (current_view->currentBuffer()->text->cursor.par->Last()) {
3189                 current_view->currentBuffer()->text->CursorLeft();
3190       
3191                 current_view->currentBuffer()->text->BreakParagraph();
3192                 current_view->currentBuffer()->update(-1);
3193         }
3194
3195         current_view->currentBuffer()->text->current_font.setLatex(LyXFont::OFF);
3196         //if (!fl_get_button(fd_form_table->check_latex)){
3197         // insert the new wysiwy table
3198         current_view->currentBuffer()->text->SetLayout(0); // standard layout
3199         if (current_view->currentBuffer()->text->cursor.par->footnoteflag ==
3200             LyXParagraph::NO_FOOTNOTE) {
3201                 current_view->currentBuffer()->
3202                         text->SetParagraph(0, 0,
3203                                            0, 0,
3204                                            VSpace (0.3 * current_view->currentBuffer()->
3205                                                    params.spacing.getValue(),
3206                                                    LyXLength::CM),
3207                                            VSpace (0.3 * current_view->currentBuffer()->
3208                                                    params.spacing.getValue(),
3209                                                    LyXLength::CM),
3210                                            LYX_ALIGN_CENTER,
3211                                            LString(),
3212                                            0);
3213         }
3214         else
3215                 current_view->currentBuffer()->text->SetParagraph(0, 0,
3216                                                                   0, 0,
3217                                                                   VSpace(VSpace::NONE),
3218                                                                   VSpace(VSpace::NONE),
3219                                            LYX_ALIGN_CENTER, 
3220                                            LString(),
3221                                            0);
3222
3223         current_view->currentBuffer()->text->cursor.par->table = new LyXTable(xsize, ysize);
3224         int i;
3225         for (i=0; i<xsize * ysize - 1; i++)
3226                 current_view->currentBuffer()->text->cursor.par->InsertChar(0,LYX_META_NEWLINE);
3227         current_view->currentBuffer()->text->RedoParagraph();
3228    
3229         current_view->currentBuffer()->text->UnFreezeUndo();
3230      
3231         current_view->currentBuffer()->update(1);
3232         minibuffer->Set(_("Table inserted"));
3233 }
3234
3235
3236 void TableCancelCB(FL_OBJECT *, long)
3237 {
3238         fl_hide_form(fd_form_table->form_table);
3239 }
3240
3241
3242 void TableOKCB(FL_OBJECT *ob, long data)
3243 {
3244         TableApplyCB(ob,data);
3245         TableCancelCB(ob,data);
3246 }
3247
3248
3249 /* callbacks for form form_print */
3250
3251 void PrintCancelCB(FL_OBJECT *, long)
3252 {
3253         fl_hide_form(fd_form_print->form_print);
3254 }
3255
3256 static bool LStringOnlyContains (LString const & LStr, const char * cset)
3257 {
3258         const char * cstr = LStr.c_str() ;
3259
3260         return strspn(cstr,cset) == strlen(cstr) ;
3261 }
3262
3263 void PrintApplyCB(FL_OBJECT *, long)
3264 {
3265         if (!current_view->available())
3266                 return;
3267         Buffer *buffer = current_view->currentBuffer();
3268         LString path = OnlyPath(buffer->getFileName());
3269
3270         LString pageflag;
3271         if (fl_get_button(fd_form_print->radio_even_pages))
3272                 pageflag = lyxrc->print_evenpage_flag + ' ';
3273         else if (fl_get_button(fd_form_print->radio_odd_pages))
3274                 pageflag = lyxrc->print_oddpage_flag + ' ';
3275
3276 // Changes by Stephan Witt (stephan.witt@beusen.de), 19-Jan-99
3277 // User may give a page (range) list
3278 // User may print multiple (unsorted) copies
3279         LString pages = fl_get_input(fd_form_print->input_pages);
3280         pages.subst(';',',');
3281         pages.subst('+',',');
3282         pages.strip () ;
3283         pages.frontStrip () ;
3284         while (!pages.empty()) { // a page range was given
3285                 LString piece ;
3286                 //int dashpos ; // unused
3287                 pages = pages.split (piece, ',') ;
3288                 piece.strip () ;
3289                 piece.frontStrip () ;
3290                 if ( !LStringOnlyContains (piece, "0123456789-") ) {
3291                         WriteAlert(_("ERROR!  Unable to print!"),
3292                                 _("Check 'range of pages'!"));
3293                         return;
3294                 }
3295                 if ( -1 == piece.charPos ('-') ) { // not found
3296                         pageflag += lyxrc->print_pagerange_flag + piece + '-' + piece + ' ' ;
3297                 } else if ( piece.suffixIs("-") ) { // missing last page
3298                         pageflag += lyxrc->print_pagerange_flag + piece + 1000 + ' ' ;
3299                 } else if ( piece.prefixIs("-") ) { // missing first page
3300                         pageflag += lyxrc->print_pagerange_flag + 1 + piece + ' ' ;
3301                 } else {
3302                         pageflag += lyxrc->print_pagerange_flag + piece + ' ' ;
3303                 }
3304         }
3305    
3306         LString copies = fl_get_input(fd_form_print->input_copies);
3307         copies.strip () ;
3308         copies.frontStrip () ;
3309         if (!copies.empty()) { // a number of copies was given
3310                 if ( !LStringOnlyContains (copies, "0123456789") ) {
3311                         WriteAlert(_("ERROR!  Unable to print!"),
3312                                 _("Check 'number of copies'!"));
3313                         return;
3314                 }
3315                 if (fl_get_button(fd_form_print->do_unsorted))
3316                         pageflag += lyxrc->print_copies_flag;
3317                 else
3318                         pageflag += lyxrc->print_collcopies_flag;
3319                 pageflag += " " + copies + ' ' ;
3320         }
3321
3322         LString reverseflag;
3323         if (fl_get_button(fd_form_print->radio_order_reverse))
3324                 reverseflag = lyxrc->print_reverse_flag + ' ';
3325    
3326         LString orientationflag;
3327         if (buffer->params.orientation == ORIENTATION_LANDSCAPE)
3328                 orientationflag = lyxrc->print_landscape_flag + ' ';
3329    
3330         LString ps_file = SpaceLess(fl_get_input(fd_form_print->input_file));
3331         LString printer = fl_get_input(fd_form_print->input_printer);
3332         printer.strip();
3333
3334         LString printerflag;
3335         if (lyxrc->print_adapt_output // printer name should be passed to dvips
3336             && ! printer.empty()) // a printer name has been given
3337                 printerflag = lyxrc->print_to_printer + printer + ' ';
3338      
3339         LString extraflags;
3340         if (!lyxrc->print_extra_options.empty())
3341                 extraflags = lyxrc->print_extra_options + ' ';
3342
3343         LString command = lyxrc->print_command + ' ' 
3344                 + printerflag + pageflag + reverseflag 
3345                 + orientationflag + extraflags;
3346  
3347         char real_papersize = buffer->params.papersize;
3348         if (real_papersize == PAPER_DEFAULT)
3349                 real_papersize = lyxrc->default_papersize;
3350         LString
3351             paper;
3352
3353         switch (real_papersize) {
3354         case PAPER_USLETTER:
3355                 paper = "letter";
3356                 break;
3357         case PAPER_A3PAPER:
3358                 paper = "a3";
3359                 break;
3360         case PAPER_A4PAPER:
3361                 paper = "a4";
3362                 break;
3363         case PAPER_A5PAPER:
3364                 paper = "a5";
3365                 break;
3366         case PAPER_B5PAPER:
3367                 paper = "b5";
3368                 break;
3369         case PAPER_EXECUTIVEPAPER:
3370                 paper = "foolscap";
3371                 break;
3372         case PAPER_LEGALPAPER:
3373                 paper = "legal";
3374                 break;
3375         default: /* If nothing else fits, keep an empty value... */
3376                 break;
3377         }
3378
3379         if (buffer->params.use_geometry
3380             && buffer->params.papersize2 == VM_PAPER_CUSTOM
3381             && !lyxrc->print_paper_dimension_flag.empty()
3382             && !buffer->params.paperwidth.empty()
3383             && !buffer->params.paperheight.empty()) {
3384                 // using a custom papersize
3385                 command += ' ';
3386                 command += lyxrc->print_paper_dimension_flag + ' ';
3387                 command += buffer->params.paperwidth + ',';
3388                 command += buffer->params.paperheight + ' ';
3389         } else if (!lyxrc->print_paper_flag.empty()
3390                    && !paper.empty()
3391                    && (real_papersize != PAPER_USLETTER ||
3392                        buffer->params.orientation == ORIENTATION_PORTRAIT)) {
3393                 command += " " + lyxrc->print_paper_flag + " " + paper + " ";
3394         }
3395         if (fl_get_button(fd_form_print->radio_file))
3396                 command += lyxrc->print_to_file + '\"'
3397                         + MakeAbsPath(ps_file, path)
3398                         + '\"';
3399         else if (!lyxrc->print_spool_command.empty())
3400                 command += lyxrc->print_to_file 
3401                         + '\"' + ps_file + '\"';
3402         
3403         // push directorypath, if necessary 
3404         if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)){
3405                 path = buffer->tmppath;
3406         }
3407         PathPush(path);
3408
3409         bool result;
3410         if (!lyxrc->print_spool_command.empty() && 
3411             !fl_get_button(fd_form_print->radio_file)) {
3412                 LString command2 = lyxrc->print_spool_command + ' ';
3413                 if (!printer.empty())
3414                         command2 += lyxrc->print_spool_printerprefix 
3415                                     + printer;
3416                 // First run dvips and, if succesful, then spool command
3417                 if ((result = RunScript(buffer, true, command))) {
3418                         result = RunScript(buffer, false, command2, ps_file);
3419                 }
3420         } else
3421                 result = RunScript(buffer, false, command);
3422         PathPop();
3423
3424         if (!result)
3425                 WriteAlert(_("Error:"),
3426                            _("Unable to print"),
3427                            _("Check that your parameters are correct"));
3428 }
3429
3430
3431 void PrintOKCB(FL_OBJECT *ob, long data)
3432 {
3433         PrintCancelCB(ob, data);  
3434         PrintApplyCB(ob,data);
3435 }
3436
3437
3438 /* callbacks for form form_figure */
3439
3440 void FigureApplyCB(FL_OBJECT *, long)
3441 {
3442         if (!current_view->available())
3443                 return;
3444
3445         Buffer * buffer = current_view->currentBuffer();
3446         if(buffer->isReadonly()) // paranoia
3447                 return;
3448         
3449         minibuffer->Set(_("Inserting figure..."));
3450         if (fl_get_button(fd_form_figure->radio_inline)
3451             || buffer->text->cursor.par->table) {
3452                 InsetFig * new_inset = new InsetFig(100, 20, buffer);
3453                 buffer->insertInset(new_inset);
3454                 minibuffer->Set(_("Figure inserted"));
3455                 new_inset->Edit(0, 0);
3456                 return;
3457         }
3458         
3459         current_view->getScreen()->HideCursor();
3460         buffer->update(-2);
3461         BeforeChange();
3462       
3463         buffer->text->SetCursorParUndo(); 
3464         buffer->text->FreezeUndo();
3465
3466         buffer->text->BreakParagraph();
3467         buffer->update(-1);
3468       
3469         if (buffer->text->cursor.par->Last()) {
3470                 buffer->text->CursorLeft();
3471          
3472                 buffer->text->BreakParagraph();
3473                 buffer->update(-1);
3474         }
3475
3476         // The standard layout should always be numer 0;
3477         buffer->text->SetLayout(0);
3478         
3479         if (buffer->text->cursor.par->footnoteflag ==
3480             LyXParagraph::NO_FOOTNOTE) {
3481                 buffer->text->SetParagraph(0, 0,
3482                                            0, 0,
3483                                            VSpace (0.3 * buffer->params.spacing.getValue(),
3484                                                    LyXLength::CM),
3485                                            VSpace (0.3 *
3486                                                    buffer->params.spacing.getValue(),
3487                                                    LyXLength::CM),
3488                                            LYX_ALIGN_CENTER, LString(), 0);
3489         } else
3490                 buffer->text->SetParagraph(0, 0,
3491                                            0, 0,
3492                                            VSpace(VSpace::NONE),
3493                                            VSpace(VSpace::NONE),
3494                                            LYX_ALIGN_CENTER, 
3495                                            LString(),
3496                                            0);
3497         
3498         buffer->update(-1);
3499       
3500         Inset *new_inset = NULL;
3501         
3502         new_inset = new InsetFig(100, 100, buffer);
3503         buffer->insertInset(new_inset);
3504         new_inset->Edit(0, 0);
3505         buffer->update(0);
3506         minibuffer->Set(_("Figure inserted"));
3507         buffer->text->UnFreezeUndo();
3508 }
3509
3510    
3511 void FigureCancelCB(FL_OBJECT *, long)
3512 {
3513         fl_hide_form(fd_form_figure->form_figure);
3514 }
3515
3516
3517 void FigureOKCB(FL_OBJECT *ob, long data)
3518 {
3519         FigureApplyCB(ob,data);
3520         FigureCancelCB(ob,data);
3521 }
3522
3523 void ScreenApplyCB(FL_OBJECT *, long)
3524 {
3525         lyxrc->roman_font_name = fl_get_input(fd_form_screen->input_roman);
3526         lyxrc->sans_font_name = fl_get_input(fd_form_screen->input_sans);
3527         lyxrc->typewriter_font_name = fl_get_input(fd_form_screen->input_typewriter);
3528         lyxrc->font_norm = fl_get_input(fd_form_screen->input_font_norm);
3529         lyxrc->zoom = atoi(fl_get_input(fd_form_screen->intinput_size));
3530         fontloader.update();
3531    
3532         // All buffers will need resize
3533         bufferlist.resize();
3534
3535         minibuffer->Set(_("Screen options set"));
3536 }
3537
3538
3539 void ScreenCancelCB(FL_OBJECT *, long)
3540 {
3541         fl_hide_form(fd_form_screen->form_screen);
3542 }
3543
3544
3545 void ScreenOKCB(FL_OBJECT *ob, long data)
3546 {
3547         ScreenCancelCB(ob,data);
3548         ScreenApplyCB(ob,data);
3549 }
3550
3551
3552 void LaTeXOptions()
3553 {
3554         if (!current_view->available())
3555                 return;
3556
3557         fl_set_button(fd_latex_options->accents,
3558                       (int)current_view->currentBuffer()->params.allowAccents);
3559         
3560         if (fd_latex_options->LaTeXOptions->visible) {
3561                 fl_raise_form(fd_latex_options->LaTeXOptions);
3562         } else {
3563                 fl_show_form(fd_latex_options->LaTeXOptions,
3564                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
3565                              _("LaTeX Options"));
3566         }
3567 }
3568
3569
3570 // This function runs "configure" and then rereads lyx.defaults to
3571 // reconfigure the automatic settings.
3572 void Reconfigure()
3573 {
3574         minibuffer->Set(_("Running configure..."));
3575
3576         // Run configure in user lyx directory
3577         PathPush(user_lyxdir);
3578         Systemcalls one(Systemcalls::System, 
3579                           AddName(system_lyxdir,"configure"));
3580         PathPop();
3581         minibuffer->Set(_("Reloading configuration..."));
3582         lyxrc->Read(LibFileSearch(LString(), "lyxrc.defaults"));
3583         WriteAlert(_("The system has been reconfigured."), 
3584                    _("You need to restart LyX to make use of any"),
3585                    _("updated document class specifications."));
3586 }
3587
3588
3589 /* these functions are for the spellchecker */ 
3590 char* NextWord(float &value)
3591 {
3592         if (!current_view->available()){
3593                 value = 1;
3594                 return NULL;
3595         }
3596    
3597         char* string =  current_view->currentBuffer()->text->SelectNextWord(value);
3598
3599         return string;
3600 }
3601
3602   
3603 void SelectLastWord()
3604 {
3605         if (!current_view->available())
3606                 return;
3607    
3608         current_view->getScreen()->HideCursor();
3609         BeforeChange(); 
3610         current_view->currentBuffer()->text->SelectSelectedWord();
3611         current_view->getScreen()->ToggleSelection(false);
3612         current_view->currentBuffer()->update(0);
3613 }
3614
3615
3616 void EndOfSpellCheck()
3617 {
3618         if (!current_view->available())
3619                 return;
3620    
3621         current_view->getScreen()->HideCursor();
3622         BeforeChange(); 
3623         current_view->currentBuffer()->text->SelectSelectedWord();
3624         current_view->currentBuffer()->text->ClearSelection();
3625         current_view->currentBuffer()->update(0);
3626 }
3627
3628
3629 void ReplaceWord(LString const & replacestring)
3630 {
3631         if (!current_view->getScreen())
3632                 return;
3633
3634         current_view->getScreen()->HideCursor();
3635         current_view->currentBuffer()->update(-2);
3636    
3637         /* clear the selection (if there is any) */ 
3638         current_view->getScreen()->ToggleSelection(false);
3639         current_view->currentBuffer()->text->
3640                 ReplaceSelectionWithString(replacestring.c_str());
3641    
3642         current_view->currentBuffer()->text->SetSelectionOverString(replacestring.c_str());
3643
3644         // Go back so that replacement string is also spellchecked
3645         for (int i=-1; i< replacestring.length(); i++) {
3646                 current_view->currentBuffer()->text->CursorLeftIntern();
3647         }
3648         current_view->currentBuffer()->update(1);
3649 }
3650 // End of spellchecker stuff
3651
3652
3653
3654 //
3655 // Table of Contents
3656 //
3657
3658 struct TocList {
3659         int counter[6];
3660         bool appendix;
3661         TocList *next;
3662 };
3663
3664
3665 static TocList* toclist = NULL;
3666
3667
3668 void TocSelectCB(FL_OBJECT *ob, long)
3669 {
3670         if (!current_view->available())
3671                 return;
3672    
3673         TocList* tmptoclist = toclist;
3674         int i = fl_get_browser(ob);
3675         int a = 0;
3676
3677         for (a=1; a<i && tmptoclist->next; a++){
3678                 tmptoclist = tmptoclist->next;
3679         }
3680
3681         if (!tmptoclist)
3682                 return;
3683      
3684
3685         LyXParagraph *par = current_view->currentBuffer()->paragraph;
3686         while (par && (par->GetFirstCounter(0) != tmptoclist->counter[0] ||
3687                        par->GetFirstCounter(1) != tmptoclist->counter[1] ||
3688                        par->GetFirstCounter(2) != tmptoclist->counter[2] ||
3689                        par->GetFirstCounter(3) != tmptoclist->counter[3] ||
3690                        par->GetFirstCounter(4) != tmptoclist->counter[4] ||
3691                        par->GetFirstCounter(5) != tmptoclist->counter[5] ||
3692                        par->appendix != tmptoclist->appendix)) {
3693                 par = par->LastPhysicalPar()->Next();
3694         }
3695    
3696         if (par) {
3697                 BeforeChange();
3698                 current_view->currentBuffer()->text->SetCursor(par, 0);
3699                 current_view->currentBuffer()->text->sel_cursor =
3700                         current_view->currentBuffer()->text->cursor;
3701                 current_view->currentBuffer()->update(0);
3702         }
3703         else {
3704                 WriteAlert(_("Error"), 
3705                            _("Couldn't find this label"), 
3706                            _("in current document."));
3707         }
3708           
3709 }
3710
3711
3712 void TocCancelCB(FL_OBJECT *, long)
3713 {
3714         fl_hide_form(fd_form_toc->form_toc);
3715 }
3716
3717
3718 void TocUpdateCB(FL_OBJECT *, long)
3719 {
3720         static LyXParagraph* stapar = NULL;
3721         TocList *tmptoclist = NULL;
3722    
3723         /* deleted the toclist */ 
3724         if (toclist){
3725                 while (toclist){
3726                         tmptoclist = toclist->next;
3727                         delete toclist;
3728                         toclist = tmptoclist;
3729                 }
3730         }
3731         toclist = NULL;
3732         tmptoclist = toclist;
3733
3734
3735         fl_clear_browser(fd_form_toc->browser_toc);
3736         if (!current_view->available()) {
3737                 fl_add_browser_line(fd_form_toc->browser_toc, _("*** No Document ***"));
3738                 return;
3739         }
3740         fl_hide_object(fd_form_toc->browser_toc);
3741         /* get the table of contents */ 
3742         LyXParagraph *par = current_view->currentBuffer()->paragraph;
3743         char labeltype;
3744         char* line = new char[200];
3745         int i = 0;
3746         int pos = 0;
3747         unsigned char c;
3748         int topline = 0;
3749    
3750         if (stapar == par)
3751                 topline = fl_get_browser_topline(fd_form_toc->browser_toc);
3752         stapar = par;
3753    
3754         while (par) {
3755                 labeltype = lyxstyle.Style(current_view->currentBuffer()->params.textclass, 
3756                                            par->GetLayout())->labeltype;
3757       
3758                 if (labeltype >= LABEL_COUNTER_CHAPTER
3759                     && labeltype <= LABEL_COUNTER_CHAPTER +
3760                     current_view->currentBuffer()->params.tocdepth) {
3761                         /* insert this into the table of contents */ 
3762                         /* first indent a little bit */ 
3763                         
3764                         for (pos=0; 
3765                              pos < (labeltype - 
3766                                     lyxstyle.TextClass(current_view->currentBuffer()->
3767                                                        params.textclass)->maxcounter) * 4 + 2;
3768                              pos++)
3769                                 line[pos] = ' ';
3770                         
3771                         /* then the labelstring */ 
3772                         i = 0;
3773                         if (!par->labelstring.empty()) {
3774                                 while (pos < 199 && i < par->labelstring.length()) {
3775                                         line[pos] = par->labelstring[i];
3776                                         i++;
3777                                         pos++;
3778                                 }
3779                         }
3780          
3781                         line[pos] = ' ';
3782                         pos++;
3783                         
3784                         /* now the contents */ 
3785                         i = 0;
3786                         while (pos < 199 && i < par->last) {
3787                                 c = par->GetChar(i);
3788                                 if (isprint((unsigned char) c) || c >= 128) {
3789                                         line[pos] = c;
3790                                         pos++;
3791                                 }
3792                                 i++;
3793                         }
3794                         line[pos] = '\0';
3795                         fl_add_browser_line(fd_form_toc->browser_toc, line);
3796                         
3797                         /* make a toclist entry */
3798                         if (!tmptoclist){
3799                                 tmptoclist = new TocList;
3800                                 toclist = tmptoclist;
3801                         } else {
3802                                 tmptoclist->next = new TocList;
3803                                 tmptoclist = tmptoclist->next;
3804                         }
3805                         
3806                         tmptoclist->next = NULL;
3807                         int a = 0;
3808                         for (a=0; a<6; a++){
3809                                 tmptoclist->counter[a] = par->GetFirstCounter(a);
3810                         }
3811                         tmptoclist->appendix = par->appendix;
3812                 }
3813                 par = par->LastPhysicalPar()->Next();
3814                 
3815         }
3816         delete[] line;
3817         fl_set_browser_topline(fd_form_toc->browser_toc, topline);
3818         fl_show_object(fd_form_toc->browser_toc);
3819 }
3820
3821
3822 /* callbacks for form form_ref */
3823 void RefSelectCB(FL_OBJECT *, long data)
3824 {
3825         if (!current_view->available())
3826                 return;
3827
3828         LString s =
3829                 fl_get_browser_line(fd_form_ref->browser_ref,
3830                                     fl_get_browser(fd_form_ref->browser_ref));
3831         LString u = fl_get_input(fd_form_ref->ref_name);
3832         u.strip();
3833         u.frontStrip();
3834         if (s.empty())
3835                 return;
3836
3837         if (data==2) {
3838                 current_view->getOwner()->getLyXFunc()->Dispatch(LFUN_REFGOTO, s.c_str());
3839                 return;
3840         }
3841             
3842         LString t;
3843         if (data==0)
3844                 t += "\\ref";
3845         else
3846                 t += "\\pageref";
3847
3848         if(current_view->currentBuffer()->isSGML())
3849                 t += "[" + u + "]" + "{" + s + "}";
3850         else
3851                 t += "{" + s + "}";
3852
3853         Inset *new_inset =
3854                 new InsetRef(t, current_view->currentBuffer());
3855         current_view->currentBuffer()->insertInset(new_inset);
3856 }
3857
3858
3859 void RefUpdateCB(FL_OBJECT *, long)
3860 {
3861         if (!current_view->available()) {
3862                 fl_clear_browser(fd_form_ref->browser_ref);
3863                 return;
3864         }
3865
3866         FL_OBJECT * brow = fd_form_ref->browser_ref;
3867
3868         // Get the current line, in order to restore it later
3869         LString currentstr = fl_get_browser_line(brow,
3870                                                  fl_get_browser(brow));
3871
3872         fl_clear_browser(brow);
3873
3874         LString refs = current_view->currentBuffer()->getReferenceList('\n');
3875         int topline = 1;
3876
3877 #if FL_REVISION > 85
3878         fl_addto_browser_chars(brow,refs.c_str());
3879         int total_lines = fl_get_browser_maxline(brow);
3880         for (int i = 1; i <= total_lines ; i++) {
3881                 if (fl_get_browser_line(brow, i) == currentstr) {
3882                         topline = i;
3883                         break;
3884                 }
3885         }
3886         fl_set_browser_topline(brow, topline);
3887 #else
3888         // Keep the old ugly code for xforms 0.81 compatibility
3889         LString curr_ref;
3890         int ref_num = 0;
3891                                        
3892         while(true) {
3893                 curr_ref = refs.token('\n',ref_num);
3894                 if (curr_ref.empty())
3895                         break;
3896                 fl_add_browser_line(brow,curr_ref.c_str());
3897                 ref_num++;
3898         }
3899 #endif
3900
3901         if (!fl_get_browser_maxline(brow)) {
3902                 fl_add_browser_line(brow, 
3903                                     _("*** No labels found in document ***"));
3904                 fl_deactivate_object(brow);
3905         } else {
3906                 fl_select_browser_line(brow, topline);
3907                 fl_activate_object(brow);
3908         }
3909         if (current_view->currentBuffer()->isReadonly()) {
3910                 // would be better to de/activate insert buttons
3911                 // but that's more work... besides this works. ARRae
3912                 fl_hide_form(fd_form_ref->form_ref);
3913         }
3914         if (!current_view->currentBuffer()->isSGML()) {
3915                 fl_deactivate_object(fd_form_ref->ref_name);
3916                 fl_set_object_lcol(fd_form_ref->ref_name, FL_INACTIVE);
3917         }
3918         else {
3919                 fl_activate_object(fd_form_ref->ref_name);
3920                 fl_set_object_lcol(fd_form_ref->ref_name, FL_BLACK);
3921         }
3922 }
3923
3924
3925 void RefHideCB(FL_OBJECT *, long)
3926 {
3927         fl_hide_form(fd_form_ref->form_ref);
3928 }
3929
3930
3931 void UpdateInset(Inset* inset, bool mark_dirty)
3932 {
3933         if (!inset)
3934                 return;
3935
3936         /* very first check for locking insets*/
3937         if (current_view->currentBuffer()->the_locking_inset == inset){
3938                 if (current_view->currentBuffer()->text->UpdateInset(inset)){
3939                         current_view->update();
3940                         if (mark_dirty){
3941                                 if (current_view->currentBuffer()->isLyxClean())
3942                                         minibuffer->setTimer(4);
3943                                 current_view->currentBuffer()->markDirty();
3944                         }
3945                         current_view->updateScrollbar();
3946                         return;
3947                 }
3948         }
3949   
3950         /* first check the current buffer */
3951         if (current_view->available()){
3952                 current_view->getScreen()->HideCursor();
3953                 current_view->currentBuffer()->update(-3);
3954                 if (current_view->currentBuffer()->text->UpdateInset(inset)){
3955                         if (mark_dirty)
3956                                 current_view->currentBuffer()->update(1);
3957                         else 
3958                                 current_view->currentBuffer()->update(3);
3959                         return;
3960                 }
3961         }
3962   
3963         // check all buffers
3964         bufferlist.updateInset(inset, mark_dirty);
3965
3966 }
3967
3968
3969 /* these functions return 1 if an error occured, 
3970    otherwise 0 */
3971 int LockInset(UpdatableInset* inset)
3972 {
3973         if (!current_view->currentBuffer()->the_locking_inset && inset){
3974                 current_view->currentBuffer()->the_locking_inset = inset;
3975                 return 0;
3976         }
3977         return 1;
3978 }
3979
3980
3981 void ShowLockedInsetCursor(long x, long y, int asc, int desc)
3982 {
3983         if (current_view->currentBuffer()->the_locking_inset &&
3984             current_view->getScreen()){
3985                 y += current_view->currentBuffer()->text->cursor.y;
3986                 current_view->getScreen()->ShowManualCursor(x, y,
3987                                                             asc, desc);
3988         }
3989 }
3990
3991
3992 void HideLockedInsetCursor(long x, long y, int asc, int desc)
3993 {
3994         if (current_view->currentBuffer()->the_locking_inset &&
3995             current_view->getScreen()){
3996                 y += current_view->currentBuffer()->text->cursor.y;
3997                 current_view->getScreen()->HideManualCursor(x, y,
3998                                                             asc, desc);
3999         }
4000 }
4001
4002
4003 void FitLockedInsetCursor(long x, long y, int asc, int desc)
4004 {
4005         if (current_view->currentBuffer()->the_locking_inset &&
4006             current_view->getScreen()){
4007                 y += current_view->currentBuffer()->text->cursor.y;
4008                 if (current_view->getScreen()->FitManualCursor(x, y, asc, desc))
4009                         current_view->updateScrollbar();
4010         }
4011 }
4012
4013
4014 int UnlockInset(UpdatableInset* inset)
4015 {
4016         if (inset &&
4017             current_view->currentBuffer()->the_locking_inset == inset){
4018                 inset->InsetUnlock();
4019                 current_view->currentBuffer()->the_locking_inset = NULL;
4020                 current_view->currentBuffer()->text->FinishUndo();
4021                 return 0;
4022         }
4023         return bufferlist.unlockInset(inset);
4024 }
4025
4026
4027 void LockedInsetStoreUndo(Undo::undo_kind kind)
4028 {
4029         if (!current_view->currentBuffer()->the_locking_inset)
4030                 return; // shouldn't happen
4031         if (kind == Undo::EDIT) // in this case insets would not be stored!
4032                 kind = Undo::FINISH;
4033         current_view->currentBuffer()->text->SetUndo(kind,
4034                               current_view->currentBuffer()->text->cursor.par->
4035                               ParFromPos(current_view->currentBuffer()->text->cursor.pos)->previous, 
4036                               current_view->currentBuffer()->text->cursor.par->
4037                               ParFromPos(current_view->currentBuffer()->text->cursor.pos)->next); 
4038 }
4039
4040
4041 void PutInsetIntoInsetUpdateList(Inset* inset)
4042 {
4043         if (inset) {
4044                 InsetUpdateStruct* tmp = new InsetUpdateStruct();
4045                 tmp->inset = inset;
4046                 tmp->next = InsetUpdateList;
4047                 InsetUpdateList = tmp;
4048         }
4049 }
4050
4051
4052 void UpdateInsetUpdateList()
4053 {
4054         InsetUpdateStruct *tmp = InsetUpdateList;
4055         while (tmp) {
4056                 UpdateInset(tmp->inset, false); // "false" because no document change
4057                 tmp = tmp->next;
4058         }
4059   
4060         /* delete the update list */
4061         while (InsetUpdateList) {
4062                 tmp = InsetUpdateList;
4063                 InsetUpdateList = InsetUpdateList->next;
4064                 delete tmp;
4065         }
4066         InsetUpdateList = NULL;
4067 }
4068
4069 #warning UGLY!!
4070 // I know we shouldn't put anything in here but this seems the fastest
4071 // way to do this (and the cleanest for now). This function just inserts
4072 // a newline in the LString and the inserts 'depth'-spaces so that the
4073 // code is indented in the right way!!!
4074 void addNewlineAndDepth(LString &file, int const depth)
4075 {
4076        file += '\n';
4077        for (int j=0;j< depth;j++)
4078                file += ' ';
4079 }
4080
4081