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