Mercurial > MadButterfly
annotate examples/calculator/main.c @ 92:3f619ae03678
Improve calcuator example program.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 24 Aug 2008 00:18:59 +0800 |
parents | bd29780bf266 |
children | c3c41f88e776 |
rev | line source |
---|---|
88 | 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) { | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
37 int r = v1; |
88 | 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 '/': | |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
50 if(v2) |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
51 r = v1 / v2; |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
52 else |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
53 r = v1; |
88 | 54 break; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
55 case 'n': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
56 r = v2; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
57 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
58 case 'N': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
59 break; |
88 | 60 } |
61 | |
62 return r; | |
63 } | |
64 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
65 static void show_text(ex_rt_t *ex_rt, int num, int saved, int op, |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
66 const char *suffix) { |
88 | 67 char buf[20]; |
68 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
69 sprintf(buf, "%d%s", num, suffix); |
88 | 70 sh_text_set_text(ex_rt->code->screen_text, buf); |
71 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
72 |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
73 if(op == 'n') |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
74 sprintf(buf, "None"); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
75 else |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
76 sprintf(buf, "%d%c", saved, op); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
77 sh_text_set_text(ex_rt->code->saved_text, buf); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
78 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->saved_text); |
88 | 79 } |
80 | |
81 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { | |
82 int i; | |
83 coord_t **coord_p; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
84 static int valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
85 static int factor = 1; |
88 | 86 static int num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
87 static int op = 'n'; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
88 static int saved = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
89 char buf[2] = " "; |
88 | 90 |
91 for(i = 0; i < 16; i++) { | |
92 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); | |
93 if(*coord_p == (void *)tgt) | |
94 break; | |
95 } | |
96 if(i >= 16) return; | |
97 | |
98 if(i < 10) { | |
99 num = num * 10 + i; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
100 show_text(ex_rt, num * factor, saved, op, ""); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
101 valid_num = 1; |
88 | 102 } else { |
103 switch(tgt_list[i].c) { | |
104 case 'c': | |
105 saved = num = 0; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
106 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
107 valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
108 op = 'n'; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
109 show_text(ex_rt, 0, saved, op, ""); |
88 | 110 break; |
111 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
112 case '-': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
113 if(!valid_num) { |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
114 factor *= -1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
115 valid_num = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
116 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
117 } |
88 | 118 case '+': |
119 case '*': | |
120 case '/': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
121 saved = real_compute(op, saved, num * factor); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
122 buf[0] = tgt_list[i].c; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
123 show_text(ex_rt, saved, saved, 'n', buf); |
90 | 124 op = tgt_list[i].c; |
88 | 125 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
126 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
127 valid_num = 0; |
88 | 128 break; |
129 | |
130 case '=': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
131 saved = real_compute(op, saved, num * factor); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
132 show_text(ex_rt, saved, 0, 'n', ""); |
88 | 133 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
134 op = 'N'; |
88 | 135 break; |
136 } | |
137 } | |
138 rdman_redraw_changed(ex_rt->rt->rdman); | |
139 } | |
140 | |
141 static void buttons_handler(event_t *evt, void *arg) { | |
142 ex_rt_t *ex_rt = (ex_rt_t *)arg; | |
143 | |
144 switch(evt->type) { | |
145 case EVT_MOUSE_BUT_PRESS: | |
146 compute(ex_rt, (coord_t *)evt->cur_tgt); | |
147 break; | |
148 } | |
149 } | |
150 | |
151 static void setup_observers(ex_rt_t *ex_rt) { | |
152 calculator_scr_t *calculator_scr; | |
153 ob_factory_t *factory; | |
154 subject_t *subject; | |
90 | 155 coord_t *coord; |
156 int off; | |
157 int i; | |
88 | 158 |
159 calculator_scr = ex_rt->code; | |
160 factory = rdman_get_ob_factory(ex_rt->rt->rdman); | |
161 | |
90 | 162 for(i = 0; i < 16; i++) { |
163 off = tgt_list[i].off; | |
164 coord = *(coord_t **)((void *)calculator_scr + off); | |
165 subject = coord_get_mouse_event(coord); | |
166 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
167 } | |
88 | 168 } |
169 | |
170 int main(int argc, char * const argv[]) { | |
171 X_MB_runtime_t rt; | |
172 calculator_scr_t *calculator_scr; | |
173 ex_rt_t ex_rt; | |
174 int r; | |
175 | |
176 r = X_MB_init(":0.0", 300, 400, &rt); | |
177 | |
178 calculator_scr = calculator_scr_new(rt.rdman); | |
179 | |
180 ex_rt.rt = &rt; | |
181 ex_rt.code = calculator_scr; | |
182 setup_observers(&ex_rt); | |
183 | |
184 X_MB_handle_connection(&rt); | |
185 | |
186 calculator_scr_free(calculator_scr); | |
187 X_MB_destroy(&rt); | |
188 | |
189 return 0; | |
190 } |