Mercurial > MadButterfly
annotate include/mb_observer.h @ 201:31933f9ee70e
Chkec in demo for dynamic rectangle creation and button.
author | wycc@wycc-desktop |
---|---|
date | Sun, 07 Dec 2008 18:00:06 +0800 |
parents | f9d507a3e1d9 |
children | 748896358da2 |
rev | line source |
---|---|
73 | 1 #ifndef __OBSERVER_H_ |
2 #define __OBSERVER_H_ | |
3 | |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
185
diff
changeset
|
4 #include "mb_tools.h" |
73 | 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 { | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
14 int type; /*!< event type (a.k.a. EVT_* */ |
73 | 15 subject_t *tgt, *cur_tgt; |
16 }; | |
17 | |
78 | 18 /*! \brief Observer of observer pattern. |
19 * | |
20 * A target for receiving events. | |
21 */ | |
73 | 22 struct _observer { |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
23 int type; |
73 | 24 evt_handler hdr; |
25 void *arg; | |
26 observer_t *next; | |
27 }; | |
28 | |
78 | 29 /*! \brief Subject of observer pattern. |
30 * | |
31 * Observer is a pattern to decouple caller and callee, | |
32 * especial for multiple callee. | |
33 * \see http://en.wikipedia.org/wiki/Observer_pattern | |
34 */ | |
73 | 35 struct _subject { |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
36 int obj_type; /*!< \brief type of object (a.k.a. OBJT_*). */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
37 void *obj; /*!< \brief the object this subject for. */ |
73 | 38 int flags; |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
39 ob_factory_t *factory; |
73 | 40 STAILQ(observer_t) observers; |
41 }; | |
78 | 42 /*! \brief Flag that make a subject to propagate events to parents. */ |
73 | 43 #define SUBF_STOP_PROPAGATE 0x1 |
125
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
44 #define SUBF_BUSY 0x2 /*!< \brief in subject_notify() */ |
1c1f28c124c9
Postponding free request when a subject is in subject_notify
Thinker K.F. Li <thinker@branda.to>
parents:
123
diff
changeset
|
45 #define SUBF_FREE 0x4 /*!< \brief in postponding subject_free() */ |
73 | 46 |
154 | 47 enum {OBJT_GEO, OBJT_COORD, OBJT_KB, OBJT_PROGM, OBJT_RDMAN}; |
73 | 48 |
49 struct _mouse_event { | |
50 event_t event; | |
51 int x, y; | |
78 | 52 unsigned int but_state; |
53 unsigned int button; | |
73 | 54 }; |
55 | |
76 | 56 #define MOUSE_BUT1 0x1 |
57 #define MOUSE_BUT2 0x2 | |
58 #define MOUSE_BUT3 0x4 | |
59 | |
73 | 60 /*! \brief Observer factory. |
61 * | |
62 * It provides functions for allocation of subject and observer objects, | |
63 * and strategy function for getting the subject of parent coord object. | |
64 */ | |
65 struct _ob_factory { | |
66 subject_t *(*subject_alloc)(ob_factory_t *factory); | |
67 void (*subject_free)(ob_factory_t *factory, subject_t *subject); | |
68 observer_t *(*observer_alloc)(ob_factory_t *factory); | |
69 void (*observer_free)(ob_factory_t *factory, observer_t *observer); | |
70 /*! This is a strategy function to get subjects of parents. */ | |
71 subject_t *(*get_parent_subject)(ob_factory_t *factory, | |
72 subject_t *cur_subject); | |
73 }; | |
74 | |
198
f9d507a3e1d9
Add event observer which listen to one event type only.
wycc@wycc-desktop
parents:
192
diff
changeset
|
75 enum {EVT_ANY,EVT_MOUSE_OVER, EVT_MOUSE_OUT, EVT_MOUSE_MOVE, |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
78
diff
changeset
|
76 EVT_MOUSE_BUT_PRESS, EVT_MOUSE_BUT_RELEASE, |
154 | 77 EVT_KB_PRESS, EVT_KB_RELEASE, EVT_PROGM_COMPLETE, |
78 EVT_RDMAN_REDRAW }; | |
73 | 79 |
80 extern subject_t *subject_new(ob_factory_t *factory, | |
81 void *obj, int obj_type); | |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
82 extern void subject_free(subject_t *subject); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
83 extern void subject_notify(subject_t *subject, event_t *evt); |
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
84 extern observer_t *subject_add_observer(subject_t *subject, |
73 | 85 evt_handler hdr, void *arg); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
86 extern void subject_remove_observer(subject_t *subject, |
73 | 87 observer_t *observer); |
88 | |
89 | |
90 #endif /* __OBSERVER_H_ */ |