PageRenderTime 51ms CodeModel.GetById 42ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ftk_group_box.c

http://ftk.googlecode.com/
C | 135 lines | 80 code | 25 blank | 30 comment | 15 complexity | b6a29ae94d71dbb71bde2f6983568dc5 MD5 | raw file
  1/*
  2 * File: ftk_group_box.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   radio group. 
  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/*
 26 * History:
 27 * ================================================================
 28 * 2009-11-15 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_theme.h"
 33#include "ftk_globals.h"
 34#include "ftk_group_box.h"
 35#include "ftk_check_button.h"
 36
 37typedef struct _GroupBoxPrivInfo
 38{
 39	FtkBitmap* bg;
 40}PrivInfo;
 41
 42static Ret ftk_group_box_on_event(FtkWidget* thiz, FtkEvent* event)
 43{
 44	return RET_OK;
 45}
 46
 47static Ret ftk_group_box_on_paint(FtkWidget* thiz)
 48{
 49	DECL_PRIV0(thiz, priv);
 50	FTK_BEGIN_PAINT(x, y, width, height, canvas);
 51
 52	return_val_if_fail(priv->bg != NULL, RET_FAIL);
 53
 54	ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz)); 
 55	ftk_canvas_draw_bg_image(canvas, priv->bg, FTK_BG_FOUR_CORNER, x, y, width, height);	
 56
 57	if(ftk_widget_get_text(thiz) != NULL)
 58	{
 59		int yoffset = 2;
 60		int xoffset = 10;
 61		FtkTextLine line = {0};
 62		int font_height = ftk_widget_get_font_size(thiz);
 63		const char* text = ftk_widget_get_text(thiz);
 64		FtkTextLayout* text_layout = ftk_default_text_layout();
 65
 66		ftk_text_layout_init(text_layout, text, -1, canvas, width - 2 * xoffset); 
 67		if(ftk_text_layout_get_visual_line(text_layout, &line) == RET_OK)
 68		{
 69			FtkGc gc = {0};
 70			gc.mask = FTK_GC_FG;
 71			/*fill bg with the first pixel of bg image.*/
 72			gc.fg = ftk_bitmap_get_pixel(priv->bg, 0, 0);
 73			
 74			ftk_canvas_set_gc(canvas, &gc);
 75			ftk_canvas_draw_rect(canvas, x + xoffset, y + yoffset, line.extent, font_height, 0, 1);
 76
 77			ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz)); 
 78			ftk_canvas_draw_string(canvas, x + xoffset, y + yoffset + font_height, line.text, line.len, 0);
 79		}
 80	}
 81
 82	FTK_END_PAINT();
 83}
 84
 85static void ftk_group_box_destroy(FtkWidget* thiz)
 86{
 87	DECL_PRIV0(thiz, priv);
 88
 89	if(priv->bg != NULL)
 90	{
 91		ftk_bitmap_unref(priv->bg);
 92	}
 93	FTK_ZFREE(thiz->priv_subclass[0], sizeof(PrivInfo));
 94
 95	return;
 96}
 97
 98FtkWidget* ftk_group_box_create(FtkWidget* parent, int x, int y, int width, int height)
 99{
100	FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget) + sizeof(PrivInfo));
101	return_val_if_fail(thiz != NULL, NULL);
102
103	thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
104	if(thiz->priv_subclass[0] != NULL)
105	{
106		DECL_PRIV0(thiz, priv);
107		thiz->on_event = ftk_group_box_on_event;
108		thiz->on_paint = ftk_group_box_on_paint;
109		thiz->destroy  = ftk_group_box_destroy;
110
111		priv->bg = ftk_theme_load_image(ftk_default_theme(), "groupbox_bg"FTK_STOCK_IMG_SUFFIX);
112		ftk_widget_init(thiz, FTK_GROUP_BOX, 0, x, y, width, height, FTK_ATTR_TRANSPARENT);
113		ftk_widget_append_child(parent, thiz);
114	}
115
116	return thiz;
117}
118
119Ret  ftk_group_box_set_checked(FtkWidget* thiz, FtkWidget* radio)
120{
121	FtkWidget* iter = NULL;
122	return_val_if_fail(thiz != NULL && radio != NULL, RET_FAIL);
123
124	iter = thiz->children;
125
126	while(iter != NULL)
127	{
128		ftk_check_button_set_checked(iter, iter == radio);
129
130		iter = iter->next;
131	}
132
133	return RET_OK;
134}
135