Mercurial > MadButterfly
comparison examples/drag/main.c @ 199:55533146efdf
Add drag example
author | wycc@wycc-desktop |
---|---|
date | Sat, 06 Dec 2008 15:40:23 +0800 |
parents | |
children | 09ad4382aa41 |
comparison
equal
deleted
inserted
replaced
198:f9d507a3e1d9 | 199:55533146efdf |
---|---|
1 /*! \file | |
2 * | |
3 * svg2code_ex is an example that show programmers how to create a | |
4 * menu with MadButterfly. | |
5 * | |
6 */ | |
7 #include <stdio.h> | |
8 #include <mb.h> | |
9 #include "menu.h" | |
10 | |
11 typedef struct _engine engine_t; | |
12 struct _engine { | |
13 X_MB_runtime_t *rt; | |
14 redraw_man_t *rdman; | |
15 menu_t *menu; | |
16 int state; | |
17 co_aix orx,ory; | |
18 int start_x,start_y; | |
19 }; | |
20 engine_t *engine_init() | |
21 { | |
22 | |
23 X_MB_runtime_t *rt; | |
24 rt = X_MB_new(":0.0", 800, 600); | |
25 engine_t *en = (engine_t *) malloc(sizeof(engine_t)); | |
26 | |
27 en->rt = rt; | |
28 en->rdman = X_MB_rdman(rt); | |
29 return en; | |
30 } | |
31 void engine_close(engine_t *en) | |
32 { | |
33 /* | |
34 * Start handle connections, includes one to X server. | |
35 * User start to interact with the application. | |
36 */ | |
37 X_MB_handle_connection(en->rt); | |
38 | |
39 /* | |
40 * Clean | |
41 */ | |
42 menu_free(en->menu); | |
43 X_MB_free(en->rt); | |
44 free(en); | |
45 } | |
46 #define COORD_SHOW(group) coord_show(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group) | |
47 #define COORD_HIDE(group) coord_hide(group);rdman_coord_changed(X_MB_rdman(ex_rt->rt), group) | |
48 | |
49 | |
50 void coord_move(coord_t *c, co_aix x, co_aix y) | |
51 { | |
52 c->matrix[2] = x; | |
53 c->matrix[5] = y; | |
54 } | |
55 | |
56 | |
57 static void cursor_press_handler(event_t *evt, void *arg) { | |
58 engine_t *en = (engine_t *) arg; | |
59 mouse_event_t *mev = (mouse_event_t *) evt; | |
60 | |
61 en->start_x = mev->x; | |
62 en->start_y = mev->y; | |
63 en->orx = en->menu->cursor->matrix[2]; | |
64 en->ory = en->menu->cursor->matrix[5]; | |
65 printf("pressed %g %g\n",en->orx,en->ory); | |
66 en->state = 1; | |
67 } | |
68 | |
69 static void cursor_release_handler(event_t *evt, void *arg) { | |
70 engine_t *en = (engine_t *) arg; | |
71 printf("up\n"); | |
72 en->state = 0; | |
73 } | |
74 | |
75 static void cursor_move_handler(event_t *evt, void *arg) { | |
76 engine_t *en = (engine_t *) arg; | |
77 mouse_event_t *mev = (mouse_event_t *) evt; | |
78 | |
79 if (en->state) { | |
80 printf("move to (%d %d)\n", mev->x,mev->y); | |
81 coord_move(en->menu->cursor,en->orx + (mev->x-en->start_x),en->ory + (mev->y-en->start_y)); | |
82 rdman_coord_changed(en->rdman, en->menu->cursor); | |
83 /* Update changed part to UI. */ | |
84 rdman_redraw_changed(en->rdman); | |
85 } | |
86 } | |
87 | |
88 int main(int argc, char * const argv[]) { | |
89 subject_t *subject; | |
90 engine_t *en; | |
91 | |
92 en = engine_init(); | |
93 en->menu = menu_new(en->rdman, en->rdman->root_coord); | |
94 | |
95 /* | |
96 * Register observers to subjects of events for objects. | |
97 */ | |
98 subject = coord_get_mouse_event(en->menu->cursor); | |
99 subject_add_event_observer(subject, EVT_MOUSE_BUT_PRESS, cursor_press_handler, en); | |
100 subject_add_event_observer(subject, EVT_MOUSE_BUT_RELEASE, cursor_release_handler, en); | |
101 subject_add_event_observer(subject, EVT_MOUSE_MOVE, cursor_move_handler, en); | |
102 | |
103 | |
104 engine_close(en); | |
105 | |
106 return 0; | |
107 } | |
108 | |
109 /* vim: set ts=4 */ |