]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiApplication.cpp
Fix crash noticed by Bennett:
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
index 01672acfe60997153bc36583ece93c30d7c3dbcb..6b0a78adaaa693a76bc20c5eda488552de9cd68b 100644 (file)
@@ -50,6 +50,7 @@
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
 #include "support/FileName.h"
+#include "support/filetools.h"
 #include "support/foreach.h"
 #include "support/ForkedCalls.h"
 #include "support/gettext.h"
@@ -799,6 +800,20 @@ docstring GuiApplication::iconName(FuncRequest const & f, bool unknown)
 }
 
 
+LyXView * GuiApplication::currentWindow() 
+{
+#ifdef Q_WS_MACX
+       /* In LyX/Mac, when a dialog is open, the menus of the
+          application can still be accessed without giving focus to
+          the main window. In this case, we want to disable the menu
+          entries that are buffer or view-related.
+       */
+       if (current_view_ && activeWindow() != current_view_)
+               return 0;
+#endif
+       return current_view_;
+}
+
 
 bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 {
@@ -813,6 +828,7 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
        case LFUN_BUFFER_NEW:
        case LFUN_BUFFER_NEW_TEMPLATE:
        case LFUN_FILE_OPEN:
+       case LFUN_HELP_OPEN:
        case LFUN_SCREEN_FONT_UPDATE:
        case LFUN_SET_COLOR:
        case LFUN_WINDOW_NEW:
@@ -843,6 +859,9 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
                // update bookmark pit of the current buffer before window close
                for (size_t i = 0; i < theSession().bookmarks().size(); ++i)
                        theLyXFunc().gotoBookmark(i+1, false, false);
+               // clear the last opened list, because
+               // maybe this will end the session
+               theSession().lastOpened().clear();
                current_view_->close();
                break;
 
@@ -874,7 +893,7 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
 
        case LFUN_BUFFER_NEW:
                if (d->views_.empty()
-                   || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
+                   || (!lyxrc.open_buffers_in_tabs && current_view_->documentBufferView() != 0)) {
                        createView(QString(), false); // keep hidden
                        current_view_->newDocument(to_utf8(cmd.argument()), false);
                        current_view_->show();
@@ -886,10 +905,10 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
 
        case LFUN_BUFFER_NEW_TEMPLATE:
                if (d->views_.empty()
-                   || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
+                   || (!lyxrc.open_buffers_in_tabs && current_view_->documentBufferView() != 0)) {
                        createView();
                        current_view_->newDocument(to_utf8(cmd.argument()), true);
-                       if (!current_view_->buffer())
+                       if (!current_view_->documentBufferView())
                                current_view_->close();
                } else {
                        current_view_->newDocument(to_utf8(cmd.argument()), true);
@@ -897,8 +916,9 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
                break;
 
        case LFUN_FILE_OPEN:
+               // FIXME: create a new method shared with LFUN_HELP_OPEN.
                if (d->views_.empty()
-                   || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
+                   || (!lyxrc.open_buffers_in_tabs && current_view_->documentBufferView() != 0)) {
                        string const fname = to_utf8(cmd.argument());
                        // We want the ui session to be saved per document and not per
                        // window number. The filename crc is a good enough identifier.
@@ -906,12 +926,41 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
                        crc = for_each(fname.begin(), fname.end(), crc);
                        createView(crc.checksum());
                        current_view_->openDocument(fname);
-                       if (current_view_ && !current_view_->buffer())
+                       if (current_view_ && !current_view_->documentBufferView())
                                current_view_->close();
                } else
                        current_view_->openDocument(to_utf8(cmd.argument()));
                break;
 
+       case LFUN_HELP_OPEN: {
+               // FIXME: create a new method shared with LFUN_FILE_OPEN.
+               if (current_view_ == 0)
+                       createView();
+               string const arg = to_utf8(cmd.argument());
+               if (arg.empty()) {
+                       current_view_->message(_("Missing argument"));
+                       break;
+               }
+               FileName fname = i18nLibFileSearch("doc", arg, "lyx");
+               if (fname.empty()) 
+                       fname = i18nLibFileSearch("examples", arg, "lyx");
+
+               if (fname.empty()) {
+                       lyxerr << "LyX: unable to find documentation file `"
+                               << arg << "'. Bad installation?" << endl;
+                       break;
+               }
+               current_view_->message(bformat(_("Opening help file %1$s..."),
+                       makeDisplayPath(fname.absFilename())));
+               Buffer * buf = current_view_->loadDocument(fname, false);
+               if (buf) {
+                       current_view_->setBuffer(buf);
+                       buf->updateLabels();
+                       buf->errors("Parse");
+               }
+               break;
+       }
+
        case LFUN_SET_COLOR: {
                string lyx_name;
                string const x11_name = split(to_utf8(cmd.argument()), lyx_name, ' ');
@@ -977,6 +1026,7 @@ void GuiApplication::resetGui()
        QHash<int, GuiView *>::iterator it;
        for (it = d->views_.begin(); it != d->views_.end(); ++it) {
                GuiView * gv = *it;
+               setCurrentView(gv);
                gv->setLayoutDirection(layoutDirection());
                gv->resetDialogs();
        }
@@ -1001,10 +1051,9 @@ void GuiApplication::createView(QString const & geometry_arg, bool autoShow,
 
        // create new view
        int id = view_id;
-       if (id == 0) {
-               while (d->views_.find(id) != d->views_.end())
-                       id++;
-       }
+       while (d->views_.find(id) != d->views_.end())
+               id++;
+
        LYXERR(Debug::GUI, "About to create new window with ID " << id);
        GuiView * view = new GuiView(id);
        // register view
@@ -1211,12 +1260,10 @@ void GuiApplication::restoreGuiSession()
        // do not add to the lastfile list since these files are restored from
        // last session, and should be already there (regular files), or should
        // not be added at all (help files).
-       // Note that we open them in reverse order. This is because we close
-       // buffers also in reverse order (aesthetically motivated).
-       for (size_t i = lastopened.size(); i > 0; --i) {
-               FileName const & file_name = lastopened[i - 1].file_name;
+       for (size_t i = 0; i < lastopened.size(); ++i) {
+               FileName const & file_name = lastopened[i].file_name;
                if (d->views_.empty() || (!lyxrc.open_buffers_in_tabs
-                         && current_view_->buffer() != 0)) {
+                         && current_view_->documentBufferView() != 0)) {
                        boost::crc_32_type crc;
                        string const & fname = file_name.absFilename();
                        crc = for_each(fname.begin(), fname.end(), crc);
@@ -1224,7 +1271,7 @@ void GuiApplication::restoreGuiSession()
                }
                current_view_->loadDocument(file_name, false);
 
-               if (lastopened[i - 1].active)
+               if (lastopened[i].active)
                        active_file = file_name;
        }
 
@@ -1317,7 +1364,9 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
                        this->exit(1);
 
                case BufferException: {
-                       Buffer * buf = current_view_->buffer();
+                       if (!current_view_->documentBufferView())
+                               return false;
+                       Buffer * buf = &current_view_->documentBufferView()->buffer();
                        docstring details = e.details_ + '\n';
                        details += buf->emergencyWrite();
                        theBufferList().release(buf);
@@ -1409,10 +1458,8 @@ void GuiApplication::unregisterView(GuiView * gv)
 {
        LASSERT(d->views_[gv->id()] == gv, /**/);
        d->views_.remove(gv->id());
-       if (current_view_ == gv) {
+       if (current_view_ == gv)
                current_view_ = 0;
-               theLyXFunc().setLyXView(0);
-       }
 }
 
 
@@ -1635,7 +1682,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
                if (xev->xselectionrequest.selection != XA_PRIMARY)
                        break;
                LYXERR(Debug::SELECTION, "X requested selection.");
-               BufferView * bv = current_view_->view();
+               BufferView * bv = current_view_->currentBufferView();
                if (bv) {
                        docstring const sel = bv->requestSelection();
                        if (!sel.empty())
@@ -1647,7 +1694,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
                if (xev->xselectionclear.selection != XA_PRIMARY)
                        break;
                LYXERR(Debug::SELECTION, "Lost selection.");
-               BufferView * bv = current_view_->view();
+               BufferView * bv = current_view_->currentBufferView();
                if (bv)
                        bv->clearSelection();
                break;