]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiApplication.cpp
Work around bug http://bugzilla.lyx.org/show_bug.cgi?id=4846
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
index f29f24a0b8a7e2ca8c9bfa938d3a920a0e8055b1..f1bbad25e3faea2b7d0db574701852f81eeab16b 100644 (file)
 #endif // Q_WS_WIN
 
 #include <boost/bind.hpp>
+#include <boost/crc.hpp>
 
 #include <exception>
 #include <vector>
@@ -338,7 +339,7 @@ QIcon getIcon(FuncRequest const & f, bool unknown)
        if (icon.isEmpty())
                return QIcon();
 
-       LYXERR(Debug::GUI, "Found icon: " << icon);
+       //LYXERR(Debug::GUI, "Found icon: " << icon);
        QPixmap pm;
        if (!pm.load(icon)) {
                LYXERR0("Cannot load icon " << icon << " please verify resource system!");
@@ -628,9 +629,7 @@ struct GuiApplication::Private
 
 #ifdef Q_WS_WIN
        /// WMF Mime handler for Windows clipboard.
-       // FIXME for Windows Vista and Qt4 (see http://bugzilla.lyx.org/show_bug.cgi?id=4846)
-       // But this makes LyX crash on exit when LyX is compiled in release mode and if there
-       // is something in the clipboard.
+       /// \warning: see comment in ~GuiApplication and in bug 4846.
        QWindowsMimeMetafile wmf_mime_;
 #endif
 };
@@ -643,6 +642,13 @@ GuiApplication::~GuiApplication()
 #ifdef Q_WS_MACX
        closeAllLinkBackLinks();
 #endif
+       // FIXME: Work around bug 4846 for Windows Vista and Qt4
+       // (see http://bugzilla.lyx.org/show_bug.cgi?id=4846)
+       // If the clipboard is not cleared, LyX crashes on exit when it is
+       // compiled in release mode and if there is something in the clipboard.
+       // This is related to QWindowsMimeMetafile which is apparently not 
+       // properly destroyed.
+       qApp->clipboard()->clear(QClipboard::Clipboard);
        delete d;
 }
 
@@ -840,8 +846,13 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
        case LFUN_FILE_OPEN:
                if (d->views_.empty()
                    || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
-                       createView();
-                       current_view_->openDocument(to_utf8(cmd.argument()));
+                       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.
+                       boost::crc_32_type crc;
+                       crc = for_each(fname.begin(), fname.end(), crc);
+                       createView(crc.checksum());
+                       current_view_->openDocument(fname);
                        if (!current_view_->buffer())
                                current_view_->close();
                } else
@@ -892,6 +903,19 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
 
 void GuiApplication::resetGui()
 {
+       // Set the language defined by the user.
+       LyX::ref().setRcGuiLanguage();
+
+       // Read menus
+       if (!readUIFile(toqstr(lyxrc.ui_file)))
+               // Gives some error box here.
+               return;
+
+       // init the global menubar on Mac. This must be done after the session
+       // was recovered to know the "last files".
+       if (d->global_menubar_)
+               d->menus_.fillMenuBar(d->global_menubar_, 0, true);
+
        QHash<int, GuiView *>::iterator it;
        for (it = d->views_.begin(); it != d->views_.end(); ++it)
                (*it)->resetDialogs();
@@ -900,7 +924,14 @@ void GuiApplication::resetGui()
 }
 
 
-void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
+void GuiApplication::createView(int view_id)
+{
+       createView(QString(), true, view_id);
+}
+
+
+void GuiApplication::createView(QString const & geometry_arg, bool autoShow,
+       int view_id)
 {
        // release the keyboard which might have been grabed by the global
        // menubar on Mac to catch shortcuts even without any GuiView.
@@ -908,15 +939,13 @@ void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
                d->global_menubar_->releaseKeyboard();
 
        // create new view
-       int id = 0;
-       while (d->views_.find(id) != d->views_.end())
-               id++;
+       int id = view_id;
+       if (id == 0) {
+               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);
-       
-       // copy the icon size from old view
-       if (current_view_)
-               view->setIconSize(current_view_->iconSize());
-
        // register view
        d->views_[id] = view;
 
@@ -1012,6 +1041,9 @@ void GuiApplication::exit(int status)
 
 void GuiApplication::execBatchCommands()
 {
+       // Set the language defined by the user.
+       LyX::ref().setRcGuiLanguage();
+
        // Read menus
        if (!readUIFile(toqstr(lyxrc.ui_file)))
                // Gives some error box here.
@@ -1314,9 +1346,16 @@ bool GuiApplication::readUIFile(QString const & name, bool include)
        // Ensure that a file is read only once (prevents include loops)
        static QStringList uifiles;
        if (uifiles.contains(name)) {
-               LYXERR(Debug::INIT, "UI file '" << name << "' has been read already. "
-                                   << "Is this an include loop?");
-               return false;
+               if (!include) {
+                       // We are reading again the top uifile so reset the safeguard:
+                       uifiles.clear();
+                       d->menus_.reset();
+                       d->toolbars_.reset();
+               } else {
+                       LYXERR(Debug::INIT, "UI file '" << name << "' has been read already. "
+                               << "Is this an include loop?");
+                       return false;
+               }
        }
 
        LYXERR(Debug::INIT, "About to read " << name << "...");