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