2011-08-27 08:29:36 +02:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file menu.cpp
|
|
|
|
/// @brief Dynamic menu and toolbar generator.
|
|
|
|
/// @ingroup menu
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2011-01-18 00:53:46 +01:00
|
|
|
#include "include/aegisub/menu.h"
|
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
#include "include/aegisub/context.h"
|
2011-01-18 00:53:46 +01:00
|
|
|
#include "include/aegisub/hotkey.h"
|
2011-08-27 08:29:36 +02:00
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
#include "auto4_base.h"
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
#include "command/command.h"
|
2011-08-27 08:29:36 +02:00
|
|
|
#include "compat.h"
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
#include "libresrc/libresrc.h"
|
|
|
|
#include "main.h"
|
2011-08-27 08:29:36 +02:00
|
|
|
#include "standard_paths.h"
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <libaegisub/json.h>
|
|
|
|
#include <libaegisub/log.h>
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <deque>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <wx/app.h>
|
2011-09-15 08:03:13 +02:00
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/frame.h>
|
|
|
|
#include <wx/menu.h>
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <wx/menuitem.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Window ID of first menu item
|
|
|
|
static const int MENU_ID_BASE = 10000;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
using std::tr1::placeholders::_1;
|
|
|
|
using std::tr1::bind;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Get the menu text for a command along with hotkey
|
|
|
|
inline wxString get_menu_text(cmd::Command *command, agi::Context *c) {
|
|
|
|
return command->StrMenu(c) + "\t" + hotkey::get_hotkey_str_first("Default", command->name());
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
class MruMenu : public wxMenu {
|
|
|
|
std::string type;
|
|
|
|
std::vector<wxMenuItem *> items;
|
|
|
|
std::vector<cmd::Command*> *cmds;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
void Resize(size_t new_size) {
|
|
|
|
for (size_t i = GetMenuItemCount(); i > new_size; --i) {
|
|
|
|
Remove(FindItemByPosition(i - 1));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
|
|
|
|
for (size_t i = GetMenuItemCount(); i < new_size; ++i) {
|
|
|
|
if (i >= items.size()) {
|
|
|
|
items.push_back(new wxMenuItem(this, MENU_ID_BASE + cmds->size(), "_"));
|
|
|
|
cmds->push_back(cmd::get(STD_STR(wxString::Format("recent/%s/%d", lagi_wxString(type).Lower(), i))));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
Append(items[i]);
|
|
|
|
}
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
public:
|
|
|
|
MruMenu(std::string const& type, std::vector<cmd::Command*> *cmds)
|
|
|
|
: type(type)
|
|
|
|
, cmds(cmds)
|
|
|
|
{
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
~MruMenu() {
|
|
|
|
// Append all items to ensure that they're all cleaned up
|
|
|
|
Resize(items.size());
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
void Update() {
|
|
|
|
const agi::MRUManager::MRUListMap *mru = config::mru->Get(type);
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
Resize(mru->size());
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (mru->empty()) {
|
|
|
|
Resize(1);
|
|
|
|
items[0]->Enable(false);
|
|
|
|
items[0]->SetItemLabel(_("Empty"));
|
|
|
|
return;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
int i = 0;
|
|
|
|
for (agi::MRUManager::MRUListMap::const_iterator it = mru->begin();
|
|
|
|
it != mru->end();
|
|
|
|
++it, ++i)
|
|
|
|
{
|
|
|
|
items[i]->SetItemLabel(wxString::Format("%s%d %s",
|
|
|
|
i <= 9 ? "&" : "", i + 1,
|
|
|
|
wxFileName(lagi_wxString(*it)).GetFullName()));
|
|
|
|
items[i]->Enable(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
struct menu_item_cmp {
|
|
|
|
wxMenuItem *item;
|
|
|
|
menu_item_cmp(wxMenuItem *item) : item(item) { }
|
|
|
|
bool operator()(std::pair<cmd::Command*, wxMenuItem*> o) const {
|
|
|
|
return o.second == item;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// @class CommandManager
|
|
|
|
/// @brief Event dispatcher to update menus on open and handle click events
|
|
|
|
///
|
|
|
|
/// Some of what the class does could be dumped off on wx, but wxEVT_MENU_OPEN
|
|
|
|
/// is super buggy (GetMenu() often returns NULL and it outright doesn't trigger
|
|
|
|
/// on submenus in many cases, and registering large numbers of wxEVT_UPDATE_UI
|
|
|
|
/// handlers makes everything involves events unusably slow.
|
|
|
|
class CommandManager {
|
|
|
|
/// Menu items which need to do something on menu open
|
|
|
|
std::deque<std::pair<cmd::Command*, wxMenuItem*> > dynamic_items;
|
|
|
|
/// window id -> command map
|
|
|
|
std::vector<cmd::Command*> items;
|
|
|
|
/// MRU menus which need to be updated on menu open
|
|
|
|
std::deque<MruMenu*> mru;
|
|
|
|
|
|
|
|
/// Project context
|
|
|
|
agi::Context *context;
|
|
|
|
|
|
|
|
/// Update a single dynamic menu item
|
|
|
|
void UpdateItem(std::pair<cmd::Command*, wxMenuItem*> const& item) {
|
|
|
|
int flags = item.first->Type();
|
|
|
|
if (flags & cmd::COMMAND_DYNAMIC_NAME)
|
|
|
|
item.second->SetItemLabel(get_menu_text(item.first, context));
|
|
|
|
if (flags & cmd::COMMAND_VALIDATE)
|
|
|
|
item.second->Enable(item.first->Validate(context));
|
|
|
|
if (flags & cmd::COMMAND_RADIO || flags & cmd::COMMAND_TOGGLE)
|
|
|
|
item.second->Check(item.first->IsActive(context));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
public:
|
|
|
|
CommandManager(agi::Context *context) : context(context) { }
|
|
|
|
|
|
|
|
/// Append a command to a menu and register the needed handlers
|
|
|
|
int AddCommand(cmd::Command *co, wxMenu *parent, std::string const& text) {
|
|
|
|
int flags = co->Type();
|
|
|
|
wxItemKind kind =
|
|
|
|
flags & cmd::COMMAND_RADIO ? wxITEM_RADIO :
|
|
|
|
flags & cmd::COMMAND_TOGGLE ? wxITEM_CHECK :
|
|
|
|
wxITEM_NORMAL;
|
|
|
|
|
|
|
|
wxString menu_text = text.empty() ?
|
|
|
|
get_menu_text(co, context) :
|
|
|
|
lagi_wxString(text) + "\t" + hotkey::get_hotkey_str_first("Default", co->name());
|
|
|
|
|
|
|
|
wxMenuItem *item = new wxMenuItem(parent, MENU_ID_BASE + items.size(), menu_text, co->StrHelp(), kind);
|
|
|
|
#ifndef __WXMAC__
|
|
|
|
/// @todo Maybe make this a configuration option instead?
|
|
|
|
item->SetBitmap(co->Icon(16));
|
|
|
|
#endif
|
|
|
|
parent->Append(item);
|
|
|
|
items.push_back(co);
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (flags != cmd::COMMAND_NORMAL)
|
|
|
|
dynamic_items.push_back(std::make_pair(co, item));
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
return item->GetId();
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
/// Unregister a dynamic menu item
|
|
|
|
void Remove(wxMenuItem *item) {
|
|
|
|
std::deque<std::pair<cmd::Command*, wxMenuItem*> >::iterator it =
|
|
|
|
find_if(dynamic_items.begin(), dynamic_items.end(), menu_item_cmp(item));
|
|
|
|
if (it != dynamic_items.end())
|
|
|
|
dynamic_items.erase(it);
|
|
|
|
}
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Create a MRU menu and register the needed handlers
|
|
|
|
/// @param name MRU type
|
|
|
|
/// @param parent Menu to append the new MRU menu to
|
|
|
|
void AddRecent(std::string const& name, wxMenu *parent) {
|
|
|
|
mru.push_back(new MruMenu(name, &items));
|
|
|
|
parent->AppendSubMenu(mru.back(), _("Recent"));
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
void OnMenuOpen(wxMenuEvent &) {
|
|
|
|
for_each(dynamic_items.begin(), dynamic_items.end(), bind(&CommandManager::UpdateItem, this, _1));
|
|
|
|
for_each(mru.begin(), mru.end(), std::mem_fun(&MruMenu::Update));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
void OnMenuClick(wxCommandEvent &evt) {
|
|
|
|
// This also gets clicks on unrelated things such as the toolbar, so
|
|
|
|
// the window ID ranges really need to be unique
|
|
|
|
size_t id = static_cast<size_t>(evt.GetId() - MENU_ID_BASE);
|
|
|
|
if (id < items.size())
|
|
|
|
(*items[id])(context);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper for wxMenu to add a command manager
|
|
|
|
struct CommandMenu : public wxMenu {
|
|
|
|
CommandManager cm;
|
|
|
|
CommandMenu(agi::Context *c) : cm(c) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper for wxMenuBar to add a command manager
|
|
|
|
struct CommandMenuBar : public wxMenuBar {
|
|
|
|
CommandManager cm;
|
|
|
|
CommandMenuBar(agi::Context *c) : cm(c) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Read a string from a json object
|
|
|
|
/// @param obj Object to read from
|
|
|
|
/// @param name Index to read from
|
|
|
|
/// @param[out] value Output value to write to
|
|
|
|
/// @return Was the requested index found
|
|
|
|
bool read_entry(json::Object const& obj, const char *name, std::string *value) {
|
2011-10-17 23:59:35 +02:00
|
|
|
json::Object::const_iterator it = obj.find(name);
|
|
|
|
if (it == obj.end()) return false;
|
2011-08-27 08:29:36 +02:00
|
|
|
*value = static_cast<json::String const&>(it->element);
|
|
|
|
return true;
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
typedef json::Array menu_items;
|
|
|
|
typedef json::Object menu_map;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Get the root object of the menu configuration
|
|
|
|
menu_map const& get_menus_root() {
|
|
|
|
static menu_map root;
|
2011-10-17 23:59:35 +02:00
|
|
|
if (!root.empty()) return root;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
try {
|
2011-08-27 08:29:36 +02:00
|
|
|
root = agi::json_util::file(StandardPaths::DecodePath("?user/menu.json").utf8_str().data(), GET_DEFAULT_CONFIG(default_menu));
|
|
|
|
return root;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
catch (json::Reader::ParseException const& e) {
|
|
|
|
LOG_E("menu/parse") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1;
|
|
|
|
throw;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
catch (std::exception const& e) {
|
|
|
|
LOG_E("menu/parse") << e.what();
|
|
|
|
throw;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Get the menu with the specified name
|
|
|
|
/// @param name Name of menu to get
|
|
|
|
/// @return Array of menu items
|
|
|
|
menu_items const& get_menu(std::string const& name) {
|
|
|
|
menu_map const& root = get_menus_root();
|
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
menu_map::const_iterator it = root.find(name);
|
|
|
|
if (it == root.end()) throw menu::UnknownMenu("Menu named " + name + " not found");
|
2011-08-27 08:29:36 +02:00
|
|
|
return it->element;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
wxMenu *build_menu(std::string const& name, agi::Context *c, CommandManager *cm, wxMenu *menu = 0);
|
|
|
|
|
|
|
|
/// Recursively process a single entry in the menu json
|
|
|
|
/// @param parent Menu to add the item(s) from this entry to
|
|
|
|
/// @param c Project context to bind the menu to
|
|
|
|
/// @param ele json object to process
|
|
|
|
/// @param cm Command manager for this menu
|
|
|
|
void process_menu_item(wxMenu *parent, agi::Context *c, json::Object const& ele, CommandManager *cm) {
|
2011-10-17 23:59:35 +02:00
|
|
|
if (ele.empty()) {
|
2011-08-27 08:29:36 +02:00
|
|
|
parent->AppendSeparator();
|
|
|
|
return;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
std::string submenu, recent, command, text, special;
|
|
|
|
read_entry(ele, "special", &special);
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (read_entry(ele, "submenu", &submenu) && read_entry(ele, "text", &text)) {
|
|
|
|
parent->AppendSubMenu(build_menu(submenu, c, cm), lagi_wxString(text));
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
if (special == "help")
|
|
|
|
wxApp::s_macHelpMenuTitleName = lagi_wxString(text);
|
|
|
|
#endif
|
|
|
|
return;
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (read_entry(ele, "recent", &recent)) {
|
|
|
|
cm->AddRecent(recent, parent);
|
|
|
|
return;
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (!read_entry(ele, "command", &command))
|
|
|
|
return;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
read_entry(ele, "text", &text);
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
try {
|
|
|
|
int id = cm->AddCommand(cmd::get(command), parent, text);
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
if (!special.empty()) {
|
|
|
|
if (special == "about")
|
|
|
|
wxApp::s_macAboutMenuItemId = id;
|
|
|
|
else if (special == "exit")
|
|
|
|
wxApp::s_macExitMenuItemId = id;
|
|
|
|
else if (special == "options")
|
|
|
|
wxApp::s_macPreferencesMenuItemId = id;
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
}
|
2011-09-15 08:03:13 +02:00
|
|
|
#else
|
|
|
|
(void)id;
|
2011-08-27 08:29:36 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
catch (agi::Exception const& e) {
|
|
|
|
#ifdef _DEBUG
|
|
|
|
parent->Append(-1, lagi_wxString(e.GetMessage()))->Enable(false);
|
|
|
|
#endif
|
|
|
|
LOG_W("menu/command/not_found") << "Skipping command " << command << ": " << e.GetMessage();
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Build the menu with the given name
|
|
|
|
/// @param name Name of the menu
|
|
|
|
/// @param c Project context to bind the menu to
|
|
|
|
wxMenu *build_menu(std::string const& name, agi::Context *c, CommandManager *cm, wxMenu *menu) {
|
|
|
|
menu_items const& items = get_menu(name);
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
if (!menu) menu = new wxMenu;
|
2011-10-17 23:59:35 +02:00
|
|
|
for_each(items.begin(), items.end(), bind(process_menu_item, menu, c, _1, cm));
|
Merge the dynamic menu, hotkey and toolbar branch to trunk. This doesn't include Windows support as vs2008 was being a major pain. This involves revisions r4921:4950, r4961:5002, r5005:5006, r5008:5056, r5062:5065, r5072, r5081:5082, r5087, r5096:5110, r5124:5125. Updates #1258.
Originally committed to SVN as r5126.
2011-01-05 14:00:46 +01:00
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
struct comp_str_menu {
|
|
|
|
agi::Context *c;
|
|
|
|
comp_str_menu(agi::Context *c) : c(c) { }
|
|
|
|
bool operator()(const cmd::Command *lft, const cmd::Command *rgt) const {
|
|
|
|
return lft->StrMenu(c) < rgt->StrMenu(c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class AutomationMenu : public wxMenu {
|
|
|
|
agi::Context *c;
|
|
|
|
CommandManager *cm;
|
|
|
|
agi::signal::Connection global_slot;
|
|
|
|
agi::signal::Connection local_slot;
|
|
|
|
|
|
|
|
void Regenerate() {
|
|
|
|
wxMenuItemList &items = GetMenuItems();
|
|
|
|
for (size_t i = items.size() - 1; i >= 2; --i) {
|
|
|
|
cm->Remove(items[i]);
|
|
|
|
Delete(items[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<cmd::Command*> macros = wxGetApp().global_scripts->GetMacros();
|
|
|
|
std::vector<cmd::Command*> local_macros = c->local_scripts->GetMacros();
|
|
|
|
copy(local_macros.begin(), local_macros.end(), back_inserter(macros));
|
|
|
|
sort(macros.begin(), macros.end(), comp_str_menu(c));
|
|
|
|
|
|
|
|
if (macros.empty())
|
|
|
|
Append(-1, _("No Automation macros loaded"))->Enable(false);
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < macros.size(); ++i)
|
|
|
|
cm->AddCommand(macros[i], this, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
AutomationMenu(agi::Context *c, CommandManager *cm)
|
|
|
|
: c(c)
|
|
|
|
, cm(cm)
|
|
|
|
, global_slot(wxGetApp().global_scripts->AddScriptChangeListener(&AutomationMenu::Regenerate, this))
|
|
|
|
, local_slot(c->local_scripts->AddScriptChangeListener(&AutomationMenu::Regenerate, this))
|
|
|
|
{
|
|
|
|
cm->AddCommand(cmd::get("am/manager"), this, "");
|
|
|
|
AppendSeparator();
|
|
|
|
Regenerate();
|
|
|
|
}
|
|
|
|
};
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace menu {
|
|
|
|
void GetMenuBar(std::string const& name, wxFrame *window, agi::Context *c) {
|
|
|
|
menu_items const& items = get_menu(name);
|
|
|
|
|
|
|
|
CommandMenuBar *menu = new CommandMenuBar(c);
|
2011-10-17 23:59:35 +02:00
|
|
|
for (menu_items::const_iterator it = items.begin(); it != items.end(); ++it) {
|
2011-08-27 08:29:36 +02:00
|
|
|
std::string submenu, disp;
|
|
|
|
read_entry(*it, "submenu", &submenu);
|
|
|
|
read_entry(*it, "text", &disp);
|
2011-09-28 21:49:56 +02:00
|
|
|
if (!submenu.empty()) {
|
|
|
|
menu->Append(build_menu(submenu, c, &menu->cm), lagi_wxString(disp));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
read_entry(*it, "special", &submenu);
|
|
|
|
if (submenu == "automation")
|
|
|
|
menu->Append(new AutomationMenu(c, &menu->cm), lagi_wxString(disp));
|
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
window->SetMenuBar(menu);
|
|
|
|
window->Bind(wxEVT_MENU_OPEN, &CommandManager::OnMenuOpen, &menu->cm);
|
|
|
|
window->Bind(wxEVT_COMMAND_MENU_SELECTED, &CommandManager::OnMenuClick, &menu->cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxMenu *GetMenu(std::string const& name, agi::Context *c) {
|
|
|
|
CommandMenu *menu = new CommandMenu(c);
|
|
|
|
build_menu(name, c, &menu->cm, menu);
|
|
|
|
menu->Bind(wxEVT_MENU_OPEN, &CommandManager::OnMenuOpen, &menu->cm);
|
|
|
|
menu->Bind(wxEVT_COMMAND_MENU_SELECTED, &CommandManager::OnMenuClick, &menu->cm);
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenPopupMenu(wxMenu *menu, wxWindow *parent_window) {
|
|
|
|
wxMenuEvent evt;
|
|
|
|
evt.SetEventType(wxEVT_MENU_OPEN);
|
|
|
|
menu->ProcessEvent(evt);
|
|
|
|
parent_window->PopupMenu(menu);
|
|
|
|
}
|
|
|
|
}
|