Beispiel #1
0
Datei: edit.c Projekt: amsl/RTGUI
static void rtgui_edit_init_caret(struct rtgui_edit *edit, rtgui_point_t visual)
{
	struct rtgui_gdev *grp = rtgui_gdev_get();
	int x, y;
	rtgui_color_t color;
	rtgui_rect_t rect;
	int ofs=0;

	RT_ASSERT(edit != RT_NULL);
	if(!RTGUI_WIDGET_IS_FOCUSED(edit)) return;

	rtgui_edit_get_caret_rect(edit, &edit->caret_rect, visual);
	rect = edit->caret_rect;
	rtgui_widget_rect_to_device(RTGUI_WIDGET(edit), &rect);

	if(edit->caret == RT_NULL)
		edit->caret = (rtgui_color_t*)rtgui_malloc(RC_W(rect) * RC_H(rect)*sizeof(rtgui_color_t));
	rtgui_timer_stop(edit->caret_timer);

	for(x=rect.x1; x<rect.x2; x++)
	{
		for(y=rect.y1; y<rect.y2; y++)
		{
			grp->ops->get_pixel(&color,x,y);
			*(edit->caret + ofs++) = color;
		}
	}

	rtgui_timer_start(edit->caret_timer);
}
Beispiel #2
0
void rtgui_anim_stop(struct rtgui_animation *anim)
{
    RT_ASSERT(anim);

    anim->state = _ANIM_STOPPED;
    rtgui_timer_stop(anim->timer);
}
void rtgui_anim_stop(struct rtgui_animation *anim)
{
    RT_ASSERT(anim);

    anim->state = _ANIM_STOPPED;
    RTGUI_WIDGET_FLAG(anim->parent) &= ~RTGUI_WIDGET_FLAG_IN_ANIM;
    rtgui_timer_stop(anim->timer);
}
Beispiel #4
0
static rt_bool_t event_handler(struct rtgui_object *object, rtgui_event_t *event)
{
    struct rtgui_widget *widget = RTGUI_WIDGET(object);

    rt_kprintf("event_handler\r\n");

    if (event->type == RTGUI_EVENT_PAINT)
    {
        rt_kprintf("RTGUI_EVENT_PAINT\r\n");
        rtgui_win_event_handler((struct rtgui_object *)object, event);
        snake_draw(widget);
        rtgui_timer_start(timer);
    }
    else if (event->type == RTGUI_EVENT_SHOW)
    {
        rt_kprintf("RTGUI_EVENT_SHOW\r\n");
        rtgui_win_event_handler((struct rtgui_object *)object, event);
        snake_draw(widget);
        rtgui_timer_start(timer);
    }
    else if (event->type == RTGUI_EVENT_HIDE)
    {
        rt_kprintf("RTGUI_EVENT_HIDE\r\n");
        rtgui_win_event_handler((struct rtgui_object *)object, event);
        rtgui_timer_stop(timer);
    }
    else if (event->type == RTGUI_EVENT_WIN_DEACTIVATE)
    {
        rt_kprintf("RTGUI_EVENT_WIN_DEACTIVATE\r\n");
        rtgui_win_event_handler((struct rtgui_object *)object, event);
        rtgui_timer_stop(timer);
    }
    else if (event->type == RTGUI_EVENT_KBD)
    {
        rtgui_win_event_handler((struct rtgui_object *)object, event);
        snake_handler(widget, event);
    }
    else
    {
        rt_kprintf("event->type:%d\r\n", event->type);
        return rtgui_win_event_handler((struct rtgui_object *)object, event);
    }

    return RT_FALSE;
}
Beispiel #5
0
void rtgui_timer_destory(rtgui_timer_t* timer)
{
	RT_ASSERT(timer != RT_NULL);

	/* stop timer firstly */
	rtgui_timer_stop(timer);

	/* detach rt-thread timer */
	rt_timer_detach(&(timer->timer));

	rtgui_free(timer);
}
Beispiel #6
0
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;
}
Beispiel #7
0
/* AUTO窗口关闭时的事件处理 */
rt_bool_t auto_window_close(struct rtgui_object *object, struct rtgui_event *event)
{
    if (timer != RT_NULL)
    {
        /* 停止并删除定时器 */
        rtgui_timer_stop(timer);
        rtgui_timer_destory(timer);

        timer = RT_NULL;
    }
    autowin = RT_NULL;

    return RT_TRUE;
}
Beispiel #8
0
Datei: edit.c Projekt: amsl/RTGUI
static rt_bool_t rtgui_edit_onunfocus(pvoid wdt, rtgui_event_t* event)
{
	struct rtgui_edit* edit = RTGUI_EDIT(wdt);

	/* stop caret timer */
	if(edit->caret_timer != RT_NULL)
	{
		rtgui_timer_stop(edit->caret_timer);
		rtgui_timer_destory(edit->caret_timer);
	}
	/* set caret to hide */
	edit->flag &= ~RTGUI_EDIT_CARET;
	rtgui_edit_draw_caret(edit);
	
	return RT_TRUE;
}
Beispiel #9
0
void rtgui_timer_set_timeout(rtgui_timer_t *timer, rt_int32_t time)
{
    RT_ASSERT(timer != RT_NULL);

    /* start rt-thread timer */
    if (timer->state == RTGUI_TIMER_ST_RUNNING)
    {
        rtgui_timer_stop(timer);
        rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &time);
        rtgui_timer_start(timer);
    }
    else
    {
        rt_timer_control(&timer->timer, RT_TIMER_CTRL_SET_TIME, &time);
    }
}
Beispiel #10
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);
	}
}
Beispiel #11
0
void rtgui_timer_destory(rtgui_timer_t *timer)
{
    RT_ASSERT(timer != RT_NULL);

    /* stop timer firstly */
    rtgui_timer_stop(timer);
    if (timer->pending_cnt != 0 && timer->app->ref_count != 0)
    {
        timer->state = RTGUI_TIMER_ST_DESTROY_PENDING;
    }
    else
    {
        /* detach rt-thread timer */
        rt_timer_detach(&(timer->timer));
        rtgui_free(timer);
    }
}
Beispiel #12
0
/* 关闭对话框时的回调函数 */
void diag_close(struct rtgui_timer* timer, void* parameter)
{
	cnt --;
	rt_sprintf(label_text, "closed then %d second!", cnt);

	/* 设置标签文本并更新控件 */
	rtgui_label_set_text(label, label_text);
	rtgui_widget_update(RTGUI_WIDGET(label));

	if (cnt == 0)
	{
		/* 超时,关闭对话框 */
		rtgui_win_destroy(msgbox);

		/* 停止并删除定时器 */
		rtgui_timer_stop(timer);
		rtgui_timer_destory(timer);
	}
}
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;
}
Beispiel #14
0
static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
    if (event->type == RTGUI_EVENT_PAINT)
    {
        struct rtgui_dc* dc;
        struct rtgui_rect rect;
        rtgui_image_t *background;

		/* draw child */
		rtgui_view_event_handler(widget, event);

        dc = rtgui_dc_begin_drawing(widget);
        if (dc == RT_NULL) return RT_FALSE;
        rtgui_widget_get_rect(widget, &rect);

        /* draw background */
        background = rtgui_image_create_from_file("hdc", "/resource/bg.hdc", RT_FALSE);
        if (background != RT_NULL)
        {
            rtgui_image_blit(background, dc, &rect);
            rtgui_image_destroy(background);
        }
        else
        {
            rtgui_dc_fill_rect(dc, &rect);
        }

        /* draw playing information */
		player_update_tag_info();

        rtgui_dc_end_drawing(dc);

        return RT_FALSE;
    }
    else if (event->type == RTGUI_EVENT_KBD)
    {
        struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
        if (ekbd->type == RTGUI_KEYDOWN)
        {
        	if ((ekbd->key == RTGUIK_LEFT) || (ekbd->key == RTGUIK_RIGHT))
        	{
        		if (player_mode == PLAYER_STOP)
        		{
                    rtgui_view_show(RTGUI_VIEW(function_view), RT_FALSE);
        		}
        		else
        		{
        			rt_device_t dev = rt_device_find("snd");
        			if (ekbd->key == RTGUIK_LEFT && radio_setup.default_volume > 0)
        			{
        				radio_setup.default_volume--;
        				rt_device_control(dev, CODEC_CMD_VOLUME, &radio_setup.default_volume);
        			}
        			else if (ekbd->key == RTGUIK_RIGHT && radio_setup.default_volume < CODEC_VOLUME_MAX)
        			{
        				radio_setup.default_volume++;
        				rt_device_control(dev, CODEC_CMD_VOLUME, &radio_setup.default_volume);
        			}
        		}
        	}
			else
			{
				return RTGUI_WIDGET(music_listbox)->event_handler(RTGUI_WIDGET(music_listbox), event);
			}
        }
        return RT_FALSE;
    }
	else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
	{
		struct rtgui_event_mouse* emouse;

		emouse = (struct rtgui_event_mouse*)event;
		if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
		{
			if (rtgui_rect_contains_point(&next_btn_rect, emouse->x, emouse->y) == RT_EOK)
				player_onbutton(NEXT_BUTTON);
			else if (rtgui_rect_contains_point(&prev_btn_rect, emouse->x, emouse->y) == RT_EOK)
				player_onbutton(PREV_BUTTON);
			else if (rtgui_rect_contains_point(&playing_btn_rect, emouse->x, emouse->y) == RT_EOK)
				player_onbutton(PLAYING_BUTTON);
		}
	}
    else if (event->type == RTGUI_EVENT_COMMAND)
    {
        struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;

        switch (ecmd->command_id)
        {
        case PLAYER_REQUEST_PLAY_SINGLE_FILE:
        case PLAYER_REQUEST_PLAY_LIST:
            rtgui_timer_start(info_timer);
			return RT_TRUE;

        case PLAYER_REQUEST_STOP:
        {
			struct play_item *item = RT_NULL;

			/* if it's radio mode, set next mode to stop */
			if (player_mode == PLAYER_PLAY_RADIO)
				next_step = PLAYER_STEP_STOP;

			/* set player mode */
			player_mode = PLAYER_STOP;

			switch (next_step)
			{
			case PLAYER_STEP_NEXT:
				/* play next */
				item = play_list_next(play_list_get_mode());
				break;
			case PLAYER_STEP_PREV:
				/* play prev */
				item = play_list_prev(play_list_get_mode());
				break;
			case PLAYER_STEP_SEEK:
				/* play current item */
				item = play_list_current();
			}

			if (item != RT_NULL)
				player_play_item(item);
			else
			{
				player_mode = PLAYER_STOP;
				rtgui_timer_stop(info_timer);
			}

			/* update tag information */
			player_update_tag_info();
        }
		return RT_TRUE;

        case PLAYER_REQUEST_FREEZE:
        {
            /* stop play */
            if (player_is_playing() == RT_TRUE)
            {
                player_stop();
            }

            /* delay some tick */
            rt_thread_delay(50);

            /* show a modal view */
            {
                rtgui_view_t *view;
                rtgui_label_t *label;
                rtgui_rect_t rect = {0, 0, 150, 150}, container_rect;

                rtgui_graphic_driver_get_default_rect(&container_rect);
                /* set centre */
                rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
                view = rtgui_view_create("USB");
                rtgui_workbench_add_view(workbench, view);

                /* set container to window rect */
                container_rect = rect;

                rect.x1 = 0;
                rect.y1 = 0;
                rect.x2 = 120;
                rect.y2 = 20;
                label = rtgui_label_create("USB 联机中...");
                rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
                rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
                rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label));

                rtgui_view_show(view, RT_TRUE);
                /* never reach hear */
            }
        }

		case PLAYER_REQUEST_UPDATE_INFO:
			/* update status information */
			player_update_tag_info();
			return RT_TRUE;

        default:
            break;
        }

        return RT_FALSE;
    }

    return rtgui_view_event_handler(widget, event);
}
Beispiel #15
0
Datei: edit.c Projekt: amsl/RTGUI
static rt_bool_t rtgui_edit_onkey(pvoid wdt, rtgui_event_t* event)
{
	enum { EDIT_NONE, EDIT_ONDRAW, EDIT_UPDATE };
	struct rtgui_edit *edit = RTGUI_EDIT(wdt);
	struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*)event;
	struct edit_line *line=RT_NULL;
	rt_bool_t update_type = EDIT_NONE;
	
	RT_ASSERT(edit != RT_NULL);
	RT_ASSERT(ekbd != RT_NULL);

	if (RTGUI_KBD_IS_UP(ekbd))
	{	/* reset function key */
		if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
			edit->flag &= ~RTGUI_EDIT_CTRL;
		else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
			edit->flag &= ~RTGUI_EDIT_ALT;
		else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
			edit->flag &= ~RTGUI_EDIT_SHIFT;
		else if(ekbd->key == RTGUIK_CAPSLOCK)
			edit->flag &= ~RTGUI_EDIT_CAPSLOCK;
		else if(ekbd->key == RTGUIK_NUMLOCK)
			edit->flag &= ~RTGUI_EDIT_NUMLOCK;
		return RT_TRUE;
	}
	
	line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
	if(line == RT_NULL) 
		return RT_FALSE;

	/* rt_kprintf("key=%04X ",ekbd->key); */
	if(ekbd->key == RTGUIK_RCTRL || ekbd->key == RTGUIK_LCTRL)
	{	/* use CTRL key */
		edit->flag |= RTGUI_EDIT_CTRL;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RALT || ekbd->key == RTGUIK_LALT)
	{	/* use ALT key */
		edit->flag |= RTGUI_EDIT_ALT;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RSHIFT || ekbd->key == RTGUIK_LSHIFT)
	{	/* use SHIFT key */
		edit->flag |= RTGUI_EDIT_SHIFT;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_CAPSLOCK)
	{
		edit->flag |= RTGUI_EDIT_CAPSLOCK;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_NUMLOCK)
	{
		edit->flag |= RTGUI_EDIT_NUMLOCK;
		return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_DELETE)
	{	/* delete latter character */
		int ofs = edit->upleft.x + edit->visual.x;
		if(ofs > line->len - 1 || (ofs==0 && line->len==0))
		{	/* will the next line marges into the current line */
			struct edit_line* next_line = line->next;
			if(next_line != RT_NULL)
			{
				struct edit_line *update_end_line;
				
				update_type = EDIT_UPDATE;
				edit->update.start = edit->visual;

				rtgui_edit_connect_line(edit, line, next_line);
				rtgui_edit_delete_line(edit, next_line);

				if(edit->max_rows-edit->upleft.y > edit->row_per_page)
				{
					update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
					if(update_end_line != RT_NULL)
					{
						edit->update.end.x = edit->col_per_page;
						edit->update.end.y = edit->upleft.y + edit->row_per_page;
					}
				}
				else
				{
					int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
					edit->update.end.x = edit->col_per_page;
					edit->update.end.y = update_end_index+1;
				}
			}
			line->len = rtgui_edit_line_strlen(line->text);
			goto _edit_exit;
		}
		else if(ofs == line->len - 1)
		{
			line->text[ofs] = '\0';
		}
		else
		{
			char *c;
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
			/* remove character */
			for(c = &line->text[ofs]; c[tmp_pos] != '\0'; c++)
				*c = c[tmp_pos];
			*c = '\0';
		}
		update_type = EDIT_UPDATE;
		edit->update.start = edit->visual;
		edit->update.end.x = line->len-edit->upleft.x;
		if (edit->update.end.x > edit->col_per_page)
			edit->update.end.x = edit->col_per_page;
		edit->update.end.y = edit->visual.y;
	}
	else if(ekbd->key == RTGUIK_BACKSPACE)
	{	
		if(edit->visual.x == 0)
		{   /* incorporated into prev line */
			struct rtgui_event_kbd event_kbd;
			struct edit_line* prev_line = line->prev;
			if(prev_line != RT_NULL)
			{
				struct edit_line *update_end_line;
				
				update_type = EDIT_UPDATE;
				edit->visual.x = prev_line->len;

				rtgui_edit_connect_line(edit, prev_line, line);
				kbd_event_set_key(&event_kbd, RTGUIK_UP);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				rtgui_edit_delete_line(edit, line);

				edit->update.start = edit->visual; /* update.start.y is changed */
				if(edit->max_rows-edit->upleft.y > edit->row_per_page)
				{
					update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page);
					if(update_end_line != RT_NULL)
					{
						edit->update.end.x = edit->col_per_page;
						edit->update.end.y = edit->upleft.y + edit->row_per_page;
					}
				}
				else
				{
					int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
					edit->update.end.x = edit->col_per_page;
					edit->update.end.y = update_end_index+1;
				}
			}
			goto _edit_exit;
		}
		
		/* delete front character */
		if(edit->visual.x == line->len)
		{
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			line->text[edit->visual.x-tmp_pos] = '\0';
			edit->visual.x -= tmp_pos;
		}
		else if(edit->visual.x != 0)
		{	/* remove current character */
			char *c;
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			/* remove character */
			for(c = &line->text[edit->visual.x - tmp_pos]; c[tmp_pos] != '\0'; c++)
			{
				*c = c[tmp_pos];
			}
			*c = '\0';
			edit->visual.x -= tmp_pos;
		}
		/* adjusted line buffer length */
		if(rtgui_edit_alloc_len(edit->bzsize, line->len+2) < line->zsize)
		{	
			line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
			line->text = rt_realloc(line->text, line->zsize);
		}
		update_type = EDIT_UPDATE;
		edit->update.start = edit->visual; 
		edit->update.end.x = line->len;
		edit->update.end.y = edit->visual.y;
	}
	else if(ekbd->key == RTGUIK_UP)
	{	/* move to prev line */
		struct edit_line* prev_line;
		if(edit->visual.y > 0)
			edit->visual.y --;
		else
		{
			/* change first row */
			if(edit->upleft.y > 0)
			{
				edit->upleft.y --;
				if(edit->first_line->prev != RT_NULL)
					edit->first_line = edit->first_line->prev;
				update_type = EDIT_ONDRAW;
			}
		}
		
		/* The position of the recount X */
		prev_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
		if(prev_line == RT_NULL)
			return RT_FALSE;

		if(edit->upleft.x > 0)
		{
			if(prev_line->len <= edit->upleft.x)
			{
				if(prev_line->len <= edit->col_per_page)
				{
					edit->upleft.x = 0;
					edit->visual.x = prev_line->len;
				}
				else
				{
					edit->upleft.x = prev_line->len - (edit->col_per_page-1);
					edit->visual.x = edit->col_per_page-1;
				}
				update_type = EDIT_ONDRAW;
			}
			else if(prev_line->len - edit->upleft.x < edit->col_per_page)
			{
				if(edit->visual.x > prev_line->len - edit->upleft.x)
					edit->visual.x = prev_line->len - edit->upleft.x;
				else
				{
					rt_uint32_t tmp_pos=0;
					if(identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
						edit->visual.x -= (2-tmp_pos);
				}
			}
		}
		else if(edit->visual.x > prev_line->len)
			edit->visual.x = prev_line->len;
		else if(prev_line->len >= 2)
		{
			rt_uint32_t tmp_pos=0;
			if(identify_double_byte(edit, prev_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
				edit->visual.x -= (2-tmp_pos);
		}

#ifdef RTGUI_EDIT_USING_SCROLL		
		/* update vscroll */
		if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
		{
			if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
				rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
		}
#endif
	}
	else if(ekbd->key == RTGUIK_DOWN)
	{	
		struct edit_line *tail_line, *next_line;
		tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
		if(tail_line != RT_NULL)
		{	/* it is tail line */
			if(tail_line == edit->tail) return RT_FALSE;
		}
		/* move to next line */
		if(edit->visual.y < edit->row_per_page - 2)
		{
			edit->visual.y ++;
		}
		else if(edit->visual.y+edit->upleft.y < edit->max_rows-1)
		{
			/* change first row */
			edit->upleft.y++;
			if(edit->first_line->next != RT_NULL)
				edit->first_line = edit->first_line->next;
			update_type = EDIT_ONDRAW;
		}
		
		/* adjust next line end position */
		next_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
		if(next_line == RT_NULL)	
			return RT_FALSE;
		
		if(edit->upleft.x > 0)
		{
			if(next_line->len <= edit->upleft.x)
			{
				if(next_line->len <= edit->col_per_page)
				{
					edit->upleft.x = 0;
					edit->visual.x = next_line->len;
				}
				else
				{
					edit->upleft.x = next_line->len - (edit->col_per_page-1);
					edit->visual.x = edit->col_per_page-1;
				}
				update_type = EDIT_ONDRAW;
			}
			else if(next_line->len - edit->upleft.x < edit->col_per_page)
			{
				if(edit->visual.x > next_line->len - edit->upleft.x)
					edit->visual.x = next_line->len - edit->upleft.x;
				else
				{
					rt_uint32_t tmp_pos=0;
					if(identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
						edit->visual.x -= (2-tmp_pos);
				}
			}
		}
		else if(edit->visual.x > next_line->len)
			edit->visual.x = next_line->len;
		else if(next_line->len >= 2)
		{
			rt_uint32_t tmp_pos=0;
			if(identify_double_byte(edit, next_line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
				edit->visual.x -= (2-tmp_pos);
		}

#ifdef RTGUI_EDIT_USING_SCROLL		
		/* update vscroll */
		if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
		{
			if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
				rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
		}
#endif
	}
	else if(ekbd->key == RTGUIK_LEFT)
	{	/* move to prev char */
		if(edit->visual.x > 0)
		{
			rt_uint32_t tmp_pos=1;
			identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
			edit->visual.x -= tmp_pos;
			if(edit->visual.x == -1)
			{
				edit->visual.x = 0;
				edit->upleft.x -= 1;
				update_type = EDIT_ONDRAW;
			}
		}
		else
		{
			if(edit->upleft.x > 0)
			{
				rt_uint32_t tmp_pos=1;
				identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos);
				edit->upleft.x -= tmp_pos;
				update_type = EDIT_ONDRAW;
			}
			else
			{	
				struct rtgui_event_kbd event_kbd;
				struct edit_line* first_line;
				first_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
				if(first_line != RT_NULL)
				{	/* it is head line */
					if(first_line == edit->head) return RT_FALSE;
				}
				/* move the caret to the prev line end */
				kbd_event_set_key(&event_kbd, RTGUIK_UP);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_END);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
	}
	else if(ekbd->key == RTGUIK_RIGHT)
	{	/* move to next char */
		if(line->len >= edit->col_per_page)
		{
			if(edit->upleft.x+edit->col_per_page <= line->len)
			{
				if(edit->visual.x < edit->col_per_page-1)
				{
					rt_uint32_t tmp_pos=1;
					identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
					edit->visual.x += tmp_pos;
				}
				else if(edit->visual.x == edit->col_per_page-1)
				{
					if(edit->upleft.x+edit->col_per_page < line->len)
						edit->upleft.x ++;
					else
						edit->upleft.x = line->len - edit->col_per_page + 1;
					update_type = EDIT_ONDRAW;
				}
			}
			else
			{
				struct rtgui_event_kbd event_kbd;
				/* move to next head */
				kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_HOME);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
		else
		{
			if(edit->visual.x < line->len)
			{
				rt_uint32_t tmp_pos=1;
				identify_double_byte(edit, line, EDIT_IDENT_DIR_RIGHT, &tmp_pos);
				edit->visual.x += tmp_pos;
			}
			else
			{
				struct rtgui_event_kbd event_kbd;
				struct edit_line* tail_line;
				tail_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y + edit->visual.y);
				if(tail_line != RT_NULL)
				{	/* it is tail line */
					if(tail_line == edit->tail) return RT_FALSE;
				}
				/* move the caret to the next line head */
				kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
				kbd_event_set_key(&event_kbd, RTGUIK_HOME);
				rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
			}
		}
	}
	else if(ekbd->key == RTGUIK_HOME)
	{	/* move cursor to line head */
		edit->visual.x = 0;
		if(edit->upleft.x > 0)
		{
			edit->upleft.x = 0;
			update_type = EDIT_ONDRAW;
		}
	}
	else if(ekbd->key == RTGUIK_END)
	{	/* move cursor to line tail */
		if(line->len >= edit->col_per_page)
		{
			edit->visual.x = edit->col_per_page - 1;
			edit->upleft.x = line->len - (edit->col_per_page-1);
			update_type = EDIT_ONDRAW;
		}
		else
			edit->visual.x = line->len;
	}
	else if(ekbd->key == RTGUIK_TAB)
	{
		int space_nums;
		struct rtgui_event_kbd event_kbd;

		/* using spaces to replace TAB */
		space_nums = edit->tabsize - (edit->upleft.x+edit->visual.x) % edit->tabsize;
		while(space_nums--)
		{
			kbd_event_set_key(&event_kbd, RTGUIK_SPACE);
			rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
		}
	}
	else if(ekbd->key == RTGUIK_PAGEUP)
	{
		if(edit->max_rows <= edit->row_per_page)
			return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_PAGEDOWN)
	{
		if(edit->max_rows <= edit->row_per_page)
			return RT_FALSE;
	}
	else if(ekbd->key == RTGUIK_RETURN)
	{
		struct edit_line *update_end_line;
		struct rtgui_event_kbd event_kbd;
	
		/* insert a new line buffer */
		rtgui_edit_insert_line(edit, line, line->text + edit->upleft.x + edit->visual.x);
		line->text[edit->upleft.x + edit->visual.x] = '\0';
		line->len = rtgui_edit_line_strlen(line->text);
		
		/* adjust update line end position */
		if((edit->max_rows-edit->upleft.y) > edit->row_per_page)
		{
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			update_end_line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->row_per_page-1);
			if(update_end_line != RT_NULL)
			{
				edit->update.end.x = update_end_line->len;
				edit->update.end.y = edit->upleft.y + edit->row_per_page;
			}
		}
		else if((edit->max_rows-edit->upleft.y) < edit->row_per_page)
		{
			int update_end_index = rtgui_edit_get_index_by_line(edit, edit->tail);
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			edit->update.end.x = edit->tail->len;
			edit->update.end.y = update_end_index;
		}
		
		/* move the caret to the next line head */
		kbd_event_set_key(&event_kbd, RTGUIK_DOWN);
		rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
		kbd_event_set_key(&event_kbd, RTGUIK_HOME);
		rtgui_edit_onkey(edit, (rtgui_event_t*)&event_kbd);
	}
	else
	{
		if(isprint((unsigned char)ekbd->key))
		{	/* it's may print character */
			update_type = EDIT_UPDATE;
			edit->update.start = edit->visual;
			
			if(edit->flag & RTGUI_EDIT_SHIFT)
				ekbd->key = query_shift_code(ekbd->key);
			if(edit->flag & RTGUI_EDIT_CAPSLOCK)
				ekbd->key = query_caps_code(ekbd->key);

			if(line->len < line->zsize-1)
			{
				int ofs = edit->upleft.x + edit->visual.x;
				if(edit->visual.x >= edit->col_per_page-1)
				{
					edit->upleft.x ++;
					update_type = EDIT_ONDRAW;
				}

				if(ofs < line->len)
				{
					char* c;
					for(c = &line->text[line->len]; c != &line->text[ofs]; c--)
						*c = *(c-1);
				}
				line->text[ofs] = ekbd->key;
				if(edit->visual.x < edit->col_per_page-1)
					edit->visual.x ++;
				line->text[line->len+1] = '\0';
				line->len = rtgui_edit_line_strlen(line->text);
				edit->update.end.x = line->len;
				if(edit->update.end.x > edit->col_per_page)
					edit->update.end.x = edit->col_per_page;
				edit->update.end.y = edit->visual.y;
			}
			else
			{	/* adjust line buffer's zone size */
				line->zsize = rtgui_edit_alloc_len(edit->bzsize, line->len+1);
				line->text = rt_realloc(line->text, line->zsize);
				rtgui_edit_onkey(edit, event); /* reentry */
			}
		}
		else
		{
			/* Is small keyboard ? */
			if(edit->flag & RTGUI_EDIT_NUMLOCK)
			{
				if(is_small_keyboard(&ekbd->key))
					rtgui_edit_onkey(edit, event);
				/* small keyboard another value reserved */
			}
		}
	}
	line->len = rtgui_edit_line_strlen(line->text); 

_edit_exit:
	if(edit->flag & RTGUI_EDIT_CARET)
	{
		if(edit->caret_timer != RT_NULL)
			rtgui_timer_stop(edit->caret_timer);

		edit->flag &= ~RTGUI_EDIT_CARET;
		rtgui_edit_draw_caret(edit);/* refresh it */
		if(edit->caret_timer != RT_NULL)
			rtgui_timer_start(edit->caret_timer);
	}

	/* re-draw edit widget */
	if(update_type == EDIT_ONDRAW)
		rtgui_edit_ondraw(edit);
	else if(update_type == EDIT_UPDATE)
		rtgui_edit_update(edit);

	if(RTGUI_WIDGET_IS_FOCUSED(edit))
	{
		rtgui_edit_init_caret(edit, edit->visual);
		edit->flag |= RTGUI_EDIT_CARET;
		rtgui_edit_draw_caret(edit);
	}
	return RT_TRUE;
}
Beispiel #16
0
Datei: edit.c Projekt: amsl/RTGUI
static void rtgui_edit_onmouse(struct rtgui_edit* edit, struct rtgui_event_mouse* emouse)
{
	rtgui_rect_t rect;

	RT_ASSERT(edit != RT_NULL);
	RT_ASSERT(emouse != RT_NULL);

	rtgui_widget_get_rect(RTGUI_WIDGET(edit), &rect);
	if((rtgui_region_contains_point(&(RTGUI_WIDGET(edit)->clip), emouse->x, emouse->y, &rect) == RT_EOK))
	{
		rt_uint16_t x, y;

		/* multiline text */
		x = (emouse->x - rect.x1) / (edit->font_width);
		y = (emouse->y - rect.y1) / (edit->item_height);
		if((x < edit->col_per_page) && (y < edit->row_per_page))
		{
			if(emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
			{
				struct edit_line *line;
				rt_uint32_t tmp_pos=0;
				
				edit->visual.x = x;
				edit->visual.y = y;
				
				line = rtgui_edit_get_line_by_index(edit, edit->upleft.y+edit->visual.y);
				if(line == RT_NULL)
					return;
				
				if(edit->visual.x > line->len)
					edit->visual.x = line->len;
				if(edit->upleft.x > 0)
				{
					if(edit->upleft.x >= line->len)
						edit->upleft.x = 0;
					else
						edit->visual.x -= edit->upleft.x;
					rtgui_edit_ondraw(edit);
				}
				if(identify_double_byte(edit, line, EDIT_IDENT_DIR_LEFT, &tmp_pos))
					edit->visual.x -= (2-tmp_pos);
				if(edit->flag & RTGUI_EDIT_CARET)
				{
					if(edit->caret_timer != RT_NULL)
						rtgui_timer_stop(edit->caret_timer);

					edit->flag &= ~RTGUI_EDIT_CARET;
					rtgui_edit_draw_caret(edit);

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

				/* set widget focus */
				rtgui_widget_focus(RTGUI_WIDGET(edit));

				if(RTGUI_WIDGET_IS_FOCUSED(edit))
				{
					rtgui_edit_init_caret(edit, edit->visual);
					edit->flag |= RTGUI_EDIT_CARET;
					rtgui_edit_draw_caret(edit);
				}
			}
			else if(emouse->button & RTGUI_MOUSE_BUTTON_UP)
			{
				/* please add codes at here. */
			}
#ifdef RTGUI_EDIT_USING_SCROLL			
			if(edit->vscroll && !RTGUI_WIDGET_IS_HIDE(edit))
			{
				if(!RTGUI_WIDGET_IS_HIDE(edit->vscroll))
					rtgui_scrollbar_set_value(edit->vscroll,edit->upleft.y);
			}
			if(edit->hscroll && !RTGUI_WIDGET_IS_HIDE(edit))
			{
				if(!RTGUI_WIDGET_IS_HIDE(edit->hscroll))
					rtgui_scrollbar_set_value(edit->hscroll,edit->upleft.x);
			}
#endif
		}
	}		
}
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;
}
static rt_bool_t stop_timer(struct rtgui_object *object, struct rtgui_event *event)
{
	if (bar_timer != RT_NULL)
		rtgui_timer_stop(bar_timer);
	return RT_TRUE;
}