ftk_app_calc.c 6.2 KB

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