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.
|
|
|
|
|
|
|
|
/// @file menu.cpp
|
|
|
|
/// @brief Dynamic menu and toolbar generator.
|
|
|
|
/// @ingroup menu
|
|
|
|
|
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"
|
2014-05-29 17:28:37 +02:00
|
|
|
#include "format.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"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.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
|
|
|
|
2014-05-23 00:40:16 +02:00
|
|
|
#include <libaegisub/cajun/reader.h>
|
2011-10-28 22:40:32 +02:00
|
|
|
#include <libaegisub/hotkey.h>
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <libaegisub/json.h>
|
|
|
|
#include <libaegisub/log.h>
|
2014-04-23 22:53:24 +02:00
|
|
|
#include <libaegisub/make_unique.h>
|
2014-05-05 22:40:25 +02:00
|
|
|
#include <libaegisub/path.h>
|
|
|
|
#include <libaegisub/split.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
|
|
|
|
2012-02-02 20:18:10 +01:00
|
|
|
#include <algorithm>
|
2014-05-29 17:28:37 +02:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2014-05-05 22:40:25 +02:00
|
|
|
#include <boost/range/algorithm_ext/push_back.hpp>
|
2015-01-25 00:35:42 +01:00
|
|
|
#include <boost/locale/collator.hpp>
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <vector>
|
2011-09-15 08:03:13 +02:00
|
|
|
#include <wx/frame.h>
|
|
|
|
#include <wx/menu.h>
|
2011-08-27 08:29:36 +02:00
|
|
|
#include <wx/menuitem.h>
|
|
|
|
|
2014-06-17 03:22:06 +02:00
|
|
|
#ifdef __WXMAC__
|
|
|
|
#include <wx/app.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
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
|
|
|
|
2014-03-13 02:39:07 +01:00
|
|
|
class MruMenu final : public wxMenu {
|
2011-08-27 08:29:36 +02:00
|
|
|
std::string type;
|
|
|
|
std::vector<wxMenuItem *> items;
|
2012-01-09 21:31:55 +01:00
|
|
|
std::vector<std::string> *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(), "_"));
|
2014-05-29 17:28:37 +02:00
|
|
|
cmds->push_back(agi::format("recent/%s/%d", boost::to_lower_copy(type), 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:
|
2013-11-21 18:13:36 +01:00
|
|
|
MruMenu(std::string type, std::vector<std::string> *cmds)
|
|
|
|
: type(std::move(type))
|
2011-08-27 08:29:36 +02:00
|
|
|
, 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() {
|
2014-07-05 06:21:35 +02:00
|
|
|
const auto mru = config::mru->Get(type.c_str());
|
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;
|
2012-11-04 04:53:03 +01:00
|
|
|
for (auto it = mru->begin(); it != mru->end(); ++it, ++i) {
|
2013-01-04 16:01:50 +01:00
|
|
|
wxString name = it->wstring();
|
|
|
|
if (!name.StartsWith("?"))
|
|
|
|
name = it->filename().wstring();
|
2014-05-29 17:28:37 +02:00
|
|
|
items[i]->SetItemLabel(fmt_wx("%s%d %s",
|
2011-08-27 08:29:36 +02:00
|
|
|
i <= 9 ? "&" : "", i + 1,
|
2013-01-04 16:01:50 +01:00
|
|
|
name));
|
2011-08-27 08:29:36 +02:00
|
|
|
items[i]->Enable(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @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
|
2012-11-13 17:51:01 +01:00
|
|
|
/// is super buggy (GetMenu() often returns nullptr and it outright doesn't trigger
|
2011-08-27 08:29:36 +02:00
|
|
|
/// 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
|
2014-05-25 17:00:36 +02:00
|
|
|
std::vector<std::pair<std::string, wxMenuItem*>> dynamic_items;
|
2011-10-28 22:40:32 +02:00
|
|
|
/// Menu items which need to be updated only when hotkeys change
|
2014-05-25 17:00:36 +02:00
|
|
|
std::vector<std::pair<std::string, wxMenuItem*>> static_items;
|
2011-08-27 08:29:36 +02:00
|
|
|
/// window id -> command map
|
2012-01-09 21:31:55 +01:00
|
|
|
std::vector<std::string> items;
|
2011-08-27 08:29:36 +02:00
|
|
|
/// MRU menus which need to be updated on menu open
|
2014-05-25 17:00:36 +02:00
|
|
|
std::vector<MruMenu*> mru;
|
2011-08-27 08:29:36 +02:00
|
|
|
|
|
|
|
/// Project context
|
|
|
|
agi::Context *context;
|
|
|
|
|
2011-10-28 22:40:32 +02:00
|
|
|
/// Connection for hotkey change signal
|
|
|
|
agi::signal::Connection hotkeys_changed;
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Update a single dynamic menu item
|
2012-01-09 21:31:55 +01:00
|
|
|
void UpdateItem(std::pair<std::string, wxMenuItem*> const& item) {
|
|
|
|
cmd::Command *c = cmd::get(item.first);
|
|
|
|
int flags = c->Type();
|
2012-01-31 05:04:02 +01:00
|
|
|
if (flags & cmd::COMMAND_VALIDATE) {
|
2012-01-25 01:21:37 +01:00
|
|
|
item.second->Enable(c->Validate(context));
|
2012-01-31 05:04:02 +01:00
|
|
|
flags = c->Type();
|
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
if (flags & cmd::COMMAND_DYNAMIC_NAME)
|
2011-10-28 22:40:32 +02:00
|
|
|
UpdateItemName(item);
|
2012-01-20 22:33:30 +01:00
|
|
|
if (flags & cmd::COMMAND_DYNAMIC_HELP)
|
|
|
|
item.second->SetHelp(c->StrHelp());
|
2012-01-08 02:04:55 +01:00
|
|
|
if (flags & cmd::COMMAND_RADIO || flags & cmd::COMMAND_TOGGLE) {
|
2012-01-09 21:31:55 +01:00
|
|
|
bool check = c->IsActive(context);
|
2012-01-08 02:04:55 +01:00
|
|
|
// Don't call Check(false) on radio items as this causes wxGtk to
|
|
|
|
// send a menu clicked event, and it should be a no-op anyway
|
|
|
|
if (check || flags & cmd::COMMAND_TOGGLE)
|
|
|
|
item.second->Check(check);
|
|
|
|
}
|
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
|
|
|
|
2012-01-09 21:31:55 +01:00
|
|
|
void UpdateItemName(std::pair<std::string, wxMenuItem*> const& item) {
|
|
|
|
cmd::Command *c = cmd::get(item.first);
|
2012-01-08 02:34:18 +01:00
|
|
|
wxString text;
|
2012-01-09 21:31:55 +01:00
|
|
|
if (c->Type() & cmd::COMMAND_DYNAMIC_NAME)
|
|
|
|
text = c->StrMenu(context);
|
2012-01-08 02:34:18 +01:00
|
|
|
else
|
|
|
|
text = item.second->GetItemLabel().BeforeFirst('\t');
|
2013-01-04 16:01:50 +01:00
|
|
|
item.second->SetItemLabel(text + to_wx("\t" + hotkey::get_hotkey_str_first("Default", c->name())));
|
2011-10-28 22:40:32 +02:00
|
|
|
}
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
public:
|
2011-10-28 22:40:32 +02:00
|
|
|
CommandManager(agi::Context *context)
|
|
|
|
: context(context)
|
|
|
|
, hotkeys_changed(hotkey::inst->AddHotkeyChangeListener(&CommandManager::OnHotkeysChanged, this))
|
|
|
|
{
|
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
|
2014-05-05 22:40:25 +02:00
|
|
|
int AddCommand(cmd::Command *co, wxMenu *parent, std::string const& text = "") {
|
|
|
|
return AddCommand(co, parent, text.empty() ? co->StrMenu(context) : _(to_wx(text)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// because wxString doesn't have a move constructor
|
|
|
|
int AddCommand(cmd::Command *co, wxMenu *parent, wxString const& menu_text) {
|
|
|
|
return AddCommand(co, parent, wxString(menu_text));
|
|
|
|
}
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
/// Append a command to a menu and register the needed handlers
|
2014-05-05 22:40:25 +02:00
|
|
|
int AddCommand(cmd::Command *co, wxMenu *parent, wxString&& menu_text) {
|
2011-08-27 08:29:36 +02:00
|
|
|
int flags = co->Type();
|
|
|
|
wxItemKind kind =
|
|
|
|
flags & cmd::COMMAND_RADIO ? wxITEM_RADIO :
|
|
|
|
flags & cmd::COMMAND_TOGGLE ? wxITEM_CHECK :
|
|
|
|
wxITEM_NORMAL;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
menu_text += to_wx("\t" + hotkey::get_hotkey_str_first("Default", co->name()));
|
2011-08-27 08:29:36 +02:00
|
|
|
|
|
|
|
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?
|
2011-11-03 01:29:23 +01:00
|
|
|
if (kind == wxITEM_NORMAL)
|
|
|
|
item->SetBitmap(co->Icon(16));
|
2011-08-27 08:29:36 +02:00
|
|
|
#endif
|
|
|
|
parent->Append(item);
|
2012-01-09 21:31:55 +01:00
|
|
|
items.push_back(co->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 (flags != cmd::COMMAND_NORMAL)
|
2014-05-25 16:41:21 +02:00
|
|
|
dynamic_items.emplace_back(co->name(), item);
|
2011-10-28 22:40:32 +02:00
|
|
|
else
|
2014-05-25 16:41:21 +02:00
|
|
|
static_items.emplace_back(co->name(), 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) {
|
2014-03-26 18:25:24 +01:00
|
|
|
auto pred = [=](std::pair<std::string, wxMenuItem*> const& o) {
|
|
|
|
return o.second == item;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto it = find_if(dynamic_items.begin(), dynamic_items.end(), pred);
|
2011-09-28 21:49:56 +02:00
|
|
|
if (it != dynamic_items.end())
|
|
|
|
dynamic_items.erase(it);
|
2014-03-26 18:25:24 +01:00
|
|
|
it = find_if(static_items.begin(), static_items.end(), pred);
|
2012-02-07 02:22:15 +01:00
|
|
|
if (it != static_items.end())
|
|
|
|
static_items.erase(it);
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
|
|
|
|
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));
|
2012-01-27 20:22:57 +01:00
|
|
|
parent->AppendSubMenu(mru.back(), _("&Recent"));
|
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
|
|
|
void OnMenuOpen(wxMenuEvent &) {
|
2014-03-26 18:25:24 +01:00
|
|
|
for (auto const& item : dynamic_items) UpdateItem(item);
|
|
|
|
for (auto item : mru) item->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())
|
2012-01-09 21:31:55 +01:00
|
|
|
cmd::call(items[id], context);
|
2012-05-16 06:57:24 +02:00
|
|
|
|
|
|
|
#ifdef __WXMAC__
|
|
|
|
else {
|
|
|
|
switch (evt.GetId()) {
|
|
|
|
case wxID_ABOUT:
|
|
|
|
cmd::call("app/about", context);
|
|
|
|
break;
|
|
|
|
case wxID_PREFERENCES:
|
|
|
|
cmd::call("app/options", context);
|
|
|
|
break;
|
|
|
|
case wxID_EXIT:
|
2012-05-19 18:22:21 +02:00
|
|
|
cmd::call("app/exit", context);
|
2012-05-16 06:57:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
2011-10-28 22:40:32 +02:00
|
|
|
|
|
|
|
/// Update the hotkeys for all menu items
|
|
|
|
void OnHotkeysChanged() {
|
2014-03-26 18:25:24 +01:00
|
|
|
for (auto const& item : dynamic_items) UpdateItemName(item);
|
|
|
|
for (auto const& item : static_items) UpdateItemName(item);
|
2011-10-28 22:40:32 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper for wxMenu to add a command manager
|
2014-03-13 02:39:07 +01:00
|
|
|
struct CommandMenu final : public wxMenu {
|
2011-08-27 08:29:36 +02:00
|
|
|
CommandManager cm;
|
|
|
|
CommandMenu(agi::Context *c) : cm(c) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper for wxMenuBar to add a command manager
|
2014-03-13 02:39:07 +01:00
|
|
|
struct CommandMenuBar final : public wxMenuBar {
|
2011-08-27 08:29:36 +02:00
|
|
|
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) {
|
2013-09-16 21:10:00 +02:00
|
|
|
auto it = obj.find(name);
|
2011-10-17 23:59:35 +02:00
|
|
|
if (it == obj.end()) return false;
|
2011-10-17 23:59:47 +02:00
|
|
|
*value = static_cast<json::String const&>(it->second);
|
2011-08-27 08:29:36 +02:00
|
|
|
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
|
|
|
/// Get the root object of the menu configuration
|
2014-03-26 18:25:24 +01:00
|
|
|
json::Object const& get_menus_root() {
|
|
|
|
static json::Object 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 {
|
2014-06-28 05:44:47 +02:00
|
|
|
root = std::move(static_cast<json::Object&>(agi::json_util::file(config::path->Decode("?user/menu.json"), GET_DEFAULT_CONFIG(default_menu))));
|
2011-08-27 08:29:36 +02:00
|
|
|
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
|
2014-03-26 18:25:24 +01:00
|
|
|
json::Array const& get_menu(std::string const& name) {
|
|
|
|
auto const& root = get_menus_root();
|
2011-08-27 08:29:36 +02:00
|
|
|
|
2013-09-16 21:10:00 +02:00
|
|
|
auto it = root.find(name);
|
2011-10-17 23:59:35 +02:00
|
|
|
if (it == root.end()) throw menu::UnknownMenu("Menu named " + name + " not found");
|
2011-10-17 23:59:47 +02:00
|
|
|
return it->second;
|
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
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
wxMenu *build_menu(std::string const& name, agi::Context *c, CommandManager *cm, wxMenu *menu = nullptr);
|
2011-08-27 08:29:36 +02:00
|
|
|
|
|
|
|
/// 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)) {
|
2012-12-23 00:18:38 +01:00
|
|
|
wxString tl_text = _(to_wx(text));
|
2012-06-27 03:44:38 +02:00
|
|
|
parent->AppendSubMenu(build_menu(submenu, c, cm), tl_text);
|
2011-08-27 08:29:36 +02:00
|
|
|
#ifdef __WXMAC__
|
|
|
|
if (special == "help")
|
2012-06-27 03:44:38 +02:00
|
|
|
wxApp::s_macHelpMenuTitleName = tl_text;
|
2011-08-27 08:29:36 +02:00
|
|
|
#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
|
2012-12-23 00:18:38 +01:00
|
|
|
parent->Append(-1, to_wx(e.GetMessage()))->Enable(false);
|
2011-08-27 08:29:36 +02:00
|
|
|
#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) {
|
|
|
|
if (!menu) menu = new wxMenu;
|
2014-03-26 18:25:24 +01:00
|
|
|
for (auto const& item : get_menu(name))
|
|
|
|
process_menu_item(menu, c, item, 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;
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:39:07 +01:00
|
|
|
class AutomationMenu final : public wxMenu {
|
2011-09-28 21:49:56 +02:00
|
|
|
agi::Context *c;
|
|
|
|
CommandManager *cm;
|
|
|
|
agi::signal::Connection global_slot;
|
|
|
|
agi::signal::Connection local_slot;
|
2014-05-05 22:40:25 +02:00
|
|
|
std::vector<wxMenuItem *> all_items;
|
2011-09-28 21:49:56 +02:00
|
|
|
|
2015-01-25 00:35:42 +01:00
|
|
|
struct WorkItem {
|
|
|
|
std::string displayname;
|
|
|
|
cmd::Command *command;
|
|
|
|
std::vector<WorkItem> subitems;
|
|
|
|
|
|
|
|
WorkItem(std::string const &displayname, cmd::Command *command = nullptr)
|
|
|
|
: displayname(displayname), command(command) { }
|
|
|
|
|
|
|
|
WorkItem *FindOrMakeSubitem(std::string const &name) {
|
|
|
|
auto sub = std::find_if(subitems.begin(), subitems.end(), [&](WorkItem const &item) { return item.displayname == name; });
|
|
|
|
if (sub != subitems.end()) return &*sub;
|
|
|
|
|
|
|
|
subitems.emplace_back(name);
|
|
|
|
return &subitems.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sort() {
|
|
|
|
if (command) return;
|
|
|
|
for (auto &sub : subitems)
|
|
|
|
sub.Sort();
|
|
|
|
auto comp = boost::locale::comparator<std::string::value_type>();
|
|
|
|
std::sort(subitems.begin(), subitems.end(), [&](WorkItem const &a, WorkItem const &b){
|
|
|
|
return comp(a.displayname, b.displayname);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateMenu(wxMenu *parent, AutomationMenu *am) {
|
|
|
|
for (auto item : subitems) {
|
|
|
|
if (item.command) {
|
|
|
|
am->cm->AddCommand(item.command, parent, item.displayname);
|
|
|
|
am->all_items.push_back(parent->GetMenuItems().back());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto submenu = new wxMenu;
|
|
|
|
parent->AppendSubMenu(submenu, to_wx(item.displayname));
|
|
|
|
item.GenerateMenu(submenu, am);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
void Regenerate() {
|
2014-05-05 22:40:25 +02:00
|
|
|
for (auto item : all_items)
|
|
|
|
cm->Remove(item);
|
|
|
|
|
2011-09-28 21:49:56 +02:00
|
|
|
wxMenuItemList &items = GetMenuItems();
|
2014-05-05 22:40:25 +02:00
|
|
|
// Remove everything but automation manager and the separator
|
|
|
|
for (size_t i = items.size() - 1; i >= 2; --i)
|
2011-09-28 21:49:56 +02:00
|
|
|
Delete(items[i]);
|
2014-05-05 22:40:25 +02:00
|
|
|
|
2014-06-06 17:24:33 +02:00
|
|
|
auto macros = config::global_scripts->GetMacros();
|
2014-05-05 22:40:25 +02:00
|
|
|
boost::push_back(macros, c->local_scripts->GetMacros());
|
|
|
|
if (macros.empty()) {
|
|
|
|
Append(-1, _("No Automation macros loaded"))->Enable(false);
|
|
|
|
return;
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
|
|
|
|
2015-01-25 00:35:42 +01:00
|
|
|
WorkItem top("");
|
2014-05-05 22:40:25 +02:00
|
|
|
for (auto macro : macros) {
|
|
|
|
const auto name = from_wx(macro->StrMenu(c));
|
2015-01-25 00:35:42 +01:00
|
|
|
WorkItem *parent = ⊤
|
2014-05-05 22:40:25 +02:00
|
|
|
for (auto section : agi::Split(name, wxS('/'))) {
|
2015-01-25 00:35:42 +01:00
|
|
|
std::string sectionname(section.begin(), section.end());
|
|
|
|
|
2014-05-05 22:40:25 +02:00
|
|
|
if (section.end() == name.end()) {
|
2015-01-25 00:35:42 +01:00
|
|
|
parent->subitems.emplace_back(sectionname, macro);
|
2014-05-05 22:40:25 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-01-25 00:35:42 +01:00
|
|
|
parent = parent->FindOrMakeSubitem(sectionname);
|
2014-05-05 22:40:25 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
2015-01-25 00:35:42 +01:00
|
|
|
top.Sort();
|
|
|
|
top.GenerateMenu(this, this);
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
AutomationMenu(agi::Context *c, CommandManager *cm)
|
|
|
|
: c(c)
|
|
|
|
, cm(cm)
|
2014-06-06 17:24:33 +02:00
|
|
|
, global_slot(config::global_scripts->AddScriptChangeListener(&AutomationMenu::Regenerate, this))
|
2011-09-28 21:49:56 +02:00
|
|
|
, local_slot(c->local_scripts->AddScriptChangeListener(&AutomationMenu::Regenerate, this))
|
|
|
|
{
|
2014-05-05 22:40:25 +02:00
|
|
|
cm->AddCommand(cmd::get("am/meta"), this);
|
2011-09-28 21:49:56 +02:00
|
|
|
AppendSeparator();
|
|
|
|
Regenerate();
|
|
|
|
}
|
|
|
|
};
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace menu {
|
|
|
|
void GetMenuBar(std::string const& name, wxFrame *window, agi::Context *c) {
|
2014-04-23 22:53:24 +02:00
|
|
|
auto menu = agi::make_unique<CommandMenuBar>(c);
|
2014-03-26 18:25:24 +01:00
|
|
|
for (auto const& item : get_menu(name)) {
|
2011-08-27 08:29:36 +02:00
|
|
|
std::string submenu, disp;
|
2012-11-04 04:53:03 +01:00
|
|
|
read_entry(item, "submenu", &submenu);
|
|
|
|
read_entry(item, "text", &disp);
|
2011-09-28 21:49:56 +02:00
|
|
|
if (!submenu.empty()) {
|
2012-11-04 04:53:03 +01:00
|
|
|
menu->Append(build_menu(submenu, c, &menu->cm), _(to_wx(disp)));
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
|
|
|
else {
|
2012-11-04 04:53:03 +01:00
|
|
|
read_entry(item, "special", &submenu);
|
2011-09-28 21:49:56 +02:00
|
|
|
if (submenu == "automation")
|
2012-11-04 04:53:03 +01:00
|
|
|
menu->Append(new AutomationMenu(c, &menu->cm), _(to_wx(disp)));
|
2011-09-28 21:49:56 +02:00
|
|
|
}
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
window->Bind(wxEVT_MENU_OPEN, &CommandManager::OnMenuOpen, &menu->cm);
|
2013-12-12 03:25:13 +01:00
|
|
|
window->Bind(wxEVT_MENU, &CommandManager::OnMenuClick, &menu->cm);
|
2012-04-07 03:06:34 +02:00
|
|
|
window->SetMenuBar(menu.get());
|
|
|
|
|
|
|
|
menu.release();
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
2013-06-10 15:58:13 +02:00
|
|
|
std::unique_ptr<wxMenu> GetMenu(std::string const& name, agi::Context *c) {
|
2014-04-23 22:53:24 +02:00
|
|
|
auto menu = agi::make_unique<CommandMenu>(c);
|
2014-03-26 18:25:24 +01:00
|
|
|
build_menu(name, c, &menu->cm, menu.get());
|
2011-08-27 08:29:36 +02:00
|
|
|
menu->Bind(wxEVT_MENU_OPEN, &CommandManager::OnMenuOpen, &menu->cm);
|
2013-12-12 03:25:13 +01:00
|
|
|
menu->Bind(wxEVT_MENU, &CommandManager::OnMenuClick, &menu->cm);
|
2014-03-26 18:25:24 +01:00
|
|
|
return std::unique_ptr<wxMenu>(menu.release());
|
2011-08-27 08:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenPopupMenu(wxMenu *menu, wxWindow *parent_window) {
|
|
|
|
wxMenuEvent evt;
|
|
|
|
evt.SetEventType(wxEVT_MENU_OPEN);
|
|
|
|
menu->ProcessEvent(evt);
|
|
|
|
parent_window->PopupMenu(menu);
|
|
|
|
}
|
|
|
|
}
|