Const correctness for some new functions.
[bertos.git] / gfx / win.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Very simple hierarchical windowing system.
13  *
14  * All functions in this module are to be intended as methods
15  * of the Window class.  Please see its documentation
16  * for a module-wise introduction.
17  *
18  * \see struct Window
19  */
20
21 /*#*
22  *#* $Log$
23  *#* Revision 1.2  2006/01/26 00:36:48  bernie
24  *#* Const correctness for some new functions.
25  *#*
26  *#* Revision 1.1  2006/01/23 23:14:29  bernie
27  *#* Implement simple, but impressive windowing system.
28  *#*
29  *#*/
30
31 #include "win.h"
32
33 /**
34  * Map the contents of all child-windows into the bitmap of \a w.
35  *
36  * Please note that recursively draw children into their parent
37  * effectively damages the parent buffer.
38  */
39 void win_compose(Window *w)
40 {
41         Window *child;
42
43         /*
44          * Walk over all children, in back to front order and tell them
45          * to compose into us.
46          */
47         REVERSE_FOREACH_NODE(child, &w->children)
48         {
49                 /* Recursively compose child first. */
50                 win_compose(child);
51
52                 /* Draw child into our bitmap. */
53                 if (w->bitmap)
54                         gfx_blit(w->bitmap, &child->geom, child->bitmap, 0, 0);
55         }
56 }
57
58 /**
59  * Map window \a w into \a parent.
60  *
61  * The new window becomes the topmost window.
62  *
63  * \note Opening a window twice is illegal.
64  *
65  * \see win_close()
66  */
67 void win_open(Window *w, Window *parent)
68 {
69         ASSERT(!w->parent);
70         w->parent = parent;
71         ADDHEAD(&parent->children, &w->link);
72 }
73
74 /**
75  * Detach window from its parent.
76  *
77  * Closing a window causes it to become orphan of its
78  * parent.  Its content will no longer appear in its
79  * parent after the next refresh cycle.
80  *
81  * \note Closing a window that has not been previously
82  *       opened is illegal.
83  *
84  * \see win_open()
85  */
86 void win_close(Window *w)
87 {
88         ASSERT(w->parent);
89         REMOVE(&w->link);
90         w->parent = NULL;
91 }
92
93 /**
94  * Move window to the topmost position relative to its sibling.
95  *
96  * \see win_move(), win_resize(), win_setGeometry()
97  */
98 void win_raise(Window *w)
99 {
100         ASSERT(w->parent);
101         REMOVE(&w->link);
102         ADDHEAD(&w->parent->children, &w->link);
103 }
104
105 /**
106  * Set window position and size at the same time.
107  *
108  * This function is equivalent to subsequent calls to win_move()
109  * and win_resize() using the coordinates provided by the
110  * \a new_geom rectangle.
111  *
112  * \note The xmax and ymax members of \a new_geom are non-inclusive,
113  *       as usual for the Rect interface.
114  *
115  * \see win_move()
116  * \see win_resize()
117  */
118 void win_setGeometry(Window *w, const Rect *new_geom)
119 {
120         // requires C99?
121         // memcpy(&w->geom, new_geom, sizeof(w->geom));
122         w->geom = *new_geom;
123 }
124
125 /**
126  * Move window to specified position.
127  *
128  * Move the window top-left corner to the pixel coordinates
129  * \a left and \a top, which are relative to the parent window.
130  *
131  * \note A window can also be moved outside the borders
132  *       of its parent, or at negative coordinates.
133  *
134  * \note It is allowed to move an orphan window.
135  */
136 void win_move(Window *w, coord_t left, coord_t top)
137 {
138         Rect r;
139
140         r.xmin = left;
141         r.ymin = top;
142         r.xmax = r.xmin + RECT_WIDTH(&w->geom);
143         r.ymax = r.ymin + RECT_WIDTH(&w->geom);
144
145         win_setGeometry(w, &r);
146 }
147
148 /**
149  * Resize the rectangle of a window.
150  *
151  * The window shrinks or grows to the specified size.
152  *
153  * \note Growing a window beyond the size of its
154  *       backing bitmap results in unspecified behavior.
155  *
156  * \note It is allowed to resize an orphan window.
157  */
158 void win_resize(Window *w, coord_t width, coord_t height)
159 {
160         Rect r;
161
162         r.xmin = w->geom.xmin;
163         r.ymin = w->geom.ymin;
164         r.xmax = r.xmin + width;
165         r.ymax = r.ymin + height;
166
167         win_setGeometry(w, &r);
168 }
169
170 /**
171  * Initialize a new window structure.
172  *
173  *
174  * The new window initial position is set to (0,0).
175  * The size is set to the size of the installed bitmap,
176  * or (0,0) if there's no backing store.
177  *
178  * \arg bm  The bitmap to install as backing store
179  *          for drawing into the window, or NULL if
180  *          the window is not drawable.
181  */
182 void win_create(Window *w, Bitmap *bm)
183 {
184         w->parent = NULL;
185         w->bitmap = bm;
186         w->geom.xmin = 0;
187         w->geom.ymin = 0;
188         if (bm)
189         {
190                 w->geom.xmax = bm->width;
191                 w->geom.ymax = bm->height;
192         }
193         LIST_INIT(&w->children);
194 }
195