4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file newgrf_gui.cpp GUI to change NewGRF settings. */
14 #include "settings_gui.h"
16 #include "strings_func.h"
17 #include "window_func.h"
19 #include "settings_type.h"
20 #include "settings_func.h"
21 #include "widgets/dropdown_type.h"
22 #include "widgets/dropdown_func.h"
23 #include "network/network.h"
24 #include "network/network_content.h"
25 #include "sortlist_type.h"
26 #include "stringfilter_type.h"
27 #include "querystring_gui.h"
28 #include "core/geometry_func.hpp"
29 #include "newgrf_text.h"
30 #include "textfile_gui.h"
31 #include "tilehighlight_func.h"
34 #include "widgets/newgrf_widget.h"
35 #include "widgets/misc_widget.h"
37 #include "table/sprites.h"
39 /* Maximum number of NewGRFs that may be loaded. Six reserved slots are:
40 * 0 - config, 1 - sound, 2 - base, 3 - logos, 4 - climate, 5 - extra */
41 static const int MAX_NEWGRFS
= MAX_FILE_SLOTS
- 6;
44 * Show the first NewGRF error we can find.
46 void ShowNewGRFError()
48 /* Do not show errors when entering the main screen */
49 if (_game_mode
== GM_MENU
) return;
51 for (const GRFConfig
*c
= _grfconfig
; c
!= NULL
; c
= c
->next
) {
52 /* We only want to show fatal errors */
53 if (c
->error
== NULL
|| c
->error
->severity
!= STR_NEWGRF_ERROR_MSG_FATAL
) continue;
55 SetDParam (0, c
->error
->custom_message
== NULL
? c
->error
->message
: STR_JUST_RAW_STRING
);
56 SetDParamStr(1, c
->error
->custom_message
);
57 SetDParamStr(2, c
->filename
);
58 SetDParamStr(3, c
->error
->data
);
59 for (uint i
= 0; i
< lengthof(c
->error
->param_value
); i
++) {
60 SetDParam(4 + i
, c
->error
->param_value
[i
]);
62 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP
, INVALID_STRING_ID
, WL_CRITICAL
);
67 static void ShowNewGRFInfo(const GRFConfig
*c
, uint x
, uint y
, uint right
, uint bottom
, bool show_params
)
69 if (c
->error
!= NULL
) {
71 SetDParamStr(0, c
->error
->custom_message
); // is skipped by built-in messages
72 SetDParamStr(1, c
->filename
);
73 SetDParamStr(2, c
->error
->data
);
74 for (uint i
= 0; i
< lengthof(c
->error
->param_value
); i
++) {
75 SetDParam(3 + i
, c
->error
->param_value
[i
]);
77 GetString(message
, c
->error
->custom_message
== NULL
? c
->error
->message
: STR_JUST_RAW_STRING
, lastof(message
));
79 SetDParamStr(0, message
);
80 y
= DrawStringMultiLine(x
, right
, y
, bottom
, c
->error
->severity
);
83 /* Draw filename or not if it is not known (GRF sent over internet) */
84 if (c
->filename
!= NULL
) {
85 SetDParamStr(0, c
->filename
);
86 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_FILENAME
);
89 /* Prepare and draw GRF ID */
91 snprintf(buff
, lengthof(buff
), "%08X", BSWAP32(c
->ident
.grfid
));
92 SetDParamStr(0, buff
);
93 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_GRF_ID
);
95 if ((_settings_client
.gui
.newgrf_developer_tools
|| _settings_client
.gui
.newgrf_show_old_versions
) && c
->version
!= 0) {
96 SetDParam(0, c
->version
);
97 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_VERSION
);
99 if ((_settings_client
.gui
.newgrf_developer_tools
|| _settings_client
.gui
.newgrf_show_old_versions
) && c
->min_loadable_version
!= 0) {
100 SetDParam(0, c
->min_loadable_version
);
101 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_MIN_VERSION
);
104 /* Prepare and draw MD5 sum */
105 md5sumToString(buff
, lastof(buff
), c
->ident
.md5sum
);
106 SetDParamStr(0, buff
);
107 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_MD5SUM
);
109 /* Show GRF parameter list */
111 if (c
->num_params
> 0) {
112 GRFBuildParamList(buff
, c
, lastof(buff
));
113 SetDParam(0, STR_JUST_RAW_STRING
);
114 SetDParamStr(1, buff
);
116 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE
);
118 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_PARAMETER
);
120 /* Draw the palette of the NewGRF */
121 if (c
->palette
& GRFP_BLT_32BPP
) {
122 SetDParamStr(0, (c
->palette
& GRFP_USE_WINDOWS
) ? "Legacy (W) / 32 bpp" : "Default (D) / 32 bpp");
124 SetDParamStr(0, (c
->palette
& GRFP_USE_WINDOWS
) ? "Legacy (W)" : "Default (D)");
126 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_PALETTE
);
130 if (c
->status
== GCS_NOT_FOUND
) y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_NOT_FOUND
);
131 if (c
->status
== GCS_DISABLED
) y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_DISABLED
);
132 if (HasBit(c
->flags
, GCF_INVALID
)) y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_INCOMPATIBLE
);
133 if (HasBit(c
->flags
, GCF_COMPATIBLE
)) y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_COMPATIBLE_LOADED
);
135 /* Draw GRF info if it exists */
136 if (!StrEmpty(c
->GetDescription())) {
137 SetDParam(0, STR_JUST_RAW_STRING
);
138 SetDParamStr(1, c
->GetDescription());
139 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_BLACK_STRING
);
141 y
= DrawStringMultiLine(x
, right
, y
, bottom
, STR_NEWGRF_SETTINGS_NO_INFO
);
146 * Window for setting the parameters of a NewGRF.
148 struct NewGRFParametersWindow
: public Window
{
149 static GRFParameterInfo dummy_parameter_info
; ///< Dummy info in case a newgrf didn't provide info about some parameter.
150 GRFConfig
*grf_config
; ///< Set the parameters of this GRFConfig.
151 uint clicked_button
; ///< The row in which a button was clicked or UINT_MAX.
152 bool clicked_increase
; ///< True if the increase button was clicked, false for the decrease button.
153 bool clicked_dropdown
; ///< Whether the dropdown is open.
154 bool closing_dropdown
; ///< True, if the dropdown list is currently closing.
155 int timeout
; ///< How long before we unpress the last-pressed button?
156 uint clicked_row
; ///< The selected parameter
157 int line_height
; ///< Height of a row in the matrix widget.
159 bool action14present
; ///< True if action14 information is present.
160 bool editable
; ///< Allow editing parameters.
162 NewGRFParametersWindow(WindowDesc
*desc
, GRFConfig
*c
, bool editable
) : Window(desc
),
164 clicked_button(UINT_MAX
),
165 clicked_dropdown(false),
166 closing_dropdown(false),
168 clicked_row(UINT_MAX
),
171 this->action14present
= (c
->num_valid_params
!= lengthof(c
->param
) || c
->param_info
.Length() != 0);
173 this->CreateNestedTree();
174 this->vscroll
= this->GetScrollbar(WID_NP_SCROLLBAR
);
175 this->GetWidget
<NWidgetStacked
>(WID_NP_SHOW_NUMPAR
)->SetDisplayedPlane(this->action14present
? SZSP_HORIZONTAL
: 0);
176 this->GetWidget
<NWidgetStacked
>(WID_NP_SHOW_DESCRIPTION
)->SetDisplayedPlane(this->action14present
? 0 : SZSP_HORIZONTAL
);
177 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
179 this->SetWidgetDisabledState(WID_NP_RESET
, !this->editable
);
181 this->InvalidateData();
185 * Get a dummy parameter-info object with default information.
186 * @param nr The param number that should be changed.
187 * @return GRFParameterInfo with dummy information about the given parameter.
189 static GRFParameterInfo
*GetDummyParameterInfo(uint nr
)
191 dummy_parameter_info
.param_nr
= nr
;
192 return &dummy_parameter_info
;
195 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
198 case WID_NP_NUMPAR_DEC
:
199 case WID_NP_NUMPAR_INC
: {
200 size
->width
= size
->height
= FONT_HEIGHT_NORMAL
;
204 case WID_NP_NUMPAR
: {
205 SetDParamMaxValue(0, lengthof(this->grf_config
->param
));
206 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
207 d
.width
+= padding
.width
;
208 d
.height
+= padding
.height
;
209 *size
= maxdim(*size
, d
);
213 case WID_NP_BACKGROUND
:
214 this->line_height
= FONT_HEIGHT_NORMAL
+ WD_MATRIX_TOP
+ WD_MATRIX_BOTTOM
;
217 resize
->height
= this->line_height
;
218 size
->height
= 5 * this->line_height
;
221 case WID_NP_DESCRIPTION
:
222 /* Minimum size of 4 lines. The 500 is the default size of the window. */
223 Dimension suggestion
= {500 - WD_FRAMERECT_LEFT
- WD_FRAMERECT_RIGHT
, FONT_HEIGHT_NORMAL
* 4 + WD_TEXTPANEL_TOP
+ WD_TEXTPANEL_BOTTOM
};
224 for (uint i
= 0; i
< this->grf_config
->param_info
.Length(); i
++) {
225 const GRFParameterInfo
*par_info
= this->grf_config
->param_info
[i
];
226 if (par_info
== NULL
) continue;
227 const char *desc
= GetGRFStringFromGRFText(par_info
->desc
);
228 if (desc
== NULL
) continue;
229 Dimension d
= GetStringMultiLineBoundingBox(desc
, suggestion
);
230 d
.height
+= WD_TEXTPANEL_TOP
+ WD_TEXTPANEL_BOTTOM
;
231 suggestion
= maxdim(d
, suggestion
);
233 size
->height
= suggestion
.height
;
238 virtual void SetStringParameters(int widget
) const
242 SetDParam(0, this->vscroll
->GetCount());
247 virtual void DrawWidget(const Rect
&r
, int widget
) const
249 if (widget
== WID_NP_DESCRIPTION
) {
250 const GRFParameterInfo
*par_info
= (this->clicked_row
< this->grf_config
->param_info
.Length()) ? this->grf_config
->param_info
[this->clicked_row
] : NULL
;
251 if (par_info
== NULL
) return;
252 const char *desc
= GetGRFStringFromGRFText(par_info
->desc
);
253 if (desc
== NULL
) return;
254 DrawStringMultiLine(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, r
.top
+ WD_TEXTPANEL_TOP
, r
.bottom
- WD_TEXTPANEL_BOTTOM
, desc
, TC_BLACK
);
256 } else if (widget
!= WID_NP_BACKGROUND
) {
260 bool rtl
= _current_text_dir
== TD_RTL
;
261 uint buttons_left
= rtl
? r
.right
- SETTING_BUTTON_WIDTH
- 3 : r
.left
+ 4;
262 uint text_left
= r
.left
+ (rtl
? WD_FRAMERECT_LEFT
: SETTING_BUTTON_WIDTH
+ 8);
263 uint text_right
= r
.right
- (rtl
? SETTING_BUTTON_WIDTH
+ 8 : WD_FRAMERECT_RIGHT
);
266 int button_y_offset
= (this->line_height
- SETTING_BUTTON_HEIGHT
) / 2;
267 for (uint i
= this->vscroll
->GetPosition(); this->vscroll
->IsVisible(i
) && i
< this->vscroll
->GetCount(); i
++) {
268 GRFParameterInfo
*par_info
= (i
< this->grf_config
->param_info
.Length()) ? this->grf_config
->param_info
[i
] : NULL
;
269 if (par_info
== NULL
) par_info
= GetDummyParameterInfo(i
);
270 uint32 current_value
= par_info
->GetValue(this->grf_config
);
271 bool selected
= (i
== this->clicked_row
);
273 if (par_info
->type
== PTYPE_BOOL
) {
274 DrawBoolButton(buttons_left
, y
+ button_y_offset
, current_value
!= 0, this->editable
);
275 SetDParam(2, par_info
->GetValue(this->grf_config
) == 0 ? STR_CONFIG_SETTING_OFF
: STR_CONFIG_SETTING_ON
);
276 } else if (par_info
->type
== PTYPE_UINT_ENUM
) {
277 if (par_info
->complete_labels
) {
278 DrawDropDownButton(buttons_left
, y
+ button_y_offset
, COLOUR_YELLOW
, this->clicked_row
== i
&& this->clicked_dropdown
, this->editable
);
280 DrawArrowButtons(buttons_left
, y
+ button_y_offset
, COLOUR_YELLOW
, (this->clicked_button
== i
) ? 1 + (this->clicked_increase
!= rtl
) : 0, this->editable
&& current_value
> par_info
->min_value
, this->editable
&& current_value
< par_info
->max_value
);
282 SetDParam(2, STR_JUST_INT
);
283 SetDParam(3, current_value
);
284 if (par_info
->value_names
.Contains(current_value
)) {
285 const char *label
= GetGRFStringFromGRFText(par_info
->value_names
.Find(current_value
)->second
);
287 SetDParam(2, STR_JUST_RAW_STRING
);
288 SetDParamStr(3, label
);
293 const char *name
= GetGRFStringFromGRFText(par_info
->name
);
295 SetDParam(0, STR_JUST_RAW_STRING
);
296 SetDParamStr(1, name
);
298 SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME
);
302 DrawString(text_left
, text_right
, y
+ WD_MATRIX_TOP
, STR_NEWGRF_PARAMETERS_SETTING
, selected
? TC_WHITE
: TC_LIGHT_BLUE
);
303 y
+= this->line_height
;
307 virtual void OnPaint()
309 if (this->closing_dropdown
) {
310 this->closing_dropdown
= false;
311 this->clicked_dropdown
= false;
316 virtual void OnClick(Point pt
, int widget
, int click_count
)
319 case WID_NP_NUMPAR_DEC
:
320 if (this->editable
&& !this->action14present
&& this->grf_config
->num_params
> 0) {
321 this->grf_config
->num_params
--;
322 this->InvalidateData();
323 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_NEWGRF_STATE
);
327 case WID_NP_NUMPAR_INC
: {
328 GRFConfig
*c
= this->grf_config
;
329 if (this->editable
&& !this->action14present
&& c
->num_params
< c
->num_valid_params
) {
330 c
->param
[c
->num_params
++] = 0;
331 this->InvalidateData();
332 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_NEWGRF_STATE
);
337 case WID_NP_BACKGROUND
: {
338 if (!this->editable
) break;
339 uint num
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_NP_BACKGROUND
);
340 if (num
>= this->vscroll
->GetCount()) break;
341 if (this->clicked_row
!= num
) {
342 DeleteChildWindows(WC_QUERY_STRING
);
343 HideDropDownMenu(this);
344 this->clicked_row
= num
;
345 this->clicked_dropdown
= false;
348 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_NP_BACKGROUND
);
349 int x
= pt
.x
- wid
->pos_x
;
350 if (_current_text_dir
== TD_RTL
) x
= wid
->current_x
- 1 - x
;
353 GRFParameterInfo
*par_info
= (num
< this->grf_config
->param_info
.Length()) ? this->grf_config
->param_info
[num
] : NULL
;
354 if (par_info
== NULL
) par_info
= GetDummyParameterInfo(num
);
356 /* One of the arrows is clicked */
357 uint32 old_val
= par_info
->GetValue(this->grf_config
);
358 if (par_info
->type
!= PTYPE_BOOL
&& IsInsideMM(x
, 0, SETTING_BUTTON_WIDTH
) && par_info
->complete_labels
) {
359 if (this->clicked_dropdown
) {
360 /* unclick the dropdown */
361 HideDropDownMenu(this);
362 this->clicked_dropdown
= false;
363 this->closing_dropdown
= false;
365 const NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_NP_BACKGROUND
);
366 int rel_y
= (pt
.y
- (int)wid
->pos_y
) % this->line_height
;
369 wi_rect
.left
= pt
.x
- (_current_text_dir
== TD_RTL
? SETTING_BUTTON_WIDTH
- 1 - x
: x
);;
370 wi_rect
.right
= wi_rect
.left
+ SETTING_BUTTON_WIDTH
- 1;
371 wi_rect
.top
= pt
.y
- rel_y
+ (this->line_height
- SETTING_BUTTON_HEIGHT
) / 2;
372 wi_rect
.bottom
= wi_rect
.top
+ SETTING_BUTTON_HEIGHT
- 1;
374 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
375 if (pt
.y
>= wi_rect
.top
&& pt
.y
<= wi_rect
.bottom
) {
376 this->clicked_dropdown
= true;
377 this->closing_dropdown
= false;
379 DropDownList
*list
= new DropDownList();
380 for (uint32 i
= par_info
->min_value
; i
<= par_info
->max_value
; i
++) {
381 *list
->Append() = new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info
->value_names
.Find(i
)->second
), i
, false);
384 ShowDropDownListAt(this, list
, old_val
, -1, wi_rect
, COLOUR_ORANGE
, true);
387 } else if (IsInsideMM(x
, 0, SETTING_BUTTON_WIDTH
)) {
388 uint32 val
= old_val
;
389 if (par_info
->type
== PTYPE_BOOL
) {
392 if (x
>= SETTING_BUTTON_WIDTH
/ 2) {
393 /* Increase button clicked */
394 if (val
< par_info
->max_value
) val
++;
395 this->clicked_increase
= true;
397 /* Decrease button clicked */
398 if (val
> par_info
->min_value
) val
--;
399 this->clicked_increase
= false;
402 if (val
!= old_val
) {
403 par_info
->SetValue(this->grf_config
, val
);
405 this->clicked_button
= num
;
408 } else if (par_info
->type
== PTYPE_UINT_ENUM
&& !par_info
->complete_labels
&& click_count
>= 2) {
409 /* Display a query box so users can enter a custom value. */
410 SetDParam(0, old_val
);
411 ShowQueryString(STR_JUST_INT
, STR_CONFIG_SETTING_QUERY_CAPTION
, 10, this, CS_NUMERAL
, QSF_NONE
);
418 if (!this->editable
) break;
419 this->grf_config
->SetParameterDefaults();
420 this->InvalidateData();
421 SetWindowDirty(WC_GAME_OPTIONS
, WN_GAME_OPTIONS_NEWGRF_STATE
);
430 virtual void OnQueryTextFinished(char *str
)
432 if (StrEmpty(str
)) return;
433 int32 value
= atoi(str
);
434 GRFParameterInfo
*par_info
= ((uint
)this->clicked_row
< this->grf_config
->param_info
.Length()) ? this->grf_config
->param_info
[this->clicked_row
] : NULL
;
435 if (par_info
== NULL
) par_info
= GetDummyParameterInfo(this->clicked_row
);
436 uint32 val
= Clamp
<uint32
>(value
, par_info
->min_value
, par_info
->max_value
);
437 par_info
->SetValue(this->grf_config
, val
);
441 virtual void OnDropdownSelect(int widget
, int index
)
443 assert(this->clicked_dropdown
);
444 GRFParameterInfo
*par_info
= ((uint
)this->clicked_row
< this->grf_config
->param_info
.Length()) ? this->grf_config
->param_info
[this->clicked_row
] : NULL
;
445 if (par_info
== NULL
) par_info
= GetDummyParameterInfo(this->clicked_row
);
446 par_info
->SetValue(this->grf_config
, index
);
450 virtual void OnDropdownClose(Point pt
, int widget
, int index
, bool instant_close
)
452 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
453 * the same dropdown button was clicked again, and then not open the dropdown again.
454 * So, we only remember that it was closed, and process it on the next OnPaint, which is
456 assert(this->clicked_dropdown
);
457 this->closing_dropdown
= true;
461 virtual void OnResize()
463 this->vscroll
->SetCapacityFromWidget(this, WID_NP_BACKGROUND
);
467 * Some data on this window has become invalid.
468 * @param data Information about the changed data.
469 * @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.
471 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
473 if (!gui_scope
) return;
474 if (!this->action14present
) {
475 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC
, !this->editable
|| this->grf_config
->num_params
== 0);
476 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC
, !this->editable
|| this->grf_config
->num_params
>= this->grf_config
->num_valid_params
);
479 this->vscroll
->SetCount(this->action14present
? this->grf_config
->num_valid_params
: this->grf_config
->num_params
);
480 if (this->clicked_row
!= UINT_MAX
&& this->clicked_row
>= this->vscroll
->GetCount()) {
481 this->clicked_row
= UINT_MAX
;
482 DeleteChildWindows(WC_QUERY_STRING
);
486 virtual void OnTick()
488 if (--this->timeout
== 0) {
489 this->clicked_button
= UINT_MAX
;
494 GRFParameterInfo
NewGRFParametersWindow::dummy_parameter_info(0);
497 static const NWidgetPart _nested_newgrf_parameter_widgets
[] = {
498 NWidget(NWID_HORIZONTAL
),
499 NWidget(WWT_CLOSEBOX
, COLOUR_MAUVE
),
500 NWidget(WWT_CAPTION
, COLOUR_MAUVE
), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
501 NWidget(WWT_DEFSIZEBOX
, COLOUR_MAUVE
),
503 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NP_SHOW_NUMPAR
),
504 NWidget(WWT_PANEL
, COLOUR_MAUVE
), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
505 NWidget(NWID_HORIZONTAL
), SetPIP(4, 0, 4),
506 NWidget(WWT_PUSHARROWBTN
, COLOUR_YELLOW
, WID_NP_NUMPAR_DEC
), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE
, STR_NULL
),
507 NWidget(WWT_PUSHARROWBTN
, COLOUR_YELLOW
, WID_NP_NUMPAR_INC
), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE
, STR_NULL
),
508 NWidget(WWT_TEXT
, COLOUR_MAUVE
, WID_NP_NUMPAR
), SetResize(1, 0), SetFill(1, 0), SetPadding(0, 0, 0, 4), SetDataTip(STR_NEWGRF_PARAMETERS_NUM_PARAM
, STR_NULL
),
512 NWidget(NWID_HORIZONTAL
),
513 NWidget(WWT_MATRIX
, COLOUR_MAUVE
, WID_NP_BACKGROUND
), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL
), SetScrollbar(WID_NP_SCROLLBAR
),
514 NWidget(NWID_VSCROLLBAR
, COLOUR_MAUVE
, WID_NP_SCROLLBAR
),
516 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NP_SHOW_DESCRIPTION
),
517 NWidget(WWT_PANEL
, COLOUR_MAUVE
, WID_NP_DESCRIPTION
), SetResize(1, 0), SetFill(1, 0),
520 NWidget(NWID_HORIZONTAL
),
521 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
522 NWidget(WWT_PUSHTXTBTN
, COLOUR_MAUVE
, WID_NP_ACCEPT
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE
, STR_NULL
),
523 NWidget(WWT_PUSHTXTBTN
, COLOUR_MAUVE
, WID_NP_RESET
), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET
, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP
),
525 NWidget(WWT_RESIZEBOX
, COLOUR_MAUVE
),
529 /** Window definition for the change grf parameters window */
530 static WindowDesc
_newgrf_parameters_desc(
531 WDP_CENTER
, "settings_newgrf_config", 500, 208,
532 WC_GRF_PARAMETERS
, WC_NONE
,
534 _nested_newgrf_parameter_widgets
, lengthof(_nested_newgrf_parameter_widgets
)
537 static void OpenGRFParameterWindow(GRFConfig
*c
, bool editable
)
539 DeleteWindowByClass(WC_GRF_PARAMETERS
);
540 new NewGRFParametersWindow(&_newgrf_parameters_desc
, c
, editable
);
543 /** Window for displaying the textfile of a NewGRF. */
544 struct NewGRFTextfileWindow
: public TextfileWindow
{
545 const GRFConfig
*grf_config
; ///< View the textfile of this GRFConfig.
547 NewGRFTextfileWindow(TextfileType file_type
, const GRFConfig
*c
) : TextfileWindow(file_type
), grf_config(c
)
549 const char *textfile
= this->grf_config
->GetTextfile(file_type
);
550 this->LoadTextfile(textfile
, NEWGRF_DIR
);
553 /* virtual */ void SetStringParameters(int widget
) const
555 if (widget
== WID_TF_CAPTION
) {
556 SetDParam(0, STR_CONTENT_TYPE_NEWGRF
);
557 SetDParamStr(1, this->grf_config
->GetName());
562 void ShowNewGRFTextfileWindow(TextfileType file_type
, const GRFConfig
*c
)
564 DeleteWindowByClass(WC_TEXTFILE
);
565 new NewGRFTextfileWindow(file_type
, c
);
568 static GRFPresetList _grf_preset_list
;
570 class DropDownListPresetItem
: public DropDownListItem
{
572 DropDownListPresetItem(int result
) : DropDownListItem(result
, false) {}
574 virtual ~DropDownListPresetItem() {}
576 bool Selectable() const
581 void Draw(int left
, int right
, int top
, int bottom
, bool sel
, int bg_colour
) const
583 DrawString(left
+ 2, right
+ 2, top
, _grf_preset_list
[this->result
], sel
? TC_WHITE
: TC_BLACK
);
587 static void NewGRFConfirmationCallback(Window
*w
, bool confirmed
);
590 * Window for showing NewGRF files
592 struct NewGRFWindow
: public Window
, NewGRFScanCallback
{
593 typedef GUIList
<const GRFConfig
*, StringFilter
&> GUIGRFConfigList
;
595 static const uint EDITBOX_MAX_SIZE
= 50;
597 static Listing last_sorting
; ///< Default sorting of #GUIGRFConfigList.
598 static Filtering last_filtering
; ///< Default filtering of #GUIGRFConfigList.
599 static GUIGRFConfigList::SortFunction
* const sorter_funcs
[]; ///< Sort functions of the #GUIGRFConfigList.
600 static GUIGRFConfigList::FilterFunction
* const filter_funcs
[]; ///< Filter functions of the #GUIGRFConfigList.
602 GUIGRFConfigList avails
; ///< Available (non-active) grfs.
603 const GRFConfig
*avail_sel
; ///< Currently selected available grf. \c NULL is none is selected.
604 int avail_pos
; ///< Index of #avail_sel if existing, else \c -1.
605 StringFilter string_filter
; ///< Filter for available grf.
606 QueryString filter_editbox
; ///< Filter editbox;
608 GRFConfig
*actives
; ///< Temporary active grf list to which changes are made.
609 GRFConfig
*active_sel
; ///< Selected active grf item.
611 GRFConfig
**orig_list
; ///< List active grfs in the game. Used as initial value, may be updated by the window.
612 bool editable
; ///< Is the window editable?
613 bool show_params
; ///< Are the grf-parameters shown in the info-panel?
614 bool execute
; ///< On pressing 'apply changes' are grf changes applied immediately, or only list is updated.
615 int preset
; ///< Selected preset.
616 int active_over
; ///< Active GRF item over which another one is dragged, \c -1 if none.
621 NewGRFWindow(WindowDesc
*desc
, bool editable
, bool show_params
, bool execute
, GRFConfig
**orig_list
) : Window(desc
), filter_editbox(EDITBOX_MAX_SIZE
)
623 this->avail_sel
= NULL
;
624 this->avail_pos
= -1;
625 this->active_sel
= NULL
;
626 this->actives
= NULL
;
627 this->orig_list
= orig_list
;
628 this->editable
= editable
;
629 this->execute
= execute
;
630 this->show_params
= show_params
;
632 this->active_over
= -1;
634 CopyGRFConfigList(&this->actives
, *orig_list
, false);
635 GetGRFPresetList(&_grf_preset_list
);
637 this->CreateNestedTree();
638 this->vscroll
= this->GetScrollbar(WID_NS_SCROLLBAR
);
639 this->vscroll2
= this->GetScrollbar(WID_NS_SCROLL2BAR
);
641 this->GetWidget
<NWidgetStacked
>(WID_NS_SHOW_REMOVE
)->SetDisplayedPlane(this->editable
? 0 : 1);
642 this->GetWidget
<NWidgetStacked
>(WID_NS_SHOW_APPLY
)->SetDisplayedPlane(this->editable
? 0 : this->show_params
? 1 : SZSP_HORIZONTAL
);
643 this->FinishInitNested(WN_GAME_OPTIONS_NEWGRF_STATE
);
645 this->querystrings
[WID_NS_FILTER
] = &this->filter_editbox
;
646 this->filter_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
648 this->SetFocusedWidget(WID_NS_FILTER
);
650 this->DisableWidget(WID_NS_FILTER
);
653 this->avails
.SetListing(this->last_sorting
);
654 this->avails
.SetFiltering(this->last_filtering
);
655 this->avails
.SetSortFuncs(this->sorter_funcs
);
656 this->avails
.SetFilterFuncs(this->filter_funcs
);
657 this->avails
.ForceRebuild();
659 this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED
);
664 DeleteWindowByClass(WC_GRF_PARAMETERS
);
665 DeleteWindowByClass(WC_TEXTFILE
);
667 if (this->editable
&& !this->execute
) {
668 CopyGRFConfigList(this->orig_list
, this->actives
, true);
669 ResetGRFConfig(false);
673 /* Remove the temporary copy of grf-list used in window */
674 ClearGRFConfigList(&this->actives
);
675 _grf_preset_list
.Clear();
678 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
681 case WID_NS_FILE_LIST
:
683 Dimension d
= maxdim(GetSpriteSize(SPR_SQUARE
), GetSpriteSize(SPR_WARNING_SIGN
));
684 resize
->height
= max(d
.height
+ 2U, FONT_HEIGHT_NORMAL
+ 2U);
685 size
->height
= max(size
->height
, WD_FRAMERECT_TOP
+ 6 * resize
->height
+ WD_FRAMERECT_BOTTOM
);
689 case WID_NS_AVAIL_LIST
:
690 resize
->height
= max(12, FONT_HEIGHT_NORMAL
+ 2);
691 size
->height
= max(size
->height
, WD_FRAMERECT_TOP
+ 8 * resize
->height
+ WD_FRAMERECT_BOTTOM
);
694 case WID_NS_NEWGRF_INFO_TITLE
: {
695 Dimension dim
= GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE
);
696 size
->height
= max(size
->height
, dim
.height
+ WD_FRAMETEXT_TOP
+ WD_FRAMETEXT_BOTTOM
);
697 size
->width
= max(size
->width
, dim
.width
+ WD_FRAMETEXT_LEFT
+ WD_FRAMETEXT_RIGHT
);
701 case WID_NS_NEWGRF_INFO
:
702 size
->height
= max(size
->height
, WD_FRAMERECT_TOP
+ 10 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
+ padding
.height
+ 2);
705 case WID_NS_PRESET_LIST
: {
706 Dimension d
= GetStringBoundingBox(STR_NUM_CUSTOM
);
707 for (uint i
= 0; i
< _grf_preset_list
.Length(); i
++) {
708 if (_grf_preset_list
[i
] != NULL
) {
709 SetDParamStr(0, _grf_preset_list
[i
]);
710 d
= maxdim(d
, GetStringBoundingBox(STR_JUST_RAW_STRING
));
713 d
.width
+= padding
.width
;
714 *size
= maxdim(d
, *size
);
718 case WID_NS_CONTENT_DOWNLOAD
:
719 case WID_NS_CONTENT_DOWNLOAD2
: {
720 Dimension d
= GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON
);
721 *size
= maxdim(d
, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT
));
722 size
->width
+= padding
.width
;
723 size
->height
+= padding
.height
;
729 virtual void OnResize()
731 this->vscroll
->SetCapacityFromWidget(this, WID_NS_FILE_LIST
);
732 this->vscroll2
->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST
);
735 virtual void SetStringParameters(int widget
) const
738 case WID_NS_PRESET_LIST
:
739 if (this->preset
== -1) {
740 SetDParam(0, STR_NUM_CUSTOM
);
742 SetDParam(0, STR_JUST_RAW_STRING
);
743 SetDParamStr(1, _grf_preset_list
[this->preset
]);
750 * Pick the palette for the sprite of the grf to display.
751 * @param c grf to display.
752 * @return Palette for the sprite.
754 inline PaletteID
GetPalette(const GRFConfig
*c
) const
762 pal
= PALETTE_TO_RED
;
765 pal
= PALETTE_TO_GREEN
;
768 pal
= PALETTE_TO_BLUE
;
772 /* Do not show a "not-failure" colour when it actually failed to load */
773 if (pal
!= PALETTE_TO_RED
) {
774 if (HasBit(c
->flags
, GCF_STATIC
)) {
775 pal
= PALETTE_TO_GREY
;
776 } else if (HasBit(c
->flags
, GCF_COMPATIBLE
)) {
777 pal
= PALETTE_TO_ORANGE
;
784 virtual void DrawWidget(const Rect
&r
, int widget
) const
787 case WID_NS_FILE_LIST
: {
788 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_BLACK
);
790 uint step_height
= this->GetWidget
<NWidgetBase
>(WID_NS_FILE_LIST
)->resize_y
;
791 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
792 Dimension square
= GetSpriteSize(SPR_SQUARE
);
793 Dimension warning
= GetSpriteSize(SPR_WARNING_SIGN
);
794 int square_offset_y
= (step_height
- square
.height
) / 2;
795 int warning_offset_y
= (step_height
- warning
.height
) / 2;
796 int offset_y
= (step_height
- FONT_HEIGHT_NORMAL
) / 2;
798 bool rtl
= _current_text_dir
== TD_RTL
;
799 uint text_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.left
+ square
.width
+ 15;
800 uint text_right
= rtl
? r
.right
- square
.width
- 15 : r
.right
- WD_FRAMERECT_RIGHT
;
801 uint square_left
= rtl
? r
.right
- square
.width
- 5 : r
.left
+ 5;
802 uint warning_left
= rtl
? r
.right
- square
.width
- warning
.width
- 10 : r
.left
+ square
.width
+ 10;
805 for (const GRFConfig
*c
= this->actives
; c
!= NULL
; c
= c
->next
, i
++) {
806 if (this->vscroll
->IsVisible(i
)) {
807 const char *text
= c
->GetName();
808 bool h
= (this->active_sel
== c
);
809 PaletteID pal
= this->GetPalette(c
);
812 GfxFillRect(r
.left
+ 1, y
, r
.right
- 1, y
+ step_height
- 1, PC_DARK_BLUE
);
813 } else if (i
== this->active_over
) {
814 /* Get index of current selection. */
815 int active_sel_pos
= 0;
816 for (GRFConfig
*c
= this->actives
; c
!= NULL
&& c
!= this->active_sel
; c
= c
->next
, active_sel_pos
++) {}
817 if (active_sel_pos
!= this->active_over
) {
818 uint top
= this->active_over
< active_sel_pos
? y
+ 1 : y
+ step_height
- 2;
819 GfxFillRect(r
.left
+ WD_FRAMERECT_LEFT
, top
- 1, r
.right
- WD_FRAMERECT_RIGHT
, top
+ 1, PC_GREY
);
822 DrawSprite(SPR_SQUARE
, pal
, square_left
, y
+ square_offset_y
);
823 if (c
->error
!= NULL
) DrawSprite(SPR_WARNING_SIGN
, 0, warning_left
, y
+ warning_offset_y
);
824 uint txtoffset
= c
->error
== NULL
? 0 : warning
.width
;
825 DrawString(text_left
+ (rtl
? 0 : txtoffset
), text_right
- (rtl
? txtoffset
: 0), y
+ offset_y
, text
, h
? TC_WHITE
: TC_ORANGE
);
829 if (i
== this->active_over
&& this->vscroll
->IsVisible(i
)) { // Highlight is after the last GRF entry.
830 GfxFillRect(r
.left
+ WD_FRAMERECT_LEFT
, y
, r
.right
- WD_FRAMERECT_RIGHT
, y
+ 2, PC_GREY
);
835 case WID_NS_AVAIL_LIST
: {
836 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, this->active_over
== -2 ? PC_DARK_GREY
: PC_BLACK
);
838 uint step_height
= this->GetWidget
<NWidgetBase
>(WID_NS_AVAIL_LIST
)->resize_y
;
839 int offset_y
= (step_height
- FONT_HEIGHT_NORMAL
) / 2;
840 uint y
= r
.top
+ WD_FRAMERECT_TOP
;
841 uint min_index
= this->vscroll2
->GetPosition();
842 uint max_index
= min(min_index
+ this->vscroll2
->GetCapacity(), this->avails
.Length());
844 for (uint i
= min_index
; i
< max_index
; i
++) {
845 const GRFConfig
*c
= this->avails
[i
];
846 bool h
= (c
== this->avail_sel
);
847 const char *text
= c
->GetName();
849 if (h
) GfxFillRect(r
.left
+ 1, y
, r
.right
- 1, y
+ step_height
- 1, PC_DARK_BLUE
);
850 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
+ offset_y
, text
, h
? TC_WHITE
: TC_SILVER
);
856 case WID_NS_NEWGRF_INFO_TITLE
:
857 /* Create the nice grayish rectangle at the details top. */
858 GfxFillRect(r
.left
+ 1, r
.top
+ 1, r
.right
- 1, r
.bottom
- 1, PC_DARK_BLUE
);
859 DrawString(r
.left
, r
.right
, (r
.top
+ r
.bottom
- FONT_HEIGHT_NORMAL
) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE
, TC_FROMSTRING
, SA_HOR_CENTER
);
862 case WID_NS_NEWGRF_INFO
: {
863 const GRFConfig
*selected
= this->active_sel
;
864 if (selected
== NULL
) selected
= this->avail_sel
;
865 if (selected
!= NULL
) {
866 ShowNewGRFInfo(selected
, r
.left
+ WD_FRAMERECT_LEFT
, r
.top
+ WD_FRAMERECT_TOP
, r
.right
- WD_FRAMERECT_RIGHT
, r
.bottom
- WD_FRAMERECT_BOTTOM
, this->show_params
);
873 virtual void OnClick(Point pt
, int widget
, int click_count
)
875 if (widget
>= WID_NS_NEWGRF_TEXTFILE
&& widget
< WID_NS_NEWGRF_TEXTFILE
+ TFT_END
) {
876 if (this->active_sel
== NULL
&& this->avail_sel
== NULL
) return;
878 ShowNewGRFTextfileWindow((TextfileType
)(widget
- WID_NS_NEWGRF_TEXTFILE
), this->active_sel
!= NULL
? this->active_sel
: this->avail_sel
);
883 case WID_NS_PRESET_LIST
: {
884 DropDownList
*list
= new DropDownList();
886 /* Add 'None' option for clearing list */
887 *list
->Append() = new DropDownListStringItem(STR_NONE
, -1, false);
889 for (uint i
= 0; i
< _grf_preset_list
.Length(); i
++) {
890 if (_grf_preset_list
[i
] != NULL
) {
891 *list
->Append() = new DropDownListPresetItem(i
);
895 this->DeleteChildWindows(WC_QUERY_STRING
); // Remove the parameter query window
896 ShowDropDownList(this, list
, this->preset
, WID_NS_PRESET_LIST
);
900 case WID_NS_OPEN_URL
: {
901 const GRFConfig
*c
= (this->avail_sel
== NULL
) ? this->active_sel
: this->avail_sel
;
903 extern void OpenBrowser(const char *url
);
904 OpenBrowser(c
->GetURL());
908 case WID_NS_PRESET_SAVE
:
909 ShowQueryString(STR_EMPTY
, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY
, 32, this, CS_ALPHANUMERAL
, QSF_NONE
);
912 case WID_NS_PRESET_DELETE
:
913 if (this->preset
== -1) return;
915 DeleteGRFPresetFromConfig(_grf_preset_list
[this->preset
]);
916 GetGRFPresetList(&_grf_preset_list
);
918 this->InvalidateData();
919 this->DeleteChildWindows(WC_QUERY_STRING
); // Remove the parameter query window
922 case WID_NS_MOVE_UP
: { // Move GRF up
923 if (this->active_sel
== NULL
|| !this->editable
) break;
926 for (GRFConfig
**pc
= &this->actives
; *pc
!= NULL
; pc
= &(*pc
)->next
, pos
++) {
928 if (c
->next
== this->active_sel
) {
929 c
->next
= this->active_sel
->next
;
930 this->active_sel
->next
= c
;
931 *pc
= this->active_sel
;
935 this->vscroll
->ScrollTowards(pos
);
937 this->InvalidateData();
941 case WID_NS_MOVE_DOWN
: { // Move GRF down
942 if (this->active_sel
== NULL
|| !this->editable
) break;
944 int pos
= 1; // Start at 1 as we swap the selected newgrf with the next one
945 for (GRFConfig
**pc
= &this->actives
; *pc
!= NULL
; pc
= &(*pc
)->next
, pos
++) {
947 if (c
== this->active_sel
) {
949 c
->next
= c
->next
->next
;
954 this->vscroll
->ScrollTowards(pos
);
956 this->InvalidateData();
960 case WID_NS_FILE_LIST
: { // Select an active GRF.
961 ResetObjectToPlace();
963 uint i
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_NS_FILE_LIST
);
966 for (c
= this->actives
; c
!= NULL
&& i
> 0; c
= c
->next
, i
--) {}
968 if (this->active_sel
!= c
) DeleteWindowByClass(WC_GRF_PARAMETERS
);
969 this->active_sel
= c
;
970 this->avail_sel
= NULL
;
971 this->avail_pos
= -1;
973 this->InvalidateData();
974 if (click_count
== 1) {
975 if (this->editable
&& this->active_sel
!= NULL
) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE
, PAL_NONE
, HT_DRAG
, this);
978 /* FALL THROUGH, with double click. */
981 case WID_NS_REMOVE
: { // Remove GRF
982 if (this->active_sel
== NULL
|| !this->editable
) break;
983 DeleteWindowByClass(WC_GRF_PARAMETERS
);
985 /* Choose the next GRF file to be the selected file. */
986 GRFConfig
*newsel
= this->active_sel
->next
;
987 for (GRFConfig
**pc
= &this->actives
; *pc
!= NULL
; pc
= &(*pc
)->next
) {
989 /* If the new selection is empty (i.e. we're deleting the last item
990 * in the list, pick the file just before the selected file */
991 if (newsel
== NULL
&& c
->next
== this->active_sel
) newsel
= c
;
993 if (c
== this->active_sel
) {
994 if (newsel
== c
) newsel
= NULL
;
1002 this->active_sel
= newsel
;
1004 this->avail_pos
= -1;
1005 this->avail_sel
= NULL
;
1006 this->avails
.ForceRebuild();
1007 this->InvalidateData(GOID_NEWGRF_LIST_EDITED
);
1011 case WID_NS_AVAIL_LIST
: { // Select a non-active GRF.
1012 ResetObjectToPlace();
1014 uint i
= this->vscroll2
->GetScrolledRowFromWidget(pt
.y
, this, WID_NS_AVAIL_LIST
);
1015 this->active_sel
= NULL
;
1016 DeleteWindowByClass(WC_GRF_PARAMETERS
);
1017 if (i
< this->avails
.Length()) {
1018 this->avail_sel
= this->avails
[i
];
1019 this->avail_pos
= i
;
1021 this->InvalidateData();
1022 if (click_count
== 1) {
1023 if (this->editable
&& this->avail_sel
!= NULL
&& !HasBit(this->avail_sel
->flags
, GCF_INVALID
)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE
, PAL_NONE
, HT_DRAG
, this);
1026 /* FALL THROUGH, with double click. */
1030 if (this->avail_sel
== NULL
|| !this->editable
|| HasBit(this->avail_sel
->flags
, GCF_INVALID
)) break;
1032 this->AddGRFToActive();
1035 case WID_NS_APPLY_CHANGES
: // Apply changes made to GRF list
1036 if (!this->editable
) break;
1037 if (this->execute
) {
1039 STR_NEWGRF_POPUP_CAUTION_CAPTION
,
1040 STR_NEWGRF_CONFIRMATION_TEXT
,
1042 NewGRFConfirmationCallback
1045 CopyGRFConfigList(this->orig_list
, this->actives
, true);
1046 ResetGRFConfig(false);
1049 this->DeleteChildWindows(WC_QUERY_STRING
); // Remove the parameter query window
1052 case WID_NS_VIEW_PARAMETERS
:
1053 case WID_NS_SET_PARAMETERS
: { // Edit parameters
1054 if (this->active_sel
== NULL
|| !this->show_params
|| this->active_sel
->num_valid_params
== 0) break;
1056 OpenGRFParameterWindow(this->active_sel
, this->editable
);
1060 case WID_NS_TOGGLE_PALETTE
:
1061 if (this->active_sel
!= NULL
&& this->editable
) {
1062 this->active_sel
->palette
^= GRFP_USE_MASK
;
1067 case WID_NS_CONTENT_DOWNLOAD
:
1068 case WID_NS_CONTENT_DOWNLOAD2
:
1069 if (!_network_available
) {
1070 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
1072 #if defined(ENABLE_NETWORK)
1073 this->DeleteChildWindows(WC_QUERY_STRING
); // Remove the parameter query window
1075 ShowMissingContentWindow(this->actives
);
1080 case WID_NS_RESCAN_FILES
:
1081 case WID_NS_RESCAN_FILES2
:
1082 ScanNewGRFFiles(this);
1087 virtual void OnNewGRFsScanned()
1089 this->avail_sel
= NULL
;
1090 this->avail_pos
= -1;
1091 this->avails
.ForceRebuild();
1092 this->DeleteChildWindows(WC_QUERY_STRING
); // Remove the parameter query window
1093 this->DeleteChildWindows(WC_TEXTFILE
); // Remove the view textfile window
1096 virtual void OnDropdownSelect(int widget
, int index
)
1098 if (!this->editable
) return;
1100 ClearGRFConfigList(&this->actives
);
1101 this->preset
= index
;
1104 this->actives
= LoadGRFPresetFromConfig(_grf_preset_list
[index
]);
1106 this->avails
.ForceRebuild();
1108 ResetObjectToPlace();
1109 DeleteWindowByClass(WC_GRF_PARAMETERS
);
1110 this->active_sel
= NULL
;
1111 this->InvalidateData(GOID_NEWGRF_PRESET_LOADED
);
1114 virtual void OnQueryTextFinished(char *str
)
1116 if (str
== NULL
) return;
1118 SaveGRFPresetToConfig(str
, this->actives
);
1119 GetGRFPresetList(&_grf_preset_list
);
1121 /* Switch to this preset */
1122 for (uint i
= 0; i
< _grf_preset_list
.Length(); i
++) {
1123 if (_grf_preset_list
[i
] != NULL
&& strcmp(_grf_preset_list
[i
], str
) == 0) {
1129 this->InvalidateData();
1133 * Some data on this window has become invalid.
1134 * @param data Information about the changed data. @see GameOptionsInvalidationData
1135 * @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.
1137 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
1139 if (!gui_scope
) return;
1142 /* Nothing important to do */
1145 case GOID_NEWGRF_RESCANNED
:
1146 /* Search the list for items that are now found and mark them as such. */
1147 for (GRFConfig
**l
= &this->actives
; *l
!= NULL
; l
= &(*l
)->next
) {
1149 bool compatible
= HasBit(c
->flags
, GCF_COMPATIBLE
);
1150 if (c
->status
!= GCS_NOT_FOUND
&& !compatible
) continue;
1152 const GRFConfig
*f
= FindGRFConfig(c
->ident
.grfid
, FGCM_EXACT
, compatible
? c
->original_md5sum
: c
->ident
.md5sum
);
1153 if (f
== NULL
|| HasBit(f
->flags
, GCF_INVALID
)) continue;
1155 *l
= new GRFConfig(*f
);
1156 (*l
)->next
= c
->next
;
1158 if (active_sel
== c
) active_sel
= *l
;
1163 this->avails
.ForceRebuild();
1165 case GOID_NEWGRF_LIST_EDITED
:
1168 case GOID_NEWGRF_PRESET_LOADED
: {
1169 /* Update scrollbars */
1171 for (const GRFConfig
*c
= this->actives
; c
!= NULL
; c
= c
->next
, i
++) {}
1173 this->vscroll
->SetCount(i
+ 1); // Reserve empty space for drag and drop handling.
1175 if (this->avail_pos
>= 0) this->vscroll2
->ScrollTowards(this->avail_pos
);
1180 this->BuildAvailables();
1182 this->SetWidgetsDisabledState(!this->editable
,
1184 WID_NS_APPLY_CHANGES
,
1185 WID_NS_TOGGLE_PALETTE
,
1188 this->SetWidgetDisabledState(WID_NS_ADD
, !this->editable
|| this->avail_sel
== NULL
|| HasBit(this->avail_sel
->flags
, GCF_INVALID
));
1190 bool disable_all
= this->active_sel
== NULL
|| !this->editable
;
1191 this->SetWidgetsDisabledState(disable_all
,
1198 const GRFConfig
*c
= (this->avail_sel
== NULL
) ? this->active_sel
: this->avail_sel
;
1199 for (TextfileType tft
= TFT_BEGIN
; tft
< TFT_END
; tft
++) {
1200 this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE
+ tft
, c
== NULL
|| c
->GetTextfile(tft
) == NULL
);
1202 this->SetWidgetDisabledState(WID_NS_OPEN_URL
, c
== NULL
|| StrEmpty(c
->GetURL()));
1204 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS
, !this->show_params
|| this->active_sel
== NULL
|| this->active_sel
->num_valid_params
== 0);
1205 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS
, !this->show_params
|| this->active_sel
== NULL
|| this->active_sel
->num_valid_params
== 0);
1206 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE
, disable_all
||
1207 (!(_settings_client
.gui
.newgrf_developer_tools
|| _settings_client
.gui
.scenario_developer
) && ((c
->palette
& GRFP_GRF_MASK
) != GRFP_GRF_UNSET
)));
1210 /* All widgets are now enabled, so disable widgets we can't use */
1211 if (this->active_sel
== this->actives
) this->DisableWidget(WID_NS_MOVE_UP
);
1212 if (this->active_sel
->next
== NULL
) this->DisableWidget(WID_NS_MOVE_DOWN
);
1213 if (this->active_sel
->IsOpenTTDBaseGRF()) this->DisableWidget(WID_NS_REMOVE
);
1216 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE
, this->preset
== -1);
1218 bool has_missing
= false;
1219 bool has_compatible
= false;
1220 for (const GRFConfig
*c
= this->actives
; !has_missing
&& c
!= NULL
; c
= c
->next
) {
1221 has_missing
|= c
->status
== GCS_NOT_FOUND
;
1222 has_compatible
|= HasBit(c
->flags
, GCF_COMPATIBLE
);
1226 if (has_missing
|| has_compatible
) {
1227 widget_data
= STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON
;
1228 tool_tip
= STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP
;
1230 widget_data
= STR_INTRO_ONLINE_CONTENT
;
1231 tool_tip
= STR_INTRO_TOOLTIP_ONLINE_CONTENT
;
1233 this->GetWidget
<NWidgetCore
>(WID_NS_CONTENT_DOWNLOAD
)->widget_data
= widget_data
;
1234 this->GetWidget
<NWidgetCore
>(WID_NS_CONTENT_DOWNLOAD
)->tool_tip
= tool_tip
;
1235 this->GetWidget
<NWidgetCore
>(WID_NS_CONTENT_DOWNLOAD2
)->widget_data
= widget_data
;
1236 this->GetWidget
<NWidgetCore
>(WID_NS_CONTENT_DOWNLOAD2
)->tool_tip
= tool_tip
;
1238 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE
, has_missing
);
1241 virtual EventState
OnKeyPress(WChar key
, uint16 keycode
)
1243 if (!this->editable
) return ES_NOT_HANDLED
;
1247 /* scroll up by one */
1248 if (this->avail_pos
> 0) this->avail_pos
--;
1252 /* scroll down by one */
1253 if (this->avail_pos
< (int)this->avails
.Length() - 1) this->avail_pos
++;
1257 /* scroll up a page */
1258 this->avail_pos
= (this->avail_pos
< this->vscroll2
->GetCapacity()) ? 0 : this->avail_pos
- this->vscroll2
->GetCapacity();
1262 /* scroll down a page */
1263 this->avail_pos
= min(this->avail_pos
+ this->vscroll2
->GetCapacity(), (int)this->avails
.Length() - 1);
1267 /* jump to beginning */
1268 this->avail_pos
= 0;
1273 this->avail_pos
= this->avails
.Length() - 1;
1277 return ES_NOT_HANDLED
;
1280 if (this->avails
.Length() == 0) this->avail_pos
= -1;
1281 if (this->avail_pos
>= 0) {
1282 this->avail_sel
= this->avails
[this->avail_pos
];
1283 this->vscroll2
->ScrollTowards(this->avail_pos
);
1284 this->InvalidateData(0);
1290 virtual void OnEditboxChanged(int wid
)
1292 if (!this->editable
) return;
1294 string_filter
.SetFilterTerm(this->filter_editbox
.text
.buf
);
1295 this->avails
.SetFilterState(!string_filter
.IsEmpty());
1296 this->avails
.ForceRebuild();
1297 this->InvalidateData(0);
1300 virtual void OnDragDrop(Point pt
, int widget
)
1302 if (!this->editable
) return;
1304 if (widget
== WID_NS_FILE_LIST
) {
1305 if (this->active_sel
!= NULL
) {
1306 /* Get pointer to the selected file in the active list. */
1308 GRFConfig
**from_prev
;
1309 for (from_prev
= &this->actives
; *from_prev
!= this->active_sel
; from_prev
= &(*from_prev
)->next
, from_pos
++) {}
1311 /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1312 int to_pos
= min(this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_NS_FILE_LIST
), this->vscroll
->GetCount() - 2);
1313 if (to_pos
!= from_pos
) { // Don't move NewGRF file over itself.
1314 /* Get pointer to destination position. */
1315 GRFConfig
**to_prev
= &this->actives
;
1316 for (int i
= from_pos
< to_pos
? -1 : 0; *to_prev
!= NULL
&& i
< to_pos
; to_prev
= &(*to_prev
)->next
, i
++) {}
1318 /* Detach NewGRF file from its original position. */
1319 *from_prev
= this->active_sel
->next
;
1321 /* Attach NewGRF file to its new position. */
1322 this->active_sel
->next
= *to_prev
;
1323 *to_prev
= this->active_sel
;
1325 this->vscroll
->ScrollTowards(to_pos
);
1327 this->InvalidateData();
1329 } else if (this->avail_sel
!= NULL
) {
1330 int to_pos
= min(this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_NS_FILE_LIST
), this->vscroll
->GetCount() - 1);
1331 this->AddGRFToActive(to_pos
);
1333 } else if (widget
== WID_NS_AVAIL_LIST
&& this->active_sel
!= NULL
) {
1334 /* Remove active NewGRF file by dragging it over available list. */
1335 Point dummy
= {-1, -1};
1336 this->OnClick(dummy
, WID_NS_REMOVE
, 1);
1339 ResetObjectToPlace();
1341 if (this->active_over
!= -1) {
1342 /* End of drag-and-drop, hide dragged destination highlight. */
1343 this->SetWidgetDirty(this->active_over
== -2 ? WID_NS_AVAIL_LIST
: WID_NS_FILE_LIST
);
1344 this->active_over
= -1;
1348 virtual void OnMouseDrag(Point pt
, int widget
)
1350 if (!this->editable
) return;
1352 if (widget
== WID_NS_FILE_LIST
&& (this->active_sel
!= NULL
|| this->avail_sel
!= NULL
)) {
1353 /* An NewGRF file is dragged over the active list. */
1354 int to_pos
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_NS_FILE_LIST
);
1355 /* Skip the last dummy line if the source is from the active list. */
1356 to_pos
= min(to_pos
, this->vscroll
->GetCount() - (this->active_sel
!= NULL
? 2 : 1));
1358 if (to_pos
!= this->active_over
) {
1359 this->active_over
= to_pos
;
1360 this->SetWidgetDirty(WID_NS_FILE_LIST
);
1362 } else if (widget
== WID_NS_AVAIL_LIST
&& this->active_sel
!= NULL
) {
1363 this->active_over
= -2;
1364 this->SetWidgetDirty(WID_NS_AVAIL_LIST
);
1365 } else if (this->active_over
!= -1) {
1366 this->SetWidgetDirty(this->active_over
== -2 ? WID_NS_AVAIL_LIST
: WID_NS_FILE_LIST
);
1367 this->active_over
= -1;
1372 /** Sort grfs by name. */
1373 static int CDECL
NameSorter(const GRFConfig
* const *a
, const GRFConfig
* const *b
)
1375 int i
= strnatcmp((*a
)->GetName(), (*b
)->GetName(), true); // Sort by name (natural sorting).
1376 if (i
!= 0) return i
;
1378 i
= (*a
)->version
- (*b
)->version
;
1379 if (i
!= 0) return i
;
1381 return memcmp((*a
)->ident
.md5sum
, (*b
)->ident
.md5sum
, lengthof((*b
)->ident
.md5sum
));
1384 /** Filter grfs by tags/name */
1385 static bool CDECL
TagNameFilter(const GRFConfig
* const *a
, StringFilter
&filter
)
1387 filter
.ResetState();
1388 filter
.AddLine((*a
)->GetName());
1389 filter
.AddLine((*a
)->filename
);
1390 filter
.AddLine((*a
)->GetDescription());
1391 return filter
.GetState();;
1394 void BuildAvailables()
1396 if (!this->avails
.NeedRebuild()) return;
1398 this->avails
.Clear();
1400 for (const GRFConfig
*c
= _all_grfs
; c
!= NULL
; c
= c
->next
) {
1402 for (const GRFConfig
*grf
= this->actives
; grf
!= NULL
&& !found
; grf
= grf
->next
) found
= grf
->ident
.HasGrfIdentifier(c
->ident
.grfid
, c
->ident
.md5sum
);
1403 if (found
) continue;
1405 if (_settings_client
.gui
.newgrf_show_old_versions
) {
1406 *this->avails
.Append() = c
;
1408 const GRFConfig
*best
= FindGRFConfig(c
->ident
.grfid
, HasBit(c
->flags
, GCF_INVALID
) ? FGCM_NEWEST
: FGCM_NEWEST_VALID
);
1410 * If the best version is 0, then all NewGRF with this GRF ID
1411 * have version 0, so for backward compatibility reasons we
1412 * want to show them all.
1413 * If we are the best version, then we definitely want to
1414 * show that NewGRF!.
1416 if (best
->version
== 0 || best
->ident
.HasGrfIdentifier(c
->ident
.grfid
, c
->ident
.md5sum
)) {
1417 *this->avails
.Append() = c
;
1422 this->avails
.Filter(this->string_filter
);
1423 this->avails
.Compact();
1424 this->avails
.RebuildDone();
1425 this->avails
.Sort();
1427 if (this->avail_sel
!= NULL
) {
1428 this->avail_pos
= this->avails
.FindIndex(this->avail_sel
);
1429 if (this->avail_pos
< 0) this->avail_sel
= NULL
;
1432 this->vscroll2
->SetCount(this->avails
.Length()); // Update the scrollbar
1436 * Insert a GRF into the active list.
1437 * @param ins_pos Insert GRF at this position.
1438 * @return True if the GRF was successfully added.
1440 bool AddGRFToActive(int ins_pos
= -1)
1442 if (this->avail_sel
== NULL
|| !this->editable
|| HasBit(this->avail_sel
->flags
, GCF_INVALID
)) return false;
1445 GRFConfig
**entry
= NULL
;
1447 /* Find last entry in the list, checking for duplicate grfid on the way */
1448 for (list
= &this->actives
; *list
!= NULL
; list
= &(*list
)->next
, ins_pos
--) {
1449 if (ins_pos
== 0) entry
= list
; // Insert position? Save.
1450 if ((*list
)->ident
.grfid
== this->avail_sel
->ident
.grfid
) {
1451 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID
, INVALID_STRING_ID
, WL_INFO
);
1456 if (entry
== NULL
) entry
= list
;
1457 if (count
>= MAX_NEWGRFS
) {
1458 ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS
, INVALID_STRING_ID
, WL_INFO
);
1462 GRFConfig
*c
= new GRFConfig(*this->avail_sel
); // Copy GRF details from scanned list.
1463 c
->SetParameterDefaults();
1465 /* Insert GRF config to configuration list. */
1469 /* Select next (or previous, if last one) item in the list. */
1470 int new_pos
= this->avail_pos
+ 1;
1471 if (new_pos
>= (int)this->avails
.Length()) new_pos
= this->avail_pos
- 1;
1472 this->avail_pos
= new_pos
;
1473 if (new_pos
>= 0) this->avail_sel
= this->avails
[new_pos
];
1475 this->avails
.ForceRebuild();
1476 this->InvalidateData(GOID_NEWGRF_LIST_EDITED
);
1481 #if defined(ENABLE_NETWORK)
1483 * Show the content list window with all missing grfs from the given list.
1484 * @param list The list of grfs to check for missing / not exactly matching ones.
1486 void ShowMissingContentWindow(const GRFConfig
*list
)
1488 /* Only show the things in the current list, or everything when nothing's selected */
1490 for (const GRFConfig
*c
= list
; c
!= NULL
; c
= c
->next
) {
1491 if (c
->status
!= GCS_NOT_FOUND
&& !HasBit(c
->flags
, GCF_COMPATIBLE
)) continue;
1493 ContentInfo
*ci
= new ContentInfo();
1494 ci
->type
= CONTENT_TYPE_NEWGRF
;
1495 ci
->state
= ContentInfo::DOES_NOT_EXIST
;
1496 ttd_strlcpy(ci
->name
, c
->GetName(), lengthof(ci
->name
));
1497 ci
->unique_id
= BSWAP32(c
->ident
.grfid
);
1498 memcpy(ci
->md5sum
, HasBit(c
->flags
, GCF_COMPATIBLE
) ? c
->original_md5sum
: c
->ident
.md5sum
, sizeof(ci
->md5sum
));
1501 ShowNetworkContentListWindow(cv
.Length() == 0 ? NULL
: &cv
, CONTENT_TYPE_NEWGRF
);
1505 Listing
NewGRFWindow::last_sorting
= {false, 0};
1506 Filtering
NewGRFWindow::last_filtering
= {false, 0};
1508 NewGRFWindow::GUIGRFConfigList::SortFunction
* const NewGRFWindow::sorter_funcs
[] = {
1512 NewGRFWindow::GUIGRFConfigList::FilterFunction
* const NewGRFWindow::filter_funcs
[] = {
1517 * Custom nested widget container for the NewGRF gui.
1518 * Depending on the space in the gui, it uses either
1519 * - two column mode, put the #acs and the #avs underneath each other and the #info next to it, or
1520 * - three column mode, put the #avs, #acs, and #info each in its own column.
1522 class NWidgetNewGRFDisplay
: public NWidgetContainer
{
1524 static const uint INTER_LIST_SPACING
; ///< Empty vertical space between both lists in the 2 column mode.
1525 static const uint INTER_COLUMN_SPACING
; ///< Empty horizontal space between two columns.
1526 static const uint MAX_EXTRA_INFO_WIDTH
; ///< Maximal additional width given to the panel.
1527 static const uint MIN_EXTRA_FOR_3_COLUMNS
; ///< Minimal additional width needed before switching to 3 columns.
1529 NWidgetBase
*avs
; ///< Widget with the available grfs list and buttons.
1530 NWidgetBase
*acs
; ///< Widget with the active grfs list and buttons.
1531 NWidgetBase
*inf
; ///< Info panel.
1532 bool editable
; ///< Editable status of the parent NewGRF window (if \c false, drop all widgets that make the window editable).
1534 NWidgetNewGRFDisplay(NWidgetBase
*avs
, NWidgetBase
*acs
, NWidgetBase
*inf
) : NWidgetContainer(NWID_HORIZONTAL
)
1540 this->Add(this->avs
);
1541 this->Add(this->acs
);
1542 this->Add(this->inf
);
1544 this->editable
= true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1547 virtual void SetupSmallestSize(Window
*w
, bool init_array
)
1549 /* Copy state flag from the window. */
1550 assert(dynamic_cast<NewGRFWindow
*>(w
) != NULL
);
1551 NewGRFWindow
*ngw
= (NewGRFWindow
*)w
;
1552 this->editable
= ngw
->editable
;
1554 this->avs
->SetupSmallestSize(w
, init_array
);
1555 this->acs
->SetupSmallestSize(w
, init_array
);
1556 this->inf
->SetupSmallestSize(w
, init_array
);
1558 uint min_avs_width
= this->avs
->smallest_x
+ this->avs
->padding_left
+ this->avs
->padding_right
;
1559 uint min_acs_width
= this->acs
->smallest_x
+ this->acs
->padding_left
+ this->acs
->padding_right
;
1560 uint min_inf_width
= this->inf
->smallest_x
+ this->inf
->padding_left
+ this->inf
->padding_right
;
1562 uint min_avs_height
= this->avs
->smallest_y
+ this->avs
->padding_top
+ this->avs
->padding_bottom
;
1563 uint min_acs_height
= this->acs
->smallest_y
+ this->acs
->padding_top
+ this->acs
->padding_bottom
;
1564 uint min_inf_height
= this->inf
->smallest_y
+ this->inf
->padding_top
+ this->inf
->padding_bottom
;
1566 /* Smallest window is in two column mode. */
1567 this->smallest_x
= max(min_avs_width
, min_acs_width
) + INTER_COLUMN_SPACING
+ min_inf_width
;
1568 this->smallest_y
= max(min_inf_height
, min_acs_height
+ INTER_LIST_SPACING
+ min_avs_height
);
1571 this->fill_x
= LeastCommonMultiple(this->avs
->fill_x
, this->acs
->fill_x
);
1572 if (this->inf
->fill_x
> 0 && (this->fill_x
== 0 || this->fill_x
> this->inf
->fill_x
)) this->fill_x
= this->inf
->fill_x
;
1574 this->fill_y
= this->avs
->fill_y
;
1575 if (this->acs
->fill_y
> 0 && (this->fill_y
== 0 || this->fill_y
> this->acs
->fill_y
)) this->fill_y
= this->acs
->fill_y
;
1576 this->fill_y
= LeastCommonMultiple(this->fill_y
, this->inf
->fill_y
);
1579 this->resize_x
= LeastCommonMultiple(this->avs
->resize_x
, this->acs
->resize_x
);
1580 if (this->inf
->resize_x
> 0 && (this->resize_x
== 0 || this->resize_x
> this->inf
->resize_x
)) this->resize_x
= this->inf
->resize_x
;
1582 this->resize_y
= this->avs
->resize_y
;
1583 if (this->acs
->resize_y
> 0 && (this->resize_y
== 0 || this->resize_y
> this->acs
->resize_y
)) this->resize_y
= this->acs
->resize_y
;
1584 this->resize_y
= LeastCommonMultiple(this->resize_y
, this->inf
->resize_y
);
1586 /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1587 this->smallest_y
= ComputeMaxSize(min_acs_height
, this->smallest_y
+ this->resize_y
- 1, this->resize_y
);
1590 virtual void AssignSizePosition(SizingType sizing
, uint x
, uint y
, uint given_width
, uint given_height
, bool rtl
)
1592 this->StoreSizePosition(sizing
, x
, y
, given_width
, given_height
);
1594 uint min_avs_width
= this->avs
->smallest_x
+ this->avs
->padding_left
+ this->avs
->padding_right
;
1595 uint min_acs_width
= this->acs
->smallest_x
+ this->acs
->padding_left
+ this->acs
->padding_right
;
1596 uint min_inf_width
= this->inf
->smallest_x
+ this->inf
->padding_left
+ this->inf
->padding_right
;
1598 uint min_list_width
= max(min_avs_width
, min_acs_width
); // Smallest width of the lists such that they have equal width (incl padding).
1599 uint avs_extra_width
= min_list_width
- min_avs_width
; // Additional width needed for avs to reach min_list_width.
1600 uint acs_extra_width
= min_list_width
- min_acs_width
; // Additional width needed for acs to reach min_list_width.
1602 /* Use 2 or 3 columns? */
1603 uint min_three_columns
= min_avs_width
+ min_acs_width
+ min_inf_width
+ 2 * INTER_COLUMN_SPACING
;
1604 uint min_two_columns
= min_list_width
+ min_inf_width
+ INTER_COLUMN_SPACING
;
1605 bool use_three_columns
= this->editable
&& (min_three_columns
+ MIN_EXTRA_FOR_3_COLUMNS
<= given_width
);
1607 /* Info panel is a separate column in both modes. Compute its width first. */
1608 uint extra_width
, inf_width
;
1609 if (use_three_columns
) {
1610 extra_width
= given_width
- min_three_columns
;
1611 inf_width
= min(MAX_EXTRA_INFO_WIDTH
, extra_width
/ 2);
1613 extra_width
= given_width
- min_two_columns
;
1614 inf_width
= min(MAX_EXTRA_INFO_WIDTH
, extra_width
/ 2);
1616 inf_width
= ComputeMaxSize(this->inf
->smallest_x
, this->inf
->smallest_x
+ inf_width
, this->inf
->GetHorizontalStepSize(sizing
));
1617 extra_width
-= inf_width
- this->inf
->smallest_x
;
1619 uint inf_height
= ComputeMaxSize(this->inf
->smallest_y
, given_height
, this->inf
->GetVerticalStepSize(sizing
));
1621 if (use_three_columns
) {
1622 /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1623 * Only keep track of what avs gets, all other space goes to acs. */
1624 uint avs_width
= min(avs_extra_width
, extra_width
);
1625 extra_width
-= avs_width
;
1626 extra_width
-= min(acs_extra_width
, extra_width
);
1627 avs_width
+= extra_width
/ 2;
1629 avs_width
= ComputeMaxSize(this->avs
->smallest_x
, this->avs
->smallest_x
+ avs_width
, this->avs
->GetHorizontalStepSize(sizing
));
1631 uint acs_width
= given_width
- // Remaining space, including horizontal padding.
1632 inf_width
- this->inf
->padding_left
- this->inf
->padding_right
-
1633 avs_width
- this->avs
->padding_left
- this->avs
->padding_right
- 2 * INTER_COLUMN_SPACING
;
1634 acs_width
= ComputeMaxSize(min_acs_width
, acs_width
, this->acs
->GetHorizontalStepSize(sizing
)) -
1635 this->acs
->padding_left
- this->acs
->padding_right
;
1637 /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1638 uint avs_height
= ComputeMaxSize(this->avs
->smallest_y
, given_height
, this->avs
->resize_y
);
1639 uint acs_height
= ComputeMaxSize(this->acs
->smallest_y
, given_height
, this->acs
->resize_y
);
1641 /* Assign size and position to the children. */
1643 x
+= this->inf
->padding_left
;
1644 this->inf
->AssignSizePosition(sizing
, x
, y
+ this->inf
->padding_top
, inf_width
, inf_height
, rtl
);
1645 x
+= inf_width
+ this->inf
->padding_right
+ INTER_COLUMN_SPACING
;
1647 x
+= this->avs
->padding_left
;
1648 this->avs
->AssignSizePosition(sizing
, x
, y
+ this->avs
->padding_top
, avs_width
, avs_height
, rtl
);
1649 x
+= avs_width
+ this->avs
->padding_right
+ INTER_COLUMN_SPACING
;
1652 x
+= this->acs
->padding_left
;
1653 this->acs
->AssignSizePosition(sizing
, x
, y
+ this->acs
->padding_top
, acs_width
, acs_height
, rtl
);
1654 x
+= acs_width
+ this->acs
->padding_right
+ INTER_COLUMN_SPACING
;
1657 x
+= this->avs
->padding_left
;
1658 this->avs
->AssignSizePosition(sizing
, x
, y
+ this->avs
->padding_top
, avs_width
, avs_height
, rtl
);
1660 x
+= this->inf
->padding_left
;
1661 this->inf
->AssignSizePosition(sizing
, x
, y
+ this->inf
->padding_top
, inf_width
, inf_height
, rtl
);
1664 /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1665 * the column is min_list_width wide at least. */
1666 uint avs_width
= ComputeMaxSize(this->avs
->smallest_x
, this->avs
->smallest_x
+ avs_extra_width
+ extra_width
,
1667 this->avs
->GetHorizontalStepSize(sizing
));
1668 uint acs_width
= ComputeMaxSize(this->acs
->smallest_x
, this->acs
->smallest_x
+ acs_extra_width
+ extra_width
,
1669 this->acs
->GetHorizontalStepSize(sizing
));
1671 uint min_avs_height
= (!this->editable
) ? 0 : this->avs
->smallest_y
+ this->avs
->padding_top
+ this->avs
->padding_bottom
+ INTER_LIST_SPACING
;
1672 uint min_acs_height
= this->acs
->smallest_y
+ this->acs
->padding_top
+ this->acs
->padding_bottom
;
1673 uint extra_height
= given_height
- min_acs_height
- min_avs_height
;
1675 /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1676 uint avs_height
= ComputeMaxSize(this->avs
->smallest_y
, this->avs
->smallest_y
+ extra_height
/ 2, this->avs
->resize_y
);
1677 if (this->editable
) extra_height
-= avs_height
- this->avs
->smallest_y
;
1678 uint acs_height
= ComputeMaxSize(this->acs
->smallest_y
, this->acs
->smallest_y
+ extra_height
, this->acs
->resize_y
);
1680 /* Assign size and position to the children. */
1682 x
+= this->inf
->padding_left
;
1683 this->inf
->AssignSizePosition(sizing
, x
, y
+ this->inf
->padding_top
, inf_width
, inf_height
, rtl
);
1684 x
+= inf_width
+ this->inf
->padding_right
+ INTER_COLUMN_SPACING
;
1686 this->acs
->AssignSizePosition(sizing
, x
+ this->acs
->padding_left
, y
+ this->acs
->padding_top
, acs_width
, acs_height
, rtl
);
1687 if (this->editable
) {
1688 this->avs
->AssignSizePosition(sizing
, x
+ this->avs
->padding_left
, y
+ given_height
- avs_height
- this->avs
->padding_bottom
, avs_width
, avs_height
, rtl
);
1690 this->avs
->AssignSizePosition(sizing
, 0, 0, this->avs
->smallest_x
, this->avs
->smallest_y
, rtl
);
1693 this->acs
->AssignSizePosition(sizing
, x
+ this->acs
->padding_left
, y
+ this->acs
->padding_top
, acs_width
, acs_height
, rtl
);
1694 if (this->editable
) {
1695 this->avs
->AssignSizePosition(sizing
, x
+ this->avs
->padding_left
, y
+ given_height
- avs_height
- this->avs
->padding_bottom
, avs_width
, avs_height
, rtl
);
1697 this->avs
->AssignSizePosition(sizing
, 0, 0, this->avs
->smallest_x
, this->avs
->smallest_y
, rtl
);
1699 uint dx
= this->acs
->current_x
+ this->acs
->padding_left
+ this->acs
->padding_right
;
1700 if (this->editable
) {
1701 dx
= max(dx
, this->avs
->current_x
+ this->avs
->padding_left
+ this->avs
->padding_right
);
1703 x
+= dx
+ INTER_COLUMN_SPACING
+ this->inf
->padding_left
;
1704 this->inf
->AssignSizePosition(sizing
, x
, y
+ this->inf
->padding_top
, inf_width
, inf_height
, rtl
);
1709 virtual NWidgetCore
*GetWidgetFromPos(int x
, int y
)
1711 if (!IsInsideBS(x
, this->pos_x
, this->current_x
) || !IsInsideBS(y
, this->pos_y
, this->current_y
)) return NULL
;
1713 NWidgetCore
*nw
= (this->editable
) ? this->avs
->GetWidgetFromPos(x
, y
) : NULL
;
1714 if (nw
== NULL
) nw
= this->acs
->GetWidgetFromPos(x
, y
);
1715 if (nw
== NULL
) nw
= this->inf
->GetWidgetFromPos(x
, y
);
1719 virtual void Draw(const Window
*w
)
1721 if (this->editable
) this->avs
->Draw(w
);
1727 const uint
NWidgetNewGRFDisplay::INTER_LIST_SPACING
= WD_RESIZEBOX_WIDTH
+ 1;
1728 const uint
NWidgetNewGRFDisplay::INTER_COLUMN_SPACING
= WD_RESIZEBOX_WIDTH
;
1729 const uint
NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH
= 150;
1730 const uint
NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS
= 50;
1732 static const NWidgetPart _nested_newgrf_actives_widgets
[] = {
1733 /* Left side, presets. */
1734 NWidget(NWID_HORIZONTAL
),
1735 NWidget(WWT_TEXT
, COLOUR_MAUVE
), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET
, STR_NULL
),
1736 SetPadding(0, WD_FRAMETEXT_RIGHT
, 0, 0),
1737 NWidget(WWT_DROPDOWN
, COLOUR_YELLOW
, WID_NS_PRESET_LIST
), SetFill(1, 0), SetResize(1, 0),
1738 SetDataTip(STR_JUST_STRING
, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP
),
1740 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1741 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_PRESET_SAVE
), SetFill(1, 0), SetResize(1, 0),
1742 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE
, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP
),
1743 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_PRESET_DELETE
), SetFill(1, 0), SetResize(1, 0),
1744 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE
, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP
),
1747 NWidget(NWID_SPACER
), SetMinimalSize(0, WD_RESIZEBOX_WIDTH
), SetResize(1, 0), SetFill(1, 0),
1748 NWidget(WWT_PANEL
, COLOUR_MAUVE
),
1749 NWidget(WWT_LABEL
, COLOUR_MAUVE
), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST
, STR_NULL
),
1750 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT
, 0, WD_FRAMETEXT_LEFT
),
1751 /* Left side, active grfs. */
1752 NWidget(NWID_HORIZONTAL
), SetPadding(0, 2, 0, 2),
1753 NWidget(WWT_PANEL
, COLOUR_MAUVE
),
1754 NWidget(WWT_INSET
, COLOUR_MAUVE
, WID_NS_FILE_LIST
), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1755 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR
), SetDataTip(STR_NULL
, STR_NEWGRF_SETTINGS_FILE_TOOLTIP
),
1758 NWidget(NWID_VSCROLLBAR
, COLOUR_MAUVE
, WID_NS_SCROLLBAR
),
1761 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NS_SHOW_REMOVE
),
1762 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH
, 0),
1763 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_REMOVE
), SetFill(1, 0), SetResize(1, 0),
1764 SetDataTip(STR_NEWGRF_SETTINGS_REMOVE
, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP
),
1765 NWidget(NWID_VERTICAL
),
1766 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_MOVE_UP
), SetFill(1, 0), SetResize(1, 0),
1767 SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP
, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP
),
1768 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_MOVE_DOWN
), SetFill(1, 0), SetResize(1, 0),
1769 SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN
, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP
),
1773 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
), SetPadding(2, 2, 2, 2),
1774 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_RESCAN_FILES2
), SetFill(1, 0), SetResize(1, 0),
1775 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES
, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP
),
1776 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_CONTENT_DOWNLOAD2
), SetFill(1, 0), SetResize(1, 0),
1777 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
1783 static const NWidgetPart _nested_newgrf_availables_widgets
[] = {
1784 NWidget(WWT_PANEL
, COLOUR_MAUVE
),
1785 NWidget(WWT_LABEL
, COLOUR_MAUVE
), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST
, STR_NULL
),
1786 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT
, 0, WD_FRAMETEXT_LEFT
),
1787 /* Left side, available grfs, filter edit box. */
1788 NWidget(NWID_HORIZONTAL
), SetPadding(WD_TEXTPANEL_TOP
, 0, WD_TEXTPANEL_BOTTOM
, 0),
1789 SetPIP(WD_FRAMETEXT_LEFT
, WD_FRAMETEXT_RIGHT
, WD_FRAMETEXT_RIGHT
),
1790 NWidget(WWT_TEXT
, COLOUR_MAUVE
), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE
, STR_NULL
),
1791 NWidget(WWT_EDITBOX
, COLOUR_MAUVE
, WID_NS_FILTER
), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1792 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
1794 /* Left side, available grfs. */
1795 NWidget(NWID_HORIZONTAL
), SetPadding(0, 2, 0, 2),
1796 NWidget(WWT_PANEL
, COLOUR_MAUVE
),
1797 NWidget(WWT_INSET
, COLOUR_MAUVE
, WID_NS_AVAIL_LIST
), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1798 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR
),
1801 NWidget(NWID_VSCROLLBAR
, COLOUR_MAUVE
, WID_NS_SCROLL2BAR
),
1803 /* Left side, available grfs, buttons. */
1804 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH
, 0),
1805 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_ADD
), SetFill(1, 0), SetResize(1, 0),
1806 SetDataTip(STR_NEWGRF_SETTINGS_ADD
, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP
),
1807 NWidget(NWID_VERTICAL
),
1808 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_RESCAN_FILES
), SetFill(1, 0), SetResize(1, 0),
1809 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES
, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP
),
1810 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_CONTENT_DOWNLOAD
), SetFill(1, 0), SetResize(1, 0),
1811 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
1817 static const NWidgetPart _nested_newgrf_infopanel_widgets
[] = {
1818 /* Right side, info panel. */
1819 NWidget(NWID_VERTICAL
), SetPadding(0, 0, 2, 0),
1820 NWidget(WWT_PANEL
, COLOUR_MAUVE
), SetPadding(0, 0, 2, 0),
1821 NWidget(WWT_EMPTY
, COLOUR_MAUVE
, WID_NS_NEWGRF_INFO_TITLE
), SetFill(1, 0), SetResize(1, 0),
1822 NWidget(WWT_EMPTY
, COLOUR_MAUVE
, WID_NS_NEWGRF_INFO
), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1824 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_OPEN_URL
), SetFill(1, 0), SetResize(1, 0),
1825 SetDataTip(STR_CONTENT_OPEN_URL
, STR_CONTENT_OPEN_URL_TOOLTIP
),
1826 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_NEWGRF_TEXTFILE
+ TFT_README
), SetFill(1, 0), SetResize(1, 0),
1827 SetDataTip(STR_TEXTFILE_VIEW_README
, STR_NULL
),
1828 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
1829 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_NEWGRF_TEXTFILE
+ TFT_CHANGELOG
), SetFill(1, 0), SetResize(1, 0),
1830 SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG
, STR_NULL
),
1831 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_NEWGRF_TEXTFILE
+ TFT_LICENSE
), SetFill(1, 0), SetResize(1, 0),
1832 SetDataTip(STR_TEXTFILE_VIEW_LICENCE
, STR_NULL
),
1835 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_NS_SHOW_APPLY
),
1836 /* Right side, buttons. */
1837 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(0, WD_RESIZEBOX_WIDTH
, 0),
1838 NWidget(NWID_VERTICAL
),
1839 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_SET_PARAMETERS
), SetFill(1, 0), SetResize(1, 0),
1840 SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS
, STR_NULL
),
1841 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_TOGGLE_PALETTE
), SetFill(1, 0), SetResize(1, 0),
1842 SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE
, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP
),
1844 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_APPLY_CHANGES
), SetFill(1, 0), SetResize(1, 0),
1845 SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES
, STR_NULL
),
1847 NWidget(WWT_PUSHTXTBTN
, COLOUR_YELLOW
, WID_NS_VIEW_PARAMETERS
), SetFill(1, 0), SetResize(1, 0),
1848 SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS
, STR_NULL
),
1852 /** Construct nested container widget for managing the lists and the info panel of the NewGRF GUI. */
1853 NWidgetBase
* NewGRFDisplay(int *biggest_index
)
1855 NWidgetBase
*avs
= MakeNWidgets(_nested_newgrf_availables_widgets
, lengthof(_nested_newgrf_availables_widgets
), biggest_index
, NULL
);
1858 NWidgetBase
*acs
= MakeNWidgets(_nested_newgrf_actives_widgets
, lengthof(_nested_newgrf_actives_widgets
), &biggest2
, NULL
);
1859 *biggest_index
= max(*biggest_index
, biggest2
);
1861 NWidgetBase
*inf
= MakeNWidgets(_nested_newgrf_infopanel_widgets
, lengthof(_nested_newgrf_infopanel_widgets
), &biggest2
, NULL
);
1862 *biggest_index
= max(*biggest_index
, biggest2
);
1864 return new NWidgetNewGRFDisplay(avs
, acs
, inf
);
1867 /* Widget definition of the manage newgrfs window */
1868 static const NWidgetPart _nested_newgrf_widgets
[] = {
1869 NWidget(NWID_HORIZONTAL
),
1870 NWidget(WWT_CLOSEBOX
, COLOUR_MAUVE
),
1871 NWidget(WWT_CAPTION
, COLOUR_MAUVE
), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1872 NWidget(WWT_DEFSIZEBOX
, COLOUR_MAUVE
),
1874 NWidget(WWT_PANEL
, COLOUR_MAUVE
),
1875 NWidgetFunction(NewGRFDisplay
), SetPadding(WD_RESIZEBOX_WIDTH
, WD_RESIZEBOX_WIDTH
, 2, WD_RESIZEBOX_WIDTH
),
1876 /* Resize button. */
1877 NWidget(NWID_HORIZONTAL
),
1878 NWidget(NWID_SPACER
), SetFill(1, 0), SetResize(1, 0),
1879 NWidget(WWT_RESIZEBOX
, COLOUR_MAUVE
),
1884 /* Window definition of the manage newgrfs window */
1885 static WindowDesc
_newgrf_desc(
1886 WDP_CENTER
, "settings_newgrf", 300, 263,
1887 WC_GAME_OPTIONS
, WC_NONE
,
1889 _nested_newgrf_widgets
, lengthof(_nested_newgrf_widgets
)
1893 * Callback function for the newgrf 'apply changes' confirmation window
1894 * @param w Window which is calling this callback
1895 * @param confirmed boolean value, true when yes was clicked, false otherwise
1897 static void NewGRFConfirmationCallback(Window
*w
, bool confirmed
)
1900 DeleteWindowByClass(WC_GRF_PARAMETERS
);
1901 NewGRFWindow
*nw
= dynamic_cast<NewGRFWindow
*>(w
);
1904 GamelogGRFUpdate(_grfconfig
, nw
->actives
); // log GRF changes
1905 CopyGRFConfigList(nw
->orig_list
, nw
->actives
, false);
1909 /* Show new, updated list */
1912 for (c
= nw
->actives
; c
!= NULL
&& c
!= nw
->active_sel
; c
= c
->next
, i
++) {}
1913 CopyGRFConfigList(&nw
->actives
, *nw
->orig_list
, false);
1914 for (c
= nw
->actives
; c
!= NULL
&& i
> 0; c
= c
->next
, i
--) {}
1916 nw
->avails
.ForceRebuild();
1918 w
->InvalidateData();
1921 DeleteWindowByClass(WC_BUILD_OBJECT
);
1928 * Setup the NewGRF gui
1929 * @param editable allow the user to make changes to the grfconfig in the window
1930 * @param show_params show information about what parameters are set for the grf files
1931 * @param exec_changes if changes are made to the list (editable is true), apply these
1932 * changes immediately or only update the list
1933 * @param config pointer to a linked-list of grfconfig's that will be shown
1935 void ShowNewGRFSettings(bool editable
, bool show_params
, bool exec_changes
, GRFConfig
**config
)
1937 DeleteWindowByClass(WC_GAME_OPTIONS
);
1938 new NewGRFWindow(&_newgrf_desc
, editable
, show_params
, exec_changes
, config
);
1941 /** Widgets for the progress window. */
1942 static const NWidgetPart _nested_scan_progress_widgets
[] = {
1943 NWidget(WWT_CAPTION
, COLOUR_GREY
), SetDataTip(STR_NEWGRF_SCAN_CAPTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
1944 NWidget(WWT_PANEL
, COLOUR_GREY
),
1945 NWidget(NWID_HORIZONTAL
), SetPIP(20, 0, 20),
1946 NWidget(NWID_VERTICAL
), SetPIP(11, 8, 11),
1947 NWidget(WWT_LABEL
, INVALID_COLOUR
), SetDataTip(STR_NEWGRF_SCAN_MESSAGE
, STR_NULL
), SetFill(1, 0),
1948 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_SP_PROGRESS_BAR
), SetFill(1, 0),
1949 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_SP_PROGRESS_TEXT
), SetFill(1, 0),
1955 /** Description of the widgets and other settings of the window. */
1956 static WindowDesc
_scan_progress_desc(
1957 WDP_CENTER
, NULL
, 0, 0,
1958 WC_MODAL_PROGRESS
, WC_NONE
,
1960 _nested_scan_progress_widgets
, lengthof(_nested_scan_progress_widgets
)
1963 /** Window for showing the progress of NewGRF scanning. */
1964 struct ScanProgressWindow
: public Window
{
1965 char *last_name
; ///< The name of the last 'seen' NewGRF.
1966 int scanned
; ///< The number of NewGRFs that we have seen.
1968 /** Create the window. */
1969 ScanProgressWindow() : Window(&_scan_progress_desc
), last_name(NULL
), scanned(0)
1971 this->InitNested(1);
1974 /** Free the last name buffer. */
1975 ~ScanProgressWindow()
1980 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
1983 case WID_SP_PROGRESS_BAR
: {
1984 SetDParamMaxValue(0, 100);
1985 *size
= GetStringBoundingBox(STR_GENERATION_PROGRESS
);
1986 /* We need some spacing for the 'border' */
1992 case WID_SP_PROGRESS_TEXT
:
1993 SetDParamMaxDigits(0, 4);
1994 SetDParamMaxDigits(1, 4);
1995 /* We really don't know the width. We could determine it by scanning the NewGRFs,
1996 * but this is the status window for scanning them... */
1997 size
->width
= max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS
).width
);
1998 size
->height
= FONT_HEIGHT_NORMAL
* 2 + WD_PAR_VSEP_NORMAL
;
2003 virtual void DrawWidget(const Rect
&r
, int widget
) const
2006 case WID_SP_PROGRESS_BAR
: {
2007 /* Draw the % complete with a bar and a text */
2008 DrawFrameRect(r
.left
, r
.top
, r
.right
, r
.bottom
, COLOUR_GREY
, FR_BORDERONLY
);
2009 uint percent
= scanned
* 100 / max(1U, _settings_client
.gui
.last_newgrf_count
);
2010 DrawFrameRect(r
.left
+ 1, r
.top
+ 1, (int)((r
.right
- r
.left
- 2) * percent
/ 100) + r
.left
+ 1, r
.bottom
- 1, COLOUR_MAUVE
, FR_NONE
);
2011 SetDParam(0, percent
);
2012 DrawString(r
.left
, r
.right
, r
.top
+ 5, STR_GENERATION_PROGRESS
, TC_FROMSTRING
, SA_HOR_CENTER
);
2016 case WID_SP_PROGRESS_TEXT
:
2017 SetDParam(0, this->scanned
);
2018 SetDParam(1, _settings_client
.gui
.last_newgrf_count
);
2019 DrawString(r
.left
, r
.right
, r
.top
, STR_NEWGRF_SCAN_STATUS
, TC_FROMSTRING
, SA_HOR_CENTER
);
2021 DrawString(r
.left
, r
.right
, r
.top
+ FONT_HEIGHT_NORMAL
+ WD_PAR_VSEP_NORMAL
, this->last_name
== NULL
? "" : this->last_name
, TC_BLACK
, SA_HOR_CENTER
);
2027 * Update the NewGRF scan status.
2028 * @param num The number of NewGRFs scanned so far.
2029 * @param name The name of the last scanned NewGRF.
2031 void UpdateNewGRFScanStatus(uint num
, const char *name
)
2033 free(this->last_name
);
2036 GetString(buf
, STR_NEWGRF_SCAN_ARCHIVES
, lastof(buf
));
2037 this->last_name
= strdup(buf
);
2039 this->last_name
= strdup(name
);
2041 this->scanned
= num
;
2042 if (num
> _settings_client
.gui
.last_newgrf_count
) _settings_client
.gui
.last_newgrf_count
= num
;
2049 * Update the NewGRF scan status.
2050 * @param num The number of NewGRFs scanned so far.
2051 * @param name The name of the last scanned NewGRF.
2053 void UpdateNewGRFScanStatus(uint num
, const char *name
)
2055 ScanProgressWindow
*w
= dynamic_cast<ScanProgressWindow
*>(FindWindowByClass(WC_MODAL_PROGRESS
));
2056 if (w
== NULL
) w
= new ScanProgressWindow();
2057 w
->UpdateNewGRFScanStatus(num
, name
);