2011-07-15 06:06:23 +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 menutool.cpp
|
|
|
|
/// @brief Dynamic menu toolbar generator.
|
|
|
|
/// @ingroup toolbar menu
|
|
|
|
|
2011-08-17 07:32:27 +02:00
|
|
|
#include "include/aegisub/toolbar.h"
|
|
|
|
|
2011-07-15 06:06:23 +02:00
|
|
|
#include "command/command.h"
|
2012-12-30 01:53:30 +01:00
|
|
|
#include "compat.h"
|
2011-08-17 07:32:27 +02:00
|
|
|
#include "include/aegisub/hotkey.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"
|
2013-12-27 04:23:59 +01:00
|
|
|
#include "retina_helper.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-12-07 17:06:03 +01:00
|
|
|
#include <libaegisub/hotkey.h>
|
|
|
|
#include <libaegisub/json.h>
|
|
|
|
#include <libaegisub/log.h>
|
|
|
|
#include <libaegisub/signal.h>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-03-21 21:41:33 +01:00
|
|
|
#include <boost/interprocess/streams/bufferstream.hpp>
|
2011-07-15 06:06:23 +02:00
|
|
|
#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-07-16 08:42:55 +02:00
|
|
|
#include <wx/frame.h>
|
2011-07-15 06:06:23 +02:00
|
|
|
#include <wx/toolbar.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-07-15 06:06:23 +02:00
|
|
|
namespace {
|
|
|
|
json::Object const& get_root() {
|
|
|
|
static json::Object root;
|
2014-03-21 21:41:33 +01:00
|
|
|
if (root.empty()) {
|
|
|
|
boost::interprocess::ibufferstream stream((const char *)default_toolbar, sizeof(default_toolbar));
|
2014-06-28 05:44:47 +02:00
|
|
|
root = std::move(static_cast<json::Object&>(agi::json_util::parse(stream)));
|
2014-03-21 21:41:33 +01:00
|
|
|
}
|
2011-07-15 06:06:23 +02:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2014-03-13 02:39:07 +01:00
|
|
|
class Toolbar final : public wxToolBar {
|
2011-07-15 06:06:23 +02:00
|
|
|
/// Window ID of first toolbar control
|
|
|
|
static const int TOOL_ID_BASE = 5000;
|
|
|
|
|
|
|
|
/// Toolbar name in config file
|
|
|
|
std::string name;
|
|
|
|
/// Project context
|
|
|
|
agi::Context *context;
|
|
|
|
/// Commands for each of the buttons
|
|
|
|
std::vector<cmd::Command *> commands;
|
2011-08-17 07:32:27 +02:00
|
|
|
/// Hotkey context
|
|
|
|
std::string ht_context;
|
2013-12-27 04:23:59 +01:00
|
|
|
|
|
|
|
RetinaHelper retina_helper;
|
|
|
|
|
2012-05-19 03:13:05 +02:00
|
|
|
/// Current icon size
|
|
|
|
int icon_size;
|
2011-07-15 06:06:23 +02:00
|
|
|
|
|
|
|
/// Listener for icon size change signal
|
|
|
|
agi::signal::Connection icon_size_slot;
|
|
|
|
|
2011-10-28 22:40:32 +02:00
|
|
|
/// Listener for hotkey change signal
|
|
|
|
agi::signal::Connection hotkeys_changed_slot;
|
|
|
|
|
2011-07-15 06:06:23 +02:00
|
|
|
/// Enable/disable the toolbar buttons
|
|
|
|
void OnIdle(wxIdleEvent &) {
|
|
|
|
for (size_t i = 0; i < commands.size(); ++i) {
|
2012-12-07 17:06:03 +01:00
|
|
|
if (commands[i]->Type() & cmd::COMMAND_VALIDATE)
|
2011-07-15 06:06:23 +02:00
|
|
|
EnableTool(TOOL_ID_BASE + i, commands[i]->Validate(context));
|
2012-12-07 17:06:03 +01:00
|
|
|
if (commands[i]->Type() & cmd::COMMAND_TOGGLE || commands[i]->Type() & cmd::COMMAND_RADIO)
|
2011-07-15 06:06:23 +02:00
|
|
|
ToggleTool(TOOL_ID_BASE + i, commands[i]->IsActive(context));
|
|
|
|
}
|
|
|
|
}
|
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-15 06:06:23 +02:00
|
|
|
/// Toolbar button click handler
|
|
|
|
void OnClick(wxCommandEvent &evt) {
|
|
|
|
(*commands[evt.GetId() - TOOL_ID_BASE])(context);
|
|
|
|
}
|
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-05-19 03:13:05 +02:00
|
|
|
/// Regenerate the toolbar when the icon size changes
|
|
|
|
void OnIconSizeChange(agi::OptionValue const& opt) {
|
|
|
|
icon_size = opt.GetInt();
|
|
|
|
RegenerateToolbar();
|
|
|
|
}
|
|
|
|
|
2011-10-28 22:40:32 +02:00
|
|
|
/// Clear the toolbar and recreate it
|
|
|
|
void RegenerateToolbar() {
|
2011-07-15 06:06:23 +02:00
|
|
|
Unbind(wxEVT_IDLE, &Toolbar::OnIdle, this);
|
|
|
|
ClearTools();
|
|
|
|
commands.clear();
|
|
|
|
Populate();
|
|
|
|
}
|
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-15 06:06:23 +02:00
|
|
|
/// Populate the toolbar with buttons
|
|
|
|
void Populate() {
|
|
|
|
json::Object const& root = get_root();
|
2013-09-16 21:10:00 +02:00
|
|
|
auto root_it = root.find(name);
|
2011-12-26 23:20:49 +01:00
|
|
|
if (root_it == root.end()) {
|
2011-07-15 06:06:23 +02:00
|
|
|
// Toolbar names are all hardcoded so this should never happen
|
2014-05-29 14:57:27 +02:00
|
|
|
throw agi::InternalError("Toolbar named " + name + " not found.");
|
2011-07-15 06:06:23 +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-12-26 23:20:49 +01:00
|
|
|
json::Array const& arr = root_it->second;
|
2011-10-17 23:59:35 +02:00
|
|
|
commands.reserve(arr.size());
|
2011-07-15 06:06:23 +02:00
|
|
|
bool needs_onidle = false;
|
2012-01-22 18:15:36 +01:00
|
|
|
bool last_was_sep = false;
|
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-11-04 04:53:03 +01:00
|
|
|
for (json::String const& command_name : arr) {
|
2011-10-17 23:59:59 +02:00
|
|
|
if (command_name.empty()) {
|
2012-01-22 18:15:36 +01:00
|
|
|
if (!last_was_sep)
|
|
|
|
AddSeparator();
|
|
|
|
last_was_sep = true;
|
|
|
|
continue;
|
2011-07-15 06:06:23 +02:00
|
|
|
}
|
2012-01-22 18:15:36 +01:00
|
|
|
|
|
|
|
cmd::Command *command;
|
|
|
|
try {
|
|
|
|
command = cmd::get(command_name);
|
|
|
|
}
|
2014-01-04 01:56:18 +01:00
|
|
|
catch (cmd::CommandNotFound const&) {
|
2012-01-22 18:15:36 +01:00
|
|
|
LOG_W("toolbar/command/not_found") << "Command '" << command_name << "' not found; skipping";
|
|
|
|
continue;
|
2011-07-15 06:06:23 +02:00
|
|
|
}
|
2012-01-22 18:15:36 +01:00
|
|
|
|
|
|
|
last_was_sep = false;
|
|
|
|
|
|
|
|
int flags = command->Type();
|
|
|
|
wxItemKind kind =
|
|
|
|
flags & cmd::COMMAND_RADIO ? wxITEM_RADIO :
|
|
|
|
flags & cmd::COMMAND_TOGGLE ? wxITEM_CHECK :
|
|
|
|
wxITEM_NORMAL;
|
|
|
|
|
2014-05-11 16:31:22 +02:00
|
|
|
wxBitmap const& bitmap = command->Icon(icon_size, GetLayoutDirection());
|
2012-09-25 06:21:33 +02:00
|
|
|
AddTool(TOOL_ID_BASE + commands.size(), command->StrDisplay(context), bitmap, GetTooltip(command), kind);
|
2012-01-22 18:15:36 +01:00
|
|
|
|
|
|
|
commands.push_back(command);
|
|
|
|
needs_onidle = needs_onidle || flags != cmd::COMMAND_NORMAL;
|
2011-07-15 06:06:23 +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-07-15 06:06:23 +02:00
|
|
|
// Only bind the update function if there are actually any dynamic tools
|
|
|
|
if (needs_onidle) {
|
|
|
|
Bind(wxEVT_IDLE, &Toolbar::OnIdle, this);
|
|
|
|
}
|
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-15 06:06:23 +02:00
|
|
|
Realize();
|
2011-01-16 08:15:11 +01:00
|
|
|
}
|
2011-10-28 22:40:32 +02:00
|
|
|
|
|
|
|
wxString GetTooltip(cmd::Command *command) {
|
|
|
|
wxString ret = command->StrHelp();
|
|
|
|
|
|
|
|
std::vector<std::string> hotkeys = hotkey::get_hotkey_strs(ht_context, command->name());
|
2012-12-07 17:06:03 +01:00
|
|
|
if (!hotkeys.empty())
|
2012-12-30 01:53:30 +01:00
|
|
|
ret += to_wx(" (" + boost::join(hotkeys, "/") + ")");
|
2011-10-28 22:40:32 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-15 06:06:23 +02:00
|
|
|
public:
|
2013-11-21 18:13:36 +01:00
|
|
|
Toolbar(wxWindow *parent, std::string name, agi::Context *c, std::string ht_context, bool vertical)
|
|
|
|
: wxToolBar(parent, -1, wxDefaultPosition, wxDefaultSize, wxTB_NODIVIDER | wxTB_FLAT | (vertical ? wxTB_VERTICAL : wxTB_HORIZONTAL))
|
|
|
|
, name(std::move(name))
|
2011-07-15 06:06:23 +02:00
|
|
|
, context(c)
|
2013-11-21 18:13:36 +01:00
|
|
|
, ht_context(std::move(ht_context))
|
2013-12-27 04:23:59 +01:00
|
|
|
, retina_helper(parent)
|
2012-05-19 03:13:05 +02:00
|
|
|
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
|
|
|
|
, icon_size_slot(OPT_SUB("App/Toolbar Icon Size", &Toolbar::OnIconSizeChange, this))
|
2011-10-28 22:40:32 +02:00
|
|
|
, hotkeys_changed_slot(hotkey::inst->AddHotkeyChangeListener(&Toolbar::RegenerateToolbar, this))
|
2011-07-15 06:06:23 +02:00
|
|
|
{
|
|
|
|
Populate();
|
2013-12-12 03:25:13 +01:00
|
|
|
Bind(wxEVT_TOOL, &Toolbar::OnClick, this);
|
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-05-19 03:13:00 +02:00
|
|
|
|
2013-11-21 18:13:36 +01:00
|
|
|
Toolbar(wxFrame *parent, std::string name, agi::Context *c, std::string ht_context)
|
2012-05-19 03:13:00 +02:00
|
|
|
: wxToolBar(parent, -1, wxDefaultPosition, wxDefaultSize, wxTB_FLAT | wxTB_HORIZONTAL)
|
2013-11-21 18:13:36 +01:00
|
|
|
, name(std::move(name))
|
2012-05-19 03:13:00 +02:00
|
|
|
, context(c)
|
2013-11-21 18:13:36 +01:00
|
|
|
, ht_context(std::move(ht_context))
|
2013-12-27 04:23:59 +01:00
|
|
|
, retina_helper(parent)
|
2012-05-19 03:13:05 +02:00
|
|
|
#ifndef __WXMAC__
|
|
|
|
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
|
|
|
|
, icon_size_slot(OPT_SUB("App/Toolbar Icon Size", &Toolbar::OnIconSizeChange, this))
|
|
|
|
#else
|
2013-12-27 04:23:59 +01:00
|
|
|
, icon_size(32 * retina_helper.GetScaleFactor())
|
|
|
|
, icon_size_slot(retina_helper.AddScaleFactorListener([=](double scale) {
|
|
|
|
icon_size = 32 * retina_helper.GetScaleFactor();
|
|
|
|
RegenerateToolbar();
|
|
|
|
}))
|
2012-05-19 03:13:05 +02:00
|
|
|
#endif
|
2012-05-19 03:13:00 +02:00
|
|
|
, hotkeys_changed_slot(hotkey::inst->AddHotkeyChangeListener(&Toolbar::RegenerateToolbar, this))
|
|
|
|
{
|
|
|
|
parent->SetToolBar(this);
|
|
|
|
Populate();
|
2013-12-12 03:25:13 +01:00
|
|
|
Bind(wxEVT_TOOL, &Toolbar::OnClick, this);
|
2012-05-19 03:13:00 +02:00
|
|
|
}
|
2011-07-15 06:06:23 +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-07-15 06:06:23 +02:00
|
|
|
namespace toolbar {
|
2011-08-17 07:32:27 +02:00
|
|
|
void AttachToolbar(wxFrame *frame, std::string const& name, agi::Context *c, std::string const& hotkey) {
|
2012-05-19 03:13:00 +02:00
|
|
|
new Toolbar(frame, name, c, hotkey);
|
2011-07-15 06:06:23 +02:00
|
|
|
}
|
2011-08-27 08:30:42 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
wxToolBar *GetToolbar(wxWindow *parent, std::string const& name, agi::Context *c, std::string const& hotkey, bool vertical) {
|
|
|
|
return new Toolbar(parent, name, c, hotkey, vertical);
|
2011-08-27 08:30:42 +02:00
|
|
|
}
|
2011-07-15 06:06:23 +02:00
|
|
|
}
|