Example #1
0
static void rtgui_textbox_onmouse(rtgui_textbox_t *box, struct rtgui_event_mouse *event)
{
	rt_size_t length;
	rt_uint16_t posbak = box->position;

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

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

	if (event->button & RTGUI_MOUSE_BUTTON_LEFT && event->button & RTGUI_MOUSE_BUTTON_DOWN)
	{
		rt_int32_t x;
		/* single line text */
		/* set caret position */
		x = event->x - RTGUI_WIDGET(box)->extent.x1;
		if (x < 0)
		{
			box->position = 0;
		}
		else if (x > (length - box->first_pos) * box->font_width)
		{
			box->position = length - box->first_pos;
		}
		else
		{
			box->position = x / box->font_width;
		}

		if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
		{
			if (box->caret_timer != RT_NULL)
				rtgui_timer_stop(box->caret_timer);

			box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
			rtgui_textbox_draw_caret(box, posbak);

			if (box->caret_timer != RT_NULL)
				rtgui_timer_start(box->caret_timer);
		}

		rtgui_textbox_init_caret(box, box->position);
		box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
		rtgui_textbox_draw_caret(box, box->position);
	}
}
Example #2
0
static rt_bool_t rtgui_textbox_onkey(struct rtgui_object *widget, rtgui_event_t *event)
{
	rtgui_textbox_t *box = RTGUI_TEXTBOX(widget);
	struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event;
	rt_size_t length;
	rt_uint16_t posbak = box->position;

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

	/* handle the key at down time and nothing to do with up */
	if (RTGUI_KBD_IS_UP(ekbd))
		return RT_TRUE;

	if (box->dis_length == 0)
	{
		rtgui_rect_t rect;

		rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);

		if (box->font_width == 0)
			return RT_FALSE;

		box->dis_length = (rtgui_rect_width(rect) - 5) / box->font_width;
	}

	length = rt_strlen(box->text);
	if (ekbd->key == RTGUIK_DELETE)
	{
		/* delete latter character */
		if (box->first_pos + box->position == length - 1)
		{
			box->text[box->first_pos + box->position] = '\0';
		}
		else
		{
			char *c;

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

				/* remove character */
				for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
					*c = c[1];
				*c = '\0';
			}
			box->position --;
		}
	}
	else if (ekbd->key == RTGUIK_LEFT)
	{
		/* move to prev */
		if (box->position > 0)
		{
			box->position --;
		}
		else
		{
			if(box->first_pos > 0)
				box->first_pos -= 1;//DEBUG
		}
	}
	else if (ekbd->key == RTGUIK_RIGHT)
	{
		/* move to next */
		if (box->first_pos + box->position < length)
		{
			if(box->position < box->dis_length)
				box->position ++;
			else 
				box->first_pos += 1;//DEBUG
		}
	}
	else if (ekbd->key == RTGUIK_HOME)
	{
		/* move cursor to start */
		box->position = 0;
		box->first_pos = 0;
	}
	else if (ekbd->key == RTGUIK_END)
	{
		/* move cursor to end */
		if(length > box->dis_length)
		{
			box->position = box->dis_length;
			box->first_pos = length - box->dis_length;
		}
		else
		{
			box->position = length;
			box->first_pos = 0;
		}
	}
	else if (ekbd->key == RTGUIK_RETURN)
	{
		if (box->on_enter != RT_NULL)
		{
			box->on_enter(box, event);
		}
	}
	else if (ekbd->key == RTGUIK_NUMLOCK)
	{
		/* change numlock state */
		/*
		extern void update_number_lock(void);
		update_number_lock();
		*/
	}
	else
	{
		if (isprint(ekbd->key))
		{
			/* it's may print character */
			/* no buffer on this line */
			if (box->flag & RTGUI_TEXTBOX_DIGIT)
			{
				/* only input digit */
				if (!isdigit(ekbd->key))
				{
					/* exception: '.' and '-' */
					if (ekbd->key != '.' && ekbd->key != '-')return RT_FALSE;
					if (ekbd->key == '.' && strchr(box->text, '.'))return RT_FALSE;

					if (ekbd->key == '-')
					{
						if (length + 1 > box->line_length) return RT_FALSE;

						if (strchr(box->text, '-'))
						{
							char *c;
							for (c = &box->text[0]; c != &box->text[length]; c++)
								*c = *(c + 1);
							box->text[length] = '\0';
							box->position --;
							goto _exit;
						}
						else
						{
							char *c;
							for (c = &box->text[length]; c != &box->text[0]; c--)
								*c = *(c - 1);
							box->text[0] = '-';
							box->text[length + 1] = '\0';
							box->position ++;
							goto _exit;
						}
					}
				}
			}
			if (length + 1 > box->line_length) return RT_FALSE;

			if (box->first_pos + box->position <= length - 1)
			{
				char *c;

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

			box->text[box->first_pos + box->position] = ekbd->key;
			if(box->position < box->dis_length)
				box->position ++;
			else
				box->first_pos ++;
		}
	}

_exit:
	if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
	{
		if (box->caret_timer != RT_NULL)
			rtgui_timer_stop(box->caret_timer);

		box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
		rtgui_textbox_draw_caret(box, posbak);/* refresh it */
		if (box->caret_timer != RT_NULL)
			rtgui_timer_start(box->caret_timer);
	}

	/* re-draw text box */
	rtgui_textbox_ondraw(box);

	rtgui_textbox_init_caret(box, box->position);
	box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
	rtgui_textbox_draw_caret(box, box->position);

	return RT_TRUE;
}