2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-12-26 10:55:43 +00:00
|
|
|
* \file qt4/Menus.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
#include "Menus.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-12-20 14:28:06 +00:00
|
|
|
#include "Action.h"
|
2007-12-25 22:27:32 +00:00
|
|
|
#include "GuiApplication.h"
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiPopupMenu.h"
|
2007-12-25 22:27:32 +00:00
|
|
|
#include "GuiView.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include <QCursor>
|
2007-02-26 19:54:41 +00:00
|
|
|
#include <QMenuBar>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
|
|
|
// MacOSX specific stuff is at the end.
|
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
void Menus::fillMenuBar(GuiView * view)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-12-23 18:51:11 +00:00
|
|
|
// Clear all menubar contents before filling it.
|
2007-12-26 10:55:43 +00:00
|
|
|
view->menuBar()->clear();
|
2007-12-23 18:51:11 +00:00
|
|
|
|
2007-12-25 21:34:53 +00:00
|
|
|
#ifdef Q_WS_MACX
|
2007-12-23 18:51:11 +00:00
|
|
|
// setup special mac specific menu item
|
2007-12-26 13:31:17 +00:00
|
|
|
macxMenuBarInit(view);
|
2007-12-25 21:34:53 +00:00
|
|
|
#endif
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
LYXERR(Debug::GUI, "populating menu bar" << to_utf8(getMenubar().name()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
if (getMenubar().size() == 0) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "\tERROR: empty menu bar"
|
2007-12-26 10:55:43 +00:00
|
|
|
<< to_utf8(getMenubar().name()));
|
2006-03-05 17:24:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "menu bar entries "
|
2007-12-26 10:55:43 +00:00
|
|
|
<< getMenubar().size());
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Menu menu;
|
2007-12-26 10:55:43 +00:00
|
|
|
expand(getMenubar(), menu, view->buffer());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
Menu::const_iterator m = menu.begin();
|
|
|
|
Menu::const_iterator end = menu.end();
|
|
|
|
|
|
|
|
for (; m != end; ++m) {
|
|
|
|
|
|
|
|
if (m->kind() != MenuItem::Submenu) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "\tERROR: not a submenu " << to_utf8(m->label()));
|
2006-03-05 17:24:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "menu bar item " << to_utf8(m->label())
|
|
|
|
<< " is a submenu named " << to_utf8(m->submenuname()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-09-09 22:27:22 +00:00
|
|
|
docstring name = m->submenuname();
|
2007-12-26 10:55:43 +00:00
|
|
|
if (!hasMenu(name)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "\tERROR: " << to_utf8(name)
|
|
|
|
<< " submenu has no menu!");
|
2006-03-05 17:24:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
GuiPopupMenu * qMenu = new GuiPopupMenu(view, *m, true);
|
|
|
|
view->menuBar()->addMenu(qMenu);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-09-28 23:26:59 +00:00
|
|
|
name_map_[toqstr(name)] = qMenu;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-26 12:40:58 +00:00
|
|
|
QMenu * Menus::menu(QString const & name)
|
2007-09-28 23:26:59 +00:00
|
|
|
{
|
2007-12-26 12:40:58 +00:00
|
|
|
LYXERR(Debug::GUI, "Context menu requested: "
|
|
|
|
<< qstring_to_ucs4(name));
|
|
|
|
GuiPopupMenu * menu = name_map_.value(name, 0);
|
|
|
|
if (!menu)
|
|
|
|
LYXERR0("resquested context menu not found: "
|
|
|
|
<< qstring_to_ucs4(name));
|
|
|
|
return menu;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-20 14:28:06 +00:00
|
|
|
/// Some special Qt/Mac support hacks
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
/*
|
|
|
|
Here is what the Qt documentation says about how a menubar is chosen:
|
|
|
|
|
|
|
|
1) If the window has a QMenuBar then it is used. 2) If the window
|
|
|
|
is a modal then its menubar is used. If no menubar is specified
|
|
|
|
then a default menubar is used (as documented below) 3) If the
|
|
|
|
window has no parent then the default menubar is used (as
|
|
|
|
documented below).
|
|
|
|
|
|
|
|
The above 3 steps are applied all the way up the parent window
|
|
|
|
chain until one of the above are satisifed. If all else fails a
|
|
|
|
default menubar will be created, the default menubar on Qt/Mac is
|
|
|
|
an empty menubar, however you can create a different default
|
|
|
|
menubar by creating a parentless QMenuBar, the first one created
|
|
|
|
will thus be designated the default menubar, and will be used
|
|
|
|
whenever a default menubar is needed.
|
|
|
|
|
|
|
|
Thus, for Qt/Mac, we add the menus to a free standing menubar, so
|
|
|
|
that this menubar will be used also when one of LyX' dialogs has
|
|
|
|
focus. (JMarc)
|
|
|
|
*/
|
2006-12-20 14:28:06 +00:00
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
void Menus::macxMenuBarInit(GuiView * view)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-01-16 16:50:10 +00:00
|
|
|
// The Mac menubar initialisation must be done only once!
|
|
|
|
static bool done = false;
|
|
|
|
if (done)
|
|
|
|
return;
|
|
|
|
done = true;
|
|
|
|
|
2007-02-26 19:54:41 +00:00
|
|
|
/* Since Qt 4.2, the qt/mac menu code has special code for
|
|
|
|
specifying the role of a menu entry. However, it does not
|
|
|
|
work very well with our scheme of creating menus on demand,
|
|
|
|
and therefore we need to put these entries in a special
|
|
|
|
invisible menu. (JMarc)
|
2006-12-20 14:28:06 +00:00
|
|
|
*/
|
2007-02-26 19:54:41 +00:00
|
|
|
|
|
|
|
/* The entries of our special mac menu. If we add support for
|
|
|
|
* special entries in MenuBackend, we could imagine something
|
|
|
|
* like
|
|
|
|
* SpecialItem About " "About LyX" "dialog-show aboutlyx"
|
|
|
|
* and therefore avoid hardcoding. I am not sure it is worth
|
|
|
|
* the hassle, though. (JMarc)
|
|
|
|
*/
|
|
|
|
struct MacMenuEntry {
|
|
|
|
kb_action action;
|
|
|
|
char const * arg;
|
|
|
|
char const * label;
|
2007-05-28 22:27:45 +00:00
|
|
|
QAction::MenuRole role;
|
2007-02-26 19:54:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MacMenuEntry entries[] = {
|
2007-05-28 22:27:45 +00:00
|
|
|
{LFUN_DIALOG_SHOW, "aboutlyx", "About LyX",
|
2007-02-26 19:54:41 +00:00
|
|
|
QAction::AboutRole},
|
2007-05-28 22:27:45 +00:00
|
|
|
{LFUN_DIALOG_SHOW, "prefs", "Preferences",
|
2007-02-26 19:54:41 +00:00
|
|
|
QAction::PreferencesRole},
|
2007-05-28 22:27:45 +00:00
|
|
|
{LFUN_RECONFIGURE, "", "Reconfigure",
|
2007-02-27 15:30:29 +00:00
|
|
|
QAction::ApplicationSpecificRole},
|
2007-02-26 19:54:41 +00:00
|
|
|
{LFUN_LYX_QUIT, "", "Quit LyX", QAction::QuitRole}
|
|
|
|
};
|
|
|
|
const size_t num_entries = sizeof(entries) / sizeof(MacMenuEntry);
|
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
// the special menu for MenuBackend.
|
2007-01-03 11:59:10 +00:00
|
|
|
Menu special;
|
2007-02-26 19:54:41 +00:00
|
|
|
for (size_t i = 0 ; i < num_entries ; ++i) {
|
2007-05-28 22:27:45 +00:00
|
|
|
FuncRequest const func(entries[i].action,
|
2007-02-26 19:54:41 +00:00
|
|
|
from_utf8(entries[i].arg));
|
2007-05-28 22:27:45 +00:00
|
|
|
special.add(MenuItem(MenuItem::Command,
|
|
|
|
from_utf8(entries[i].label),
|
2007-02-26 19:54:41 +00:00
|
|
|
func));
|
|
|
|
}
|
2007-12-26 10:55:43 +00:00
|
|
|
specialMenu(special);
|
2007-02-26 19:54:41 +00:00
|
|
|
|
|
|
|
// add the entries to a QMenu that will eventually be empty
|
|
|
|
// and therefore invisible.
|
2007-12-26 10:55:43 +00:00
|
|
|
QMenu * qMenu = view->menuBar()->addMenu("special");
|
2007-02-26 19:54:41 +00:00
|
|
|
|
|
|
|
// we do not use 'special' because it is a temporary variable,
|
|
|
|
// whereas MenuBackend::specialMenu points to a persistent
|
|
|
|
// copy.
|
2007-12-26 10:55:43 +00:00
|
|
|
Menu::const_iterator cit = specialMenu().begin();
|
|
|
|
Menu::const_iterator end = specialMenu().end();
|
2007-02-26 19:54:41 +00:00
|
|
|
for (size_t i = 0 ; cit != end ; ++cit, ++i) {
|
2007-12-26 10:55:43 +00:00
|
|
|
Action * action = new Action(*view, QIcon(),
|
2007-10-02 07:51:25 +00:00
|
|
|
toqstr(cit->label()),
|
|
|
|
cit->func(), QString());
|
2007-02-26 19:54:41 +00:00
|
|
|
action->setMenuRole(entries[i].role);
|
|
|
|
qMenu->addAction(action);
|
|
|
|
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2007-12-26 10:55:43 +00:00
|
|
|
#include "Menus_moc.cpp"
|