Update preset.
[bertos.git] / bertos / gfx / win.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  *
34  * \author Bernie Innocenti <bernie@codewiz.org>
35  *
36  * \brief Very simple hierarchical windowing system.
37  *
38  * All functions in this module are to be intended as methods
39  * of the Window class.  Please see its documentation
40  * for a module-wise introduction.
41  *
42  * \see struct Window
43  */
44
45 /*#*
46  *#* $Log$
47  *#* Revision 1.2  2006/08/01 17:26:04  batt
48  *#* Update docs.
49  *#*
50  *#* Revision 1.1  2006/08/01 15:43:01  batt
51  *#* Add in board_kd current edited channel visualization.
52  *#*
53  *#* Revision 1.4  2006/07/19 12:56:26  bernie
54  *#* Convert to new Doxygen style.
55  *#*
56  *#* Revision 1.3  2006/02/10 12:25:41  bernie
57  *#* Add missing header.
58  *#*
59  *#* Revision 1.2  2006/01/26 00:36:48  bernie
60  *#* Const correctness for some new functions.
61  *#*
62  *#* Revision 1.1  2006/01/23 23:14:29  bernie
63  *#* Implement simple, but impressive windowing system.
64  *#*
65  *#*/
66
67 #include "win.h"
68 #include <struct/list.h>
69
70 /**
71  * Map the contents of all child-windows into the bitmap of \a w.
72  *
73  * \note Recursively drawing children into their parent
74  *       effectively damages the parent buffer.
75  */
76 void win_compose(Window *w)
77 {
78         Window *child;
79
80         /*
81          * Walk over all children, in back to front order and tell them
82          * to compose into us.
83          */
84         REVERSE_FOREACH_NODE(child, &w->children)
85         {
86                 /* Recursively compose child first. */
87                 win_compose(child);
88
89                 /* Draw child into our bitmap. */
90                 if (w->bitmap)
91                         gfx_blit(w->bitmap, &child->geom, child->bitmap, 0, 0);
92         }
93 }
94
95 /**
96  * Map window \a w into \a parent.
97  *
98  * The new window becomes the topmost window.
99  *
100  * \note Opening a window twice is illegal.
101  *
102  * \see win_close()
103  */
104 void win_open(Window *w, Window *parent)
105 {
106         ASSERT(!w->parent);
107         w->parent = parent;
108         ADDHEAD(&parent->children, &w->link);
109 }
110
111 /**
112  * Detach window from its parent.
113  *
114  * Closing a window causes it to become orphan of its
115  * parent.  Its content will no longer appear in its
116  * parent after the next refresh cycle.
117  *
118  * \note Closing a window that has not been previously
119  *       opened is illegal.
120  *
121  * \see win_open()
122  */
123 void win_close(Window *w)
124 {
125         ASSERT(w->parent);
126         REMOVE(&w->link);
127         w->parent = NULL;
128 }
129
130 /**
131  * Move window to the topmost position relative to its sibling.
132  *
133  * \see win_move(), win_resize(), win_setGeometry()
134  */
135 void win_raise(Window *w)
136 {
137         ASSERT(w->parent);
138         REMOVE(&w->link);
139         ADDHEAD(&w->parent->children, &w->link);
140 }
141
142 /**
143  * Set window position and size at the same time.
144  *
145  * This function is equivalent to subsequent calls to win_move()
146  * and win_resize() using the coordinates provided by the
147  * \a new_geom rectangle.
148  *
149  * \note The xmax and ymax members of \a new_geom are non-inclusive,
150  *       as usual for the Rect interface.
151  *
152  * \see win_move()
153  * \see win_resize()
154  */
155 void win_setGeometry(Window *w, const Rect *new_geom)
156 {
157         // requires C99?
158         // memcpy(&w->geom, new_geom, sizeof(w->geom));
159         w->geom = *new_geom;
160 }
161
162 /**
163  * Move window to specified position.
164  *
165  * Move the window top-left corner to the pixel coordinates
166  * \a left and \a top, which are relative to the parent window.
167  *
168  * \note A window can also be moved outside the borders
169  *       of its parent, or at negative coordinates.
170  *
171  * \note It is allowed to move an orphan window.
172  */
173 void win_move(Window *w, coord_t left, coord_t top)
174 {
175         Rect r;
176
177         r.xmin = left;
178         r.ymin = top;
179         r.xmax = r.xmin + RECT_WIDTH(&w->geom);
180         r.ymax = r.ymin + RECT_WIDTH(&w->geom);
181
182         win_setGeometry(w, &r);
183 }
184
185 /**
186  * Resize the rectangle of a window.
187  *
188  * The window shrinks or grows to the specified size.
189  *
190  * \note Growing a window beyond the size of its
191  *       backing bitmap results in unspecified behavior.
192  *
193  * \note It is allowed to resize an orphan window.
194  */
195 void win_resize(Window *w, coord_t width, coord_t height)
196 {
197         Rect r;
198
199         r.xmin = w->geom.xmin;
200         r.ymin = w->geom.ymin;
201         r.xmax = r.xmin + width;
202         r.ymax = r.ymin + height;
203
204         win_setGeometry(w, &r);
205 }
206
207 /**
208  * Initialize a new window structure.
209  *
210  * The new window initial position is set to (0,0).
211  * The size is set to the size of the installed bitmap,
212  * or (0,0) if there's no backing store.
213  *
214  * \arg bm  The bitmap to install as backing store
215  *          for drawing into the window, or NULL if
216  *          the window is not drawable.
217  */
218 void win_create(Window *w, Bitmap *bm)
219 {
220         w->parent = NULL;
221         w->bitmap = bm;
222         w->geom.xmin = 0;
223         w->geom.ymin = 0;
224         if (bm)
225         {
226                 w->geom.xmax = bm->width;
227                 w->geom.ymax = bm->height;
228         }
229         LIST_INIT(&w->children);
230 }
231