Mercurial > MadButterfly
comparison 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 |
comparison
equal
deleted
inserted
replaced
93:c3c41f88e776 | 94:57f8fecca8f3 |
---|---|
3 #include <X_supp.h> | 3 #include <X_supp.h> |
4 #include <shapes.h> | 4 #include <shapes.h> |
5 #include <tools.h> | 5 #include <tools.h> |
6 #include "calculator_scr.h" | 6 #include "calculator_scr.h" |
7 | 7 |
8 typedef struct _ex_rt ex_rt_t; | 8 typedef struct _calc_data calc_data_t; |
9 struct _ex_rt { | 9 struct _calc_data { |
10 X_MB_runtime_t *rt; | 10 X_MB_runtime_t *rt; |
11 calculator_scr_t *code; | 11 calculator_scr_t *code; |
12 }; | 12 }; |
13 | 13 |
14 static struct { | 14 static struct { |
59 } | 59 } |
60 | 60 |
61 return r; | 61 return r; |
62 } | 62 } |
63 | 63 |
64 static void show_text(ex_rt_t *ex_rt, int num, int saved, int op, | 64 static void show_text(calc_data_t *calc_data, int num, int saved, int op, |
65 const char *suffix) { | 65 const char *suffix) { |
66 char buf[20]; | 66 char buf[20]; |
67 | 67 |
68 sprintf(buf, "%d%s", num, suffix); | 68 sprintf(buf, "%d%s", num, suffix); |
69 sh_text_set_text(ex_rt->code->screen_text, buf); | 69 sh_text_set_text(calc_data->code->screen_text, buf); |
70 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); | 70 rdman_shape_changed(calc_data->rt->rdman, calc_data->code->screen_text); |
71 | 71 |
72 if(op == 'n') | 72 if(op == 'n') |
73 sprintf(buf, "None"); | 73 sprintf(buf, "None"); |
74 else | 74 else |
75 sprintf(buf, "%d%c", saved, op); | 75 sprintf(buf, "%d%c", saved, op); |
76 sh_text_set_text(ex_rt->code->saved_text, buf); | 76 sh_text_set_text(calc_data->code->saved_text, buf); |
77 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->saved_text); | 77 rdman_shape_changed(calc_data->rt->rdman, calc_data->code->saved_text); |
78 } | 78 } |
79 | 79 |
80 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { | 80 static void compute(calc_data_t *calc_data, coord_t *tgt) { |
81 int i; | 81 int i; |
82 coord_t **coord_p; | 82 coord_t **coord_p; |
83 static int valid_num = 0; | 83 static int valid_num = 0; |
84 static int factor = 1; | 84 static int factor = 1; |
85 static int num = 0; | 85 static int num = 0; |
86 static int op = 'n'; | 86 static int op = 'n'; |
87 static int saved = 0; | 87 static int saved = 0; |
88 char buf[2] = " "; | 88 char buf[2] = " "; |
89 | 89 |
90 for(i = 0; i < 16; i++) { | 90 for(i = 0; i < 16; i++) { |
91 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); | 91 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
92 if(*coord_p == (void *)tgt) | 92 if(*coord_p == (void *)tgt) |
93 break; | 93 break; |
94 } | 94 } |
95 if(i >= 16) return; | 95 if(i >= 16) return; |
96 | 96 |
97 if(i < 10) { | 97 if(i < 10) { |
98 num = num * 10 + i; | 98 num = num * 10 + i; |
99 show_text(ex_rt, num * factor, saved, op, ""); | 99 show_text(calc_data, num * factor, saved, op, ""); |
100 valid_num = 1; | 100 valid_num = 1; |
101 } else { | 101 } else { |
102 switch(tgt_list[i].c) { | 102 switch(tgt_list[i].c) { |
103 case 'c': | 103 case 'c': |
104 saved = num = 0; | 104 saved = num = 0; |
105 factor = 1; | 105 factor = 1; |
106 valid_num = 0; | 106 valid_num = 0; |
107 op = 'n'; | 107 op = 'n'; |
108 show_text(ex_rt, 0, saved, op, ""); | 108 show_text(calc_data, 0, saved, op, ""); |
109 break; | 109 break; |
110 | 110 |
111 case '-': | 111 case '-': |
112 if(!valid_num) { | 112 if(!valid_num) { |
113 factor *= -1; | 113 factor *= -1; |
117 case '+': | 117 case '+': |
118 case '*': | 118 case '*': |
119 case '/': | 119 case '/': |
120 saved = real_compute(op, saved, num * factor); | 120 saved = real_compute(op, saved, num * factor); |
121 buf[0] = tgt_list[i].c; | 121 buf[0] = tgt_list[i].c; |
122 show_text(ex_rt, saved, saved, 'n', buf); | 122 show_text(calc_data, saved, saved, 'n', buf); |
123 op = tgt_list[i].c; | 123 op = tgt_list[i].c; |
124 num = 0; | 124 num = 0; |
125 factor = 1; | 125 factor = 1; |
126 valid_num = 0; | 126 valid_num = 0; |
127 break; | 127 break; |
128 | 128 |
129 case '=': | 129 case '=': |
130 saved = real_compute(op, saved, num * factor); | 130 saved = real_compute(op, saved, num * factor); |
131 show_text(ex_rt, saved, 0, 'n', ""); | 131 show_text(calc_data, saved, 0, 'n', ""); |
132 num = 0; | 132 num = 0; |
133 op = 'N'; | 133 op = 'N'; |
134 break; | 134 break; |
135 } | 135 } |
136 } | 136 } |
137 rdman_redraw_changed(ex_rt->rt->rdman); | 137 rdman_redraw_changed(calc_data->rt->rdman); |
138 } | 138 } |
139 | 139 |
140 static void buttons_handler(event_t *evt, void *arg) { | 140 static void buttons_handler(event_t *evt, void *arg) { |
141 ex_rt_t *ex_rt = (ex_rt_t *)arg; | 141 calc_data_t *calc_data = (calc_data_t *)arg; |
142 | 142 |
143 switch(evt->type) { | 143 switch(evt->type) { |
144 case EVT_MOUSE_BUT_PRESS: | 144 case EVT_MOUSE_BUT_PRESS: |
145 compute(ex_rt, (coord_t *)evt->cur_tgt); | 145 compute(calc_data, (coord_t *)evt->cur_tgt); |
146 break; | 146 break; |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 static void setup_observers(ex_rt_t *ex_rt) { | 150 static void setup_observers(calc_data_t *calc_data) { |
151 calculator_scr_t *calculator_scr; | 151 calculator_scr_t *calculator_scr; |
152 ob_factory_t *factory; | 152 ob_factory_t *factory; |
153 subject_t *subject; | 153 subject_t *subject; |
154 coord_t *coord; | 154 coord_t *coord; |
155 int off; | 155 int off; |
156 int i; | 156 int i; |
157 | 157 |
158 calculator_scr = ex_rt->code; | 158 calculator_scr = calc_data->code; |
159 factory = rdman_get_ob_factory(ex_rt->rt->rdman); | 159 factory = rdman_get_ob_factory(calc_data->rt->rdman); |
160 | 160 |
161 for(i = 0; i < 16; i++) { | 161 for(i = 0; i < 16; i++) { |
162 off = tgt_list[i].off; | 162 off = tgt_list[i].off; |
163 coord = OFF2TYPE(calculator_scr, off, coord_t *); | 163 coord = OFF2TYPE(calculator_scr, off, coord_t *); |
164 subject = coord_get_mouse_event(coord); | 164 subject = coord_get_mouse_event(coord); |
165 subject_add_observer(factory, subject, buttons_handler, ex_rt); | 165 subject_add_observer(factory, subject, buttons_handler, calc_data); |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 int main(int argc, char * const argv[]) { | 169 int main(int argc, char * const argv[]) { |
170 X_MB_runtime_t rt; | 170 X_MB_runtime_t rt; |
171 calculator_scr_t *calculator_scr; | 171 calculator_scr_t *calculator_scr; |
172 ex_rt_t ex_rt; | 172 calc_data_t calc_data; |
173 int r; | 173 int r; |
174 | 174 |
175 r = X_MB_init(":0.0", 300, 400, &rt); | 175 r = X_MB_init(":0.0", 300, 400, &rt); |
176 | 176 |
177 calculator_scr = calculator_scr_new(rt.rdman); | 177 calculator_scr = calculator_scr_new(rt.rdman); |
178 | 178 |
179 ex_rt.rt = &rt; | 179 calc_data.rt = &rt; |
180 ex_rt.code = calculator_scr; | 180 calc_data.code = calculator_scr; |
181 setup_observers(&ex_rt); | 181 setup_observers(&calc_data); |
182 | 182 |
183 X_MB_handle_connection(&rt); | 183 X_MB_handle_connection(&rt); |
184 | 184 |
185 calculator_scr_free(calculator_scr); | 185 calculator_scr_free(calculator_scr); |
186 X_MB_destroy(&rt); | 186 X_MB_destroy(&rt); |