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