comparison src/mouse.c @ 231:2637519e2bd7

Move mouse event handler and interpreter to src/mouse.c. - Also change makefiles of examples with a better configuration.
author Thinker K.F. Li <thinker@branda.to>
date Thu, 18 Dec 2008 22:37:15 +0800
parents
children 65cabbdd5284
comparison
equal deleted inserted replaced
230:3e6da6f6a226 231:2637519e2bd7
1 #include "mb_types.h"
2 #include "mb_redraw_man.h"
3
4 #define ASSERT(x)
5
6 static void mouse_event_interpreter(event_t *evt, void *arg) {
7 mouse_event_t *mevt = (mouse_event_t *)evt;
8 redraw_man_t *rdman = (redraw_man_t *)arg;
9 mb_obj_t *obj;
10 mouse_event_t new_evt;
11 coord_t *coord;
12 shape_t *shape;
13
14 ASSERT(evt->type == EVT_MOUSE_MOVE_RAW);
15
16 obj = (mb_obj_t *)subject_get_object(evt->cur_tgt);
17 if(rdman->last_mouse_over == obj) {
18 evt->type = EVT_MOUSE_MOVE;
19 return;
20 }
21
22 new_evt.x = mevt->x;
23 new_evt.y = mevt->y;
24 new_evt.but_state = mevt->but_state;
25 new_evt.button = mevt->button;
26
27 if(rdman->last_mouse_over != NULL) {
28 new_evt.event.type = EVT_MOUSE_OUT;
29 if(IS_MBO_COORD(rdman->last_mouse_over)) {
30 coord = (coord_t *)rdman->last_mouse_over;
31 subject_notify(coord->mouse_event, (event_t *)&new_evt);
32 } else if(IS_MBO_SHAPES(rdman->last_mouse_over)) {
33 shape = (shape_t *)rdman->last_mouse_over;
34 ASSERT(shape->geo != NULL);
35 subject_notify(shape->geo->mouse_event, (event_t *)&new_evt);
36 }
37 }
38
39 new_evt.event.type = EVT_MOUSE_OVER;
40 subject_notify(evt->cur_tgt, (event_t *)&new_evt);
41 rdman->last_mouse_over = obj;
42
43 evt->flags |= EVTF_STOP_NOTIFY;
44 }
45
46 /*! \brief This is event handler that observes addrm_monitor subject.
47 *
48 * addrm_monitor subject is a member of redraw manager objects.
49 * Monitor of mouse event subjects of mb_obj_t objects are set to this
50 * subject by redraw manager.
51 *
52 * addrm_monitor_hdlr() monitor adding and removing observers of mouse
53 * event subjects, and install special observers to these subjects to handle
54 * and interpret mouse events (EVT_MOUSE_MOVE_RAW).
55 */
56 void addrm_monitor_hdlr(event_t *evt, void *arg) {
57 monitor_event_t *mevt;
58 redraw_man_t *rdman;
59 mb_obj_t *obj;
60 mb_prop_store_t *props;
61 observer_t *observer;
62 int cnt = 0;
63
64 mevt = (monitor_event_t *)evt;
65 rdman = (redraw_man_t *)evt->tgt;
66 obj = (mb_obj_t *)subject_get_object(mevt->subject);
67 props = mb_obj_prop_store(obj);
68
69 switch(evt->type) {
70 case EVT_MONITOR_ADD:
71 if(!mb_prop_has(props, PROP_MEVT_OB_CNT))
72 cnt = 0;
73 else
74 cnt = (int)mb_prop_get(props, PROP_MEVT_OB_CNT);
75
76 cnt++;
77 mb_prop_set(props, PROP_MEVT_OB_CNT, (void *)cnt);
78 if(cnt == 1) {
79 observer =
80 subject_add_event_observer_head(mevt->subject,
81 EVT_MOUSE_MOVE_RAW,
82 mouse_event_interpreter,
83 rdman);
84 ASSERT(observer != NULL);
85 mb_prop_set(props, PROP_MEVT_OBSERVER, observer);
86 }
87 break;
88
89 case EVT_MONITOR_REMOVE:
90 cnt = (int)mb_prop_get(props, PROP_MEVT_OB_CNT);
91 cnt--;
92 mb_prop_set(props, PROP_MEVT_OB_CNT, (void *)cnt);
93 if(cnt == 1) {
94 observer = (observer_t *)mb_prop_get(props, PROP_MEVT_OBSERVER);
95 subject_remove_observer(mevt->subject, observer);
96 mb_prop_del(props, PROP_MEVT_OBSERVER);
97 }
98 break;
99
100 case EVT_MONITOR_FREE:
101 break;
102 }
103 }
104