Mercurial > MadButterfly
annotate examples/calculator/main.c @ 1005:714169f33f13 refine_backend_if
Fix X_init_connection() to match modification of mb_backend_t
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 22 Nov 2010 00:42:30 +0800 |
parents | 331467b8e778 |
children | 32425882e351 |
rev | line source |
---|---|
88 | 1 #include <stdio.h> |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
2 #include <string.h> |
186
530bb7728546
Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents:
130
diff
changeset
|
3 #include <mb.h> |
88 | 4 #include "calculator_scr.h" |
5 | |
94 | 6 typedef struct _calc_data calc_data_t; |
7 struct _calc_data { | |
88 | 8 X_MB_runtime_t *rt; |
9 calculator_scr_t *code; | |
10 }; | |
11 | |
12 static struct { | |
13 int c; | |
14 int off; | |
15 } tgt_list[] = { | |
16 { 0, OFFSET(calculator_scr_t, but_0) }, | |
17 { 1, OFFSET(calculator_scr_t, but_1) }, | |
18 { 2, OFFSET(calculator_scr_t, but_2) }, | |
19 { 3, OFFSET(calculator_scr_t, but_3) }, | |
20 { 4, OFFSET(calculator_scr_t, but_4) }, | |
21 { 5, OFFSET(calculator_scr_t, but_5) }, | |
22 { 6, OFFSET(calculator_scr_t, but_6) }, | |
23 { 7, OFFSET(calculator_scr_t, but_7) }, | |
24 { 8, OFFSET(calculator_scr_t, but_8) }, | |
25 { 9, OFFSET(calculator_scr_t, but_9) }, | |
26 { '+', OFFSET(calculator_scr_t, but_add) }, | |
27 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
28 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
29 { '/', OFFSET(calculator_scr_t, but_div) }, | |
30 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
31 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
32 }; | |
33 | |
34 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
|
35 int r = v1; |
88 | 36 |
37 switch(op) { | |
38 case '+': | |
39 r = v1 + v2; | |
40 break; | |
41 case '-': | |
42 r = v1 - v2; | |
43 break; | |
44 case '*': | |
45 r = v1 * v2; | |
46 break; | |
47 case '/': | |
93 | 48 r = v1; |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
49 if(v2) |
93 | 50 r /= v2; |
88 | 51 break; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
52 case 'n': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
53 r = v2; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
54 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
55 case 'N': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
56 break; |
88 | 57 } |
58 | |
59 return r; | |
60 } | |
61 | |
94 | 62 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
|
63 const char *suffix) { |
88 | 64 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
|
65 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
|
66 |
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
67 rdman = X_MB_rdman(calc_data->rt); |
88 | 68 |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
69 sprintf(buf, "%d%s", num, suffix); |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
70 sh_stext_set_text(calc_data->code->screen_text_u, buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
71 calc_data->code->screen_text_u_style_blks[0].n_chars = strlen(buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
72 sh_stext_set_style(calc_data->code->screen_text_u, |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
73 calc_data->code->screen_text_u_style_blks, 1); |
278
a90fd749af82
Implement the whole tspan attribute. Currently, we can accept font family/font style/font weight and font size.
wycc
parents:
192
diff
changeset
|
74 rdman_shape_changed(rdman, calc_data->code->screen_text_u); |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
75 |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
76 if(op == 'n') |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
77 sprintf(buf, "None"); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
78 else |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
79 sprintf(buf, "%d%c", saved, op); |
432
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
80 sh_stext_set_text(calc_data->code->saved_text, buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
81 calc_data->code->saved_text_style_blks[0].n_chars = strlen(buf); |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
82 sh_stext_set_style(calc_data->code->saved_text, |
331467b8e778
Make calcuator uses stext instead of text.
Thinker K.F. Li <thinker@branda.to>
parents:
280
diff
changeset
|
83 calc_data->code->saved_text_style_blks, 1); |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
84 rdman_shape_changed(rdman, calc_data->code->saved_text); |
88 | 85 } |
86 | |
94 | 87 static void compute(calc_data_t *calc_data, coord_t *tgt) { |
88 | 88 int i; |
89 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
|
90 redraw_man_t *rdman; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
91 static int valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
92 static int factor = 1; |
88 | 93 static int num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
94 static int op = 'n'; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
95 static int saved = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
96 char buf[2] = " "; |
88 | 97 |
98 for(i = 0; i < 16; i++) { | |
94 | 99 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
88 | 100 if(*coord_p == (void *)tgt) |
101 break; | |
102 } | |
103 if(i >= 16) return; | |
104 | |
105 if(i < 10) { | |
106 num = num * 10 + i; | |
94 | 107 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
|
108 valid_num = 1; |
88 | 109 } else { |
110 switch(tgt_list[i].c) { | |
111 case 'c': | |
112 saved = num = 0; | |
92
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 = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
115 op = 'n'; |
94 | 116 show_text(calc_data, 0, saved, op, ""); |
88 | 117 break; |
118 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
119 case '-': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
120 if(!valid_num) { |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
121 factor *= -1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
122 valid_num = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
123 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
124 } |
88 | 125 case '+': |
126 case '*': | |
127 case '/': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
128 saved = real_compute(op, saved, num * factor); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
129 buf[0] = tgt_list[i].c; |
94 | 130 show_text(calc_data, saved, saved, 'n', buf); |
90 | 131 op = tgt_list[i].c; |
88 | 132 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
133 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
134 valid_num = 0; |
88 | 135 break; |
136 | |
137 case '=': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
138 saved = real_compute(op, saved, num * factor); |
94 | 139 show_text(calc_data, saved, 0, 'n', ""); |
88 | 140 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
141 op = 'N'; |
88 | 142 break; |
143 } | |
144 } | |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
145 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
|
146 rdman_redraw_changed(rdman); |
88 | 147 } |
148 | |
149 static void buttons_handler(event_t *evt, void *arg) { | |
94 | 150 calc_data_t *calc_data = (calc_data_t *)arg; |
88 | 151 |
152 switch(evt->type) { | |
153 case EVT_MOUSE_BUT_PRESS: | |
271 | 154 compute(calc_data, (coord_t *)evt->cur_tgt->obj); |
88 | 155 break; |
156 } | |
157 } | |
158 | |
94 | 159 static void setup_observers(calc_data_t *calc_data) { |
88 | 160 calculator_scr_t *calculator_scr; |
161 subject_t *subject; | |
90 | 162 coord_t *coord; |
163 int off; | |
164 int i; | |
88 | 165 |
94 | 166 calculator_scr = calc_data->code; |
88 | 167 |
90 | 168 for(i = 0; i < 16; i++) { |
169 off = tgt_list[i].off; | |
93 | 170 coord = OFF2TYPE(calculator_scr, off, coord_t *); |
90 | 171 subject = coord_get_mouse_event(coord); |
192
54fdc2a65242
Remove factory from observer APIs.
Thinker K.F. Li <thinker@branda.to>
parents:
186
diff
changeset
|
172 subject_add_observer(subject, buttons_handler, calc_data); |
90 | 173 } |
88 | 174 } |
175 | |
176 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
|
177 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
|
178 redraw_man_t *rdman; |
88 | 179 calculator_scr_t *calculator_scr; |
94 | 180 calc_data_t calc_data; |
88 | 181 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
182 rt = X_MB_new(":0.0", 300, 400); |
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 rdman = X_MB_rdman(rt); |
130
3a4d6179e6a9
change mb_c_source.m4 and mb_c_header.m4 to specify parent for SVG object
Thinker K.F. Li <thinker@branda.to>
parents:
122
diff
changeset
|
185 calculator_scr = calculator_scr_new(rdman, rdman->root_coord); |
88 | 186 |
122
17e97e92b76e
Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents:
108
diff
changeset
|
187 calc_data.rt = rt; |
94 | 188 calc_data.code = calculator_scr; |
189 setup_observers(&calc_data); | |
88 | 190 |
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_handle_connection(rt); |
88 | 192 |
193 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
|
194 X_MB_free(rt); |
88 | 195 |
196 return 0; | |
197 } |