(svn r13117) -Codechange: make a window class of the PlayerFinancesWindow.
authorrubidium
Fri, 16 May 2008 07:34:48 +0000
changeset 10573 ccd86de2c268
parent 10572 bdc63a6797fe
child 10574 8ce36e759149
(svn r13117) -Codechange: make a window class of the PlayerFinancesWindow.
src/player_gui.cpp
src/window_gui.h
--- a/src/player_gui.cpp	Fri May 16 07:24:21 2008 +0000
+++ b/src/player_gui.cpp	Fri May 16 07:34:48 2008 +0000
@@ -48,14 +48,14 @@
 static void DoShowPlayerFinances(PlayerID player, bool show_small, bool show_stickied, int top = FIRST_GUI_CALL, int left = FIRST_GUI_CALL);
 static void DoSelectPlayerFace(Window *parent, bool show_big, int top =  FIRST_GUI_CALL, int left = FIRST_GUI_CALL);
 
-static void DrawPlayerEconomyStats(const Player *p, byte mode)
+static void DrawPlayerEconomyStats(const Player *p, bool small)
 {
 	int x, y, i, j, year;
 	const Money (*tbl)[EXPENSES_END];
 	Money sum, cost;
 	StringID str;
 
-	if (!(mode & 1)) { // normal sized economics window (mode&1) is minimized status
+	if (!small) { // normal sized economics window
 		/* draw categories */
 		DrawStringCenterUnderline(61, 15, STR_700F_EXPENDITURE_INCOME, TC_FROMSTRING);
 		for (i = 0; i != EXPENSES_END; i++)
@@ -154,73 +154,89 @@
 {   WIDGETS_END},
 };
 
-
-static void PlayerFinancesWndProc(Window *w, WindowEvent *e)
-{
-	switch (e->event) {
-		case WE_PAINT: {
-			PlayerID player = (PlayerID)w->window_number;
-			const Player *p = GetPlayer(player);
-
-			/* Recheck the size of the window as it might need to be resized due to the local player changing */
-			int new_height = ((player != _local_player) ? 0 : 12) + ((WP(w, def_d).data_1 != 0) ? 48 : 74 + 10 * EXPENSES_END);
-			if (w->height != new_height) {
-				/* Make window dirty before and after resizing */
-				w->SetDirty();
-				w->height = new_height;
-				w->SetDirty();
-
-				w->SetWidgetHiddenState(PFW_WIDGET_INCREASE_LOAN, player != _local_player);
-				w->SetWidgetHiddenState(PFW_WIDGET_REPAY_LOAN,    player != _local_player);
-			}
-
-			/* Borrow button only shows when there is any more money to loan */
-			w->SetWidgetDisabledState(PFW_WIDGET_INCREASE_LOAN, p->current_loan == _economy.max_loan);
+struct PlayerFinancesWindow : Window {
+	bool small;
 
-			/* Repay button only shows when there is any more money to repay */
-			w->SetWidgetDisabledState(PFW_WIDGET_REPAY_LOAN, player != _local_player || p->current_loan == 0);
-
-			SetDParam(0, p->index);
-			SetDParam(1, p->index);
-			SetDParam(2, LOAN_INTERVAL);
-			DrawWindowWidgets(w);
-
-			DrawPlayerEconomyStats(p, (byte)WP(w, def_d).data_1);
-		} break;
+	PlayerFinancesWindow(const WindowDesc *desc, PlayerID player, bool show_small,
+					bool show_stickied, int top, int left) :
+			Window(desc, player),
+			small(show_small)
+	{
+		this->caption_color = this->window_number;
 
-		case WE_CLICK:
-			switch (e->we.click.widget) {
-				case PFW_WIDGET_TOGGLE_SIZE: {/* toggle size */
-					byte mode = (byte)WP(w, def_d).data_1;
-					bool stickied = !!(w->flags4 & WF_STICKY);
-					int oldtop = w->top;   ///< current top position of the window before closing it
-					int oldleft = w->left; ///< current left position of the window before closing it
-					PlayerID player = (PlayerID)w->window_number;
+		if (show_stickied) this->flags4 |= WF_STICKY;
 
-					delete w;
-					/* Open up the (toggled size) Finance window at the same position as the previous */
-					DoShowPlayerFinances(player, !HasBit(mode, 0), stickied, oldtop, oldleft);
-				}
+		/* Check if repositioning from default is required */
+		if (top != FIRST_GUI_CALL && left != FIRST_GUI_CALL) {
+			this->top = top;
+			this->left = left;
+		}
+	}
+
+	virtual void OnPaint()
+	{
+		PlayerID player = (PlayerID)this->window_number;
+		const Player *p = GetPlayer(player);
+
+		/* Recheck the size of the window as it might need to be resized due to the local player changing */
+		int new_height = ((player != _local_player) ? 0 : 12) + ((this->small != 0) ? 48 : 74 + 10 * EXPENSES_END);
+		if (this->height != new_height) {
+			/* Make window dirty before and after resizing */
+			this->SetDirty();
+			this->height = new_height;
+			this->SetDirty();
+
+			this->SetWidgetHiddenState(PFW_WIDGET_INCREASE_LOAN, player != _local_player);
+			this->SetWidgetHiddenState(PFW_WIDGET_REPAY_LOAN,    player != _local_player);
+		}
+
+		/* Borrow button only shows when there is any more money to loan */
+		this->SetWidgetDisabledState(PFW_WIDGET_INCREASE_LOAN, p->current_loan == _economy.max_loan);
+
+		/* Repay button only shows when there is any more money to repay */
+		this->SetWidgetDisabledState(PFW_WIDGET_REPAY_LOAN, player != _local_player || p->current_loan == 0);
+
+		SetDParam(0, p->index);
+		SetDParam(1, p->index);
+		SetDParam(2, LOAN_INTERVAL);
+		DrawWindowWidgets(this);
+
+		DrawPlayerEconomyStats(p, this->small);
+	}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		switch (widget) {
+			case PFW_WIDGET_TOGGLE_SIZE: {/* toggle size */
+				bool new_mode = !this->small;
+				bool stickied = !!(this->flags4 & WF_STICKY);
+				int oldtop = this->top;   ///< current top position of the window before closing it
+				int oldleft = this->left; ///< current left position of the window before closing it
+				PlayerID player = (PlayerID)this->window_number;
+
+				delete this;
+				/* Open up the (toggled size) Finance window at the same position as the previous */
+				DoShowPlayerFinances(player, new_mode, stickied, oldtop, oldleft);
+			}
+			break;
+
+			case PFW_WIDGET_INCREASE_LOAN: /* increase loan */
+				DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_INCREASE_LOAN | CMD_MSG(STR_702C_CAN_T_BORROW_ANY_MORE_MONEY));
 				break;
 
-				case PFW_WIDGET_INCREASE_LOAN: /* increase loan */
-					DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_INCREASE_LOAN | CMD_MSG(STR_702C_CAN_T_BORROW_ANY_MORE_MONEY));
-					break;
-
-				case PFW_WIDGET_REPAY_LOAN: /* repay loan */
-					DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_DECREASE_LOAN | CMD_MSG(STR_702F_CAN_T_REPAY_LOAN));
-					break;
-			}
-			break;
+			case PFW_WIDGET_REPAY_LOAN: /* repay loan */
+				DoCommandP(0, 0, _ctrl_pressed, NULL, CMD_DECREASE_LOAN | CMD_MSG(STR_702F_CAN_T_REPAY_LOAN));
+				break;
+		}
 	}
-}
+};
 
 static const WindowDesc _player_finances_desc = {
 	WDP_AUTO, WDP_AUTO, 407, 86 + 10 * EXPENSES_END, 407, 86 + 10 * EXPENSES_END,
 	WC_FINANCES, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
 	_player_finances_widgets,
-	PlayerFinancesWndProc
+	NULL
 };
 
 static const WindowDesc _player_finances_small_desc = {
@@ -228,7 +244,7 @@
 	WC_FINANCES, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
 	_player_finances_small_widgets,
-	PlayerFinancesWndProc
+	NULL
 };
 
 /**
@@ -246,19 +262,8 @@
 {
 	if (!IsValidPlayer(player)) return;
 
-	Window *w = AllocateWindowDescFront<Window>(show_small ? &_player_finances_small_desc : &_player_finances_desc, player);
-	if (w != NULL) {
-		w->caption_color = w->window_number;
-		WP(w, def_d).data_1 = show_small;
-
-		if (show_stickied) w->flags4 |= WF_STICKY;
-
-		/* Check if repositioning from default is required */
-		if (top != FIRST_GUI_CALL && left != FIRST_GUI_CALL) {
-			w->top = top;
-			w->left = left;
-		}
-	}
+	if (BringWindowToFrontById(WC_FINANCES, player)) return;
+	new PlayerFinancesWindow(show_small ? &_player_finances_small_desc : &_player_finances_desc, player, show_small, show_stickied, top, left);
 }
 
 void ShowPlayerFinances(PlayerID player)
--- a/src/window_gui.h	Fri May 16 07:24:21 2008 +0000
+++ b/src/window_gui.h	Fri May 16 07:34:48 2008 +0000
@@ -515,11 +515,6 @@
 	/*** End of the event handling ***/
 };
 
-struct def_d {
-	int16 data_1, data_2, data_3;
-};
-assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(def_d));
-
 enum SortListFlags {
 	VL_NONE    = 0,      ///< no sort
 	VL_DESC    = 1 << 0, ///< sort descending or ascending