*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         title = 0;
11         rect.x = rect.y = 0;
12         rect.width = rect.height = 128;
13         memset(&callbacks, 0, sizeof callbacks);
14         dirty = true;
15         managed = true;
16 }
17
18 Window::~Window()
19 {
20         for(size_t i=0; i<children.size(); i++) {
21                 wm->remove_window(children[i]);
22                 delete children[i];
23         }
24
25         delete [] title;
26 }
27
28 const Rect &Window::get_rect() const
29 {
30         return rect;
31 }
32
33 bool Window::contains_point(int ptr_x, int ptr_y)
34 {
35         return ptr_x >= rect.x && ptr_x < rect.x + rect.width &&
36                         ptr_y >= rect.y && ptr_y < rect.y + rect.height;
37 }
38
39 void Window::move(int x, int y)
40 {
41         invalidate();   // moved, should redraw, MUST BE CALLED FIRST
42         rect.x = x;
43         rect.y = y;
44 }
45
46 void Window::resize(int x, int y)
47 {
48         invalidate();   // resized, should redraw, MUST BE CALLED FIRST
49         rect.width = x;
50         rect.height = y;
51 }
52
53 void Window::set_title(const char *s)
54 {
55         delete [] title;
56
57         title = new char[strlen(s) + 1];
58         strcpy(title, s);
59 }
60
61 const char *Window::get_title() const
62 {
63         return title;
64 }
65
66 void Window::invalidate()
67 {
68         dirty = true;
69         wm->invalidate_region(rect);
70 }
71
72 void Window::draw(const Rect &dirty_region)
73 {
74         //TODO
75         //titlebar, frame
76
77         Rect intersect = rect_intersection(rect, dirty_region);
78         if(intersect.width && intersect.height) {
79                 if(callbacks.display) {
80                         callbacks.display(this);
81                 }
82                 dirty = false;
83
84                 draw_children(rect);
85         }
86 }
87
88 void Window::draw_children(const Rect &dirty_region)
89 {
90         for(size_t i=0; i<children.size(); i++) {
91                 children[i]->draw(dirty_region);
92         }
93 }
94
95 unsigned char *Window::get_win_start_on_fb()
96 {
97         unsigned char *fb = get_framebuffer();
98         return fb + get_color_depth() * (get_screen_size().x * rect.y + rect.x) / 8;
99 }
100
101 int Window::get_scanline_width()
102 {
103         return get_screen_size().x;
104 }
105
106 void Window::set_managed(bool managed)
107 {
108         this->managed = managed;
109 }
110
111 bool Window::get_managed() const
112 {
113         return managed;
114 }
115
116 void Window::set_display_callback(DisplayFuncType func)
117 {
118         callbacks.display = func;
119 }
120
121 void Window::set_keyboard_callback(KeyboardFuncType func)
122 {
123         callbacks.keyboard = func;
124 }
125
126 void Window::set_mouse_button_callback(MouseButtonFuncType func)
127 {
128         callbacks.button = func;
129 }
130
131 void Window::set_mouse_motion_callback(MouseMotionFuncType func)
132 {
133         callbacks.motion = func;
134 }
135
136 const DisplayFuncType Window::get_display_callback() const
137 {
138         return callbacks.display;
139 }
140
141 const KeyboardFuncType Window::get_keyboard_callback() const
142 {
143         return callbacks.keyboard;
144 }
145
146 const MouseButtonFuncType Window::get_mouse_button_callback() const
147 {
148         return callbacks.button;
149 }
150
151 const MouseMotionFuncType Window::get_mouse_motion_callback() const
152 {
153         return callbacks.motion;
154 }
155
156 void Window::add_child(Window *win)
157 {
158         children.push_back(win);
159         if(win->parent) {
160                 win->parent->remove_child(win);
161         }
162         win->parent = this;
163 }
164
165 void Window::remove_child(Window *win)
166 {
167         std::vector<Window*>::iterator it;
168         it = std::find(children.begin(), children.end(), win);
169         if(it != children.end()) {
170                 children.erase(it);
171                 win->parent = 0;
172         }
173 }
174
175 const Window *Window::get_parent() const
176 {
177         return parent;
178 }
179
180 Window *Window::get_parent()
181 {
182         return parent;
183 }