el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Revision:
12:63db16fea709
Parent:
8:a460cabc85ac
Child:
13:6714534e7974
--- a/Widgets/Widget.h	Fri Apr 22 16:12:42 2016 +0000
+++ b/Widgets/Widget.h	Sun May 08 14:42:08 2016 +0000
@@ -1,107 +1,122 @@
 #ifndef SIMPLEGUI_WIDGET_H
 #define SIMPLEGUI_WIDGET_H
 
-#include "GUI.h"
+class Widget;
+
+#include "GraphicsContext.h"
+#include "EventListener.h"
+#include "Rectangle.h"
 
 /**
 * A basic widget draws itself in a rectangular area
 **/
 
-class Widget : public EventListener {
+class Widget : public EventListener
+{
+
+public:
+
+    Widget(GraphicsContext* context);
+    
+    virtual void setParent(Widget* parent);
+    
+    /********************************************************
+    * Methods relating to containment
+    * In the base class these methods do nothing.
+    * They are implemented in container classes
+    ********************************************************/
+    virtual void attach(Widget *widget) {}
+    virtual void detach(Widget *widget) {}
+    
+    /********************************************************
+    * Common Widget methods
+    ********************************************************/
+    virtual bool isEventTarget(Event e);
     
-    public:
+    virtual void setLocation(int x, int y);
+    virtual void setSize(int width, int height);
+    virtual void setWidth(int width);
+    virtual void setHeight(int height);
+    virtual int x();
+    virtual int y();
+    virtual int height();
+    virtual int width();
+    const Rectangle &outer();
+    const Rectangle &inner();
+    
+    virtual void setForeground(uint16_t color);
+    virtual void setBackground(uint16_t color);
+    
+    void setPadding(int pixels);
+    void setBorder(int width, uint16_t colour);    
+   
+    /**
+    * Cause the widget to redraw itself if is is dirty or damaged
+    **/
+    virtual void draw();
+    virtual void clear();
+    
+    void show();
+    void hide();
+    bool isHidden();
+    
+    void setEventHandler(EventHandler *handler);
+    void unsetEventHandler(EventHandler *handler);
     
-        Widget(GUI* gui) : _gui(gui), _fg(White), _bg(Black), _x(0), _y(0), _width(0), _height(0), _hidden(false) {}
-     
-        virtual bool isEventTarget(Event e) {
-            return !_hidden && e.screenX >= _x && e.screenX <= (_x+_width) && e.screenY >= _y && e.screenY <= (_y+_height);
-        }
-       
-        virtual void setLocation(int x, int y) {
-            _x = x;
-            _y = y;
-        }
-        
-        virtual void setSize(int width, int height) {
-            _width = width;
-            _height = height;
-        }
-        
-        virtual int x() {
-            return _x;
-        }
-        
-        virtual int y() {
-            return _y;
-        }
-        
-        virtual int height() {
-            return _height;
-        }
-        
-        virtual int width() {
-            return _width;
-        }
-        
-        virtual void setForeground(uint16_t color) {
-            _fg = color;
-        }
-        
-        virtual void setBackground(uint16_t color) {
-            _bg = color;
-        }
-        
-        virtual void draw() {
-            if(!_hidden) _draw();
-        }
-        
-        virtual void clear() {
-            if(!_hidden) _clear();
-        }
-        
-        void show() {
-            _hidden = false;
-            draw();
-        }
-        
-        void hide() {
-            clear();
-            _hidden = true;
-        }
-        
-        bool isHidden() {
-            return _hidden;
-        }
+    bool intersects(Widget *widget);
+
+    /**
+    * Mark the widget as needing to be redrawn
+    **/
+    void dirty();
+    bool isDirty();
+    /**
+    * Mark the widget and all its children as dirty
+    **/
+    virtual void dirtyAll();
+    
+    /**
+    * Mark the widget as having changed dimensions or location
+    **/
+    void damage();
+    bool isDamaged();
+
+    void adjust();
+
+    /**
+    * Convenience method
+    **/
+    GraphicsDisplay *display();
+    
+protected:
 
-        void setEventHandler(uint8_t type, EventHandler handler) {
-            EventListener::setEventHandler(type, handler);
-            _gui->eventDispatcher()->detachListener(this);
-            _gui->eventDispatcher()->attachListener(this);
-        }
-        
-        int unsetEventHandler(uint8_t type) {
-            
-            int remaining = EventListener::unsetEventHandler(type);
-            
-            if(remaining == 0) {
-                _gui->eventDispatcher()->detachListener(this);
-            }
-            
-            return remaining;
-        }
+    virtual void _draw();
+    virtual void _clear();
+    
+    virtual void _dirty();
+    virtual void _damage();
+    virtual void _adjust();
+    
+    
+    Widget* _parent;
+    GraphicsContext* _context;
+    uint16_t _fg, _bg;
+    
+    Rectangle _outer;
+    Rectangle _inner;
+    
+    bool _hidden;
+    int _padding;
+    int _borderWidth;
+    uint16_t _borderColour;
 
-       
-    protected:
+    bool _adjusting;
 
-        virtual void _draw() = 0;
-        virtual void _clear() = 0;
-        
-    
-        GUI* _gui;
-        uint16_t _fg, _bg;
-        int _x,_y,_width,_height;
-        bool _hidden;
-        
+    bool _dirtied;
+    bool _dirtying;
+
+    bool _damaged;
+    bool _damaging;
 };
 
 #endif
\ No newline at end of file