]> git.lyx.org Git - lyx.git/blobdiff - src/LyXFunc.cpp
* Layout.cpp (read): fix ObsoletedBy and DependsOn for layout names
[lyx.git] / src / LyXFunc.cpp
index fc1eb74a040bf994115fc51e185fa94095147ee5..b753551211f418c5e647a8e0267790457f3d1682 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #include <config.h>
+#include <vector>
 
 #include "LyXFunc.h"
 
@@ -85,6 +86,7 @@
 #include "frontends/LyXView.h"
 #include "frontends/Menubar.h"
 #include "frontends/Toolbars.h"
+#include "frontends/Selection.h"
 
 #include "support/environment.h"
 #include "support/FileFilterList.h"
@@ -217,6 +219,10 @@ void LyXFunc::initKeySequences(KeyMap * kb)
 
 void LyXFunc::setLyXView(LyXView * lv)
 {
+       if (lyx_view_ && lyx_view_ != lv)
+               // save current selection to the selection buffer to allow
+               // middle-button paste in another window
+               cap::saveSelection(lyx_view_->view()->cursor());
        lyx_view_ = lv;
 }
 
@@ -272,7 +278,7 @@ void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
                // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
                if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos || bm.top_id != new_id )
                        const_cast<BookmarksSection::Bookmark &>(bm).updatePos(new_pit, new_pos, new_id);
-       } 
+       }
 }
 
 
@@ -616,10 +622,13 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
                enable = LyX::ref().session().bookmarks().size() > 0;
                break;
 
-       case LFUN_TOOLBAR_TOGGLE_STATE: {
-               ToolbarInfo::Flags flags = lyx_view_->getToolbarState(to_utf8(cmd.argument()));
-               if (!(flags & ToolbarInfo::AUTO))
-                       flag.setOnOff(flags & ToolbarInfo::ON);
+       case LFUN_TOOLBAR_TOGGLE: {
+               bool const current = lyx_view_->getToolbars().visible(cmd.getArg(0));
+               flag.setOnOff(current);
+               break;
+       }
+       case LFUN_WINDOW_CLOSE: {
+               enable = (theApp()->gui().viewIds().size() > 1);
                break;
        }
 
@@ -683,7 +692,6 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        case LFUN_BUFFER_NEXT:
        case LFUN_BUFFER_PREVIOUS:
        case LFUN_WINDOW_NEW:
-       case LFUN_WINDOW_CLOSE:
        case LFUN_LYX_QUIT:
                // these are handled in our dispatch()
                break;
@@ -888,7 +896,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                menuWrite(lyx_view_->buffer());
                                lyx_view_->message(str + _(" done."));
                        } else {
-                               writeAs(lyx_view_->buffer());
+                               writeAs(lyx_view_->buffer());
                        }
                        updateFlags = Update::None;
                        break;
@@ -905,7 +913,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        docstring text = bformat(_("Any changes will be lost. Are you sure "
                                                             "you want to revert to the saved version of the document %1$s?"), file);
                        int const ret = Alert::prompt(_("Revert to saved document?"),
-                               text, 0, 1, _("&Revert"), _("&Cancel"));
+                               text, 1, 1, _("&Revert"), _("&Cancel"));
 
                        if (ret == 0)
                                reloadBuffer();
@@ -986,21 +994,22 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                case LFUN_BUFFER_PRINT: {
                        BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
-                       string target;
-                       string target_name;
-                       string command = split(split(argument, target, ' '),
-                                              target_name, ' ');
+                       // FIXME: cmd.getArg() might fail if one of the arguments
+                       // contains double quotes
+                       string target = cmd.getArg(0);
+                       string target_name = cmd.getArg(1);
+                       string command = cmd.getArg(2);
 
                        if (target.empty()
                            || target_name.empty()
                            || command.empty()) {
                                lyxerr << "Unable to parse \""
-                                      << argument << '"' << std::endl;
+                                      << argument << '"' << endl;
                                break;
                        }
                        if (target != "printer" && target != "file") {
                                lyxerr << "Unrecognized target \""
-                                      << target << '"' << std::endl;
+                                      << target << '"' << endl;
                                break;
                        }
 
@@ -1032,13 +1041,13 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                        // case 3: print using a spool
                                        string const psname =
                                                changeExtension(dviname,".ps");
-                                       command += lyxrc.print_to_file
+                                       command += ' ' + lyxrc.print_to_file
                                                + quoteName(psname)
                                                + ' '
                                                + quoteName(dviname);
 
                                        string command2 =
-                                               lyxrc.print_spool_command +' ';
+                                               lyxrc.print_spool_command + ' ';
                                        if (target_name != "default") {
                                                command2 += lyxrc.print_spool_printerprefix
                                                        + target_name
@@ -1057,6 +1066,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                                                        command2);
                                } else {
                                        // case 2: print directly to a printer
+                                       if (target_name != "default")
+                                               command += ' ' + lyxrc.print_to_printer + target_name + ' ';
                                        res = one.startscript(
                                                Systemcall::DontWait,
                                                command + quoteName(dviname));
@@ -1064,20 +1075,22 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                        } else {
                                // case 1: print to a file
-                               FileName const filename(makeAbsPath(target_name, path));
+                               FileName const filename(makeAbsPath(target_name,
+                                                       lyx_view_->buffer()->filePath()));
+                               FileName const dvifile(makeAbsPath(dviname, path));
                                if (fs::exists(filename.toFilesystemEncoding())) {
                                        docstring text = bformat(
                                                _("The file %1$s already exists.\n\n"
-                                                 "Do you want to over-write that file?"),
+                                                 "Do you want to overwrite that file?"),
                                                makeDisplayPath(filename.absFilename()));
-                                       if (Alert::prompt(_("Over-write file?"),
-                                           text, 0, 1, _("&Over-write"), _("&Cancel")) != 0)
+                                       if (Alert::prompt(_("Overwrite file?"),
+                                           text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
                                                break;
                                }
-                               command += lyxrc.print_to_file
+                               command += ' ' + lyxrc.print_to_file
                                        + quoteName(filename.toFilesystemEncoding())
                                        + ' '
-                                       + quoteName(dviname);
+                                       + quoteName(dvifile.toFilesystemEncoding());
                                res = one.startscript(Systemcall::DontWait,
                                                      command);
                        }
@@ -1405,21 +1418,39 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_BUFFER_CHILD_OPEN: {
+                       // takes an optional argument, "|bool", at the end
+                       // indicating whether this file is being opened automatically
+                       // by LyX itself, in which case we will not want to switch
+                       // buffers after opening. The default is false, so in practice
+                       // it is used only when true.
                        BOOST_ASSERT(lyx_view_);
-                       FileName const filename =
+                       int const arglength = argument.length();
+                       FileName filename;
+                       bool autoOpen = false;
+                       if (argument.substr(arglength - 5, 5) == "|true") {
+                               autoOpen = true;
+                               filename = makeAbsPath(argument.substr(0, arglength - 5), 
+                                       lyx_view_->buffer()->filePath());
+                       } else if (argument.substr(arglength - 6, 6) == "|false") {
+                               filename = makeAbsPath(argument.substr(0, arglength - 6), 
+                                       lyx_view_->buffer()->filePath());
+                       } else filename = 
                                makeAbsPath(argument, lyx_view_->buffer()->filePath());
-                       setMessage(bformat(_("Opening child document %1$s..."),
-                                          makeDisplayPath(filename.absFilename())));
                        view()->saveBookmark(false);
-                       string const parentfilename = lyx_view_->buffer()->fileName();
-                       if (theBufferList().exists(filename.absFilename()))
-                               lyx_view_->setBuffer(theBufferList().getBuffer(filename.absFilename()));
-                       else
-                               lyx_view_->loadLyXFile(filename);
-                       // Set the parent name of the child document.
-                       // This makes insertion of citations and references in the child work,
-                       // when the target is in the parent or another child document.
-                       lyx_view_->buffer()->setParentName(parentfilename);
+                       if (theBufferList().exists(filename.absFilename())) {
+                               Buffer * buf = theBufferList().getBuffer(filename.absFilename());
+                               if (!autoOpen)
+                                       lyx_view_->setBuffer(buf, true);
+                               else
+                                       buf->setParentName(lyx_view_->buffer()->fileName());
+                       } else
+                               lyx_view_->loadLyXFile(filename, true, true, autoOpen);
+
+                       // If a screen update is required (in case where auto_open is false), 
+                       // loadLyXFile() would have taken care of it already. Otherwise we shall 
+                       // reset the update flag because it can cause a circular problem.
+                       // See bug 3970.
+                       updateFlags = Update::None;
                        break;
                }
 
@@ -1476,8 +1507,8 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                case LFUN_PREFERENCES_SAVE: {
                        lyxrc.write(makeAbsPath("preferences",
-                                               package().user_support().absFilename()),
-                                   false);
+                                               package().user_support().absFilename()),
+                                   false);
                        break;
                }
 
@@ -1621,7 +1652,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
 
                        if (defaults.writeFile(FileName(defaults.fileName())))
                                setMessage(bformat(_("Document defaults saved in %1$s"),
-                                                  makeDisplayPath(fname)));
+                                                  makeDisplayPath(fname)));
                        else
                                setErrorMessage(from_ascii(N_("Unable to save document defaults")));
                        break;
@@ -1748,9 +1779,29 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                        LyX::ref().session().bookmarks().clear();
                        break;
 
-               case LFUN_TOOLBAR_TOGGLE_STATE:
-                       lyx_view_->toggleToolbarState(argument);
+               case LFUN_TOOLBAR_TOGGLE: {
+                       BOOST_ASSERT(lyx_view_);
+                       string const name = cmd.getArg(0);
+                       bool const allowauto = cmd.getArg(1) == "allowauto";
+                       lyx_view_->toggleToolbarState(name, allowauto);
+                       ToolbarInfo * tbi = lyx_view_->getToolbarInfo(name);
+                       if (!tbi) {
+                               setMessage(bformat(_("Unknown toolbar \"%1$s\""),
+                                                  from_utf8(name)));
+                               break;
+                       }
+                       docstring state;
+                       if (tbi->flags & ToolbarInfo::ON)
+                               state = _("on");
+                       else if (tbi->flags & ToolbarInfo::OFF)
+                               state = _("off");
+                       else if (tbi->flags & ToolbarInfo::AUTO)
+                               state = _("auto");
+
+                       setMessage(bformat(_("Toolbar \"%1$s\" state set to %2$s"), 
+                                          _(tbi->gui_name), state));
                        break;
+               }
 
                default: {
                        BOOST_ASSERT(lyx_view_);
@@ -1782,6 +1833,9 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                            && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
                                view()->buffer()->markDirty();
 
+                       //Do we have a selection?
+                       theSelection().haveSelection(view()->cursor().selection());
+
                        if (view()->cursor().inTexted()) {
                                lyx_view_->updateLayoutChoice();
                        }
@@ -2034,9 +2088,9 @@ void LyXFunc::doImport(string const & argument)
                docstring const file = makeDisplayPath(lyxfile.absFilename(), 30);
 
                docstring text = bformat(_("The document %1$s already exists.\n\n"
-                                                    "Do you want to over-write that document?"), file);
-               int const ret = Alert::prompt(_("Over-write document?"),
-                       text, 0, 1, _("&Over-write"), _("&Cancel"));
+                                                    "Do you want to overwrite that document?"), file);
+               int const ret = Alert::prompt(_("Overwrite document?"),
+                       text, 0, 1, _("&Overwrite"), _("&Cancel"));
 
                if (ret == 1) {
                        lyx_view_->message(_("Canceled."));
@@ -2055,16 +2109,8 @@ void LyXFunc::closeBuffer()
        // goto bookmark to update bookmark pit.
        for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
                gotoBookmark(i+1, false, false);
-       if (theBufferList().close(lyx_view_->buffer(), true) && !quitting) {
-               if (theBufferList().empty()) {
-                       // need this otherwise SEGV may occur while
-                       // trying to set variables that don't exist
-                       // since there's no current buffer
-                       lyx_view_->getDialogs().hideBufferDependent();
-               } else {
-                       lyx_view_->setBuffer(theBufferList().first());
-               }
-       }
+       
+       theBufferList().close(lyx_view_->buffer(), true);
 }