2009-11-24 07:34:58 +08:00
|
|
|
/*
|
|
|
|
* File : button.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
|
|
|
|
*/
|
|
|
|
#include <rtgui/dc.h>
|
|
|
|
#include <rtgui/rtgui_theme.h>
|
2011-06-29 17:57:28 +08:00
|
|
|
#include <rtgui/widgets/button.h>
|
2012-04-18 23:06:12 +08:00
|
|
|
#include <rtgui/widgets/window.h>
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-09-07 09:50:13 +08:00
|
|
|
static rt_bool_t rtgui_button_onunfocus(struct rtgui_object* object, rtgui_event_t* event);
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
static void _rtgui_button_constructor(rtgui_button_t *button)
|
|
|
|
{
|
|
|
|
/* init widget and set event handler */
|
|
|
|
RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
|
2012-04-18 23:06:12 +08:00
|
|
|
rtgui_object_set_event_handler(RTGUI_OBJECT(button), rtgui_button_event_handler);
|
2012-09-07 09:50:13 +08:00
|
|
|
rtgui_widget_set_onunfocus(RTGUI_WIDGET(button), rtgui_button_onunfocus);
|
2009-11-24 07:34:58 +08:00
|
|
|
/* un-press button */
|
2010-05-08 23:50:45 +08:00
|
|
|
button->flag = 0;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* set flag and on_button event handler */
|
|
|
|
button->pressed_image = RT_NULL;
|
|
|
|
button->unpressed_image = RT_NULL;
|
|
|
|
button->on_button = RT_NULL;
|
|
|
|
|
|
|
|
/* set gc */
|
2012-08-13 14:30:05 +08:00
|
|
|
RTGUI_WIDGET_FOREGROUND(button) = default_foreground;
|
|
|
|
RTGUI_WIDGET_BACKGROUND(button) = RTGUI_RGB(212, 208, 200);
|
|
|
|
RTGUI_WIDGET_TEXTALIGN(button) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _rtgui_button_destructor(rtgui_button_t *button)
|
|
|
|
{
|
|
|
|
if (button->pressed_image != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_image_destroy(button->pressed_image);
|
|
|
|
button->pressed_image = RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button->unpressed_image != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_image_destroy(button->unpressed_image);
|
|
|
|
button->unpressed_image = RT_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-13 18:53:51 +08:00
|
|
|
DEFINE_CLASS_TYPE(button, "button",
|
|
|
|
RTGUI_LABEL_TYPE,
|
|
|
|
_rtgui_button_constructor,
|
|
|
|
_rtgui_button_destructor,
|
|
|
|
sizeof(struct rtgui_button));
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
rt_bool_t rtgui_button_event_handler(struct rtgui_object* object, struct rtgui_event* event)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_widget *widget;
|
|
|
|
struct rtgui_button *btn;
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-07-20 20:37:56 +08:00
|
|
|
RT_ASSERT(object != RT_NULL);
|
2012-04-18 23:06:12 +08:00
|
|
|
RT_ASSERT(event != RT_NULL);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
widget = RTGUI_WIDGET(object);
|
|
|
|
btn = RTGUI_BUTTON(widget);
|
2009-11-24 07:34:58 +08:00
|
|
|
switch (event->type)
|
|
|
|
{
|
|
|
|
case RTGUI_EVENT_PAINT:
|
2011-06-05 23:19:19 +08:00
|
|
|
rtgui_theme_draw_button(btn);
|
2009-11-24 07:34:58 +08:00
|
|
|
break;
|
|
|
|
|
2010-01-18 07:43:20 +08:00
|
|
|
case RTGUI_EVENT_KBD:
|
|
|
|
{
|
|
|
|
struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*) event;
|
|
|
|
|
2010-09-10 07:07:22 +08:00
|
|
|
if (RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
|
2010-01-18 07:43:20 +08:00
|
|
|
if ((ekbd->key == RTGUIK_RETURN) || (ekbd->key == RTGUIK_SPACE))
|
|
|
|
{
|
|
|
|
if (RTGUI_KBD_IS_DOWN(ekbd))
|
|
|
|
{
|
|
|
|
btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw button */
|
|
|
|
rtgui_theme_draw_button(btn);
|
|
|
|
|
|
|
|
if ((btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
|
|
|
|
{
|
|
|
|
/* call on button handler */
|
2012-04-20 14:47:38 +08:00
|
|
|
btn->on_button(RTGUI_OBJECT(widget), event);
|
2010-01-18 07:43:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
case RTGUI_EVENT_MOUSE_BUTTON:
|
2010-09-10 07:07:22 +08:00
|
|
|
if (RTGUI_WIDGET_IS_HIDE(widget)) return RT_FALSE;
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
|
|
|
|
|
2011-06-29 17:57:28 +08:00
|
|
|
/* it's not this widget event, clean status */
|
|
|
|
if (rtgui_rect_contains_point(&(RTGUI_WIDGET(btn)->extent),
|
|
|
|
emouse->x, emouse->y) != RT_EOK)
|
|
|
|
{
|
|
|
|
btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
|
|
|
|
/* draw button */
|
|
|
|
rtgui_theme_draw_button(btn);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
|
|
|
|
{
|
|
|
|
/* it's a push button */
|
|
|
|
if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
|
|
|
|
{
|
|
|
|
if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
|
2010-05-08 23:50:45 +08:00
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
|
2010-05-08 23:50:45 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
else
|
2010-05-08 23:50:45 +08:00
|
|
|
{
|
2009-11-24 07:34:58 +08:00
|
|
|
btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
|
2010-05-08 23:50:45 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
/* draw button */
|
2011-06-29 17:57:28 +08:00
|
|
|
rtgui_theme_draw_button(btn);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-01-18 07:43:20 +08:00
|
|
|
if (btn->on_button != RT_NULL)
|
|
|
|
{
|
|
|
|
/* call on button handler */
|
2012-04-20 14:47:38 +08:00
|
|
|
btn->on_button(RTGUI_OBJECT(widget), event);
|
2010-01-18 07:43:20 +08:00
|
|
|
}
|
|
|
|
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2009-11-24 07:34:58 +08:00
|
|
|
/* invokes call back */
|
|
|
|
if (widget->on_mouseclick != RT_NULL &&
|
|
|
|
emouse->button & RTGUI_MOUSE_BUTTON_UP)
|
2012-04-20 14:47:38 +08:00
|
|
|
return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
|
|
|
|
{
|
2011-06-29 17:57:28 +08:00
|
|
|
/* set the last mouse event handled widget */
|
2012-04-18 23:06:12 +08:00
|
|
|
struct rtgui_win* win;
|
2011-06-29 17:57:28 +08:00
|
|
|
|
2012-04-18 23:06:12 +08:00
|
|
|
win = RTGUI_WIN(RTGUI_WIDGET(btn)->toplevel);
|
|
|
|
win->last_mevent_widget = RTGUI_WIDGET(btn);
|
2011-06-29 17:57:28 +08:00
|
|
|
|
2009-11-24 07:34:58 +08:00
|
|
|
/* it's a normal button */
|
|
|
|
if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
|
|
|
|
{
|
|
|
|
btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* draw button */
|
2011-06-29 17:57:28 +08:00
|
|
|
rtgui_theme_draw_button(btn);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-01-04 08:05:01 +08:00
|
|
|
#ifndef RTGUI_USING_SMALL_SIZE
|
2009-11-24 07:34:58 +08:00
|
|
|
/* invokes call back */
|
|
|
|
if (widget->on_mouseclick != RT_NULL &&
|
|
|
|
emouse->button & RTGUI_MOUSE_BUTTON_UP)
|
2012-04-20 14:47:38 +08:00
|
|
|
return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
|
2010-01-04 08:05:01 +08:00
|
|
|
#endif
|
2010-01-26 07:38:47 +08:00
|
|
|
|
|
|
|
if (!(btn->flag & RTGUI_BUTTON_FLAG_PRESS) && (btn->on_button != RT_NULL))
|
|
|
|
{
|
|
|
|
/* call on button handler */
|
2012-04-20 14:47:38 +08:00
|
|
|
btn->on_button(RTGUI_OBJECT(widget), event);
|
2010-01-26 07:38:47 +08:00
|
|
|
}
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|
2012-07-20 20:37:56 +08:00
|
|
|
default:
|
|
|
|
return rtgui_widget_event_handler(object, event);
|
2009-11-24 07:34:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_event_handler);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-11-23 20:22:48 +08:00
|
|
|
rtgui_button_t* rtgui_button_create(const char* text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
struct rtgui_button* btn;
|
|
|
|
|
|
|
|
btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE);
|
|
|
|
if (btn != RT_NULL)
|
|
|
|
{
|
|
|
|
rtgui_rect_t rect;
|
|
|
|
|
|
|
|
/* set default rect */
|
|
|
|
rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
|
|
|
|
rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
|
|
|
|
rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
|
|
|
|
rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
|
|
|
|
rtgui_label_set_text(RTGUI_LABEL(btn), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return btn;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_create);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-11-23 20:22:48 +08:00
|
|
|
rtgui_button_t* rtgui_pushbutton_create(const char* text)
|
2009-11-24 07:34:58 +08:00
|
|
|
{
|
|
|
|
rtgui_button_t* btn;
|
|
|
|
|
|
|
|
btn = rtgui_button_create(text);
|
|
|
|
if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
|
|
|
|
|
|
|
|
return btn;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_pushbutton_create);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
void rtgui_button_destroy(rtgui_button_t* btn)
|
|
|
|
{
|
|
|
|
rtgui_widget_destroy(RTGUI_WIDGET(btn));
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_destroy);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image)
|
|
|
|
{
|
|
|
|
RT_ASSERT(btn != RT_NULL);
|
|
|
|
|
|
|
|
btn->pressed_image = image;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_set_pressed_image);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
|
|
|
void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image)
|
|
|
|
{
|
|
|
|
RT_ASSERT(btn != RT_NULL);
|
|
|
|
|
|
|
|
btn->unpressed_image = image;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_set_unpressed_image);
|
2009-11-24 07:34:58 +08:00
|
|
|
|
2010-01-18 07:43:20 +08:00
|
|
|
void rtgui_button_set_onbutton(rtgui_button_t* btn, rtgui_onbutton_func_t func)
|
|
|
|
{
|
|
|
|
RT_ASSERT(btn != RT_NULL);
|
|
|
|
|
|
|
|
btn->on_button = func;
|
|
|
|
}
|
2012-08-27 15:57:19 +08:00
|
|
|
RTM_EXPORT(rtgui_button_set_onbutton);
|
2010-01-18 07:43:20 +08:00
|
|
|
|
2012-09-07 09:50:13 +08:00
|
|
|
static rt_bool_t rtgui_button_onunfocus(struct rtgui_object* object, rtgui_event_t* event)
|
|
|
|
{
|
|
|
|
rtgui_rect_t rect;
|
|
|
|
rtgui_widget_t *widget;
|
|
|
|
struct rtgui_dc *dc;
|
|
|
|
|
|
|
|
RT_ASSERT(object);
|
|
|
|
widget = RTGUI_WIDGET(object);
|
|
|
|
|
|
|
|
dc = rtgui_dc_begin_drawing(widget);
|
|
|
|
if(dc == RT_NULL) return RT_FALSE;
|
|
|
|
|
|
|
|
rtgui_widget_get_rect(widget, &rect);
|
|
|
|
|
|
|
|
if(!RTGUI_WIDGET_IS_FOCUSED(widget))
|
|
|
|
{
|
|
|
|
/* only clear focus rect */
|
|
|
|
rtgui_color_t color;
|
|
|
|
rtgui_rect_inflate(&rect, -2);
|
|
|
|
color = RTGUI_DC_FC(dc);
|
|
|
|
RTGUI_DC_FC(dc) = RTGUI_DC_BC(dc);
|
|
|
|
rtgui_dc_draw_focus_rect(dc, &rect);
|
|
|
|
RTGUI_DC_FC(dc) = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtgui_dc_end_drawing(dc);
|
|
|
|
return RT_TRUE;
|
|
|
|
}
|