1 /* This is a small demo of the high-performance GUIX graphics framework. */
2 
3 #include <stdio.h>
4 #include "gx_api.h"
5 
6 #include "guix_calculator_resources.h"
7 #include "guix_calculator_specifications.h"
8 
9 GX_WINDOW_ROOT    *root;
10 
11 #define TYPE_OPERAND       1
12 #define TYPE_OPERATION     2
13 #define TYPE_DOT           3
14 #define TYPE_FUNCTION      4
15 
16 #define MAX_TEXT_LENGTH    3
17 #define MAX_EXPRESSION_LENGTH 99
18 
19 extern GX_CHAR result[128];
20 void evaluate(GX_STRING *expression, GX_BOOL persent_flag);
21 
22 GX_BOOL    clear_input_buffer = GX_FALSE;
23 GX_BOOL    is_decimal_point = GX_FALSE;
24 
25 typedef struct
26 {
27     GX_CHAR *text;
28     INT      x_offset;
29     INT      y_offset;
30     USHORT   widget_id;
31     USHORT   event_type;
32     LONG     type;
33 } KEY_INFO_ENTRY;
34 
35 KEY_INFO_ENTRY key_info[] = {
36     { "0", 0, 0, ID_CALCULATOR_ZERO, GX_SIGNAL(ID_CALCULATOR_ZERO, GX_EVENT_CLICKED), TYPE_OPERAND },
37     { "1", 0, 0, ID_CALCULATOR_ONE, GX_SIGNAL(ID_CALCULATOR_ONE, GX_EVENT_CLICKED), TYPE_OPERAND },
38     { "2", 0, 0, ID_CALCULATOR_TWO, GX_SIGNAL(ID_CALCULATOR_TWO, GX_EVENT_CLICKED), TYPE_OPERAND },
39     { "3", 0, 0, ID_CALCULATOR_THREE, GX_SIGNAL(ID_CALCULATOR_THREE, GX_EVENT_CLICKED), TYPE_OPERAND },
40     { "4", 0, 0, ID_CALCULATOR_FOUR, GX_SIGNAL(ID_CALCULATOR_FOUR, GX_EVENT_CLICKED), TYPE_OPERAND },
41     { "5", 0, 0, ID_CALCULATOR_FIVE, GX_SIGNAL(ID_CALCULATOR_FIVE, GX_EVENT_CLICKED), TYPE_OPERAND },
42     { "6", 0, 0, ID_CALCULATOR_SIX, GX_SIGNAL(ID_CALCULATOR_SIX, GX_EVENT_CLICKED), TYPE_OPERAND },
43     { "7", 0, 0, ID_CALCULATOR_SEVEN, GX_SIGNAL(ID_CALCULATOR_SEVEN, GX_EVENT_CLICKED), TYPE_OPERAND },
44     { "8", 0, 0, ID_CALCULATOR_EIGHT, GX_SIGNAL(ID_CALCULATOR_EIGHT, GX_EVENT_CLICKED), TYPE_OPERAND },
45     { "9", 0, 0, ID_CALCULATOR_NINE, GX_SIGNAL(ID_CALCULATOR_NINE, GX_EVENT_CLICKED), TYPE_OPERAND },
46     { "+", 0, -2, ID_CALCULATOR_PLUS, GX_SIGNAL(ID_CALCULATOR_PLUS, GX_EVENT_CLICKED), TYPE_OPERATION },
47     { "-", 0, -2, ID_CALCULATOR_MINUS, GX_SIGNAL(ID_CALCULATOR_MINUS, GX_EVENT_CLICKED), TYPE_OPERATION },
48     { "*", 0, 2, ID_CALCULATOR_MULTI, GX_SIGNAL(ID_CALCULATOR_MULTI, GX_EVENT_CLICKED), TYPE_OPERATION },
49     { "/", 0, -2, ID_CALCULATOR_DIVIDE, GX_SIGNAL(ID_CALCULATOR_DIVIDE, GX_EVENT_CLICKED), TYPE_OPERATION },
50     { ".", 0, 0, ID_CALCULATOR_DOT, GX_SIGNAL(ID_CALCULATOR_DOT, GX_EVENT_CLICKED), TYPE_DOT },
51     { "C", 0, 0, ID_CALCULATOR_C, 0, TYPE_FUNCTION },
52     { "MC", 0, 0, ID_CALCULATOR_MC, 0, TYPE_FUNCTION },
53     { "MR", 0, 0, ID_CALCULATOR_MR, 0, TYPE_FUNCTION },
54     { "Del", 0, 0, ID_CALCULATOR_DEL, 0, TYPE_FUNCTION },
55     { "=", 0, 0, ID_CALCULATOR_RESULT, 0, TYPE_FUNCTION },
56     { "%", 0, 0, ID_CALCULATOR_PERCENT, 0, TYPE_FUNCTION },
57     { GX_NULL, 0, 0, 0, 0, 0 }
58 };
59 
60 /* Define prototypes.   */
61 extern UINT win32_graphics_driver_setup_565rgb(GX_DISPLAY *display);
62 UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length);
63 
main(int argc,char ** argv)64 int main(int argc, char ** argv)
65 {
66   tx_kernel_enter();
67   return(0);
68 }
69 
70 /* Custom pixelmap button draw. */
custom_pixelmap_button_draw(GX_PIXELMAP_BUTTON * key)71 VOID custom_pixelmap_button_draw(GX_PIXELMAP_BUTTON *key)
72 {
73 GX_RESOURCE_ID  font_id = GX_FONT_ID_MIDSIZE;
74 KEY_INFO_ENTRY *pKeyInfo = key_info;
75 INT             x_offset;
76 INT             y_offset;
77 GX_STRING       text;
78 
79     gx_pixelmap_button_draw(key);
80 
81     while (pKeyInfo->text)
82     {
83         if (pKeyInfo->widget_id == key->gx_widget_id)
84         {
85             x_offset = pKeyInfo->x_offset;
86             y_offset = pKeyInfo->y_offset;
87 
88             if (key->gx_widget_style & GX_STYLE_BUTTON_PUSHED)
89             {
90                 x_offset++;
91                 y_offset++;
92             }
93 
94             text.gx_string_ptr = pKeyInfo->text;
95             text.gx_string_length = string_length_get(pKeyInfo->text, MAX_TEXT_LENGTH);
96 
97             gx_widget_text_draw_ext(key, GX_COLOR_ID_WHITE,
98                                     font_id, &text,
99                                     x_offset, y_offset);
100             break;
101         }
102 
103         pKeyInfo++;
104     }
105 }
106 
tx_application_define(void * first_unused_memory)107 VOID tx_application_define(void *first_unused_memory)
108 {
109     /* Initialize GUIX.  */
110     gx_system_initialize();
111 
112     gx_studio_display_configure(MAIN_SCREEN, win32_graphics_driver_setup_565rgb,
113                                 LANGUAGE_ENGLISH, MAIN_SCREEN_DEFAULT_THEME, &root);
114 
115     /* Create calculator screen */
116     gx_studio_named_widget_create("frame", (GX_WIDGET *)root, GX_NULL);
117 
118     /* Show the root window to make it and patients screen visible.  */
119     gx_widget_show(root);
120 
121     /* start GUIX thread */
122     gx_system_start();
123 }
124 
calculator_event_handler(GX_WINDOW * window,GX_EVENT * event_ptr)125 UINT calculator_event_handler(GX_WINDOW *window, GX_EVENT *event_ptr)
126 {
127 GX_SINGLE_LINE_TEXT_INPUT *text_input;
128 KEY_INFO_ENTRY            *pKeyInfo = key_info;
129 GX_CHAR                    pre_char = '\0';
130 INT                        string_size;
131 GX_STRING                  string;
132 
133     text_input = &frame.frame_display_result;
134 
135     if ((event_ptr->gx_event_type & GX_SIGNAL_EVENT_MASK) == GX_EVENT_CLICKED)
136     {
137         while (pKeyInfo->text)
138         {
139             if (pKeyInfo->event_type == event_ptr->gx_event_type)
140             {
141                 if (clear_input_buffer)
142                 {
143                     if ((pKeyInfo->text[0] >= '0') && (pKeyInfo->text[0] <= '9'))
144                     {
145                         /* Clear input buffer.  */
146                         gx_single_line_text_input_buffer_clear(text_input);
147                     }
148                     else if (!strcmp(text_input->gx_single_line_text_input_buffer, "Error Format"))
149                     {
150                         return 0;
151                     }
152 
153                     clear_input_buffer = GX_FALSE;
154                 }
155 
156                 string_size = text_input->gx_single_line_text_input_string_size;
157 
158                 if (string_size)
159                 {
160                     /* Get previous character.  */
161                     pre_char = text_input->gx_single_line_text_input_buffer[string_size - 1];
162                 }
163 
164                 if (pKeyInfo->type == TYPE_OPERAND)
165                 {
166                     if ((pre_char == '0') && (string_size == 1))
167                     {
168                         gx_single_line_text_input_backspace(text_input);
169                     }
170                     else if ((pre_char == '/') && (pKeyInfo->text[0] == '0'))
171                     {
172                         return 0;
173                     }
174 
175                     gx_single_line_text_input_character_insert(text_input, (GX_UBYTE *)&pKeyInfo->text[0], 1);
176                 }
177                 else if (string_size)
178                 {
179                     if ((pre_char >= '0') && (pre_char <= '9'))
180                     {
181                         if (pKeyInfo->type == TYPE_OPERATION)
182                         {
183                             gx_single_line_text_input_character_insert(text_input, (GX_UBYTE *)&pKeyInfo->text[0], 1);
184                             is_decimal_point = GX_FALSE;
185                         }
186                         else if ((pKeyInfo->type == TYPE_DOT) && !is_decimal_point)
187                         {
188                             gx_single_line_text_input_character_insert(text_input, (GX_UBYTE *)&pKeyInfo->text[0], 1);
189                             is_decimal_point = GX_TRUE;
190                         }
191                     }
192                 }
193 
194                 return 0;
195             }
196             pKeyInfo++;
197         }
198     }
199 
200     switch (event_ptr->gx_event_type)
201     {
202     case GX_SIGNAL(ID_CALCULATOR_DEL, GX_EVENT_CLICKED):
203         gx_single_line_text_input_backspace(text_input);
204         break;
205 
206     case GX_SIGNAL(ID_CALCULATOR_C, GX_EVENT_CLICKED):
207         /* Clear input buffer. */
208         gx_single_line_text_input_buffer_clear(text_input);
209         break;
210 
211     case GX_SIGNAL(ID_CALCULATOR_RESULT, GX_EVENT_CLICKED):
212         if (string_length_get(text_input->gx_single_line_text_input_buffer, MAX_EXPRESSION_LENGTH) > 0)
213         {
214             /* Evaluate expression. */
215             string.gx_string_ptr = text_input->gx_single_line_text_input_buffer;
216             string.gx_string_length = string_length_get(string.gx_string_ptr, text_input->gx_single_line_text_input_buffer_size - 1);
217             evaluate(&string, GX_FALSE);
218 
219             /* Clear input buffer. */
220             gx_single_line_text_input_buffer_clear(text_input);
221 
222             /* Show result. */
223             gx_single_line_text_input_character_insert(text_input, (GX_UBYTE *)result, string_length_get(result, MAX_EXPRESSION_LENGTH));
224 
225             clear_input_buffer = GX_TRUE;
226         }
227         break;
228 
229     case GX_SIGNAL(ID_CALCULATOR_PERCENT, GX_EVENT_CLICKED):
230         if (string_length_get(text_input->gx_single_line_text_input_buffer, MAX_EXPRESSION_LENGTH) > 0)
231         {
232             string.gx_string_ptr = text_input->gx_single_line_text_input_buffer;
233             string.gx_string_length = string_length_get(string.gx_string_ptr, text_input->gx_single_line_text_input_buffer_size - 1);
234             evaluate(&string, GX_TRUE);
235 
236             /* Clear input buffer. */
237             gx_single_line_text_input_buffer_clear(text_input);
238 
239             /* Show result. */
240             gx_single_line_text_input_character_insert(text_input, (GX_UBYTE *)result, string_length_get(result, MAX_EXPRESSION_LENGTH));
241 
242             clear_input_buffer = GX_TRUE;
243         }
244         break;
245 
246     default:
247         return gx_window_event_process(window, event_ptr);
248     }
249 
250     return 0;
251 }
252 
string_length_get(GX_CONST GX_CHAR * input_string,UINT max_string_length)253 UINT string_length_get(GX_CONST GX_CHAR *input_string, UINT max_string_length)
254 {
255 UINT length = 0;
256 
257     if (input_string)
258     {
259         /* Traverse the string.  */
260         for (length = 0; input_string[length]; length++)
261         {
262             /* Check if the string length is bigger than the max string length.  */
263             if (length >= max_string_length)
264             {
265                 break;
266             }
267         }
268     }
269 
270     return length;
271 }
272 
safe_string_copy(GX_CHAR * put,const GX_CHAR * get,INT max_len)273 VOID safe_string_copy(GX_CHAR *put, const GX_CHAR *get, INT max_len)
274 {
275     while(max_len > 1 && *get)
276     {
277         *put++ = *get++;
278         max_len--;
279     }
280     *put = 0;
281 }
282 
283 
284 
285