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.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file menutool.cpp
|
|
|
|
/// @brief Dynamic menu toolbar generator.
|
|
|
|
/// @ingroup toolbar menu
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
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"
|
|
|
|
#include "include/aegisub/context.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"
|
2011-07-15 06:06:23 +02:00
|
|
|
#include "main.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
|
|
|
#ifndef AGI_PRE
|
|
|
|
#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>
|
|
|
|
#endif
|
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
|
|
|
#include <libaegisub/json.h>
|
2011-07-16 01:43:52 +02:00
|
|
|
#include <libaegisub/log.h>
|
2011-07-15 06:06:23 +02:00
|
|
|
#include <libaegisub/signal.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;
|
|
|
|
if (root.Empty()) {
|
|
|
|
root = agi::json_util::parse(new std::istringstream(GET_DEFAULT_CONFIG(default_toolbar)));
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Toolbar : public wxToolBar {
|
|
|
|
/// 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;
|
2011-07-15 06:06:23 +02:00
|
|
|
|
|
|
|
/// Listener for icon size change signal
|
|
|
|
agi::signal::Connection icon_size_slot;
|
|
|
|
|
|
|
|
/// Enable/disable the toolbar buttons
|
|
|
|
void OnIdle(wxIdleEvent &) {
|
|
|
|
for (size_t i = 0; i < commands.size(); ++i) {
|
|
|
|
if (commands[i]->Type() & cmd::COMMAND_VALIDATE) {
|
|
|
|
EnableTool(TOOL_ID_BASE + i, commands[i]->Validate(context));
|
|
|
|
}
|
|
|
|
if (commands[i]->Type() & cmd::COMMAND_TOGGLE) {
|
|
|
|
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
|
|
|
|
2011-07-15 06:06:23 +02:00
|
|
|
/// Clear the toolbar and recreate it with the new icon size
|
|
|
|
void OnIconSizeChanged() {
|
|
|
|
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();
|
|
|
|
json::Object::const_iterator it = root.Find(name);
|
|
|
|
if (it == root.End()) {
|
|
|
|
// Toolbar names are all hardcoded so this should never happen
|
|
|
|
throw agi::InternalError("Toolbar named " + name + " not found.", 0);
|
|
|
|
}
|
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
|
|
|
int icon_size = OPT_GET("App/Toolbar Icon Size")->GetInt();
|
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
|
|
|
json::Array arr = it->element;
|
|
|
|
commands.reserve(arr.Size());
|
|
|
|
bool needs_onidle = 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
|
|
|
|
2011-07-15 06:06:23 +02:00
|
|
|
for (json::Array::const_iterator it = arr.Begin(); it != arr.End(); ++it) {
|
|
|
|
json::String const& command_name = *it;
|
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
|
|
|
if (command_name.Value().empty()) {
|
|
|
|
AddSeparator();
|
|
|
|
}
|
|
|
|
else {
|
2011-07-16 01:43:52 +02:00
|
|
|
cmd::Command *command;
|
|
|
|
try {
|
|
|
|
command = cmd::get(command_name.Value());
|
|
|
|
}
|
|
|
|
catch (CommandNotFound const&) {
|
|
|
|
LOG_W("toolbar/command/not_found") << "Command '" << command_name.Value() << "' not found; skipping";
|
|
|
|
continue;
|
|
|
|
}
|
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 19:24:05 +02:00
|
|
|
wxBitmap const& bitmap = command->Icon(icon_size);
|
2011-07-15 06:06:23 +02:00
|
|
|
// this hack is needed because ???
|
2011-07-15 19:24:05 +02:00
|
|
|
wxBitmap icon = bitmap.GetSubBitmap(wxRect(0, 0, bitmap.GetWidth(), bitmap.GetHeight()));
|
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
|
|
|
int flags = command->Type();
|
|
|
|
wxItemKind kind =
|
|
|
|
flags & cmd::COMMAND_RADIO ? wxITEM_RADIO :
|
|
|
|
flags & cmd::COMMAND_TOGGLE ? wxITEM_CHECK :
|
|
|
|
wxITEM_NORMAL;
|
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-17 07:32:27 +02:00
|
|
|
wxString tooltip = command->StrHelp();
|
|
|
|
|
|
|
|
std::vector<std::string> hotkeys = hotkey::get_hotkey_strs(ht_context, command->name());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < hotkeys.size(); ++i) {
|
|
|
|
if (i == 0)
|
|
|
|
tooltip += " (";
|
|
|
|
else
|
|
|
|
tooltip += "/";
|
|
|
|
tooltip += hotkeys[i];
|
|
|
|
}
|
|
|
|
if (hotkeys.size()) tooltip += ")";
|
|
|
|
|
2011-08-27 08:29:36 +02:00
|
|
|
AddTool(TOOL_ID_BASE + commands.size(), command->StrDisplay(context), icon, tooltip, kind);
|
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
|
|
|
commands.push_back(command);
|
|
|
|
needs_onidle = needs_onidle || flags != cmd::COMMAND_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
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-07-15 06:06:23 +02:00
|
|
|
public:
|
2011-08-17 07:32:27 +02:00
|
|
|
Toolbar(wxWindow *parent, std::string const& name, agi::Context *c, std::string const& ht_context)
|
2011-07-15 06:06:23 +02:00
|
|
|
: wxToolBar(parent, -1, wxDefaultPosition, wxDefaultSize, wxTB_FLAT | wxTB_HORIZONTAL)
|
|
|
|
, name(name)
|
|
|
|
, context(c)
|
2011-08-17 07:32:27 +02:00
|
|
|
, ht_context(ht_context)
|
2011-07-15 06:06:23 +02:00
|
|
|
, icon_size_slot(OPT_SUB("App/Toolbar Icon Size", &Toolbar::OnIconSizeChanged, this))
|
2011-08-17 07:32:27 +02:00
|
|
|
/// @todo bind to hotkey changed event when such a thing exists
|
2011-07-15 06:06:23 +02:00
|
|
|
{
|
|
|
|
Populate();
|
|
|
|
Bind(wxEVT_COMMAND_TOOL_CLICKED, &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
|
|
|
}
|
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) {
|
|
|
|
frame->SetToolBar(new Toolbar(frame, name, c, hotkey));
|
2011-07-15 06:06:23 +02:00
|
|
|
}
|
|
|
|
}
|