]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiApplication.cpp
Compil fix.
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
index df3039e36c88690db4b36c856feea09df25d8a81..eaeb6e138aeca5109e414841280eae804fbd632a 100644 (file)
@@ -22,6 +22,7 @@
 #include "GuiView.h"
 #include "Menus.h"
 #include "qt_helpers.h"
+#include "Toolbars.h"
 
 #include "frontends/alert.h"
 #include "frontends/Application.h"
 #include "Buffer.h"
 #include "BufferList.h"
 #include "BufferView.h"
+#include "Color.h"
 #include "Font.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "Language.h"
+#include "Lexer.h"
 #include "LyX.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
@@ -45,6 +48,7 @@
 #include "support/debug.h"
 #include "support/ExceptionMessage.h"
 #include "support/FileName.h"
+#include "support/foreach.h"
 #include "support/ForkedCalls.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
@@ -58,6 +62,7 @@
 #include <QClipboard>
 #include <QEventLoop>
 #include <QFileOpenEvent>
+#include <QHash>
 #include <QLocale>
 #include <QLibraryInfo>
 #include <QMacPasteboardMime>
@@ -82,7 +87,7 @@
 #endif
 
 #ifdef Q_WS_WIN
-#include <QVector>
+#include <QList>
 #include <QWindowsMime>
 #if defined(Q_CYGWIN_WIN) || defined(Q_CC_MINGW)
 #include <wtypes.h>
 #include <boost/bind.hpp>
 
 #include <exception>
-#include <map>
 
 using namespace std;
 using namespace lyx::support;
 
-// FIXME: These strings are also used in GuiClipboard.cpp.
-static char const * const lyx_mime_type = "application/x-lyx";
-static char const * const pdf_mime_type = "application/pdf";
-static char const * const emf_mime_type = "image/x-emf";
-static char const * const wmf_mime_type = "image/x-wmf";
 
 namespace lyx {
 
@@ -111,7 +110,6 @@ frontend::Application * createApplication(int & argc, char * argv[])
        return new frontend::GuiApplication(argc, argv);
 }
 
-
 namespace frontend {
 
 class SocketNotifier : public QSocketNotifier
@@ -188,7 +186,7 @@ public:
        QString flavorFor(QString const & mime)
        {
                LYXERR(Debug::ACTION, "flavorFor " << mime);
-               if (mime == QLatin1String(pdf_mime_type))
+               if (mime == pdfMimeType())
                        return QLatin1String("com.adobe.pdf");
                return QString();
        }
@@ -197,7 +195,7 @@ public:
        {
                LYXERR(Debug::ACTION, "mimeFor " << flav);
                if (flav == QLatin1String("com.adobe.pdf"))
-                       return QLatin1String(pdf_mime_type);
+                       return pdfMimeType();
                return QString();
        }
 
@@ -234,10 +232,10 @@ public:
 static FORMATETC cfFromMime(QString const & mimetype)
 {
        FORMATETC formatetc;
-       if (mimetype == emf_mime_type) {
+       if (mimetype == emfMimeType()) {
                formatetc.cfFormat = CF_ENHMETAFILE;
                formatetc.tymed = TYMED_ENHMF;
-       } else if (mimetype == wmf_mime_type) {
+       } else if (mimetype == wmfMimeType()) {
                formatetc.cfFormat = CF_METAFILEPICT;
                formatetc.tymed = TYMED_MFPICT;
        }
@@ -261,7 +259,7 @@ public:
        bool canConvertToMime(QString const & mimetype,
                IDataObject * pDataObj) const
        {
-               if (mimetype != emf_mime_type && mimetype != wmf_mime_type)
+               if (mimetype != emfMimeType() && mimetype != wmfMimeType())
                        return false;
                FORMATETC formatetc = cfFromMime(mimetype);
                return pDataObj->QueryGetData(&formatetc) == S_OK;
@@ -316,9 +314,9 @@ public:
        {
                switch (formatetc.cfFormat) {
                case CF_ENHMETAFILE:
-                       return emf_mime_type
+                       return emfMimeType()
                case CF_METAFILEPICT:
-                       return wmf_mime_type;
+                       return wmfMimeType();
                }
                return QString();
        }
@@ -332,7 +330,8 @@ public:
 
 struct GuiApplication::Private
 {
-       Private(): language_model_(0), global_menubar_(0)
+       Private()
+               : language_model_(0), global_menubar_(0)
        {
 #ifdef Q_WS_MACX
                // Create the global default menubar which is shown for the dialogs
@@ -354,9 +353,13 @@ struct GuiApplication::Private
        ///
        QTranslator qt_trans_;
        ///
-       std::map<int, SocketNotifier *> socket_notifiers_;
+       QHash<int, SocketNotifier *> socket_notifiers_;
        ///
        Menus menus_;
+       ///
+       /// The global instance
+       Toolbars toolbars_;
+
        /// this timer is used for any regular events one wants to
        /// perform. at present it is used to check if forked processes
        /// are done.
@@ -368,9 +371,7 @@ struct GuiApplication::Private
        * object is handled by Qt when the view is closed
        * \sa Qt::WA_DeleteOnClose attribute.
        */
-       std::map<int, GuiView *> views_;
-       ///
-       std::vector<int> view_ids_;
+       QHash<int, GuiView *> views_;
 
        /// Only used on mac.
        GlobalMenuBar * global_menubar_;
@@ -489,34 +490,40 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
 }
 
 
-FuncStatus GuiApplication::getStatus(FuncRequest const & cmd)
+bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 {
-       FuncStatus flag;
        bool enable = true;
 
        switch(cmd.action) {
 
        case LFUN_WINDOW_CLOSE:
-               enable = d->view_ids_.size() > 0;
+               enable = d->views_.size() > 0;
+               break;
+
+       case LFUN_BUFFER_NEW:
+       case LFUN_BUFFER_NEW_TEMPLATE:
+       case LFUN_FILE_OPEN:
+       case LFUN_SCREEN_FONT_UPDATE:
+       case LFUN_SET_COLOR:
+       case LFUN_WINDOW_NEW:
+       case LFUN_LYX_QUIT:
+               enable = true;
                break;
 
        default:
-               if (!current_view_) {
-                       enable = false;
-                       break;
-               }
+               return false;
        }
 
        if (!enable)
                flag.enabled(false);
 
-       return flag;
+       return true;
 }
 
        
 bool GuiApplication::dispatch(FuncRequest const & cmd)
 {
-       switch(cmd.action) {
+       switch (cmd.action) {
 
        case LFUN_WINDOW_NEW:
                createView(toqstr(cmd.argument()));
@@ -556,29 +563,31 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
        }
 
        case LFUN_BUFFER_NEW:
-               if (viewCount() == 0
+               if (d->views_.empty()
                    || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
                        createView(QString(), false); // keep hidden
                        current_view_->newDocument(to_utf8(cmd.argument()), false);
                        current_view_->show();
                        setActiveWindow(current_view_);
-               } else
+               } else {
                        current_view_->newDocument(to_utf8(cmd.argument()), false);
+               }
                break;
 
        case LFUN_BUFFER_NEW_TEMPLATE:
-               if (viewCount() == 0 
+               if (d->views_.empty()
                    || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
                        createView();
                        current_view_->newDocument(to_utf8(cmd.argument()), true);
                        if (!current_view_->buffer())
                                current_view_->close();
-               } else
+               } else {
                        current_view_->newDocument(to_utf8(cmd.argument()), true);
+               }
                break;
 
        case LFUN_FILE_OPEN:
-               if (viewCount() == 0
+               if (d->views_.empty()
                    || (!lyxrc.open_buffers_in_tabs && current_view_->buffer() != 0)) {
                        createView();
                        current_view_->openDocument(to_utf8(cmd.argument()));
@@ -588,6 +597,38 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
                        current_view_->openDocument(to_utf8(cmd.argument()));
                break;
 
+       case LFUN_SET_COLOR: {
+               string lyx_name;
+               string const x11_name = split(to_utf8(cmd.argument()), lyx_name, ' ');
+               if (lyx_name.empty() || x11_name.empty()) {
+                       current_view_->message(
+                               _("Syntax: set-color <lyx_name> <x11_name>"));
+                       break;
+               }
+
+               string const graphicsbg = lcolor.getLyXName(Color_graphicsbg);
+               bool const graphicsbg_changed = lyx_name == graphicsbg
+                       && x11_name != graphicsbg;
+               if (graphicsbg_changed) {
+                       // FIXME: The graphics cache no longer has a changeDisplay method.
+#if 0
+                       graphics::GCache::get().changeDisplay(true);
+#endif
+               }
+
+               if (!lcolor.setColor(lyx_name, x11_name)) {
+                       current_view_->message(
+                                       bformat(_("Set-color \"%1$s\" failed "
+                                                              "- color is undefined or "
+                                                              "may not be redefined"),
+                                                                  from_utf8(lyx_name)));
+                       break;
+               }
+               // Make sure we don't keep old colors in cache.
+               d->color_cache_.clear();
+               break;
+       }
+
        default:
                // Notify the caller that the action has not been dispatched.
                return false;
@@ -600,23 +641,14 @@ bool GuiApplication::dispatch(FuncRequest const & cmd)
 
 void GuiApplication::resetGui()
 {
-       map<int, GuiView *>::iterator it;
+       QHash<int, GuiView *>::iterator it;
        for (it = d->views_.begin(); it != d->views_.end(); ++it)
-               it->second->resetDialogs();
+               (*it)->resetDialogs();
 
        dispatch(FuncRequest(LFUN_SCREEN_FONT_UPDATE));
 }
 
 
-static void updateIds(map<int, GuiView *> const & stdmap, vector<int> & ids)
-{
-       ids.clear();
-       map<int, GuiView *>::const_iterator it;
-       for (it = stdmap.begin(); it != stdmap.end(); ++it)
-               ids.push_back(it->first);
-}
-
-
 void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
 {
        // release the keyboard which might have been grabed by the global
@@ -625,19 +657,17 @@ void GuiApplication::createView(QString const & geometry_arg, bool autoShow)
                d->global_menubar_->releaseKeyboard();
 
        // create new view
-       updateIds(d->views_, d->view_ids_);
        int id = 0;
        while (d->views_.find(id) != d->views_.end())
                id++;
        GuiView * view = new GuiView(id);
        
        // copy the icon size from old view
-       if (viewCount() > 0)
+       if (current_view_)
                view->setIconSize(current_view_->iconSize());
 
        // register view
        d->views_[id] = view;
-       updateIds(d->views_, d->view_ids_);
 
        if (autoShow) {
                view->show();
@@ -680,6 +710,18 @@ FontLoader & GuiApplication::fontLoader()
 }
 
 
+Toolbars const & GuiApplication::toolbars() const 
+{
+       return d->toolbars_;
+}
+
+
+Toolbars & GuiApplication::toolbars()
+{
+       return d->toolbars_; 
+}
+
+
 Menus const & GuiApplication::menus() const 
 {
        return d->menus_;
@@ -692,17 +734,12 @@ Menus & GuiApplication::menus()
 }
 
 
-size_t GuiApplication::viewCount() const
+QList<int> GuiApplication::viewIds() const
 {
-       return d->view_ids_.size();
+       return d->views_.keys();
 }
 
 
-vector<int> const & GuiApplication::viewIds()
-{
-       return d->view_ids_;
-}
-
 ColorCache & GuiApplication::colorCache()
 {
        return d->color_cache_;
@@ -724,6 +761,11 @@ void GuiApplication::exit(int status)
 
 void GuiApplication::execBatchCommands()
 {
+       // 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_)
@@ -915,13 +957,6 @@ string const GuiApplication::hexName(ColorCode col)
 }
 
 
-void GuiApplication::updateColor(ColorCode)
-{
-       // FIXME: Bleh, can't we just clear them all at once ?
-       d->color_cache_.clear();
-}
-
-
 void GuiApplication::registerSocketCallback(int fd, SocketCallback func)
 {
        SocketNotifier * sn = new SocketNotifier(this, fd, func);
@@ -938,8 +973,7 @@ void GuiApplication::socketDataReceived(int fd)
 
 void GuiApplication::unregisterSocketCallback(int fd)
 {
-       d->socket_notifiers_[fd]->setEnabled(false);
-       d->socket_notifiers_.erase(fd);
+       d->socket_notifiers_.take(fd)->setEnabled(false);
 }
 
 
@@ -957,83 +991,153 @@ void GuiApplication::commitData(QSessionManager & sm)
 }
 
 
-bool GuiApplication::unregisterView(int id)
+void GuiApplication::unregisterView(GuiView * gv)
 {
-       updateIds(d->views_, d->view_ids_);
-       LASSERT(d->views_.find(id) != d->views_.end(), /**/);
-       LASSERT(d->views_[id], /**/);
-
-       map<int, GuiView *>::iterator it;
-       for (it = d->views_.begin(); it != d->views_.end(); ++it) {
-               if (it->first == id) {
-                       d->views_.erase(id);
-                       break;
-               }
+       LASSERT(d->views_[gv->id()] == gv, /**/);
+       d->views_.remove(gv->id());
+       if (current_view_ == gv) {
+               current_view_ = 0;
+               theLyXFunc().setLyXView(0);
        }
-       updateIds(d->views_, d->view_ids_);
-       return true;
 }
 
 
 bool GuiApplication::closeAllViews()
 {
-       updateIds(d->views_, d->view_ids_);
        if (d->views_.empty())
                return true;
 
-       map<int, GuiView*> const cmap = d->views_;
-       map<int, GuiView*>::const_iterator it;
-       for (it = cmap.begin(); it != cmap.end(); ++it) {
-               if (!it->second->close())
+       QList<GuiView *> views = d->views_.values();
+       foreach (GuiView * view, views) {
+               if (!view->close())
                        return false;
        }
 
        d->views_.clear();
-       d->view_ids_.clear();
        return true;
 }
 
 
 GuiView & GuiApplication::view(int id) const
 {
-       LASSERT(d->views_.find(id) != d->views_.end(), /**/);
-       return *d->views_.find(id)->second;
+       LASSERT(d->views_.contains(id), /**/);
+       return *d->views_.value(id);
 }
 
 
 void GuiApplication::hideDialogs(string const & name, Inset * inset) const
 {
-       vector<int>::const_iterator it = d->view_ids_.begin();
-       vector<int>::const_iterator const end = d->view_ids_.end();
-       for (; it != end; ++it)
-               view(*it).hideDialog(name, inset);
+       QList<GuiView *> views = d->views_.values();
+       foreach (GuiView * view, views)
+               view->hideDialog(name, inset);
 }
 
 
 Buffer const * GuiApplication::updateInset(Inset const * inset) const
 {
        Buffer const * buffer_ = 0;
-       vector<int>::const_iterator it = d->view_ids_.begin();
-       vector<int>::const_iterator const end = d->view_ids_.end();
-       for (; it != end; ++it) {
-               Buffer const * ptr = view(*it).updateInset(inset);
-               if (ptr)
+       QHash<int, GuiView *>::iterator end = d->views_.end();
+       for (QHash<int, GuiView *>::iterator it = d->views_.begin(); it != end; ++it) {
+               if (Buffer const * ptr = (*it)->updateInset(inset))
                        buffer_ = ptr;
        }
        return buffer_;
 }
 
 
-void GuiApplication::readMenus(Lexer & lex)
+bool GuiApplication::searchMenu(FuncRequest const & func,
+       docstring_list & names) const
 {
-       menus().read(lex);
+       return d->menus_.searchMenu(func, names);
 }
 
 
-bool GuiApplication::searchMenu(FuncRequest const & func,
-       vector<docstring> & names) const
+bool GuiApplication::readUIFile(QString const & name, bool include)
 {
-       return menus().searchMenu(func, names);
+       enum {
+               ui_menuset = 1,
+               ui_toolbars,
+               ui_toolbarset,
+               ui_include,
+               ui_last
+       };
+
+       LexerKeyword uitags[] = {
+               { "include", ui_include },
+               { "menuset", ui_menuset },
+               { "toolbars", ui_toolbars },
+               { "toolbarset", ui_toolbarset }
+       };
+
+       // 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;
+       }
+
+       LYXERR(Debug::INIT, "About to read " << name << "...");
+
+       FileName ui_path;
+       if (include) {
+               ui_path = libFileSearch("ui", name, "inc");
+               if (ui_path.empty())
+                       ui_path = libFileSearch("ui", changeExtension(name, "inc"));
+       } else {
+               ui_path = libFileSearch("ui", name, "ui");
+       }
+
+       if (ui_path.empty()) {
+               LYXERR(Debug::INIT, "Could not find " << name);
+               Alert::warning(_("Could not find UI defintion file"),
+                              bformat(_("Error while reading the configuration file\n%1$s.\n"
+                                  "Please check your installation."), qstring_to_ucs4(name)));
+               return false;
+       }
+
+       uifiles.push_back(name);
+
+       LYXERR(Debug::INIT, "Found " << name << " in " << ui_path);
+       Lexer lex(uitags);
+       lex.setFile(ui_path);
+       if (!lex.isOK()) {
+               lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
+                      << endl;
+       }
+
+       if (lyxerr.debugging(Debug::PARSER))
+               lex.printTable(lyxerr);
+
+       while (lex.isOK()) {
+               switch (lex.lex()) {
+               case ui_include: {
+                       lex.next(true);
+                       QString const file = toqstr(lex.getString());
+                       if (!readUIFile(file, true))
+                               return false;
+                       break;
+               }
+               case ui_menuset:
+                       d->menus_.read(lex);
+                       break;
+
+               case ui_toolbarset:
+                       d->toolbars_.readToolbars(lex);
+                       break;
+
+               case ui_toolbars:
+                       d->toolbars_.readToolbarSettings(lex);
+                       break;
+
+               default:
+                       if (!rtrim(lex.getString()).empty())
+                               lex.printError("LyX::ReadUIFile: "
+                                              "Unknown menu tag: `$$Token'");
+                       break;
+               }
+       }
+       return true;
 }
 
 
@@ -1063,7 +1167,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
                if (bv) {
                        docstring const sel = bv->requestSelection();
                        if (!sel.empty())
-                               selection_.put(sel);
+                               d->selection_.put(sel);
                }
                break;
        }