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