2002-08-25 18:26:45 +00:00
|
|
|
/**
|
2003-06-28 01:23:11 +00:00
|
|
|
* \file QLPopupMenu.C
|
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
|
|
|
*
|
2003-08-23 00:17:00 +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>
|
|
|
|
|
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
#include "support/lstrings.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "MenuBackend.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
|
|
|
|
2003-07-25 21:34:45 +00:00
|
|
|
#include "QLMenubar.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
#include "QLPopupMenu.h"
|
2002-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::subst;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2004-01-28 16:21:29 +00:00
|
|
|
using std::distance;
|
2003-09-09 22:13:45 +00:00
|
|
|
using std::make_pair;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2002-09-10 19:23:38 +00:00
|
|
|
using std::pair;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
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
|
|
|
|
2003-09-21 23:00:47 +00:00
|
|
|
if (!shortcut.empty()) {
|
|
|
|
string::size_type pos = label.find(shortcut);
|
|
|
|
if (pos != string::npos)
|
|
|
|
label.insert(pos, 1, '&');
|
|
|
|
}
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
if (mi.kind() == MenuItem::Command) {
|
2003-02-15 21:03:40 +00:00
|
|
|
string const binding(mi.binding());
|
|
|
|
if (!binding.empty()) {
|
|
|
|
label += '\t' + binding;
|
2002-11-20 16:46:17 +00:00
|
|
|
}
|
2003-03-27 04:29:14 +00:00
|
|
|
}
|
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 *>
|
2003-07-25 21:34:45 +00:00
|
|
|
createMenu(QMenuData * parent, MenuItem const * item, QLMenubar * owner,
|
2003-02-15 21:03:40 +00:00
|
|
|
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);
|
2003-09-21 18:57:15 +00:00
|
|
|
int const id = parent->insertItem(toqstr(getLabel(*item)), pm);
|
2002-09-10 19:23:38 +00:00
|
|
|
return make_pair(id, pm);
|
2002-08-25 18:26:45 +00:00
|
|
|
}
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
2003-07-25 21:34:45 +00:00
|
|
|
QLPopupMenu::QLPopupMenu(QLMenubar * owner,
|
2002-10-20 01:48:28 +00:00
|
|
|
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)),
|
2003-09-21 18:57:15 +00:00
|
|
|
this, SLOT(fire(int)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void QLPopupMenu::fire(int index)
|
|
|
|
{
|
|
|
|
owner_->view()->activated(funcs_[index]);
|
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-09-10 19:23:38 +00:00
|
|
|
void QLPopupMenu::populate(Menu * menu)
|
2002-08-25 18:26:45 +00:00
|
|
|
{
|
2003-09-21 18:57:15 +00:00
|
|
|
funcs_.clear();
|
|
|
|
|
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) {
|
2003-02-15 21:03:40 +00:00
|
|
|
insertSeparator();
|
2002-08-25 18:26:45 +00:00
|
|
|
} else if (m->kind() == MenuItem::Submenu) {
|
2002-09-14 20:10:10 +00:00
|
|
|
pair<int, QLPopupMenu *> res = createMenu(this, &(*m), owner_);
|
2004-03-18 13:57:20 +00:00
|
|
|
setItemEnabled(res.first, m->status().enabled());
|
2002-09-10 19:23:38 +00:00
|
|
|
res.second->populate(m->submenu());
|
2002-08-25 18:26:45 +00:00
|
|
|
} else {
|
2003-02-15 21:03:40 +00:00
|
|
|
FuncStatus const status = m->status();
|
2003-02-25 13:35:26 +00:00
|
|
|
|
2003-09-21 18:57:15 +00:00
|
|
|
Funcs::iterator fit =
|
|
|
|
funcs_.insert(funcs_.end(), m->func());
|
2004-01-28 16:21:29 +00:00
|
|
|
int const index = distance(funcs_.begin(), fit);
|
2003-09-21 18:57:15 +00:00
|
|
|
|
|
|
|
insertItem(toqstr(getLabel(*m)), index);
|
2004-03-18 13:57:20 +00:00
|
|
|
setItemEnabled(index, status.enabled());
|
2003-09-21 18:57:15 +00:00
|
|
|
setItemChecked(index, status.onoff(true));
|
2002-08-25 18:26:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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_);
|
2003-02-15 21:03:40 +00:00
|
|
|
owner_->backend().expand(frommenu, tomenu, owner_->view());
|
2002-10-20 01:48:28 +00:00
|
|
|
populate(&tomenu);
|
2002-09-10 19:23:38 +00:00
|
|
|
}
|