*in progress*
[winnie] / src / window.cc
1 #include <algorithm>
2 #include <string.h>
3
4 #include "gfx.h"
5 #include "window.h"
6 #include "wm.h"
7
8 Window::Window()
9 {
10         parent = 0;
11         title = 0;
12         rect.x = rect.y = 0;
13         rect.width = rect.height = 128;
14         memset(&callbacks, 0, sizeof callbacks);
15         dirty = true;
16         managed = true;
17         focusable = true;
18 }
19
20 Window::~Window()
21 {
22         for(size_t i=0; i<children.size(); i++) {
23                 wm->remove_window(children[i]);
24                 delete children[i];
25         }
26
27         delete [] title;
28 }
29
30 const Rect &Window::get_rect() const
31 {
32         return rect;
33 }
34
35 Rect Window::get_absolute_rect() const
36 {
37         if(!parent) {
38                 return rect;
39         }
40
41         Rect absolute_rect;
42         absolute_rect = parent->get_absolute_rect();
43
44         absolute_rect.x += rect.x;
45         absolute_rect.y += rect.y;
46         absolute_rect.width = rect.width;
47         absolute_rect.height = rect.height;
48
49         return absolute_rect;
50 }
51
52 bool Window::contains_point(int ptr_x, int ptr_y)
53 {
54         Rect abs_rect = get_absolute_rect();
55         return ptr_x >= abs_rect.x && ptr_x < abs_rect.x + abs_rect.width &&
56                         ptr_y >= abs_rect.y && ptr_y < abs_rect.y + abs_rect.height;
57 }
58
59 void Window::move(int x, int y)
60 {
61         invalidate();   // moved, should redraw, MUST BE CALLED FIRST
62         rect.x = x;
63         rect.y = y;
64 }
65
66 void Window::resize(int x, int y)
67 {
68         invalidate();   // resized, should redraw, MUST BE CALLED FIRST
69         rect.width = x;
70         rect.height = y;
71 }
72
73 void Window::set_title(const char *s)
74 {
75         delete [] title;
76
77         title = new char[strlen(s) + 1];
78         strcpy(title, s);
79 }
80
81 const char *Window::get_title() const
82 {
83         return title;
84 }
85
86 void Window::invalidate()
87 {
88         dirty = true;
89         Rect abs_rect = get_absolute_rect();
90         wm->invalidate_region(abs_rect);
91 }
92
93 void Window::draw(const Rect &dirty_region)
94 {
95         Rect intersect = rect_intersection(rect, dirty_region);
96         if(intersect.width && intersect.height) {
97                 if(callbacks.display) {
98                         callbacks.display(this);
99                 }
100                 dirty = false;
101
102                 draw_children(rect);
103         }
104 }
105
106 void Window::draw_children(const Rect &dirty_region)
107 {
108         for(size_t i=0; i<children.size(); i++) {
109                 children[i]->draw(dirty_region);
110         }
111 }
112
113 unsigned char *Window::get_win_start_on_fb()
114 {
115         unsigned char *fb = get_framebuffer();
116         Rect abs_rect = get_absolute_rect();
117         return fb + get_color_depth() * (get_screen_size().x * abs_rect.y + abs_rect.x) / 8;
118 }
119
120 int Window::get_scanline_width()
121 {
122         return get_screen_size().x;
123 }
124
125 void Window::set_managed(bool managed)
126 {
127         this->managed = managed;
128 }
129
130 bool Window::get_managed() const
131 {
132         return managed;
133 }
134
135 void Window::set_focusable(bool focusable)
136 {
137         this->focusable = focusable;
138 }
139
140 bool Window::get_focusable() const
141 {
142         return focusable;
143 }
144
145 void Window::set_display_callback(DisplayFuncType func)
146 {
147         callbacks.display = func;
148 }
149
150 void Window::set_keyboard_callback(KeyboardFuncType func)
151 {
152         callbacks.keyboard = func;
153 }
154
155 void Window::set_mouse_button_callback(MouseButtonFuncType func)
156 {
157         callbacks.button = func;
158 }
159
160 void Window::set_mouse_motion_callback(MouseMotionFuncType func)
161 {
162         callbacks.motion = func;
163 }
164
165 const DisplayFuncType Window::get_display_callback() const
166 {
167         return callbacks.display;
168 }
169
170 const KeyboardFuncType Window::get_keyboard_callback() const
171 {
172         return callbacks.keyboard;
173 }
174
175 const MouseButtonFuncType Window::get_mouse_button_callback() const
176 {
177         return callbacks.button;
178 }
179
180 const MouseMotionFuncType Window::get_mouse_motion_callback() const
181 {
182         return callbacks.motion;
183 }
184
185 void Window::add_child(Window *win)
186 {
187         children.push_back(win);
188         if(win->parent) {
189                 win->parent->remove_child(win);
190         }
191         win->parent = this;
192 }
193
194 void Window::remove_child(Window *win)
195 {
196         std::vector<Window*>::iterator it;
197         it = std::find(children.begin(), children.end(), win);
198         if(it != children.end()) {
199                 children.erase(it);
200                 win->parent = 0;
201         }
202 }
203
204 Window **Window::get_children()
205 {
206         if(children.empty()) {
207                 return 0;
208         }
209         return &children[0];
210 }
211
212 int Window::get_children_count() const
213 {
214         return (int)children.size();
215 }
216
217 const Window *Window::get_parent() const
218 {
219         return parent;
220 }
221
222 Window *Window::get_parent()
223 {
224         return parent;
225 }