2002-08-25 18:26:45 +00:00
|
|
|
/**
|
|
|
|
* \file QLPopupMenu.h
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-08-25 18:26:45 +00:00
|
|
|
*
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author John Levon
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-08-25 18:26:45 +00:00
|
|
|
*/
|
|
|
|
|
2002-09-24 13:57:09 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
#include "support/lstrings.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "MenuBackend.h"
|
|
|
|
#include "lyxfunc.h"
|
2002-11-20 16:46:17 +00:00
|
|
|
#include "kbmap.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "debug.h"
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "QtView.h"
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "QLPopupMenu.h"
|
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-09-10 19:23:38 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
namespace {
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
string const getLabel(MenuItem const & mi)
|
|
|
|
{
|
|
|
|
string const shortcut = mi.shortcut();
|
2002-10-20 01:48:28 +00:00
|
|
|
string label = subst(mi.label(), "&", "&&");
|
2002-08-25 18:26:45 +00:00
|
|
|
|
|
|
|
if (shortcut.empty())
|
|
|
|
return label;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
string::size_type pos = label.find(shortcut);
|
|
|
|
if (pos == string::npos)
|
|
|
|
return label;
|
2002-11-27 10:30:28 +00:00
|
|
|
label.insert(pos, 1, '&');
|
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
if (mi.kind() == MenuItem::Command) {
|
|
|
|
// FIXME: backend should do this
|
|
|
|
string const accel(toplevel_keymap->findbinding(mi.action()));
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
if (!accel.empty()) {
|
2002-11-27 10:30:28 +00:00
|
|
|
label += '\t' + accel.substr(1, accel.find(']') - 1);
|
2002-11-20 16:46:17 +00:00
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
lyxerr[Debug::GUI] << "Label: " << mi.label()
|
|
|
|
<< " Shortcut: " << mi.shortcut()
|
|
|
|
<< " Accel: " << accel << endl;
|
|
|
|
} else
|
|
|
|
lyxerr[Debug::GUI] << "Label: " << mi.label()
|
|
|
|
<< " Shortcut: " << mi.shortcut() << endl;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
} // namespace anon
|
2002-08-25 18:26:45 +00:00
|
|
|
|
|
|
|
|
2002-10-20 01:48:28 +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-10-20 01:48:28 +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-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
|
2002-11-20 17:21:42 +00:00
|
|
|
// FIXME: should all be in backend
|
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-10-20 01:48:28 +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) {
|
2002-11-20 17:21:42 +00:00
|
|
|
if (m->kind() == MenuItem::Submenu) {
|
|
|
|
if (!disabled(m->submenu()))
|
|
|
|
disable = false;
|
|
|
|
} else if (m->kind() != MenuItem::Separator) {
|
2002-08-25 18:26:45 +00:00
|
|
|
FuncStatus const status =
|
2002-10-20 01:48:28 +00:00
|
|
|
owner_->view()->getLyXFunc()
|
|
|
|
.getStatus(m->action());
|
2002-08-25 18:26:45 +00:00
|
|
|
if (!status.disabled())
|
|
|
|
disable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return disable;
|
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-08-25 18:26:45 +00:00
|
|
|
|
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
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
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());
|
2002-10-20 01:48:28 +00:00
|
|
|
populate(&tomenu);
|
2002-09-10 19:23:38 +00:00
|
|
|
}
|