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>
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
#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-12-17 20:37:13 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-08-25 18:26:45 +00:00
|
|
|
|
2002-11-20 16:46:17 +00:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2002-12-17 02:43:16 +00:00
|
|
|
using std::endl;
|
2002-09-10 19:23:38 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::make_pair;
|
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) {
|
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
|
|
|
}
|
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()
|
2003-02-15 21:03:40 +00:00
|
|
|
<< " Accel: " << binding << endl;
|
2002-11-20 16:46:17 +00:00
|
|
|
} 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 *>
|
2003-02-15 21:03:40 +00:00
|
|
|
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);
|
2002-12-17 20:37:13 +00:00
|
|
|
int 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
|
|
|
|
|
|
|
|
|
|
|
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-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) {
|
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_);
|
2003-02-15 21:03:40 +00:00
|
|
|
setItemEnabled(res.first,
|
|
|
|
!m->status().disabled());
|
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
|
|
|
|
2002-12-17 20:37:13 +00:00
|
|
|
insertItem(toqstr(getLabel(*m)), 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_);
|
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
|
|
|
}
|