← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2969: correct splitter position checking; add a splitter test

 

------------------------------------------------------------
revno: 2969
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Fri 2012-06-29 21:05:29 +0200
message:
  correct splitter position checking; add a splitter test
added:
  dwt/test/SplitTest.cpp
modified:
  dwt/include/dwt/widgets/SplitterContainer.h
  dwt/src/widgets/Splitter.cpp
  dwt/src/widgets/SplitterContainer.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dwt/include/dwt/widgets/SplitterContainer.h'
--- dwt/include/dwt/widgets/SplitterContainer.h	2012-01-13 20:55:20 +0000
+++ dwt/include/dwt/widgets/SplitterContainer.h	2012-06-29 19:05:29 +0000
@@ -74,7 +74,9 @@
 
 	size_t ensureSplitters();
 
-	double getMaxSize(SplitterPtr splitter);
+	/** Make sure the splitter's position is within acceptable bounds and doesn't step on other
+	splitters. */
+	void checkSplitterPos(SplitterPtr splitter);
 
 	void onMove();
 

=== modified file 'dwt/src/widgets/Splitter.cpp'
--- dwt/src/widgets/Splitter.cpp	2012-01-13 20:55:20 +0000
+++ dwt/src/widgets/Splitter.cpp	2012-06-29 19:05:29 +0000
@@ -106,9 +106,10 @@
 
 	if(moving && mouseEvent.ButtonPressed == MouseEvent::LEFT) {
 		ClientCoordinate cc(mouseEvent.pos, getParent());
-		pos = (horizontal ? cc.y() : cc.x()) / getParent()->getMaxSize(this);
-		if(pos < 0.) pos = 0.;
-		if(pos > 1.) pos = 1.;
+		double mpos = horizontal ? cc.y() : cc.x();
+		double size = horizontal ? getParent()->getClientSize().y : getParent()->getClientSize().x;
+		pos = mpos / size;
+		getParent()->checkSplitterPos(this);
 		getParent()->onMove();
 	}
 

=== modified file 'dwt/src/widgets/SplitterContainer.cpp'
--- dwt/src/widgets/SplitterContainer.cpp	2012-01-13 20:55:20 +0000
+++ dwt/src/widgets/SplitterContainer.cpp	2012-06-29 19:05:29 +0000
@@ -37,7 +37,6 @@
 #include <boost/next_prior.hpp>
 #include <boost/range/distance.hpp>
 #include <boost/range/adaptor/filtered.hpp>
-#include <boost/range/algorithm/find.hpp>
 #include <boost/range/algorithm/for_each.hpp>
 
 namespace dwt {
@@ -47,7 +46,7 @@
 	bool isNotSplitter(Widget *w) { return !isSplitter(w); }
 }
 
-using boost::find;
+using boost::distance;
 using boost::for_each;
 using boost::next;
 using boost::adaptors::filtered;
@@ -92,7 +91,7 @@
 	auto splitters = getChildren<Splitter>();
 
 	auto rc = Rectangle(getClientSize());
-	util::HoldResize hr(this, boost::distance(children));
+	util::HoldResize hr(this, distance(children));
 
 	if(maximized) {
 		for_each(children, [&](Widget *w) {
@@ -117,7 +116,7 @@
 
 	auto splitter_iter = splitters.first;
 
-	auto nc = boost::distance(children | filtered(&isNotSplitter));
+	auto nc = distance(children | filtered(&isNotSplitter));
 	auto i = 0;
 	for_each(children | filtered(&isNotSplitter), [&](Widget *w) {
 		if(i++ == nc-1) {
@@ -157,25 +156,22 @@
 	return ns;
 }
 
-double SplitterContainer::getMaxSize(SplitterPtr splitter) {
-	double ret = splitter->thickness();
-
-	auto children = getChildren<Widget>();
-	auto splitters = getChildren<Splitter>();
-
-	splitters.second = find(splitters, splitter);
-	auto n = boost::distance(splitters);
-
-	bool horizontal = splitter->horizontal;
-	auto pos = 0;
-	for_each(children, [&](Widget* w) {
-		if((pos == n || pos == n + 1) && w) {
-			ret += horizontal ? w->getClientSize().y : w->getClientSize().x;
+void SplitterContainer::checkSplitterPos(SplitterPtr splitter) {
+	/* ideally we'd just use find & iter-- to get the previous splitter, but the child enumeration
+	doesn't support going backwards. */
+	SplitterPtr last = nullptr, prev = nullptr, next = nullptr;
+	for_each(getChildren<Splitter>(), [&](Splitter* w) {
+		if(w == splitter) {
+			prev = last;
+		} else if(last == splitter) {
+			next = w;
 		}
-		++pos;
+		last = w;
 	});
 
-	return ret;
+	auto& pos = splitter->pos;
+	pos = std::max(pos, prev ? prev->pos : 0.);
+	pos = std::min(pos, next ? next->pos : 1.);
 }
 
 void SplitterContainer::onMove() {

=== added file 'dwt/test/SplitTest.cpp'
--- dwt/test/SplitTest.cpp	1970-01-01 00:00:00 +0000
+++ dwt/test/SplitTest.cpp	2012-06-29 19:05:29 +0000
@@ -0,0 +1,27 @@
+#include <dwt/widgets/Window.h>
+#include <dwt/widgets/Label.h>
+#include <dwt/widgets/SplitterContainer.h>
+#include <dwt/Texts.h>
+
+namespace dwt {
+tstring Texts::get(Text text) { return _T("test"); }
+}
+
+int dwtMain(dwt::Application& app)
+{
+	auto window = new dwt::Window();
+	window->create();
+	window->onClosing([] { return ::PostQuitMessage(0), true; });
+
+	auto split = window->addChild(dwt::SplitterContainer::Seed(0.3, true));
+
+	split->addChild(dwt::Label::Seed(_T("First row")));
+	split->addChild(dwt::Label::Seed(_T("Second row")));
+	split->addChild(dwt::Label::Seed(_T("Third row")));
+
+	split->resize(dwt::Rectangle(window->getClientSize()));
+
+	app.run();
+
+	return 0;
+}