PageRenderTime 63ms CodeModel.GetById 34ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/src/ftk_wait_box.c

http://ftk.googlecode.com/
C | 143 lines | 89 code | 25 blank | 29 comment | 10 complexity | 23957117847164e17bcbc2c676b1b2c2 MD5 | raw file
  1/*
  2 * File: ftk_wait_box.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   wait box 
  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-19 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_globals.h"
 33#include "ftk_wait_box.h"
 34#include "ftk_source_timer.h"
 35
 36typedef struct _WaitBoxPrivInfo
 37{
 38	int offset;
 39	int waiting;
 40	FtkSource* timer;
 41	FtkBitmap* bitmap;
 42}PrivInfo;
 43
 44static Ret ftk_wait_box_on_event(FtkWidget* thiz, FtkEvent* event)
 45{
 46	return RET_OK;
 47}
 48
 49static Ret ftk_wait_box_on_paint(FtkWidget* thiz)
 50{
 51	DECL_PRIV0(thiz, priv);
 52	int bitmap_w = ftk_bitmap_width(priv->bitmap);
 53	int bitmap_h = ftk_bitmap_height(priv->bitmap);
 54	FTK_BEGIN_PAINT(x, y, width, height, canvas);
 55
 56	(void)width;
 57	(void)height;
 58
 59	if(priv->waiting)
 60	{
 61		priv->offset = priv->offset < bitmap_h ? priv->offset : 0;
 62		ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, priv->offset, bitmap_w, bitmap_w, x, y);
 63		priv->offset += bitmap_w;
 64	}
 65
 66	FTK_END_PAINT();
 67}
 68
 69static void ftk_wait_box_destroy(FtkWidget* thiz)
 70{
 71	if(thiz != NULL)
 72	{
 73		DECL_PRIV0(thiz, priv);
 74
 75		if(priv->waiting)
 76		{
 77			ftk_wait_box_stop_waiting(thiz);
 78		}
 79		ftk_source_unref(priv->timer);
 80		ftk_bitmap_unref(priv->bitmap);
 81		FTK_ZFREE(priv, sizeof(PrivInfo));
 82	}
 83
 84	return;
 85}
 86
 87FtkWidget* ftk_wait_box_create(FtkWidget* parent, int x, int y, int w, int h)
 88{
 89	FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
 90	return_val_if_fail(thiz != NULL, NULL);
 91
 92	thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
 93	if(thiz->priv_subclass[0] != NULL)
 94	{
 95		int w = 0;
 96		DECL_PRIV0(thiz, priv);
 97
 98		thiz->on_event = ftk_wait_box_on_event;
 99		thiz->on_paint = ftk_wait_box_on_paint;
100		thiz->destroy  = ftk_wait_box_destroy;
101
102		priv->bitmap = ftk_theme_load_image(ftk_default_theme(), "wait_box"FTK_STOCK_IMG_SUFFIX);
103		assert(priv->bitmap != NULL);
104		
105		w = ftk_bitmap_width(priv->bitmap);
106		ftk_widget_init(thiz, FTK_WAIT_BOX, 0, x, y, w, w, FTK_ATTR_TRANSPARENT|FTK_ATTR_INSENSITIVE);
107		ftk_widget_set_attr(thiz, FTK_ATTR_TRANSPARENT|FTK_ATTR_INSENSITIVE);
108
109		priv->timer = ftk_source_timer_create(500, (FtkTimer)ftk_widget_invalidate, thiz);
110		ftk_widget_append_child(parent, thiz);
111	}
112	else
113	{
114		FTK_FREE(thiz);
115	}
116
117	return thiz;
118}
119
120Ret ftk_wait_box_start_waiting(FtkWidget* thiz)
121{
122	DECL_PRIV0(thiz, priv);
123	return_val_if_fail(priv != NULL, RET_FAIL);
124	return_val_if_fail(!priv->waiting, RET_FAIL);
125
126	priv->offset = 0;
127	priv->waiting = 1;
128	ftk_source_ref(priv->timer);
129
130	return ftk_main_loop_add_source(ftk_default_main_loop(), priv->timer);
131}
132
133Ret ftk_wait_box_stop_waiting(FtkWidget* thiz)
134{
135	DECL_PRIV0(thiz, priv);
136	return_val_if_fail(priv != NULL, RET_FAIL);
137	return_val_if_fail(priv->waiting, RET_FAIL);
138	
139	priv->waiting = 0;
140
141	return ftk_main_loop_remove_source(ftk_default_main_loop(), priv->timer);
142}
143