comparison examples/calculator/main.c @ 90:bd29780bf266

Reduce redundant code
author Thinker K.F. Li <thinker@branda.to>
date Sat, 23 Aug 2008 22:23:44 +0800
parents 90428161fc61
children 3f619ae03678
comparison
equal deleted inserted replaced
89:90428161fc61 90:bd29780bf266
55 } 55 }
56 56
57 return r; 57 return r;
58 } 58 }
59 59
60 static void show_text(ex_rt_t *ex_rt, int num) { 60 static void show_text(ex_rt_t *ex_rt, int num, const char *prefix) {
61 char buf[20]; 61 char buf[20];
62 62
63 sprintf(buf, "%d", num); 63 sprintf(buf, "%s%d", prefix, num);
64 sh_text_set_text(ex_rt->code->screen_text, buf); 64 sh_text_set_text(ex_rt->code->screen_text, buf);
65 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); 65 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text);
66 } 66 }
67 67
68 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { 68 static void compute(ex_rt_t *ex_rt, coord_t *tgt) {
69 int i; 69 int i;
70 coord_t **coord_p; 70 coord_t **coord_p;
71 static int num = 0; 71 static int num = 0;
72 static int saved = 0; 72 static int saved = 0, saved2 = 0;
73 static int op = 0; 73 static int op = 0, op1 = 0;
74 74
75 for(i = 0; i < 16; i++) { 75 for(i = 0; i < 16; i++) {
76 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); 76 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off);
77 if(*coord_p == (void *)tgt) 77 if(*coord_p == (void *)tgt)
78 break; 78 break;
79 } 79 }
80 if(i >= 16) return; 80 if(i >= 16) return;
81 81
82 if(i < 10) { 82 if(i < 10) {
83 num = num * 10 + i; 83 num = num * 10 + i;
84 show_text(ex_rt, num); 84 show_text(ex_rt, num, "");
85 } else { 85 } else {
86 switch(tgt_list[i].c) { 86 switch(tgt_list[i].c) {
87 case 'c': 87 case 'c':
88 saved = num = 0; 88 saved = num = 0;
89 show_text(ex_rt, 0); 89 show_text(ex_rt, 0, "");
90 break; 90 break;
91 91
92 case '+': 92 case '+':
93 saved = real_compute(op, saved, num);
94 show_text(ex_rt, saved);
95 op = '+';
96 num = 0;
97 break;
98
99 case '-': 93 case '-':
100 saved = real_compute(op, saved, num);
101 show_text(ex_rt, saved);
102 op = '-';
103 num = 0;
104 break;
105
106 case '*': 94 case '*':
107 saved = real_compute(op, saved, num);
108 show_text(ex_rt, saved);
109 op = '*';
110 num = 0;
111 break;
112
113 case '/': 95 case '/':
114 saved = real_compute(op, saved, num); 96 saved = real_compute(op, saved, num);
115 show_text(ex_rt, saved); 97 show_text(ex_rt, saved, "=");
116 op = '/'; 98 op = tgt_list[i].c;
99 saved2 = num;
117 num = 0; 100 num = 0;
118 break; 101 break;
119 102
120 case '=': 103 case '=':
121 saved = real_compute(op, saved, num); 104 saved = real_compute(op, saved, saved2);
122 show_text(ex_rt, saved); 105 show_text(ex_rt, saved, "=");
123 num = 0; 106 num = 0;
124 break; 107 break;
125 } 108 }
126 } 109 }
127 rdman_redraw_changed(ex_rt->rt->rdman); 110 rdman_redraw_changed(ex_rt->rt->rdman);
139 122
140 static void setup_observers(ex_rt_t *ex_rt) { 123 static void setup_observers(ex_rt_t *ex_rt) {
141 calculator_scr_t *calculator_scr; 124 calculator_scr_t *calculator_scr;
142 ob_factory_t *factory; 125 ob_factory_t *factory;
143 subject_t *subject; 126 subject_t *subject;
127 coord_t *coord;
128 int off;
129 int i;
144 130
145 calculator_scr = ex_rt->code; 131 calculator_scr = ex_rt->code;
146 factory = rdman_get_ob_factory(ex_rt->rt->rdman); 132 factory = rdman_get_ob_factory(ex_rt->rt->rdman);
147 133
148 subject = coord_get_mouse_event(calculator_scr->but_0); 134 for(i = 0; i < 16; i++) {
149 subject_add_observer(factory, subject, buttons_handler, ex_rt); 135 off = tgt_list[i].off;
150 subject = coord_get_mouse_event(calculator_scr->but_1); 136 coord = *(coord_t **)((void *)calculator_scr + off);
151 subject_add_observer(factory, subject, buttons_handler, ex_rt); 137 subject = coord_get_mouse_event(coord);
152 subject = coord_get_mouse_event(calculator_scr->but_2); 138 subject_add_observer(factory, subject, buttons_handler, ex_rt);
153 subject_add_observer(factory, subject, buttons_handler, ex_rt); 139 }
154 subject = coord_get_mouse_event(calculator_scr->but_3);
155 subject_add_observer(factory, subject, buttons_handler, ex_rt);
156 subject = coord_get_mouse_event(calculator_scr->but_4);
157 subject_add_observer(factory, subject, buttons_handler, ex_rt);
158 subject = coord_get_mouse_event(calculator_scr->but_5);
159 subject_add_observer(factory, subject, buttons_handler, ex_rt);
160 subject = coord_get_mouse_event(calculator_scr->but_6);
161 subject_add_observer(factory, subject, buttons_handler, ex_rt);
162 subject = coord_get_mouse_event(calculator_scr->but_7);
163 subject_add_observer(factory, subject, buttons_handler, ex_rt);
164 subject = coord_get_mouse_event(calculator_scr->but_8);
165 subject_add_observer(factory, subject, buttons_handler, ex_rt);
166 subject = coord_get_mouse_event(calculator_scr->but_9);
167 subject_add_observer(factory, subject, buttons_handler, ex_rt);
168 subject = coord_get_mouse_event(calculator_scr->but_add);
169 subject_add_observer(factory, subject, buttons_handler, ex_rt);
170 subject = coord_get_mouse_event(calculator_scr->but_minus);
171 subject_add_observer(factory, subject, buttons_handler, ex_rt);
172 subject = coord_get_mouse_event(calculator_scr->but_mul);
173 subject_add_observer(factory, subject, buttons_handler, ex_rt);
174 subject = coord_get_mouse_event(calculator_scr->but_div);
175 subject_add_observer(factory, subject, buttons_handler, ex_rt);
176 subject = coord_get_mouse_event(calculator_scr->but_eq);
177 subject_add_observer(factory, subject, buttons_handler, ex_rt);
178 subject = coord_get_mouse_event(calculator_scr->but_clr);
179 subject_add_observer(factory, subject, buttons_handler, ex_rt);
180 } 140 }
181 141
182 int main(int argc, char * const argv[]) { 142 int main(int argc, char * const argv[]) {
183 X_MB_runtime_t rt; 143 X_MB_runtime_t rt;
184 calculator_scr_t *calculator_scr; 144 calculator_scr_t *calculator_scr;