hotkeys.h

Go to the documentation of this file.
00001 /* $Id$ */
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 
00022 template<class T>
00023 struct Hotkey {
00024   typedef void (T::*hotkey_callback)(int);
00025 
00033   struct CallbackWrapper {
00034     CallbackWrapper(hotkey_callback callback) :
00035       callback(callback)
00036     {}
00037     hotkey_callback callback;
00038   };
00039 
00047   Hotkey(uint16 default_keycode, const char *name, int num, hotkey_callback callback = NULL) :
00048     name(name),
00049     num(num)
00050   {
00051     if (callback == NULL) {
00052       this->callback = NULL;
00053     } else {
00054       this->callback = new CallbackWrapper(callback);
00055     }
00056     if (default_keycode != 0) this->AddKeycode(default_keycode);
00057   }
00058 
00066   Hotkey(const uint16 *default_keycodes, const char *name, int num, hotkey_callback callback = NULL) :
00067     name(name),
00068     num(num)
00069   {
00070     if (callback == NULL) {
00071       this->callback = NULL;
00072     } else {
00073       this->callback = new CallbackWrapper(callback);
00074     }
00075 
00076     const uint16 *keycode = default_keycodes;
00077     while (*keycode != 0) {
00078       this->AddKeycode(*keycode);
00079       keycode++;
00080     }
00081   }
00082 
00083   ~Hotkey()
00084   {
00085     delete this->callback;
00086   }
00087 
00093   void AddKeycode(uint16 keycode)
00094   {
00095     this->keycodes.Include(keycode);
00096   }
00097 
00098   const char *name;
00099   int num;
00100   SmallVector<uint16, 1> keycodes;
00101   CallbackWrapper *callback;
00102 };
00103 
00104 #define HOTKEY_LIST_END(window_class) Hotkey<window_class>((uint16)0, NULL, -1)
00105 
00114 template<class T>
00115 int CheckHotkeyMatch(Hotkey<T> *list, uint16 keycode, T *w, bool global_only = false)
00116 {
00117   while (list->num != -1) {
00118     if (list->keycodes.Contains(keycode | WKC_GLOBAL_HOTKEY) || (!global_only && list->keycodes.Contains(keycode))) {
00119       if (!global_only && list->callback != NULL) (w->*(list->callback->callback))(-1);
00120       return list->num;
00121     }
00122     list++;
00123   }
00124   return -1;
00125 }
00126 
00127 bool IsQuitKey(uint16 keycode);
00128 
00129 void LoadHotkeysFromConfig();
00130 void SaveHotkeysToConfig();
00131 
00132 
00133 void HandleGlobalHotkeys(uint16 key, uint16 keycode);
00134 
00135 #endif /* HOTKEYS_H */

Generated on Sun Jun 5 04:19:56 2011 for OpenTTD by  doxygen 1.6.1