static void rtgui_textbox_timeout(rtgui_timer_t *timer, void *parameter) { rtgui_textbox_t *box; box = RTGUI_TEXTBOX(parameter); /* set caret flag */ if (box->flag & RTGUI_TEXTBOX_CARET_SHOW) box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; else box->flag |= RTGUI_TEXTBOX_CARET_SHOW; rtgui_textbox_draw_caret(box, box->position); }
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object* object, struct rtgui_event* event) { struct rtgui_textbox* box; RTGUI_WIDGET_EVENT_HANDLER_PREPARE box = RTGUI_TEXTBOX(object); /* stop caret timer */ rtgui_timer_stop(box->caret_timer); /* set caret to hide */ box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; return RT_TRUE; }
static rt_bool_t rtgui_textbox_onfocus(struct rtgui_object *widget, rtgui_event_t *event) { rtgui_textbox_t *box = RTGUI_TEXTBOX(widget); /* if there is already a timer, don't create another one. */ if (box->caret_timer == RT_NULL) { box->caret_timer = rtgui_timer_create(50, RT_TIMER_FLAG_PERIODIC, rtgui_textbox_timeout, box); /* set caret to show */ box->flag |= RTGUI_TEXTBOX_CARET_SHOW; /* start caret timer */ if (box->caret_timer != RT_NULL) rtgui_timer_start(box->caret_timer); } return RT_TRUE; }
static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_object *widget, rtgui_event_t *event) { rtgui_textbox_t *box = RTGUI_TEXTBOX(widget); /* stop caret timer */ if (box->caret_timer != RT_NULL) { rtgui_timer_stop(box->caret_timer); rtgui_timer_destory(box->caret_timer); box->caret_timer = RT_NULL; } /* set caret to hide */ box->flag &= ~RTGUI_TEXTBOX_CARET_SHOW; rtgui_textbox_draw_caret(box, box->position); if (box->on_enter != RT_NULL) box->on_enter(box, event); return RT_TRUE; }
rt_bool_t rtgui_textbox_event_handler(struct rtgui_object *object, rtgui_event_t *event) { rtgui_widget_t *widget = RTGUI_WIDGET(object); rtgui_textbox_t *box = RTGUI_TEXTBOX(object); switch (event->type) { case RTGUI_EVENT_PAINT: #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_draw != RT_NULL) widget->on_draw(RTGUI_OBJECT(widget), event); else #endif rtgui_textbox_ondraw(box); break; case RTGUI_EVENT_MOUSE_BUTTON: #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(RTGUI_OBJECT(widget), event); else #endif rtgui_textbox_onmouse(box, (struct rtgui_event_mouse *)event); return RT_TRUE; case RTGUI_EVENT_KBD: #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_key != RT_NULL) widget->on_key(RTGUI_OBJECT(widget), event); else #endif rtgui_textbox_onkey(RTGUI_OBJECT(box), (struct rtgui_event *)event); return RT_TRUE; default: return rtgui_widget_event_handler(RTGUI_OBJECT(widget), event); } return RT_FALSE; }
rt_bool_t rtgui_textbox_event_handler(struct rtgui_object* object, struct rtgui_event* event) { struct rtgui_textbox* box; RTGUI_WIDGET_EVENT_HANDLER_PREPARE box = RTGUI_TEXTBOX(object); switch (event->type) { case RTGUI_EVENT_PAINT: #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); else #endif rtgui_theme_draw_textbox(box); break; case RTGUI_EVENT_MOUSE_BUTTON: if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE; #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event); else #endif rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event); return RT_TRUE; case RTGUI_EVENT_KBD: if (!RTGUI_WIDGET_IS_ENABLE(widget) || RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE; #ifndef RTGUI_USING_SMALL_SIZE if (widget->on_key != RT_NULL) widget->on_key(widget, event); else #endif rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event); return RT_TRUE; } return RT_FALSE; }
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) & ~0x1; } length = rt_strlen((char*)box->text); if (ekbd->key == RTGUIK_DELETE) { int chw; unsigned char *c; if (TB_ABSPOS(box) == length) { goto _exit; } chw = _string_char_width((char*)box->text, length, TB_ABSPOS(box)).char_width; /* remove character */ for (c = &box->text[TB_ABSPOS(box)]; c[chw] != '\0'; c++) *c = c[chw]; *c = '\0'; } else if (ekbd->key == RTGUIK_BACKSPACE) { /* delete front character */ if (box->position == 0) { if(box->first_pos == 0) goto _exit; if(box->first_pos > box->dis_length) { int head_fix; int chw = _string_char_width((char*)box->text, length, TB_ABSPOS(box) - 1).char_width; rt_memmove(box->text + TB_ABSPOS(box) - chw, box->text + TB_ABSPOS(box), length - TB_ABSPOS(box) + 1); head_fix = 0; /* FIXME: */ if (box->text[box->first_pos - box->dis_length - chw] > 0x80) { int i; for (i = box->first_pos - box->dis_length - chw; i < box->first_pos; i++) { if (box->text[i] > 0x80) head_fix++; else break; } /* if the head is in middle of wide char, move one less * byte */ head_fix = head_fix % 2; } box->first_pos = box->first_pos - box->dis_length + head_fix - chw; box->position = box->dis_length - head_fix; } else { int chw; if (box->text[TB_ABSPOS(box) - 1] < 0x80) { /* also copy the \0 */ rt_memmove(box->text + TB_ABSPOS(box) - 1, box->text + TB_ABSPOS(box), length - TB_ABSPOS(box) + 1); chw = 1; } else { rt_memmove(box->text + TB_ABSPOS(box) - 2, box->text + TB_ABSPOS(box), length - TB_ABSPOS(box) + 1); chw = 2; } box->position = box->first_pos - chw; box->first_pos = 0; } } else { unsigned char *c; int chw; chw = _string_char_width((char*)box->text, length, TB_ABSPOS(box) - 1).char_width; /* remove character */ for (c = &box->text[TB_ABSPOS(box) - chw]; c[chw] != '\0'; c++) *c = c[chw]; *c = '\0'; box->position -= chw; } } else if (ekbd->key == RTGUIK_LEFT) { int chw; if (box->first_pos == 0 && box->position == 0) goto _exit; if (box->text[TB_ABSPOS(box) - 1] > 0x80) chw = 2; else chw = 1; /* move to prev */ if (box->position >= chw) { box->position -= chw; } else { if (box->first_pos >= chw) box->first_pos -= chw; else box->first_pos = 0; } } else if (ekbd->key == RTGUIK_RIGHT) { int chw; if ((TB_ABSPOS(box) + 2) <= length && box->text[TB_ABSPOS(box)] > 0x80) chw = 2; else chw = 1; /* move to next */ if (TB_ABSPOS(box) < length) { if(box->position + chw <= box->dis_length) box->position += chw; else { /* always move one wide char when the first char is wide */ if (box->text[box->first_pos] > 0x80) { box->first_pos += 2; if (chw == 2) { int i; int head_fix = 0; for (i = box->first_pos; i < box->first_pos + box->dis_length; i++) { if (box->text[i] > 0x80) head_fix++; } head_fix %= 2; if (head_fix) { box->first_pos += 2; box->position = box->dis_length - 1; } } else if (chw == 1) /* we have moved the box by 2 bytes but the last one is * a narrow char */ box->position -= 1; else RT_ASSERT(0); } else box->first_pos += chw; } } } 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 { rt_uint16_t chr; int chw; if (!(ekbd->unicode || isprint(ekbd->key))) goto _exit; if (ekbd->unicode) { chr = ekbd->unicode; chw = 2; } else { chr = ekbd->key; chw = 1; } if (box->flag & RTGUI_TEXTBOX_DIGIT) { /* only input digit */ if (!isdigit(chr)) { /* exception: '.' and '-' */ if (chr != '.' && chr != '-')return RT_FALSE; if (chr == '.' && strchr((char*)box->text, '.'))return RT_FALSE; if (chr == '-') { if (length + chw > box->line_length) return RT_FALSE; if (strchr((char*)box->text, '-')) { unsigned char *c; for (c = &box->text[0]; c != &box->text[length]; c++) *c = *(c + 1); box->text[length] = '\0'; box->position --; goto _exit; } else { unsigned 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 + chw > box->line_length) return RT_FALSE; if (TB_ABSPOS(box) <= length - 1) { unsigned char *c; for (c = &box->text[length + chw - 1]; c != &box->text[TB_ABSPOS(box)]; c -= 1) *c = *(c - chw); } if (chw == 1) { box->text[TB_ABSPOS(box)] = chr; } else if (chw == 2) { box->text[TB_ABSPOS(box)] = chr >> 8; box->text[TB_ABSPOS(box)+1] = chr & 0xFF; } else {
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; }