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
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2011-01-08 13:10:40 +01:00
|
|
|
#include "libaegisub/hotkey.h"
|
|
|
|
|
|
|
|
#include "libaegisub/cajun/writer.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 "libaegisub/io.h"
|
|
|
|
#include "libaegisub/json.h"
|
|
|
|
#include "libaegisub/log.h"
|
2014-07-05 05:16:24 +02:00
|
|
|
#include "libaegisub/split.h"
|
2011-01-08 13:10:40 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <algorithm>
|
2014-07-05 05:16:24 +02:00
|
|
|
#include <boost/range/algorithm/equal_range.hpp>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <tuple>
|
2012-11-13 15:07:39 +01:00
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
namespace agi { namespace hotkey {
|
|
|
|
namespace {
|
|
|
|
struct combo_cmp {
|
|
|
|
bool operator()(const Combo *a, const Combo *b) {
|
|
|
|
return a->Str() < b->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
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
bool operator()(const Combo *a, std::string const& b) {
|
|
|
|
return a->Str() < b;
|
|
|
|
}
|
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-07-05 05:16:24 +02:00
|
|
|
bool operator()(std::string const& a, const Combo *b) {
|
|
|
|
return a < b->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
|
|
|
}
|
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
Hotkey::Hotkey(fs::path const& file, std::pair<const char *, size_t> default_config)
|
2011-07-15 19:36:06 +02:00
|
|
|
: config_file(file)
|
|
|
|
{
|
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
|
|
|
LOG_D("hotkey/init") << "Generating hotkeys.";
|
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
auto root = json_util::file(config_file, default_config);
|
|
|
|
for (auto const& hotkey_context : static_cast<json::Object const&>(root))
|
2012-11-13 15:07:39 +01:00
|
|
|
BuildHotkey(hotkey_context.first, hotkey_context.second);
|
2014-07-05 05:16:24 +02:00
|
|
|
UpdateStrMap();
|
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-13 15:07:39 +01:00
|
|
|
void Hotkey::BuildHotkey(std::string const& context, json::Object const& hotkeys) {
|
|
|
|
for (auto const& command : hotkeys) {
|
2014-07-05 05:16:24 +02:00
|
|
|
json::Array const& command_hotkeys = command.second;
|
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-06-28 05:44:47 +02:00
|
|
|
for (json::Object const& hotkey : command_hotkeys) {
|
2014-07-05 05:16:24 +02:00
|
|
|
auto mod_it = hotkey.find("modifiers");
|
|
|
|
if (mod_it == end(hotkey)) {
|
2014-06-28 05:44:47 +02:00
|
|
|
LOG_E("agi/hotkey/load") << "Hotkey for command '" << command.first << "' is missing modifiers";
|
|
|
|
continue;
|
2012-01-25 20:07:18 +01:00
|
|
|
}
|
2014-07-05 05:16:24 +02:00
|
|
|
auto key_it = hotkey.find("key");
|
|
|
|
if (key_it == end(hotkey)) {
|
2014-06-28 05:44:47 +02:00
|
|
|
LOG_E("agi/hotkey/load") << "Hotkey for command '" << command.first << "' is missing the key";
|
|
|
|
continue;
|
2012-01-25 20:07:18 +01:00
|
|
|
}
|
2011-10-18 00:00:28 +02:00
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
std::string key_str;
|
|
|
|
json::Array const& arr_mod = mod_it->second;
|
|
|
|
for (std::string const& mod : arr_mod) {
|
|
|
|
key_str += mod;
|
|
|
|
key_str += '-';
|
|
|
|
}
|
|
|
|
key_str += static_cast<std::string const&>(key_it->second);
|
|
|
|
|
|
|
|
cmd_map.insert(make_pair(command.first, Combo(context, command.first, std::move(key_str))));
|
2011-10-18 00:00:28 +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
|
|
|
}
|
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
std::string Hotkey::Scan(std::string const& context, std::string const& str, bool always) const {
|
|
|
|
const std::string *local = nullptr, *dfault = nullptr;
|
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-07-05 05:16:24 +02:00
|
|
|
std::vector<const Combo *>::const_iterator index, end;
|
|
|
|
for (std::tie(index, end) = boost::equal_range(str_map, str, combo_cmp()); index != end; ++index) {
|
|
|
|
std::string const& ctext = (*index)->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-26 21:52:25 +02:00
|
|
|
if (always && ctext == "Always") {
|
2014-07-05 05:16:24 +02:00
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Always Command: " << (*index)->CmdName();
|
|
|
|
return (*index)->CmdName();
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
2011-10-28 22:40:32 +02:00
|
|
|
if (ctext == "Default")
|
2014-07-05 05:16:24 +02:00
|
|
|
dfault = &(*index)->CmdName();
|
2011-10-28 22:40:32 +02:00
|
|
|
else if (ctext == context)
|
2014-07-05 05:16:24 +02:00
|
|
|
local = &(*index)->CmdName();
|
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-07-05 05:16:24 +02:00
|
|
|
if (local) {
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context: " << context << " Command: " << *local;
|
|
|
|
return *local;
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
2014-07-05 05:16:24 +02:00
|
|
|
if (dfault) {
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << *dfault;
|
|
|
|
return *dfault;
|
2011-01-16 08:17:53 +01: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-22 22:27:06 +01:00
|
|
|
return "";
|
2011-01-18 00:53:46 +01:00
|
|
|
}
|
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
bool Hotkey::HasHotkey(std::string const& context, std::string const& str) const {
|
|
|
|
std::vector<const Combo *>::const_iterator index, end;
|
|
|
|
for (std::tie(index, end) = boost::equal_range(str_map, str, combo_cmp()); index != end; ++index) {
|
|
|
|
if (context == (*index)->Context())
|
2013-06-09 03:41:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
std::vector<std::string> Hotkey::GetHotkeys(std::string const& context, std::string const& command) const {
|
2011-01-18 00:53:46 +01:00
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
|
|
|
HotkeyMap::const_iterator it, end;
|
2012-09-25 01:35:27 +02:00
|
|
|
for (std::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
2014-07-05 05:16:24 +02:00
|
|
|
std::string const& ctext = it->second.Context();
|
2011-10-28 22:40:32 +02:00
|
|
|
if (ctext == "Always" || ctext == "Default" || ctext == context)
|
2014-07-05 05:16:24 +02:00
|
|
|
ret.emplace_back(it->second.Str());
|
2011-01-18 00:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sort(ret.begin(), ret.end());
|
|
|
|
ret.erase(unique(ret.begin(), ret.end()), ret.end());
|
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-01-18 00:53:46 +01:00
|
|
|
return ret;
|
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-07-05 05:16:24 +02:00
|
|
|
std::string Hotkey::GetHotkey(std::string const& context, std::string const& command) const {
|
2011-07-26 21:52:57 +02:00
|
|
|
std::string ret;
|
|
|
|
HotkeyMap::const_iterator it, end;
|
2012-09-25 01:35:27 +02:00
|
|
|
for (std::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
2014-07-05 05:16:24 +02:00
|
|
|
std::string const& ctext = it->second.Context();
|
|
|
|
if (ctext == context) return it->second.Str();
|
2011-10-28 22:40:32 +02:00
|
|
|
if (ctext == "Default")
|
2014-07-05 05:16:24 +02:00
|
|
|
ret = it->second.Str();
|
2011-10-28 22:40:32 +02:00
|
|
|
else if (ret.empty() && ctext == "Always")
|
2014-07-05 05:16:24 +02:00
|
|
|
ret = it->second.Str();
|
2011-07-26 21:52:57 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void Hotkey::Flush() {
|
|
|
|
json::Object root;
|
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
for (auto const& combo : str_map) {
|
|
|
|
auto keys = combo->Str();
|
|
|
|
if (keys.empty()) 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
|
|
|
json::Object hotkey;
|
2014-07-05 05:16:24 +02:00
|
|
|
json::Array& modifiers = hotkey["modifiers"];
|
|
|
|
for (auto tok : agi::Split(keys, '-')) {
|
|
|
|
if (end(tok) == end(keys))
|
|
|
|
hotkey.insert(make_pair("key", agi::str(tok)));
|
|
|
|
else
|
|
|
|
modifiers.push_back(agi::str(tok));
|
2012-01-25 20:07:18 +01: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
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
json::Object& context = root[combo->Context()];
|
|
|
|
json::Array& combo_array = context[combo->CmdName()];
|
2012-11-13 15:07:39 +01:00
|
|
|
combo_array.push_back(std::move(hotkey));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
io::Save file(config_file);
|
2014-06-28 05:44:47 +02:00
|
|
|
JsonWriter::Write(root, file.Get());
|
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-07-05 05:16:24 +02:00
|
|
|
void Hotkey::UpdateStrMap() {
|
2011-10-28 22:40:32 +02:00
|
|
|
str_map.clear();
|
2014-07-05 05:16:24 +02:00
|
|
|
str_map.reserve(cmd_map.size());
|
|
|
|
for (auto const& combo : cmd_map)
|
|
|
|
str_map.push_back(&combo.second);
|
|
|
|
|
|
|
|
sort(begin(str_map), end(str_map), combo_cmp());
|
|
|
|
}
|
2011-10-28 22:40:32 +02:00
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
void Hotkey::SetHotkeyMap(HotkeyMap new_map) {
|
|
|
|
cmd_map = std::move(new_map);
|
|
|
|
UpdateStrMap();
|
2011-11-06 18:18:08 +01:00
|
|
|
Flush();
|
2011-10-28 22:40:32 +02:00
|
|
|
HotkeysChanged();
|
|
|
|
}
|
|
|
|
|
2014-07-05 05:16:24 +02:00
|
|
|
} }
|