textbox.c 8.1 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * File      : textbox.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 */
#include <rtgui/dc.h>
#include <rtgui/widgets/textbox.h>
#include <rtgui/rtgui_theme.h>

#ifndef _WIN32
#include <ctype.h>
#endif

#define RTGUI_TEXTBOX_LINE_MAX		64
#define RTGUI_TEXTBOX_MARGIN		3

static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event);
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event);
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event);

29 30 31 32 33 34 35 36 37
static void _rtgui_textbox_caret_timeout(void *parameter)
{
	rtgui_textbox_t* box;

	box = (rtgui_textbox_t*)parameter;
	
	return ;
}

B
bernard.xiong 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
static void _rtgui_textbox_constructor(rtgui_textbox_t *box)
{
	rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
	rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);

	RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
	rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_textbox_event_handler);
	rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus);
	rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus);

	/* set default text align */
	RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_CENTER_VERTICAL;

	/* set proper of control */
52 53
	box->caret_timer = rtgui_timer_create(RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC, 
		_rtgui_textbox_caret_timeout, box);
B
bernard.xiong 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

	box->line = box->line_begin = box->position = 0;
	box->type = RTGUI_TEXTBOX_SINGLE;

	/* allocate default line buffer */
	box->text = RT_NULL;

	rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
	box->font_width = rtgui_rect_width(rect);
}

static void _rtgui_textbox_deconstructor(rtgui_textbox_t *box)
{
	if (box->text != RT_NULL)
	{
		rt_free(box->text);
		box->text = RT_NULL;
	}

73 74
	rtgui_timer_destory(box->caret_timer);
	box->caret_timer = RT_NULL;
B
bernard.xiong 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

rtgui_type_t *rtgui_textbox_type_get(void)
{
	static rtgui_type_t *textbox_type = RT_NULL;

	if (!textbox_type)
	{
		textbox_type = rtgui_type_create("textbox", RTGUI_WIDGET_TYPE,
			sizeof(rtgui_textbox_t),
			RTGUI_CONSTRUCTOR(_rtgui_textbox_constructor),
			RTGUI_DESTRUCTOR(_rtgui_textbox_deconstructor));
	}

	return textbox_type;
}

static void rtgui_textbox_onmouse(struct rtgui_textbox* box, struct rtgui_event_mouse* event)
{
	rt_size_t length;

	RT_ASSERT(box != RT_NULL);
	RT_ASSERT(event != RT_NULL);

	length = rt_strlen((const char*)box->text);

	if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
		event->button & RTGUI_MOUSE_BUTTON_DOWN)
	{
		rt_int32_t x;

		/* set caret position */
		x = event->x - RTGUI_WIDGET(box)->extent.x1;
		if (x < 0)
		{
			box->position = 0;
		}
		else if (x > length * box->font_width)
		{
			box->position = length;
		}
		else
		{
			box->position = x / box->font_width;
		}

		/* set widget focus */
		rtgui_widget_focus(RTGUI_WIDGET(box));
	}
}

static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event)
{
	rt_size_t length;

	RT_ASSERT(box != RT_NULL);
	RT_ASSERT(event != RT_NULL);

	if (event->type != RTGUI_KEYDOWN)
		return ;

	length = rt_strlen((const char*)box->text);
	if (event->key == RTGUIK_DELETE)
	{
		if (box->position == length - 1)
		{
			box->text[box->position] = '\0';
		}
		else
		{
			unsigned char *c;

			/* remove character */
			for (c = &box->text[box->position]; c[1] != '\0'; c++)
				*c = c[1];
			*c = '\0';
		}
	}
	else if (event->key == RTGUIK_BACKSPACE)
	{
		if (box->position == length - 1)
		{
			box->text[box->position] = '\0';
			box->position --;
		}
		else if (box->position != 0)
		{
			/* remove current character */
			if (box->position != 0)
			{
				unsigned char *c;

				/* remove character */
				for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
					*c = c[1];
				*c = '\0';
			}
			box->position --;
		}
	}
	else if (event->key == RTGUIK_LEFT)
	{
		if (box->position > 0) box->position --;
	}
	else if (event->key == RTGUIK_RIGHT)
	{
		if (box->position < length) box->position ++;
	}
	else if (event->key == RTGUIK_HOME)
	{
		box->position = 0;
	}
	else if (event->key == RTGUIK_END)
	{
		box->position = length;
	}
	else if (event->key == RTGUIK_RETURN)
	{
		if (box->on_enter != RT_NULL)
		{
			box->on_enter(RTGUI_WIDGET(box), RT_NULL);
		}
	}
	else
	{
		if (isprint(event->key) || isdigit(event->key))
		{
			/* no buffer on this line */
			if (length + 1 > box->line_length) return;

			if (box->position < length - 1)
			{
				unsigned char* c;

				for (c = &box->text[length]; c != &box->text[box->position]; c--)
					*c = *(c-1);
				box->text[length + 1] = '\0';
			}

			box->text[box->position] = event->key;
			box->position ++;
		}
	}

	/* re-draw text box */
	rtgui_theme_draw_textbox(box);
}

static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event)
{
	struct rtgui_textbox* box = (struct rtgui_textbox*)widget;

227 228
	/* start caret timer */
	rtgui_timer_start(box->caret_timer);
B
bernard.xiong 已提交
229 230 231 232 233 234 235 236

	return RT_TRUE;
}

static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event)
{
	struct rtgui_textbox* box = (struct rtgui_textbox*)widget;

237 238
	/* stop caret timer */
	rtgui_timer_stop(box->caret_timer);
B
bernard.xiong 已提交
239 240 241 242 243 244 245 246 247 248 249 250
	/* hide caret */

	return RT_TRUE;
}

rt_bool_t rtgui_textbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
	struct rtgui_textbox* box = (struct rtgui_textbox*)widget;

	switch (event->type)
	{
	case RTGUI_EVENT_PAINT:
251
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
252
		if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
253 254 255
		else 
#endif
			rtgui_theme_draw_textbox(box);
B
bernard.xiong 已提交
256 257 258
		break;

	case RTGUI_EVENT_MOUSE_BUTTON:
259
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
260
		if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
261 262 263
		else 
#endif
			rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event);
B
bernard.xiong 已提交
264 265 266
		return RT_TRUE;

	case RTGUI_EVENT_KBD:
267
#ifndef RTGUI_USING_SMALL_SIZE
B
bernard.xiong 已提交
268
		if (widget->on_key != RT_NULL) widget->on_key(widget, event);
269 270 271
		else 
#endif
			rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event);
B
bernard.xiong 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
		return RT_TRUE;
	}

	return RT_FALSE;
}

struct rtgui_textbox* rtgui_textbox_create(const char* text)
{
    struct rtgui_textbox* box;

    box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE);
    if (box != RT_NULL)
    {
		rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};

		/* allocate default line buffer */
		rtgui_textbox_set_value(box, text);

		rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
		box->font_width = rtgui_rect_width(rect);
	}

	return box;
}

void rtgui_textbox_destroy(struct rtgui_textbox* box)
{
	rtgui_widget_destroy(RTGUI_WIDGET(box));
}

void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text)
{
	if (box->text != RT_NULL)
	{
		if (box->line_length > rt_strlen(text) + 1)
		{
			rt_memcpy(box->text, text, rt_strlen(text) + 1);
			return;
		}
		else
		{
			/* free the old text */
			rtgui_free(box->text);
			box->text = RT_NULL;
		}
	}

	box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ?
		RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1;

	/* allocate line buffer */
	box->text = rtgui_malloc(box->line_length);
	rt_memset(box->text, 0, box->line_length);

	/* copy text */
	rt_memcpy(box->text, text, rt_strlen(text) + 1);

	/* set current position */
	box->position = 0;
}

const char* rtgui_textbox_get_value(struct rtgui_textbox* box)
{
	return (const char*)box->text;
}

void rtgui_widget_set_line_length(struct rtgui_textbox* box, rt_size_t length)
{
	rt_uint8_t* new_line;

	RT_ASSERT(box != RT_NULL);

	/* invalid length */
	if (length <= 0) return;

	new_line = rtgui_malloc(length);
	if (length < box->line_length)
	{
		rt_memcpy(new_line, box->text, length - 1);
		new_line[length] = '\0';
	}
	else
	{
		rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text));
	}

	/* set line length */
	box->line_length = length;
}
361