Mercurial > MadButterfly
comparison examples/calculator/main.c @ 88:dd813dcc232c
New example, calculator.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 23 Aug 2008 15:26:47 +0800 |
parents | |
children | 90428161fc61 |
comparison
equal
deleted
inserted
replaced
87:8dfebf76cb59 | 88:dd813dcc232c |
---|---|
1 #include <stdio.h> | |
2 #include <mb_types.h> | |
3 #include <X_supp.h> | |
4 #include <shapes.h> | |
5 #include <tools.h> | |
6 #include "calculator_scr.h" | |
7 | |
8 typedef struct _ex_rt ex_rt_t; | |
9 struct _ex_rt { | |
10 X_MB_runtime_t *rt; | |
11 calculator_scr_t *code; | |
12 }; | |
13 | |
14 static struct { | |
15 int c; | |
16 int off; | |
17 } tgt_list[] = { | |
18 { 0, OFFSET(calculator_scr_t, but_0) }, | |
19 { 1, OFFSET(calculator_scr_t, but_1) }, | |
20 { 2, OFFSET(calculator_scr_t, but_2) }, | |
21 { 3, OFFSET(calculator_scr_t, but_3) }, | |
22 { 4, OFFSET(calculator_scr_t, but_4) }, | |
23 { 5, OFFSET(calculator_scr_t, but_5) }, | |
24 { 6, OFFSET(calculator_scr_t, but_6) }, | |
25 { 7, OFFSET(calculator_scr_t, but_7) }, | |
26 { 8, OFFSET(calculator_scr_t, but_8) }, | |
27 { 9, OFFSET(calculator_scr_t, but_9) }, | |
28 { '+', OFFSET(calculator_scr_t, but_add) }, | |
29 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
30 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
31 { '/', OFFSET(calculator_scr_t, but_div) }, | |
32 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
33 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
34 }; | |
35 | |
36 static int real_compute(int op, int v1, int v2) { | |
37 int r = v2; | |
38 | |
39 switch(op) { | |
40 case '+': | |
41 r = v1 + v2; | |
42 break; | |
43 case '-': | |
44 r = v1 - v2; | |
45 break; | |
46 case '*': | |
47 r = v1 * v2; | |
48 break; | |
49 case '/': | |
50 r = v1 / v2; | |
51 break; | |
52 } | |
53 | |
54 return r; | |
55 } | |
56 | |
57 static void show_text(ex_rt_t *ex_rt, int num) { | |
58 char buf[20]; | |
59 | |
60 sprintf(buf, "%d", num); | |
61 sh_text_set_text(ex_rt->code->screen_text, buf); | |
62 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); | |
63 } | |
64 | |
65 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { | |
66 int i; | |
67 coord_t **coord_p; | |
68 static int num = 0; | |
69 static int saved = 0; | |
70 static int op = 0; | |
71 | |
72 for(i = 0; i < 16; i++) { | |
73 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); | |
74 if(*coord_p == (void *)tgt) | |
75 break; | |
76 } | |
77 if(i >= 16) return; | |
78 | |
79 if(i < 10) { | |
80 num = num * 10 + i; | |
81 show_text(ex_rt, num); | |
82 } else { | |
83 switch(tgt_list[i].c) { | |
84 case 'c': | |
85 saved = num = 0; | |
86 show_text(ex_rt, 0); | |
87 break; | |
88 | |
89 case '+': | |
90 saved = real_compute(op, saved, num); | |
91 show_text(ex_rt, saved); | |
92 op = '+'; | |
93 num = 0; | |
94 break; | |
95 | |
96 case '-': | |
97 saved = real_compute(op, saved, num); | |
98 show_text(ex_rt, saved); | |
99 op = '-'; | |
100 num = 0; | |
101 break; | |
102 | |
103 case '*': | |
104 saved = real_compute(op, saved, num); | |
105 show_text(ex_rt, saved); | |
106 op = '*'; | |
107 num = 0; | |
108 break; | |
109 | |
110 case '/': | |
111 saved = real_compute(op, saved, num); | |
112 show_text(ex_rt, saved); | |
113 op = '/'; | |
114 num = 0; | |
115 break; | |
116 | |
117 case '=': | |
118 saved = real_compute(op, saved, num); | |
119 show_text(ex_rt, saved); | |
120 num = 0; | |
121 break; | |
122 } | |
123 } | |
124 rdman_redraw_changed(ex_rt->rt->rdman); | |
125 } | |
126 | |
127 static void buttons_handler(event_t *evt, void *arg) { | |
128 ex_rt_t *ex_rt = (ex_rt_t *)arg; | |
129 | |
130 switch(evt->type) { | |
131 case EVT_MOUSE_BUT_PRESS: | |
132 compute(ex_rt, (coord_t *)evt->cur_tgt); | |
133 break; | |
134 } | |
135 } | |
136 | |
137 static void setup_observers(ex_rt_t *ex_rt) { | |
138 calculator_scr_t *calculator_scr; | |
139 ob_factory_t *factory; | |
140 subject_t *subject; | |
141 | |
142 calculator_scr = ex_rt->code; | |
143 factory = rdman_get_ob_factory(ex_rt->rt->rdman); | |
144 | |
145 subject = coord_get_mouse_event(calculator_scr->but_0); | |
146 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
147 subject = coord_get_mouse_event(calculator_scr->but_1); | |
148 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
149 subject = coord_get_mouse_event(calculator_scr->but_2); | |
150 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
151 subject = coord_get_mouse_event(calculator_scr->but_3); | |
152 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
153 subject = coord_get_mouse_event(calculator_scr->but_4); | |
154 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
155 subject = coord_get_mouse_event(calculator_scr->but_5); | |
156 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
157 subject = coord_get_mouse_event(calculator_scr->but_6); | |
158 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
159 subject = coord_get_mouse_event(calculator_scr->but_7); | |
160 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
161 subject = coord_get_mouse_event(calculator_scr->but_8); | |
162 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
163 subject = coord_get_mouse_event(calculator_scr->but_9); | |
164 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
165 subject = coord_get_mouse_event(calculator_scr->but_add); | |
166 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
167 subject = coord_get_mouse_event(calculator_scr->but_minus); | |
168 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
169 subject = coord_get_mouse_event(calculator_scr->but_mul); | |
170 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
171 subject = coord_get_mouse_event(calculator_scr->but_div); | |
172 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
173 subject = coord_get_mouse_event(calculator_scr->but_eq); | |
174 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
175 subject = coord_get_mouse_event(calculator_scr->but_clr); | |
176 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
177 } | |
178 | |
179 int main(int argc, char * const argv[]) { | |
180 X_MB_runtime_t rt; | |
181 calculator_scr_t *calculator_scr; | |
182 ex_rt_t ex_rt; | |
183 int r; | |
184 | |
185 r = X_MB_init(":0.0", 300, 400, &rt); | |
186 | |
187 calculator_scr = calculator_scr_new(rt.rdman); | |
188 | |
189 ex_rt.rt = &rt; | |
190 ex_rt.code = calculator_scr; | |
191 setup_observers(&ex_rt); | |
192 | |
193 X_MB_handle_connection(&rt); | |
194 | |
195 calculator_scr_free(calculator_scr); | |
196 X_MB_destroy(&rt); | |
197 | |
198 return 0; | |
199 } |