summaryrefslogtreecommitdiff
path: root/dispatcher.h
blob: 77b1786449fe0110a162fb14aa9d8b0c8dfff0bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
//
// This program 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; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

#ifndef BDE_DISPATCHER_H
#define BDE_DISPATCHER_H

#include "event.h"

#include <string.h>

// class Dispatcher represents a class that receives GUI events.

#define INTERACTIVE

struct binding_entry {
    Event evt;
    const char *action;
};

class Dispatcher {
    
public:

    Dispatcher() { }

    virtual bool do_action(const char *action) {
	return false;
    }

    virtual const char *get_event_action(const Event &evt) {
	return NULL;
    }
    
    virtual bool get_action_event(const char *action, Event &evt) {
	return false;
    }
    
    virtual const char *get_action_description(const char *action) {
	return NULL;
    }

    virtual bool handle_event(const Event &evt) {
	return do_action(get_event_action(evt));
    }
};

// :TODO: I should use STL's map instead of a simple linear search.

#define HAS_ACTIONS_MAP(CLASS, PARENT_CLASS)    \
    typedef void (CLASS::*method_ptr)();    \
    struct action_entry {		    \
	const char *action;		    \
	method_ptr method;		    \
	const char *short_description;	    \
    };					    \
    static action_entry actions_table[];    \
    virtual bool do_action(const char *action) {    \
	if (!action) return false;		    \
	action_entry *entry = actions_table;	    \
	while (entry->action) {			    \
	    if (!strcmp(entry->action, action)) {   \
		(this->*(entry->method))();	    \
		return true;			    \
	    }					    \
	    entry++;				    \
	}					    \
	return PARENT_CLASS::do_action(action);	    \
    }						    \
    virtual const char *get_action_description	    \
    (const char *action) {			    \
	if (!action) return NULL;		    \
	action_entry *entry = actions_table;	    \
	while (entry->action) {			    \
	    if (!strcmp(entry->action, action))	    \
		return entry->short_description;    \
	    entry++;				    \
	}					    \
	return PARENT_CLASS::			    \
	    get_action_description(action);	    \
    }

#define ADD_ACTION(CLASS, METHOD, DESC)	\
 { #METHOD, &CLASS::METHOD, DESC }

#define END_ACTIONS \
 { 0, 0 }

#define HAS_BINDINGS_MAP(CLASS, PARENT_CLASS)		    	\
    static binding_entry bindings_table[];		    	\
    virtual const char *get_event_action(const Event &evt) {	\
	binding_entry *entry = bindings_table;			\
	while (entry->action) {					\
	    if (entry->evt == evt)				\
		return entry->action;				\
	    entry++;						\
	}							\
	return PARENT_CLASS::get_event_action(evt);		\
    } \
    virtual bool get_action_event(const char *action, Event &evt) { \
	binding_entry *entry = bindings_table;			\
	while (entry->action) {					\
	    if (!strcmp(entry->action, action))	{		\
		evt = entry->evt;				\
		return true;					\
	    }							\
	    entry++;						\
	}							\
	return PARENT_CLASS::get_action_event(action, evt);	\
    }

#define END_BINDINGS \
 { Event(), 0 }

#endif