4925ace68c2bd97534335ff3627e11b36f29cc65
[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  * \version $Id$
34  *
35  * \author Bernardo Innocenti <bernie@develer.com>
36  *
37  * \brief Very simple hierarchical windowing system.
38  *
39  * All functions in this module are to be intended as methods
40  * of the Window class.  Please see its documentation
41  * for a module-wise introduction.
42  *
43  * \see struct Window
44  */
45
46 /*#*
47  *#* $Log$
48  *#* Revision 1.2  2006/08/01 17:26:04  batt
49  *#* Update docs.
50  *#*
51  *#* Revision 1.1  2006/08/01 15:43:01  batt
52  *#* Add in board_kd current edited channel visualization.
53  *#*
54  *#* Revision 1.4  2006/07/19 12:56:26  bernie
55  *#* Convert to new Doxygen style.
56  *#*
57  *#* Revision 1.3  2006/02/10 12:25:41  bernie
58  *#* Add missing header.
59  *#*
60  *#* Revision 1.2  2006/01/26 00:36:48  bernie
61  *#* Const correctness for some new functions.
62  *#*
63  *#* Revision 1.1  2006/01/23 23:14:29  bernie
64  *#* Implement simple, but impressive windowing system.
65  *#*
66  *#*/
67
68 #include "win.h"
69 #include <mware/list.h>
70
71 /**
72  * Map the contents of all child-windows into the bitmap of \a w.
73  *
74  * \note Recursively drawing children into their parent
75  *       effectively damages the parent buffer.
76  */
77 void win_compose(Window *w)
78 {
79         Window *child;
80
81         /*
82          * Walk over all children, in back to front order and tell them
83          * to compose into us.
84          */
85         REVERSE_FOREACH_NODE(child, &w->children)
86         {
87                 /* Recursively compose child first. */
88                 win_compose(child);
89
90                 /* Draw child into our bitmap. */
91                 if (w->bitmap)
92                         gfx_blit(w->bitmap, &child->geom, child->bitmap, 0, 0);
93         }
94 }
95
96 /**
97  * Map window \a w into \a parent.
98  *
99  * The new window becomes the topmost window.
100  *
101  * \note Opening a window twice is illegal.
102  *
103  * \see win_close()
104  */
105 void win_open(Window *w, Window *parent)
106 {
107         ASSERT(!w->parent);
108         w->parent = parent;
109         ADDHEAD(&parent->children, &w->link);
110 }
111
112 /**
113  * Detach window from its parent.
114  *
115  * Closing a window causes it to become orphan of its
116  * parent.  Its content will no longer appear in its
117  * parent after the next refresh cycle.
118  *
119  * \note Closing a window that has not been previously
120  *       opened is illegal.
121  *
122  * \see win_open()
123  */
124 void win_close(Window *w)
125 {
126         ASSERT(w->parent);
127         REMOVE(&w->link);
128         w->parent = NULL;
129 }
130
131 /**
132  * Move window to the topmost position relative to its sibling.
133  *
134  * \see win_move(), win_resize(), win_setGeometry()
135  */
136 void win_raise(Window *w)
137 {
138         ASSERT(w->parent);
139         REMOVE(&w->link);
140         ADDHEAD(&w->parent->children, &w->link);
141 }
142
143 /**
144  * Set window position and size at the same time.
145  *
146  * This function is equivalent to subsequent calls to win_move()
147  * and win_resize() using the coordinates provided by the
148  * \a new_geom rectangle.
149  *
150  * \note The xmax and ymax members of \a new_geom are non-inclusive,
151  *       as usual for the Rect interface.
152  *
153  * \see win_move()
154  * \see win_resize()
155  */
156 void win_setGeometry(Window *w, const Rect *new_geom)
157 {
158         // requires C99?
159         // memcpy(&w->geom, new_geom, sizeof(w->geom));
160         w->geom = *new_geom;
161 }
162
163 /**
164  * Move window to specified position.
165  *
166  * Move the window top-left corner to the pixel coordinates
167  * \a left and \a top, which are relative to the parent window.
168  *
169  * \note A window can also be moved outside the borders
170  *       of its parent, or at negative coordinates.
171  *
172  * \note It is allowed to move an orphan window.
173  */
174 void win_move(Window *w, coord_t left, coord_t top)
175 {
176         Rect r;
177
178         r.xmin = left;
179         r.ymin = top;
180         r.xmax = r.xmin + RECT_WIDTH(&w->geom);
181         r.ymax = r.ymin + RECT_WIDTH(&w->geom);
182
183         win_setGeometry(w, &r);
184 }
185
186 /**
187  * Resize the rectangle of a window.
188  *
189  * The window shrinks or grows to the specified size.
190  *
191  * \note Growing a window beyond the size of its
192  *       backing bitmap results in unspecified behavior.
193  *
194  * \note It is allowed to resize an orphan window.
195  */
196 void win_resize(Window *w, coord_t width, coord_t height)
197 {
198         Rect r;
199
200         r.xmin = w->geom.xmin;
201         r.ymin = w->geom.ymin;
202         r.xmax = r.xmin + width;
203         r.ymax = r.ymin + height;
204
205         win_setGeometry(w, &r);
206 }
207
208 /**
209  * Initialize a new window structure.
210  *
211  * The new window initial position is set to (0,0).
212  * The size is set to the size of the installed bitmap,
213  * or (0,0) if there's no backing store.
214  *
215  * \arg bm  The bitmap to install as backing store
216  *          for drawing into the window, or NULL if
217  *          the window is not drawable.
218  */
219 void win_create(Window *w, Bitmap *bm)
220 {
221         w->parent = NULL;
222         w->bitmap = bm;
223         w->geom.xmin = 0;
224         w->geom.ymin = 0;
225         if (bm)
226         {
227                 w->geom.xmax = bm->width;
228                 w->geom.ymax = bm->height;
229         }
230         LIST_INIT(&w->children);
231 }
232