1 | /*
|
---|
2 | * Copyright (c) 2012 Petr Koupy
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup gui
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef GUI_WIDGET_H_
|
---|
37 | #define GUI_WIDGET_H_
|
---|
38 |
|
---|
39 | #include <adt/list.h>
|
---|
40 | #include <io/window.h>
|
---|
41 |
|
---|
42 | struct window;
|
---|
43 | typedef struct window window_t;
|
---|
44 |
|
---|
45 | struct widget;
|
---|
46 | typedef struct widget widget_t;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Base class for all widgets. This structure should be first data member of
|
---|
50 | * any derived widget structure.
|
---|
51 | */
|
---|
52 | struct widget {
|
---|
53 | link_t link;
|
---|
54 | widget_t *parent; /**< Parent widget of this widget. NULL for root widget. */
|
---|
55 | list_t children; /**< Children widgets of this widget. */
|
---|
56 | window_t *window; /**< Window into which this widget belongs. */
|
---|
57 | const void *data; /**< Custom client data. */
|
---|
58 |
|
---|
59 | sysarg_t hpos; /**< Horizontal position in window coordinates. */
|
---|
60 | sysarg_t vpos; /**< Vertical position in window coordinates. */
|
---|
61 | sysarg_t width;
|
---|
62 | sysarg_t height;
|
---|
63 |
|
---|
64 | sysarg_t width_min;
|
---|
65 | sysarg_t height_min;
|
---|
66 | sysarg_t width_ideal; /**< Width size hint for initialization. */
|
---|
67 | sysarg_t height_ideal; /**< Height size hint for initialization, */
|
---|
68 | sysarg_t width_max;
|
---|
69 | sysarg_t height_max;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Virtual destructor. Apart from deallocating the resources specific for
|
---|
73 | * the particular widget, each widget shall remove itself from parents
|
---|
74 | * children and deallocate itself.
|
---|
75 | */
|
---|
76 | void (*destroy)(widget_t *);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Reserved for bottom-top traversal when widget changes its properties and
|
---|
80 | * want to inform its ancestors in widget hierarchy to consider rearranging
|
---|
81 | * their children. As a reaction to this call, each widget shall fetch
|
---|
82 | * information from its children and decide whether its own properties have
|
---|
83 | * to be changed. If not, widget shall calculate new layout for its children
|
---|
84 | * and call rearrange() on each of them. Otherwise, widget shall change its
|
---|
85 | * own properties and call reconfigure() on its parent.
|
---|
86 | */
|
---|
87 | void (*reconfigure)(widget_t *);
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Reserved for top-bottom traversal when widget decides to change layout
|
---|
91 | * of its childer. As a reaction to this call, widget shall change its
|
---|
92 | * position and size according to provided arguments, paint itself,
|
---|
93 | * calculate new layout for its children and call rearrange() on each
|
---|
94 | * of them.
|
---|
95 | */
|
---|
96 | void (*rearrange)(widget_t *, sysarg_t, sysarg_t, sysarg_t, sysarg_t);
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * As a reaction to window refresh event, widget hierarchy is traversed
|
---|
100 | * in top-bottom order and repaint() is called on each widget. Widget shall
|
---|
101 | * either paint itself or copy its private buffer onto window surface.
|
---|
102 | * Widget shall also post damage event into window event loop.
|
---|
103 | */
|
---|
104 | void (*repaint)(widget_t *);
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Keyboard events are delivered to widgets that have keyboard focus. As a
|
---|
108 | * reaction to the event, widget might call reconfigure() on its parent or
|
---|
109 | * rearrange() on its children. If the widget wants to change its visual
|
---|
110 | * information, refresh event should be posted to the window event loop.
|
---|
111 | */
|
---|
112 | void (*handle_keyboard_event)(widget_t *, kbd_event_t);
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Position events are delivered to those widgets that have mouse grab or
|
---|
116 | * those that intersects with cursor. As a reaction to the event, widget
|
---|
117 | * might call reconfigure() on its parent or rearrange() on its children.
|
---|
118 | * If the widget wants to change its visual information, refresh event
|
---|
119 | * should be posted to the window event loop. If the widget accepts
|
---|
120 | * keyboard events, it should take ownership of keyboard focus. Widget can
|
---|
121 | * also acquire or release mouse grab.
|
---|
122 | */
|
---|
123 | void (*handle_position_event)(widget_t *, pos_event_t);
|
---|
124 | };
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * Note on derived widget constructor/destructor:
|
---|
128 | * In order to support inheritance, wach widget shall provide init and deinit
|
---|
129 | * function. These functions take already allocated widget and are responsible
|
---|
130 | * for (de)initializing all widget-specific resources, inserting/removing the
|
---|
131 | * widget into/from widget hierarchy and (de)initializing all data and functions
|
---|
132 | * from the top-level base class defined above. For convenience, each widget
|
---|
133 | * should also provide constructor to allocate and init the widget in one step.
|
---|
134 | */
|
---|
135 |
|
---|
136 | extern void widget_init(widget_t *, widget_t *, const void *);
|
---|
137 | extern void widget_modify(widget_t *, sysarg_t, sysarg_t, sysarg_t, sysarg_t);
|
---|
138 | extern const void *widget_get_data(widget_t *);
|
---|
139 | extern void widget_deinit(widget_t *);
|
---|
140 |
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | /** @}
|
---|
144 | */
|
---|
145 |
|
---|