/src/ftk_progress_bar.c

http://ftk.googlecode.com/ · C · 305 lines · 232 code · 43 blank · 30 comment · 42 complexity · 5214b02a4f6c8e648e8af01834873cff MD5 · raw file

  1. /*
  2. * File: ftk_progress_bar.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: progress bar
  5. *
  6. * Copyright (c) 2009 - 2010 Li XianJing <xianjimli@hotmail.com>
  7. *
  8. * Licensed under the Academic Free License version 2.1
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. * History:
  26. * ================================================================
  27. * 2009-11-15 Li XianJing <xianjimli@hotmail.com> created
  28. * 2010-09-22 Li XianJing <xianjimli@hotmail.com> add interactive feature.
  29. *
  30. */
  31. #include "ftk_window.h"
  32. #include "ftk_globals.h"
  33. #include "ftk_text_layout.h"
  34. #include "ftk_progress_bar.h"
  35. typedef struct _ProgressBarPrivInfo
  36. {
  37. int percent;
  38. int interactive;
  39. int mouse_pressed;
  40. FtkBitmap* bg;
  41. FtkBitmap* cursor;
  42. }PrivInfo;
  43. static Ret ftk_progress_bar_on_event(FtkWidget* thiz, FtkEvent* event)
  44. {
  45. return RET_OK;
  46. }
  47. static Ret ftk_progress_bar_on_paint(FtkWidget* thiz)
  48. {
  49. int fg_width = 0;
  50. DECL_PRIV0(thiz, priv);
  51. FtkGc gc = {0};
  52. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  53. return_val_if_fail(width > 4 && height > 4, RET_FAIL);
  54. gc.mask = FTK_GC_FG;
  55. gc.fg = ftk_widget_get_gc(thiz)->bg;
  56. ftk_canvas_set_gc(canvas, &gc);
  57. ftk_canvas_draw_rect(canvas, x, y, width, height, 1, 1);
  58. gc.fg = ftk_widget_get_gc(thiz)->fg;
  59. ftk_canvas_set_gc(canvas, &gc);
  60. fg_width = width * priv->percent/100;
  61. if(fg_width >= 10)
  62. {
  63. ftk_canvas_draw_rect(canvas, x+1, y+1, fg_width-2, height-2, 1, 1);
  64. if(fg_width < width && fg_width > 2)
  65. {
  66. ftk_canvas_clear_rect(canvas, x + fg_width - 3, y+1, 3, height-2);
  67. }
  68. }
  69. ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
  70. if(ftk_widget_get_text(thiz) != NULL)
  71. {
  72. int xoffset = 0;
  73. int yoffset = FTK_HALF(height);
  74. FtkTextLine line = {0};
  75. const char* text = ftk_widget_get_text(thiz);
  76. FtkTextLayout* text_layout = ftk_default_text_layout();
  77. gc.fg.a = 0xff;
  78. gc.fg.r = 0x00;
  79. gc.fg.g = 0x00;
  80. gc.fg.b = 0x00;
  81. ftk_canvas_set_gc(canvas, &gc);
  82. ftk_text_layout_init(text_layout, text, -1, canvas, width);
  83. if(ftk_text_layout_get_visual_line(text_layout, &line) == RET_OK)
  84. {
  85. xoffset = FTK_HALF(width - line.extent);
  86. ftk_canvas_draw_string(canvas, x + xoffset, y + yoffset, text, -1, 1);
  87. }
  88. }
  89. FTK_END_PAINT();
  90. }
  91. static Ret ftk_progress_bar_on_event_interactive(FtkWidget* thiz, FtkEvent* event)
  92. {
  93. DECL_PRIV0(thiz, priv);
  94. int percent = priv->percent;
  95. return_val_if_fail(thiz != NULL && event != NULL, RET_FAIL);
  96. if(event->type == FTK_EVT_KEY_UP)
  97. {
  98. switch(event->u.key.code)
  99. {
  100. case FTK_KEY_HOME:
  101. {
  102. percent = 0;
  103. break;
  104. }
  105. case FTK_KEY_END:
  106. {
  107. percent = 100;
  108. break;
  109. }
  110. case FTK_KEY_PAGEUP:
  111. {
  112. percent += 10;
  113. break;
  114. }
  115. case FTK_KEY_PAGEDOWN:
  116. {
  117. percent -= 10;
  118. break;
  119. }
  120. case FTK_KEY_LEFT:
  121. case FTK_KEY_DOWN:
  122. {
  123. percent--;
  124. break;
  125. }
  126. case FTK_KEY_UP:
  127. case FTK_KEY_RIGHT:
  128. {
  129. percent++;
  130. break;
  131. }
  132. default:break;
  133. }
  134. }
  135. else if(event->type == FTK_EVT_MOUSE_DOWN)
  136. {
  137. priv->mouse_pressed = 1;
  138. ftk_window_grab(ftk_widget_toplevel(thiz), thiz);
  139. }
  140. else if(event->type == FTK_EVT_MOUSE_MOVE && priv->mouse_pressed)
  141. {
  142. int width = ftk_widget_width(thiz);
  143. int offset = event->u.mouse.x - ftk_widget_left_abs(thiz);
  144. percent = 100 * offset / width;
  145. }
  146. else if(event->type == FTK_EVT_MOUSE_UP)
  147. {
  148. int width = ftk_widget_width(thiz);
  149. int offset = event->u.mouse.x - ftk_widget_left_abs(thiz);
  150. priv->mouse_pressed = 0;
  151. percent = 100 * offset / width;
  152. ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz);
  153. }
  154. else
  155. {
  156. return RET_OK;
  157. }
  158. ftk_progress_bar_set_percent(thiz, percent);
  159. return RET_OK;
  160. }
  161. static Ret ftk_progress_bar_on_paint_interactive(FtkWidget* thiz)
  162. {
  163. int ox = 0;
  164. int oy = 0;
  165. DECL_PRIV0(thiz, priv);
  166. FTK_BEGIN_PAINT(x, y, width, height, canvas);
  167. return_val_if_fail(thiz != NULL, RET_FAIL);
  168. oy = y + FTK_HALF(height - ftk_bitmap_height(priv->bg));
  169. ftk_canvas_draw_bg_image(canvas, priv->bg, FTK_BG_FOUR_CORNER, x, oy, width,
  170. ftk_bitmap_height(priv->bg));
  171. ox = x + width * priv->percent / 100 - FTK_HALF(ftk_bitmap_width(priv->cursor));
  172. oy = y + FTK_HALF(height) - FTK_HALF(ftk_bitmap_height(priv->cursor));
  173. if(ox < x)
  174. {
  175. ox = x;
  176. }
  177. if(ox > (x + width - ftk_bitmap_width(priv->cursor)))
  178. {
  179. ox = x + width - ftk_bitmap_width(priv->cursor);
  180. }
  181. ftk_canvas_draw_bitmap_simple(canvas, priv->cursor, 0, 0, ftk_bitmap_width(priv->cursor),
  182. ftk_bitmap_height(priv->cursor), ox, oy);
  183. FTK_END_PAINT();
  184. }
  185. static void ftk_progress_bar_destroy(FtkWidget* thiz)
  186. {
  187. if(thiz != NULL)
  188. {
  189. DECL_PRIV0(thiz, priv);
  190. FTK_BITMAP_UNREF(priv->bg);
  191. FTK_BITMAP_UNREF(priv->cursor);
  192. FTK_ZFREE(priv, sizeof(PrivInfo));
  193. }
  194. return;
  195. }
  196. FtkWidget* ftk_progress_bar_create(FtkWidget* parent, int x, int y, int width, int height)
  197. {
  198. FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
  199. return_val_if_fail(thiz != NULL, NULL);
  200. thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
  201. if(thiz->priv_subclass[0] != NULL)
  202. {
  203. thiz->on_event = ftk_progress_bar_on_event;
  204. thiz->on_paint = ftk_progress_bar_on_paint;
  205. thiz->destroy = ftk_progress_bar_destroy;
  206. ftk_widget_init(thiz, FTK_PROGRESS_BAR, 0, x, y, width, height,
  207. FTK_ATTR_TRANSPARENT|FTK_ATTR_INSENSITIVE);
  208. ftk_widget_append_child(parent, thiz);
  209. }
  210. else
  211. {
  212. FTK_FREE(thiz);
  213. }
  214. return thiz;
  215. }
  216. Ret ftk_progress_bar_set_percent(FtkWidget* thiz, int percent)
  217. {
  218. DECL_PRIV0(thiz, priv);
  219. return_val_if_fail(thiz != NULL, RET_FAIL);
  220. percent = percent < 0 ? 0 : percent;
  221. percent = percent > 100 ? 100 : percent;
  222. if(percent != priv->percent)
  223. {
  224. priv->percent = percent;
  225. ftk_widget_invalidate(thiz);
  226. }
  227. return RET_OK;
  228. }
  229. int ftk_progress_bar_get_percent(FtkWidget* thiz)
  230. {
  231. DECL_PRIV0(thiz, priv);
  232. return_val_if_fail(thiz != NULL, 0);
  233. return priv->percent;
  234. }
  235. Ret ftk_progress_bar_set_interactive(FtkWidget* thiz, int interactive)
  236. {
  237. DECL_PRIV0(thiz, priv);
  238. return_val_if_fail(thiz != NULL, RET_FAIL);
  239. priv->interactive = interactive;
  240. if(interactive)
  241. {
  242. if(priv->bg == NULL)
  243. {
  244. priv->bg = ftk_theme_load_image(ftk_default_theme(),
  245. "progressbar_i_bg"FTK_STOCK_IMG_SUFFIX);
  246. }
  247. if(priv->cursor == NULL)
  248. {
  249. priv->cursor = ftk_theme_load_image(ftk_default_theme(),
  250. "progressbar_i_cursor"FTK_STOCK_IMG_SUFFIX);
  251. }
  252. thiz->on_event = ftk_progress_bar_on_event_interactive;
  253. thiz->on_paint = ftk_progress_bar_on_paint_interactive;
  254. }
  255. else
  256. {
  257. thiz->on_event = ftk_progress_bar_on_event;
  258. thiz->on_paint = ftk_progress_bar_on_paint;
  259. }
  260. ftk_widget_set_insensitive(thiz, !interactive);
  261. return RET_OK;
  262. }