dispatcher.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // class Dispatcher represents a class that receives GUI events.
  20. #define INTERACTIVE
  21. struct binding_entry {
  22. Event evt;
  23. const char *action;
  24. };
  25. class Dispatcher {
  26. public:
  27. Dispatcher() { }
  28. virtual bool do_action(const char *action) {
  29. return false;
  30. }
  31. virtual const char *get_event_action(const Event &evt) {
  32. return NULL;
  33. }
  34. virtual bool get_action_event(const char *action, Event &evt) {
  35. return false;
  36. }
  37. virtual const char *get_action_description(const char *action) {
  38. return NULL;
  39. }
  40. virtual bool handle_event(const Event &evt) {
  41. return do_action(get_event_action(evt));
  42. }
  43. };
  44. // :TODO: I should use STL's map instead of a simple linear search.
  45. #define HAS_ACTIONS_MAP(CLASS, PARENT_CLASS) \
  46. typedef void (CLASS::*method_ptr)(); \
  47. struct action_entry { \
  48. const char *action; \
  49. method_ptr method; \
  50. const char *short_description; \
  51. }; \
  52. static action_entry actions_table[]; \
  53. virtual bool do_action(const char *action) { \
  54. if (!action) return false; \
  55. action_entry *entry = actions_table; \
  56. while (entry->action) { \
  57. if (!strcmp(entry->action, action)) { \
  58. (this->*(entry->method))(); \
  59. return true; \
  60. } \
  61. entry++; \
  62. } \
  63. return PARENT_CLASS::do_action(action); \
  64. } \
  65. virtual const char *get_action_description \
  66. (const char *action) { \
  67. if (!action) return NULL; \
  68. action_entry *entry = actions_table; \
  69. while (entry->action) { \
  70. if (!strcmp(entry->action, action)) \
  71. return entry->short_description; \
  72. entry++; \
  73. } \
  74. return PARENT_CLASS:: \
  75. get_action_description(action); \
  76. }
  77. #define ADD_ACTION(CLASS, METHOD, DESC) \
  78. { #METHOD, &CLASS::METHOD, DESC }
  79. #define END_ACTIONS \
  80. { 0, 0 }
  81. #define HAS_BINDINGS_MAP(CLASS, PARENT_CLASS) \
  82. static binding_entry bindings_table[]; \
  83. virtual const char *get_event_action(const Event &evt) { \
  84. binding_entry *entry = bindings_table; \
  85. while (entry->action) { \
  86. if (entry->evt == evt) \
  87. return entry->action; \
  88. entry++; \
  89. } \
  90. return PARENT_CLASS::get_event_action(evt); \
  91. } \
  92. virtual bool get_action_event(const char *action, Event &evt) { \
  93. binding_entry *entry = bindings_table; \
  94. while (entry->action) { \
  95. if (!strcmp(entry->action, action)) { \
  96. evt = entry->evt; \
  97. return true; \
  98. } \
  99. entry++; \
  100. } \
  101. return PARENT_CLASS::get_action_event(action, evt); \
  102. }
  103. #define END_BINDINGS \
  104. { Event(), 0 }
  105. #endif