(svn r9096) [cpp_gui] -Codechange: CompositeWidget now uses std::vector for child widgets instead of std::map cpp_gui
authorKUDr
Sat, 10 Mar 2007 11:07:13 +0000
branchcpp_gui
changeset 6297 4bf29d14edba
parent 6296 3205d21b662f
child 6298 c30fe89622df
(svn r9096) [cpp_gui] -Codechange: CompositeWidget now uses std::vector for child widgets instead of std::map
src/widget/widget.h
src/widget/widget_composite.cpp
src/window.h
--- a/src/widget/widget.h	Sat Mar 10 11:05:19 2007 +0000
+++ b/src/widget/widget.h	Sat Mar 10 11:07:13 2007 +0000
@@ -5,7 +5,7 @@
 
 
 #include <list>
-#include <map>
+#include <vector>
 #include "../macros.h"
 #include "../string.h"
 #include "../order.h"
@@ -49,6 +49,10 @@
 	FF_TRANSPARENT         = (1 <<  5),
 	FF_TOGGLE_BUTTON       = (1 <<  6),
 	FF_STICKED             = (1 <<  7),
+	FF_FIXED_WIDTH         = (1 <<  8),
+	FF_FIXED_HEIGHT        = (1 <<  9),
+
+	FF_FIXED_SIZE          = FF_FIXED_WIDTH | FF_FIXED_HEIGHT,
 };
 
 DECLARE_ENUM_AS_BIT_SET(FeatureFlags);
@@ -199,8 +203,13 @@
 };
 
 struct CompositeWidget : public Widget {
+	struct Slot {
+		WidgetPtr    m_wi;
+		uint8        m_span;
+	};
+
 	typedef Widget super;
-	typedef std::map<WidgetId, WidgetPtr> Widgets;
+	typedef std::vector<Slot> Widgets;
 	typedef Widgets::iterator WidgetIterator;
 	typedef Widgets::reverse_iterator WidgetReverseIterator;
 
@@ -217,6 +226,7 @@
 	{}
 
 	void AddWidget(Widget *wd);
+	WidgetIterator FindWidget(WidgetId id);
 	Widget* GetWidget(WidgetId id);
 	Widget* RemoveWidget(WidgetId id);
 
@@ -233,7 +243,6 @@
 	/*virtual*/ void OnResize(EvtResize &ev);
 };
 
-
 }; // namespace gui
 
 #endif /* WIDGET_H */
--- a/src/widget/widget_composite.cpp	Sat Mar 10 11:05:19 2007 +0000
+++ b/src/widget/widget_composite.cpp	Sat Mar 10 11:07:13 2007 +0000
@@ -17,27 +17,41 @@
 #include "../helpers.hpp"
 #include "window_events.hpp"
 
+#include <algorithm>
+
 namespace gui {
 
 
-void CompositeWidget::AddWidget(Widget *wd)
+void CompositeWidget::AddWidget(Widget *wi)
 {
-	WidgetId id = wd->GetId();
-	std::pair<WidgetIterator, bool> pib = m_widgets.insert(Widgets::value_type(id, wd));
-	assert(pib.second);
+	Widgets::value_type new_slot = {wi, 0};
+	m_widgets.push_back(new_slot);
+}
+
+CompositeWidget::WidgetIterator CompositeWidget::FindWidget(WidgetId id)
+{
+	/* iterate through children */
+	for (WidgetIterator it = m_widgets.begin(); it != m_widgets.end(); ++it) {
+		/* get child */
+		Slot &child = (*it);
+		/* check its id */
+		if (child.m_wi == NULL || child.m_wi->m_id != id) continue;
+		return it;
+	}
+	return m_widgets.end();
 }
 
 Widget* CompositeWidget::GetWidget(WidgetId id)
 {
-	WidgetIterator it = m_widgets.find(id);
-	return (it != m_widgets.end()) ? (*it).second : NULL;
+	WidgetIterator it = FindWidget(id);
+	return it != m_widgets.end() ? (*it).m_wi : NULL;
 }
 
 Widget* CompositeWidget::RemoveWidget(WidgetId id)
 {
-	WidgetIterator it = m_widgets.find(id);
+	WidgetIterator it = FindWidget(id);
 	if (it == m_widgets.end()) return NULL;
-	Widget *wd = (*it).second;
+	Widget *wd = (*it).m_wi;
 	assert(wd->m_container == this);
 	wd->m_container = NULL;
 	m_widgets.erase(it);
@@ -50,7 +64,7 @@
 	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;
+		Widget *wd_child = (*rit).m_wi;
 		/* ask the child recursively */
 		Widget *wd = wd_child->WidgetFromPt(pt_local);
 		if (wd != NULL) {
@@ -71,7 +85,7 @@
 		/* save the iterator (it can be invalidated) and move forward */
 		WidgetReverseIterator rit = rit_next++;
 		/* get child */
-		Widget *wd_child = (*rit).second;
+		Widget *wd_child = (*rit).m_wi;
 		/* tell him we are closing */
 		wd_child->Close();
 	}
@@ -93,7 +107,7 @@
 		/* save the iterator (it can be invalidated) and move forward */
 		WidgetIterator it = it_next++;
 		/* get child */
-		Widget *wd_child = (*it).second;
+		Widget *wd_child = (*it).m_wi;
 		/* tell him we are creating window */
 		wd_child->OnCreate(ev);
 	}
@@ -111,7 +125,7 @@
 		/* save the iterator (it can be invalidated) and move forward */
 		WidgetIterator it = it_next++;
 		/* get child */
-		Widget *wd_child = (*it).second;
+		Widget *wd_child = (*it).m_wi;
 		/* tell him we are painting */
 		ClipDrawContext ctx(wd_child->Left(), wd_child->Top(), wd_child->Width(), wd_child->Height());
 		if (!ctx.IsEmpty()) wd_child->OnPaint(ev);
@@ -150,7 +164,7 @@
 		/* save the iterator (it can be invalidated) and move forward */
 		WidgetIterator it = it_next++;
 		/* get child */
-		Widget *wd_child = (*it).second;
+		Widget *wd_child = (*it).m_wi;
 		/* tell him we are resizing */
 		wd_child->OnResizeParent(evp);
 	}
--- a/src/window.h	Sat Mar 10 11:05:19 2007 +0000
+++ b/src/window.h	Sat Mar 10 11:07:13 2007 +0000
@@ -6,6 +6,7 @@
 #define WINDOW_H
 
 #include "widget/widget.h"
+#include <map>
 
 struct WindowEvent;