PageRenderTime 62ms CodeModel.GetById 18ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

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