changeset 12272:c8708dfd0118 draft

(svn r16687) -Codechange: Perform re-initialization of windows with nested widgets after a language change.
author alberth <alberth@openttd.org>
date Sun, 28 Jun 2009 20:09:40 +0000
parents e6a64c020cd8
children 7e7d72410611
files src/settings_gui.cpp src/window.cpp src/window_func.h src/window_gui.h
diffstat 4 files changed, 56 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -323,6 +323,7 @@
 				CheckForMissingGlyphsInLoadedLanguagePack();
 				UpdateAllStationVirtCoord();
 				UpdateAllWaypointSigns();
+				ReInitAllWindows();
 				MarkWholeScreenDirty();
 				break;
 
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -560,6 +560,48 @@
 	if (w != NULL) w->SetDirty();
 }
 
+/** Re-initialize a window.
+ * @todo Extend the function to handle viewports.
+ */
+void Window::ReInit()
+{
+	if (this->nested_root == NULL) return; // Only nested widget windows can re-initialize.
+
+	this->SetDirty(); // Mark whole current window as dirty.
+
+	/* Save current size. */
+	int window_width  = this->width;
+	int window_height = this->height;
+
+	/* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
+	this->nested_root->SetupSmallestSize();
+	this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, false, false, false);
+	this->width  = this->nested_root->smallest_x;
+	this->height = this->nested_root->smallest_y;
+	this->resize.width  = this->nested_root->smallest_x;
+	this->resize.height = this->nested_root->smallest_y;
+	this->resize.step_width  = this->nested_root->resize_x;
+	this->resize.step_height = this->nested_root->resize_y;
+
+	/* Resize as close to the original size as possible. */
+	window_width  = max(window_width,  this->width);
+	window_height = max(window_height, this->height);
+	int dx = (this->resize.step_width  == 0) ? 0 : window_width  - this->width;
+	int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
+	/* dx and dy has to go by step.. calculate it.
+	 * The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
+	if (this->resize.step_width  > 1) dx -= dx % (int)this->resize.step_width;
+	if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;
+
+	if (dx == 0 && dy == 0) return; // No resize needed.
+
+	ResizeWindow(this, dx, dy); // Sets post-resize dirty blocks.
+	Point diff;
+	diff.x = dx;
+	diff.y = dy;
+	this->OnResize(diff);
+}
+
 /** Find the Window whose parent pointer points to this window
  * @param w parent Window to find child of
  * @return a Window pointer that is the child of w, or NULL otherwise */
@@ -2488,6 +2530,16 @@
 	DeleteWindowById(WC_STATUS_BAR, 0);
 }
 
+/** Re-initialize all windows. */
+void ReInitAllWindows()
+{
+	Window *w;
+
+	FOR_ALL_WINDOWS_FROM_BACK(w) {
+		w->ReInit();
+	}
+}
+
 /**
  * (Re)position main toolbar window at the screen
  * @param w Window structure of the main toolbar window, may also be \c NULL
--- a/src/window_func.h
+++ b/src/window_func.h
@@ -32,6 +32,8 @@
 void HideVitalWindows();
 void ShowVitalWindows();
 
+void ReInitAllWindows();
+
 void InvalidateWindowWidget(WindowClass cls, WindowNumber number, byte widget_index);
 void InvalidateWindow(WindowClass cls, WindowNumber number);
 void InvalidateWindowClasses(WindowClass cls);
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -429,6 +429,7 @@
 	void DeleteChildWindows() const;
 
 	void SetDirty() const;
+	void ReInit();
 
 	/*** Event handling ***/