changeset 20036:ac8c347f0113 draft

(svn r24968) -Fix [FS#5379]: company window was not updated when shared were enabled/disabled
author rubidium <rubidium@openttd.org>
date Mon, 04 Feb 2013 20:29:38 +0000
parents e59052be06e9
children 8c3ee483c03e
files src/company_gui.cpp src/economy.cpp src/settings.cpp src/table/settings.ini
diffstat 4 files changed, 46 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/company_gui.cpp
+++ b/src/company_gui.cpp
@@ -1987,6 +1987,7 @@
 	{
 		this->InitNested(desc, window_number);
 		this->owner = (Owner)this->window_number;
+		this->OnInvalidateData();
 	}
 
 	virtual void OnPaint()
@@ -2055,25 +2056,6 @@
 			}
 		}
 
-		if (!local) {
-			if (_settings_game.economy.allow_shares) { // Shares are allowed
-				/* If all shares are owned by someone (none by nobody), disable buy button */
-				this->SetWidgetDisabledState(WID_C_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
-						/* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
-						(GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
-						/* Spectators cannot do anything of course */
-						_local_company == COMPANY_SPECTATOR);
-
-				/* If the company doesn't own any shares, disable sell button */
-				this->SetWidgetDisabledState(WID_C_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
-						/* Spectators cannot do anything of course */
-						_local_company == COMPANY_SPECTATOR);
-			} else { // Shares are not allowed, disable buy/sell buttons
-				this->DisableWidget(WID_C_BUY_SHARE);
-				this->DisableWidget(WID_C_SELL_SHARE);
-			}
-		}
-
 		this->DrawWidgets();
 	}
 
@@ -2396,6 +2378,36 @@
 #endif /* ENABLE_NETWORK */
 		}
 	}
+
+
+	/**
+	 * Some data on this window has become invalid.
+	 * @param data Information about the changed data.
+	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
+	 */
+	virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
+	{
+		if (this->window_number == _local_company) return;
+
+		if (_settings_game.economy.allow_shares) { // Shares are allowed
+			const Company *c = Company::Get(this->window_number);
+
+			/* If all shares are owned by someone (none by nobody), disable buy button */
+			this->SetWidgetDisabledState(WID_C_BUY_SHARE, GetAmountOwnedBy(c, INVALID_OWNER) == 0 ||
+					/* Only 25% left to buy. If the company is human, disable buying it up.. TODO issues! */
+					(GetAmountOwnedBy(c, INVALID_OWNER) == 1 && !c->is_ai) ||
+					/* Spectators cannot do anything of course */
+					_local_company == COMPANY_SPECTATOR);
+
+			/* If the company doesn't own any shares, disable sell button */
+			this->SetWidgetDisabledState(WID_C_SELL_SHARE, (GetAmountOwnedBy(c, _local_company) == 0) ||
+					/* Spectators cannot do anything of course */
+					_local_company == COMPANY_SPECTATOR);
+		} else { // Shares are not allowed, disable buy/sell buttons
+			this->DisableWidget(WID_C_BUY_SHARE);
+			this->DisableWidget(WID_C_SELL_SHARE);
+		}
+	}
 };
 
 static const WindowDesc _company_desc(
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -1818,7 +1818,7 @@
 				break;
 			}
 		}
-		SetWindowDirty(WC_COMPANY, target_company);
+		InvalidateWindowData(WC_COMPANY, target_company);
 		CompanyAdminUpdate(c);
 	}
 	return cost;
@@ -1856,7 +1856,7 @@
 		OwnerByte *b = c->share_owners;
 		while (*b != _current_company) b++; // share owners is guaranteed to contain company
 		*b = COMPANY_SPECTATOR;
-		SetWindowDirty(WC_COMPANY, target_company);
+		InvalidateWindowData(WC_COMPANY, target_company);
 		CompanyAdminUpdate(c);
 	}
 	return CommandCost(EXPENSES_OTHER, cost);
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -1038,6 +1038,17 @@
 	return true;
 }
 
+/**
+ * Invalidate the company details window after the shares setting changed.
+ * @param p1 Unused.
+ * @return Always true.
+ */
+static bool InvalidateCompanyWindow(int32 p1)
+{
+	InvalidateWindowClassesData(WC_COMPANY);
+	return true;
+}
+
 /** Checks if any settings are set to incorrect values, and sets them to correct values in that case. */
 static void ValidateSettings()
 {
--- a/src/table/settings.ini
+++ b/src/table/settings.ini
@@ -38,6 +38,7 @@
 static bool InvalidateAISettingsWindow(int32 p1);
 static bool RedrawTownAuthority(int32 p1);
 static bool InvalidateCompanyInfrastructureWindow(int32 p1);
+static bool InvalidateCompanyWindow(int32 p1);
 static bool ZoomMinMaxChanged(int32 p1);
 
 #ifdef ENABLE_NETWORK
@@ -1227,6 +1228,7 @@
 def      = false
 str      = STR_CONFIG_SETTING_ALLOW_SHARES
 strhelp  = STR_CONFIG_SETTING_ALLOW_SHARES_HELPTEXT
+proc     = InvalidateCompanyWindow
 
 [SDT_VAR]
 base     = GameSettings