struct rtgui_app *rtgui_app_create(const char *title)
{
    rt_thread_t tid = rt_thread_self();
    struct rtgui_app *app;
    struct rtgui_app *srv_app;
    struct rtgui_event_application event;
    char mq_name[RT_NAME_MAX];

    RT_ASSERT(tid != RT_NULL);
    RT_ASSERT(title != RT_NULL);

    /* create application */
    app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE));
    if (app == RT_NULL)
        return RT_NULL;

    /* one thread only can create one rtgui application */
    RT_ASSERT(tid->user_data == 0);
    app->tid = tid;

    rt_snprintf(mq_name, RT_NAME_MAX, "g%s", title);
    app->mq = rt_mq_create(mq_name, sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
    if (app->mq == RT_NULL)
    {
        rt_kprintf("create msgq failed.\n");
        goto __mq_err;
    }

    /* set application title */
    app->name = (unsigned char *)rt_strdup((char *)title);
    if (app->name == RT_NULL)
        goto __err;

    /* the first app should be the server */
    srv_app = rtgui_get_server();
    if (srv_app == RT_NULL)
    {
        /* set user thread */
        tid->user_data = (rt_uint32_t)app;
        return app;
    }

    RTGUI_EVENT_APP_CREATE_INIT(&event);
    event.app = app;

    /* notify rtgui server to one application has been created */
    if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
    {
        /* set user thread */
        tid->user_data = (rt_uint32_t)app;
        return app;
    }

__err:
__mq_err:
    rtgui_object_destroy(RTGUI_OBJECT(app));
    return RT_NULL;
}
Exemple #2
0
struct rtgui_app* rtgui_app_create(
        rt_thread_t tid,
        const char *title)
{
	rt_thread_t srv_tid;
	struct rtgui_app *app;
	struct rtgui_event_application event;

	RT_ASSERT(tid != RT_NULL);
	RT_ASSERT(title != RT_NULL);

	/* create application */
	app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE));
	if (app == RT_NULL)
		return RT_NULL;

	/* one thread only can create one rtgui application */
	RT_ASSERT(tid->user_data == 0);
	app->tid = tid;
	/* set user thread */
	tid->user_data = (rt_uint32_t)app;

	app->mq = rt_mq_create("rtgui", sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
	if (app->mq == RT_NULL)
	{
		rt_kprintf("create msgq failed.\n");
		goto __mq_err;
	}

	/* set application title */
	app->name = (unsigned char*)rt_strdup((char*)title);
	if (app->name == RT_NULL)
		goto __err;

	/* send a message to notify rtgui server */
	srv_tid = rtgui_get_server();
	if (srv_tid == RT_NULL)
	{
		rt_kprintf("gui server is not running.\n");
		goto __err;
	}

	/* create the rtgui server application */
	if (srv_tid == rt_thread_self())
		return app;

	RTGUI_EVENT_APP_CREATE_INIT(&event);
	event.app = app;

	/* notify rtgui server to one application has been created */
	if (rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
	{
		return app;
	}

__err:
__mq_err:
	rtgui_object_destroy(RTGUI_OBJECT(app));
	tid->user_data = 0;
	return RT_NULL;
}
Exemple #3
0
rt_bool_t rtgui_app_event_handler(struct rtgui_object* object, rtgui_event_t* event)
{
	struct rtgui_app* app;

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

	app = RTGUI_APP(object);

	switch (event->type)
	{
	case RTGUI_EVENT_PAINT:
	case RTGUI_EVENT_CLIP_INFO:
	case RTGUI_EVENT_WIN_ACTIVATE:
	case RTGUI_EVENT_WIN_DEACTIVATE:
	case RTGUI_EVENT_WIN_CLOSE:
	case RTGUI_EVENT_WIN_MOVE:
	case RTGUI_EVENT_KBD:
		_rtgui_application_dest_handle(app, event);
		break;

	case RTGUI_EVENT_APP_ACTIVATE:
		if (app->main_object != RT_NULL)
		{
			rtgui_win_activate(RTGUI_WIN(app->main_object));
			if (app->modal_object != RT_NULL)
				rtgui_win_activate(RTGUI_WIN(app->modal_object));
		}
		else if (app->modal_object != RT_NULL)
		{
			rtgui_win_activate(RTGUI_WIN(app->modal_object));
		}
		break;

	case RTGUI_EVENT_APP_DESTROY:
		rtgui_app_exit(app, 0);
		break;

	case RTGUI_EVENT_MOUSE_BUTTON:
	case RTGUI_EVENT_MOUSE_MOTION:
		{
			struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
			struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);

			// FIXME: let application determine the dest_wiget but not in sever
			// so we can combine this handler with above one
			if (app->modal_object != RT_NULL &&
				dest_object != app->modal_object)
			{
//				rt_kprintf("discard event %s that is not sent to modal object\n",
//						   event_string[event->type]);
			}
			else
			{
				_rtgui_application_dest_handle(app, event);
			}
		}
		break;

	case RTGUI_EVENT_TIMER:
		{
			struct rtgui_timer* timer;
			struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;

			timer = etimer->timer;
			if (timer->timeout != RT_NULL)
			{
				/* call timeout function */
				timer->timeout(timer, timer->user_data);
			}
		}
		break;

        case RTGUI_EVENT_COMMAND:
        {
            struct rtgui_event_command *ecmd = (struct rtgui_event_command*)event;

            if (ecmd->wid != RT_NULL)
                    return _rtgui_application_dest_handle(app, event);
        }
	}

	return rtgui_object_event_handler(object, event);
}
rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *event)
{
    struct rtgui_app *app;

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

    app = RTGUI_APP(object);

    switch (event->type)
    {
    case RTGUI_EVENT_PAINT:
    case RTGUI_EVENT_VPAINT_REQ:
    case RTGUI_EVENT_MOUSE_BUTTON:
    case RTGUI_EVENT_MOUSE_MOTION:
    case RTGUI_EVENT_CLIP_INFO:
    case RTGUI_EVENT_WIN_ACTIVATE:
    case RTGUI_EVENT_WIN_DEACTIVATE:
    case RTGUI_EVENT_WIN_CLOSE:
    case RTGUI_EVENT_WIN_MOVE:
    case RTGUI_EVENT_WIN_SHOW:
    case RTGUI_EVENT_WIN_HIDE:
    case RTGUI_EVENT_KBD:
    case RTGUI_EVENT_GESTURE:
        _rtgui_application_dest_handle(app, event);
        break;

    case RTGUI_EVENT_APP_ACTIVATE:
        if (app->main_object != RT_NULL)
        {
            /* Let the polymorphism work. */
            struct rtgui_event_win_show wev;

            RTGUI_EVENT_WIN_SHOW_INIT(&wev);
            wev.wid = (struct rtgui_win*)app->main_object;

            rtgui_object_handle(app->main_object, &wev.parent);
        }
        break;

    case RTGUI_EVENT_APP_DESTROY:
        rtgui_app_exit(app, 0);
        break;

    case RTGUI_EVENT_TIMER:
    {
        struct rtgui_timer *timer;
        struct rtgui_event_timer *etimer = (struct rtgui_event_timer *) event;

        timer = etimer->timer;
        timer->pending_cnt--;
        RT_ASSERT(timer->pending_cnt >= 0);
        if (timer->state == RTGUI_TIMER_ST_DESTROY_PENDING)
        {
            /* Truly destroy the timer when there is no pending event. */
            if (timer->pending_cnt == 0)
                rtgui_timer_destory(timer);
        }
        else if (timer->state == RTGUI_TIMER_ST_RUNNING && timer->timeout != RT_NULL)
        {
            /* call timeout function */
            timer->timeout(timer, timer->user_data);
        }
    }
    break;

    case RTGUI_EVENT_MV_MODEL:
    {
        struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event;
        RT_ASSERT(emodel->view);
        return rtgui_object_handle(RTGUI_OBJECT(emodel->view), event);
    }

    case RTGUI_EVENT_COMMAND:
    {
        struct rtgui_event_command *ecmd = (struct rtgui_event_command *)event;

        if (ecmd->wid != RT_NULL)
            return _rtgui_application_dest_handle(app, event);
    }
    default:
        return rtgui_object_event_handler(object, event);
    }

    return RT_TRUE;
}
Exemple #5
0
rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *event)
{
    struct rtgui_app *app;

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

    app = RTGUI_APP(object);

    switch (event->type)
    {
    case RTGUI_EVENT_PAINT:
    case RTGUI_EVENT_MOUSE_BUTTON:
    case RTGUI_EVENT_MOUSE_MOTION:
    case RTGUI_EVENT_CLIP_INFO:
    case RTGUI_EVENT_WIN_ACTIVATE:
    case RTGUI_EVENT_WIN_DEACTIVATE:
    case RTGUI_EVENT_WIN_CLOSE:
    case RTGUI_EVENT_WIN_MOVE:
    case RTGUI_EVENT_KBD:
        _rtgui_application_dest_handle(app, event);
        break;

    case RTGUI_EVENT_APP_ACTIVATE:
        if (app->main_object != RT_NULL)
        {
            rtgui_win_activate(RTGUI_WIN(app->main_object));
        }
        break;

    case RTGUI_EVENT_APP_DESTROY:
        rtgui_app_exit(app, 0);
        break;

    case RTGUI_EVENT_TIMER:
    {
        struct rtgui_timer *timer;
        struct rtgui_event_timer *etimer = (struct rtgui_event_timer *) event;

        timer = etimer->timer;
        if (timer->timeout != RT_NULL)
        {
            /* call timeout function */
            timer->timeout(timer, timer->user_data);
        }
    }
    break;

    case RTGUI_EVENT_MV_MODEL:
    {
        struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event;
        RT_ASSERT(emodel->view);
        return rtgui_object_handle(RTGUI_OBJECT(emodel->view), event);
    }

    case RTGUI_EVENT_COMMAND:
    {
        struct rtgui_event_command *ecmd = (struct rtgui_event_command *)event;

        if (ecmd->wid != RT_NULL)
            return _rtgui_application_dest_handle(app, event);
    }
    default:
    return rtgui_object_event_handler(object, event);
    }

    return RT_TRUE;
}