src/widget/widget_composite.cpp
branchcpp_gui
changeset 6258 a2f86b8fd99b
child 6260 740c702f6871
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/widget/widget_composite.cpp	Sun Feb 18 14:17:28 2007 +0000
@@ -0,0 +1,79 @@
+/* $Id$ */
+
+#include "../stdafx.h"
+#include <stdarg.h>
+#include "../openttd.h"
+#include "../debug.h"
+#include "../functions.h"
+#include "../map.h"
+#include "../player.h"
+#include "../window.h"
+#include "../gfx.h"
+#include "../viewport.h"
+#include "../console.h"
+#include "../variables.h"
+#include "../table/sprites.h"
+#include "../genworld.h"
+#include "../helpers.hpp"
+
+
+void CompositeWidget::AddWidget(Widget *wd)
+{
+	WidgetId id = wd->GetId();
+	std::pair<WidgetIterator, bool> pib = m_widgets.insert(Widgets::value_type(id, wd));
+	assert(pib.second);
+}
+
+Widget* CompositeWidget::GetWidget(WidgetId id)
+{
+	WidgetIterator it = m_widgets.find(id);
+	return (it != m_widgets.end()) ? (*it).second : NULL;
+}
+
+Widget* CompositeWidget::RemoveWidget(WidgetId id)
+{
+	WidgetIterator it = m_widgets.find(id);
+	if (it == m_widgets.end()) return NULL;
+	Widget *wd = (*it).second;
+	assert(wd->m_container == this);
+	wd->m_container = NULL;
+	m_widgets.erase(it);
+	return wd;
+}
+
+/*virtual*/ Widget* CompositeWidget::WidgetFromPt(const Point16 &pt_parent)
+{
+	Point16 pt_local = pt_parent - TopLeft();
+	bool inside_me = m_rect.PtInRect(pt_parent);
+	for (WidgetReverseIterator rit = m_widgets.rbegin(); rit != m_widgets.rend(); ++rit) {
+		/* get next child */
+		Widget *wd_child = (*rit).second;
+		/* ask the child recursively */
+		Widget *wd = wd_child->WidgetFromPt(pt_local);
+		if (wd != NULL) {
+			/* if the widget we found is inside me it is what we are searching for */
+			if (inside_me) return wd;
+			/* the point is outside me. The widget is clipped unless it is on-top widget */
+			if (wd->m_dont_clip) return wd;
+			/* try next child */
+		}
+	}
+	return inside_me ? this : NULL;
+}
+
+/*virtual*/ void CompositeWidget::Close()
+{
+	/* mark all children as closed in safe way */
+	for (WidgetReverseIterator rit_next = m_widgets.rbegin(); rit_next != m_widgets.rend(); ) {
+		/* save the iterator (it can be invalidated) and move forward */
+		WidgetReverseIterator rit = rit_next++;
+		/* get child */
+		Widget *wd_child = (*rit).second;
+		/* tell him we are closing */
+		wd_child->Close();
+	}
+	/* remove children */
+	m_widgets.clear();
+	/* mark self as closed */
+	super::Close();
+}