*work in progress*
[winnie] / src / wm.cc
1 #include <stdexcept>
2 #include "gfx.h"
3 #include "wm.h"
4 #include "window.h"
5
6 WindowManager *wm;
7 static WindowManager wminst;
8
9 WindowManager::WindowManager()
10 {
11         if(!wm) {
12                 wm = this;
13         } else {
14                 throw std::runtime_error("Trying to create a second instance of WindowManager!\n");
15         }
16
17         bg_color[0] = 210;
18         bg_color[1] = 106;
19         bg_color[2] = 106;
20 }
21
22 void WindowManager::invalidate_region(const Rect &rect)
23 {
24         dirty_rects.push_back(rect);
25 }
26
27 void WindowManager::process_windows()
28 {
29         if(dirty_rects.empty()) {
30                 return;
31         }
32
33         std::list<Rect>::iterator drit = dirty_rects.begin();
34         Rect uni = *drit++;
35         while(drit != dirty_rects.end()) {
36                 uni = rect_union(uni, *drit++);
37         }
38         dirty_rects.clear();
39
40         fill_rect(uni, bg_color[0], bg_color[1], bg_color[2]);
41         
42         std::list<Window*>::iterator it = windows.begin();
43         while(it != windows.end()) {
44                 Rect intersect = rect_intersection((*it)->rect, uni); 
45                 if(intersect.width && intersect.height) {
46                         (*it)->draw();
47                 }
48         }
49 }
50
51 void WindowManager::add_window(Window *win)
52 {
53         windows.push_back(win);
54 }