Allow multiple overlays

This effectively allows us to support popup dialogs and the like.

Change-Id: Iafb3fa60ed635287cb59dce118f74dc8f2a4e60a
diff --git a/gui/gui.cpp b/gui/gui.cpp
index c755a98..ebd7053 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -734,6 +734,7 @@
 
 int gui_changeOverlay(std::string overlay)
 {
+	LOGINFO("Set overlay: '%s'\n", overlay.c_str());
 	PageManager::ChangeOverlay(overlay);
 	gForceRender.set_value(1);
 	return 0;
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 23cfcd8..7107c92 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -655,7 +655,6 @@
 {
 	mResources = new ResourceManager;
 	mCurrentPage = NULL;
-	mOverlayPage = NULL;
 
 	mXmlFile = xmlFile;
 	if (xmlFile)
@@ -666,6 +665,7 @@
 
 PageSet::~PageSet()
 {
+	mOverlays.clear();
 	for (std::vector<Page*>::iterator itr = mPages.begin(); itr != mPages.end(); ++itr)
 		delete *itr;
 
@@ -931,12 +931,27 @@
 
 int PageSet::SetOverlay(Page* page)
 {
-	if (mOverlayPage)   mOverlayPage->SetPageFocus(0);
-	mOverlayPage = page;
-	if (mOverlayPage)
-	{
-		mOverlayPage->SetPageFocus(1);
-		mOverlayPage->NotifyVarChange("", "");
+	if (page) {
+		if (mOverlays.size() >= 10) {
+			LOGERR("Too many overlays requested, max is 10.\n");
+			return -1;
+		}
+		page->SetPageFocus(1);
+		page->NotifyVarChange("", "");
+
+		if (!mOverlays.empty())
+			mOverlays.back()->SetPageFocus(0);
+
+		mOverlays.push_back(page);
+	} else {
+		if (!mOverlays.empty()) {
+			mOverlays.back()->SetPageFocus(0);
+			mOverlays.pop_back();
+			if (!mOverlays.empty())
+				mOverlays.back()->SetPageFocus(1);
+			else if (mCurrentPage)
+				mCurrentPage->SetPageFocus(1); // Just in case somehow the regular page lost focus, we'll set it again
+		}
 	}
 	return 0;
 }
@@ -1055,7 +1070,14 @@
 	ret = (mCurrentPage ? mCurrentPage->Render() : -1);
 	if (ret < 0)
 		return ret;
-	ret = (mOverlayPage ? mOverlayPage->Render() : -1);
+
+	std::vector<Page*>::iterator iter;
+
+	for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
+		ret = ((*iter) ? (*iter)->Render() : -1);
+		if (ret < 0)
+			return ret;
+	}
 	return ret;
 }
 
@@ -1066,46 +1088,55 @@
 	ret = (mCurrentPage ? mCurrentPage->Update() : -1);
 	if (ret < 0 || ret > 1)
 		return ret;
-	ret = (mOverlayPage ? mOverlayPage->Update() : -1);
+
+	std::vector<Page*>::iterator iter;
+
+	for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++) {
+		ret = ((*iter) ? (*iter)->Update() : -1);
+		if (ret < 0)
+			return ret;
+	}
 	return ret;
 }
 
 int PageSet::NotifyTouch(TOUCH_STATE state, int x, int y)
 {
-	if (mOverlayPage)
-		return (mOverlayPage->NotifyTouch(state, x, y));
+	if (!mOverlays.empty())
+		return mOverlays.back()->NotifyTouch(state, x, y);
 
 	return (mCurrentPage ? mCurrentPage->NotifyTouch(state, x, y) : -1);
 }
 
 int PageSet::NotifyKey(int key, bool down)
 {
-	if (mOverlayPage)
-		return (mOverlayPage->NotifyKey(key, down));
+	if (!mOverlays.empty())
+		return mOverlays.back()->NotifyKey(key, down);
 
 	return (mCurrentPage ? mCurrentPage->NotifyKey(key, down) : -1);
 }
 
 int PageSet::NotifyKeyboard(int key)
 {
-	if (mOverlayPage)
-		return (mOverlayPage->NotifyKeyboard(key));
+	if (!mOverlays.empty())
+		return mOverlays.back()->NotifyKeyboard(key);
 
 	return (mCurrentPage ? mCurrentPage->NotifyKeyboard(key) : -1);
 }
 
 int PageSet::SetKeyBoardFocus(int inFocus)
 {
-	if (mOverlayPage)
-		return (mOverlayPage->SetKeyBoardFocus(inFocus));
+	if (!mOverlays.empty())
+		return mOverlays.back()->SetKeyBoardFocus(inFocus);
 
 	return (mCurrentPage ? mCurrentPage->SetKeyBoardFocus(inFocus) : -1);
 }
 
 int PageSet::NotifyVarChange(std::string varName, std::string value)
 {
-	if (mOverlayPage)
-		mOverlayPage->NotifyVarChange(varName, value);
+	std::vector<Page*>::iterator iter;
+
+	for (iter = mOverlays.begin(); iter != mOverlays.end(); iter++)
+		(*iter)->NotifyVarChange(varName, value);
 
 	return (mCurrentPage ? mCurrentPage->NotifyVarChange(varName, value) : -1);
 }
diff --git a/gui/pages.hpp b/gui/pages.hpp
index 50155d0..f0b2155 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -115,7 +115,7 @@
 	std::vector<Page*> mPages;
 	std::vector<xml_node<>*> templates;
 	Page* mCurrentPage;
-	Page* mOverlayPage; // This is a special case, used for "locking" the screen
+	std::vector<Page*> mOverlays; // Special case for popup dialogs and the lock screen
 	std::vector<xml_document<>*> mIncludedDocs;
 };