OpenTTD Source
1.11.0-beta2
|
Go to the documentation of this file.
19 std::string _hotkeys_file;
41 {
"BACKSPACE", WKC_BACKSPACE},
44 {
"PAGEUP", WKC_PAGEUP},
45 {
"PAGEDOWN", WKC_PAGEDOWN},
48 {
"RETURN", WKC_RETURN},
62 {
"BACKQUOTE", WKC_BACKQUOTE},
64 {
"NUM_DIV", WKC_NUM_DIV},
65 {
"NUM_MUL", WKC_NUM_MUL},
66 {
"NUM_MINUS", WKC_NUM_MINUS},
67 {
"NUM_PLUS", WKC_NUM_PLUS},
68 {
"NUM_ENTER", WKC_NUM_ENTER},
69 {
"NUM_DOT", WKC_NUM_DECIMAL},
97 static uint16
ParseCode(
const char *start,
const char *end)
100 while (start < end && *start ==
' ') start++;
101 while (end > start && *end ==
' ') end--;
107 if (end - start == 1) {
108 if (*start >=
'a' && *start <=
'z')
return *start - (
'a'-
'A');
110 if (*(
const uint8 *)start < 128)
return *start;
123 assert(start <= end);
126 const char *cur = start;
127 while (*cur !=
'+' && cur != end) cur++;
129 if (code == 0)
return 0;
130 if (code & WKC_SPECIAL_KEYS) {
132 if (code & ~WKC_SPECIAL_KEYS)
return 0;
136 if (keycode & ~WKC_SPECIAL_KEYS)
return 0;
139 if (cur == end)
break;
153 const char *start = value;
154 while (*start !=
'\0') {
155 const char *end = start;
156 while (*end !=
'\0' && *end !=
',') end++;
158 if (keycode != 0) hotkey->
AddKeycode(keycode);
159 start = (*end ==
',') ? end + 1: end;
181 if (keycode & WKC_SHIFT) {
186 if (keycode & WKC_CTRL) {
191 if (keycode & WKC_ALT) {
196 if (keycode & WKC_META) {
202 keycode = keycode & ~WKC_SPECIAL_KEYS;
210 assert(keycode < 128);
228 static char buf[128];
230 for (uint i = 0; i < hotkey->keycodes.size(); i++) {
248 if (default_keycode != 0) this->
AddKeycode(default_keycode);
261 const uint16 *keycode = default_keycodes;
262 while (*keycode != 0) {
275 include(this->keycodes, keycode);
278 HotkeyList::HotkeyList(
const char *ini_group,
Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
279 global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
285 HotkeyList::~HotkeyList()
297 for (
Hotkey *hotkey = this->items; hotkey->name !=
nullptr; ++hotkey) {
299 if (item !=
nullptr) {
300 hotkey->keycodes.clear();
313 for (
const Hotkey *hotkey = this->items; hotkey->name !=
nullptr; ++hotkey) {
327 for (
const Hotkey *list = this->items; list->name !=
nullptr; ++list) {
328 auto begin = list->keycodes.begin();
329 auto end = list->keycodes.end();
330 if (std::find(begin, end, keycode |
WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
338 static void SaveLoadHotkeys(
bool save)
359 SaveLoadHotkeys(
false);
365 SaveLoadHotkeys(
true);
368 void HandleGlobalHotkeys(
WChar key, uint16 keycode)
371 if (list->global_hotkey_handler ==
nullptr)
continue;
373 int hotkey = list->CheckMatch(keycode,
true);
374 if (hotkey >= 0 && (list->global_hotkey_handler(hotkey) ==
ES_HANDLED))
return;
@ ES_HANDLED
The passed event is handled.
@ WKC_SINGLEQUOTE
' Single quote
static const KeycodeNames _keycode_to_name[]
Array of non-standard keycodes that can be used in the hotkeys config file.
char32_t WChar
Type for wide characters, i.e.
List of hotkeys for a window.
A single "line" in an ini file.
@ WKC_SLASH
/ Forward slash
A group within an ini file.
@ WKC_BACKSLASH
\ Backslash
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
@ WKC_L_BRACKET
[ Left square bracket
static uint16 ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
static void ParseHotkeys(Hotkey *hotkey, const char *value)
Parse a string to the keycodes it represents.
void AddKeycode(uint16 keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
String representation of a keycode.
@ WKC_GLOBAL_HOTKEY
Fake keycode bit to indicate global hotkeys.
void Save(IniFile *ini) const
Save HotkeyList to IniFile.
void Load(IniFile *ini)
Load HotkeyList from IniFile.
std::optional< std::string > value
The value of this item.
const char * name
Name of the keycode.
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
WindowKeyCodes keycode
The keycode.
@ WKC_R_BRACKET
] Right square bracket
const char * SaveKeycodes(const Hotkey *hotkey)
Convert all keycodes attached to a hotkey to a single string.
static const char * KeycodeToString(uint16 keycode)
Convert a hotkey to it's string representation so it can be written to the config file.
Ini file that supports both loading and saving.
@ NO_DIRECTORY
A path without any base directory.
static uint16 ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
Hotkey(uint16 default_keycode, const char *name, int num)
Create a new Hotkey object with a single default keycode.
void SetValue(const char *value)
Replace the current value with another value.
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
@ WKC_SEMICOLON
; Semicolon
#define lengthof(x)
Return the length of an fixed size array.
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
#define lastof(x)
Get the last element of an fixed size array.
All data for a single hotkey.
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.