*in progress*
[winnie] / src / window.cc
index 880a6ed..2a8b6f5 100644 (file)
@@ -1,4 +1,6 @@
+#include <algorithm>
 #include <string.h>
+
 #include "gfx.h"
 #include "window.h"
 #include "wm.h"
@@ -10,10 +12,17 @@ Window::Window()
        rect.width = rect.height = 128;
        memset(&callbacks, 0, sizeof callbacks);
        dirty = true;
+       managed = true;
+       focusable = true;
 }
 
 Window::~Window()
 {
+       for(size_t i=0; i<children.size(); i++) {
+               wm->remove_window(children[i]);
+               delete children[i];
+       }
+
        delete [] title;
 }
 
@@ -22,6 +31,12 @@ const Rect &Window::get_rect() const
        return rect;
 }
 
+bool Window::contains_point(int ptr_x, int ptr_y)
+{
+       return ptr_x >= rect.x && ptr_x < rect.x + rect.width &&
+                       ptr_y >= rect.y && ptr_y < rect.y + rect.height;
+}
+
 void Window::move(int x, int y)
 {
        invalidate();   // moved, should redraw, MUST BE CALLED FIRST
@@ -55,12 +70,27 @@ void Window::invalidate()
        wm->invalidate_region(rect);
 }
 
-void Window::draw()
+void Window::draw(const Rect &dirty_region)
 {
        //TODO
        //titlebar, frame
-       callbacks.display(this);
-       dirty = false;
+
+       Rect intersect = rect_intersection(rect, dirty_region);
+       if(intersect.width && intersect.height) {
+               if(callbacks.display) {
+                       callbacks.display(this);
+               }
+               dirty = false;
+
+               draw_children(rect);
+       }
+}
+
+void Window::draw_children(const Rect &dirty_region)
+{
+       for(size_t i=0; i<children.size(); i++) {
+               children[i]->draw(dirty_region);
+       }
 }
 
 unsigned char *Window::get_win_start_on_fb()
@@ -74,6 +104,26 @@ int Window::get_scanline_width()
        return get_screen_size().x;
 }
 
+void Window::set_managed(bool managed)
+{
+       this->managed = managed;
+}
+
+bool Window::get_managed() const
+{
+       return managed;
+}
+
+void Window::set_focusable(bool focusable)
+{
+       this->focusable = focusable;
+}
+
+bool Window::get_focusable() const
+{
+       return focusable;
+}
+
 void Window::set_display_callback(DisplayFuncType func)
 {
        callbacks.display = func;
@@ -113,3 +163,45 @@ const MouseMotionFuncType Window::get_mouse_motion_callback() const
 {
        return callbacks.motion;
 }
+
+void Window::add_child(Window *win)
+{
+       children.push_back(win);
+       if(win->parent) {
+               win->parent->remove_child(win);
+       }
+       win->parent = this;
+}
+
+void Window::remove_child(Window *win)
+{
+       std::vector<Window*>::iterator it;
+       it = std::find(children.begin(), children.end(), win);
+       if(it != children.end()) {
+               children.erase(it);
+               win->parent = 0;
+       }
+}
+
+Window **Window::get_children()
+{
+       if(children.empty()) {
+               return 0;
+       }
+       return &children[0];
+}
+
+int Window::get_children_count() const
+{
+       return (int)children.size();
+}
+
+const Window *Window::get_parent() const
+{
+       return parent;
+}
+
+Window *Window::get_parent()
+{
+       return parent;
+}