]> git.lyx.org Git - lyx.git/blobdiff - src/lyx_cb.C
Dekels tabular/textinset patches
[lyx.git] / src / lyx_cb.C
index c09fe4cc8aa5eca359dd21ac9c2b9fecffe60aad..5509d936bf428507359d3d16b7e42925dd34017a 100644 (file)
 #include FORMS_H_LOCATION
 #include "lyx.h"
 #include "layout_forms.h"
-#include "form1.h"
 #include "lyx_main.h"
 #include "lyx_cb.h"
-#include "insets/insetref.h"
 #include "insets/insetlabel.h"
 #include "insets/figinset.h"
 #include "lyxfunc.h"
 #include "filedlg.h"
 #include "lyx_gui_misc.h"
 #include "LyXView.h"
-#include "BufferView.h"
 #include "lastfiles.h"
 #include "bufferview_funcs.h"
 #include "support/FileInfo.h"
 #include "support/syscall.h"
 #include "support/filetools.h"
 #include "support/path.h"
-#include "lyxserver.h"
 #include "lyxrc.h"
 #include "lyxtext.h"
-#include "CutAndPaste.h"
-#include "exporter.h"
 
 using std::ifstream;
 using std::copy;
@@ -68,10 +62,11 @@ extern BufferView * current_view; // called too many times in this file...
 
 extern void DeleteSimpleCutBuffer(); /* for the cleanup when exiting */
 
+#if 0
 extern bool send_fax(string const & fname, string const & sendcmd);
-extern void MenuSendto();
+#endif
 
-extern LyXServer * lyxserver;
+extern void MenuSendto();
 
 // this should be static, but I need it in buffer.C
 bool quitting; // flag, that we are quitting the program
@@ -144,28 +139,17 @@ void ToggleLockedInsetCursor(int x, int y, int asc, int desc);
   -----------------------------------------------------------------------
  */
 
-/* some function prototypes */
 
-int RunLinuxDoc(BufferView *, int, string const &);
-int RunDocBook(int, string const &);
-bool MenuWrite(Buffer * buf);
-bool MenuWriteAs(Buffer * buffer);
-void MenuReload(Buffer * buf);
-void MenuLayoutSave();
-
-
-void ShowMessage(Buffer * buf,
+void ShowMessage(Buffer const * buf,
                 string const & msg1,
                 string const & msg2,
                 string const & msg3, int delay)
 {
-       if (lyxrc.use_gui) {
+       if (lyxrc.use_gui)
                buf->getUser()->owner()->getMiniBuffer()->Set(msg1, msg2,
                                                              msg3, delay);
-       } else {
-               // can somebody think of something more clever? cerr?
-               cout << msg1 << msg2 << msg3 << endl;
-       }
+       else
+               lyxerr << msg1 << msg2 << msg3 << endl;
 }
 
 
@@ -178,16 +162,16 @@ void ShowMessage(Buffer * buf,
 //
 
 // should be moved to lyxfunc.C
-bool MenuWrite(Buffer * buffer)
+bool MenuWrite(BufferView * bv, Buffer * buffer)
 {
-       XFlush(fl_display);
+       XFlush(fl_get_display());
        if (!buffer->save()) {
-               string fname = buffer->fileName();
-               string s = MakeAbsPath(fname);
+               string const fname = buffer->fileName();
+               string const s = MakeAbsPath(fname);
                if (AskQuestion(_("Save failed. Rename and try again?"),
                                MakeDisplayPath(s, 50),
                                _("(If not, document is not saved.)"))) {
-                       return MenuWriteAs(buffer);
+                       return MenuWriteAs(bv, buffer);
                }
                return false;
        } else {
@@ -199,7 +183,7 @@ bool MenuWrite(Buffer * buffer)
 
 // should be moved to BufferView.C
 // Half of this func should be in LyXView, the rest in BufferView.
-bool MenuWriteAs(Buffer * buffer)
+bool MenuWriteAs(BufferView * bv, Buffer * buffer)
 {
        // Why do we require BufferView::text to be able to write a
        // document? I see no point in that. (Lgb)
@@ -209,36 +193,30 @@ bool MenuWriteAs(Buffer * buffer)
        string oldname = fname;
        LyXFileDlg fileDlg;
 
-       ProhibitInput(current_view);
+       ProhibitInput(bv);
        fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
        fileDlg.SetButton(1, _("Templates"), lyxrc.template_path);
 
        if (!IsLyXFilename(fname))
                fname += ".lyx";
 
-       if (buffer->isUnnamed()) {
-               fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
-                                      "",
-                                      "*.lyx", 
-                                      "");
-       } else {
-               fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
-                                      OnlyPath(fname),
-                                      "*.lyx", 
-                                      OnlyFilename(fname));
-       }
-       AllowInput(current_view);
+       fname = fileDlg.Select(_("Enter Filename to Save Document as"), 
+                              OnlyPath(fname),
+                              "*.lyx", 
+                              OnlyFilename(fname));
 
-       if (fname.empty()) {
+       AllowInput(bv);
+
+       if (fname.empty())
                return false;
-       }
+
        // Make sure the absolute filename ends with appropriate suffix
        string s = MakeAbsPath(fname);
        if (!IsLyXFilename(s))
                s += ".lyx";
 
        // Same name as we have already?
-       if (s == oldname) {
+       if (!buffer->isUnnamed() && s == oldname) {
                if (!AskQuestion(_("Same name as document already has:"),
                                 MakeDisplayPath(s, 50),
                                 _("Save anyway?")))
@@ -246,7 +224,7 @@ bool MenuWriteAs(Buffer * buffer)
                // Falls through to name change and save
        } 
        // No, but do we have another file with this name open?
-       else if (bufferlist.exists(s)) {
+       else if (!buffer->isUnnamed() && bufferlist.exists(s)) {
                if (AskQuestion(_("Another document with same name open!"),
                                MakeDisplayPath(s, 50),
                                _("Replace with current document?")))
@@ -254,7 +232,7 @@ bool MenuWriteAs(Buffer * buffer)
                                bufferlist.close(bufferlist.getBuffer(s));
 
                                // Ok, change the name of the buffer, but don't save!
-                               buffer->fileName(s);
+                               buffer->setFileName(s);
                                buffer->markDirty();
 
                                ShowMessage(buffer, _("Document renamed to '"),
@@ -263,7 +241,7 @@ bool MenuWriteAs(Buffer * buffer)
                return false;
        } // Check whether the file exists
        else {
-               FileInfo myfile(s);
+               FileInfo const myfile(s);
                if (myfile.isOK() && !AskQuestion(_("Document already exists:"), 
                                                  MakeDisplayPath(s, 50),
                                                  _("Replace file?")))
@@ -271,7 +249,7 @@ bool MenuWriteAs(Buffer * buffer)
        }
 
        // Ok, change the name of the buffer
-       buffer->fileName(s);
+       buffer->setFileName(s);
        buffer->markDirty();
        bool unnamed = buffer->isUnnamed();
        buffer->setUnnamed(false);
@@ -279,8 +257,8 @@ bool MenuWriteAs(Buffer * buffer)
        // Small bug: If the save fails, we have irreversible changed the name
        // of the document.
        // Hope this is fixed this way! (Jug)
-       if (!MenuWrite(buffer)) {
-           buffer->fileName(oldname);
+       if (!MenuWrite(bv, buffer)) {
+           buffer->setFileName(oldname);
            buffer->setUnnamed(unnamed);
            ShowMessage(buffer, _("Document could not be saved!"),
                        _("Holding the old name."), MakeDisplayPath(oldname));
@@ -323,6 +301,7 @@ int MenuRunChktex(Buffer * buffer)
 }
 
 
+#if 0
 void MenuFax(Buffer * buffer)
 {
        // Generate postscript file
@@ -345,6 +324,7 @@ void MenuFax(Buffer * buffer)
        } else
                send_fax(ps, lyxrc.fax_command);
 }
+#endif
 
 
 void QuitLyX()
@@ -396,9 +376,7 @@ void AutoSave(BufferView * bv)
        
        // tmp_ret will be located (usually) in /tmp
        // will that be a problem?
-       string tmp_ret = tmpnam(0);
-       
-       pid_t pid = fork(); // If you want to debug the autosave
+       pid_t const pid = fork(); // If you want to debug the autosave
        // you should set pid to -1, and comment out the
        // fork.
        if (pid == 0 || pid == -1) {
@@ -406,10 +384,12 @@ void AutoSave(BufferView * bv)
                // to fork. But we will do the save
                // anyway.
                bool failed = false;
+               
+               string const tmp_ret = lyx::tempName(string(), "lyxauto");
                if (!tmp_ret.empty()) {
                        bv->buffer()->writeFile(tmp_ret, 1);
                        // assume successful write of tmp_ret
-                       if (lyx::rename(tmp_ret, fname)) {
+                       if (!lyx::rename(tmp_ret, fname)) {
                                failed = true;
                                // most likely couldn't move between filesystems
                                // unless write of tmp_ret failed
@@ -511,7 +491,7 @@ void InsertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
        // We use this until the compilers get better...
        vector<char> tmp;
        copy(ii, end, back_inserter(tmp));
-       string tmpstr(tmp.begin(), tmp.end());
+       string const tmpstr(tmp.begin(), tmp.end());
 #else
        // This is what we want to use and what we will use once the
        // compilers get good enough. 
@@ -533,13 +513,80 @@ void InsertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
 }
 
 
-void MenuInsertLabel(string const & arg)
+void MenuInsertLabel(BufferView * bv, string const & arg)
 {
        string label(arg);
-       ProhibitInput(current_view);
+       ProhibitInput(bv);
        if (label.empty()) {
-               pair<bool, string>
-                       result = askForText(_("Enter new label to insert:"));
+#ifdef LABEL_INIT
+#ifndef NEW_INSETS
+               LyXParagraph * par =
+                       bv->text->cursor.par()->FirstPhysicalPar();
+#else
+               LyXParagraph * par = bv->text->cursor.par();
+#endif
+               LyXLayout const * layout =
+                       &textclasslist.Style(bv->buffer()->params.textclass,
+                                            par->GetLayout());
+
+               if (layout->latextype == LATEX_PARAGRAPH && par->previous) {
+#ifndef NEW_INSETS
+                       LyXParagraph * par2 = par->previous->FirstPhysicalPar();
+#else
+                       LyXParagraph * par2 = par->previous;
+#endif
+                       LyXLayout const * layout2 =
+                               &textclasslist.Style(bv->buffer()->params.textclass,
+                                                    par2->GetLayout());
+                       if (layout2->latextype != LATEX_PARAGRAPH) {
+                               par = par2;
+                               layout = layout2;
+                       }
+               }
+               string text = layout->latexname().substr(0, 3);
+               if (layout->latexname() == "theorem")
+                       text = "thm"; // Create a correct prefix for prettyref
+#ifndef NEW_INSETS
+               if (par->footnoteflag==LyXParagraph::OPEN_FOOTNOTE)
+                       switch (par->footnotekind) {
+                       case LyXParagraph::FIG:
+                       case LyXParagraph::WIDE_FIG:
+                               text = "fig";
+                               break;
+                       case LyXParagraph::TAB:
+                       case LyXParagraph::WIDE_TAB:
+                               text = "tab";
+                               break;
+                       case LyXParagraph::ALGORITHM:
+                               text = "alg";
+                               break;
+                       case LyXParagraph::FOOTNOTE:    
+                       case LyXParagraph::MARGIN:
+                               break;
+                       }
+#endif
+               text += ":";
+               if (layout->latextype == LATEX_PARAGRAPH ||
+                   lyxrc.label_init_length < 0)
+                       text.erase();
+               string par_text = par->String(bv->buffer(), false);
+               for (int i = 0; i < lyxrc.label_init_length; ++i) {
+                       if (par_text.empty())
+                               break;
+                       string head;
+                       par_text = split(par_text, head, ' ');
+                       if (i > 0)
+                               text += '_'; // Is it legal to use spaces in
+                                            // labels ?
+                       text += head;
+               }
+
+               pair<bool, string> result =
+                       askForText(_("Enter new label to insert:"), text);
+#else
+               pair<bool, string> result =
+                       askForText(_("Enter new label to insert:"));
+#endif
                if (result.first) {
                        label = frontStrip(strip(result.second));
                }
@@ -547,9 +594,9 @@ void MenuInsertLabel(string const & arg)
        if (!label.empty()) {
                InsetCommandParams p( "label", label );
                InsetLabel * inset = new InsetLabel( p );
-               current_view->insertInset( inset );
+               bv->insertInset( inset );
        }
-       AllowInput(current_view);
+       AllowInput(bv);
 }
 
 
@@ -557,7 +604,7 @@ void MenuInsertLabel(string const & arg)
 // current_view. (Lgb)
 void LayoutsCB(int sel, void *, Combox *)
 {
-       string tmp = tostr(sel);
+       string const tmp = tostr(sel);
        current_view->owner()->getLyXFunc()->Dispatch(LFUN_LAYOUTNO,
                                                      tmp);
 }
@@ -571,7 +618,7 @@ void MenuLayoutCharacter()
                fl_raise_form(fd_form_character->form_character);
        } else {
                fl_show_form(fd_form_character->form_character,
-                            FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
+                            FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
                             _("Character Style"));
                if (ow < 0) {
                        ow = fd_form_character->form_character->w;
@@ -582,24 +629,23 @@ void MenuLayoutCharacter()
 }
 
 
-bool UpdateLayoutPreamble()
+bool UpdateLayoutPreamble(BufferView * bv)
 {
        bool update = true;
-       if (!current_view->available())
+       if (!bv->available())
                update = false;
 
        if (update) {
                fl_set_input(fd_form_preamble->input_preamble,
-                            current_view->buffer()->params.preamble.c_str());
+                            bv->buffer()->params.preamble.c_str());
 
-               if (current_view->buffer()->isReadonly()) {
+               if (bv->buffer()->isReadonly()) {
                        fl_deactivate_object(fd_form_preamble->input_preamble);
                        fl_deactivate_object(fd_form_preamble->button_ok);
                        fl_deactivate_object(fd_form_preamble->button_apply);
                        fl_set_object_lcol(fd_form_preamble->button_ok, FL_INACTIVE);
                        fl_set_object_lcol(fd_form_preamble->button_apply, FL_INACTIVE);
-               }
-               else {
+               } else {
                        fl_activate_object(fd_form_preamble->input_preamble);
                        fl_activate_object(fd_form_preamble->button_ok);
                        fl_activate_object(fd_form_preamble->button_apply);
@@ -617,13 +663,13 @@ void MenuLayoutPreamble()
 {
        static int ow = -1, oh;
 
-       if (UpdateLayoutPreamble()) {
+       if (UpdateLayoutPreamble(current_view)) {
                if (fd_form_preamble->form_preamble->visible) {
                        fl_raise_form(fd_form_preamble->form_preamble);
                } else {
                        fl_show_form(fd_form_preamble->form_preamble,
                                     FL_PLACE_MOUSE | FL_FREE_SIZE,
-                                    FL_FULLBORDER,
+                                    FL_TRANSIENT,
                                     _("LaTeX Preamble"));
                        if (ow < 0) {
                                ow = fd_form_preamble->form_preamble->w;
@@ -636,15 +682,15 @@ void MenuLayoutPreamble()
 }
 
 
-void MenuLayoutSave()
+void MenuLayoutSave(BufferView * bv)
 {
-       if (!current_view->available())
+       if (!bv->available())
                return;
 
        if (AskQuestion(_("Do you want to save the current settings"),
                        _("for Character, Document, Paper and Quotes"),
                        _("as default for new documents?")))
-               current_view->buffer()->saveParamsAsDefaults();
+               bv->buffer()->saveParamsAsDefaults();
 }
 
 
@@ -655,7 +701,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        LyXFont font(LyXFont::ALL_IGNORE);
 
        int pos = fl_get_choice(fd_form_character->choice_family);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setFamily(LyXFont::IGNORE_FAMILY); break;
        case 2: font.setFamily(LyXFont::ROMAN_FAMILY); break;
        case 3: font.setFamily(LyXFont::SANS_FAMILY); break;
@@ -664,7 +710,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        }
 
        pos = fl_get_choice(fd_form_character->choice_series);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setSeries(LyXFont::IGNORE_SERIES); break;
        case 2: font.setSeries(LyXFont::MEDIUM_SERIES); break;
        case 3: font.setSeries(LyXFont::BOLD_SERIES); break;
@@ -672,7 +718,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        }
 
        pos = fl_get_choice(fd_form_character->choice_shape);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setShape(LyXFont::IGNORE_SHAPE); break;
        case 2: font.setShape(LyXFont::UP_SHAPE); break;
        case 3: font.setShape(LyXFont::ITALIC_SHAPE); break;
@@ -682,7 +728,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        }
 
        pos = fl_get_choice(fd_form_character->choice_size);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setSize(LyXFont::IGNORE_SIZE); break;
        case 2: font.setSize(LyXFont::SIZE_TINY); break;
        case 3: font.setSize(LyXFont::SIZE_SCRIPT); break;
@@ -700,7 +746,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        }
 
        pos = fl_get_choice(fd_form_character->choice_bar);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setEmph(LyXFont::IGNORE);
                font.setUnderbar(LyXFont::IGNORE);
                font.setNoun(LyXFont::IGNORE);
@@ -718,7 +764,7 @@ LyXFont const UserFreeFont(BufferParams const & params)
        }
 
        pos = fl_get_choice(fd_form_character->choice_color);
-       switch(pos) {
+       switch (pos) {
        case 1: font.setColor(LColor::ignore); break;
        case 2: font.setColor(LColor::none); break;
        case 3: font.setColor(LColor::black); break;
@@ -732,23 +778,26 @@ LyXFont const UserFreeFont(BufferParams const & params)
        case 11: font.setColor(LColor::inherit); break;
        }
 
-       int choice = combo_language2->get();
+       int const choice = combo_language2->get();
        if (choice == 1)
                font.setLanguage(ignore_language);
        else if (choice == 2)
-               font.setLanguage(params.language_info);
+               font.setLanguage(params.language);
        else
-               font.setLanguage(&languages[combo_language2->getline()]);
+               font.setLanguage(languages.getLanguage(combo_language2->getline()));
 
        return font; 
 }
 
 
 /* callbacks for form form_title */
-extern "C" void TimerCB(FL_OBJECT *, long)
+extern "C"
+void TimerCB(FL_OBJECT *, long)
 {
        // only if the form still exists
-       if (lyxrc.show_banner && fd_form_title->form_title != 0) {
+       if (lyxrc.show_banner
+           && fd_form_title
+           && fd_form_title->form_title) {
                if (fd_form_title->form_title->visible) {
                        fl_hide_form(fd_form_title->form_title);
                }
@@ -760,7 +809,8 @@ extern "C" void TimerCB(FL_OBJECT *, long)
 
 /* callbacks for form form_character */
 
-extern "C" void CharacterApplyCB(FL_OBJECT *, long)
+extern "C"
+void CharacterApplyCB(FL_OBJECT *, long)
 {
        // we set toggleall locally here, since it should be true for
        // all other uses of ToggleAndShow() (JMarc)
@@ -771,13 +821,15 @@ extern "C" void CharacterApplyCB(FL_OBJECT *, long)
 }
 
 
-extern "C" void CharacterCloseCB(FL_OBJECT *, long)
+extern "C"
+void CharacterCloseCB(FL_OBJECT *, long)
 {
        fl_hide_form(fd_form_character->form_character);
 }
 
 
-extern "C" void CharacterOKCB(FL_OBJECT *ob, long data)
+extern "C"
+void CharacterOKCB(FL_OBJECT * ob, long data)
 {
        CharacterApplyCB(ob, data);
        CharacterCloseCB(ob, data);
@@ -786,13 +838,15 @@ extern "C" void CharacterOKCB(FL_OBJECT *ob, long data)
 
 /* callbacks for form form_preamble */
 
-extern "C" void PreambleCancelCB(FL_OBJECT *, long)
+extern "C"
+void PreambleCancelCB(FL_OBJECT *, long)
 {
        fl_hide_form(fd_form_preamble->form_preamble);
 }
 
 
-extern "C" void PreambleApplyCB(FL_OBJECT *, long)
+extern "C"
+void PreambleApplyCB(FL_OBJECT *, long)
 {
        if (!current_view->available())
                return;
@@ -804,7 +858,8 @@ extern "C" void PreambleApplyCB(FL_OBJECT *, long)
 }
 
    
-extern "C" void PreambleOKCB(FL_OBJECT * ob, long data)
+extern "C"
+void PreambleOKCB(FL_OBJECT * ob, long data)
 {
        PreambleApplyCB(ob, data);
        PreambleCancelCB(ob, data);
@@ -817,7 +872,7 @@ void Figure()
                fl_raise_form(fd_form_figure->form_figure);
        } else {
                fl_show_form(fd_form_figure->form_figure,
-                            FL_PLACE_MOUSE, FL_FULLBORDER,
+                            FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
                             _("Insert Figure"));
        }
 }
@@ -831,12 +886,12 @@ void FigureApplyCB(FL_OBJECT *, long)
                return;
 
        Buffer * buffer = current_view->buffer();
-       if(buffer->isReadonly()) // paranoia
+       if (buffer->isReadonly()) // paranoia
                return;
        
        current_view->owner()->getMiniBuffer()->Set(_("Inserting figure..."));
        if (fl_get_button(fd_form_figure->radio_inline)) {
-               InsetFig * new_inset = new InsetFig(100, 20, buffer);
+               InsetFig * new_inset = new InsetFig(100, 20, *buffer);
                current_view->insertInset(new_inset);
                current_view->owner()->getMiniBuffer()->Set(_("Figure inserted"));
                new_inset->Edit(current_view, 0, 0, 0);
@@ -890,7 +945,7 @@ void FigureApplyCB(FL_OBJECT *, long)
        
        current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
       
-       Inset * new_inset = new InsetFig(100, 100, buffer);
+       Inset * new_inset = new InsetFig(100, 100, *buffer);
        current_view->insertInset(new_inset);
        new_inset->Edit(current_view, 0, 0, 0);
        current_view->update(BufferView::SELECT|BufferView::FITCUR);