/src/ftk_canvas.h

http://ftk.googlecode.com/ · C Header · 240 lines · 159 code · 51 blank · 30 comment · 70 complexity · a4e806fc4e1a3b0f44bdcce29d7c2e62 MD5 · raw file

  1. /*
  2. * File: ftk_canvas.h
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: drawing kit.
  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-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. * 2011-04-16 Li XianJing <xianjimli@hotmail.com> change to interface.
  29. *
  30. */
  31. #ifndef FTK_CANVAS_H
  32. #define FTK_CANVAS_H
  33. #include "ftk_gc.h"
  34. #include "ftk_display.h"
  35. FTK_BEGIN_DECLS
  36. typedef enum _FtkBgStyle
  37. {
  38. FTK_BG_NORMAL = 0,
  39. FTK_BG_TILE,
  40. FTK_BG_CENTER,
  41. FTK_BG_FOUR_CORNER
  42. }FtkBgStyle;
  43. struct _FtkCanvas;
  44. typedef struct _FtkCanvas FtkCanvas;
  45. struct _FtkCanvasVTable;
  46. typedef struct _FtkCanvasVTable FtkCanvasVTable;
  47. typedef Ret (*FtkCanvasSyncGc)(FtkCanvas* thiz);
  48. typedef Ret (*FtkCanvasSetClip)(FtkCanvas* thiz, FtkRegion* clip);
  49. typedef Ret (*FtkCanvasDrawPixels)(FtkCanvas* thiz, FtkPoint* points, int nr);
  50. typedef Ret (*FtkCanvasDrawLine)(FtkCanvas* thiz, int x1, int y1, int x2, int y2);
  51. typedef Ret (*FtkCanvasClearRect)(FtkCanvas* thiz, int x, int y, int w, int h);
  52. typedef Ret (*FtkCanvasDrawRect)(FtkCanvas* thiz, int x, int y, int w, int h, int round, int fill);
  53. typedef Ret (*FtkCanvasDrawBitmap)(FtkCanvas* thiz, FtkBitmap* bitmap, FtkRect* s, FtkRect* d, int alpha);
  54. typedef Ret (*FtkCanvasDrawString)(FtkCanvas* thiz, int x, int y, const char* str, int len, int vcenter);
  55. typedef int (*FtkCanvasGetCharExtent)(FtkCanvas* thiz, unsigned short code);
  56. typedef int (*FtkCanvasGetStrExtent)(FtkCanvas* thiz, const char* str, int len);
  57. typedef Ret (*FtkCanvasLockBuffer)(FtkCanvas* thiz, FtkBitmap** bitmap);
  58. typedef Ret (*FtkCanvasUnlockBuffer)(FtkCanvas* thiz);
  59. typedef void (*FtkCanvasDestroy)(FtkCanvas* thiz);
  60. struct _FtkCanvasVTable
  61. {
  62. FtkCanvasSyncGc sync_gc;
  63. FtkCanvasSetClip set_clip;
  64. FtkCanvasDrawPixels draw_pixels;
  65. FtkCanvasDrawLine draw_line;
  66. FtkCanvasClearRect clear_rect;
  67. FtkCanvasDrawRect draw_rect;
  68. FtkCanvasDrawBitmap draw_bitmap;
  69. FtkCanvasDrawString draw_string;
  70. FtkCanvasGetStrExtent get_str_extent;
  71. FtkCanvasGetCharExtent get_char_extent;
  72. FtkCanvasLockBuffer lock_buffer;
  73. FtkCanvasUnlockBuffer unlock_buffer;
  74. FtkCanvasDestroy destroy;
  75. };
  76. struct _FtkCanvas
  77. {
  78. FtkGc gc;
  79. int width;
  80. int height;
  81. const FtkCanvasVTable* vtable;
  82. char priv[1];
  83. };
  84. static inline Ret ftk_canvas_sync_gc(FtkCanvas* thiz)
  85. {
  86. Ret ret = RET_FAIL;
  87. if(thiz != NULL && thiz->vtable != NULL && thiz->vtable->sync_gc != NULL)
  88. {
  89. ret = thiz->vtable->sync_gc(thiz);
  90. }
  91. return ret;
  92. }
  93. static inline Ret ftk_canvas_set_clip(FtkCanvas* thiz, FtkRegion* clip)
  94. {
  95. Ret ret = RET_FAIL;
  96. if(thiz != NULL && thiz->vtable != NULL && thiz->vtable->set_clip != NULL)
  97. {
  98. ret = thiz->vtable->set_clip(thiz, clip);
  99. }
  100. return ret;
  101. }
  102. static inline Ret ftk_canvas_draw_pixels(FtkCanvas* thiz, FtkPoint* points, int nr)
  103. {
  104. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  105. && thiz->vtable->draw_pixels != NULL, RET_FAIL);
  106. return thiz->vtable->draw_pixels(thiz, points, nr);
  107. }
  108. static inline Ret ftk_canvas_draw_line(FtkCanvas* thiz, int x1, int y1, int x2, int y2)
  109. {
  110. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  111. && thiz->vtable->draw_line != NULL, RET_FAIL);
  112. return thiz->vtable->draw_line(thiz, x1, y1, x2, y2);
  113. }
  114. static inline Ret ftk_canvas_clear_rect(FtkCanvas* thiz, int x, int y, int w, int h)
  115. {
  116. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  117. && thiz->vtable->clear_rect != NULL, RET_FAIL);
  118. return thiz->vtable->clear_rect(thiz, x, y, w, h);
  119. }
  120. static inline Ret ftk_canvas_draw_rect(FtkCanvas* thiz, int x, int y, int w, int h, int round, int fill)
  121. {
  122. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  123. && thiz->vtable->draw_rect != NULL, RET_FAIL);
  124. return thiz->vtable->draw_rect(thiz, x, y, w, h, round, fill);
  125. }
  126. static inline Ret ftk_canvas_draw_bitmap(FtkCanvas* thiz, FtkBitmap* bmp, FtkRect* s, FtkRect* d, int alpha)
  127. {
  128. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  129. && thiz->vtable->draw_bitmap != NULL, RET_FAIL);
  130. return thiz->vtable->draw_bitmap(thiz, bmp, s, d, alpha);
  131. }
  132. static inline Ret ftk_canvas_draw_string(FtkCanvas* thiz, int x, int y,
  133. const char* str, int len, int vcenter)
  134. {
  135. len = (len < 0 && str != NULL) ? (int)strlen(str) : len;
  136. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  137. && thiz->vtable->draw_string != NULL, RET_FAIL);
  138. return thiz->vtable->draw_string(thiz, x, y, str, len, vcenter);
  139. }
  140. static inline int ftk_canvas_get_str_extent(FtkCanvas* thiz, const char* str, int len)
  141. {
  142. len = len >= 0 ? len : strlen(str);
  143. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  144. && thiz->vtable->get_str_extent != NULL, 0);
  145. return thiz->vtable->get_str_extent(thiz, str, len);
  146. }
  147. static inline int ftk_canvas_get_char_extent(FtkCanvas* thiz, unsigned short code)
  148. {
  149. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  150. && thiz->vtable->get_char_extent != NULL, 0);
  151. return thiz->vtable->get_char_extent(thiz, code);
  152. }
  153. static inline Ret ftk_canvas_lock_buffer(FtkCanvas* thiz, FtkBitmap** bitmap)
  154. {
  155. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  156. && thiz->vtable->lock_buffer != NULL, RET_FAIL);
  157. return thiz->vtable->lock_buffer(thiz, bitmap);
  158. }
  159. static inline Ret ftk_canvas_unlock_buffer(FtkCanvas* thiz)
  160. {
  161. return_val_if_fail(thiz != NULL && thiz->vtable != NULL
  162. && thiz->vtable->unlock_buffer != NULL, RET_FAIL);
  163. return thiz->vtable->unlock_buffer(thiz);
  164. }
  165. static inline void ftk_canvas_destroy(FtkCanvas* thiz)
  166. {
  167. if(thiz != NULL && thiz->vtable != NULL && thiz->vtable->destroy != NULL)
  168. {
  169. thiz->vtable->destroy(thiz);
  170. }
  171. return;
  172. }
  173. FtkGc* ftk_canvas_get_gc(FtkCanvas* thiz);
  174. Ret ftk_canvas_reset_gc(FtkCanvas* thiz, FtkGc* gc);
  175. Ret ftk_canvas_set_gc(FtkCanvas* thiz, FtkGc* gc);
  176. Ret ftk_canvas_set_clip_rect(FtkCanvas* thiz, FtkRect* rect);
  177. Ret ftk_canvas_set_clip_region(FtkCanvas* thiz, FtkRegion* region);
  178. Ret ftk_canvas_draw_vline(FtkCanvas* thiz, int x, int y, int h);
  179. Ret ftk_canvas_draw_hline(FtkCanvas* thiz, int x, int y, int w);
  180. Ret ftk_canvas_draw_bitmap_simple(FtkCanvas* thiz, FtkBitmap* b,
  181. int x, int y, int w, int h, int ox, int oy);
  182. Ret ftk_canvas_draw_bg_image(FtkCanvas* thiz, FtkBitmap* bitmap,
  183. FtkBgStyle style, int x, int y, int w, int h);
  184. const char* ftk_canvas_calc_str_visible_range(FtkCanvas* thiz,
  185. const char* start, int vstart, int vend, int width, int* ret_extent);
  186. Ret ftk_canvas_show(FtkCanvas* thiz, FtkDisplay* display, FtkRect* rect, int ox, int oy);
  187. FtkCanvas* ftk_canvas_create(int w, int h, FtkColor* clear_color);
  188. FTK_END_DECLS
  189. #endif/*FTK_CANVAS_H*/