]> git.lyx.org Git - features.git/commitdiff
Populate menus on popup - good news JMarc, it seems to work (almost) perfectly
authorJohn Levon <levon@movementarian.org>
Sun, 25 Aug 2002 18:26:45 +0000 (18:26 +0000)
committerJohn Levon <levon@movementarian.org>
Sun, 25 Aug 2002 18:26:45 +0000 (18:26 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@5101 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/qt2/ChangeLog
src/frontends/qt2/Makefile.am
src/frontends/qt2/Menubar_pimpl.C
src/frontends/qt2/Menubar_pimpl.h
src/frontends/qt2/QLPopupMenu.C [new file with mode: 0644]
src/frontends/qt2/QLPopupMenu.h [new file with mode: 0644]
src/frontends/qt2/moc/Makefile.am

index 58bd3472a7d6218a7ea32ed838c6cf36810d3913..94057198d72008d9100d0babb7fd9e5f1acbe2e4 100644 (file)
@@ -1,3 +1,11 @@
+2002-08-25  John Levon  <levon@movementarian.org>
+
+       * Makefile.am:
+       * Menubar_pimpl.h:
+       * Menubar_pimpl.C:
+       * QLPopupMenu.h:
+       * QLPopupMenu.C: Populate menus upon open
 2002-08-25  John Levon  <levon@movementarian.org>
 
        * qlkey.h: add A-Z into string_to_qkey
index 8ae0c389728aaac3812cf7972dbbb8957e95b4c3..64915dc5e6a24efa33cda9423c50cf270145c769 100644 (file)
@@ -41,6 +41,8 @@ libqt2_la_SOURCES = \
        QLImage.h \
        QLPainter.C \
        QLPainter.h \
+       QLPopupMenu.C \
+       QLPopupMenu.h \
        QLyXKeySym.C \
        QLyXKeySym.h \
        Qt2Base.C \
index e62242f2e47ddfe974884e7baa7221d41995fa86..d0dbb5a9c83cc48b642a5c6d45e300ca59937203 100644 (file)
 #include "debug.h"
 
 #include "QtView.h"
+#include "QLPopupMenu.h"
  
 #include <qmenubar.h>
-#include <qpopupmenu.h>
  
 using std::endl;
 using std::vector;
@@ -37,141 +38,18 @@ using std::max;
 using std::min;
 using std::for_each;
 
-namespace {
-string const getLabel(MenuItem const & mi)
-{
-       string const shortcut = mi.shortcut();
-       string label = mi.label();
-
-       label = subst(label, "&", "&&");
-       if (shortcut.empty())
-               return label;
-       string::size_type pos = label.find(shortcut);
-       if (pos == string::npos)
-               return label;
-       label.insert(pos, "&");
-       return label;
-}
-
-}
-typedef vector<int>::size_type size_type;
-
-extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
-
-
 Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe) 
        : owner_(static_cast<QtView*>(view)), menubackend_(mbe)
 {
        Menu::const_iterator m = mbe.getMenubar().begin();
        Menu::const_iterator end = mbe.getMenubar().end();
        for (; m != end; ++m) {
-               Menu tomenu;
-               Menu const frommenu = menubackend_.getMenu(m->submenuname());
-               menubackend_.expand(frommenu, tomenu, owner_->buffer());
-               fillMenu(createMenu(owner_->menuBar(), &(*m)), tomenu);
+               createMenu(owner_->menuBar(), &(*m), this);
        }
 }
 
 
-QPopupMenu * Menubar::Pimpl::createMenu(QMenuData * parent, MenuItem const * item)
-{
-       // FIXME: does this leak or not ?
-       QPopupMenu * pm = new QPopupMenu();
-       int const parentid = parent->insertItem(getLabel(*item).c_str(), pm);
-
-       MenuItemInfo const info(parent, parentid, item);
-       items_[item->label()] = info;
-       return pm;
-}
-
-void Menubar::Pimpl::fillMenu(QMenuData * qmenu, Menu const & menu)
-{
-       Menu::const_iterator m = menu.begin();
-       Menu::const_iterator end = menu.end();
-       for (; m != end; ++m) {
-               if (m->kind() == MenuItem::Separator) {
-                       qmenu->insertSeparator();
-               } else if (m->kind() == MenuItem::Submenu) {
-                       fillMenu(createMenu(qmenu, &(*m)), *m->submenu());
-               } else {
-                       qmenu->insertItem(getLabel(*m).c_str(), m->action());
-                       MenuItemInfo const info(qmenu, m->action(), &(*m));
-                       items_[m->label()] = info;
-                       updateItem(info);
-               }
-       }
-}
-
-// FIXME: this is probably buggy with respect to enabling
-// two-level submenus
-void Menubar::Pimpl::updateSubmenu(MenuItemInfo const & i)
+void Menubar::Pimpl::openByName(string const &)
 {
-#if 0 // SEGFAULTS
-// 7  0x0809d372 in Menu::begin (this=0x0) at MenuBackend.h:138
-// 8  0x081dcf60 in Menubar::Pimpl::updateSubmenu (this=0x839eaa0, i=@0xbffff010) at Menubar_pimpl.C:116 
-       bool enable = false;
-       Menu::const_iterator m = i.item_->submenu().begin();
-       Menu::const_iterator end = i.item_->submenu().end();
-       for (; m != end; ++m) {
-               if (m->action() > 0) {
-                       FuncStatus const status =
-                               owner_->getLyXFunc().getStatus(m->action());
-                       if (!status.disabled())
-                               enable = true;
-               }
-       }
-#else
-       bool enable = true;
-#endif
-       i.parent_->setItemEnabled(i.id_, enable);
-}
-void Menubar::Pimpl::updateItem(MenuItemInfo const & i)
-{
-       if (i.item_->kind() == MenuItem::Submenu) {
-               updateSubmenu(i);
-               return;
-       }
-       // FIXME
-       if (i.id_ < 0)
-               return;
-       FuncStatus const status = owner_->getLyXFunc().getStatus(i.id_);
-       i.parent_->setItemEnabled(i.id_, !status.disabled());
-       i.parent_->setItemChecked(i.id_, status.onoff(true));
-}
-
-void Menubar::Pimpl::update()
-{
-       // FIXME: handle special stuff to be updated.
-       ItemMap::const_iterator cit = items_.begin();
-       ItemMap::const_iterator end = items_.end();
-
-       for (; cit != end; ++cit)
-               updateItem(cit->second);
-}
-
-
-void Menubar::Pimpl::openByName(string const & name)
-{
-       lyxerr << "Menubar::Pimpl::openByName: menu " << name << endl;
-
-       ItemMap::iterator it = items_.find(name);
-
-       if (it == items_.end())
-               lyxerr << "NOT FOUND " << name << endl;
-
        // FIXME 
 }
index 0ffe61261d3a609551d74555b891c2b1d83dabfa..1dc44a423bf253fb6a9993bbe04552f5c0e21829 100644 (file)
 
 #include "LString.h"
 #include "frontends/Menubar.h"
-#include "commandtags.h"
 
 class LyXView;
 class QtView;
-class QMenuData;
-class QPopupMenu;
-class Menu;
-class MenuItem;
 class MenuBackend;
 
-/// stored state for menu items
-struct MenuItemInfo {
-       // I REALLY hate this stupid requirement of std::map
-       MenuItemInfo()
-               : parent_(0), id_(0), item_(0) {};
-       MenuItemInfo(QMenuData * p, int id, MenuItem const * item)
-               : parent_(p), id_(id), item_(item) {};
-       /// menu containing item
-       QMenuData * parent_;
-       /// id in containing menu
-       int id_;
-       /// LyX info for item
-       MenuItem const * item_;
-};
-
 struct Menubar::Pimpl {
 public:
        Pimpl(LyXView *, MenuBackend const &);
@@ -60,32 +35,20 @@ public:
        /// opens a top-level submenu given its name
        void openByName(string const &);
 
-       /// update the state of the menuitems
-       void update();
+       /// update the state of the menuitems - not needed
+       void update() {};
 
-private:
-       /// create a menu
-       QPopupMenu * createMenu(QMenuData * parent, MenuItem const * item);
-       /// populate a menu (recursively)
-       void fillMenu(QMenuData * qmenu, Menu const & menu);
-       /// special handling updating a submenu label
-       void updateSubmenu(MenuItemInfo const & i);
-       /// update an individual item, returns true if enabled
-       void updateItem(MenuItemInfo const & i);
+       /// return the owning view
+       QtView * view() { return owner_; } 
 
+       /// return the menu controller
+       MenuBackend const & backend() { return menubackend_; }
+private:
        /// owning view
        QtView * owner_;
 
        /// menu controller
        MenuBackend const & menubackend_;
-
-       typedef std::map<string, MenuItemInfo> ItemMap;
-
-       /// menu items
-       ItemMap items_;
 };
  
 #endif // MENUBAR_PIMPL_H
diff --git a/src/frontends/qt2/QLPopupMenu.C b/src/frontends/qt2/QLPopupMenu.C
new file mode 100644 (file)
index 0000000..76beaeb
--- /dev/null
@@ -0,0 +1,102 @@
+/**
+ * \file QLPopupMenu.h
+ * Copyright 2002 the LyX Team
+ * Read the file COPYING
+ *
+ * \author John Levon <levon@movementarian.org>
+ */
+
+#include "MenuBackend.h"
+#include "lyxfunc.h"
+#include "debug.h"
+#include "QtView.h"
+#include "QLPopupMenu.h"
+
+#include "support/lstrings.h"
+
+namespace {
+string const getLabel(MenuItem const & mi)
+{
+       string const shortcut = mi.shortcut();
+       string label = mi.label();
+
+       label = subst(label, "&", "&&");
+       if (shortcut.empty())
+               return label;
+       string::size_type pos = label.find(shortcut);
+       if (pos == string::npos)
+               return label;
+       label.insert(pos, "&");
+       return label;
+}
+
+} 
+
+
+int createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner)
+{
+       // FIXME: leaks ??
+       QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname());
+       return parent->insertItem(getLabel(*item).c_str(), pm);
+}
+QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name)
+       : owner_(owner), name_(name)
+{
+       connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
+}
+
+bool QLPopupMenu::disabled(string const & name)
+{
+       bool disable = true;
+       Menu tomenu;
+       Menu const frommenu = owner_->backend().getMenu(name);
+       owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
+       Menu::const_iterator m = tomenu.begin();
+       Menu::const_iterator end = tomenu.end();
+       for (; m != end; ++m) {
+               if (m->kind() == MenuItem::Submenu && !disabled(m->submenuname())) {
+                       disable = false;
+               } else {
+                       FuncStatus const status =
+                               owner_->view()->getLyXFunc().getStatus(m->action());
+                       if (!status.disabled())
+                               disable = false;
+               }
+       }
+       return disable;
+}
+
+void QLPopupMenu::showing()
+{
+       clear();
+       Menu tomenu;
+       Menu const frommenu = owner_->backend().getMenu(name_);
+       owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
+       Menu::const_iterator m = tomenu.begin();
+       Menu::const_iterator end = tomenu.end();
+       for (; m != end; ++m) {
+               if (m->kind() == MenuItem::Separator) {
+                       insertSeparator();
+               } else if (m->kind() == MenuItem::Submenu) {
+                       int id(createMenu(this, m, owner_));
+                       setItemEnabled(id, !disabled(m->submenuname()));
+               } else {
+                       insertItem(getLabel(*m).c_str(), m->action());
+                       FuncStatus const status =
+                               owner_->view()->getLyXFunc().getStatus(m->action());
+                       setItemEnabled(m->action(), !status.disabled());
+                       setItemChecked(m->action(), status.onoff(true));
+               }
+       }
+}
diff --git a/src/frontends/qt2/QLPopupMenu.h b/src/frontends/qt2/QLPopupMenu.h
new file mode 100644 (file)
index 0000000..13cae44
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * \file QLPopupMenu.h
+ * Copyright 2002 the LyX Team
+ * Read the file COPYING
+ *
+ * \author John Levon <levon@movementarian.org>
+ */
+
+#ifndef QLPOPUPMENU_H
+#define QLPOPUPMENU_H
+#include "Menubar_pimpl.h"
+#include <qpopupmenu.h> 
+
+#include "LString.h"
+class MenuBackend;
+class QtView;
+class MenuItem;
+class QMenuData;
+
+/// create a sub-menu
+int createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner);
+/// a submenu
+class QLPopupMenu : public QPopupMenu {
+       Q_OBJECT
+public:
+       QLPopupMenu(Menubar::Pimpl * owner, string const & name);
+
+public slots:
+       /// populate the menu
+       void showing();
+
+private:
+       /// return true if the given submenu is disabled
+       bool disabled(string const & name);
+       /// our owning menubar
+       Menubar::Pimpl * owner_;
+
+       /// the name of this menu
+       string name_;
+};
+
+#endif // QLPOPUPMENU_H
index da3753fde18adb63d91ea0a6755bfb6a224443a8..1d978d0515155c6770e52f95e41e54241ef67270 100644 (file)
@@ -25,6 +25,7 @@ nodist_libqt2moc_la_SOURCES = \
        QCommandBuffer_moc.C \
        QCommandEdit_moc.C \
        QContentPane_moc.C \
+       QLPopupMenu_moc.C \
        Qt2Base_moc.C \
        QtView_moc.C \
        io_callback_moc.C \