notebook.c 8.9 KB
Newer Older
B
bernard.xiong@gmail.com 已提交
1
#include <rtgui/dc.h>
2
#include <rtgui/rtgui.h>
B
bernard.xiong@gmail.com 已提交
3
#include <rtgui/rtgui_system.h>
4 5
#include <rtgui/widgets/notebook.h>

6 7
#define RTGUI_NOTEBOOK_TAB_WIDTH     80

8 9 10
static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);
static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect);

B
bernard.xiong@gmail.com 已提交
11 12
static void _rtgui_notebook_constructor(rtgui_notebook_t *notebook)
{
13
	notebook->flag = 0;
B
bernard.xiong@gmail.com 已提交
14 15 16
	notebook->childs = RT_NULL;
	notebook->count = 0;
	notebook->current = RTGUI_NOT_FOUND;
17 18 19

	RTGUI_WIDGET(notebook)->gc.textalign = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
	rtgui_widget_set_event_handler(RTGUI_WIDGET(notebook), rtgui_notebook_event_handler);
B
bernard.xiong@gmail.com 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
}

static void _rtgui_notebook_destructor(rtgui_notebook_t *notebook)
{
	int index;

	if (notebook->childs != RT_NULL)
	{
		for (index = 0; index < notebook->count; index ++)
		{
			rtgui_widget_destroy(notebook->childs[index].widget);
			rt_free(notebook->childs[index].title);
		}

		rtgui_free(notebook->childs);
	}
}

static void _rtgui_notebook_ondraw(rtgui_notebook_t *notebook)
{
	struct rtgui_dc* dc;
	rtgui_rect_t rect;
	int index;

	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
	if (dc == RT_NULL) return;

	rtgui_widget_get_rect(RTGUI_WIDGET(notebook), &rect);

	if (notebook->count == 0)
	{
		rtgui_dc_fill_rect(dc, &rect);
	}
	else
	{
55 56 57 58 59
		if (notebook->current == RTGUI_NOT_FOUND)
			notebook->current = 0;

		_rtgui_notebook_get_bar_rect(notebook, &rect);
		rtgui_dc_fill_rect(dc, &rect);
60
		rect.x2 = rect.x1 + RTGUI_NOTEBOOK_TAB_WIDTH;
B
bernard.xiong@gmail.com 已提交
61 62 63
		/* draw tab bar */
		for (index = 0; index < notebook->count; index ++)
		{
64 65 66 67
			if (notebook->current == index)
				rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
			else
				rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
68

69
			rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
70 71
			rect.x1 += RTGUI_NOTEBOOK_TAB_WIDTH;
			rect.x2 += RTGUI_NOTEBOOK_TAB_WIDTH;
B
bernard.xiong@gmail.com 已提交
72 73 74 75 76 77 78 79
		}

		/* draw current tab */
		rtgui_widget_update(notebook->childs[notebook->current].widget);
	}
	rtgui_dc_end_drawing(dc);
}

80 81 82 83 84 85 86 87 88 89 90 91
static void _rtgui_notebook_onmouse(rtgui_notebook_t *notebook, struct rtgui_event_mouse* emouse)
{
	rtgui_rect_t rect;
	
	/* handle notebook bar */
	_rtgui_notebook_get_bar_rect(notebook, &rect);
	rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
	if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK)
	{
		int index;
		struct rtgui_dc* dc;

92
		index = (emouse->x - rect.x1) / RTGUI_NOTEBOOK_TAB_WIDTH;
93 94 95 96 97 98 99 100 101 102
		if (index < notebook->count)
		{
			/* update tab bar */
			dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(notebook));
			if (dc == RT_NULL) return;

			rtgui_notebook_set_current_by_index(notebook, index);

			_rtgui_notebook_get_bar_rect(notebook, &rect);
			rtgui_dc_fill_rect(dc, &rect);
103
			rect.x2 = rect.x1 + RTGUI_NOTEBOOK_TAB_WIDTH;
104 105 106 107 108 109 110 111
			/* draw tab bar */
			for (index = 0; index < notebook->count; index ++)
			{
				if (notebook->current == index)
					rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
				else
					rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
				rtgui_dc_draw_text(dc, notebook->childs[index].title, &rect);
112 113
				rect.x1 += RTGUI_NOTEBOOK_TAB_WIDTH;
				rect.x2 += RTGUI_NOTEBOOK_TAB_WIDTH;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
			}

			rtgui_dc_end_drawing(dc);

			return;
		}
	}

	/* handle on page */
	if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
		notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget, 
			&(emouse->parent));
}

static void _rtgui_notebook_get_page_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
{
	RT_ASSERT(notebook != RT_NULL);
	RT_ASSERT(rect != RT_NULL);

	rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);

	if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
	else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
		rect->y1 = rect->y1 + 25;
	else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
		rect->y2 = rect->y2 - 25;
}

static void _rtgui_notebook_get_bar_rect(rtgui_notebook_t *notebook, struct rtgui_rect* rect)
{
	RT_ASSERT(notebook != RT_NULL);
	RT_ASSERT(rect != RT_NULL);

	rtgui_widget_get_rect(RTGUI_WIDGET(notebook), rect);
	if (notebook->flag == RTGUI_NOTEBOOK_NOTAB) return;
	else if (notebook->flag == RTGUI_NOTEBOOK_TOP)
		rect->y2 = rect->y1 + 25;
	else if (notebook->flag == RTGUI_NOTEBOOK_BOTTOM)
		rect->y1 = rect->y2 - 25;
}

155 156 157 158 159
DEFINE_CLASS_TYPE(notebook, "notebook", 
	RTGUI_CONTAINER_TYPE,
	_rtgui_notebook_constructor,
	_rtgui_notebook_destructor,
	sizeof(struct rtgui_notebook));
160

161 162 163
rtgui_notebook_tab_t *tabs;
struct rtgui_notebook *_notebook;
rtgui_notebook_t* rtgui_notebook_create(const rtgui_rect_t* rect, rt_uint8_t style)
164
{
B
bernard.xiong@gmail.com 已提交
165 166 167 168 169
    struct rtgui_notebook* notebook;

    notebook = (struct rtgui_notebook*) rtgui_widget_create (RTGUI_NOTEBOOK_TYPE);
    if (notebook != RT_NULL)
    {
170
		notebook->flag = style;
B
bernard.xiong@gmail.com 已提交
171 172 173
		rtgui_widget_set_rect(RTGUI_WIDGET(notebook), rect);
    }

174
	_notebook = notebook;
B
bernard.xiong@gmail.com 已提交
175
    return notebook;
176 177 178 179
}

void rtgui_notebook_destroy(rtgui_notebook_t* notebook)
{
B
bernard.xiong@gmail.com 已提交
180
	rtgui_widget_destroy(RTGUI_WIDGET(notebook));
181 182 183 184
}

void rtgui_notebook_add(rtgui_notebook_t* notebook, const char* label, rtgui_widget_t* child)
{
185
	rtgui_rect_t rect;
B
bernard.xiong@gmail.com 已提交
186 187 188 189 190 191 192 193
	RT_ASSERT(notebook != RT_NULL);

	notebook->count += 1;
	notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, 
		sizeof(struct rtgui_notebook_tab) * notebook->count);

	notebook->childs[notebook->count - 1].title = rt_strdup(label);
	notebook->childs[notebook->count - 1].widget = child;
194 195 196 197 198 199 200 201 202

	tabs = notebook->childs;

	/* set parent */
	rtgui_widget_set_parent(child, RTGUI_WIDGET(notebook));

	_rtgui_notebook_get_page_rect(notebook, &rect);
	rtgui_widget_rect_to_device(RTGUI_WIDGET(notebook), &rect);
	rtgui_widget_set_rect(child, &rect);
B
bernard.xiong@gmail.com 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
}

void rtgui_notebook_remove(rtgui_notebook_t* notebook, rt_uint16_t index)
{
	struct rtgui_notebook_tab tab;
	RT_ASSERT(notebook != RT_NULL);

	if (index < notebook->count)
	{
		if (notebook->count == 1)
		{
			tab = notebook->childs[0];
			rtgui_free(notebook->childs);
			notebook->childs = RT_NULL;
			notebook->count = 0;
		}
		else
		{
			tab = notebook->childs[index];
			for (;index < notebook->count - 1; index ++)
			{
				notebook->childs[index] = notebook->childs[index + 1];
			}

			notebook->count -= 1;
			notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, 
				sizeof(struct rtgui_notebook_tab) * notebook->count);
		}

		rtgui_widget_destroy(tab.widget);
		rtgui_free(tab.title);

		if (notebook->current == index)
		{
			/* update tab */
			if (notebook->current > notebook->count - 1)
				notebook->current = notebook->count - 1;
			rtgui_widget_update(RTGUI_WIDGET(notebook));
		}
	}
243 244
}

B
bernard.xiong@gmail.com 已提交
245
int rtgui_notebook_get_count(rtgui_notebook_t* notebook)
246 247 248 249 250 251 252 253
{
	return notebook->count;
}

rtgui_widget_t* rtgui_notebook_get_current(rtgui_notebook_t* notebook)
{
	RT_ASSERT(notebook != RT_NULL);
	if (notebook->current != RTGUI_NOT_FOUND)
B
bernard.xiong@gmail.com 已提交
254
		return notebook->childs[notebook->current].widget;
255 256 257 258 259 260 261 262 263 264 265

	return RT_NULL;
}
void rtgui_notebook_set_current(rtgui_notebook_t* notebook, rtgui_widget_t* widget)
{
	rt_int16_t index;

	RT_ASSERT(notebook != RT_NULL);

	for (index = 0; index < notebook->count; index ++)
	{
B
bernard.xiong@gmail.com 已提交
266
		if (widget == notebook->childs[index].widget)
267 268 269 270 271 272 273 274 275 276 277 278 279 280
		{
			rtgui_notebook_set_current_by_index(notebook, index);
			return;
		}
	}
}

void rtgui_notebook_set_current_by_index(rtgui_notebook_t* notebook, rt_uint16_t index)
{
	RT_ASSERT(notebook != RT_NULL);

	if ((index < notebook->count) && (notebook->current != index))
	{
		if (notebook->current != RTGUI_NOT_FOUND)
B
bernard.xiong@gmail.com 已提交
281
			rtgui_widget_hide(notebook->childs[notebook->current].widget);
282

283
		notebook->current = index;
B
bernard.xiong@gmail.com 已提交
284 285
		rtgui_widget_show(notebook->childs[notebook->current].widget);
		rtgui_widget_update(notebook->childs[notebook->current].widget);
286 287 288 289 290 291 292
	}
}

rtgui_widget_t* rtgui_notebook_get_index(rtgui_notebook_t* notebook, rt_uint16_t index)
{
	RT_ASSERT(notebook != RT_NULL);
	if (index < notebook->count)
B
bernard.xiong@gmail.com 已提交
293
		return notebook->childs[index].widget;
294 295 296 297 298 299

	return RT_NULL;
}

rt_bool_t rtgui_notebook_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
300 301 302
	struct rtgui_notebook* notebook;

	notebook = RTGUI_NOTEBOOK(widget);
B
bernard.xiong@gmail.com 已提交
303 304
	if (event->type == RTGUI_EVENT_PAINT)
	{
305 306 307 308 309 310 311 312 313 314 315 316 317 318
		_rtgui_notebook_ondraw(notebook);
	}
	else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
	{
		_rtgui_notebook_onmouse(notebook, (struct rtgui_event_mouse*)event);
	}
	else if (event->type == RTGUI_EVENT_KBD)
	{
		if (notebook->current != RTGUI_NOT_FOUND)
		{
			if (notebook->childs[notebook->current].widget->event_handler != RT_NULL)
				return notebook->childs[notebook->current].widget->event_handler(notebook->childs[notebook->current].widget,
					event);
		}
B
bernard.xiong@gmail.com 已提交
319 320 321 322 323 324 325
	}
	else
	{
		/* use parent event handler */
		return rtgui_widget_event_handler(widget, event);
	}

326 327
	return RT_FALSE;
}