fixed grab win initialization
[winnie] / src / wm.cc
1 #include <algorithm>
2 #include <stdexcept>
3 #include <stdio.h>      // TODO
4
5 #include "gfx.h"
6 #include "mouse.h"
7 #include "mouse_cursor.h"
8 #include "text.h"
9 #include "wm.h"
10 #include "window.h"
11
12 WindowManager *wm;
13
14 static void display(Window *win);
15 static void mouse(Window *win, int bn, bool pressed, int x, int y);
16 static void motion(Window *win, int x, int y);
17
18 bool init_window_manager()
19 {
20         if(!(wm = new WindowManager)) {
21                 return false;
22         }
23
24         return true;
25 }
26
27 void destroy_window_manager()
28 {
29         delete wm;
30 }
31
32 void WindowManager::create_frame(Window *win)
33 {
34         Window *frame = new Window;
35         Window *parent = win->get_parent();
36
37         frame->set_display_callback(display);
38         frame->set_mouse_button_callback(mouse);
39         frame->set_mouse_motion_callback(motion);
40         frame->set_focusable(false);
41         frame->add_child(win);
42
43         windows.push_back(frame);
44
45         Rect win_rect = win->get_rect();
46         frame->move(win_rect.x - frame_thickness,
47                         win_rect.y - frame_thickness - titlebar_thickness);
48         frame->resize(win_rect.width + frame_thickness * 2,
49                         win_rect.height + frame_thickness * 2 + titlebar_thickness);
50
51         win->move(frame_thickness, frame_thickness + titlebar_thickness);
52         parent->add_child(frame);
53 }
54
55 void WindowManager::destroy_frame(Window *win)
56 {
57         Window *frame = win->parent;
58         if(!frame) {
59                 return;
60         }
61
62         if(grab_win == win) {
63                 release_mouse();
64         }
65
66         std::list<Window*>::iterator it;
67         it = std::find(windows.begin(), windows.end(), frame);
68         if(it != windows.end()) {
69                 root_win->add_child(win);
70                 windows.erase(it);
71                 delete frame;
72         }
73 }
74
75 WindowManager::WindowManager()
76 {
77         if(!wm) {
78                 wm = this;
79         } else {
80                 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
81         }
82
83         root_win = new Window;
84         root_win->resize(get_screen_size().width, get_screen_size().height);
85         root_win->move(0, 0);
86         root_win->set_managed(false);
87
88         grab_win = 0;
89         focused_win = 0;
90
91         bg_color[0] = 210;
92         bg_color[1] = 106;
93         bg_color[2] = 106;
94
95         frame_thickness = 8;
96         titlebar_thickness = 16;
97
98         set_focused_frame_color(0, 0, 0);
99         set_unfocused_frame_color(200, 200, 200);
100
101         mouse_cursor.set_image(mouse_cursor_width, mouse_cursor_height);
102         unsigned char *pixels = mouse_cursor.get_image();
103
104         for(int i=0; i<mouse_cursor_height; i++) {
105                 for(int j=0; j<mouse_cursor_width; j++) {
106                         int val = mouse_cursor_bw[i * mouse_cursor_width + j];
107                         *pixels++ = val;
108                         *pixels++ = val;
109                         *pixels++ = val;
110                         *pixels++ = 255;
111                 }
112         }
113 }
114
115 WindowManager::~WindowManager()
116 {
117         delete root_win;
118 }
119
120 void WindowManager::invalidate_region(const Rect &rect)
121 {
122         dirty_rects.push_back(rect);
123 }
124
125 void WindowManager::process_windows()
126 {
127         if(dirty_rects.empty()) {
128                 return;
129         }
130
131         std::list<Rect>::iterator drit = dirty_rects.begin();
132         Rect uni = *drit++;
133         while(drit != dirty_rects.end()) {
134                 uni = rect_union(uni, *drit++);
135         }
136         dirty_rects.clear();
137
138         wait_vsync();
139
140         fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
141
142         root_win->draw_children(uni);
143
144         // draw mouse cursor
145         int mouse_x, mouse_y;
146         get_pointer_pos(&mouse_x, &mouse_y);
147
148         blit_key(mouse_cursor.get_image(), mouse_cursor.get_rect(),
149                         get_framebuffer(), get_screen_size(), mouse_x, mouse_y,
150                         0, 0, 0);
151
152         Rect mouse_rect = {mouse_x, mouse_y, mouse_cursor.get_width(), mouse_cursor.get_height()};
153         invalidate_region(mouse_rect);
154
155         gfx_update();
156 }
157
158 void WindowManager::add_window(Window *win)
159 {
160         if(!win || win == root_win) {
161                 return;
162         }
163
164         root_win->add_child(win);
165
166         if(windows.empty()) {
167                 focused_win = win;
168         }
169
170         if(win->get_managed()) {
171                 create_frame(win);
172         }
173
174         windows.push_back(win);
175 }
176
177 void WindowManager::remove_window(Window *win)
178 {
179         std::list<Window*>::iterator it;
180         it = std::find(windows.begin(), windows.end(), win);
181
182         if(it != windows.end()) {
183                 windows.erase(it);
184         }
185 }
186
187 void WindowManager::set_focused_window(Window *win)
188 {
189         if(win && win == focused_win) {
190                 return;
191         }
192
193         Window *parent;
194         if(focused_win) {
195                 // invalidate the frame (if any)
196                 parent = focused_win->get_parent();
197                 if(parent && parent != root_win) {
198                         parent->invalidate();
199                         fill_rect(parent->get_absolute_rect(), frame_ucolor[0], frame_ucolor[1], frame_ucolor[2]);
200                 }
201         }
202
203         if(!win) {
204                 focused_win = 0;
205                 return;
206         }
207
208         if(win->get_focusable()) {
209                 focused_win = win;
210                 parent = focused_win->get_parent();
211                 fill_rect(parent->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
212                 return;
213         }
214
215         Window **children = win->get_children();
216         for(int i=0; i<win->get_children_count(); i++) {
217                 if(children[0]->get_focusable()) {
218                         set_focused_window(children[0]);
219                         fill_rect(win->get_absolute_rect(), frame_fcolor[0], frame_fcolor[1], frame_fcolor[2]);
220                         return;
221                 }
222         }
223
224         focused_win = 0;
225 }
226
227 const Window *WindowManager::get_focused_window() const
228 {
229         return focused_win;
230 }
231
232 Window *WindowManager::get_focused_window()
233 {
234         return focused_win;
235 }
236
237 Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
238 {
239         Window *root_win = wm->get_root_window();
240         Window **children = root_win->get_children();
241         for(int i=root_win->get_children_count() - 1; i>=0; i--) {
242                 if(children[i]->contains_point(pointer_x, pointer_y)) {
243                         return children[i];
244                 }
245         }
246
247         return 0;
248 }
249
250 Window *WindowManager::get_root_window() const
251 {
252         return root_win;
253 }
254
255 void WindowManager::set_focused_frame_color(int r, int g, int b)
256 {
257         frame_fcolor[0] = r;
258         frame_fcolor[1] = g;
259         frame_fcolor[2] = b;
260 }
261
262 void WindowManager::get_focused_frame_color(int *r, int *g, int *b) const
263 {
264         *r = frame_fcolor[0];
265         *g = frame_fcolor[1];
266         *b = frame_fcolor[2];
267 }
268
269 void WindowManager::set_unfocused_frame_color(int r, int g, int b)
270 {
271         frame_ucolor[0] = r;
272         frame_ucolor[1] = g;
273         frame_ucolor[2] = b;
274 }
275
276 void WindowManager::get_unfocused_frame_color(int *r, int *g, int *b) const
277 {
278         *r = frame_ucolor[0];
279         *g = frame_ucolor[1];
280         *b = frame_ucolor[2];
281 }
282
283 Window *WindowManager::get_grab_window() const
284 {
285         return grab_win;
286 }
287
288 void WindowManager::grab_mouse(Window *win)
289 {
290         grab_win = win;
291 }
292
293 void WindowManager::release_mouse()
294 {
295         grab_win = 0;
296 }
297
298 void WindowManager::raise_window(Window *win)
299 {
300         if(!win) {
301                 return;
302         }
303
304         Window *parent = win->get_parent();
305         if(parent != root_win) {
306                 if(parent->get_parent() == root_win) {
307                         win = parent;
308                 }
309                 else {
310                         return;
311                 }
312         }
313
314         root_win->remove_child(win);
315         root_win->add_child(win);
316 }
317
318 void WindowManager::sink_window(Window *win)
319 {
320         if(!win) {
321                 return;
322         }
323
324         std::list<Window*>::iterator it;
325         it = std::find(windows.begin(), windows.end(), win);
326         if(it != windows.end()) {
327                 windows.erase(it);
328                 windows.push_front(win);
329         }
330 }
331
332 static void display(Window *win)
333 {
334         //frame display:
335         Window *child = win->get_children()[0];
336         int r, g, b;
337         Rect abs_rect = win->get_absolute_rect();
338
339         //TODO 5 not hardcoded
340         set_text_position(abs_rect.x + 5, abs_rect.y + 15);
341         set_text_color(255, 255, 255);
342
343         if(child == wm->get_focused_window()) {
344                 wm->get_focused_frame_color(&r, &g, &b);
345                 fill_rect(abs_rect, r, g, b);
346         }
347         else {
348                 wm->get_unfocused_frame_color(&r, &g, &b);
349                 fill_rect(win->get_absolute_rect(), r, g, b);
350         }
351
352         draw_text(child->get_title());
353 }
354
355 static int prev_x, prev_y;
356
357 static void mouse(Window *win, int bn, bool pressed, int x, int y)
358 {
359         if(bn == 0) {
360                 if(pressed) {
361                         wm->grab_mouse(win);
362                         wm->raise_window(win);
363                         prev_x = x;
364                         prev_y = y;
365                 }
366                 else {
367                         wm->release_mouse();
368                 }
369         }
370 }
371
372 static void motion(Window *win, int x, int y)
373 {
374         int left_bn = get_button(0);
375
376         if(left_bn) {
377                 int dx = x - prev_x;
378                 int dy = y - prev_y;
379                 prev_x = x - dx;
380                 prev_y = y - dy;
381
382                 Rect rect = win->get_rect();
383                 win->move(rect.x + dx, rect.y + dy);
384         }
385 }