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
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
std::string Combo::Str() {
|
|
|
|
std::string str(key_map[0]);
|
|
|
|
for (unsigned int i=1; i < key_map.size(); i++) {
|
|
|
|
str.append("-" + key_map[i]);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Combo::StrMenu() {
|
|
|
|
return Str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Hotkey::ComboInsert(Combo *combo) {
|
2011-01-18 00:53:46 +01:00
|
|
|
str_map.insert(make_pair(combo->Str(), combo));
|
|
|
|
cmd_map.insert(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();
|
|
|
|
}
|
|
|
|
|
|
|
|
Hotkey::Hotkey(const std::string &file, const std::string &default_config):
|
|
|
|
config_file(file), config_default(default_config) {
|
|
|
|
|
|
|
|
LOG_D("hotkey/init") << "Generating hotkeys.";
|
|
|
|
|
|
|
|
std::istream *stream;
|
|
|
|
|
2011-01-18 00:53:46 +01:00
|
|
|
try {
|
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
|
|
|
stream = agi::io::Open(config_file);
|
|
|
|
} catch (const acs::AcsNotFound&) {
|
2011-01-08 13:10:40 +01:00
|
|
|
stream = new std::istringstream(config_default);
|
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
|
|
|
|
|
|
|
|
|
|
|
json::UnknownElement hotkey_root;
|
2011-01-18 00:53:46 +01:00
|
|
|
try {
|
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_root = agi::json_util::parse(stream);
|
2011-01-08 13:13:24 +01:00
|
|
|
} catch (...) {
|
2011-01-18 00:53:46 +01:00
|
|
|
// There's definitely a better way to do this.
|
|
|
|
delete stream;
|
|
|
|
stream = new std::istringstream(config_default);
|
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_root = agi::json_util::parse(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Object object = hotkey_root;
|
|
|
|
|
|
|
|
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) {
|
|
|
|
const json::Object::Member& member = *index;
|
|
|
|
const json::Object& obj = member.element;
|
|
|
|
BuildHotkey(member.name, obj);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Hotkey::BuildHotkey(std::string context, const json::Object& object) {
|
|
|
|
for (json::Object::const_iterator index(object.Begin()); index != object.End(); index++) {
|
|
|
|
const json::Object::Member& member = *index;
|
|
|
|
const json::Array& array = member.element;
|
|
|
|
|
2011-01-18 00:53:46 +01:00
|
|
|
for (json::Array::const_iterator arr_index(array.Begin()); arr_index != array.End(); arr_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
|
|
|
Combo *combo = new Combo(context, member.name);
|
|
|
|
|
2011-01-18 00:53:46 +01:00
|
|
|
const json::Object& obj = *arr_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
|
|
|
|
|
|
|
const json::Array& arr_mod = obj["modifiers"];
|
|
|
|
|
|
|
|
if (arr_mod.Size() > 0) {
|
|
|
|
for (json::Array::const_iterator arr_mod_index(arr_mod.Begin()); arr_mod_index != arr_mod.End(); arr_mod_index++) {
|
|
|
|
const json::String& key_mod = *arr_mod_index;
|
|
|
|
combo->KeyInsert(key_mod.Value());
|
|
|
|
} // for arr_mod_index
|
|
|
|
|
|
|
|
}
|
2011-01-18 00:53:46 +01:00
|
|
|
combo->KeyInsert(static_cast<const json::String&>(obj["key"]).Value());
|
|
|
|
combo->Enable(static_cast<const json::Boolean&>(obj["enable"]).Value());
|
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);
|
|
|
|
} // for arr_index
|
|
|
|
} // for index
|
|
|
|
}
|
|
|
|
|
2011-01-18 00:53:38 +01:00
|
|
|
bool Hotkey::Scan(const std::string &context, const std::string &str, 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-01-16 08:17:53 +01:00
|
|
|
std::string 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
|
|
|
|
|
|
|
if (ctext == "Always") {
|
2011-01-16 08:17:53 +01: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;
|
|
|
|
return 0;
|
|
|
|
} else if (ctext == "Default") {
|
2011-01-16 08:17:53 +01:00
|
|
|
dfault = 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
|
|
|
} else if (ctext == context) {
|
2011-01-16 08:17:53 +01:00
|
|
|
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;
|
|
|
|
return 0;
|
|
|
|
} else if (!dfault.empty()) {
|
|
|
|
cmd = dfault;
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << dfault;
|
|
|
|
return 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-01-16 08:17:53 +01:00
|
|
|
return 1;
|
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) {
|
|
|
|
std::string ctext = it->second->Context();
|
|
|
|
if (ctext == "Always" || ctext == "Default" || ctext == context) {
|
|
|
|
ret.push_back(it->second->StrMenu());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Hotkey::Flush() {
|
|
|
|
|
|
|
|
json::Object root;
|
|
|
|
|
|
|
|
HotkeyMap::iterator index;
|
2011-01-18 00:53:46 +01:00
|
|
|
for (index = str_map.begin(); index != str_map.end(); ++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
|
|
|
|
|
|
|
Combo::ComboMap combo_map(index->second->Get());
|
|
|
|
|
|
|
|
json::Array modifiers;
|
|
|
|
for (int i = 0; i != combo_map.size()-1; i++) {
|
|
|
|
modifiers.Insert(json::String(combo_map[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
json::Object hotkey;
|
|
|
|
hotkey["modifiers"] = modifiers;
|
|
|
|
hotkey["key"] = json::String(combo_map.back());
|
|
|
|
hotkey["enable"] = json::Boolean(index->second->IsEnabled());
|
|
|
|
|
|
|
|
json::Object& context_obj = root[index->second->Context()];
|
|
|
|
json::Array& combo_array = context_obj[index->second->CmdName()];
|
|
|
|
|
|
|
|
combo_array.Insert(hotkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
io::Save file(config_file);
|
|
|
|
json::Writer::Write(root, file.Get());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace toolbar
|
|
|
|
} // namespace agi
|