/* ui/widget.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #ifndef __INCLUDED_UI_WIDGET_H__ #define __INCLUDED_UI_WIDGET_H__ #include "SDL/SDL.h" #include #include #include "auxiliary/functions.h" #include "math/color.h" #include "math/vector2f.h" #include "ui/font.h" #include "ui/palette.h" #include "ui/definitions.h" #include "sys/sys.h" namespace ui { class Widget { public: /// type definition for child widgets typedef std::list Children; /// create a new widget Widget(Widget *parent=0); /// destroy a widget virtual ~Widget(); /* -- inspectors -------------------------------------------- */ inline math::Vector2f &location() { return widget_location; } inline math::Vector2f &size() { return widget_size; } inline float const left() const { return widget_location.x; } inline float const top() const { return widget_location.y; } inline float const width() const { return widget_size.x; } inline float const height() const { return widget_size.y; } inline std::string const &label() const { return widget_label; } inline bool border() const { return widget_border; } inline bool background() const { return widget_background; } inline Widget *parent() const { return widget_parent; } inline bool visible() const { return widget_visible; } Palette const *palette() const; Font const *font() const; bool has_focus() const; /* -- mutators --------------------------------------------- */ /// raise the widget to the top of the widget stack void raise(); /// lower the widget to the bottom of the widget stack void lower(); /// show the widget void show(); /// hide the widget void hide(); /// set visibility void set_visible(bool visible = true); /// set location of the top-left corner void set_location(float const x, float const y); /// set the widgets width and height void set_size(float const w, float const h); /// set the widgets width and height void set_size(math::Vector2f const &size); /// set the widgets width void set_width(float const w); /// set the widgets height void set_height(float const h); /// set the widgets palette void set_palette(Palette *palette); /// set the widgets font void set_font(Font *font); /// set the widgets label void set_label(std::string const &label); /// set the widgets label void set_label(char const *label); void set_border(bool border); void set_background(bool background); /// resize event virtual void event_resize(); /// draw event virtual void event_draw(); /// keyboard event virtual bool event_key(bool pressed, unsigned int key, unsigned int modifier); /// find the child widget with focus /** @param pos local position within the widget */ Widget *event_focus(math::Vector2f const & pos); /// child widgets inline Children &children() { return widget_children; } protected: /// handle keyboard events /** returns true if the event was handled by this widget */ virtual bool keypress(unsigned int key, unsigned int modifier); /// handle keyboard events /** returns true if the event was handled by this widget */ virtual bool keyrelease(unsigned int key, unsigned int modifier); /// draw the widget virtual void draw(); /// resize the widget virtual void resize(); /// draw the widget background virtual void draw_background(); /// draw the widget border virtual void draw_border(); /// list widget content size_t list(size_t indent); /// print widget description virtual void print(size_t indent); /// map local coordinates to global coordinates inline math::Vector2f to_global_coords(math::Vector2f const &local) { math::Vector2f v(local); Widget *parent = widget_parent; do { v -= parent->location(); parent = parent->widget_parent; } while(parent); return v; } /// map global coordinates to local coordinates inline math::Vector2f to_local_coords(math::Vector2f const &global) { math::Vector2f v(global); Widget *parent = this; while (parent) { v += parent->location(); parent = parent->widget_parent; } return v; } /// map local widget location to global location inline math::Vector2f global_location() const { math::Vector2f v(widget_location); Widget *parent = widget_parent; while (parent) { v += parent->location(); parent = parent->widget_parent; } return v; } /// remove a child widget void remove_child(Widget *child); private: bool widget_visible; bool widget_background; bool widget_border; math::Vector2f widget_location; math::Vector2f widget_size; std::string widget_label; Children widget_children; Palette *widget_palette; Font *widget_font; Widget *widget_parent; Children::iterator find_child(Widget *child); void add_child(Widget *child); }; } #endif // __INCLUDED_UI_WIDGET_H__