hotkeys.h

Go to the documentation of this file.
00001 /* $Id: hotkeys.h 25982 2013-11-13 21:35:44Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef HOTKEYS_H
00013 #define HOTKEYS_H
00014 
00015 #include "core/smallvec_type.hpp"
00016 #include "gfx_type.h"
00017 #include "string_type.h"
00018 
00023 template<class T>
00024 struct Hotkey {
00025   typedef void (T::*hotkey_callback)(int);
00026 
00034   struct CallbackWrapper {
00035     CallbackWrapper(hotkey_callback callback) :
00036       callback(callback)
00037     {}
00038     hotkey_callback callback;
00039   };
00040 
00048   Hotkey(uint16 default_keycode, const char *name, int num, hotkey_callback callback = NULL) :
00049     name(name),
00050     num(num)
00051   {
00052     if (callback == NULL) {
00053       this->callback = NULL;
00054     } else {
00055       this->callback = new CallbackWrapper(callback);
00056     }
00057     if (default_keycode != 0) this->AddKeycode(default_keycode);
00058   }
00059 
00067   Hotkey(const uint16 *default_keycodes, const char *name, int num, hotkey_callback callback = NULL) :
00068     name(name),
00069     num(num)
00070   {
00071     if (callback == NULL) {
00072       this->callback = NULL;
00073     } else {
00074       this->callback = new CallbackWrapper(callback);
00075     }
00076 
00077     const uint16 *keycode = default_keycodes;
00078     while (*keycode != 0) {
00079       this->AddKeycode(*keycode);
00080       keycode++;
00081     }
00082   }
00083 
00084   ~Hotkey()
00085   {
00086     delete this->callback;
00087   }
00088 
00094   void AddKeycode(uint16 keycode)
00095   {
00096     this->keycodes.Include(keycode);
00097   }
00098 
00099   const char *name;
00100   int num;
00101   SmallVector<uint16, 1> keycodes;
00102   CallbackWrapper *callback;
00103 };
00104 
00105 #define HOTKEY_LIST_END(window_class) Hotkey<window_class>((uint16)0, NULL, -1)
00106 
00115 template<class T>
00116 int CheckHotkeyMatch(Hotkey<T> *list, uint16 keycode, T *w, bool global_only = false)
00117 {
00118   while (list->num != -1) {
00119     if (list->keycodes.Contains(keycode | WKC_GLOBAL_HOTKEY) || (!global_only && list->keycodes.Contains(keycode))) {
00120       if (!global_only && list->callback != NULL) (w->*(list->callback->callback))(-1);
00121       return list->num;
00122     }
00123     list++;
00124   }
00125   return -1;
00126 }
00127 
00128 bool IsQuitKey(uint16 keycode);
00129 
00130 void LoadHotkeysFromConfig();
00131 void SaveHotkeysToConfig();
00132 
00133 
00134 void HandleGlobalHotkeys(WChar key, uint16 keycode);
00135 
00136 #endif /* HOTKEYS_H */