Mercurial > MadButterfly
annotate examples/calculator/main.c @ 122:17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Mon, 15 Sep 2008 20:33:06 +0800 |
parents | 565b55508c8d |
children | 3a4d6179e6a9 |
rev | line source |
---|---|
88 | 1 #include <stdio.h> |
108 | 2 #include <mb/mb.h> |
88 | 3 #include "calculator_scr.h" |
4 | |
94 | 5 typedef struct _calc_data calc_data_t; |
6 struct _calc_data { | |
88 | 7 X_MB_runtime_t *rt; |
8 calculator_scr_t *code; | |
9 }; | |
10 | |
11 static struct { | |
12 int c; | |
13 int off; | |
14 } tgt_list[] = { | |
15 { 0, OFFSET(calculator_scr_t, but_0) }, | |
16 { 1, OFFSET(calculator_scr_t, but_1) }, | |
17 { 2, OFFSET(calculator_scr_t, but_2) }, | |
18 { 3, OFFSET(calculator_scr_t, but_3) }, | |
19 { 4, OFFSET(calculator_scr_t, but_4) }, | |
20 { 5, OFFSET(calculator_scr_t, but_5) }, | |
21 { 6, OFFSET(calculator_scr_t, but_6) }, | |
22 { 7, OFFSET(calculator_scr_t, but_7) }, | |
23 { 8, OFFSET(calculator_scr_t, but_8) }, | |
24 { 9, OFFSET(calculator_scr_t, but_9) }, | |
25 { '+', OFFSET(calculator_scr_t, but_add) }, | |
26 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
27 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
28 { '/', OFFSET(calculator_scr_t, but_div) }, | |
29 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
30 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
31 }; | |
32 | |
33 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
|
34 int r = v1; |
88 | 35 |
36 switch(op) { | |
37 case '+': | |
38 r = v1 + v2; | |
39 break; | |
40 case '-': | |
41 r = v1 - v2; | |
42 break; | |
43 case '*': | |
44 r = v1 * v2; | |
45 break; | |
46 case '/': | |
93 | 47 r = v1; |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
48 if(v2) |
93 | 49 r /= v2; |
88 | 50 break; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
51 case 'n': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
52 r = v2; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
53 break; |
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 break; |
88 | 56 } |
57 | |
58 return r; | |
59 } | |
60 | |
94 | 61 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
|
62 const char *suffix) { |
88 | 63 char buf[20]; |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
64 redraw_man_t *rdman; |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
65 |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
66 rdman = X_MB_rdman(calc_data->rt); |
88 | 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); |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
70 rdman_shape_changed(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); |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
77 rdman_shape_changed(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; | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
83 redraw_man_t *rdman; |
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++) { | |
94 | 92 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
88 | 93 if(*coord_p == (void *)tgt) |
94 break; | |
95 } | |
96 if(i >= 16) return; | |
97 | |
98 if(i < 10) { | |
99 num = num * 10 + i; | |
94 | 100 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
|
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'; |
94 | 109 show_text(calc_data, 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; |
94 | 123 show_text(calc_data, 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); |
94 | 132 show_text(calc_data, 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 } | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
138 rdman = X_MB_rdman(calc_data->rt); |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
139 rdman_redraw_changed(rdman); |
88 | 140 } |
141 | |
142 static void buttons_handler(event_t *evt, void *arg) { | |
94 | 143 calc_data_t *calc_data = (calc_data_t *)arg; |
88 | 144 |
145 switch(evt->type) { | |
146 case EVT_MOUSE_BUT_PRESS: | |
94 | 147 compute(calc_data, (coord_t *)evt->cur_tgt); |
88 | 148 break; |
149 } | |
150 } | |
151 | |
94 | 152 static void setup_observers(calc_data_t *calc_data) { |
88 | 153 calculator_scr_t *calculator_scr; |
154 ob_factory_t *factory; | |
155 subject_t *subject; | |
90 | 156 coord_t *coord; |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
157 redraw_man_t *rdman; |
90 | 158 int off; |
159 int i; | |
88 | 160 |
94 | 161 calculator_scr = calc_data->code; |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
162 rdman = X_MB_rdman(calc_data->rt); |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
163 factory = rdman_get_ob_factory(rdman); |
88 | 164 |
90 | 165 for(i = 0; i < 16; i++) { |
166 off = tgt_list[i].off; | |
93 | 167 coord = OFF2TYPE(calculator_scr, off, coord_t *); |
90 | 168 subject = coord_get_mouse_event(coord); |
94 | 169 subject_add_observer(factory, subject, buttons_handler, calc_data); |
90 | 170 } |
88 | 171 } |
172 | |
173 int main(int argc, char * const argv[]) { | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
174 X_MB_runtime_t *rt; |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
175 redraw_man_t *rdman; |
88 | 176 calculator_scr_t *calculator_scr; |
94 | 177 calc_data_t calc_data; |
88 | 178 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
179 rt = X_MB_new(":0.0", 300, 400); |
88 | 180 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
181 rdman = X_MB_rdman(rt); |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
182 calculator_scr = calculator_scr_new(rdman); |
88 | 183 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
184 calc_data.rt = rt; |
94 | 185 calc_data.code = calculator_scr; |
186 setup_observers(&calc_data); | |
88 | 187 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
188 X_MB_handle_connection(rt); |
88 | 189 |
190 calculator_scr_free(calculator_scr); | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
191 X_MB_free(rt); |
88 | 192 |
193 return 0; | |
194 } |