Update list of wide characters
[centerim5.git] / cppconsui / KeyConfig.cpp
blobf5d435b0c87fadb9bed600f25194dc1c2ccc4832
1 // Copyright (C) 2009-2015 Petr Pavlu <setup@dagobah.cz>
2 //
3 // This file is part of CenterIM.
4 //
5 // CenterIM is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // CenterIM is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
18 /// @file
19 /// KeyConfig class implementation.
20 ///
21 /// @ingroup cppconsui
23 #include "KeyConfig.h"
25 #include "CoreManager.h"
27 #include "gettext.h"
28 #include <cstring>
30 namespace CppConsUI {
32 bool KeyConfig::bindKey(
33 const char *context, const char *action, const char *key)
35 TermKeyKey tkey;
36 const char *res = termkey_strpkey(
37 COREMANAGER->getTermKeyHandle(), key, &tkey, TERMKEY_FORMAT_LONGMOD);
38 if (res == nullptr || res[0] != '\0')
39 return false;
41 binds_[context][tkey] = action;
42 return true;
45 const KeyConfig::KeyBindContext *KeyConfig::getKeyBinds(
46 const char *context) const
48 KeyBinds::const_iterator i = binds_.find(context);
49 if (i == binds_.end())
50 return nullptr;
51 return &i->second;
54 const char *KeyConfig::getKeyBind(const char *context, const char *action) const
56 KeyBinds::const_iterator i = binds_.find(context);
57 if (i == binds_.end())
58 return nullptr;
60 for (const KeyBindContext::value_type &key_action : i->second)
61 if (!key_action.second.compare(action)) {
62 TermKeyKey key = key_action.first;
63 static char out[256];
64 termkey_strfkey(COREMANAGER->getTermKeyHandle(), out, sizeof(out), &key,
65 TERMKEY_FORMAT_CARETCTRL);
66 return out;
69 return _("<unbound>");
72 char *KeyConfig::termKeyToString(const TermKeyKey &key) const
74 TermKeyKey key_copy = key;
75 char out[256];
76 termkey_strfkey(COREMANAGER->getTermKeyHandle(), out, sizeof(out), &key_copy,
77 TERMKEY_FORMAT_LONGMOD);
79 std::size_t size = std::strlen(out) + 1;
80 auto res = new char[size];
81 std::strcpy(res, out);
82 return res;
85 bool KeyConfig::stringToTermKey(const char *key, TermKeyKey *termkey) const
87 const char *res = termkey_strpkey(
88 COREMANAGER->getTermKeyHandle(), key, termkey, TERMKEY_FORMAT_LONGMOD);
89 return res != nullptr && res[0] == '\0';
92 void KeyConfig::clear()
94 binds_.clear();
97 void KeyConfig::loadDefaultKeyConfig()
99 // Clear current bindings.
100 clear();
102 bindKey("button", "activate", "Enter");
104 bindKey("checkbox", "toggle", "Enter");
106 bindKey("container", "focus-previous", "Shift-Tab");
107 bindKey("container", "focus-next", "Tab");
108 bindKey("container", "focus-up", "Up");
109 bindKey("container", "focus-down", "Down");
110 bindKey("container", "focus-left", "Left");
111 bindKey("container", "focus-right", "Right");
112 bindKey("container", "focus-page-up", "PageUp");
113 bindKey("container", "focus-page-down", "PageDown");
114 bindKey("container", "focus-begin", "Home");
115 bindKey("container", "focus-end", "End");
117 bindKey("coremanager", "redraw-screen", "Ctrl-l");
119 bindKey("textentry", "cursor-right", "Right");
120 bindKey("textentry", "cursor-left", "Left");
121 bindKey("textentry", "cursor-down", "Down");
122 bindKey("textentry", "cursor-up", "Up");
123 bindKey("textentry", "cursor-right-word", "Ctrl-Right");
124 bindKey("textentry", "cursor-left-word", "Ctrl-Left");
125 bindKey("textentry", "cursor-end", "End");
126 bindKey("textentry", "cursor-begin", "Home");
127 bindKey("textentry", "delete-char", "Delete");
128 bindKey("textentry", "backspace", "Backspace");
130 bindKey("textentry", "delete-word-end", "Ctrl-Delete");
131 /// @todo Enable toogle-overwrite bind.
132 // bindKey("textentry", "toggle-overwrite", "Insert");
134 bindKey("textentry", "activate", "Enter");
136 bindKey("textview", "scroll-up", "PageUp");
137 bindKey("textview", "scroll-down", "PageDown");
139 bindKey("treeview", "fold-subtree", "-");
140 bindKey("treeview", "unfold-subtree", "+");
142 bindKey("window", "close-window", "Escape");
145 } // namespace CppConsUI
147 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: