checkbox.c 4.2 KB
Newer Older
1 2 3 4 5 6
#include <rtgui/dc.h>
#include <rtgui/rtgui_theme.h>
#include <rtgui/widgets/checkbox.h>

static void _rtgui_checkbox_constructor(rtgui_checkbox_t *box)
{
7 8 9
    /* init widget and set event handler */
    RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
    rtgui_object_set_event_handler(RTGUI_OBJECT(box), rtgui_checkbox_event_handler);
10

11 12 13
    /* set status */
    box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;
    box->on_button = RT_NULL;
14

15 16
    /* set default gc */
    RTGUI_WIDGET_TEXTALIGN(box) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
17 18
}

19
DEFINE_CLASS_TYPE(checkbox, "checkbox",
20 21 22 23
                  RTGUI_LABEL_TYPE,
                  _rtgui_checkbox_constructor,
                  RT_NULL,
                  sizeof(struct rtgui_checkbox));
24

25
void rtgui_checkbox_set_onbutton(rtgui_checkbox_t *checkbox, rtgui_onbutton_func_t func)
26
{
27
    RT_ASSERT(checkbox != RT_NULL);
28

29
    checkbox->on_button = func;
30 31
}

32
rt_bool_t rtgui_checkbox_event_handler(struct rtgui_object *object, struct rtgui_event *event)
33
{
34
    struct rtgui_checkbox *box;
35

36
    RTGUI_WIDGET_EVENT_HANDLER_PREPARE
37

38
    box = RTGUI_CHECKBOX(object);
39

40 41 42
    switch (event->type)
    {
    case RTGUI_EVENT_PAINT:
43
#ifndef RTGUI_USING_SMALL_SIZE
44 45 46 47 48
        if (widget->on_draw != RT_NULL)
        {
            return widget->on_draw(RTGUI_OBJECT(widget), event);
        }
        else
49
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            rtgui_theme_draw_checkbox(box);
        break;

    case RTGUI_EVENT_MOUSE_BUTTON:
    {
        if (RTGUI_WIDGET_IS_ENABLE(widget) && !RTGUI_WIDGET_IS_HIDE(widget))
        {
            struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event;
            if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT &&
                    emouse->button & RTGUI_MOUSE_BUTTON_UP)
            {
                /* set focus */
                rtgui_widget_focus(widget);

                if (box->status_down & RTGUI_CHECKBOX_STATUS_UNCHECKED)
                {
                    /* check it */
                    box->status_down = RTGUI_CHECKBOX_STATUS_CHECKED;
                }
                else
                {
                    /* un-check it */
                    box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;
                }
            }

            /* draw checkbox */
            rtgui_theme_draw_checkbox(box);
78 79

#ifndef RTGUI_USING_SMALL_SIZE
80 81 82 83 84
            /* call user callback */
            if (widget->on_mouseclick != RT_NULL)
            {
                return widget->on_mouseclick(RTGUI_OBJECT(widget), event);
            }
85
#endif
86 87 88 89 90 91 92 93 94 95 96 97 98 99
            if (box->on_button != RT_NULL)
            {
                box->on_button(RTGUI_OBJECT(widget), event);
                return RT_TRUE;
            }
        }

        return RT_TRUE;
    }
    default:
        return rtgui_widget_event_handler(object, event);
    }

    return RT_FALSE;
100 101
}

102
struct rtgui_checkbox *rtgui_checkbox_create(const char *text, rt_bool_t checked)
103
{
104
    struct rtgui_checkbox *box;
105

106
    box = (struct rtgui_checkbox *) rtgui_widget_create(RTGUI_CHECKBOX_TYPE);
107 108
    if (box != RT_NULL)
    {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        rtgui_rect_t rect;

        /* set default rect */
        rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
        rect.x2 += RTGUI_BORDER_DEFAULT_WIDTH + 5 + (RTGUI_BORDER_DEFAULT_WIDTH << 1);
        rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);

        rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
        rtgui_label_set_text(RTGUI_LABEL(box), text);

        if (checked == RT_TRUE)
            box->status_down = RTGUI_CHECKBOX_STATUS_CHECKED;
        else
            box->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;
    }

    return box;
126 127
}

128
void rtgui_checkbox_destroy(rtgui_checkbox_t *box)
129
{
130
    rtgui_widget_destroy(RTGUI_WIDGET(box));
131 132
}

133
void rtgui_checkbox_set_checked(rtgui_checkbox_t *checkbox, rt_bool_t checked)
134
{
135 136 137 138 139 140
    RT_ASSERT(checkbox != RT_NULL);
    if (checked == RT_TRUE)
        checkbox->status_down = RTGUI_CHECKBOX_STATUS_CHECKED;
    else
        checkbox->status_down = RTGUI_CHECKBOX_STATUS_UNCHECKED;

141 142
}

143
rt_bool_t rtgui_checkbox_get_checked(rtgui_checkbox_t *checkbox)
144
{
145
    RT_ASSERT(checkbox != RT_NULL);
146

147 148
    if (checkbox->status_down == RTGUI_CHECKBOX_STATUS_CHECKED)
        return RT_TRUE;
149

150
    return RT_FALSE;
151 152
}