summaryrefslogtreecommitdiff
path: root/components/gui/src/title.c
blob: e3ba27ffbef746d202cc5cae65e97f97a0169b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * File      : title.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/rtgui_system.h>
//#include <rtgui/rtgui_theme.h>

#include <rtgui/widgets/window.h>
#include <rtgui/widgets/title.h>

//#include "../server/mouse.h"

/* there is no event handler in wintitle but handle the event on topwin of server */
static void _rtgui_wintitle_constructor(rtgui_wintitle_t *wintitle)
{
    RTGUI_WIDGET(wintitle)->flag = RTGUI_WIDGET_FLAG_DEFAULT;
    RTGUI_WIDGET_TEXTALIGN(wintitle) = RTGUI_ALIGN_CENTER_VERTICAL;

    rtgui_object_set_event_handler(RTGUI_OBJECT(wintitle),
                                   rtgui_wintile_event_handler);
}

static void _rtgui_wintitle_deconstructor(rtgui_wintitle_t *wintitle)
{
}

DEFINE_CLASS_TYPE(wintitle, "wintitle",
                  RTGUI_PARENT_TYPE(widget),
                  _rtgui_wintitle_constructor,
                  _rtgui_wintitle_deconstructor,
                  sizeof(struct rtgui_wintitle));

rtgui_wintitle_t *rtgui_wintitle_create(struct rtgui_win *window)
{
    rtgui_wintitle_t *wintitle;

    wintitle = (rtgui_wintitle_t *)rtgui_widget_create(RTGUI_WINTITLE_TYPE);
    if (wintitle != RT_NULL)
    {
        RTGUI_WIDGET(wintitle)->toplevel = window;
    }

    return wintitle;
}

void rtgui_wintitle_destroy(rtgui_wintitle_t *wintitle)
{
    rtgui_widget_destroy(RTGUI_WIDGET(wintitle));
}

rt_bool_t rtgui_wintile_event_handler(struct rtgui_object *obj, rtgui_event_t *eve)
{
    struct rtgui_wintitle *wint;
    struct rtgui_win *win;

    RT_ASSERT(obj);
    RT_ASSERT(eve);

    wint = RTGUI_WINTITLE(obj);
    win = RTGUI_WIDGET(wint)->toplevel;
    RT_ASSERT(win);

    switch (eve->type)
    {
    case RTGUI_EVENT_PAINT:
        rtgui_theme_draw_win(wint);
        return RT_FALSE;

    case RTGUI_EVENT_MOUSE_BUTTON: {
        struct rtgui_event_mouse *emou = (struct rtgui_event_mouse *)eve;

        if (win->style & RTGUI_WIN_STYLE_CLOSEBOX)
        {
            rtgui_rect_t rect;

            /* get close button rect (device value) */
            rect.x1 = RTGUI_WIDGET(wint)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
            rect.y1 = RTGUI_WIDGET(wint)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
            rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
            rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;

            if (emou->button & RTGUI_MOUSE_BUTTON_LEFT)
            {
                if (emou->button & RTGUI_MOUSE_BUTTON_DOWN)
                {
                    if (rtgui_rect_contains_point(&rect, emou->x, emou->y) == RT_EOK)
                    {
                        win->flag |= RTGUI_WIN_FLAG_CB_PRESSED;
                        rtgui_theme_draw_win(wint);
                    }
#ifdef RTGUI_USING_WINMOVE
                    else
                    {
                        rtgui_winrect_set(win);
                    }
#endif
                }
                else if (emou->button & RTGUI_MOUSE_BUTTON_UP)
                {
                    if (win->flag & RTGUI_WIN_FLAG_CB_PRESSED &&
                        rtgui_rect_contains_point(&rect,
                                                  emou->x, emou->y) == RT_EOK)
                    {
                        rtgui_win_close(win);
                        return RT_TRUE;
                    }

                    win->flag &= ~RTGUI_WIN_FLAG_CB_PRESSED;
                    rtgui_theme_draw_win(wint);
#ifdef RTGUI_USING_WINMOVE
                    /* Reset the window movement state machine. */
                    rtgui_winrect_moved_done(RT_NULL, RT_NULL);
#endif
                }
            }
        }
        else if (emou->button & RTGUI_MOUSE_BUTTON_DOWN)
        {
#ifdef RTGUI_USING_WINMOVE
            rtgui_winrect_set(win);
#endif
        }
    }
        return RT_TRUE;
    default:
        return rtgui_widget_event_handler(obj, eve);
    }
}