/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "ui/widget.h" using namespace GS; using namespace GS::S2D; //------------------------------------------------------------------------------ void Widget::SetParent(Widget *w) { if (w != parent) { if (parent) parent->children.Remove(this); parent = w; if (parent) parent->children.Add(this); } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void Widget::SetHidden(bool h) { if (h != hidden) { hidden = h; dirty = true; } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Widget *Widget::GetChild(int id) { if (GetId() == id) return this; ListForeachPtr(Widget *, w, GetChildren()) if (Widget *match = w->GetChild(id)) return match; return NULL; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void Widget::Invalidate() { dirty = true; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ iRect Widget::GetRect() const { return iRect(rect.sx, rect.sy, rect.ex, rect.ey); } void Widget::SetBorderSize(float t, float b, float l, float r) { border_size.Set(t, b, l, r); Invalidate(); } void Widget::SetHAlign(Align a) { h_align = a; Invalidate(); } void Widget::SetVAlign(Align a) { v_align = a; Invalidate(); } void Widget::SetFormattingSize(const Vector2 &s) { format = s; Invalidate(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ Vector2 Widget::GetFormat() const { switch (GetType()) { case WidgetTypeHSizer: case WidgetTypeVSizer: break; default: return format; } // Combine all child formats. Vector2 total_format(0, 0); ListForeachPtr(Widget *, w, GetChildren()) { Vector2 child_format = w->GetFormat(); switch (GetType()) { case WidgetTypeHSizer: total_format.x += child_format.x; total_format.y = total_format.y > child_format.y ? total_format.y : child_format.y; break; case WidgetTypeVSizer: total_format.x = total_format.x > child_format.x ? total_format.x : child_format.x; total_format.y += child_format.y; break; } } return total_format * format; } //------------------------------------------------------------------------------ //-------------------------------------------------------------------------------------- iRect Widget::FormatOutputRect(const iRect &rect, const iRect &clip_rect) //-------------------------------------------------------------------------------------- { iRect out_rect(clip_rect.sx, clip_rect.sy, clip_rect.sx + rect.GetWidth(), clip_rect.sy + rect.GetHeight()); switch (h_align) { case AlignLeft: break; case AlignMiddle: out_rect = out_rect.Offset((clip_rect.GetWidth() - rect.GetWidth()) / 2, 0); break; case AlignRight: out_rect = out_rect.Offset(clip_rect.GetWidth() - rect.GetWidth(), 0); break; case AlignGrow: out_rect.ex = clip_rect.ex; break; } switch (v_align) { case AlignTop: break; case AlignMiddle: out_rect = out_rect.Offset(0, (clip_rect.GetHeight() - rect.GetHeight()) / 2); break; case AlignBottom: out_rect = out_rect.Offset(0, clip_rect.GetHeight() - rect.GetHeight()); break; case AlignGrow: out_rect.ey = clip_rect.ey; break; } return out_rect; } //------------------------------------------------------------------------------ Widget::Widget(int id) { object_id = id; parent = NULL; format.Set(1, 1); SetBorderSize(); h_align = AlignMiddle; v_align = AlignMiddle; dirty = true; hidden = false; type = WidgetTypeBase; event_table = new EventTable; } Widget::~Widget() { SetParent(NULL); ListForeachPtr(Widget *, c, children) c->SetParent(NULL); } //------------------------------------------------------------------------------