From fc2658eff011adb08192cf897416698c34aab268 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Wed, 26 Dec 2007 12:40:58 +0000 Subject: [PATCH] initial basic context menu support. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22309 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 9 +++++++-- src/BufferView.h | 2 +- src/frontends/qt4/GuiView.cpp | 3 ++- src/frontends/qt4/GuiWorkArea.cpp | 22 ++++++++++++++++------ src/frontends/qt4/Menus.cpp | 11 ++++++++--- src/frontends/qt4/Menus.h | 6 +++--- src/insets/Inset.cpp | 6 ++++++ src/insets/Inset.h | 4 ++++ 8 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 3cab67b095..3fdb769ed7 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -492,10 +492,15 @@ docstring BufferView::toolTip(int x, int y) const } -Menu const & BufferView::contextMenu(int x, int y) const +docstring BufferView::contextMenu(int x, int y) const { + // Get inset under mouse, if there is one. + Inset const * covering_inset = getCoveringInset(buffer_.text(), x, y); + if (covering_inset) + return covering_inset->contextMenu(*this, x, y); + // FIXME: Do something more elaborate here. - return theApp()->menuBackend().getMenu(from_ascii("edit")); + return from_ascii("edit"); } diff --git a/src/BufferView.h b/src/BufferView.h index 53821b7693..f1897cb964 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -108,7 +108,7 @@ public: /// \return Tool tip for the given position. docstring toolTip(int x, int y) const; /// \return the context menu for the given position. - Menu const & contextMenu(int x, int y) const; + docstring contextMenu(int x, int y) const; /// Save the current position as bookmark. /// if idx == 0, save to temp_bookmark diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 72df7a3e08..85890fed8e 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -1365,7 +1365,8 @@ bool GuiView::dispatch(FuncRequest const & cmd) break; case LFUN_MENU_OPEN: - guiApp->menus().openByName(toqstr(cmd.argument())); + if (QMenu * menu = guiApp->menus().menu(toqstr(cmd.argument()))) + menu->exec(QCursor::pos()); break; case LFUN_FILE_INSERT: diff --git a/src/frontends/qt4/GuiWorkArea.cpp b/src/frontends/qt4/GuiWorkArea.cpp index fe83308bc7..0f71037183 100644 --- a/src/frontends/qt4/GuiWorkArea.cpp +++ b/src/frontends/qt4/GuiWorkArea.cpp @@ -519,12 +519,22 @@ bool GuiWorkArea::event(QEvent * e) void GuiWorkArea::contextMenuEvent(QContextMenuEvent * e) { QPoint pos = e->pos(); - Menu const & menu = buffer_view_->contextMenu(pos.x(), pos.y()); - LYXERR(Debug::GUI, "context menu resquested: " << menu.name()); - - // FIXME: do something with GuiPopupMenu and MenuBackend. - // Some cleanups of GuiPopupMenu and GuiMenubar are needed - // before! + docstring name = buffer_view_->contextMenu(pos.x(), pos.y()); + if (name.empty()) { + QAbstractScrollArea::contextMenuEvent(e); + return; + } + QMenu * menu = guiApp->menus().menu(toqstr(name)); + if (!menu) { + QAbstractScrollArea::contextMenuEvent(e); + return; + } + // Position the menu to the right. + // FIXME: menu position should be different for RTL text. + pos.rx() += menu->width(); + // FIXME: correct vertical position of the menu WRT screen espace. + //pos.ry() += menu->height(); + menu->exec(pos); QAbstractScrollArea::contextMenuEvent(e); } diff --git a/src/frontends/qt4/Menus.cpp b/src/frontends/qt4/Menus.cpp index f47d94d1aa..9175d23cff 100644 --- a/src/frontends/qt4/Menus.cpp +++ b/src/frontends/qt4/Menus.cpp @@ -86,10 +86,15 @@ void Menus::fillMenuBar(GuiView * view) } -void Menus::openByName(QString const & name) +QMenu * Menus::menu(QString const & name) { - if (QMenu * menu = name_map_.value(name)) - menu->exec(QCursor::pos()); + LYXERR(Debug::GUI, "Context menu requested: " + << qstring_to_ucs4(name)); + GuiPopupMenu * menu = name_map_.value(name, 0); + if (!menu) + LYXERR0("resquested context menu not found: " + << qstring_to_ucs4(name)); + return menu; } diff --git a/src/frontends/qt4/Menus.h b/src/frontends/qt4/Menus.h index 633cd72151..6f67d1d5e1 100644 --- a/src/frontends/qt4/Menus.h +++ b/src/frontends/qt4/Menus.h @@ -34,8 +34,8 @@ public: /// void fillMenuBar(GuiView * view); - /// opens a top-level submenu given its name - void openByName(QString const & name); + /// \return a top-level submenu given its name. + QMenu * menu(QString const & name); /// update the state of the menuitems - not needed void updateView(); @@ -46,7 +46,7 @@ private: typedef QHash NameMap; - /// name to menu for openByName + /// name to menu for \c menu() method. NameMap name_map_; }; diff --git a/src/insets/Inset.cpp b/src/insets/Inset.cpp index 7e833e450b..616c180c3a 100644 --- a/src/insets/Inset.cpp +++ b/src/insets/Inset.cpp @@ -131,6 +131,12 @@ docstring Inset::toolTip(BufferView const &, int, int) const } +docstring Inset::contextMenu(BufferView const &, int, int) const +{ + return docstring(); +} + + Dimension const Inset::dimension(BufferView const & bv) const { return bv.coordCache().getInsets().dim(this); diff --git a/src/insets/Inset.h b/src/insets/Inset.h index a519ec536f..c512abc27b 100644 --- a/src/insets/Inset.h +++ b/src/insets/Inset.h @@ -288,6 +288,10 @@ public: /// This default implementation returns an empty string. virtual docstring toolTip(BufferView const & bv, int x, int y) const; + /// \return Context menu identifier for this inset. + /// This default implementation returns an empty string. + virtual docstring contextMenu(BufferView const & bv, int x, int y) const; + // FIXME This should really disappear in favor of // docstring name() const { return from_ascii(insetName(lyxCode()))); } // There's no reason to be using different names in different places. -- 2.39.2