PageRenderTime 315ms CodeModel.GetById 202ms app.highlight 15ms RepoModel.GetById 95ms app.codeStats 0ms

/src/ftk_list_model_default.c

http://ftk.googlecode.com/
C | 219 lines | 147 code | 43 blank | 29 comment | 45 complexity | 13072a427bc0a177ac0d05a704113e30 MD5 | raw file
  1/*
  2 * File: ftk_list_model_default.c
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   default list model
  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-28 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31
 32#include "ftk_list_model_default.h"
 33
 34typedef struct _ListModelDefaultPrivInfo
 35{
 36	size_t nr;
 37	size_t alloc_nr;
 38	FtkListItemInfo* items;
 39}PrivInfo;
 40
 41static Ret ftk_list_item_reset(FtkListItemInfo* info);
 42
 43static int  ftk_list_model_default_get_total(FtkListModel* thiz)
 44{
 45	DECL_PRIV(thiz, priv);
 46	return_val_if_fail(priv != NULL, 0);
 47
 48	return priv->nr;
 49}
 50
 51static Ret  ftk_list_model_default_get_data(FtkListModel* thiz, size_t index, void** ret)
 52{
 53	DECL_PRIV(thiz, priv);
 54	return_val_if_fail(priv != NULL && index < priv->nr && ret != NULL, RET_FAIL);
 55	
 56	*ret = priv->items+index;
 57	
 58	return RET_OK;
 59}
 60
 61static void ftk_list_model_default_destroy(FtkListModel* thiz)
 62{
 63	DECL_PRIV(thiz, priv);
 64	
 65	if(priv != NULL)
 66	{
 67		size_t i = 0;
 68
 69		for(i = 0; i < priv->nr; i++)
 70		{
 71			ftk_list_item_reset(priv->items+i);
 72		}
 73		FTK_FREE(priv->items);
 74		FTK_ZFREE(thiz, sizeof(FtkListModel) + sizeof(PrivInfo));
 75	}
 76
 77	return;
 78}
 79
 80static Ret ftk_list_model_default_extend(FtkListModel* thiz, size_t delta)
 81{
 82	int alloc_nr = 0;
 83	DECL_PRIV(thiz, priv);
 84	FtkListItemInfo* items = NULL;
 85
 86	if(priv->items != NULL && (priv->nr + delta) < priv->alloc_nr)
 87	{
 88		return RET_OK;
 89	}
 90
 91	alloc_nr = (priv->alloc_nr + delta) + FTK_HALF(priv->alloc_nr + delta) + 2; 
 92	items = (FtkListItemInfo*)FTK_REALLOC(priv->items, sizeof(FtkListItemInfo) * alloc_nr);
 93	if(items != NULL)
 94	{
 95		priv->items = items;
 96		priv->alloc_nr = alloc_nr;
 97	}
 98
 99	return (priv->nr + delta) < priv->alloc_nr ? RET_OK : RET_FAIL;
100}
101
102static Ret ftk_list_item_reset(FtkListItemInfo* info)
103{
104	return_val_if_fail(info != NULL, RET_FAIL);
105
106	if(info->text != NULL)
107	{
108		FTK_FREE(info->text);
109	}
110
111	if(info->left_icon != NULL)
112	{
113		ftk_bitmap_unref(info->left_icon);
114	}
115
116	if(info->right_icon != NULL)
117	{
118		ftk_bitmap_unref(info->right_icon);
119	}
120
121	memset(info, 0x00, sizeof(FtkListItemInfo));
122
123	return RET_OK;
124}
125
126static Ret ftk_list_item_copy(FtkListModel* thiz, FtkListItemInfo* dst, FtkListItemInfo* src)
127{
128	return_val_if_fail(dst != NULL && src != NULL && thiz != NULL, RET_FAIL);
129	
130	memcpy(dst, src, sizeof(FtkListItemInfo));
131
132	if(src->text != NULL)
133	{
134		dst->text = ftk_strdup(src->text);
135	}
136
137	if(dst->left_icon != NULL)
138	{
139		ftk_bitmap_ref(dst->left_icon);
140	}
141
142	if(dst->right_icon != NULL)
143	{
144		ftk_bitmap_ref(dst->right_icon);
145	}
146
147	return RET_OK;
148}
149
150Ret ftk_list_model_default_add(FtkListModel* thiz, void* item)
151{
152	DECL_PRIV(thiz, priv);
153	FtkListItemInfo* info = (FtkListItemInfo*)item;
154	return_val_if_fail(thiz != NULL && info != NULL, RET_FAIL);
155	return_val_if_fail(ftk_list_model_default_extend(thiz, 1) == RET_OK, RET_FAIL);
156
157	if(ftk_list_item_copy(thiz, priv->items+priv->nr, info) == RET_OK)
158	{
159		priv->nr++;
160	}
161
162	return RET_OK;
163}
164
165static Ret ftk_list_model_default_reset(FtkListModel* thiz)
166{
167	size_t i = 0;
168	DECL_PRIV(thiz, priv);
169	return_val_if_fail(thiz != NULL, RET_FAIL);
170
171	for(i = 0; i < priv->nr; i++)
172	{
173		ftk_list_item_reset(priv->items+i);
174	}
175	priv->nr = 0;
176
177	return RET_OK;
178}
179
180Ret ftk_list_model_default_remove(FtkListModel* thiz, size_t index)
181{
182	DECL_PRIV(thiz, priv);
183	return_val_if_fail(thiz != NULL && index < priv->nr, RET_FAIL);
184
185	ftk_list_item_reset(priv->items+index);
186	if(index < (priv->nr - 1))
187	{
188		size_t i = index; 
189		for(; i < priv->nr; i++)
190		{
191			memcpy(priv->items+i, priv->items+i+1, sizeof(FtkListItemInfo));
192		}
193	}
194
195	priv->nr--;
196
197	return RET_OK;
198}
199
200FtkListModel* ftk_list_model_default_create(size_t init_nr)
201{
202	FtkListModel* thiz = FTK_NEW_PRIV(FtkListModel);
203	if(thiz != NULL)
204	{
205		DECL_PRIV(thiz, priv);
206		thiz->get_total = ftk_list_model_default_get_total;
207		thiz->get_data  = ftk_list_model_default_get_data;
208		thiz->add       = ftk_list_model_default_add;
209		thiz->reset     = ftk_list_model_default_reset;
210		thiz->remove    = ftk_list_model_default_remove;
211		thiz->destroy   = ftk_list_model_default_destroy;
212
213		thiz->ref = 1;
214		priv->alloc_nr = init_nr;
215	}
216
217	return thiz;
218}
219