/src/canvas/win32/ftk_canvas_win32.cpp

http://ftk.googlecode.com/ · C++ · 241 lines · 172 code · 41 blank · 28 comment · 13 complexity · 69fdebaf6e2cdd50d56aabebd271a699 MD5 · raw file

  1. /*
  2. * File: ftk_canvas_win32.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: win32 canvas implemntation.
  5. *
  6. * Copyright (c) 2009 - 2011 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. * 2011-04-18 Li XianJing <xianjimli@hotmail.com> created
  28. */
  29. #include "ftk_log.h"
  30. #include "ftk_util.h"
  31. #include "ftk_bitmap.h"
  32. #include "ftk_canvas.h"
  33. #include "ftk_globals.h"
  34. #ifdef VC6
  35. #define ULONG_PTR unsigned long*
  36. #endif
  37. #include <windows.h>
  38. #include <gdiplus.h>
  39. #include "ftk_win32.h"
  40. typedef struct _CanvasWin32PrivInfo
  41. {
  42. int w;
  43. int h;
  44. Pen* pen;
  45. Graphics* grapics;
  46. FtkBitmap* bitmap;
  47. }PrivInfo;
  48. static Ret ftk_canvas_win32_sync_gc(FtkCanvas* thiz)
  49. {
  50. int size = 0;
  51. DECL_PRIV(thiz, priv);
  52. FtkColor c = thiz->gc.fg;
  53. if(thiz->gc.font != NULL)
  54. {
  55. char desc[64] = {0};
  56. size = ftk_font_desc_get_size(thiz->gc.font);
  57. }
  58. return RET_OK;
  59. }
  60. static Ret ftk_canvas_win32_set_clip(FtkCanvas* thiz, FtkRegion* clip)
  61. {
  62. return RET_OK;
  63. }
  64. static Ret ftk_canvas_win32_draw_pixels(FtkCanvas* thiz, FtkPoint* points, int nr)
  65. {
  66. DECL_PRIV(thiz, priv);
  67. return RET_OK;
  68. }
  69. static Ret ftk_canvas_win32_draw_line(FtkCanvas* thiz, int x1, int y1, int x2, int y2)
  70. {
  71. DECL_PRIV(thiz, priv);
  72. return RET_OK;
  73. }
  74. static Ret ftk_canvas_win32_clear_rect(FtkCanvas* thiz, int x, int y, int w, int h)
  75. {
  76. DECL_PRIV(thiz, priv);
  77. priv->grapics->DrawLine(priv->pen, x, y, x + w, y + h);
  78. return RET_OK;
  79. }
  80. static Ret ftk_canvas_win32_draw_rect(FtkCanvas* thiz, int x, int y, int w, int h,
  81. int round, int fill)
  82. {
  83. DECL_PRIV(thiz, priv);
  84. if(fill)
  85. {
  86. Rect r(x, y, w, h);
  87. FtkColor c = thiz->gc.fg;
  88. SolidBrush brush(Color(c.a, c.r, c.g, c.b));
  89. if(round)
  90. {
  91. priv->grapics->FillRectangle(&brush, r);
  92. }
  93. else
  94. {
  95. priv->grapics->FillRectangle(&brush, r);
  96. }
  97. }
  98. else
  99. {
  100. if(round)
  101. {
  102. priv->grapics->DrawRectangle(priv->pen, x, y, w, h);
  103. }
  104. else
  105. {
  106. priv->grapics->DrawRectangle(priv->pen, x, y, w, h);
  107. }
  108. }
  109. return RET_OK;
  110. }
  111. static Ret ftk_canvas_win32_draw_bitmap(FtkCanvas* thiz, FtkBitmap* bitmap,
  112. FtkRect* src_r, FtkRect* dst_r, int alpha)
  113. {
  114. Ret ret = RET_FAIL;
  115. DECL_PRIV(thiz, priv);
  116. return ret;
  117. }
  118. static Ret ftk_canvas_win32_draw_string(FtkCanvas* thiz, int x, int y,
  119. const char* str, int len, int vcenter)
  120. {
  121. size_t size = 0;
  122. WCHAR* utf16 = NULL;
  123. DECL_PRIV(thiz, priv);
  124. FtkColor c = thiz->gc->fg;
  125. Font font(FontFamily::GenericSerif(),
  126. ftk_font_desc_get_size(thiz->gc.font));
  127. SolidBrush brush(Color(c.a, c.r, c.g, c.b));
  128. utf16 = new WCHAR[len+1];
  129. if((size = MultiByteToWideChar(CP_ACP, 0, str, len, utf16, len * sizeof(WCHAR))) > 0)
  130. {
  131. priv->grapics->DrawString(utf16, size, &font, Point(x, y), &brush);
  132. }
  133. delete utf16;
  134. return RET_OK;
  135. }
  136. static Ret fk_canvas_win32_lock_buffer(FtkCanvas* thiz, FtkBitmap** bitmap)
  137. {
  138. DECL_PRIV(thiz, priv);
  139. return RET_OK;
  140. }
  141. static Ret ftk_canvas_win32_unlock_buffer(FtkCanvas* thiz)
  142. {
  143. return RET_OK;
  144. }
  145. static void ftk_canvas_win32_destroy(FtkCanvas* thiz)
  146. {
  147. DECL_PRIV(thiz, priv);
  148. if(thiz != NULL)
  149. {
  150. FTK_FREE(thiz);
  151. }
  152. return;
  153. }
  154. static int ftk_canvas_win32_get_char_extent(FtkCanvas* thiz, unsigned short code)
  155. {
  156. return 0;
  157. }
  158. static int ftk_canvas_win32_get_str_extent(FtkCanvas* thiz, const char* str, int len)
  159. {
  160. return 0;
  161. }
  162. static const FtkCanvasVTable g_canvas_win32_vtable=
  163. {
  164. ftk_canvas_win32_sync_gc,
  165. ftk_canvas_win32_set_clip,
  166. ftk_canvas_win32_draw_pixels,
  167. ftk_canvas_win32_draw_line,
  168. ftk_canvas_win32_clear_rect,
  169. ftk_canvas_win32_draw_rect,
  170. ftk_canvas_win32_draw_bitmap,
  171. ftk_canvas_win32_draw_string,
  172. ftk_canvas_win32_get_str_extent,
  173. ftk_canvas_win32_get_char_extent,
  174. fk_canvas_win32_lock_buffer,
  175. ftk_canvas_win32_unlock_buffer,
  176. ftk_canvas_win32_destroy
  177. };
  178. FtkCanvas* ftk_canvas_create(int w, int h, FtkColor* clear_color)
  179. {
  180. FtkCanvas* thiz = NULL;
  181. return_val_if_fail(w > 0 && h > 0 && clear_color != NULL, NULL);
  182. thiz = (FtkCanvas*)FTK_ZALLOC(sizeof(FtkCanvas) + sizeof(PrivInfo));
  183. if(thiz != NULL)
  184. {
  185. Color c(0xff, 0, 0, 0);
  186. Bitmap* bitmap = NULL;
  187. DECL_PRIV(thiz, priv);
  188. thiz->gc.bg = *clear_color;
  189. thiz->gc.fg.a = 0xff;
  190. thiz->gc.fg.r = 0xff - clear_color->r;
  191. thiz->gc.fg.g = 0xff - clear_color->g;
  192. thiz->gc.fg.b = 0xff - clear_color->b;
  193. thiz->gc.mask = FTK_GC_FG | FTK_GC_BG;
  194. thiz->vtable = &g_canvas_win32_vtable;
  195. priv->pen = new Pen(c, 1.0);
  196. priv->bitmap = ftk_bitmap_create(w, h, *clear_color);
  197. priv->grapics = new Graphics((Bitmap*)ftk_bitmap_get_native(priv->bitmap));
  198. }
  199. return thiz;
  200. }