Mercurial > MadButterfly
comparison examples/dynamic/main.c @ 221:ad4f8a956505
Implement a workaround for the button class. However, this won't solve all issues. We can use this as example to fix the mouse out event issue. When we move the curosr over the text inside the button. The upper layer group will receive MOUSE_OUT events. This is absolute incorrect.
author | wycc |
---|---|
date | Sun, 14 Dec 2008 12:35:13 +0800 |
parents | 8d9d717c9300 |
children | 29e1b2bffe4c |
comparison
equal
deleted
inserted
replaced
219:1eb9ee5ae4f2 | 221:ad4f8a956505 |
---|---|
32 coord_t *active; | 32 coord_t *active; |
33 coord_t *normal; | 33 coord_t *normal; |
34 coord_t *click; | 34 coord_t *click; |
35 void (*press)(); | 35 void (*press)(); |
36 void *arg; | 36 void *arg; |
37 observer_t *obs_move,*obs_out,*obs_press; | |
37 } mb_button_t; | 38 } mb_button_t; |
38 | 39 |
39 | 40 |
40 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group) | 41 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(en->rdman, group) |
41 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(en->rdman, group) | 42 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(en->rdman, group) |
42 | 43 |
43 #define CMOUSE(e) (coord_get_mouse_event(e)) | 44 #define CMOUSE(e) (coord_get_mouse_event(e)) |
44 | 45 |
45 | 46 |
47 static void mb_button_pressed(event_t *evt, void *arg); | |
48 static void mb_button_out(event_t *evt, void *arg); | |
46 | 49 |
47 static void mb_button_move(event_t *evt, void *arg) | 50 static void mb_button_move(event_t *evt, void *arg) |
48 { | 51 { |
49 mb_button_t *btn = (mb_button_t *) arg; | 52 mb_button_t *btn = (mb_button_t *) arg; |
50 engine_t *en = btn->en; | 53 engine_t *en = btn->en; |
51 | 54 |
52 | 55 |
53 printf("Mouse move\n"); | 56 printf("Mouse move\n"); |
54 COORD_SHOW(btn->active); | 57 COORD_SHOW(btn->active); |
58 COORD_HIDE(btn->normal); | |
55 rdman_coord_changed(btn->en->rdman,btn->root); | 59 rdman_coord_changed(btn->en->rdman,btn->root); |
56 rdman_redraw_changed(btn->en->rdman); | 60 rdman_redraw_changed(btn->en->rdman); |
61 subject_remove_observer(CMOUSE(btn->normal), btn->obs_move); | |
62 btn->obs_press = subject_add_event_observer(CMOUSE(btn->active), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn); | |
63 btn->obs_out = subject_add_event_observer(CMOUSE(btn->active), EVT_MOUSE_OUT, mb_button_out,btn); | |
57 } | 64 } |
58 static void mb_button_out(event_t *evt, void *arg) | 65 static void mb_button_out(event_t *evt, void *arg) |
59 { | 66 { |
60 mb_button_t *btn = (mb_button_t *) arg; | 67 mb_button_t *btn = (mb_button_t *) arg; |
61 engine_t *en = btn->en; | 68 engine_t *en = btn->en; |
62 | 69 |
63 printf("mouse out\n"); | 70 printf("mouse out\n"); |
64 COORD_HIDE(btn->active); | 71 COORD_HIDE(btn->active); |
72 COORD_SHOW(btn->normal); | |
65 rdman_coord_changed(btn->en->rdman,btn->root); | 73 rdman_coord_changed(btn->en->rdman,btn->root); |
66 rdman_redraw_changed(btn->en->rdman); | 74 rdman_redraw_changed(btn->en->rdman); |
67 } | 75 subject_remove_observer(CMOUSE(btn->active), btn->obs_press); |
68 | 76 subject_remove_observer(CMOUSE(btn->active), btn->obs_out); |
69 void mb_button_show_active(const mb_timeval_t *tmo, const mb_timeval_t *now, void *arg) | 77 btn->obs_move = subject_add_event_observer(CMOUSE(btn->normal), EVT_MOUSE_MOVE, mb_button_move,btn); |
78 } | |
79 | |
80 void mb_button_show_active(event_t *evt, void *arg) | |
70 { | 81 { |
71 mb_button_t *btn = (mb_button_t *) arg; | 82 mb_button_t *btn = (mb_button_t *) arg; |
72 engine_t *en = btn->en; | 83 engine_t *en = btn->en; |
73 | 84 |
74 COORD_HIDE(btn->click); | 85 COORD_HIDE(btn->click); |
86 COORD_SHOW(btn->active); | |
75 rdman_coord_changed(btn->en->rdman,btn->root); | 87 rdman_coord_changed(btn->en->rdman,btn->root); |
76 rdman_redraw_changed(btn->en->rdman); | 88 rdman_redraw_changed(btn->en->rdman); |
89 subject_remove_observer(CMOUSE(btn->click), btn->obs_press); | |
90 btn->obs_press = subject_add_event_observer(CMOUSE(btn->active), EVT_MOUSE_BUT_PRESS, mb_button_pressed,btn); | |
91 btn->obs_out = subject_add_event_observer(CMOUSE(btn->active), EVT_MOUSE_OUT, mb_button_out,btn); | |
77 } | 92 } |
78 | 93 |
79 void mb_button_pressed(event_t *evt, void *arg) | 94 void mb_button_pressed(event_t *evt, void *arg) |
80 { | 95 { |
81 mb_button_t *btn = (mb_button_t *) arg; | 96 mb_button_t *btn = (mb_button_t *) arg; |
82 engine_t *en = btn->en; | 97 engine_t *en = btn->en; |
83 mb_timeval_t now,to; | 98 mb_timeval_t now,to; |
84 | 99 |
85 printf("Pressed\n"); | 100 printf("Pressed\n"); |
86 COORD_SHOW(btn->click); | 101 COORD_SHOW(btn->click); |
102 COORD_HIDE(btn->active); | |
87 rdman_coord_changed(en->rdman,en->button->root_coord); | 103 rdman_coord_changed(en->rdman,en->button->root_coord); |
88 rdman_redraw_changed(en->rdman); | 104 rdman_redraw_changed(en->rdman); |
89 get_now(&now); | 105 get_now(&now); |
90 MB_TIMEVAL_SET(&to, 0, 500); | 106 MB_TIMEVAL_SET(&to, 0, 500); |
91 MB_TIMEVAL_ADD(&to,&now); | 107 MB_TIMEVAL_ADD(&to,&now); |
92 | 108 subject_remove_observer(CMOUSE(btn->active), btn->obs_press); |
93 mb_tman_timeout(X_MB_tman(btn->en->rt), &to, mb_button_show_active, arg); | 109 subject_remove_observer(CMOUSE(btn->active), btn->obs_out); |
110 btn->obs_press = subject_add_event_observer(CMOUSE(btn->click), EVT_MOUSE_BUT_RELEASE, mb_button_show_active,btn); | |
111 btn->obs_out = NULL; | |
94 } | 112 } |
95 mb_button_t *mb_button_new(engine_t *en,mb_sprite_t *sp, char *name) | 113 mb_button_t *mb_button_new(engine_t *en,mb_sprite_t *sp, char *name) |
96 { | 114 { |
97 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t)); | 115 mb_button_t *btn = (mb_button_t *) malloc(sizeof(mb_button_t)); |
98 char *buf = (char *) malloc(strlen(name)+5); | 116 char *buf = (char *) malloc(strlen(name)+5); |
115 } | 133 } |
116 btn->press = NULL; | 134 btn->press = NULL; |
117 // Show only the normal button | 135 // Show only the normal button |
118 COORD_HIDE(btn->active); | 136 COORD_HIDE(btn->active); |
119 COORD_HIDE(btn->click); | 137 COORD_HIDE(btn->click); |
138 COORD_SHOW(btn->normal); | |
120 // Move to the same position | 139 // Move to the same position |
121 btn->active->matrix[2] = 200; | 140 btn->active->matrix[2] = 200; |
122 btn->active->matrix[5] = 200; | 141 btn->active->matrix[5] = 200; |
123 btn->normal->matrix[2] = 200; | 142 btn->normal->matrix[2] = 200; |
124 btn->normal->matrix[5] = 200; | 143 btn->normal->matrix[5] = 200; |
125 btn->click->matrix[2] = 200; | 144 btn->click->matrix[2] = 200; |
126 btn->click->matrix[5] = 200; | 145 btn->click->matrix[5] = 200; |
127 btn->en = en; | 146 btn->en = en; |
128 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_MOVE, mb_button_move,btn); | 147 btn->obs_move = subject_add_event_observer(CMOUSE(btn->normal), EVT_MOUSE_MOVE, mb_button_move,btn); |
129 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_OUT, mb_button_out,btn); | 148 btn->obs_out = NULL; |
130 subject_add_event_observer(CMOUSE(btn->root), EVT_MOUSE_BUT_RELEASE, mb_button_pressed,btn); | 149 btn->obs_press = NULL; |
150 rdman_coord_changed(en->rdman,en->button->root_coord); | |
151 rdman_redraw_changed(en->rdman); | |
131 return btn; | 152 return btn; |
132 } | 153 } |
133 | 154 |
134 | 155 |
135 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg) | 156 void mb_button_add_onClick(mb_button_t *b, void (*h)(void *arg), void *arg) |
148 en->rt = rt; | 169 en->rt = rt; |
149 en->rdman = X_MB_rdman(rt); | 170 en->rdman = X_MB_rdman(rt); |
150 return en; | 171 return en; |
151 } | 172 } |
152 | 173 |
153 void engine_close(engine_t *en) | 174 void engine_mainloop(engine_t *en) |
154 { | 175 { |
155 /* | 176 /* |
156 * Start handle connections, includes one to X server. | 177 * Start handle connections, includes one to X server. |
157 * User start to interact with the application. | 178 * User start to interact with the application. |
158 */ | 179 */ |
267 */ | 288 */ |
268 subject = coord_get_mouse_event(en->menu->rect); | 289 subject = coord_get_mouse_event(en->menu->rect); |
269 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, add_rect, en); | 290 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, add_rect, en); |
270 | 291 |
271 | 292 |
272 engine_close(en); | 293 engine_mainloop(en); |
273 | 294 |
274 return 0; | 295 return 0; |
275 } | 296 } |
276 | 297 |
277 /* vim: set ts=4 */ | 298 /* vim: set ts=4 */ |