PageRenderTime 46ms CodeModel.GetById 20ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/apps/calculator/ftk_app_calc.c

http://ftk.googlecode.com/
C | 262 lines | 219 code | 43 blank | 0 comment | 30 complexity | d0581c217af2512daee9d1b74e795c12 MD5 | raw file
  1#include "ftk_xul.h"
  2#include "ftk_expr.h"
  3#include "ftk_app_calc.h"
  4
  5typedef struct _PrivInfo
  6{
  7	FtkBitmap* icon;
  8}PrivInfo;
  9
 10#define IDC_ENTRY 100
 11
 12static Ret ftk_calc_on_button_clicked(void* ctx, void* obj);
 13
 14const char* ftk_translate_path(const char* path, char out_path[FTK_MAX_PATH+1])
 15{
 16	snprintf(out_path, FTK_MAX_PATH, "%s/%s", APP_DATA_DIR, path);
 17	if(access(out_path, R_OK) < 0)
 18	{
 19		snprintf(out_path, FTK_MAX_PATH, "%s/%s", APP_LOCAL_DATA_DIR, path);
 20	}
 21	ftk_logd("%s->%s\n", path, out_path);
 22
 23	return out_path;
 24}
 25
 26const char* buttons[] =
 27{
 28	"0",
 29	"1",
 30	"2",
 31	"3",
 32	"4",
 33	"5",
 34	"6",
 35	"7",
 36	"8",
 37	"9",
 38	".",
 39	"+",
 40	"-",
 41	"*",
 42	"/",
 43	"(",
 44	")",
 45	"=",
 46	"<--",
 47	"Quit"
 48};
 49
 50static FtkWidget* ftk_calc_create_window(void)
 51{
 52	int i = 0;
 53	int j = 0;
 54	int x = 0;
 55	int y = 0;
 56	int row = 0;
 57	int col = 0;
 58	int small = 0;
 59	int xoffset = 0;
 60	int yoffset = 0;
 61	int width   = 0;
 62	int height  = 0;
 63	int v_margin  = 5;
 64	int h_margin  = 5;
 65	int item_width = 0;
 66	int item_height = 0;
 67	FtkGc gc = {0};
 68	FtkWidget* entry = NULL;
 69	FtkWidget* button = NULL;
 70	FtkBitmap* bitmap_normal = NULL;
 71	FtkBitmap* bitmap_active = NULL;
 72	FtkBitmap* bitmap_focus = NULL;
 73	char path[FTK_MAX_PATH+1] = {0};
 74	FtkWidget* win =  ftk_app_window_create();
 75	ftk_window_set_animation_hint(win, "app_main_window");
 76	width = ftk_widget_width(win);
 77	height = ftk_widget_height(win);
 78	entry = ftk_entry_create(win, 0, 0, width, 30);
 79	ftk_widget_set_id(entry, IDC_ENTRY);
 80	height -= ftk_widget_height(entry);
 81
 82	row = width > height ? 4 : 5;
 83	col = width > height ? 5 : 4;
 84
 85	item_width = width / col;
 86	item_height = height /row;
 87	small = (item_width < 60 || item_height < 60) ? 1 : 0;
 88
 89	item_width = item_height = small ? 36 : 60;
 90	
 91	h_margin = width/col - item_width;
 92	h_margin = h_margin > 5 ? 5 : h_margin;
 93
 94	v_margin = height/row - item_height;
 95	v_margin = v_margin > 5 ? 5 : v_margin;
 96
 97	xoffset = (width - (h_margin + item_width) * col) >> 1;
 98	yoffset = (height - (v_margin + item_height) * row) >> 1;
 99
100	xoffset = xoffset < 0 ? 0 : xoffset;
101	yoffset = yoffset < 0 ? 0 : yoffset;
102
103	yoffset += ftk_widget_height(entry);
104	gc.mask = FTK_GC_BITMAP;
105	if(small)
106	{
107		bitmap_normal =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button-small.png", path));
108		bitmap_active =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button-pressed-small.png", path));
109		bitmap_focus =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button-selected-small.png", path));
110	}
111	else
112	{
113		bitmap_normal =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button.png", path));
114		bitmap_active =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button-pressed.png", path));
115		bitmap_focus =  ftk_bitmap_factory_load(ftk_default_bitmap_factory(), ftk_translate_path("icons/button-selected.png", path));
116	}
117
118	for(i = 0; i < row; i++)
119	{
120		y = yoffset + i * (item_height + v_margin);
121		for(j = 0; j < col; j++)
122		{
123			const char* text = buttons[i * col + j];
124			if(text != NULL)
125			{
126				x = xoffset + j * (item_width + h_margin);
127				button = ftk_button_create(win, x, y, item_width, item_height);
128				ftk_widget_set_text(button, text);
129				ftk_button_set_clicked_listener(button, ftk_calc_on_button_clicked, win);
130				gc.bitmap = bitmap_normal;
131				ftk_widget_set_gc(button, FTK_WIDGET_NORMAL, &gc);
132				
133				gc.bitmap = bitmap_focus;
134				ftk_widget_set_gc(button, FTK_WIDGET_FOCUSED, &gc);
135				
136				gc.bitmap = bitmap_active;
137				ftk_widget_set_gc(button, FTK_WIDGET_ACTIVE, &gc);
138			}
139		}
140	}
141
142	ftk_bitmap_unref(bitmap_normal);
143	ftk_bitmap_unref(bitmap_active);
144	ftk_bitmap_unref(bitmap_focus);
145
146	return win;
147}
148
149static Ret ftk_calc_on_shutdown(void* ctx, void* obj)
150{
151	ftk_widget_unref(ctx);
152
153	return RET_OK;
154}
155
156static Ret ftk_calc_on_prepare_options_menu(void* ctx, FtkWidget* menu_panel)
157{
158	FtkWidget* item = ftk_menu_item_create(menu_panel);
159	ftk_widget_set_text(item, _("Quit"));
160	ftk_menu_item_set_clicked_listener(item, ftk_calc_on_shutdown, ctx);
161	ftk_widget_show(item, 1);
162
163	return RET_OK;
164}
165
166static Ret ftk_calc_on_button_clicked(void* ctx, void* obj)
167{
168	FtkWidget* entry = ftk_widget_lookup(ctx, IDC_ENTRY);
169	const char* text = ftk_widget_get_text(obj);
170	return_val_if_fail(text != NULL && entry != NULL, RET_FAIL);
171	
172	if(text[0] == '=')
173	{
174		char buff[32] = {0};
175		double val = ftk_expr_eval(ftk_entry_get_text(entry));
176		ftk_snprintf(buff, sizeof(buff), "%lf", val);
177		ftk_entry_set_text(entry, buff);
178	}
179	else if(text[0] == '<')
180	{
181		ftk_entry_set_text(entry, "");
182	}
183	else if(text[0] == 'Q' || strcmp(text, _("Quit")) == 0)
184	{
185		ftk_widget_unref(ctx);
186	}
187	else
188	{
189		ftk_entry_insert_text(entry, -1, text);
190	}
191
192	return RET_OK;
193}
194
195static FtkBitmap* ftk_app_calc_get_icon(FtkApp* thiz)
196{
197	DECL_PRIV(thiz, priv);
198	const char* name="calc.png";
199	char file_name[FTK_MAX_PATH + 1] = {0};
200	return_val_if_fail(priv != NULL, NULL);
201	
202	if(priv->icon != NULL) return priv->icon;
203	
204	snprintf(file_name, FTK_MAX_PATH, "%s/icons/%s", APP_DATA_DIR, name);
205	priv->icon = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), file_name);
206	if(priv->icon != NULL) return priv->icon;
207
208	snprintf(file_name, FTK_MAX_PATH, "%s/icons/%s", APP_LOCAL_DATA_DIR, name);
209	priv->icon = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), file_name);
210
211	return priv->icon;
212}
213
214static const char* ftk_app_calc_get_name(FtkApp* thiz)
215{
216	return _("Calculator");
217}
218
219static Ret ftk_app_calc_run(FtkApp* thiz, int argc, char* argv[])
220{
221	DECL_PRIV(thiz, priv);
222	FtkWidget* win = NULL;
223	return_val_if_fail(priv != NULL, RET_FAIL);
224
225	win = ftk_calc_create_window();
226	ftk_app_window_set_on_prepare_options_menu(win, ftk_calc_on_prepare_options_menu, win);
227	ftk_widget_show_all(win, 1);
228
229#ifdef HAS_MAIN
230	FTK_QUIT_WHEN_WIDGET_CLOSE(win);
231#endif	
232
233	return RET_OK;
234}
235
236static void ftk_app_calc_destroy(FtkApp* thiz)
237{
238	if(thiz != NULL)
239	{
240		DECL_PRIV(thiz, priv);
241		ftk_bitmap_unref(priv->icon);
242		FTK_FREE(thiz);
243	}
244
245	return;
246}
247
248FtkApp* ftk_app_calc_create(void)
249{
250	FtkApp* thiz = FTK_ZALLOC(sizeof(FtkApp) + sizeof(PrivInfo));
251
252	if(thiz != NULL)
253	{
254		thiz->run  = ftk_app_calc_run;
255		thiz->get_icon = ftk_app_calc_get_icon;
256		thiz->get_name = ftk_app_calc_get_name;
257		thiz->destroy = ftk_app_calc_destroy;
258	}
259
260	return thiz;
261}
262