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-12-22 22:10:50 +01:00
|
|
|
#include <algorithm>
|
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"
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
std::string Combo::Str() const {
|
2011-10-28 22:40:32 +02:00
|
|
|
if (key_map.empty()) return "";
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-12-22 22:10:50 +01:00
|
|
|
json::Object object(agi::json_util::file(config_file, default_config));
|
2011-10-28 22:40:32 +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);
|
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-28 22:40:32 +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-28 22:40:32 +02:00
|
|
|
for (json::Array::const_iterator arr_index(array.begin()); arr_index != array.end(); ++arr_index) {
|
2011-10-18 00:00:28 +02:00
|
|
|
const json::Array& arr_mod = (*arr_index)["modifiers"];
|
2011-10-28 22:40:32 +02:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
keys.reserve(arr_mod.size() + 1);
|
|
|
|
copy(arr_mod.begin(), arr_mod.end(), back_inserter(keys));
|
|
|
|
keys.push_back((*arr_index)["key"]);
|
2011-10-18 00:00:28 +02:00
|
|
|
|
2011-10-28 22:40:32 +02:00
|
|
|
ComboInsert(Combo(context, index->first, keys));
|
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-12-22 22:27:06 +01:00
|
|
|
std::string Hotkey::Scan(const std::string &context, const std::string &str, bool always) 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-12-22 22:27:06 +01:00
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Always Command: " << index->second.CmdName();
|
|
|
|
return index->second.CmdName();
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
2011-10-28 22:40:32 +02:00
|
|
|
if (ctext == "Default")
|
2011-07-15 19:36:06 +02:00
|
|
|
dfault = index->second.CmdName();
|
2011-10-28 22:40:32 +02:00
|
|
|
else if (ctext == context)
|
2011-07-15 19:36:06 +02: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()) {
|
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context: " << context << " Command: " << local;
|
2011-12-22 22:27:06 +01:00
|
|
|
return local;
|
2011-07-15 19:36:06 +02:00
|
|
|
}
|
|
|
|
if (!dfault.empty()) {
|
2011-01-16 08:17:53 +01:00
|
|
|
LOG_D("agi/hotkey/found") << "Found: " << str << " Context (req/found): " << context << "/Default Command: " << dfault;
|
2011-12-22 22:27:06 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-10-28 22:40:32 +02: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();
|
2011-10-28 22:40:32 +02:00
|
|
|
if (ctext == "Default")
|
|
|
|
ret = it->second.StrMenu();
|
|
|
|
else if (ret.empty() && ctext == "Always")
|
2011-12-22 22:27:17 +01:00
|
|
|
ret = it->second.StrMenu();
|
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;
|
|
|
|
|
2011-07-15 19:36:06 +02:00
|
|
|
for (HotkeyMap::iterator index = str_map.begin(); index != str_map.end(); ++index) {
|
2011-10-28 22:40:32 +02:00
|
|
|
std::vector<std::string> const& 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::Object hotkey;
|
2011-10-18 00:00:28 +02:00
|
|
|
hotkey["key"] = combo_map.back();
|
2011-12-22 22:10:50 +01:00
|
|
|
json::Array& modifiers = hotkey["modifiers"];
|
|
|
|
|
|
|
|
copy(combo_map.begin(), combo_map.end() - 1, std::back_inserter(modifiers));
|
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());
|
|
|
|
}
|
|
|
|
|
2011-10-28 22:40:32 +02:00
|
|
|
void Hotkey::SetHotkeyMap(HotkeyMap const& new_map) {
|
|
|
|
cmd_map = new_map;
|
|
|
|
|
|
|
|
str_map.clear();
|
|
|
|
for (HotkeyMap::iterator it = cmd_map.begin(); it != cmd_map.end(); ++it)
|
|
|
|
str_map.insert(make_pair(it->second.Str(), it->second));
|
|
|
|
|
2011-11-06 18:18:08 +01:00
|
|
|
Flush();
|
2011-10-28 22:40:32 +02:00
|
|
|
HotkeysChanged();
|
|
|
|
}
|
|
|
|
|
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 toolbar
|
|
|
|
} // namespace agi
|