added license GPL
[winnie] / src / window.cc
index 2a8b6f5..04f72cd 100644 (file)
@@ -1,4 +1,26 @@
+/*
+winnie - an experimental window system
+
+Copyright (C) 2013 Eleni Maria Stea
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Author: Eleni Maria Stea <elene.mst@gmail.com>
+*/
+
 #include <algorithm>
+#include <stdio.h> //TODO
 #include <string.h>
 
 #include "gfx.h"
@@ -7,6 +29,7 @@
 
 Window::Window()
 {
+       parent = 0;
        title = 0;
        rect.x = rect.y = 0;
        rect.width = rect.height = 128;
@@ -14,6 +37,7 @@ Window::Window()
        dirty = true;
        managed = true;
        focusable = true;
+       state = STATE_NORMAL;
 }
 
 Window::~Window()
@@ -31,10 +55,28 @@ const Rect &Window::get_rect() const
        return rect;
 }
 
+Rect Window::get_absolute_rect() const
+{
+       if(!parent) {
+               return rect;
+       }
+
+       Rect absolute_rect;
+       absolute_rect = parent->get_absolute_rect();
+
+       absolute_rect.x += rect.x;
+       absolute_rect.y += rect.y;
+       absolute_rect.width = rect.width;
+       absolute_rect.height = rect.height;
+
+       return absolute_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;
+       Rect abs_rect = get_absolute_rect();
+       return ptr_x >= abs_rect.x && ptr_x < abs_rect.x + abs_rect.width &&
+                       ptr_y >= abs_rect.y && ptr_y < abs_rect.y + abs_rect.height;
 }
 
 void Window::move(int x, int y)
@@ -67,36 +109,43 @@ const char *Window::get_title() const
 void Window::invalidate()
 {
        dirty = true;
-       wm->invalidate_region(rect);
+       Rect abs_rect = get_absolute_rect();
+       wm->invalidate_region(abs_rect);
 }
 
-void Window::draw(const Rect &dirty_region)
+void Window::draw(Rect *dirty_region)
 {
-       //TODO
-       //titlebar, frame
-
-       Rect intersect = rect_intersection(rect, dirty_region);
+       Rect abs_rect = get_absolute_rect();
+       Rect intersect = rect_intersection(abs_rect, *dirty_region);
        if(intersect.width && intersect.height) {
+               Rect prev_clip = get_clipping_rect();
+               set_clipping_rect(abs_rect);
+               
                if(callbacks.display) {
                        callbacks.display(this);
                }
                dirty = false;
 
-               draw_children(rect);
+               draw_children(abs_rect);
+               
+               *dirty_region = rect_union(*dirty_region, abs_rect);
+               set_clipping_rect(prev_clip);
        }
 }
 
 void Window::draw_children(const Rect &dirty_region)
 {
+       Rect drect = dirty_region;
        for(size_t i=0; i<children.size(); i++) {
-               children[i]->draw(dirty_region);
+               children[i]->draw(&drect);
        }
 }
 
 unsigned char *Window::get_win_start_on_fb()
 {
        unsigned char *fb = get_framebuffer();
-       return fb + get_color_depth() * (get_screen_size().x * rect.y + rect.x) / 8;
+       Rect abs_rect = get_absolute_rect();
+       return fb + get_color_depth() * (get_screen_size().x * abs_rect.y + abs_rect.x) / 8;
 }
 
 int Window::get_scanline_width()
@@ -124,6 +173,11 @@ bool Window::get_focusable() const
        return focusable;
 }
 
+bool Window::get_dirty() const
+{
+       return dirty;
+}
+
 void Window::set_display_callback(DisplayFuncType func)
 {
        callbacks.display = func;
@@ -205,3 +259,13 @@ Window *Window::get_parent()
 {
        return parent;
 }
+
+void Window::set_state(State state)
+{
+       this->state = state;
+}
+
+Window::State Window::get_state() const
+{
+       return state;
+}