comparison src/observer.h @ 73:9ab15ebc9061

Observer for mouse events
author Thinker K.F. Li <thinker@branda.to>
date Mon, 18 Aug 2008 01:59:26 +0800
parents
children 8706356a61b4
comparison
equal deleted inserted replaced
72:171a8cb7e4b5 73:9ab15ebc9061
1 #ifndef __OBSERVER_H_
2 #define __OBSERVER_H_
3
4 #include "tools.h"
5
6 typedef struct _event event_t;
7 typedef struct _observer observer_t;
8 typedef struct _subject subject_t;
9 typedef struct _mouse_event mouse_event_t;
10 typedef struct _ob_factory ob_factory_t;
11 typedef void (*evt_handler)(event_t *event, void *arg);
12
13 struct _event {
14 int type;
15 subject_t *tgt, *cur_tgt;
16 };
17
18 struct _observer {
19 evt_handler hdr;
20 void *arg;
21 observer_t *next;
22 };
23
24 struct _subject {
25 int obj_type;
26 void *obj;
27 int flags;
28 STAILQ(observer_t) observers;
29 };
30 #define SUBF_STOP_PROPAGATE 0x1
31
32 enum {OBJT_GEO, OBJT_COORD};
33
34 struct _mouse_event {
35 event_t event;
36 int x, y;
37 int button;
38 };
39
40 /*! \brief Observer factory.
41 *
42 * It provides functions for allocation of subject and observer objects,
43 * and strategy function for getting the subject of parent coord object.
44 */
45 struct _ob_factory {
46 subject_t *(*subject_alloc)(ob_factory_t *factory);
47 void (*subject_free)(ob_factory_t *factory, subject_t *subject);
48 observer_t *(*observer_alloc)(ob_factory_t *factory);
49 void (*observer_free)(ob_factory_t *factory, observer_t *observer);
50 /*! This is a strategy function to get subjects of parents. */
51 subject_t *(*get_parent_subject)(ob_factory_t *factory,
52 subject_t *cur_subject);
53 };
54
55 enum {EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE,
56 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE};
57
58 extern subject_t *subject_new(ob_factory_t *factory,
59 void *obj, int obj_type);
60 extern void subject_free(ob_factory_t *factory, subject_t *subject);
61 extern void subject_notify(ob_factory_t *factory,
62 subject_t *subject, event_t *evt);
63 extern observer_t *subject_add_observer(ob_factory_t *factory,
64 subject_t *subject,
65 evt_handler hdr, void *arg);
66 extern void subject_remove_observer(ob_factory_t *factory,
67 subject_t *subject,
68 observer_t *observer);
69
70
71 #endif /* __OBSERVER_H_ */