/* ui/widget.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include "auxiliary/functions.h" #include "sys/sys.h" #include "ui/paint.h" #include "ui/ui.h" #include "ui/widget.h" namespace ui { Widget::Widget(Widget *parent) { widget_focus = false; widget_visible = true; widget_border = true; widget_background = false; widget_palette = 0; widget_font = 0; widget_label.assign("widget"); if (!parent) { widget_parent = root(); } else { widget_parent = parent; } if (widget_parent) widget_parent->add_child(this); } Widget::~Widget() { for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { delete(*it); (*it) = 0; } widget_children.clear(); } size_t Widget::list(const size_t indent) const { print(indent); size_t n = 1; for (Children::const_iterator it = widget_children.begin(); it != widget_children.end(); it++) { n += (*it)->list(indent+1); } return n; } void Widget::print(const size_t indent) const { if (indent) { std::string marker; if (widget_focus) marker.assign("^B* ^N"); else marker.assign(" "); con_print << aux::pad_left(marker, indent*2) << label() << std::endl; } } const Palette *Widget::palette() const { if (widget_palette) { return widget_palette; } else { return parent()->palette(); } } const Font *Widget::font() const { if (widget_font) { return widget_font; } else { return parent()->font(); } } void Widget::lower() { if (!parent()) return; Children::iterator it = parent()->find_child(this); if (it != parent()->children().end()) { parent()->children().erase(it); parent()->children().push_front(this); } } void Widget::raise() { if (!parent()) return; Children::iterator it = parent()->find_child(this); if (it != parent()->children().end()) { parent()->children().erase(it); parent()->children().push_back(this); } } void Widget::set_focus() { if (!parent()) { widget_focus = true; return; } for (Children::iterator it = parent()->children().begin(); it != parent()->children().end(); it++) { (*it)->widget_focus = false; } widget_focus = true; } void Widget::show() { widget_visible = true; } void Widget::hide() { widget_visible = false; if (parent() && focus()) { Widget::Children::reverse_iterator it = parent()->children().rbegin(); while (it != parent()->children().rend()) { Widget *w = (*it); if (w != this && w->visible()) { widget_focus = false; w->widget_focus = true; it = parent()->children().rend(); } else { it++; } } } } void Widget::set_visible(bool visible) { if (visible) show(); else hide(); } void Widget::set_border(bool border) { widget_border = border; } void Widget::set_background(bool background) { widget_background = background; } void Widget::set_label(std::string const & label) { set_label(label.c_str()); } void Widget::set_label(char const *label) { widget_label.assign(label); aux::to_label(widget_label); } void Widget::set_palette(const Palette *palette) { widget_palette = palette; } void Widget::set_font(const Font *font) { widget_font = font; } void Widget::set_location(float const x, float const y) { widget_location.assign(x, y); } void Widget::set_location(const math::Vector2f &location) { widget_location.assign(location); } void Widget::set_size(float const w, float const h) { widget_size.assign(w, h); } void Widget::set_size(math::Vector2f const &size) { widget_size.assign(size); } void Widget::set_width(float const w) { widget_size.x = w; } void Widget::set_height(float const h) { widget_size.y = h; } Widget::Children::iterator Widget::find_child(Widget *child) { Children::iterator it; for (it = widget_children.begin(); it != widget_children.end(); it++) { if ((*it) == child) return it; } return it; } void Widget::add_child(Widget *child) { Children::iterator it = find_child(child); if (it == widget_children.end()) { widget_children.push_back(child); } } void Widget::remove_child(Widget *child) { Children::iterator it = find_child(child); if (it != widget_children.end()) { Widget *w = (*it); w->widget_parent = 0; delete(w); widget_children.erase(it); } } Widget *Widget::find_input_focus() { if (!visible() || !widget_focus) return 0; for (Children::const_reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { Widget *w = (*rit); if (w->visible() && w->widget_focus) { Widget *f = w->find_input_focus(); if (f) return f; } } // no child with input focus return this; } Widget *Widget::find_mouse_focus(const math::Vector2f & pos) { // this widget is not visible if (!visible() || !size().contains(pos)) return 0; // reverse-iterate children for (Children::const_reverse_iterator rit = widget_children.rbegin(); rit != widget_children.rend(); ++rit) { Widget *w = (*rit); if (w->visible()) { Widget *f = w->find_mouse_focus(pos - w->location()); if (f) return f; } } // no child with mouse focus return this; } bool Widget::has_mouse_focus() const { return (root()->mouse_focus() == this); } bool Widget::has_input_focus() const { return (root()->input_focus() == this); } /* -- event distributors ------------------------------------------- */ bool Widget::event_key(const bool pressed, const int key, const unsigned int modifier) { bool handled = false; if (pressed) { handled = on_keypress(key, modifier); } else { handled = on_keyrelease(key, modifier); } if (!handled && parent()) handled = parent()->event_key(pressed, key, modifier); return handled; } bool Widget::event_mouse(const math::Vector2f &cursor) { math::Vector2f local_cursor = to_local_coords(cursor); bool handled = false; if (root()->mouse_focus() != this) { on_mouseover(local_cursor); } on_mousemove(local_cursor); return handled; } void Widget::event_draw() { if (!visible()) return; draw(); for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { if ((*it)->visible()) (*it)->event_draw(); } } void Widget::event_resize() { resize(); for (Children::iterator it = widget_children.begin(); it != widget_children.end(); it++) { (*it)->event_resize(); } } /* -- event handlers ----------------------------------------------- */ void Widget::on_mouseover(const math::Vector2f &cursor) { return; } void Widget::on_mousemove(const math::Vector2f &cursor) { return; } bool Widget::on_keypress(const int key, const unsigned int modifier) { return false; } bool Widget::on_keyrelease(const int key, const unsigned int modifier) { return false; } /* -- draw functions ----------------------------------------------- */ void Widget::resize() { } void Widget::draw() { draw_background(); draw_border(); } void Widget::draw_background() { if (!widget_background) return; paint::color(palette()->background()); paint::rectangle(global_location(), size()); } void Widget::draw_border() { if (!widget_border) return; paint::color(palette()->border()); paint::border(global_location(), size()); } }