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.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file hotkey.cpp
|
|
|
|
/// @brief Hotkey handler
|
|
|
|
/// @ingroup hotkey menu event window
|
|
|
|
|
2011-01-05 19:40:37 +01:00
|
|
|
#include "../config.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
|
|
|
|
|
|
|
#ifndef LAGI_PRE
|
2011-07-27 00:25:21 +02:00
|
|
|
#include <cmath>
|
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 <memory>
|
|
|
|
#endif
|
|
|
|
|
2011-01-18 01:29:40 +01:00
|
|
|
#ifdef _WIN32
|
2011-01-18 00:53:46 +01:00
|
|
|
#include <tuple>
|
2011-01-18 01:29:40 +01:00
|
|
|
#else
|
|
|
|
#include <tr1/tuple>
|
|
|
|
#endif
|
2011-01-18 00:53:46 +01:00
|
|
|
|
2011-01-08 13:10:40 +01:00
|
|
|
#include "libaegisub/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 "libaegisub/access.h"
|
2011-01-08 13:10:40 +01:00
|
|
|
#include "libaegisub/cajun/writer.h"
|
|
|
|
#include "libaegisub/exception.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"
|
2011-01-08 13:10:40 +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
|
|
|
namespace agi {
|
|
|
|
namespace hotkey {
|
|
|
|
|
|
|
|
Hotkey *hotkey;
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
std::string Combo::Str() const {
|
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
|
|
|
std::string str(key_map[0]);
|
2011-07-15 19:36:06 +02:00
|
|
|
str.reserve(str.size() + (key_map.size() - 1) * 2);
|
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
|
|
|
for (unsigned int i=1; i < key_map.size(); i++) {
|
|
|
|
str.append("-" + key_map[i]);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
std::string Combo::StrMenu() const {
|
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 Str();
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
void Hotkey::ComboInsert(Combo const& combo) {
|
|
|
|
str_map.insert(std::make_pair(combo.Str(), combo));
|
|
|
|
cmd_map.insert(std::make_pair(combo.CmdName(), combo));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Hotkey::~Hotkey() {
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
Hotkey::Hotkey(const std::string &file, const std::string &default_config)
|
|
|
|
: 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.";
|
|
|
|
|
2011-07-26 22:26:09 +02:00
|
|
|
json::UnknownElement hotkey_root = agi::json_util::file(config_file, default_config);
|
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 object = hotkey_root;
|
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
|
2011-10-18 00:00:28 +02:00
|
|
|
BuildHotkey(index->first, index->second);
|
2011-01-18 00:53:46 +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-07-15 19:36:06 +02:00
|
|
|
void Hotkey::BuildHotkey(std::string const& context, const json::Object& object) {
|
2011-10-17 23:59:35 +02:00
|
|
|
for (json::Object::const_iterator index(object.begin()); index != object.end(); index++) {
|
2011-10-17 23:59:47 +02:00
|
|
|
const json::Array& array = index->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
|
|
|
|
2011-10-17 23:59:35 +02:00
|
|
|
for (json::Array::const_iterator arr_index(array.begin()); arr_index != array.end(); arr_index++) {
|
2011-10-17 23:59:47 +02:00
|
|
|
Combo combo(context, index->first);
|
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-10-18 00:00:28 +02:00
|
|
|
const json::Array& arr_mod = (*arr_index)["modifiers"];
|
2011-10-17 23:59:35 +02:00
|
|
|
for (json::Array::const_iterator arr_mod_index(arr_mod.begin()); arr_mod_index != arr_mod.end(); arr_mod_index++) {
|
2011-10-18 00:00:28 +02:00
|
|
|
combo.KeyInsert(*arr_mod_index);
|
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-10-18 00:00:28 +02:00
|
|
|
|
|
|
|
combo.KeyInsert((*arr_index)["key"]);
|
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
|
|
|
ComboInsert(combo);
|
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
|
|
|
}
|
|
|
|
|
2011-07-26 21:52:25 +02:00
|
|
|
bool Hotkey::Scan(const std::string &context, const std::string &str, bool always, std::string &cmd) const {
|
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
|
|
|
std::string local, dfault;
|
|
|
|
|
2011-01-18 00:53:46 +01:00
|
|
|
HotkeyMap::const_iterator index, end;
|
|
|
|
for (std::tr1::tie(index, end) = str_map.equal_range(str); index != end; ++index) {
|
2011-07-15 19:36:06 +02:00
|
|
|
std::string const& ctext = index->second.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") {
|
2011-07-15 19:36:06 +02:00
|
|
|
cmd = index->second.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
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Always Command: " << cmd;
|
2011-07-26 21:52:36 +02:00
|
|
|
return true;
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
|
|
|
if (ctext == "Default") {
|
|
|
|
dfault = index->second.CmdName();
|
|
|
|
}
|
|
|
|
else if (ctext == context) {
|
|
|
|
local = index->second.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 08:17:53 +01:00
|
|
|
if (!local.empty()) {
|
|
|
|
cmd = local;
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context: " << context << " Command: " << local;
|
2011-07-26 21:52:36 +02:00
|
|
|
return true;
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
|
|
|
if (!dfault.empty()) {
|
2011-01-16 08:17:53 +01:00
|
|
|
cmd = dfault;
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << dfault;
|
2011-07-26 21:52:36 +02:00
|
|
|
return true;
|
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-07-26 21:52:36 +02:00
|
|
|
return false;
|
2011-01-18 00:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> Hotkey::GetHotkeys(const std::string &context, const std::string &command) const {
|
|
|
|
std::vector<std::string> ret;
|
|
|
|
|
|
|
|
HotkeyMap::const_iterator it, end;
|
|
|
|
for (std::tr1::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
2011-07-15 19:36:06 +02:00
|
|
|
std::string ctext = it->second.Context();
|
2011-01-18 00:53:46 +01:00
|
|
|
if (ctext == "Always" || ctext == "Default" || ctext == context) {
|
2011-07-15 19:36:06 +02:00
|
|
|
ret.push_back(it->second.StrMenu());
|
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
|
|
|
}
|
|
|
|
|
2011-07-26 21:52:57 +02:00
|
|
|
std::string Hotkey::GetHotkey(const std::string &context, const std::string &command) const {
|
|
|
|
std::string ret;
|
|
|
|
HotkeyMap::const_iterator it, end;
|
|
|
|
for (std::tr1::tie(it, end) = cmd_map.equal_range(command); it != end; ++it) {
|
|
|
|
std::string ctext = it->second.Context();
|
|
|
|
if (ctext == context) return it->second.StrMenu();
|
|
|
|
if (ctext == "Default") ret = it->second.StrMenu();
|
|
|
|
else if (ret.empty() && ctext == "Always") it->second.StrMenu();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
for (HotkeyMap::iterator index = str_map.begin(); index != str_map.end(); ++index) {
|
|
|
|
Combo::ComboMap combo_map(index->second.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
|
|
|
|
|
|
|
json::Array modifiers;
|
|
|
|
for (int i = 0; i != combo_map.size()-1; i++) {
|
2011-10-17 23:59:35 +02:00
|
|
|
modifiers.push_back(json::String(combo_map[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
|
|
|
}
|
|
|
|
|
|
|
|
json::Object hotkey;
|
|
|
|
hotkey["modifiers"] = modifiers;
|
2011-10-18 00:00:28 +02:00
|
|
|
hotkey["key"] = combo_map.back();
|
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-10-18 00:00:28 +02:00
|
|
|
json::Array& combo_array = root[index->second.Context()][index->second.CmdName()];
|
2011-10-17 23:59:35 +02:00
|
|
|
combo_array.push_back(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);
|
|
|
|
json::Writer::Write(root, file.Get());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace toolbar
|
|
|
|
} // namespace agi
|