From d63821939cccea8a57418412de06eb3f96efb58e Mon Sep 17 00:00:00 2001 From: Stefan Schimanski Date: Fri, 14 Mar 2008 23:25:11 +0000 Subject: [PATCH] * create global menubar on Mac without a parent. It will be shown if no GuiView exists. * fill global menubar with the normal actions * capture the keyboard on the menubar if no GuiView exists. This enables the shortcuts on Mac in those cases. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23731 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/LyX.cpp | 7 +++- src/frontends/Application.h | 5 +++ src/frontends/qt4/GuiApplication.cpp | 53 +++++++++++++++++++++++++--- src/frontends/qt4/GuiApplication.h | 10 ++++-- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/LyX.cpp b/src/LyX.cpp index 7de306c0d2..04970c9e39 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -877,7 +877,12 @@ bool LyX::init() // This must happen after package initialization and after lyxrc is // read, therefore it can't be done by a static object. ConverterCache::init(); - + + // init the global menubar on Mac. This must be done after the session + // was recovered to know the "last files". + if (use_gui) + theApp()->initGlobalMenu(); + return true; } diff --git a/src/frontends/Application.h b/src/frontends/Application.h index 0cc2ed00f6..ab08ace618 100644 --- a/src/frontends/Application.h +++ b/src/frontends/Application.h @@ -219,6 +219,11 @@ public: */ virtual void readMenus(Lexer & lex) = 0; + /** + * initialize the global menubar on Mac + */ + virtual void initGlobalMenu() = 0; + /** * add a callback for socket read notification * @param fd socket descriptor (file/socket/etc) diff --git a/src/frontends/qt4/GuiApplication.cpp b/src/frontends/qt4/GuiApplication.cpp index 704533d4af..b3ba440f41 100644 --- a/src/frontends/qt4/GuiApplication.cpp +++ b/src/frontends/qt4/GuiApplication.cpp @@ -16,6 +16,7 @@ #include "qt_helpers.h" #include "GuiImage.h" +#include "GuiKeySymbol.h" #include "GuiView.h" #include "frontends/alert.h" @@ -121,6 +122,27 @@ public: } }; +class GlobalMenuBar : public QMenuBar +{ +public: + /// + GlobalMenuBar() : QMenuBar(0) {} + + /// + bool event(QEvent * e) + { + if (e->type() == QEvent::ShortcutOverride) { + // && activeWindow() == 0) { + QKeyEvent * ke = static_cast(e); + KeySymbol sym; + setKeySymbol(&sym, ke); + theLyXFunc().processKeySym(sym, q_key_state(ke->modifiers())); + e->accept(); + return true; + } + return false; + } +}; /////////////////////////////////////////////////////////////// // You can find more platform specific stuff @@ -132,7 +154,7 @@ GuiApplication * guiApp; GuiApplication::GuiApplication(int & argc, char ** argv) - : QApplication(argc, argv), Application(), current_view_(0) + : QApplication(argc, argv), Application(), current_view_(0), global_menubar_(0) { QString app_name = "LyX"; QCoreApplication::setOrganizationName(app_name); @@ -146,7 +168,10 @@ GuiApplication::GuiApplication(int & argc, char ** argv) if (lyxrc.quit_on_last_window_closed) setQuitOnLastWindowClosed(false); */ - +#ifdef Q_WS_MAC + setQuitOnLastWindowClosed(false); +#endif + #ifdef Q_WS_X11 // doubleClickInterval() is 400 ms on X11 which is just too long. // On Windows and Mac OS X, the operating system's value is used. @@ -175,11 +200,10 @@ GuiApplication::GuiApplication(int & argc, char ** argv) << fromqstr(language_name)); #ifdef Q_WS_MACX - // all windows in a Mac application share the same menu bar. - QMenuBar *menuBar = new QMenuBar(0); // This allows to translate the strings that appear in the LyX menu. addMenuTranslator(); #endif + connect(this, SIGNAL(lastWindowClosed()), this, SLOT(onLastWindowClosed())); using namespace lyx::graphics; @@ -210,6 +234,14 @@ GuiApplication::GuiApplication(int & argc, char ** argv) connect(&general_timer_, SIGNAL(timeout()), this, SLOT(handleRegularEvents())); general_timer_.start(); + +#ifdef Q_WS_MACX + if (global_menubar_ == 0) { + // Create the global default menubar which is shown for the dialogs + // and if no GuiView is visible. + global_menubar_ = new GlobalMenuBar(); + } +#endif } @@ -697,6 +729,19 @@ bool GuiApplication::searchMenu(FuncRequest const & func, } +void GuiApplication::initGlobalMenu() +{ + if (global_menubar_) + menus().fillMenuBar(global_menubar_, 0); +} + + +void GuiApplication::onLastWindowClosed() +{ + if (global_menubar_) + global_menubar_->grabKeyboard(); +} + //////////////////////////////////////////////////////////////////////// // X11 specific stuff goes here... #ifdef Q_WS_X11 diff --git a/src/frontends/qt4/GuiApplication.h b/src/frontends/qt4/GuiApplication.h index f9a770a3ab..c6695cc131 100644 --- a/src/frontends/qt4/GuiApplication.h +++ b/src/frontends/qt4/GuiApplication.h @@ -39,6 +39,7 @@ namespace frontend { class GuiView; class LyXView; +class GlobalMenuBar; class GuiWorkArea; class SocketNotifier; @@ -72,6 +73,7 @@ public: virtual std::string const hexName(ColorCode col); virtual void updateColor(ColorCode col); virtual void readMenus(Lexer & lex); + virtual void initGlobalMenu(); virtual void registerSocketCallback(int fd, SocketCallback func); void unregisterSocketCallback(int fd); bool searchMenu(FuncRequest const & func, std::vector & names) const; @@ -87,7 +89,7 @@ public: /// Create the main window with given geometry settings. /// \param geometry_arg: only for Windows platform. - void createView(QString const & geometry_arg); + void createView(QString const & geometry_arg = QString()); /// GuiView const * currentView() const { return current_view_; } /// @@ -128,7 +130,9 @@ private Q_SLOTS: void socketDataReceived(int fd); /// events to be triggered by general_timer_ should go here void handleRegularEvents(); - + /// + void onLastWindowClosed(); + private: /// bool closeAllViews(); @@ -171,6 +175,8 @@ public: /// This LyXView is the one receiving Clipboard and Selection /// events GuiView * current_view_; + /// only used on mac + GlobalMenuBar * global_menubar_; }; // GuiApplication extern GuiApplication * guiApp; -- 2.39.2