2002-08-25 18:26:45 +00:00
|
|
|
/**
|
|
|
|
* \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"
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
pair<int, QLPopupMenu *> createMenu(QMenuData * parent, MenuItem const * item, Menubar::Pimpl * owner, bool is_toplevel)
|
2002-08-25 18:26:45 +00:00
|
|
|
{
|
|
|
|
// FIXME: leaks ??
|
2002-09-10 19:23:38 +00:00
|
|
|
QLPopupMenu * pm = new QLPopupMenu(owner, item->submenuname(), is_toplevel);
|
|
|
|
int id = parent->insertItem(getLabel(*item).c_str(), pm);
|
|
|
|
return make_pair(id, pm);
|
2002-08-25 18:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
QLPopupMenu::QLPopupMenu(Menubar::Pimpl * owner, string const & name, bool toplevel)
|
2002-08-25 18:26:45 +00:00
|
|
|
: owner_(owner), name_(name)
|
|
|
|
{
|
2002-09-10 19:23:38 +00:00
|
|
|
if (toplevel)
|
|
|
|
connect(this, SIGNAL(aboutToShow()), this, SLOT(showing()));
|
2002-09-13 00:04:22 +00:00
|
|
|
connect(this, SIGNAL(activated(int)),
|
|
|
|
owner_->view(), SLOT(activated(int)));
|
2002-08-25 18:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
bool QLPopupMenu::disabled(Menu * menu)
|
2002-08-25 18:26:45 +00:00
|
|
|
{
|
|
|
|
bool disable = true;
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
Menu::const_iterator m = menu->begin();
|
|
|
|
Menu::const_iterator end = menu->end();
|
2002-08-25 18:26:45 +00:00
|
|
|
for (; m != end; ++m) {
|
2002-09-10 19:23:38 +00:00
|
|
|
if (m->kind() == MenuItem::Submenu && !disabled(m->submenu())) {
|
2002-08-25 18:26:45 +00:00
|
|
|
disable = false;
|
|
|
|
} else {
|
|
|
|
FuncStatus const status =
|
|
|
|
owner_->view()->getLyXFunc().getStatus(m->action());
|
|
|
|
if (!status.disabled())
|
|
|
|
disable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
void QLPopupMenu::populate(Menu * menu)
|
2002-08-25 18:26:45 +00:00
|
|
|
{
|
2002-09-10 19:23:38 +00:00
|
|
|
Menu::const_iterator m = menu->begin();
|
|
|
|
Menu::const_iterator end = menu->end();
|
2002-08-25 18:26:45 +00:00
|
|
|
for (; m != end; ++m) {
|
|
|
|
if (m->kind() == MenuItem::Separator) {
|
|
|
|
insertSeparator();
|
|
|
|
} else if (m->kind() == MenuItem::Submenu) {
|
2002-09-14 20:10:10 +00:00
|
|
|
pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
|
2002-09-10 19:23:38 +00:00
|
|
|
setItemEnabled(res.first, !disabled(m->submenu()));
|
|
|
|
res.second->populate(m->submenu());
|
2002-08-25 18:26:45 +00:00
|
|
|
} else {
|
|
|
|
FuncStatus const status =
|
|
|
|
owner_->view()->getLyXFunc().getStatus(m->action());
|
2002-08-25 20:53:39 +00:00
|
|
|
if (status.disabled() && m->optional())
|
|
|
|
continue;
|
|
|
|
insertItem(getLabel(*m).c_str(), m->action());
|
2002-08-25 18:26:45 +00:00
|
|
|
setItemEnabled(m->action(), !status.disabled());
|
|
|
|
setItemChecked(m->action(), status.onoff(true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-10 19:23:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
void QLPopupMenu::showing()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
Menu tomenu;
|
|
|
|
Menu const frommenu = owner_->backend().getMenu(name_);
|
|
|
|
owner_->backend().expand(frommenu, tomenu, owner_->view()->buffer());
|
|
|
|
populate(&tomenu);
|
|
|
|
}
|