PageRenderTime 110ms CodeModel.GetById 27ms app.highlight 40ms RepoModel.GetById 24ms app.codeStats 1ms

/src/ftk_scroll_bar.c

http://ftk.googlecode.com/
C | 391 lines | 292 code | 68 blank | 31 comment | 48 complexity | ff4184f3a8806b660a9d0d8fd4807a67 MD5 | raw file
  1/*
  2 * File: ftk_scroll_bar.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   scroll bar
  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-20 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_window.h"
 33#include "ftk_globals.h"
 34#include "ftk_scroll_bar.h"
 35
 36typedef struct _ScrollBarPrivInfo
 37{
 38	int tracker_pos;
 39	int tracker_size;
 40
 41	int drag_enabled;
 42	int mouse_pressed;
 43	int last_mouse_pos;
 44
 45	int value;
 46	int vertical;
 47	int max_value;
 48	int page_delta;
 49	FtkBitmap* bitmap;
 50	void* listener_ctx;
 51	FtkListener listener;
 52}PrivInfo;
 53
 54static Ret ftk_scroll_bar_on_event(FtkWidget* thiz, FtkEvent* event)
 55{
 56	int x = 0;
 57	int y = 0;
 58	Ret ret = RET_OK;
 59	DECL_PRIV0(thiz, priv);
 60
 61	if(priv->page_delta == priv->max_value)
 62	{
 63		/*one page, no scroll*/
 64		return RET_OK;
 65	}
 66
 67	switch(event->type)
 68	{
 69		case FTK_EVT_KEY_DOWN:
 70		{
 71			if(event->u.key.code == FTK_KEY_DOWN)
 72			{
 73				ftk_scroll_bar_inc(thiz);
 74				ret = RET_REMOVE;
 75			}
 76			else if(event->u.key.code == FTK_KEY_UP)
 77			{
 78				ftk_scroll_bar_dec(thiz);
 79				ret = RET_REMOVE;
 80			}
 81			else if(event->u.key.code == FTK_KEY_PAGEDOWN)
 82			{
 83				ftk_scroll_bar_pagedown(thiz);
 84			}
 85			else if(event->u.key.code == FTK_KEY_PAGEUP)
 86			{
 87				ftk_scroll_bar_pageup(thiz);
 88			}
 89			break;
 90		}
 91		case FTK_EVT_MOUSE_DOWN:
 92		{
 93			int pos = 0;
 94			int value = 0;
 95
 96			x = event->u.mouse.x;
 97			y = event->u.mouse.y;
 98
 99			priv->mouse_pressed = 1;
100			ftk_window_grab(ftk_widget_toplevel(thiz), thiz);
101
102			pos = priv->vertical ? y : x;
103			priv->drag_enabled = pos >= priv->tracker_pos && pos < (priv->tracker_pos + priv->tracker_size);
104
105			if(!priv->drag_enabled)
106			{
107				value = pos < priv->tracker_pos ? priv->value - priv->page_delta : priv->value + priv->page_delta;
108				ftk_scroll_bar_set_value(thiz, value);
109			}
110		
111			priv->last_mouse_pos = pos;
112
113			break;
114		}
115		case FTK_EVT_MOUSE_MOVE:
116		{
117			int pos = 0;
118			int delta =0;
119			int value = 0;
120			int width = 0;
121			int height = 0;
122			
123			if(!priv->drag_enabled) break;
124			
125			x = event->u.mouse.x;
126			y = event->u.mouse.y;
127			pos = priv->vertical ? y : x;
128			delta = pos - priv->last_mouse_pos;
129
130			width = ftk_widget_width(thiz);
131			height = ftk_widget_height(thiz);
132
133			value = priv->vertical ? priv->max_value * delta/height : priv->max_value * delta/width;
134
135			if(value != 0)
136			{
137				value += priv->value;
138				priv->last_mouse_pos = pos;
139				ftk_scroll_bar_set_value(thiz, value);
140			}
141
142			break;
143		}
144		case FTK_EVT_MOUSE_UP:
145		{
146			priv->drag_enabled = 0;
147			priv->mouse_pressed = 0;
148			ftk_window_ungrab(ftk_widget_toplevel(thiz), thiz);
149
150			break;
151		}
152		default:break;
153	}
154
155	return ret;
156}
157
158static Ret ftk_scroll_bar_on_paint(FtkWidget* thiz)
159{
160	int i = 0;
161	int dx = 0;
162	int dy = 0;
163	int fill_size = 0;
164	int half_size = 0;
165	int bitmap_width = 0;
166	int bitmap_height = 0;
167	DECL_PRIV0(thiz, priv);
168	FTK_BEGIN_PAINT(x, y, width, height, canvas);
169
170	return_val_if_fail(priv->bitmap != NULL, RET_FAIL);
171
172	bitmap_width = ftk_bitmap_width(priv->bitmap);
173	bitmap_height = ftk_bitmap_height(priv->bitmap);
174	
175	if(priv->vertical)
176	{
177		priv->tracker_size = height * priv->page_delta/priv->max_value;
178		priv->tracker_size = priv->tracker_size < bitmap_height ? bitmap_height : priv->tracker_size;
179
180		dy = height * priv->value / priv->max_value;
181		dy = (dy + priv->tracker_size) < height ? dy : height - priv->tracker_size;
182	
183		priv->tracker_pos = dy + ftk_widget_top_abs(thiz);
184		fill_size = priv->tracker_size - bitmap_height;
185
186		dx += x;
187		dy += y;
188		half_size = bitmap_height/2;
189		
190		ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, 0, bitmap_width, half_size, dx, dy);
191		for(i = 0; i < fill_size; i++)
192		{
193			ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, half_size, bitmap_width, 1, dx, dy + i + half_size);
194		}
195		ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, half_size, bitmap_width, half_size, dx, dy + half_size + fill_size);
196	}
197	else
198	{
199		priv->tracker_size = width * priv->page_delta/priv->max_value;
200		priv->tracker_size = priv->tracker_size < bitmap_width ? bitmap_width : priv->tracker_size;
201
202		dx = width * priv->value / priv->max_value;
203		dx = (dx + priv->tracker_size) < width ? dx : width - priv->tracker_size;
204	
205		priv->tracker_pos = dx + ftk_widget_left_abs(thiz);
206		fill_size = priv->tracker_size - bitmap_width;
207
208		dy += y;
209		dx += x;
210		half_size = bitmap_width/2;
211		
212		ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, 0, 0, half_size, bitmap_height, dx, dy);
213		for(i = 0; i < fill_size; i++)
214		{
215			ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, half_size, 0, 1, bitmap_height, dx + i + half_size, dy);
216		}
217		ftk_canvas_draw_bitmap_simple(canvas, priv->bitmap, half_size, 0, half_size, bitmap_height, dx + fill_size + half_size, dy);
218	}
219
220	FTK_END_PAINT();
221}
222
223static void ftk_scroll_bar_destroy(FtkWidget* thiz)
224{
225	if(thiz != NULL)
226	{
227		DECL_PRIV0(thiz, priv);
228		ftk_bitmap_unref(priv->bitmap);
229		FTK_ZFREE(priv, sizeof(PrivInfo));
230	}
231
232	return;
233}
234
235FtkWidget* ftk_scroll_bar_create(FtkWidget* parent, int x, int y, int width, int height)
236{
237	FtkWidget* thiz = (FtkWidget*)FTK_ZALLOC(sizeof(FtkWidget));
238	return_val_if_fail(thiz != NULL, NULL);
239
240	thiz->priv_subclass[0] = (PrivInfo*)FTK_ZALLOC(sizeof(PrivInfo));
241	if(thiz != NULL)
242	{
243		DECL_PRIV0(thiz, priv);
244		thiz->on_event = ftk_scroll_bar_on_event;
245		thiz->on_paint = ftk_scroll_bar_on_paint;
246		thiz->destroy  = ftk_scroll_bar_destroy;
247
248		if(width < height)
249		{
250			/*vertical*/
251			priv->vertical = 1;
252			priv->bitmap = ftk_theme_load_image(ftk_default_theme(), 
253				"scrollbar_handle_vertical"FTK_STOCK_IMG_SUFFIX);
254			width = ftk_bitmap_width(priv->bitmap);
255			assert(width < height);
256		}
257		else
258		{
259			priv->vertical = 0;
260			priv->bitmap = ftk_theme_load_image(ftk_default_theme(), 
261				"scrollbar_handle_horizontal"FTK_STOCK_IMG_SUFFIX);
262			height = ftk_bitmap_height(priv->bitmap);	
263			assert(width > height);
264		}
265
266		ftk_widget_init(thiz, width < height ? FTK_SCROLL_VBAR : FTK_SCROLL_HBAR, 0, 
267			x, y, width, height, FTK_ATTR_TRANSPARENT);
268		ftk_widget_append_child(parent, thiz);
269	}
270
271	return thiz;
272}
273
274Ret ftk_scroll_bar_set_bitmap(FtkWidget* thiz, FtkBitmap* bitmap)
275{
276	DECL_PRIV0(thiz, priv);
277	return_val_if_fail(priv != NULL, RET_FAIL);
278
279	ftk_bitmap_unref(priv->bitmap);
280	priv->bitmap = bitmap;
281	ftk_bitmap_ref(priv->bitmap);
282
283	return RET_OK;
284}
285
286Ret ftk_scroll_bar_set_param(FtkWidget* thiz, int value, int max_value, int page_delta)
287{
288	DECL_PRIV0(thiz, priv);
289	return_val_if_fail(priv != NULL, RET_FAIL);
290	return_val_if_fail(value <= max_value && page_delta <= max_value, RET_FAIL);
291	return_val_if_fail(max_value > 0 && page_delta > 0, RET_FAIL);
292
293	if(priv->value == value 
294		&& priv->max_value == max_value 
295		&& priv->page_delta == page_delta)
296	{
297		return RET_OK;
298	}
299
300	priv->value      = value < 0 ? 0 : value;
301	priv->max_value  = max_value;
302	priv->page_delta = page_delta;
303	ftk_widget_invalidate(thiz);	
304
305	return RET_OK;
306}
307
308Ret ftk_scroll_bar_set_listener(FtkWidget* thiz, FtkListener listener, void* ctx)
309{
310	DECL_PRIV0(thiz, priv);
311	return_val_if_fail(priv != NULL, RET_FAIL);
312
313	priv->listener = listener;
314	priv->listener_ctx = ctx;
315
316	return RET_OK;
317}
318
319int ftk_scroll_bar_get_value(FtkWidget* thiz)
320{
321	DECL_PRIV0(thiz, priv);
322	return_val_if_fail(priv != NULL, 0);
323
324	return priv->value;
325}
326
327int ftk_scroll_bar_get_max_value(FtkWidget* thiz)
328{
329	DECL_PRIV0(thiz, priv);
330	return_val_if_fail(priv != NULL, 0);
331
332	return priv->max_value;
333}
334
335Ret ftk_scroll_bar_inc(FtkWidget* thiz)
336{
337	int value = 0;
338	DECL_PRIV0(thiz, priv);
339	return_val_if_fail(priv != NULL, RET_FAIL);
340	value = priv->value + 1;
341
342	return ftk_scroll_bar_set_value(thiz, value);
343}
344
345Ret ftk_scroll_bar_dec(FtkWidget* thiz)
346{
347	int value = 0;
348	DECL_PRIV0(thiz, priv);
349	return_val_if_fail(priv != NULL, RET_FAIL);
350	value = priv->value - 1;
351	
352	return ftk_scroll_bar_set_value(thiz, value);
353}
354
355Ret ftk_scroll_bar_pageup(FtkWidget* thiz)
356{
357	int value = 0;
358	DECL_PRIV0(thiz, priv);
359	return_val_if_fail(priv != NULL, RET_FAIL);
360	value = priv->value - priv->page_delta;
361	
362	return ftk_scroll_bar_set_value(thiz, value);
363}
364
365Ret ftk_scroll_bar_pagedown(FtkWidget* thiz)
366{
367	int value = 0;
368	DECL_PRIV0(thiz, priv);
369	return_val_if_fail(priv != NULL, RET_FAIL);
370	value = priv->value + priv->page_delta;
371	
372	return ftk_scroll_bar_set_value(thiz, value);
373}
374
375Ret ftk_scroll_bar_set_value(FtkWidget* thiz, int value)
376{
377	DECL_PRIV0(thiz, priv);
378	return_val_if_fail(priv != NULL, RET_FAIL);
379	value = value < 0 ? 0 : value;
380	value = (value + priv->page_delta) < priv->max_value ? value : priv->max_value - priv->page_delta;
381	
382	if(value != priv->value)
383	{
384		priv->value = value;
385		ftk_widget_invalidate(thiz);	
386		return FTK_CALL_LISTENER(priv->listener, priv->listener_ctx, thiz);
387	}
388
389	return RET_OK;
390}
391