dispatcher.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_DISPATCHER_H
  17. #define BDE_DISPATCHER_H
  18. #include "event.h"
  19. #include <string.h>
  20. // class Dispatcher represents a class that receives GUI events.
  21. #define INTERACTIVE
  22. struct binding_entry {
  23. Event evt;
  24. const char *action;
  25. };
  26. class Dispatcher {
  27. public:
  28. Dispatcher() { }
  29. virtual bool do_action(const char *action) {
  30. return false;
  31. }
  32. virtual const char *get_event_action(const Event &evt) {
  33. return NULL;
  34. }
  35. virtual bool get_action_event(const char *action, Event &evt) {
  36. return false;
  37. }
  38. virtual const char *get_action_description(const char *action) {
  39. return NULL;
  40. }
  41. virtual bool handle_event(const Event &evt) {
  42. return do_action(get_event_action(evt));
  43. }
  44. };
  45. // :TODO: I should use STL's map instead of a simple linear search.
  46. #define HAS_ACTIONS_MAP(CLASS, PARENT_CLASS) \
  47. typedef void (CLASS::*method_ptr)(); \
  48. struct action_entry { \
  49. const char *action; \
  50. method_ptr method; \
  51. const char *short_description; \
  52. }; \
  53. static action_entry actions_table[]; \
  54. virtual bool do_action(const char *action) { \
  55. if (!action) return false; \
  56. action_entry *entry = actions_table; \
  57. while (entry->action) { \
  58. if (!strcmp(entry->action, action)) { \
  59. (this->*(entry->method))(); \
  60. return true; \
  61. } \
  62. entry++; \
  63. } \
  64. return PARENT_CLASS::do_action(action); \
  65. } \
  66. virtual const char *get_action_description \
  67. (const char *action) { \
  68. if (!action) return NULL; \
  69. action_entry *entry = actions_table; \
  70. while (entry->action) { \
  71. if (!strcmp(entry->action, action)) \
  72. return entry->short_description; \
  73. entry++; \
  74. } \
  75. return PARENT_CLASS:: \
  76. get_action_description(action); \
  77. }
  78. #define ADD_ACTION(CLASS, METHOD, DESC) \
  79. { #METHOD, &CLASS::METHOD, DESC }
  80. #define END_ACTIONS \
  81. { 0, 0 }
  82. #define HAS_BINDINGS_MAP(CLASS, PARENT_CLASS) \
  83. static binding_entry bindings_table[]; \
  84. virtual const char *get_event_action(const Event &evt) { \
  85. binding_entry *entry = bindings_table; \
  86. while (entry->action) { \
  87. if (entry->evt == evt) \
  88. return entry->action; \
  89. entry++; \
  90. } \
  91. return PARENT_CLASS::get_event_action(evt); \
  92. } \
  93. virtual bool get_action_event(const char *action, Event &evt) { \
  94. binding_entry *entry = bindings_table; \
  95. while (entry->action) { \
  96. if (!strcmp(entry->action, action)) { \
  97. evt = entry->evt; \
  98. return true; \
  99. } \
  100. entry++; \
  101. } \
  102. return PARENT_CLASS::get_action_event(action, evt); \
  103. }
  104. #define END_BINDINGS \
  105. { Event(), 0 }
  106. #endif