PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/ftk_status_item.c

http://ftk.googlecode.com/
C | 171 lines | 119 code | 23 blank | 29 comment | 15 complexity | 6c32520e51c59c1254db082e83412df8 MD5 | raw file
  1/*
  2 * File: ftk_status_item.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   item on the status panel. 
  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-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_window.h"
 34#include "ftk_globals.h"
 35#include "ftk_status_item.h"
 36#include "ftk_status_panel.h"
 37
 38typedef struct _StutusItemPrivInfo
 39{
 40	int pos;
 41	FtkListener listener;
 42	void* listener_ctx;
 43}PrivInfo;
 44
 45static Ret ftk_status_item_on_event(FtkWidget* thiz, FtkEvent* event)
 46{
 47	Ret ret = RET_OK;
 48	DECL_PRIV0(thiz, priv);
 49	switch(event->type)
 50	{
 51		case FTK_EVT_MOUSE_DOWN:
 52		{
 53			ftk_widget_set_active(thiz, 1);
 54			ftk_window_grab(ftk_widget_toplevel(thiz), thiz);
 55			break;
 56		}
 57		case FTK_EVT_MOUSE_UP:
 58		{
 59			ftk_widget_set_active(thiz, 0);
 60			ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz);
 61			ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
 62			break;
 63		}
 64		case FTK_EVT_KEY_DOWN:
 65		{
 66			if(FTK_IS_ACTIVE_KEY(event->u.key.code))
 67			{
 68				ftk_widget_set_active(thiz, 1);
 69			}
 70			break;
 71		}
 72		case FTK_EVT_KEY_UP:
 73		{
 74			if(FTK_IS_ACTIVE_KEY(event->u.key.code) && ftk_widget_is_active(thiz))
 75			{
 76				ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
 77				ftk_widget_set_active(thiz, 0);
 78			}
 79			break;
 80		}
 81		default:break;
 82	}
 83
 84	return ret;
 85}
 86
 87static Ret ftk_status_item_on_paint(FtkWidget* thiz)
 88{
 89	FTK_BEGIN_PAINT(x, y, width, height, canvas);
 90	ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz)); 
 91	if(ftk_widget_get_text(thiz) != NULL)
 92	{
 93		int yoffset = FTK_HALF(height);
 94		FtkTextLine line = {0};
 95		const char* text = ftk_widget_get_text(thiz);
 96		FtkTextLayout* text_layout = ftk_default_text_layout();
 97
 98		ftk_text_layout_init(text_layout, text, -1, canvas, width); 
 99		if(ftk_text_layout_get_visual_line(text_layout, &line) == RET_OK)
100		{
101			ftk_canvas_draw_string(canvas, x, y + yoffset, line.text, line.len, 1);
102		}
103	}
104
105	FTK_END_PAINT();
106}
107
108static void ftk_status_item_destroy(FtkWidget* thiz)
109{
110	if(thiz != NULL)
111	{
112		DECL_PRIV0(thiz, priv);
113		FTK_FREE(priv);
114	}
115
116	return;
117}
118
119FtkWidget* ftk_status_item_create(FtkWidget* parent, int pos, int width)
120{
121	FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
122	return_val_if_fail(thiz != NULL, NULL);
123
124	thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
125	if(thiz->priv_subclass[0] != NULL)
126	{
127		thiz->on_event = ftk_status_item_on_event;
128		thiz->on_paint = ftk_status_item_on_paint;
129		thiz->destroy  = ftk_status_item_destroy;
130
131		ftk_widget_init(thiz, FTK_STATUS_ITEM, 0, 0, 0, width, 0, FTK_ATTR_TRANSPARENT);
132		ftk_status_item_set_position(thiz, pos);
133		ftk_status_panel_add(parent, thiz);
134	}
135	else
136	{
137		FTK_FREE(thiz);
138	}
139
140	return thiz;
141}
142
143Ret         ftk_status_item_set_position(FtkWidget* thiz, int pos)
144{
145	DECL_PRIV0(thiz, priv);
146	return_val_if_fail(thiz != NULL, RET_OK);
147
148	priv->pos = pos;
149
150	return RET_OK;
151}
152
153int         ftk_status_item_get_position(FtkWidget* thiz)
154{
155	DECL_PRIV0(thiz, priv);
156	return_val_if_fail(thiz != NULL, 0);
157
158	return priv->pos;
159}
160
161Ret ftk_status_item_set_clicked_listener(FtkWidget* thiz, FtkListener listener, void* ctx)
162{
163	DECL_PRIV0(thiz, priv);
164	return_val_if_fail(thiz != NULL, RET_FAIL);
165
166	priv->listener_ctx = ctx;
167	priv->listener = listener;
168
169	return RET_OK;
170}
171