2009-11-24 07:34:58 +08:00
|
|
|
/*
|
2012-11-26 10:42:52 +08:00
|
|
|
* 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
|
|
|
|
* 2011-01-224 Bernard fix backspace issue.
|
|
|
|
* 2012-06-09 asml refactor
|
|
|
|
* 2012-06-17 Grissiom misc cleanup and merge
|
|
|
|
*/
|
2012-07-20 20:37:56 +08:00
|
|
|
#include <string.h>
|
2010-01-04 14:52:32 +08:00
|
|
|
#include <rtgui/widgets/textbox.h>
|
2012-07-20 20:37:56 +08:00
|
|
|
#include <rtgui/widgets/combobox.h>
|
|
|
|
#include <rtgui/rtgui_system.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2010-01-08 08:02:24 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
static void rtgui_textbox_draw_caret(rtgui_textbox_t *box, rt_uint16_t position);
|
2012-11-26 10:42:52 +08:00
|
|
|
static rt_bool_t rtgui_textbox_onkey(struct rtgui_object *widget, rtgui_event_t *event);
|
|
|
|
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_object *widget, rtgui_event_t *event);
|
|
|
|
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object *widget, rtgui_event_t *event);
|
2010-01-04 08:05:01 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
static void _rtgui_textbox_constructor(rtgui_textbox_t *box)
|
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_rect_t rect;
|
|
|
|
|
|
|
|
RTGUI_WIDGET_FLAG(RTGUI_WIDGET(box)) |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_textbox_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus);
|
|
|
|
rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus);
|
2012-07-20 20:37:56 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_widget_set_onkey(RTGUI_WIDGET(box), rtgui_textbox_onkey);
|
2012-07-20 20:37:56 +08:00
|
|
|
#endif
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_FOREGROUND(box) = black;
|
|
|
|
RTGUI_WIDGET_BACKGROUND(box) = white;
|
2009-11-24 07:34:58 +08:00
|
|
|
/* set default text align */
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_CENTER_VERTICAL;
|
2009-11-24 07:34:58 +08:00
|
|
|
/* set proper of control */
|
2012-07-20 20:37:56 +08:00
|
|
|
box->caret_timer = RT_NULL;
|
|
|
|
box->caret = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
box->line = box->line_begin = box->position = 0;
|
2010-01-08 08:02:24 +08:00
|
|
|
box->flag = RTGUI_TEXTBOX_SINGLE;
|
2009-11-24 07:34:58 +08:00
|
|
|
/* allocate default line buffer */
|
|
|
|
box->text = RT_NULL;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_set_mask_char(box, '*');
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-08-13 14:30:05 +08:00
|
|
|
rtgui_font_get_metrics(RTGUI_WIDGET_FONT(box), "H", &rect);
|
2009-11-24 07:34:58 +08:00
|
|
|
box->font_width = rtgui_rect_width(rect);
|
2012-07-20 20:37:56 +08:00
|
|
|
box->on_enter = RT_NULL;
|
|
|
|
box->dis_length = 0;
|
2012-11-26 10:42:52 +08:00
|
|
|
box->first_pos = 0;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
static void _rtgui_textbox_deconstructor(rtgui_textbox_t *textbox)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (textbox->text != RT_NULL)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_free(textbox->text);
|
|
|
|
textbox->text = RT_NULL;
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
if (textbox->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
rtgui_timer_destory(textbox->caret_timer);
|
|
|
|
textbox->caret_timer = RT_NULL;
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
if (textbox->caret != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
rtgui_free(textbox->caret);
|
|
|
|
textbox->caret = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
DEFINE_CLASS_TYPE(textbox, "textbox",
|
2012-11-26 10:42:52 +08:00
|
|
|
RTGUI_WIDGET_TYPE,
|
|
|
|
_rtgui_textbox_constructor,
|
|
|
|
_rtgui_textbox_deconstructor,
|
|
|
|
sizeof(struct rtgui_textbox));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
static void rtgui_textbox_get_caret_rect(rtgui_textbox_t *box, rtgui_rect_t *rect, rt_uint16_t position)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
int font_h, box_h;
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_rect_t item_rect;
|
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(box), rect);
|
|
|
|
|
2012-08-13 14:30:05 +08:00
|
|
|
rtgui_font_get_metrics(RTGUI_WIDGET_FONT(box), "H", &item_rect);
|
2012-07-20 20:37:56 +08:00
|
|
|
font_h = rtgui_rect_height(item_rect);
|
|
|
|
box_h = rtgui_rect_height(*rect);
|
|
|
|
|
|
|
|
rect->x1 += position * box->font_width + 2;
|
|
|
|
rect->x2 = rect->x1 + 2;
|
|
|
|
rect->y1 += (box_h - font_h) / 2;
|
|
|
|
rect->y2 = rect->y1 + font_h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rtgui_textbox_init_caret(rtgui_textbox_t *box, rt_uint16_t position)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
rtgui_color_t color;
|
|
|
|
rtgui_rect_t rect;
|
|
|
|
int ofs = 0;
|
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
|
2012-08-13 14:30:05 +08:00
|
|
|
if (!RTGUI_WIDGET_IS_FOCUSED(box))
|
2012-07-20 20:37:56 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
rtgui_textbox_get_caret_rect(box, &box->caret_rect, position);
|
|
|
|
rect = box->caret_rect;
|
|
|
|
rtgui_widget_rect_to_device(RTGUI_WIDGET(box), &rect);
|
|
|
|
|
|
|
|
if (box->caret == RT_NULL)
|
2012-11-26 10:42:52 +08:00
|
|
|
box->caret = rtgui_malloc(rtgui_rect_width(rect) * rtgui_rect_height(rect) * sizeof(rtgui_color_t));
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
for (x = rect.x1; x < rect.x2; x++)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
for (y = rect.y1; y < rect.y2; y++)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
rtgui_graphic_driver_get_default()->ops->get_pixel(&color, x, y);
|
|
|
|
*(box->caret + ofs) = color;
|
|
|
|
ofs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw caret */
|
|
|
|
static void rtgui_textbox_draw_caret(rtgui_textbox_t *box, rt_uint16_t position)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
int x, y;
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_color_t color;
|
|
|
|
rtgui_rect_t rect;
|
|
|
|
int ofs = 0;
|
|
|
|
struct rtgui_dc *dc;
|
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
if (box->caret == RT_NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
|
|
|
|
if (dc == RT_NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rect = box->caret_rect;
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
for (x = rect.x1; x < rect.x2; x++)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
for (y = rect.y1; y < rect.y2; y++)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
color = *(box->caret + ofs);
|
|
|
|
ofs++;
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
color = ~color;
|
|
|
|
}
|
|
|
|
rtgui_dc_draw_color_point(dc, x, y, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
static void rtgui_textbox_timeout(rtgui_timer_t *timer, void *parameter)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box;
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
box = RTGUI_TEXTBOX(parameter);
|
|
|
|
/* set caret flag */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
|
2012-07-20 20:37:56 +08:00
|
|
|
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
|
|
|
|
else
|
|
|
|
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_draw_caret(box, box->position);
|
2012-07-20 20:37:56 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
static void rtgui_textbox_onmouse(rtgui_textbox_t *box, struct rtgui_event_mouse *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rt_size_t length;
|
2012-07-20 20:37:56 +08:00
|
|
|
rt_uint16_t posbak = box->position;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
RT_ASSERT(event != RT_NULL);
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
length = rt_strlen(box->text);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (event->button & RTGUI_MOUSE_BUTTON_LEFT && event->button & RTGUI_MOUSE_BUTTON_DOWN)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rt_int32_t x;
|
2012-07-20 20:37:56 +08:00
|
|
|
/* single line text */
|
2009-11-24 07:34:58 +08:00
|
|
|
/* set caret position */
|
|
|
|
x = event->x - RTGUI_WIDGET(box)->extent.x1;
|
2012-11-26 10:42:52 +08:00
|
|
|
if (x < 0)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
box->position = 0;
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (x > (length - box->first_pos) * box->font_width)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
box->position = length - box->first_pos;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
box->position = x / box->font_width;
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_stop(box->caret_timer);
|
2010-01-25 08:01:18 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
|
|
|
|
rtgui_textbox_draw_caret(box, posbak);
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_start(box->caret_timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_textbox_init_caret(box, box->position);
|
|
|
|
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_draw_caret(box, box->position);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
static rt_bool_t rtgui_textbox_onkey(struct rtgui_object *widget, rtgui_event_t *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box = RTGUI_TEXTBOX(widget);
|
|
|
|
struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event;
|
2009-11-24 07:34:58 +08:00
|
|
|
rt_size_t length;
|
2012-07-20 20:37:56 +08:00
|
|
|
rt_uint16_t posbak = box->position;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
2012-07-20 20:37:56 +08:00
|
|
|
RT_ASSERT(ekbd != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* handle the key at down time and nothing to do with up */
|
|
|
|
if (RTGUI_KBD_IS_UP(ekbd))
|
|
|
|
return RT_TRUE;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
if (box->dis_length == 0)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
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);
|
2012-11-26 10:42:52 +08:00
|
|
|
if (ekbd->key == RTGUIK_DELETE)
|
|
|
|
{
|
|
|
|
/* delete latter character */
|
|
|
|
if (box->first_pos + box->position == length - 1)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
box->text[box->first_pos + box->position] = '\0';
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-18 07:56:49 +08:00
|
|
|
char *c;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* remove character */
|
2012-11-26 10:42:52 +08:00
|
|
|
for (c = &box->text[box->first_pos + box->position]; c[1] != '\0'; c++)
|
2009-11-24 07:34:58 +08:00
|
|
|
*c = c[1];
|
|
|
|
*c = '\0';
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
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)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
box->text[box->first_pos + box->position - 1] = '\0';
|
2009-11-24 07:34:58 +08:00
|
|
|
box->position --;
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (box->position != 0)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
/* remove current character */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->position != 0)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
char *c;
|
2010-08-05 18:00:09 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* remove character */
|
2012-11-26 10:42:52 +08:00
|
|
|
for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
|
2012-07-20 20:37:56 +08:00
|
|
|
*c = c[1];
|
|
|
|
*c = '\0';
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
box->position --;
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (ekbd->key == RTGUIK_LEFT)
|
|
|
|
{
|
|
|
|
/* move to prev */
|
|
|
|
if (box->position > 0)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
box->position --;
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(box->first_pos > 0)
|
|
|
|
box->first_pos -= 1;//DEBUG
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (ekbd->key == RTGUIK_RIGHT)
|
|
|
|
{
|
|
|
|
/* move to next */
|
|
|
|
if (box->first_pos + box->position < length)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if(box->position < box->dis_length)
|
|
|
|
box->position ++;
|
|
|
|
else
|
|
|
|
box->first_pos += 1;//DEBUG
|
2012-07-20 20:37:56 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (ekbd->key == RTGUIK_HOME)
|
|
|
|
{
|
|
|
|
/* move cursor to start */
|
2009-11-24 07:34:58 +08:00
|
|
|
box->position = 0;
|
2012-11-26 10:42:52 +08:00
|
|
|
box->first_pos = 0;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (ekbd->key == RTGUIK_RETURN)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->on_enter != RT_NULL)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
box->on_enter(box, event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
else if (ekbd->key == RTGUIK_NUMLOCK)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
/* change numlock state */
|
|
|
|
/*
|
|
|
|
extern void update_number_lock(void);
|
|
|
|
update_number_lock();
|
|
|
|
*/
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
else
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (isprint(ekbd->key))
|
|
|
|
{
|
|
|
|
/* it's may print character */
|
2009-11-24 07:34:58 +08:00
|
|
|
/* no buffer on this line */
|
2012-11-26 10:42:52 +08:00
|
|
|
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 == '-')
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (length + 1 > box->line_length) return RT_FALSE;
|
2012-07-20 20:37:56 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (strchr(box->text, '-'))
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
char *c;
|
|
|
|
for (c = &box->text[0]; c != &box->text[length]; c++)
|
|
|
|
*c = *(c + 1);
|
2012-07-20 20:37:56 +08:00
|
|
|
box->text[length] = '\0';
|
|
|
|
box->position --;
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
char *c;
|
|
|
|
for (c = &box->text[length]; c != &box->text[0]; c--)
|
|
|
|
*c = *(c - 1);
|
2012-07-20 20:37:56 +08:00
|
|
|
box->text[0] = '-';
|
2012-11-26 10:42:52 +08:00
|
|
|
box->text[length + 1] = '\0';
|
2012-07-20 20:37:56 +08:00
|
|
|
box->position ++;
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-26 10:42:52 +08:00
|
|
|
if (length + 1 > box->line_length) return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->first_pos + box->position <= length - 1)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
char *c;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
for (c = &box->text[length]; c != &box->text[box->first_pos + box->position]; c--)
|
|
|
|
*c = *(c - 1);
|
|
|
|
box->text[length + 1] = '\0';
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
box->text[box->first_pos + box->position] = ekbd->key;
|
|
|
|
if(box->position < box->dis_length)
|
|
|
|
box->position ++;
|
|
|
|
else
|
|
|
|
box->first_pos ++;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
_exit:
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_stop(box->caret_timer);
|
2010-01-08 08:02:24 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
|
|
|
|
rtgui_textbox_draw_caret(box, posbak);/* refresh it */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_start(box->caret_timer);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* re-draw text box */
|
|
|
|
rtgui_textbox_ondraw(box);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_textbox_init_caret(box, box->position);
|
2010-01-25 08:01:18 +08:00
|
|
|
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_draw_caret(box, box->position);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_object *widget, rtgui_event_t *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box = RTGUI_TEXTBOX(widget);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* if there is already a timer, don't create another one. */
|
|
|
|
if (box->caret_timer == RT_NULL)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
box->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC, rtgui_textbox_timeout, box);
|
2012-07-20 20:37:56 +08:00
|
|
|
/* set caret to show */
|
|
|
|
box->flag |= RTGUI_TEXTBOX_CARET_SHOW;
|
|
|
|
/* start caret timer */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_start(box->caret_timer);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object *widget, rtgui_event_t *event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box = RTGUI_TEXTBOX(widget);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* stop caret timer */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->caret_timer != RT_NULL)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_timer_stop(box->caret_timer);
|
|
|
|
rtgui_timer_destory(box->caret_timer);
|
|
|
|
box->caret_timer = RT_NULL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
2012-07-20 20:37:56 +08:00
|
|
|
/* set caret to hide */
|
|
|
|
box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_draw_caret(box, box->position);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->on_enter != RT_NULL)
|
|
|
|
box->on_enter(box, event);
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
return RT_TRUE;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *rtgui_textbox_create(const char *text, rt_uint32_t flag)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
box = (struct rtgui_textbox *)rtgui_widget_create(RTGUI_TEXTBOX_TYPE);
|
|
|
|
if (box != RT_NULL)
|
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
/* allocate default line buffer */
|
|
|
|
rtgui_textbox_set_value(box, text);
|
2010-03-09 07:11:34 +08:00
|
|
|
box->flag = flag;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_textbox_destroy(rtgui_textbox_t *box)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(box));
|
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_textbox_ondraw(rtgui_textbox_t *box)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
/* draw button */
|
|
|
|
rtgui_rect_t rect;
|
2012-11-26 10:42:52 +08:00
|
|
|
struct rtgui_dc *dc;
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_color_t fc;
|
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
|
|
|
|
/* begin drawing */
|
|
|
|
dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
|
|
|
|
if (dc == RT_NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* get widget rect */
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
|
2012-08-13 14:30:05 +08:00
|
|
|
fc = RTGUI_WIDGET_FOREGROUND(box);
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
rtgui_rect_inflate(&rect, -1);
|
|
|
|
|
|
|
|
/* fill widget rect with white color */
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_BACKGROUND(box) = white;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_dc_fill_rect(dc, &rect);
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
rtgui_rect_inflate(&rect, 1);
|
|
|
|
/* draw border */
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_FOREGROUND(box) = RTGUI_RGB(123, 158, 189);
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_dc_draw_rect(dc, &rect);
|
|
|
|
|
|
|
|
/* draw text */
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_FOREGROUND(box) = fc;
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->text != RT_NULL)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-07-20 20:37:56 +08:00
|
|
|
rect.x1 += RTGUI_WIDGET_DEFAULT_MARGIN;
|
|
|
|
/* draw single text */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->flag & RTGUI_TEXTBOX_MASK)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
/* draw mask char */
|
2012-07-20 20:37:56 +08:00
|
|
|
rt_size_t len = rt_strlen(box->text);
|
2012-11-26 10:42:52 +08:00
|
|
|
if (len > 0)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
char *text_mask = rtgui_malloc(len + 1);
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_memset(text_mask, box->mask_char, len + 1);
|
2012-07-20 20:37:56 +08:00
|
|
|
text_mask[len] = 0;
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_dc_draw_text(dc, text_mask+box->first_pos, &rect);
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_free(text_mask);
|
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_dc_draw_text(dc, box->text+box->first_pos, &rect);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set textbox text */
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_textbox_set_value(rtgui_textbox_t *box, const char *text)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
if (box->text != RT_NULL)
|
|
|
|
{
|
|
|
|
/* yet exist something */
|
2012-07-20 20:37:56 +08:00
|
|
|
/* free the old text */
|
|
|
|
rtgui_free(box->text);
|
|
|
|
box->text = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no something */
|
2012-11-26 10:42:52 +08:00
|
|
|
box->line_length = ((rt_strlen(text) + 1) / RTGUI_TEXTBOX_LINE_MAX + 1) * RTGUI_TEXTBOX_LINE_MAX;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* allocate line buffer */
|
2012-11-26 10:42:52 +08:00
|
|
|
box->text = rtgui_malloc(box->line_length+1);
|
|
|
|
rt_memset(box->text, 0, box->line_length+1);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* copy text */
|
|
|
|
rt_memcpy(box->text, text, rt_strlen(text) + 1);
|
|
|
|
|
|
|
|
/* set current position */
|
2012-07-20 20:37:56 +08:00
|
|
|
box->position = rt_strlen(text);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
const char *rtgui_textbox_get_value(rtgui_textbox_t *box)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
return (const char *)box->text;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_textbox_set_mask_char(rtgui_textbox_t *box, const char ch)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
box->mask_char = ch;
|
|
|
|
}
|
|
|
|
|
2012-12-14 11:34:24 +08:00
|
|
|
char rtgui_textbox_get_mask_char(rtgui_textbox_t *box)
|
2012-11-26 10:42:52 +08:00
|
|
|
{
|
|
|
|
return box->mask_char;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtgui_textbox_set_line_length(rtgui_textbox_t *box, rt_size_t length)
|
|
|
|
{
|
|
|
|
rt_uint8_t *new_line;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
RT_ASSERT(box != RT_NULL);
|
|
|
|
|
|
|
|
/* invalid length */
|
2012-11-26 10:42:52 +08:00
|
|
|
if (length <= 0)
|
2012-07-20 20:37:56 +08:00
|
|
|
return;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
new_line = rtgui_malloc(length);
|
2012-11-26 10:42:52 +08:00
|
|
|
if (length < box->line_length)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rt_memcpy(new_line, box->text, length - 1);
|
|
|
|
new_line[length] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_memcpy(new_line, (const char *)box->text, rt_strlen((const char *)box->text));
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set line length */
|
|
|
|
box->line_length = length;
|
|
|
|
}
|
2010-01-04 08:05:01 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
/* get textbox text area */
|
2012-11-26 10:42:52 +08:00
|
|
|
void rtgui_textbox_get_edit_rect(rtgui_textbox_t *box, rtgui_rect_t *rect)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(box), rect);
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_rect_inflate(rect, -1);
|
2012-07-20 20:37:56 +08:00
|
|
|
}
|
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
rt_bool_t rtgui_textbox_event_handler(struct rtgui_object *object, rtgui_event_t *event)
|
2012-07-20 20:37:56 +08:00
|
|
|
{
|
|
|
|
rtgui_widget_t *widget = RTGUI_WIDGET(object);
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_t *box = RTGUI_TEXTBOX(object);
|
2012-07-20 20:37:56 +08:00
|
|
|
|
|
|
|
switch (event->type)
|
|
|
|
{
|
2012-11-26 10:42:52 +08:00
|
|
|
case RTGUI_EVENT_PAINT:
|
2012-07-20 20:37:56 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-11-26 10:42:52 +08:00
|
|
|
if (widget->on_draw != RT_NULL)
|
|
|
|
widget->on_draw(RTGUI_OBJECT(widget), event);
|
|
|
|
else
|
2012-07-20 20:37:56 +08:00
|
|
|
#endif
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_ondraw(box);
|
|
|
|
break;
|
2012-07-20 20:37:56 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
case RTGUI_EVENT_MOUSE_BUTTON:
|
2012-07-20 20:37:56 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-11-26 10:42:52 +08:00
|
|
|
if (widget->on_mouseclick != RT_NULL)
|
|
|
|
widget->on_mouseclick(RTGUI_OBJECT(widget), event);
|
|
|
|
else
|
2012-07-20 20:37:56 +08:00
|
|
|
#endif
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_onmouse(box, (struct rtgui_event_mouse *)event);
|
|
|
|
return RT_TRUE;
|
2012-07-20 20:37:56 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
case RTGUI_EVENT_KBD:
|
2012-07-20 20:37:56 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2012-11-26 10:42:52 +08:00
|
|
|
if (widget->on_key != RT_NULL)
|
|
|
|
widget->on_key(RTGUI_OBJECT(widget), event);
|
|
|
|
else
|
2012-07-20 20:37:56 +08:00
|
|
|
#endif
|
2012-11-26 10:42:52 +08:00
|
|
|
rtgui_textbox_onkey(RTGUI_OBJECT(box), (struct rtgui_event *)event);
|
|
|
|
return RT_TRUE;
|
2012-07-20 20:37:56 +08:00
|
|
|
|
2012-11-26 10:42:52 +08:00
|
|
|
default:
|
|
|
|
return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event);
|
2012-07-20 20:37:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|