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