~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/autoreplace_gui.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: autoreplace_gui.cpp 12054 2008-02-04 11:28:12Z peter1138 $ */
 
1
/* $Id: autoreplace_gui.cpp 15723 2009-03-15 15:12:06Z rubidium $ */
2
2
 
3
 
/** @file autoreplace_gui.cpp */
 
3
/** @file autoreplace_gui.cpp GUI for autoreplace handling. */
4
4
 
5
5
#include "stdafx.h"
6
 
#include "openttd.h"
7
 
#include "debug.h"
8
 
#include "gui.h"
9
6
#include "command_func.h"
10
 
#include "variables.h"
11
7
#include "vehicle_gui.h"
12
8
#include "newgrf_engine.h"
13
9
#include "group.h"
14
10
#include "rail.h"
15
11
#include "strings_func.h"
16
12
#include "window_func.h"
17
 
#include "vehicle_func.h"
18
13
#include "autoreplace_func.h"
19
14
#include "gfx_func.h"
20
 
#include "player_func.h"
21
 
#include "widgets/dropdown_func.h"
 
15
#include "company_func.h"
 
16
#include "widgets/dropdown_type.h"
 
17
#include "engine_base.h"
 
18
#include "window_gui.h"
 
19
#include "engine_gui.h"
22
20
 
23
 
#include "table/sprites.h"
24
21
#include "table/strings.h"
25
22
 
26
 
static RailType _railtype_selected_in_replace_gui;
27
 
 
28
 
static bool _rebuild_left_list;
29
 
static bool _rebuild_right_list;
30
 
 
31
 
static const StringID _rail_types_list[] = {
32
 
        STR_RAIL_VEHICLES,
33
 
        STR_ELRAIL_VEHICLES,
34
 
        STR_MONORAIL_VEHICLES,
35
 
        STR_MAGLEV_VEHICLES,
36
 
        INVALID_STRING_ID
37
 
};
 
23
void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group);
38
24
 
39
25
enum ReplaceVehicleWindowWidgets {
40
 
        RVW_WIDGET_LEFT_DETAILS = 3,
41
 
        RVW_WIDGET_START_REPLACE,
42
 
        RVW_WIDGET_INFO_TAB,
43
 
        RVW_WIDGET_STOP_REPLACE,
44
 
        RVW_WIDGET_LEFT_MATRIX,
 
26
        RVW_WIDGET_LEFT_MATRIX = 3,
45
27
        RVW_WIDGET_LEFT_SCROLLBAR,
46
28
        RVW_WIDGET_RIGHT_MATRIX,
47
29
        RVW_WIDGET_RIGHT_SCROLLBAR,
 
30
        RVW_WIDGET_LEFT_DETAILS,
48
31
        RVW_WIDGET_RIGHT_DETAILS,
49
32
 
 
33
        /* Button row */
 
34
        RVW_WIDGET_START_REPLACE,
 
35
        RVW_WIDGET_INFO_TAB,
 
36
        RVW_WIDGET_STOP_REPLACE,
 
37
        RVW_WIDGET_RESIZE,
 
38
 
 
39
        /* Train only widgets */
50
40
        RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE,
51
41
        RVW_WIDGET_TRAIN_FLUFF_LEFT,
52
42
        RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN,
54
44
        RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE,
55
45
};
56
46
 
57
 
static int CDECL TrainEngineNumberSorter(const void *a, const void *b)
 
47
static int CDECL EngineNumberSorter(const void *a, const void *b)
58
48
{
59
49
        const EngineID va = *(const EngineID*)a;
60
50
        const EngineID vb = *(const EngineID*)b;
63
53
        return r;
64
54
}
65
55
 
66
 
/* General Vehicle GUI based procedures that are independent of vehicle types */
67
 
void InitializeVehiclesGuiList()
68
 
{
69
 
        _railtype_selected_in_replace_gui = RAILTYPE_RAIL;
70
 
}
71
 
 
72
56
/** Rebuild the left autoreplace list if an engine is removed or added
73
57
 * @param e Engine to check if it is removed or added
74
58
 * @param id_g The group the engine belongs to
79
63
 */
80
64
void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
81
65
{
82
 
        Player *p = GetPlayer(_local_player);
83
 
        VehicleType type = GetEngine(e)->type;
84
 
        uint num_engines = GetGroupNumEngines(_local_player, id_g, e);
 
66
        Company *c = GetCompany(_local_company);
 
67
        uint num_engines = GetGroupNumEngines(_local_company, id_g, e);
85
68
 
86
 
        if (num_engines == 0 || p->num_engines[e] == 0) {
 
69
        if (num_engines == 0 || c->num_engines[e] == 0) {
87
70
                /* We don't have any of this engine type.
88
71
                 * Either we just sold the last one, we build a new one or we stopped replacing it.
89
72
                 * In all cases, we need to update the left list */
90
 
                _rebuild_left_list = true;
91
 
        } else {
92
 
                _rebuild_left_list = false;
 
73
                InvalidateWindowData(WC_REPLACE_VEHICLE, GetEngine(e)->type, true);
93
74
        }
94
 
        _rebuild_right_list = false;
95
 
        InvalidateWindowData(WC_REPLACE_VEHICLE, type);
96
75
}
97
76
 
98
77
/** When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
100
79
 */
101
80
void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
102
81
{
103
 
        _rebuild_left_list = false; // left list is only for the vehicles the player owns and is not related to being buildable
104
 
        _rebuild_right_list = true;
105
 
        InvalidateWindowData(WC_REPLACE_VEHICLE, type); // Update the autoreplace window
 
82
        InvalidateWindowData(WC_REPLACE_VEHICLE, type, false); // Update the autoreplace window
106
83
        InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
107
84
}
108
85
 
109
 
/** Get the default cargo type for an engine
110
 
 * @param engine the EngineID to get the cargo for
111
 
 * @return the cargo type carried by the engine (CT_INVALID if engine got no cargo capacity)
112
 
 */
113
 
static CargoID EngineCargo(EngineID engine)
114
 
{
115
 
        if (engine == INVALID_ENGINE) return CT_INVALID; // surely INVALID_ENGINE can't carry anything but CT_INVALID
116
 
 
117
 
        switch (GetEngine(engine)->type) {
118
 
                default: NOT_REACHED();
119
 
                case VEH_TRAIN:
120
 
                        if (RailVehInfo(engine)->capacity == 0) return CT_INVALID; // no capacity -> can't carry cargo
121
 
                        return RailVehInfo(engine)->cargo_type;
122
 
                case VEH_ROAD:       return RoadVehInfo(engine)->cargo_type;
123
 
                case VEH_SHIP:       return ShipVehInfo(engine)->cargo_type;
124
 
                case VEH_AIRCRAFT:   return CT_PASSENGERS; // all planes are build with passengers by default
125
 
        }
126
 
}
127
 
 
128
 
/** Figure out if an engine should be added to a list
129
 
 * @param e The EngineID
130
 
 * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected)
131
 
 * @param show_engines if truem then locomotives are drawn, else wagons (never both)
132
 
 * @return true if the engine should be in the list (based on this check)
133
 
 */
134
 
static bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
135
 
{
136
 
        const RailVehicleInfo *rvi = RailVehInfo(e);
137
 
 
138
 
        /* Ensure that the wagon/engine selection fits the engine. */
139
 
        if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
140
 
 
141
 
        if (draw_left && show_engines) {
142
 
                /* Ensure that the railtype is specific to the selected one */
143
 
                if (rvi->railtype != _railtype_selected_in_replace_gui) return false;
144
 
        } else {
145
 
                /* Ensure that it's a compatible railtype to the selected one (like electric <-> diesel)
146
 
                 * The vehicle do not have to have power on the railtype in question, only able to drive (pulled if needed) */
147
 
                if (!IsCompatibleRail(rvi->railtype, _railtype_selected_in_replace_gui)) return false;
148
 
        }
149
 
        return true;
150
 
}
151
 
 
152
 
/** Figure out if two engines got at least one type of cargo in common (refitting if needed)
153
 
 * @param engine_a one of the EngineIDs
154
 
 * @param engine_b the other EngineID
155
 
 * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
156
 
 */
157
 
static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b)
158
 
{
159
 
        CargoID a = EngineCargo(engine_a);
160
 
        CargoID b = EngineCargo(engine_b);
161
 
 
162
 
         /* we should always be able to refit to/from locomotives without capacity
163
 
          * Because of that, CT_INVALID shoudl always return true */
164
 
        if (a == CT_INVALID || b == CT_INVALID || a == b) return true; // they carry no ro the same type by default
165
 
        if (EngInfo(engine_a)->refit_mask & EngInfo(engine_b)->refit_mask) return true; // both can refit to the same
166
 
        if (CanRefitTo(engine_a, b) || CanRefitTo(engine_b, a)) return true; // one can refit to what the other one carries
167
 
        return false;
168
 
}
169
 
 
170
 
/** Generate a list
171
 
 * @param w Window, that contains the list
172
 
 * @param draw_left true if generating the left list, otherwise false
173
 
 */
174
 
static void GenerateReplaceVehList(Window *w, bool draw_left)
175
 
{
176
 
        EngineID e;
177
 
        EngineID selected_engine = INVALID_ENGINE;
178
 
        VehicleType type = (VehicleType)w->window_number;
179
 
        byte i = draw_left ? 0 : 1;
180
 
 
181
 
        EngineList *list = &WP(w, replaceveh_d).list[i];
182
 
        EngList_RemoveAll(list);
183
 
 
184
 
        FOR_ALL_ENGINEIDS_OF_TYPE(e, type) {
185
 
                if (type == VEH_TRAIN && !GenerateReplaceRailList(e, draw_left, WP(w, replaceveh_d).wagon_btnstate)) continue; // special rules for trains
186
 
 
187
 
                if (draw_left) {
188
 
                        const GroupID selected_group = WP(w, replaceveh_d).sel_group;
189
 
                        const uint num_engines = GetGroupNumEngines(_local_player, selected_group, e);
190
 
 
191
 
                        /* Skip drawing the engines we don't have any of and haven't set for replacement */
192
 
                        if (num_engines == 0 && EngineReplacementForPlayer(GetPlayer(_local_player), e, selected_group) == INVALID_ENGINE) continue;
193
 
                } else {
194
 
                        /* This is for engines we can replace to and they should depend on what we selected to replace from */
195
 
                        if (!IsEngineBuildable(e, type, _local_player)) continue; // we need to be able to build the engine
196
 
                        if (!EnginesGotCargoInCommon(e, WP(w, replaceveh_d).sel_engine[0])) continue; // the engines needs to be able to carry the same cargo
197
 
 
198
 
                        /* Road vehicles can't be replaced by trams and vice-versa */
199
 
                        if (type == VEH_ROAD && HasBit(EngInfo(WP(w, replaceveh_d).sel_engine[0])->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(e)->misc_flags, EF_ROAD_TRAM)) continue;
200
 
                        if (e == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
201
 
                }
202
 
 
203
 
                EngList_Add(list, e);
204
 
                if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list
205
 
        }
206
 
        WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
207
 
        if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter);
208
 
}
209
 
 
210
 
/** Generate the lists
211
 
 * @param w Window containing the lists
212
 
 */
213
 
static void GenerateLists(Window *w)
214
 
{
215
 
        EngineID e = WP(w, replaceveh_d).sel_engine[0];
216
 
 
217
 
        if (WP(w, replaceveh_d).update_left == true) {
218
 
                /* We need to rebuild the left list */
219
 
                GenerateReplaceVehList(w, true);
220
 
                SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0]));
221
 
                if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[0]) != 0) {
222
 
                        WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0];
223
 
                }
224
 
        }
225
 
 
226
 
        if (WP(w, replaceveh_d).update_right || e != WP(w, replaceveh_d).sel_engine[0]) {
227
 
                /* Either we got a request to rebuild the right list or the left list selected a different engine */
228
 
                if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) {
229
 
                        /* Always empty the right list when nothing is selected in the left list */
230
 
                        EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
231
 
                        WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
232
 
                } else {
233
 
                        GenerateReplaceVehList(w, false);
234
 
                        SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1]));
235
 
                        if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[1]) != 0) {
236
 
                                WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0];
237
 
                        }
238
 
                }
239
 
        }
240
 
        /* Reset the flags about needed updates */
241
 
        WP(w, replaceveh_d).update_left  = false;
242
 
        WP(w, replaceveh_d).update_right = false;
243
 
        WP(w, replaceveh_d).init_lists   = false;
244
 
}
245
 
 
246
 
 
247
 
void DrawEngineList(VehicleType type, int x, int y, const EngineList eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
248
 
 
249
 
static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
250
 
{
251
 
        /* Strings for the pulldown menu */
252
 
        static const StringID _vehicle_type_names[] = {
253
 
                STR_019F_TRAIN,
254
 
                STR_019C_ROAD_VEHICLE,
255
 
                STR_019E_SHIP,
256
 
                STR_019D_AIRCRAFT
257
 
        };
258
 
 
259
 
        switch (e->event) {
260
 
                case WE_CREATE:
261
 
                        WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
262
 
                        EngList_Create(&WP(w, replaceveh_d).list[0]);
263
 
                        EngList_Create(&WP(w, replaceveh_d).list[1]);
264
 
                        WP(w, replaceveh_d).update_left   = true;
265
 
                        WP(w, replaceveh_d).update_right  = true;
266
 
                        WP(w, replaceveh_d).init_lists    = true;
267
 
                        WP(w, replaceveh_d).sel_engine[0] = INVALID_ENGINE;
268
 
                        WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
269
 
                        break;
270
 
 
271
 
                case WE_PAINT: {
272
 
                        if (WP(w, replaceveh_d).update_left || WP(w, replaceveh_d).update_right) GenerateLists(w);
273
 
 
274
 
                        Player *p = GetPlayer(_local_player);
275
 
                        EngineID selected_id[2];
276
 
                        const GroupID selected_group = WP(w, replaceveh_d).sel_group;
277
 
 
278
 
                        selected_id[0] = WP(w, replaceveh_d).sel_engine[0];
279
 
                        selected_id[1] = WP(w, replaceveh_d).sel_engine[1];
280
 
 
281
 
                        /* Disable the "Start Replacing" button if:
282
 
                         *    Either list is empty
283
 
                         * or The selected replacement engine has a replacement (to prevent loops)
284
 
                         * or The right list (new replacement) has the existing replacement vehicle selected */
285
 
                        w->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
286
 
                                                                                 selected_id[0] == INVALID_ENGINE ||
287
 
                                                                                 selected_id[1] == INVALID_ENGINE ||
288
 
                                                                                 EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
289
 
                                                                                 EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]);
290
 
 
291
 
                        /* Disable the "Stop Replacing" button if:
292
 
                         *   The left list (existing vehicle) is empty
293
 
                         *   or The selected vehicle has no replacement set up */
294
 
                        w->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
295
 
                                                                                 selected_id[0] == INVALID_ENGINE ||
296
 
                                                                                 !EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
297
 
 
298
 
                        /* now the actual drawing of the window itself takes place */
299
 
                        SetDParam(0, _vehicle_type_names[w->window_number]);
300
 
 
301
 
                        if (w->window_number == VEH_TRAIN) {
302
 
                                /* set on/off for renew_keep_length */
303
 
                                SetDParam(1, p->renew_keep_length ? STR_CONFIG_PATCHES_ON : STR_CONFIG_PATCHES_OFF);
304
 
 
305
 
                                /* set wagon/engine button */
306
 
                                SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
307
 
 
308
 
                                /* sets the colour of that art thing */
309
 
                                w->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color  = _player_colors[_local_player];
310
 
                                w->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player];
311
 
                        }
312
 
 
313
 
                        if (w->window_number == VEH_TRAIN) {
314
 
                                /* Show the selected railtype in the pulldown menu */
315
 
                                RailType railtype = _railtype_selected_in_replace_gui;
316
 
                                w->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[railtype];
317
 
                        }
318
 
 
319
 
                        DrawWindowWidgets(w);
320
 
 
321
 
                        /* sets up the string for the vehicle that is being replaced to */
322
 
                        if (selected_id[0] != INVALID_ENGINE) {
323
 
                                if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
324
 
                                        SetDParam(0, STR_NOT_REPLACING);
325
 
                                } else {
326
 
                                        SetDParam(0, STR_ENGINE_NAME);
327
 
                                        SetDParam(1, EngineReplacementForPlayer(p, selected_id[0], selected_group));
328
 
                                }
329
 
                        } else {
330
 
                                SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
331
 
                        }
332
 
 
333
 
                        DrawString(145, w->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK);
334
 
 
335
 
                        /* Draw the lists */
336
 
                        for(byte i = 0; i < 2; i++) {
337
 
                                uint widget     = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
338
 
                                EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
339
 
                                EngineID start  = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
340
 
                                EngineID end    = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
341
 
 
342
 
                                /* Do the actual drawing */
343
 
                                DrawEngineList((VehicleType)w->window_number, w->widget[widget].left + 2, w->widget[widget].top + 1, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
344
 
 
345
 
                                /* Also draw the details if an engine is selected */
346
 
                                if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
347
 
                                        const Widget *wi = &w->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
348
 
                                        int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
349
 
 
350
 
                                        if (text_end > wi->bottom) {
351
 
                                                SetWindowDirty(w);
352
 
                                                ResizeWindowForWidget(w, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom);
353
 
                                                SetWindowDirty(w);
354
 
                                        }
355
 
                                }
356
 
                        }
357
 
 
358
 
                } break;   // end of paint
359
 
 
360
 
                case WE_CLICK: {
361
 
                        switch (e->we.click.widget) {
362
 
                                case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
363
 
                                        WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
364
 
                                        WP(w, replaceveh_d).update_left = true;
365
 
                                        WP(w, replaceveh_d).init_lists  = true;
366
 
                                        SetWindowDirty(w);
367
 
                                        break;
368
 
 
369
 
                                case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */
370
 
                                        ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes);
371
 
                                        break;
372
 
 
373
 
                                case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */
374
 
                                        DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE);
375
 
                                        break;
376
 
 
377
 
                                case RVW_WIDGET_START_REPLACE: { /* Start replacing */
378
 
                                        EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
379
 
                                        EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
380
 
                                        DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
381
 
                                } break;
382
 
 
383
 
                                case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */
384
 
                                        EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
385
 
                                        DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
386
 
                                } break;
387
 
 
388
 
                                case RVW_WIDGET_LEFT_MATRIX:
389
 
                                case RVW_WIDGET_RIGHT_MATRIX: {
390
 
                                        uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
391
 
                                        uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
392
 
                                        uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
393
 
                                        byte click_side         = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
394
 
                                        uint16 engine_count     = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
395
 
 
396
 
                                        if (i < click_scroll_cap) {
397
 
                                                i += click_scroll_pos;
398
 
                                                EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE;
399
 
                                                if (e == WP(w, replaceveh_d).sel_engine[click_side]) break; // we clicked the one we already selected
400
 
                                                WP(w, replaceveh_d).sel_engine[click_side] = e;
401
 
                                                if (click_side == 0) {
402
 
                                                        WP(w, replaceveh_d).update_right = true;
403
 
                                                        WP(w, replaceveh_d).init_lists   = true;
404
 
                                                }
405
 
                                                SetWindowDirty(w);
406
 
                                                }
407
 
                                        break;
408
 
                                        }
409
 
                        }
410
 
                        break;
411
 
                }
412
 
 
413
 
                case WE_DROPDOWN_SELECT: { /* we have selected a dropdown item in the list */
414
 
                        RailType temp = (RailType)e->we.dropdown.index;
415
 
                        if (temp == _railtype_selected_in_replace_gui) break; // we didn't select a new one. No need to change anything
416
 
                        _railtype_selected_in_replace_gui = temp;
417
 
                        /* Reset scrollbar positions */
418
 
                        w->vscroll.pos  = 0;
419
 
                        w->vscroll2.pos = 0;
420
 
                        /* Rebuild the lists */
421
 
                        WP(w, replaceveh_d).update_left  = true;
422
 
                        WP(w, replaceveh_d).update_right = true;
423
 
                        WP(w, replaceveh_d).init_lists   = true;
424
 
                        SetWindowDirty(w);
425
 
                } break;
426
 
 
427
 
                case WE_RESIZE:
428
 
                        w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
429
 
                        w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
430
 
 
431
 
                        w->widget[RVW_WIDGET_LEFT_MATRIX].data  = (w->vscroll.cap  << 8) + 1;
432
 
                        w->widget[RVW_WIDGET_RIGHT_MATRIX].data = (w->vscroll2.cap << 8) + 1;
433
 
                        break;
434
 
 
435
 
                case WE_INVALIDATE_DATA:
436
 
                        if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true;
437
 
                        if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true;
438
 
                        SetWindowDirty(w);
439
 
                        break;
440
 
 
441
 
                case WE_DESTROY:
442
 
                        EngList_RemoveAll(&WP(w, replaceveh_d).list[0]);
443
 
                        EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
444
 
                break;
445
 
        }
446
 
}
447
 
 
448
 
static const Widget _replace_rail_vehicle_widgets[] = {
449
 
{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,       STR_018B_CLOSE_WINDOW},
450
 
{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES_WHITE, STR_018C_WINDOW_TITLE_DRAG_THIS},
451
 
{  WWT_STICKYBOX,   RESIZE_NONE,    14,   444,   455,     0,    13, STR_NULL,       STR_STICKY_BUTTON},
452
 
{      WWT_PANEL,     RESIZE_TB,    14,     0,   227,   126,   227, 0x0,            STR_NULL},
453
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   138,   240,   251, STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON},
454
 
{      WWT_PANEL,     RESIZE_TB,    14,   139,   316,   228,   239, 0x0,            STR_REPLACE_HELP_REPLACE_INFO_TAB},
455
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,   306,   443,   240,   251, STR_REPLACE_VEHICLES_STOP,  STR_REPLACE_HELP_STOP_BUTTON},
456
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,     0,   215,    14,   125, 0x801,          STR_REPLACE_HELP_LEFT_ARRAY},
457
 
{  WWT_SCROLLBAR, RESIZE_BOTTOM,    14,   216,   227,    14,   125, STR_NULL,       STR_0190_SCROLL_BAR_SCROLLS_LIST},
458
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,   228,   443,    14,   125, 0x801,          STR_REPLACE_HELP_RIGHT_ARRAY},
459
 
{ WWT_SCROLL2BAR, RESIZE_BOTTOM,    14,   444,   455,    14,   125, STR_NULL,       STR_0190_SCROLL_BAR_SCROLLS_LIST},
460
 
{      WWT_PANEL,     RESIZE_TB,    14,   228,   455,   126,   227, 0x0,            STR_NULL},
461
 
// train specific stuff
462
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   138,   228,   239, STR_REPLACE_ENGINE_WAGON_SELECT,       STR_REPLACE_ENGINE_WAGON_SELECT_HELP},  // widget 12
463
 
{      WWT_PANEL,     RESIZE_TB,    14,   139,   153,   240,   251, 0x0,            STR_NULL},
464
 
{   WWT_DROPDOWN,     RESIZE_TB,    14,   154,   289,   240,   251, 0x0,            STR_REPLACE_HELP_RAILTYPE},
465
 
{      WWT_PANEL,     RESIZE_TB,    14,   290,   305,   240,   251, 0x0,            STR_NULL},
466
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,   317,   455,   228,   239, STR_REPLACE_REMOVE_WAGON,       STR_REPLACE_REMOVE_WAGON_HELP},
467
 
// end of train specific stuff
468
 
{  WWT_RESIZEBOX,     RESIZE_TB,    14,   444,   455,   240,   251, STR_NULL,       STR_RESIZE_BUTTON},
469
 
{   WIDGETS_END},
470
 
};
471
 
 
472
 
static const Widget _replace_road_vehicle_widgets[] = {
473
 
{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
474
 
{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES_WHITE,  STR_018C_WINDOW_TITLE_DRAG_THIS},
475
 
{  WWT_STICKYBOX,   RESIZE_NONE,    14,   444,   455,     0,    13, STR_NULL,                    STR_STICKY_BUTTON},
476
 
{      WWT_PANEL,     RESIZE_TB,    14,     0,   227,   126,   217, 0x0,                         STR_NULL},
477
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   138,   218,   229, STR_REPLACE_VEHICLES_START,  STR_REPLACE_HELP_START_BUTTON},
478
 
{      WWT_PANEL,     RESIZE_TB,    14,   139,   305,   218,   229, 0x0,                         STR_REPLACE_HELP_REPLACE_INFO_TAB},
479
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,   306,   443,   218,   229, STR_REPLACE_VEHICLES_STOP,   STR_REPLACE_HELP_STOP_BUTTON},
480
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,     0,   215,    14,   125, 0x801,                       STR_REPLACE_HELP_LEFT_ARRAY},
481
 
{  WWT_SCROLLBAR, RESIZE_BOTTOM,    14,   216,   227,    14,   125, STR_NULL,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
482
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,   228,   443,    14,   125, 0x801,                       STR_REPLACE_HELP_RIGHT_ARRAY},
483
 
{ WWT_SCROLL2BAR, RESIZE_BOTTOM,    14,   444,   455,    14,   125, STR_NULL,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
484
 
{      WWT_PANEL,     RESIZE_TB,    14,   228,   455,   126,   217, 0x0,                         STR_NULL},
485
 
{  WWT_RESIZEBOX,     RESIZE_TB,    14,   444,   455,   218,   229, STR_NULL,                    STR_RESIZE_BUTTON},
486
 
{   WIDGETS_END},
487
 
};
488
 
 
489
 
static const Widget _replace_ship_aircraft_vehicle_widgets[] = {
490
 
{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
491
 
{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   443,     0,    13, STR_REPLACE_VEHICLES_WHITE,  STR_018C_WINDOW_TITLE_DRAG_THIS},
492
 
{  WWT_STICKYBOX,   RESIZE_NONE,    14,   444,   455,     0,    13, STR_NULL,                    STR_STICKY_BUTTON},
493
 
{      WWT_PANEL,     RESIZE_TB,    14,     0,   227,   110,   201, 0x0,                         STR_NULL},
494
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   138,   202,   213, STR_REPLACE_VEHICLES_START,  STR_REPLACE_HELP_START_BUTTON},
495
 
{      WWT_PANEL,     RESIZE_TB,    14,   139,   305,   202,   213, 0x0,                         STR_REPLACE_HELP_REPLACE_INFO_TAB},
496
 
{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,   306,   443,   202,   213, STR_REPLACE_VEHICLES_STOP,   STR_REPLACE_HELP_STOP_BUTTON},
497
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,     0,   215,    14,   109, 0x401,                       STR_REPLACE_HELP_LEFT_ARRAY},
498
 
{  WWT_SCROLLBAR, RESIZE_BOTTOM,    14,   216,   227,    14,   109, STR_NULL,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
499
 
{     WWT_MATRIX, RESIZE_BOTTOM,    14,   228,   443,    14,   109, 0x401,                       STR_REPLACE_HELP_RIGHT_ARRAY},
500
 
{ WWT_SCROLL2BAR, RESIZE_BOTTOM,    14,   444,   455,    14,   109, STR_NULL,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
501
 
{      WWT_PANEL,     RESIZE_TB,    14,   228,   455,   110,   201, 0x0,                         STR_NULL},
502
 
{  WWT_RESIZEBOX,     RESIZE_TB,    14,   444,   455,   202,   213, STR_NULL,                    STR_RESIZE_BUTTON},
503
 
{   WIDGETS_END},
504
 
};
505
 
 
506
 
static const WindowDesc _replace_rail_vehicle_desc = {
507
 
        WDP_AUTO, WDP_AUTO, 456, 252, 456, 252,
508
 
        WC_REPLACE_VEHICLE, WC_NONE,
509
 
        WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
510
 
        _replace_rail_vehicle_widgets,
511
 
        ReplaceVehicleWndProc
512
 
};
513
 
 
514
 
static const WindowDesc _replace_road_vehicle_desc = {
515
 
        WDP_AUTO, WDP_AUTO, 456, 230, 456, 230,
516
 
        WC_REPLACE_VEHICLE, WC_NONE,
517
 
        WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
518
 
        _replace_road_vehicle_widgets,
519
 
        ReplaceVehicleWndProc
520
 
};
521
 
 
522
 
static const WindowDesc _replace_ship_aircraft_vehicle_desc = {
523
 
        WDP_AUTO, WDP_AUTO, 456, 214, 456, 214,
524
 
        WC_REPLACE_VEHICLE, WC_NONE,
525
 
        WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
526
 
        _replace_ship_aircraft_vehicle_widgets,
527
 
        ReplaceVehicleWndProc
528
 
};
529
 
 
 
86
/**
 
87
 * Window for the autoreplacing of vehicles.
 
88
 */
 
89
class ReplaceVehicleWindow : public Window {
 
90
        byte sel_index[2];
 
91
        EngineID sel_engine[2];
 
92
        uint16 count[2];
 
93
        bool wagon_btnstate; ///< true means engine is selected
 
94
        GUIEngineList list[2];
 
95
        bool update_left;
 
96
        bool update_right;
 
97
        bool init_lists;
 
98
        GroupID sel_group;
 
99
        static RailType sel_railtype;
 
100
 
 
101
        /** Figure out if an engine should be added to a list
 
102
         * @param e The EngineID
 
103
         * @param draw_left If true, then the left list is drawn (the engines specific to the railtype you selected)
 
104
         * @param show_engines if truem then locomotives are drawn, else wagons (never both)
 
105
         * @return true if the engine should be in the list (based on this check)
 
106
         */
 
107
        bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
 
108
        {
 
109
                const RailVehicleInfo *rvi = RailVehInfo(e);
 
110
 
 
111
                /* Ensure that the wagon/engine selection fits the engine. */
 
112
                if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
 
113
 
 
114
                if (draw_left && show_engines) {
 
115
                        /* Ensure that the railtype is specific to the selected one */
 
116
                        if (rvi->railtype != this->sel_railtype) return false;
 
117
                }
 
118
                return true;
 
119
        }
 
120
 
 
121
 
 
122
        /** Generate a list
 
123
         * @param w Window, that contains the list
 
124
         * @param draw_left true if generating the left list, otherwise false
 
125
         */
 
126
        void GenerateReplaceVehList(Window *w, bool draw_left)
 
127
        {
 
128
                EngineID selected_engine = INVALID_ENGINE;
 
129
                VehicleType type = (VehicleType)this->window_number;
 
130
                byte i = draw_left ? 0 : 1;
 
131
 
 
132
                GUIEngineList *list = &this->list[i];
 
133
                list->Clear();
 
134
 
 
135
                const Engine *e;
 
136
                FOR_ALL_ENGINES_OF_TYPE(e, type) {
 
137
                        EngineID eid = e->index;
 
138
                        if (type == VEH_TRAIN && !GenerateReplaceRailList(eid, draw_left, this->wagon_btnstate)) continue; // special rules for trains
 
139
 
 
140
                        if (draw_left) {
 
141
                                const GroupID selected_group = this->sel_group;
 
142
                                const uint num_engines = GetGroupNumEngines(_local_company, selected_group, eid);
 
143
 
 
144
                                /* Skip drawing the engines we don't have any of and haven't set for replacement */
 
145
                                if (num_engines == 0 && EngineReplacementForCompany(GetCompany(_local_company), eid, selected_group) == INVALID_ENGINE) continue;
 
146
                        } else {
 
147
                                if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
 
148
                        }
 
149
 
 
150
                        *list->Append() = eid;
 
151
                        if (eid == this->sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
 
152
                }
 
153
                this->sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
 
154
                EngList_Sort(list, &EngineNumberSorter);
 
155
        }
 
156
 
 
157
        /** Generate the lists */
 
158
        void GenerateLists()
 
159
        {
 
160
                EngineID e = this->sel_engine[0];
 
161
 
 
162
                if (this->update_left == true) {
 
163
                        /* We need to rebuild the left list */
 
164
                        GenerateReplaceVehList(this, true);
 
165
                        SetVScrollCount(this, this->list[0].Length());
 
166
                        if (this->init_lists && this->sel_engine[0] == INVALID_ENGINE && this->list[0].Length() != 0) {
 
167
                                this->sel_engine[0] = this->list[0][0];
 
168
                        }
 
169
                }
 
170
 
 
171
                if (this->update_right || e != this->sel_engine[0]) {
 
172
                        /* Either we got a request to rebuild the right list or the left list selected a different engine */
 
173
                        if (this->sel_engine[0] == INVALID_ENGINE) {
 
174
                                /* Always empty the right list when nothing is selected in the left list */
 
175
                                this->list[1].Clear();
 
176
                                this->sel_engine[1] = INVALID_ENGINE;
 
177
                        } else {
 
178
                                GenerateReplaceVehList(this, false);
 
179
                                SetVScroll2Count(this, this->list[1].Length());
 
180
                                if (this->init_lists && this->sel_engine[1] == INVALID_ENGINE && this->list[1].Length() != 0) {
 
181
                                        this->sel_engine[1] = this->list[1][0];
 
182
                                }
 
183
                        }
 
184
                }
 
185
                /* Reset the flags about needed updates */
 
186
                this->update_left  = false;
 
187
                this->update_right = false;
 
188
                this->init_lists   = false;
 
189
        }
 
190
 
 
191
public:
 
192
        ReplaceVehicleWindow(const WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc, vehicletype)
 
193
        {
 
194
                this->wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
 
195
                this->update_left   = true;
 
196
                this->update_right  = true;
 
197
                this->init_lists    = true;
 
198
                this->sel_engine[0] = INVALID_ENGINE;
 
199
                this->sel_engine[1] = INVALID_ENGINE;
 
200
 
 
201
                this->resize.step_height = GetVehicleListHeight(vehicletype);
 
202
                this->vscroll.cap = this->resize.step_height == 14 ? 8 : 4;
 
203
 
 
204
                Widget *widget = this->widget;
 
205
                widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll.cap << 8) + 1;
 
206
 
 
207
                if (vehicletype == VEH_TRAIN) {
 
208
                        this->wagon_btnstate = true;
 
209
                        /* The train window is bigger so we will move some of the widgets to fit the new size.
 
210
                         * We will start by moving the resize button to the lower right corner.                 */
 
211
                        widget[RVW_WIDGET_RESIZE].top         = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].top;
 
212
                        widget[RVW_WIDGET_RESIZE].bottom      = widget[RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE].bottom;
 
213
                        widget[RVW_WIDGET_STOP_REPLACE].right = widget[RVW_WIDGET_RESIZE].right;
 
214
 
 
215
                        /* The detail panel is one line taller for trains so we will move some of the widgets one line (10 pixels) down. */
 
216
                        widget[RVW_WIDGET_LEFT_DETAILS].bottom  += 10;
 
217
                        widget[RVW_WIDGET_RIGHT_DETAILS].bottom += 10;
 
218
                        for (int i = RVW_WIDGET_START_REPLACE; i < RVW_WIDGET_RESIZE; i++) {
 
219
                                widget[i].top    += 10;
 
220
                                widget[i].bottom += 10;
 
221
                        }
 
222
                } else {
 
223
                        /* Since it's not a train we will hide the train only widgets. */
 
224
                        this->SetWidgetsHiddenState(true,
 
225
                                                                        RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE,
 
226
                                                                        RVW_WIDGET_TRAIN_FLUFF_LEFT,
 
227
                                                                        RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN,
 
228
                                                                        RVW_WIDGET_TRAIN_FLUFF_RIGHT,
 
229
                                                                        RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE,
 
230
                                                                        WIDGET_LIST_END);
 
231
                }
 
232
 
 
233
                ResizeWindow(this, 0, this->resize.step_height * this->vscroll.cap);
 
234
 
 
235
                /* Set the minimum window size to the current window size */
 
236
                this->resize.width  = this->width;
 
237
                this->resize.height = this->height;
 
238
 
 
239
                this->owner = _local_company;
 
240
                this->sel_group = id_g;
 
241
                this->vscroll2.cap = this->vscroll.cap;   // these two are always the same
 
242
 
 
243
                this->FindWindowPlacementAndResize(desc);
 
244
        }
 
245
 
 
246
        virtual void OnPaint()
 
247
        {
 
248
                if (this->update_left || this->update_right) this->GenerateLists();
 
249
 
 
250
                Company *c = GetCompany(_local_company);
 
251
                EngineID selected_id[2];
 
252
                const GroupID selected_group = this->sel_group;
 
253
 
 
254
                selected_id[0] = this->sel_engine[0];
 
255
                selected_id[1] = this->sel_engine[1];
 
256
 
 
257
                /* Disable the "Start Replacing" button if:
 
258
                 *    Either list is empty
 
259
                 * or The selected replacement engine has a replacement (to prevent loops)
 
260
                 * or The right list (new replacement) has the existing replacement vehicle selected */
 
261
                this->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
 
262
                                                                                selected_id[0] == INVALID_ENGINE ||
 
263
                                                                                selected_id[1] == INVALID_ENGINE ||
 
264
                                                                                EngineReplacementForCompany(c, selected_id[1], selected_group) != INVALID_ENGINE ||
 
265
                                                                                EngineReplacementForCompany(c, selected_id[0], selected_group) == selected_id[1]);
 
266
 
 
267
                /* Disable the "Stop Replacing" button if:
 
268
                 *   The left list (existing vehicle) is empty
 
269
                 *   or The selected vehicle has no replacement set up */
 
270
                this->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
 
271
                                                                                selected_id[0] == INVALID_ENGINE ||
 
272
                                                                                !EngineHasReplacementForCompany(c, selected_id[0], selected_group));
 
273
 
 
274
                /* now the actual drawing of the window itself takes place */
 
275
                SetDParam(0, STR_019F_TRAIN + this->window_number);
 
276
 
 
277
                if (this->window_number == VEH_TRAIN) {
 
278
                        /* set on/off for renew_keep_length */
 
279
                        SetDParam(1, c->renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 
280
 
 
281
                        /* set wagon/engine button */
 
282
                        SetDParam(2, this->wagon_btnstate ? STR_ENGINES : STR_WAGONS);
 
283
 
 
284
                        /* sets the colour of that art thing */
 
285
                        this->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].colour  = _company_colours[_local_company];
 
286
                        this->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].colour = _company_colours[_local_company];
 
287
                }
 
288
 
 
289
                if (this->window_number == VEH_TRAIN) {
 
290
                        /* Show the selected railtype in the pulldown menu */
 
291
                        const RailtypeInfo *rti = GetRailTypeInfo(sel_railtype);
 
292
                        this->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = rti->strings.replace_text;
 
293
                }
 
294
 
 
295
                this->DrawWidgets();
 
296
 
 
297
                /* sets up the string for the vehicle that is being replaced to */
 
298
                if (selected_id[0] != INVALID_ENGINE) {
 
299
                        if (!EngineHasReplacementForCompany(c, selected_id[0], selected_group)) {
 
300
                                SetDParam(0, STR_NOT_REPLACING);
 
301
                        } else {
 
302
                                SetDParam(0, STR_ENGINE_NAME);
 
303
                                SetDParam(1, EngineReplacementForCompany(c, selected_id[0], selected_group));
 
304
                        }
 
305
                } else {
 
306
                        SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
 
307
                }
 
308
 
 
309
                DrawStringTruncated(this->widget[RVW_WIDGET_INFO_TAB].left + 6, this->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK, this->GetWidgetWidth(RVW_WIDGET_INFO_TAB) - 12);
 
310
 
 
311
                /* Draw the lists */
 
312
                for (byte i = 0; i < 2; i++) {
 
313
                        uint widget     = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
 
314
                        GUIEngineList *list = &this->list[i]; // which list to draw
 
315
                        EngineID start  = i == 0 ? this->vscroll.pos : this->vscroll2.pos; // what is the offset for the start (scrolling)
 
316
                        EngineID end    = min((i == 0 ? this->vscroll.cap : this->vscroll2.cap) + start, list->Length());
 
317
 
 
318
                        /* Do the actual drawing */
 
319
                        DrawEngineList((VehicleType)this->window_number, this->widget[widget].left + 2, this->widget[widget].right, this->widget[widget].top + 1, list, start, end, this->sel_engine[i], i == 0 ? this->widget[RVW_WIDGET_LEFT_MATRIX].right - 2 : 0, selected_group);
 
320
 
 
321
                        /* Also draw the details if an engine is selected */
 
322
                        if (this->sel_engine[i] != INVALID_ENGINE) {
 
323
                                const Widget *wi = &this->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
 
324
                                int text_end = DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine[i]);
 
325
 
 
326
                                if (text_end > wi->bottom) {
 
327
                                        this->SetDirty();
 
328
                                        ResizeWindowForWidget(this, i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS, 0, text_end - wi->bottom);
 
329
                                        this->SetDirty();
 
330
                                }
 
331
                        }
 
332
                }
 
333
        }
 
334
 
 
335
        virtual void OnClick(Point pt, int widget)
 
336
        {
 
337
                switch (widget) {
 
338
                        case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
 
339
                                this->wagon_btnstate = !(this->wagon_btnstate);
 
340
                                this->update_left = true;
 
341
                                this->init_lists  = true;
 
342
                                this->SetDirty();
 
343
                                break;
 
344
 
 
345
                        case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: { // Railtype selection dropdown menu
 
346
                                const Company *c = GetCompany(_local_company);
 
347
                                DropDownList *list = new DropDownList();
 
348
                                for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
 
349
                                        const RailtypeInfo *rti = GetRailTypeInfo(rt);
 
350
                                        /* Skip rail type if it has no label */
 
351
                                        if (rti->label == 0) continue;
 
352
                                        list->push_back(new DropDownListStringItem(rti->strings.replace_text, rt, !HasBit(c->avail_railtypes, rt)));
 
353
                                }
 
354
                                ShowDropDownList(this, list, sel_railtype, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN);
 
355
                                break;
 
356
                        }
 
357
 
 
358
                        case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
 
359
                                DoCommandP(0, 5, GetCompany(_local_company)->renew_keep_length ? 0 : 1, CMD_SET_AUTOREPLACE);
 
360
                                break;
 
361
 
 
362
                        case RVW_WIDGET_START_REPLACE: { // Start replacing
 
363
                                EngineID veh_from = this->sel_engine[0];
 
364
                                EngineID veh_to = this->sel_engine[1];
 
365
                                DoCommandP(0, 3 + (this->sel_group << 16) , veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
 
366
                                this->SetDirty();
 
367
                        } break;
 
368
 
 
369
                        case RVW_WIDGET_STOP_REPLACE: { // Stop replacing
 
370
                                EngineID veh_from = this->sel_engine[0];
 
371
                                DoCommandP(0, 3 + (this->sel_group << 16), veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
 
372
                                this->SetDirty();
 
373
                        } break;
 
374
 
 
375
                        case RVW_WIDGET_LEFT_MATRIX:
 
376
                        case RVW_WIDGET_RIGHT_MATRIX: {
 
377
                                uint i = (pt.y - 14) / this->resize.step_height;
 
378
                                uint16 click_scroll_pos = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.pos : this->vscroll2.pos;
 
379
                                uint16 click_scroll_cap = widget == RVW_WIDGET_LEFT_MATRIX ? this->vscroll.cap : this->vscroll2.cap;
 
380
                                byte click_side         = widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
 
381
                                size_t engine_count     = this->list[click_side].Length();
 
382
 
 
383
                                if (i < click_scroll_cap) {
 
384
                                        i += click_scroll_pos;
 
385
                                        EngineID e = engine_count > i ? this->list[click_side][i] : INVALID_ENGINE;
 
386
                                        if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
 
387
                                        this->sel_engine[click_side] = e;
 
388
                                        if (click_side == 0) {
 
389
                                                this->update_right = true;
 
390
                                                this->init_lists   = true;
 
391
                                        }
 
392
                                        this->SetDirty();
 
393
                                        }
 
394
                                break;
 
395
                                }
 
396
                }
 
397
        }
 
398
 
 
399
        virtual void OnDropdownSelect(int widget, int index)
 
400
        {
 
401
                RailType temp = (RailType)index;
 
402
                if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
 
403
                sel_railtype = temp;
 
404
                /* Reset scrollbar positions */
 
405
                this->vscroll.pos  = 0;
 
406
                this->vscroll2.pos = 0;
 
407
                /* Rebuild the lists */
 
408
                this->update_left  = true;
 
409
                this->update_right = true;
 
410
                this->init_lists   = true;
 
411
                this->SetDirty();
 
412
        }
 
413
 
 
414
        virtual void OnResize(Point new_size, Point delta) {
 
415
                this->vscroll.cap  += delta.y / (int)this->resize.step_height;
 
416
                this->vscroll2.cap += delta.y / (int)this->resize.step_height;
 
417
 
 
418
                Widget *widget = this->widget;
 
419
 
 
420
                widget[RVW_WIDGET_LEFT_MATRIX].data = widget[RVW_WIDGET_RIGHT_MATRIX].data = (this->vscroll2.cap << 8) + 1;
 
421
 
 
422
                if (delta.x != 0) {
 
423
                        /* We changed the width of the window so we have to resize the lists.
 
424
                         * Because ResizeButtons() makes each widget the same size it can't be used on the lists
 
425
                         * because then the lists would have the same size as the scrollbars.
 
426
                         * Instead we use it on the detail panels.
 
427
                         * Afterwards we use the new location of the detail panels (the middle of the window)
 
428
                         * to place the lists.
 
429
                         * This way the lists will have equal size while keeping the width of the scrollbars unchanged. */
 
430
                        ResizeButtons(this, RVW_WIDGET_LEFT_DETAILS, RVW_WIDGET_RIGHT_DETAILS);
 
431
                        widget[RVW_WIDGET_RIGHT_MATRIX].left    = widget[RVW_WIDGET_RIGHT_DETAILS].left;
 
432
                        widget[RVW_WIDGET_LEFT_SCROLLBAR].right = widget[RVW_WIDGET_LEFT_DETAILS].right;
 
433
                        widget[RVW_WIDGET_LEFT_SCROLLBAR].left  = widget[RVW_WIDGET_LEFT_SCROLLBAR].right - 11;
 
434
                        widget[RVW_WIDGET_LEFT_MATRIX].right    = widget[RVW_WIDGET_LEFT_SCROLLBAR].left - 1;
 
435
                }
 
436
        }
 
437
 
 
438
        virtual void OnInvalidateData(int data)
 
439
        {
 
440
                if (data != 0) {
 
441
                        this->update_left = true;
 
442
                } else {
 
443
                        this->update_right = true;
 
444
                }
 
445
        }
 
446
};
 
447
 
 
448
static const Widget _replace_vehicle_widgets[] = {
 
449
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                        STR_018B_CLOSE_WINDOW},
 
450
{    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,    11,   443,     0,    13, STR_REPLACE_VEHICLES_WHITE,      STR_018C_WINDOW_TITLE_DRAG_THIS},
 
451
{  WWT_STICKYBOX,     RESIZE_LR,  COLOUR_GREY,   444,   455,     0,    13, STR_NULL,                        STR_STICKY_BUTTON},
 
452
 
 
453
{     WWT_MATRIX, RESIZE_BOTTOM,  COLOUR_GREY,     0,   215,    14,    13, 0x1,                             STR_REPLACE_HELP_LEFT_ARRAY},
 
454
{  WWT_SCROLLBAR, RESIZE_BOTTOM,  COLOUR_GREY,   216,   227,    14,    13, STR_NULL,                        STR_0190_SCROLL_BAR_SCROLLS_LIST},
 
455
{     WWT_MATRIX,    RESIZE_LRB,  COLOUR_GREY,   228,   443,    14,    13, 0x1,                             STR_REPLACE_HELP_RIGHT_ARRAY},
 
456
{ WWT_SCROLL2BAR,    RESIZE_LRB,  COLOUR_GREY,   444,   455,    14,    13, STR_NULL,                        STR_0190_SCROLL_BAR_SCROLLS_LIST},
 
457
{      WWT_PANEL,     RESIZE_TB,  COLOUR_GREY,     0,   227,    14,   105, 0x0,                             STR_NULL},
 
458
{      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,   228,   455,    14,   105, 0x0,                             STR_NULL},
 
459
 
 
460
{ WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   138,   106,   117, STR_REPLACE_VEHICLES_START,      STR_REPLACE_HELP_START_BUTTON},
 
461
{      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,   139,   305,   106,   117, 0x0,                             STR_REPLACE_HELP_REPLACE_INFO_TAB},
 
462
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,  COLOUR_GREY,   306,   443,   106,   117, STR_REPLACE_VEHICLES_STOP,       STR_REPLACE_HELP_STOP_BUTTON},
 
463
{  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY,   444,   455,   106,   117, STR_NULL,                        STR_RESIZE_BUTTON},
 
464
 
 
465
{ WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   138,   128,   139, STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP},
 
466
{      WWT_PANEL,     RESIZE_TB,  COLOUR_GREY,   139,   153,   128,   139, 0x0,                             STR_NULL},
 
467
{   WWT_DROPDOWN,    RESIZE_RTB,  COLOUR_GREY,   154,   289,   128,   139, 0x0,                             STR_REPLACE_HELP_RAILTYPE},
 
468
{      WWT_PANEL,   RESIZE_LRTB,  COLOUR_GREY,   290,   305,   128,   139, 0x0,                             STR_NULL},
 
469
{ WWT_PUSHTXTBTN,   RESIZE_LRTB,  COLOUR_GREY,   306,   443,   128,   139, STR_REPLACE_REMOVE_WAGON,        STR_REPLACE_REMOVE_WAGON_HELP},
 
470
{   WIDGETS_END},
 
471
};
 
472
 
 
473
static const WindowDesc _replace_rail_vehicle_desc(
 
474
        WDP_AUTO, WDP_AUTO, 456, 140, 456, 140,
 
475
        WC_REPLACE_VEHICLE, WC_NONE,
 
476
        WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE | WDF_CONSTRUCTION,
 
477
        _replace_vehicle_widgets
 
478
);
 
479
 
 
480
static const WindowDesc _replace_vehicle_desc(
 
481
        WDP_AUTO, WDP_AUTO, 456, 118, 456, 118,
 
482
        WC_REPLACE_VEHICLE, WC_NONE,
 
483
        WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE | WDF_CONSTRUCTION,
 
484
        _replace_vehicle_widgets
 
485
);
 
486
 
 
487
RailType ReplaceVehicleWindow::sel_railtype = RAILTYPE_RAIL;
530
488
 
531
489
void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
532
490
{
533
 
        Window *w;
534
 
 
535
491
        DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
536
 
 
537
 
        switch (vehicletype) {
538
 
                default: NOT_REACHED();
539
 
                case VEH_TRAIN:
540
 
                        w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype);
541
 
                        w->vscroll.cap  = 8;
542
 
                        w->resize.step_height = 14;
543
 
                        WP(w, replaceveh_d).wagon_btnstate = true;
544
 
                        break;
545
 
                case VEH_ROAD:
546
 
                        w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype);
547
 
                        w->vscroll.cap  = 8;
548
 
                        w->resize.step_height = 14;
549
 
                        break;
550
 
                case VEH_SHIP:
551
 
                case VEH_AIRCRAFT:
552
 
                        w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype);
553
 
                        w->vscroll.cap  = 4;
554
 
                        w->resize.step_height = 24;
555
 
                        break;
556
 
        }
557
 
 
558
 
        w->caption_color = _local_player;
559
 
        WP(w, replaceveh_d).sel_group = id_g;
560
 
        w->vscroll2.cap = w->vscroll.cap;   // these two are always the same
 
492
        new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);
561
493
}