/*************************************************************************** * Copyright (C) 2004 by Christoph Freundl * * Christoph.Freundl@informatik.uni-erlangen.de * * * * 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 2 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, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifndef SDLWIDGETS_WIDGET_H #define SDLWIDGETS_WIDGET_H #include #include #include #include "layout.h" namespace SDLwidgets { class RootContainer; class Widget { protected: Widget* _parent; Sint16 _pos_x, _pos_y; Uint16 _width, _height; Uint16 _min_width, _min_height; Uint16 _max_width, _max_height; WidgetStretch _horizontal_stretch, _vertical_stretch; SDL_Color _color; // default values static SDL_Color _default_color; public: Widget( Widget* parent = 0 ); virtual ~Widget() { } virtual void setParent( Widget* parent ) { _parent = parent; } virtual Widget* getParent() const { return _parent; } virtual RootContainer* getRoot(); virtual void setPosition( const Sint16 pos_x, const Sint16 pos_y ) { _pos_x = pos_x; _pos_y = pos_y; } virtual void getPosition( Sint16& pos_x, Sint16& pos_y ) const { pos_x = _pos_x; pos_y = _pos_y; } virtual void getOffset( Sint16& offset_x, Sint16& offset_y ) const; virtual void setMinWidth( const Uint16 min_width ) { _min_width = min_width; } virtual Uint16 getMinWidth() const { return _min_width; } virtual void setMinHeight( const Uint16 min_height ) { _min_height = min_height; } virtual Uint16 getMinHeight() const { return _min_height; } virtual void setMaxWidth( const Uint16 max_width ) { _max_width = max_width; } virtual Uint16 getMaxWidth() const { return _max_width; } virtual void setMaxHeight( const Uint16 max_height ) { _max_height = max_height; } virtual Uint16 getMaxHeight() const { return _max_height; } virtual void setWidth( const Uint16 width ) { _width = width; if ( _parent ) _parent->layoutContent(); // doLayout(); } virtual void setHeight( const Uint16 height ) { _height = height; if ( _parent ) _parent->layoutContent(); // doLayout(); } virtual void setHorizontalStretch( WidgetStretch stretch ) { _horizontal_stretch = stretch; if ( _parent ) _parent->layoutContent(); } virtual WidgetStretch getHorizontalStretch() const { return _horizontal_stretch; } virtual void setVerticalStretch( WidgetStretch stretch ) { _vertical_stretch = stretch; if ( _parent ) _parent->layoutContent(); } virtual WidgetStretch getVerticalStretch() const { return _vertical_stretch; } virtual void setColor( const SDL_Color& color ) { _color = color; } virtual const SDL_Color& getColor() const { return _color; } virtual void draw( SDL_Surface* surface ) { } virtual void drawOffset( SDL_Surface* surface, int x_offset, int y_offset ) { } virtual void add( Widget* widget ) { } virtual void remove( Widget* widget ) { } virtual bool contains( const Widget* widget ) { return ( this == widget ); } virtual int getWidth() const { return _width; } virtual int getHeight() const { return _height; } virtual bool containsPoint( Uint16 x, Uint16 y ); virtual bool handleEvent( SDL_Event* event ) { return false; } virtual void doLayout() { if ( _parent ) _parent->doLayout(); } virtual void layoutContent() { } static void setDefaultColor( SDL_Color color ); }; class WidgetLeaf : public Widget { public: virtual void draw( SDL_Surface* surface ) { } }; class WidgetComposite : public Widget { protected: std::vector _children; LayoutManager* _layout_manager; bool _valid; public: WidgetComposite( Widget* parent = 0 ); ~WidgetComposite(); WidgetComposite( LayoutManager* layout_manager, Widget* parent = 0 ) : Widget( parent ), _layout_manager( layout_manager ) { } virtual void draw( SDL_Surface* surface ); virtual void drawOffset( SDL_Surface* surface, int x_offset, int y_offset ); virtual void add( Widget* widget ); virtual void addBefore( Widget* old_widget, Widget* new_widget ); virtual void remove( Widget* widget ); virtual void removeAll(); virtual void removeAndDeleteAll(); virtual bool contains( const Widget* widget ); virtual void setLayout( LayoutManager* layout ); virtual LayoutManager* getLayout() { return _layout_manager; } virtual void setWidth( const Uint16 width ) { Widget::setMinWidth( width ); Widget::setMaxWidth( width ); // _layout_manager->arrangeToWidth( width ); _layout_manager->arrangeAll(); // Widget::doLayout(); } virtual int getWidth() const { return _layout_manager->getWidth(); } virtual void setHeight( const Uint16 height ) { Widget::setMinHeight( height ); Widget::setMaxHeight( height ); _layout_manager->arrangeAll(); // Widget::doLayout(); } virtual int getHeight() const { return _layout_manager->getHeight(); } virtual bool handleEvent( SDL_Event* event ); virtual void doLayout() { _layout_manager->arrangeAll(); Widget::doLayout(); } virtual void layoutContent() { // _layout_manager->arrangeAll(); _valid = false; } }; class WidgetDecorator : public Widget { protected: Widget* _widget; public: WidgetDecorator( Widget* widget ) : _widget( widget ) { if ( _widget ) _widget->setParent( this ); } virtual void add( Widget* widget ) { if ( _widget ) _widget->add( widget ); } virtual void remove( Widget* widget ) { if ( _widget ) _widget->remove( widget ); } virtual bool contains( const Widget* widget ) { return ( _widget && ( widget == _widget || _widget->contains( widget ) ) ); } virtual bool handleEvent( SDL_Event* event ) { if ( _widget ) return _widget->handleEvent( event ); return false; } }; class WidgetProxy : public Widget { protected: Widget* _widget; public: WidgetProxy( Widget* widget = 0 ) : _widget( widget ) { } virtual ~WidgetProxy() { } void setWidget( Widget* widget ) { _widget = widget; } Widget* getWidget() const { return _widget; } virtual void setParent( Widget* parent ) { _widget->setParent( parent ); } virtual Widget* getParent() const { return _widget->getParent(); } virtual RootContainer* getRoot() { return _widget->getRoot(); } virtual void setPosition( const Sint16 pos_x, const Sint16 pos_y ) { _widget->setPosition( pos_x, pos_y ); } virtual void getPosition( Sint16& pos_x, Sint16& pos_y ) const { _widget->getPosition( pos_x, pos_y ); } virtual void getOffset( Sint16& offset_x, Sint16& offset_y ) const { _widget->getOffset( offset_x, offset_y ); } virtual void setMaxWidth( const Uint16 max_width ) { _widget->setMaxWidth( max_width ); } virtual Uint16 getMaxWidth() const { return _widget->getMaxWidth(); } virtual void setMaxHeight( const Uint16 max_height ) { _widget->setMaxHeight( max_height ); } virtual Uint16 getMaxHeight() const { return _widget->getMaxHeight(); } virtual void setWidth( const Uint16 width ) { _widget->setWidth( width ); } virtual void setHeight( const Uint16 height ) { _widget->setHeight( height ); } virtual void setColor( const SDL_Color& color ) { _widget->setColor( color ); } virtual const SDL_Color& getColor() const { return _widget->getColor(); } virtual void draw( SDL_Surface* surface ) { _widget->draw( surface ); } virtual void drawOffset( SDL_Surface* surface, int x_offset, int y_offset ) { _widget->drawOffset( surface, x_offset, y_offset ); } virtual void add( Widget* widget ) { _widget->add( widget ); } virtual void remove( Widget* widget ) { _widget->remove( widget ); } virtual bool contains( const Widget* widget ) { return _widget->contains( widget ); } virtual int getWidth() const { return _widget->getWidth(); } virtual int getHeight() const { return _widget->getHeight(); } virtual bool handleEvent( SDL_Event* event ) { return _widget->handleEvent( event ); } virtual void doLayout() { _widget->doLayout(); } virtual void layoutContent() { _widget->layoutContent(); } }; } #endif // SDLWIDGETS_WIDGET_H