From: Mikko Rasa Date: Thu, 8 Aug 2013 16:02:10 +0000 (+0300) Subject: Add some utility functions for dealing with widget hierarchy X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e6a6b3fbd760e487c5df1a6d70ed0700d151710b;p=libs%2Fgltk.git Add some utility functions for dealing with widget hierarchy --- diff --git a/source/widget.cpp b/source/widget.cpp index 13c5174..a9c233b 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -75,6 +75,21 @@ void Widget::set_geometry(const Geometry &g) } } +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::set_parent(Container *p) { if(parent && p) diff --git a/source/widget.h b/source/widget.h index 5c81b80..dfef4ba 100644 --- a/source/widget.h +++ b/source/widget.h @@ -68,6 +68,8 @@ public: void set_geometry(const Geometry &); const Geometry &get_geometry() const { return geom; } + void map_coords_to_ancestor(int &, int &, const Widget &) const; + protected: /** Sets the widget's parent Container. The widget must be unparented when calling this function with a non-null parameter. */ @@ -75,6 +77,16 @@ protected: public: Container *get_parent() const { return parent; } + /** Finds the closest ancestor of a specific type. */ + template + T *find_ancestor() const + { + for(Widget *w=parent; w; w=w->get_parent()) + if(T *tw = dynamic_cast(w)) + return tw; + return 0; + } + /** Sets the widget style. The name of the resource to be looked up is constructed by concatenating the widget class and the style name with a dash. */