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