PageRenderTime 867ms CodeModel.GetById 694ms app.highlight 9ms RepoModel.GetById 162ms app.codeStats 0ms

/src/ftk_menu_item.c

http://ftk.googlecode.com/
C | 153 lines | 104 code | 20 blank | 29 comment | 12 complexity | 592b1bcccd0e0ae9d75ba5122202b76d MD5 | raw file
  1/*
  2 * File: ftk_menu_item.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   menu item. 
  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-30 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_canvas.h"
 34#include "ftk_menu_item.h"
 35#include "ftk_globals.h"
 36#include "ftk_icon_cache.h"
 37#include "ftk_menu_panel.h"
 38
 39typedef struct _MenuItemPrivInfo
 40{
 41	FtkListener listener;
 42	void* listener_ctx;
 43}PrivInfo;
 44
 45static Ret ftk_menu_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				ftk_widget_set_active(thiz, 0);
 77				ret = FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
 78			}
 79			break;
 80		}
 81		default:break;
 82	}
 83
 84	return ret;
 85}
 86
 87static Ret ftk_menu_item_on_paint(FtkWidget* thiz)
 88{
 89	FTK_BEGIN_PAINT(x, y, width, height, canvas);
 90	return_val_if_fail(width > 0 && height > 0, RET_FAIL);
 91
 92	ftk_canvas_reset_gc(canvas, ftk_widget_get_gc(thiz));
 93	if(ftk_widget_get_text(thiz))
 94	{
 95		const char* text = ftk_widget_get_text(thiz);
 96		int fh = ftk_widget_get_font_size(thiz);
 97		int fw = ftk_canvas_get_str_extent(canvas, text, -1);
 98		int dx = FTK_HALF(width - fw);
 99		int dy = FTK_HALF(height);
100
101		assert(fh < height && fw < width);
102		ftk_canvas_draw_string(canvas, x + dx, y + dy, text, -1, 1);
103	}
104	
105	FTK_END_PAINT();
106}
107
108static void ftk_menu_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_menu_item_create(FtkWidget* parent)
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 != NULL)
126	{
127		thiz->on_event = ftk_menu_item_on_event;
128		thiz->on_paint = ftk_menu_item_on_paint;
129		thiz->destroy  = ftk_menu_item_destroy;
130
131		ftk_widget_init(thiz, FTK_MENU_ITEM, 0, 0, 0, 0, 0, FTK_ATTR_TRANSPARENT|FTK_ATTR_BG_FOUR_CORNER);
132
133		ftk_menu_panel_add(parent, thiz);
134	}
135	else
136	{
137		FTK_FREE(thiz);
138	}
139
140	return thiz;
141}
142
143Ret ftk_menu_item_set_clicked_listener(FtkWidget* thiz, FtkListener listener, void* ctx)
144{
145	DECL_PRIV0(thiz, priv);
146	return_val_if_fail(thiz != NULL, RET_FAIL);
147
148	priv->listener_ctx = ctx;
149	priv->listener = listener;
150
151	return RET_OK;
152}
153