]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/widget.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / widget.cpp
index c146d235c80602e061a02dd8d7a6ef832f507102..b7dcfc5e0d1ed6d104dde077195fe5f7c79b4870 100644 (file)
@@ -1,14 +1,6 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007-2011  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/gl/immediate.h>
 #include <msp/gl/matrix.h>
-#include <msp/gl/transform.h>
-#include <msp/strings/formatter.h>
+#include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include "container.h"
 #include "resources.h"
 #include "root.h"
@@ -19,48 +11,109 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Widget::Widget():
-       style(0),
-       state(NORMAL),
-       visible(true),
-       focusable(true),
-       parent(0)
-{ }
-
 Widget::~Widget()
 {
        if(parent)
-               parent->remove(*this);
+       {
+               Container *p = parent;
+               parent = nullptr;
+               p->remove(*this);
+       }
 }
 
 void Widget::set_position(int x, int y)
 {
-       geom.x = x;
-       geom.y = y;
-       on_geometry_change();
+       set_geometry(Geometry(x, y, geom.w, geom.h));
 }
 
 void Widget::set_size(unsigned w, unsigned h)
 {
-       geom.w = w;
-       geom.h = h;
-       on_geometry_change();
+       set_geometry(Geometry(geom.x, geom.y, w, h));
+}
+
+void Widget::autosize()
+{
+       Geometry ageom;
+       autosize(ageom);
+       set_geometry(ageom);
+}
+
+void Widget::autosize(Geometry &ageom) const
+{
+       if(!style)
+               return;
+
+       ageom = Geometry(geom.x, geom.y, 0, 0);
+       for(const Part &p: style->get_parts())
+       {
+               if(p.get_name().empty())
+               {
+                       const Geometry &pgeom = p.get_geometry();
+                       const Sides &pmargin = p.get_margin();
+                       ageom.w = max(ageom.w, pgeom.w+pmargin.left+pmargin.right);
+                       ageom.h = max(ageom.h, pgeom.h+pmargin.top+pmargin.bottom);
+               }
+               else
+                       autosize_special(p, ageom);
+       }
 }
 
 void Widget::set_geometry(const Geometry &g)
 {
+       bool size_changed = (g.w!=geom.w || g.h!=geom.h);
        geom = g;
-       on_geometry_change();
+       if(size_changed)
+       {
+               on_size_change();
+               mark_rebuild();
+       }
+}
+
+void Widget::map_coords_to_ancestor(int &x, int &y, const Widget &ancestor) const
+{
+       for(const Widget *w=this; w; w=w->get_parent())
+       {
+               if(w==&ancestor)
+                       return;
+
+               const Geometry &wgeom = w->get_geometry();
+               x += wgeom.x;
+               y += wgeom.y;
+       }
+
+       throw hierarchy_error("not an ancestor");
+}
+
+void Widget::map_coords_to_root(int &x, int &y) const
+{
+       for(const Widget *w=this; w; w=w->get_parent())
+       {
+               const Geometry &wgeom = w->get_geometry();
+               x += wgeom.x;
+               y += wgeom.y;
+       }
 }
 
 void Widget::set_parent(Container *p)
 {
        if(parent && p)
-               throw InvalidState("Widget is already in a Container");
-       parent = p;
+               throw hierarchy_error("widget already parented");
+       else if(p==parent)
+               return;
 
-       on_reparent();
-       update_style();
+       try
+       {
+               parent = p;
+
+               on_reparent();
+               update_style();
+       }
+       catch(...)
+       {
+               // The container has not yet added the widget as its child
+               parent = nullptr;
+               throw;
+       }
 }
 
 void Widget::set_style(const string &s)
@@ -75,20 +128,18 @@ void Widget::update_style()
        for(top=this; top->parent; top=top->parent) ;
        Root *root = dynamic_cast<Root *>(top);
        if(!root)
-               style = 0;
+               style = nullptr;
        else
        {
                string sname = get_class();
-               if(!style_name.empty())
-               {
-                       sname += '-';
-                       sname += style_name;
-               }
+               append(sname, "-", style_name);
 
-               style = root->get_resources().get<Style>(sname);
+               style = &root->get_resources().get<Style>(sname);
        }
 
        on_style_change();
+       signal_autosize_changed.emit();
+       mark_rebuild();
 }
 
 void Widget::set_tooltip(const string &t)
@@ -106,65 +157,146 @@ void Widget::set_visible(bool v)
        signal_visibility_changed.emit(visible);
 }
 
-void Widget::set_focusable(bool f)
-{
-       focusable = f;
-}
-
 void Widget::set_focus()
 {
        if(!parent)
-               throw InvalidState("No parent");
+               throw hierarchy_error("no parent");
        if(!visible)
-               throw InvalidState("Can't set focus on invisible widget");
+               throw logic_error("!visible");
 
        signal_request_focus.emit();
 }
 
-void Widget::render() const
+void Widget::set_enabled(bool e)
+{
+       set_state(DISABLED, (e ? NORMAL : DISABLED));
+}
+
+void Widget::set_state(State mask, State bits)
+{
+       State old_state = state;
+       state = (state&~mask)|bits;
+       if(style && style->compare_states(old_state, state))
+               mark_rebuild();
+}
+
+void Widget::set_animation_interval(const Time::TimeDelta &iv)
+{
+       if(iv<Time::zero)
+               throw invalid_argument("Widget::set_animation_interval");
+
+       anim_interval = iv;
+       signal_request_animation.emit(anim_interval);
+}
+
+void Widget::stop_animation()
+{
+       set_animation_interval(Time::zero);
+}
+
+void Widget::mark_rebuild()
+{
+       if(rebuild_needed)
+               return;
+
+       rebuild_needed = true;
+       signal_rebuild_needed.emit();
+}
+
+void Widget::rebuild_hierarchy()
+{
+       if(rebuild_needed)
+       {
+               rebuild_needed = false;
+               rebuild();
+       }
+}
+
+void Widget::rebuild()
 {
        if(!style)
-               throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
+               return;
 
-       GL::push_matrix();
-       GL::translate(geom.x, geom.y, 0);
-       const Style::PartSeq &parts = style->get_parts();
-       for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+       PartCache::Rebuild rebuild_cache(part_cache);
+       for(const Part &p: style->get_parts())
        {
-               if(i->get_name().empty())
+               if(p.get_name().empty())
+                       p.build(geom, state, part_cache);
+               else
+                       rebuild_special(p);
+       }
+}
+
+void Widget::rebuild_special(const Part &part)
+{
+       part_cache.insert_special(part);
+}
+
+void Widget::render(GL::Renderer &renderer) const
+{
+       if(!style)
+               throw logic_error(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name));
+
+       static const GL::Tag texture_tag("ui_tex");
+
+       GL::Renderer::Push _push(renderer);
+       int x = 0;
+       int y = 0;
+       map_coords_to_root(x, y);
+       renderer.set_matrix(GL::Matrix::translation(x, y, 0));
+       for(const CachedPart &p: part_cache.get_parts())
+       {
+               if(p.mesh && p.texture)
                {
-                       GL::PushMatrix push_;
-                       i->render(geom, state);
+                       renderer.set_texture(texture_tag, p.texture, &style->get_sampler());
+                       p.mesh->draw(renderer);
                }
-               else
-                       render_special(*i);
+               else if(p.part)
+                       render_special(*p.part, renderer);
        }
-       GL::pop_matrix();
 }
 
 void Widget::pointer_enter()
 {
-       state |= HOVER;
+       set_state(HOVER);
 }
 
 void Widget::pointer_leave()
 {
-       state &= ~HOVER;
+       clear_state(HOVER);
+}
+
+void Widget::touch_press(int x, int y, unsigned finger)
+{
+       if(finger==0)
+               button_press(x, y, 1);
+}
+
+void Widget::touch_release(int x, int y, unsigned finger)
+{
+       if(finger==0)
+               button_release(x, y, 1);
+}
+
+void Widget::touch_motion(int x, int y, unsigned finger)
+{
+       if(finger==0)
+               pointer_motion(x, y);
 }
 
 void Widget::focus_in()
 {
-       state |= FOCUS;
+       set_state(FOCUS);
 }
 
 void Widget::focus_out()
 {
-       state &= ~FOCUS;
+       clear_state(FOCUS);
 }
 
 
 Widget::Loader::Loader(Widget &w):
-       wdg(w)
+       DataFile::ObjectLoader<Widget>(w)
 {
        add("position", &Loader::position);
        add("size",     &Loader::size);
@@ -174,17 +306,17 @@ Widget::Loader::Loader(Widget &w):
 
 void Widget::Loader::position(int x, int y)
 {
-       wdg.set_position(x, y);
+       obj.set_position(x, y);
 }
 
 void Widget::Loader::size(unsigned w, unsigned h)
 {
-       wdg.set_size(w, h);
+       obj.set_size(w, h);
 }
 
 void Widget::Loader::style(const string &s)
 {
-       wdg.set_style(s);
+       obj.set_style(s);
 }
 
 } // namespace GLtk