+
+void WindowManager::remove_window(Window *win)
+{
+ std::list<Window*>::iterator it;
+ it = std::find(windows.begin(), windows.end(), win);
+
+ if(it != windows.end()) {
+ windows.erase(it);
+ }
+}
+
+void WindowManager::set_focused_window(Window *win)
+{
+ if(win && win == focused_win) {
+ return;
+ }
+
+ if(focused_win) {
+ // invalidate the frame (if any)
+ Window *parent = focused_win->get_parent();
+ if(parent && parent != root_win) {
+ parent->invalidate();
+ }
+ }
+
+ if(!win) {
+ focused_win = 0;
+ return;
+ }
+
+ if(win->get_focusable()) {
+ focused_win = win;
+ return;
+ }
+
+ Window **children = win->get_children();
+ for(int i=0; i<win->get_children_count(); i++) {
+ if(children[0]->get_focusable()) {
+ set_focused_window(children[0]);
+ return;
+ }
+ }
+
+ focused_win = 0;
+}
+
+const Window *WindowManager::get_focused_window() const
+{
+ return focused_win;
+}
+
+Window *WindowManager::get_focused_window()
+{
+ return focused_win;
+}
+
+Window *WindowManager::get_window_at_pos(int pointer_x, int pointer_y)
+{
+ std::list<Window*>::reverse_iterator rit = windows.rbegin();
+ while(rit != windows.rend()) {
+ Window *w = *rit++;
+ Window *parent = w->get_parent();
+
+ if(parent == root_win && w->contains_point(pointer_x, pointer_y)) {
+ return w;
+ }
+ }
+
+ return 0;
+}
+
+Window *WindowManager::get_grab_window() const
+{
+ return grab_win;
+}
+
+void WindowManager::grab_mouse(Window *win)
+{
+ grab_win = win;
+}
+
+void WindowManager::release_mouse()
+{
+ grab_win = 0;
+}
+
+static void display(Window *win)
+{
+ fill_rect(win->get_absolute_rect(), 255, 211, 5);
+}
+
+static int prev_x, prev_y;
+
+static void mouse(Window *win, int bn, bool pressed, int x, int y)
+{
+ if(bn == 0) {
+ if(pressed) {
+ wm->grab_mouse(win);
+ prev_x = x;
+ prev_y = y;
+ }
+ else {
+ wm->release_mouse();
+ }
+ }
+}
+
+static void motion(Window *win, int x, int y)
+{
+ int left_bn = get_button(0);
+ if(left_bn) {
+ int dx = x - prev_x;
+ int dy = y - prev_y;
+ prev_x = x - dx;
+ prev_y = y - dy;
+
+ Rect rect = win->get_rect();
+ win->move(rect.x + dx, rect.y + dy);
+ }
+}