Mercurial > MadButterfly
annotate examples/calculator/main.c @ 94:57f8fecca8f3
-
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 24 Aug 2008 08:11:16 +0800 |
parents | c3c41f88e776 |
children | 565b55508c8d |
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 | |
94 | 8 typedef struct _calc_data calc_data_t; |
9 struct _calc_data { | |
88 | 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 | |
94 | 64 static void show_text(calc_data_t *calc_data, int num, int saved, int op, |
92
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); |
94 | 69 sh_text_set_text(calc_data->code->screen_text, buf); |
70 rdman_shape_changed(calc_data->rt->rdman, calc_data->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); |
94 | 76 sh_text_set_text(calc_data->code->saved_text, buf); |
77 rdman_shape_changed(calc_data->rt->rdman, calc_data->code->saved_text); | |
88 | 78 } |
79 | |
94 | 80 static void compute(calc_data_t *calc_data, coord_t *tgt) { |
88 | 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++) { | |
94 | 91 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
88 | 92 if(*coord_p == (void *)tgt) |
93 break; | |
94 } | |
95 if(i >= 16) return; | |
96 | |
97 if(i < 10) { | |
98 num = num * 10 + i; | |
94 | 99 show_text(calc_data, num * factor, saved, op, ""); |
92
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'; |
94 | 108 show_text(calc_data, 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; |
94 | 122 show_text(calc_data, 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); |
94 | 131 show_text(calc_data, 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 } | |
94 | 137 rdman_redraw_changed(calc_data->rt->rdman); |
88 | 138 } |
139 | |
140 static void buttons_handler(event_t *evt, void *arg) { | |
94 | 141 calc_data_t *calc_data = (calc_data_t *)arg; |
88 | 142 |
143 switch(evt->type) { | |
144 case EVT_MOUSE_BUT_PRESS: | |
94 | 145 compute(calc_data, (coord_t *)evt->cur_tgt); |
88 | 146 break; |
147 } | |
148 } | |
149 | |
94 | 150 static void setup_observers(calc_data_t *calc_data) { |
88 | 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 |
94 | 158 calculator_scr = calc_data->code; |
159 factory = rdman_get_ob_factory(calc_data->rt->rdman); | |
88 | 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); |
94 | 165 subject_add_observer(factory, subject, buttons_handler, calc_data); |
90 | 166 } |
88 | 167 } |
168 | |
169 int main(int argc, char * const argv[]) { | |
170 X_MB_runtime_t rt; | |
171 calculator_scr_t *calculator_scr; | |
94 | 172 calc_data_t calc_data; |
88 | 173 int r; |
174 | |
175 r = X_MB_init(":0.0", 300, 400, &rt); | |
176 | |
177 calculator_scr = calculator_scr_new(rt.rdman); | |
178 | |
94 | 179 calc_data.rt = &rt; |
180 calc_data.code = calculator_scr; | |
181 setup_observers(&calc_data); | |
88 | 182 |
183 X_MB_handle_connection(&rt); | |
184 | |
185 calculator_scr_free(calculator_scr); | |
186 X_MB_destroy(&rt); | |
187 | |
188 return 0; | |
189 } |