Files @ r5312:ffd375effb01
Branch filter:

Location: cpp/openttd-patchpack/source/train_gui.c - annotation

Darkvater
(svn r7468) -Codechange: [win32] Add some comments to MB/WIDE_TO_WIDE/MB_[BUFFER] macros and
use them some more in win32 code. Also for the clipboard use the convert_from_fs
function instead of calling Win32 API directly. Make the static buffers in OTTD2FS
and FS2OTTD the same size (character-length wise)
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
r2186:5ee653b1b5e1
r2186:5ee653b1b5e1
r0:d63b455452f6
r1891:c5c5466afa35
r1299:07d5483b3f76
r2163:ae001e2aa5b0
r4041:22c02258c9b7
r1922:d5634e2fe7b3
r507:b1b0a0a3c313
r679:3a7b08cc8504
r5187:03b78dac4092
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r588:c0748f0d2104
r1313:da473558c9d7
r2676:cfc81c07cc6a
r2962:16f2ef02fde1
r4261:8c2d0c75e37a
r4770:543b61edc4ba
r420:d99a908fcdb7
r5273:6960a710403e
r4764:db558507b5fe
r4764:db558507b5fe
r5187:03b78dac4092
r4770:543b61edc4ba
r4770:543b61edc4ba
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r5273:6960a710403e
r4764:db558507b5fe
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4931:1f19f64cce73
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r5187:03b78dac4092
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5195:e3fe1da3f768
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4835:4fc5f9cd28bc
r4770:543b61edc4ba
r4770:543b61edc4ba
r4912:d420151de4c6
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4912:d420151de4c6
r4770:543b61edc4ba
r4770:543b61edc4ba
r4836:47fab20f68b2
r4770:543b61edc4ba
r4836:47fab20f68b2
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5187:03b78dac4092
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4764:db558507b5fe
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r4930:fb0ce02440ea
r842:96908cec3367
r842:96908cec3367
r4171:bf306290d87e
r842:96908cec3367
r842:96908cec3367
r4288:b6dbdc0a7329
r842:96908cec3367
r1907:da80804490bd
r842:96908cec3367
r842:96908cec3367
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r3477:aa44670525a9
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r842:96908cec3367
r1907:da80804490bd
r2840:09966d5ba228
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1908:8c22d41a448a
r1907:da80804490bd
r4898:23ea96bac352
r4898:23ea96bac352
r4898:23ea96bac352
r4898:23ea96bac352
r4896:6cf2d452f4cc
r4737:000dc240da9f
r1907:da80804490bd
r842:96908cec3367
r1907:da80804490bd
r1907:da80804490bd
r842:96908cec3367
r1907:da80804490bd
r4293:cbb984a32af5
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r842:96908cec3367
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r3651:5f0e6bc9275f
r3651:5f0e6bc9275f
r4930:fb0ce02440ea
r4933:e3a1a0b9aa71
r842:96908cec3367
r842:96908cec3367
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r4930:fb0ce02440ea
r842:96908cec3367
r842:96908cec3367
r3393:9d958dc7a778
r867:c270355a1b34
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r1907:da80804490bd
r3685:4cab4e77bdcd
r4898:23ea96bac352
r4898:23ea96bac352
r4898:23ea96bac352
r4898:23ea96bac352
r4896:6cf2d452f4cc
r4737:000dc240da9f
r3685:4cab4e77bdcd
r3685:4cab4e77bdcd
r1907:da80804490bd
r1907:da80804490bd
r2443:6fdc13cc4fbe
r2443:6fdc13cc4fbe
r2456:aad40f8d9c89
r3477:aa44670525a9
r2443:6fdc13cc4fbe
r2443:6fdc13cc4fbe
r2443:6fdc13cc4fbe
r3651:5f0e6bc9275f
r3651:5f0e6bc9275f
r4930:fb0ce02440ea
r4932:f8667c11cd4e
r842:96908cec3367
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r4171:bf306290d87e
r0:d63b455452f6
r4077:259c4c4aacad
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2676:cfc81c07cc6a
r0:d63b455452f6
r0:d63b455452f6
r4077:259c4c4aacad
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r3948:4c986a48e294
r588:c0748f0d2104
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1977:1f8b99c96041
r0:d63b455452f6
r4171:bf306290d87e
r0:d63b455452f6
r2639:e298192a7c03
r193:6aa65dc5a4b4
r3948:4c986a48e294
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r3887:731a50a79f3b
r2244:0a45bb36cb48
r3948:4c986a48e294
r2244:0a45bb36cb48
r5187:03b78dac4092
r5187:03b78dac4092
r4765:7228ab2d82f2
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r5187:03b78dac4092
r524:21cbb8d1771d
r5200:6c1c28578fd0
r5187:03b78dac4092
r5190:f351385d22fb
r5187:03b78dac4092
r4765:7228ab2d82f2
r5199:e855b32ff1fa
r524:21cbb8d1771d
r5187:03b78dac4092
r5199:e855b32ff1fa
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r4765:7228ab2d82f2
r5187:03b78dac4092
r5215:5975e06c9f82
r5187:03b78dac4092
r5199:e855b32ff1fa
r5190:f351385d22fb
r5190:f351385d22fb
r5190:f351385d22fb
r5190:f351385d22fb
r5190:f351385d22fb
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r4765:7228ab2d82f2
r5190:f351385d22fb
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r5195:e3fe1da3f768
r5187:03b78dac4092
r5187:03b78dac4092
r5190:f351385d22fb
r5187:03b78dac4092
r5199:e855b32ff1fa
r5187:03b78dac4092
r5190:f351385d22fb
r5190:f351385d22fb
r5190:f351385d22fb
r4770:543b61edc4ba
r4770:543b61edc4ba
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r5199:e855b32ff1fa
r4765:7228ab2d82f2
r4770:543b61edc4ba
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4767:0bea6c082a65
r5200:6c1c28578fd0
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4930:fb0ce02440ea
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4930:fb0ce02440ea
r4765:7228ab2d82f2
r4930:fb0ce02440ea
r4765:7228ab2d82f2
r524:21cbb8d1771d
r5187:03b78dac4092
r5187:03b78dac4092
r524:21cbb8d1771d
r524:21cbb8d1771d
r0:d63b455452f6
r0:d63b455452f6
r5199:e855b32ff1fa
r2952:e97f823d2dbe
r4765:7228ab2d82f2
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5191:8c925f68db4f
r5200:6c1c28578fd0
r5200:6c1c28578fd0
r4766:faaab7cb5a8d
r4766:faaab7cb5a8d
r4766:faaab7cb5a8d
r5191:8c925f68db4f
r4765:7228ab2d82f2
r0:d63b455452f6
r4765:7228ab2d82f2
r5187:03b78dac4092
r4765:7228ab2d82f2
r4764:db558507b5fe
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r5187:03b78dac4092
r5199:e855b32ff1fa
r5199:e855b32ff1fa
r5191:8c925f68db4f
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r5187:03b78dac4092
r4770:543b61edc4ba
r4770:543b61edc4ba
r4764:db558507b5fe
r4770:543b61edc4ba
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r0:d63b455452f6
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r4765:7228ab2d82f2
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r5187:03b78dac4092
r4765:7228ab2d82f2
r4765:7228ab2d82f2
r5187:03b78dac4092
r0:d63b455452f6
r5187:03b78dac4092
r5187:03b78dac4092
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r4764:db558507b5fe
r5187:03b78dac4092
r968:cc00c557f4bf
r5187:03b78dac4092
r5187:03b78dac4092
r4764:db558507b5fe
r4770:543b61edc4ba
r5187:03b78dac4092
r5199:e855b32ff1fa
r5191:8c925f68db4f
r5191:8c925f68db4f
r4770:543b61edc4ba
r4770:543b61edc4ba
r4770:543b61edc4ba
r4764:db558507b5fe
r4764:db558507b5fe
r0:d63b455452f6
r4764:db558507b5fe
r4764:db558507b5fe
r5187:03b78dac4092
r5187:03b78dac4092
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r0:d63b455452f6
r5187:03b78dac4092
r0:d63b455452f6
r867:c270355a1b34
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4442:b5f1e6ca1edf
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r5124:e841fbeef573
r0:d63b455452f6
r4764:db558507b5fe
r867:c270355a1b34
r867:c270355a1b34
r4000:702cb45b8eab
r193:6aa65dc5a4b4
r0:d63b455452f6
r1901:e806afa50723
r5187:03b78dac4092
r0:d63b455452f6
r0:d63b455452f6
r5187:03b78dac4092
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2593:575b757911eb
r2593:575b757911eb
r2593:575b757911eb
r2593:575b757911eb
r2593:575b757911eb
r4638:2cf83a5d0131
r3845:e0b12600a260
r2593:575b757911eb
r2593:575b757911eb
r4442:b5f1e6ca1edf
r0:d63b455452f6
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r3919:4c67b41f0b48
r4429:9a1fdade8310
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r1922:d5634e2fe7b3
r0:d63b455452f6
r3919:4c67b41f0b48
r1922:d5634e2fe7b3
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r3049:fe328ef6dad7
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r1922:d5634e2fe7b3
r0:d63b455452f6
r3919:4c67b41f0b48
r0:d63b455452f6
r2593:575b757911eb
r3919:4c67b41f0b48
r3919:4c67b41f0b48
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r4049:81f2dc20439f
r3919:4c67b41f0b48
r0:d63b455452f6
r0:d63b455452f6
r2275:5b6aa087e1e2
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4939:8315d865f2fb
r4938:ccfdb2e58d04
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r4937:3124578cbfd9
r1485:a9e66a254466
r0:d63b455452f6
r0:d63b455452f6
r4171:bf306290d87e
r2561:bff0d1890738
r0:d63b455452f6
r0:d63b455452f6
r1802:acc0f0bda113
r0:d63b455452f6
r1802:acc0f0bda113
r0:d63b455452f6
r4709:fa407dc35407
r0:d63b455452f6
r919:57ef81f1ba8e
r193:6aa65dc5a4b4
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r193:6aa65dc5a4b4
r4871:e5e554491c47
r4871:e5e554491c47
r4871:e5e554491c47
r4709:fa407dc35407
r2867:03d72c58b3ea
r2867:03d72c58b3ea
r3393:9d958dc7a778
r2867:03d72c58b3ea
r4709:fa407dc35407
r2867:03d72c58b3ea
r2867:03d72c58b3ea
r2867:03d72c58b3ea
r1387:87d700a7ee55
r1387:87d700a7ee55
r1387:87d700a7ee55
r0:d63b455452f6
r534:46e9db8de483
r2261:e6fb9fd7ed61
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4251:8615bb529756
r4251:8615bb529756
r4251:8615bb529756
r4251:8615bb529756
r4251:8615bb529756
r0:d63b455452f6
r3477:aa44670525a9
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r555:cefc6ad212ed
r0:d63b455452f6
r0:d63b455452f6
r4527:4b01e6756ce8
r3477:aa44670525a9
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4527:4b01e6756ce8
r534:46e9db8de483
r4633:85fa4aef8071
r4412:141c517ed974
r4412:141c517ed974
r4412:141c517ed974
r4412:141c517ed974
r3477:aa44670525a9
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r395:9c19f4df13fc
r4527:4b01e6756ce8
r395:9c19f4df13fc
r3477:aa44670525a9
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r3477:aa44670525a9
r4077:259c4c4aacad
r0:d63b455452f6
r4077:259c4c4aacad
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2103:d4b2ab6a0279
r2517:f67017080a5a
r2113:600d6ea0d897
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4634:c040de534aac
r919:57ef81f1ba8e
r0:d63b455452f6
r2952:e97f823d2dbe
r755:02ea8c27f87d
r193:6aa65dc5a4b4
r0:d63b455452f6
r4434:c817458d470e
r0:d63b455452f6
r0:d63b455452f6
r4434:c817458d470e
r601:44f11989d19e
r4506:98668beebd0e
r0:d63b455452f6
r755:02ea8c27f87d
r193:6aa65dc5a4b4
r0:d63b455452f6
r755:02ea8c27f87d
r193:6aa65dc5a4b4
r0:d63b455452f6
r755:02ea8c27f87d
r0:d63b455452f6
r0:d63b455452f6
r755:02ea8c27f87d
r0:d63b455452f6
r0:d63b455452f6
r755:02ea8c27f87d
r4712:ace1fd623579
r0:d63b455452f6
r2244:0a45bb36cb48
r2244:0a45bb36cb48
r2244:0a45bb36cb48
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1485:a9e66a254466
r4634:c040de534aac
r4634:c040de534aac
r4634:c040de534aac
r4634:c040de534aac
r1485:a9e66a254466
r1485:a9e66a254466
r0:d63b455452f6
r338:3990dde2fc4f
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4171:bf306290d87e
r4702:5afc739029ec
r0:d63b455452f6
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r4702:5afc739029ec
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r5070:6da414a78d0e
r0:d63b455452f6
r1485:a9e66a254466
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4171:bf306290d87e
r0:d63b455452f6
r4171:bf306290d87e
r0:d63b455452f6
r2561:bff0d1890738
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1790:aafbb2ff1ae7
r0:d63b455452f6
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r0:d63b455452f6
r0:d63b455452f6
r534:46e9db8de483
r534:46e9db8de483
r534:46e9db8de483
r5163:a7c06b0ec769
r5163:a7c06b0ec769
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1790:aafbb2ff1ae7
r0:d63b455452f6
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r534:46e9db8de483
r4329:7372b441f7d8
r534:46e9db8de483
r0:d63b455452f6
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r1790:aafbb2ff1ae7
r0:d63b455452f6
r0:d63b455452f6
r4896:6cf2d452f4cc
r534:46e9db8de483
r5163:a7c06b0ec769
r5163:a7c06b0ec769
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r5187:03b78dac4092
r5187:03b78dac4092
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r0:d63b455452f6
r0:d63b455452f6
r919:57ef81f1ba8e
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r0:d63b455452f6
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4868:203ecee5a52e
r0:d63b455452f6
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r0:d63b455452f6
r4492:d00de011a64a
r0:d63b455452f6
r4434:c817458d470e
r4492:d00de011a64a
r4492:d00de011a64a
r4868:203ecee5a52e
r4868:203ecee5a52e
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4709:fa407dc35407
r4709:fa407dc35407
r0:d63b455452f6
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r156:dd00481b3251
r534:46e9db8de483
r534:46e9db8de483
r0:d63b455452f6
r0:d63b455452f6
r4492:d00de011a64a
r193:6aa65dc5a4b4
r0:d63b455452f6
r193:6aa65dc5a4b4
r2252:82a854ab03b0
r534:46e9db8de483
r534:46e9db8de483
r0:d63b455452f6
r0:d63b455452f6
r3477:aa44670525a9
r534:46e9db8de483
r534:46e9db8de483
r3401:4b5dcf559b96
r0:d63b455452f6
r534:46e9db8de483
r534:46e9db8de483
r0:d63b455452f6
r0:d63b455452f6
r534:46e9db8de483
r534:46e9db8de483
r0:d63b455452f6
r0:d63b455452f6
r534:46e9db8de483
r534:46e9db8de483
r2959:9dd09fc8fb13
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r193:6aa65dc5a4b4
r0:d63b455452f6
r0:d63b455452f6
r2822:5fd54c42ddc7
r2952:e97f823d2dbe
r2959:9dd09fc8fb13
r2602:e9a03af71c79
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r2602:e9a03af71c79
r2602:e9a03af71c79
r3844:fefde6f58157
r3844:fefde6f58157
r2602:e9a03af71c79
r2602:e9a03af71c79
r4868:203ecee5a52e
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4492:d00de011a64a
r4492:d00de011a64a
r0:d63b455452f6
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r4868:203ecee5a52e
r0:d63b455452f6
r4492:d00de011a64a
r0:d63b455452f6
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r0:d63b455452f6
r2959:9dd09fc8fb13
r4492:d00de011a64a
r0:d63b455452f6
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r4492:d00de011a64a
r5163:a7c06b0ec769
r5163:a7c06b0ec769
r0:d63b455452f6
r2959:9dd09fc8fb13
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1790:aafbb2ff1ae7
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r1790:aafbb2ff1ae7
r4634:c040de534aac
r0:d63b455452f6
r919:57ef81f1ba8e
r534:46e9db8de483
r4299:ee21d1b71ef0
r0:d63b455452f6
r4434:c817458d470e
r156:dd00481b3251
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r156:dd00481b3251
r0:d63b455452f6
r919:57ef81f1ba8e
r156:dd00481b3251
r1790:aafbb2ff1ae7
r1790:aafbb2ff1ae7
r193:6aa65dc5a4b4
r2819:50c046aedc63
r0:d63b455452f6
r0:d63b455452f6
r4434:c817458d470e
r4434:c817458d470e
r4434:c817458d470e
r4434:c817458d470e
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r4709:fa407dc35407
r4634:c040de534aac
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2548:9570bbbdacab
r4634:c040de534aac
r4634:c040de534aac
r1820:335b057adac7
r1820:335b057adac7
r1820:335b057adac7
r2548:9570bbbdacab
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r4634:c040de534aac
r2959:9dd09fc8fb13
r4634:c040de534aac
r4547:6bbd94aaa2a8
r2959:9dd09fc8fb13
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r176:7d9100f64c27
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r5070:6da414a78d0e
r0:d63b455452f6
r2959:9dd09fc8fb13
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4171:bf306290d87e
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r5071:949c4f7f3fa6
r0:d63b455452f6
r0:d63b455452f6
r0:d63b455452f6
r4547:6bbd94aaa2a8
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r2959:9dd09fc8fb13
r0:d63b455452f6
r0:d63b455452f6
/* $Id$ */

#include "stdafx.h"
#include "openttd.h"
#include "debug.h"
#include "functions.h"
#include "rail_map.h"
#include "table/sprites.h"
#include "table/strings.h"
#include "map.h"
#include "engine.h"
#include "window.h"
#include "gui.h"
#include "gfx.h"
#include "vehicle.h"
#include "viewport.h"
#include "station.h"
#include "command.h"
#include "player.h"
#include "vehicle_gui.h"
#include "depot.h"
#include "train.h"
#include "newgrf_engine.h"
#include "date.h"
#include "strings.h"

enum BuildTrainWidgets {
	BUILD_TRAIN_WIDGET_CLOSEBOX = 0,
	BUILD_TRAIN_WIDGET_CAPTION,
	BUILD_TRAIN_WIDGET_SORT_ASCENDING_DESCENDING,
	BUILD_TRAIN_WIDGET_SORT_TEXT,
	BUILD_TRAIN_WIDGET_SORT_DROPDOWN,
	BUILD_TRAIN_WIDGET_LIST,
	BUILD_TRAIN_WIDGET_SCROLLBAR,
	BUILD_TRAIN_WIDGET_PANEL,
	BUILD_TRAIN_WIDGET_BUILD,
	BUILD_TRAIN_WIDGET_RENAME,
	BUILD_TRAIN_WIDGET_RESIZE,
};

static const Widget _new_rail_vehicle_widgets[] = {
	{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
	{    WWT_CAPTION,   RESIZE_NONE,    14,    11,   227,     0,    13, STR_JUST_STRING,        STR_018C_WINDOW_TITLE_DRAG_THIS},
	{ WWT_PUSHTXTBTN,   RESIZE_NONE,    14,     0,    80,    14,    25, STR_SORT_BY,            STR_SORT_ORDER_TIP},
	{      WWT_PANEL,   RESIZE_NONE,    14,    81,   215,    14,    25, 0x0,                    STR_SORT_CRITERIA_TIP},
	{    WWT_TEXTBTN,   RESIZE_NONE,    14,   216,   227,    14,    25, STR_0225,               STR_SORT_CRITERIA_TIP},
	{     WWT_MATRIX, RESIZE_BOTTOM,    14,     0,   215,    26,   137, 0x801,                  STR_8843_TRAIN_VEHICLE_SELECTION},
	{  WWT_SCROLLBAR, RESIZE_BOTTOM,    14,   216,   227,    26,   137, 0x0,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
	{      WWT_PANEL,     RESIZE_TB,    14,     0,   227,   138,   239, 0x0,                    STR_NULL},
	{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   107,   240,   251, STR_881F_BUILD_VEHICLE, STR_8844_BUILD_THE_HIGHLIGHTED_TRAIN},
	{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,   108,   215,   240,   251, STR_8820_RENAME,        STR_8845_RENAME_TRAIN_VEHICLE_TYPE},
	{  WWT_RESIZEBOX,     RESIZE_TB,    14,   216,   227,   240,   251, 0x0,                    STR_RESIZE_BUTTON},
	{   WIDGETS_END},
};

static bool _internal_sort_order; // false = ascending, true = descending
static byte _last_sort_criteria = 0;
static bool _last_sort_order = false;

static int CDECL TrainEngineNumberSorter(const void *a, const void *b)
{
	const EngineID va = *(const EngineID*)a;
	const EngineID vb = *(const EngineID*)b;
	int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb);

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b)
{
	EngineID va = *(const EngineID*)a;
	EngineID vb = *(const EngineID*)b;
	int val_a = ((RailVehInfo(va)->flags & RVI_WAGON) != 0) ? 1 : 0;
	int val_b = ((RailVehInfo(vb)->flags & RVI_WAGON) != 0) ? 1 : 0;
	int r = val_a - val_b;

	/* Use EngineID to sort instead since we want consistent sorting */
	if (r == 0) return TrainEngineNumberSorter(a, b);

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEngineCostSorter(const void *a, const void *b)
{
	int va = RailVehInfo(*(const EngineID*)a)->base_cost;
	int vb = RailVehInfo(*(const EngineID*)b)->base_cost;
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEngineSpeedSorter(const void *a, const void *b)
{
	int va = RailVehInfo(*(const EngineID*)a)->max_speed;
	int vb = RailVehInfo(*(const EngineID*)b)->max_speed;
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEnginePowerSorter(const void *a, const void *b)
{
	const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
	const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);

	int va = rvi_a->power << (rvi_a->flags & RVI_MULTIHEAD ? 1 : 0);
	int vb = rvi_b->power << (rvi_b->flags & RVI_MULTIHEAD ? 1 : 0);
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEngineIntroDateSorter(const void *a, const void *b)
{
	int va = GetEngine(*(const EngineID*)a)->intro_date;
	int vb = GetEngine(*(const EngineID*)b)->intro_date;
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static EngineID _last_engine; // cached vehicle to hopefully speed up name-sorting

static char _bufcache[64]; // used together with _last_vehicle to hopefully speed up stringsorting
static int CDECL TrainEngineNameSorter(const void *a, const void *b)
{
	const EngineID va = *(const EngineID*)a;
	const EngineID vb = *(const EngineID*)b;
	char buf1[64];
	int r;

	GetString(buf1, GetCustomEngineName(va), lastof(buf1));

	if (vb != _last_engine) {
		_last_engine = vb;
		_bufcache[0] = '\0';

		GetString(_bufcache, GetCustomEngineName(vb), lastof(_bufcache));
	}

	r =  strcasecmp(buf1, _bufcache); // sort by name

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b)
{
	const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
	const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);

	int va = rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class] * (rvi_a->flags & RVI_MULTIHEAD ? 2 : 1);
	int vb = rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class] * (rvi_b->flags & RVI_MULTIHEAD ? 2 : 1);
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void *b)
{
	const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
	const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);

	/* Here we are using a few tricks to get the right sort.
	 * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
	 * we will actually calculate cunning cost/power (to make it more than 1).
	 * Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
	 * Another thing is that both power and running costs should be doubled for multiheaded engines.
	 * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */
	int va = (rvi_a->running_cost_base * _price.running_rail[rvi_a->running_cost_class]) / max(1, rvi_a->power);
	int vb = (rvi_b->running_cost_base * _price.running_rail[rvi_b->running_cost_class]) / max(1, rvi_b->power);
	int r = vb - va;

	return _internal_sort_order ? -r : r;
}

static int CDECL TrainEngineReliabilitySorter(const void *a, const void *b)
{
	int va = GetEngine(*(const EngineID*)a)->reliability;
	int vb = GetEngine(*(const EngineID*)b)->reliability;
	int r = va - vb;

	return _internal_sort_order ? -r : r;
}

static EngList_SortTypeFunction * const _engine_sorter[] = {
	&TrainEngineNumberSorter,
	&TrainEngineCostSorter,
	&TrainEngineSpeedSorter,
	&TrainEnginePowerSorter,
	&TrainEngineIntroDateSorter,
	&TrainEngineNameSorter,
	&TrainEngineRunningCostSorter,
	&TrainEnginePowerVsRunningCostSorter,
	&TrainEngineReliabilitySorter,
};

static const StringID _engine_sort_listing[] = {
	STR_ENGINE_SORT_ENGINE_ID,
	STR_ENGINE_SORT_COST,
	STR_SORT_BY_MAX_SPEED,
	STR_ENGINE_SORT_POWER,
	STR_ENGINE_SORT_INTRO_DATE,
	STR_SORT_BY_DROPDOWN_NAME,
	STR_ENGINE_SORT_RUNNING_COST,
	STR_ENGINE_SORT_POWER_VS_RUNNING_COST,
	STR_SORT_BY_RELIABILITY,
	INVALID_STRING_ID
};

/**
 * Draw the purchase info details of train engine at a given location.
 * @param x,y location where to draw the info
 * @param engine_number the engine of which to draw the info of
 */
void DrawTrainEnginePurchaseInfo(int x, int y, uint w, EngineID engine_number)
{
	const RailVehicleInfo *rvi = RailVehInfo(engine_number);
	const Engine *e = GetEngine(engine_number);
	int multihead = (rvi->flags&RVI_MULTIHEAD?1:0);
	YearMonthDay ymd;
	ConvertDateToYMD(e->intro_date, &ymd);

	/* Purchase Cost - Engine weight */
	SetDParam(0, rvi->base_cost * (_price.build_railvehicle >> 3) >> 5);
	SetDParam(1, rvi->weight << multihead);
	DrawString(x,y, STR_PURCHASE_INFO_COST_WEIGHT, 0);
	y += 10;

	/* Max speed - Engine power */
	SetDParam(0, rvi->max_speed);
	SetDParam(1, rvi->power << multihead);
	DrawString(x,y, STR_PURCHASE_INFO_SPEED_POWER, 0);
	y += 10;

	/* Running cost */
	SetDParam(0, (rvi->running_cost_base * _price.running_rail[rvi->running_cost_class] >> 8) << multihead);
	DrawString(x,y, STR_PURCHASE_INFO_RUNNINGCOST, 0);
	y += 10;

	/* Powered wagons power - Powered wagons extra weight */
	if (rvi->pow_wag_power != 0) {
		SetDParam(0, rvi->pow_wag_power);
		SetDParam(1, rvi->pow_wag_weight);
		DrawString(x,y, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT, 0);
		y += 10;
	};

	/* Cargo type + capacity, or N/A */
	if (rvi->capacity == 0) {
		SetDParam(0, CT_INVALID);
		SetDParam(2, STR_EMPTY);
	} else {
		SetDParam(0, rvi->cargo_type);
		SetDParam(1, (rvi->capacity * (CountArticulatedParts(engine_number) + 1)) << multihead);
		SetDParam(2, STR_9842_REFITTABLE);
	}
	DrawString(x,y, STR_PURCHASE_INFO_CAPACITY, 0);
	y += 10;

	/* Design date - Life length */
	SetDParam(0, ymd.year);
	SetDParam(1, e->lifelength);
	DrawString(x,y, STR_PURCHASE_INFO_DESIGNED_LIFE, 0);
	y += 10;

	/* Reliability */
	SetDParam(0, e->reliability * 100 >> 16);
	DrawString(x,y, STR_PURCHASE_INFO_RELIABILITY, 0);
	y += 10;

	/* Additional text from NewGRF */
	y += ShowAdditionalText(x, y, w, engine_number);
	if (rvi->capacity > 0) y += ShowRefitOptionsList(x, y, w, engine_number);
}

/**
 * Draw the purchase info details of a train wagon at a given location.
 * @param x,y location where to draw the info
 * @param engine_number the engine of which to draw the info of
 */
void DrawTrainWagonPurchaseInfo(int x, int y, uint w, EngineID engine_number)
{
	const RailVehicleInfo *rvi = RailVehInfo(engine_number);
	bool refittable = (EngInfo(engine_number)->refit_mask != 0);

	/* Purchase cost */
	SetDParam(0, (rvi->base_cost * _price.build_railwagon) >> 8);
	DrawString(x, y, STR_PURCHASE_INFO_COST, 0);
	y += 10;

	/* Wagon weight - (including cargo) */
	SetDParam(0, rvi->weight);
	SetDParam(1, (_cargoc.weights[rvi->cargo_type] * rvi->capacity >> 4) + rvi->weight);
	DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, 0);
	y += 10;

	/* Cargo type + capacity, or N/A */
	if (rvi->capacity == 0) {
		SetDParam(0, CT_INVALID);
		SetDParam(2, STR_EMPTY);
	} else {
		SetDParam(0, rvi->cargo_type);
		SetDParam(1, rvi->capacity * (CountArticulatedParts(engine_number) + 1));
		SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY);
	}
	DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, 0);
	y += 10;

	/* Wagon speed limit, displayed if above zero */
	if (rvi->max_speed > 0 && _patches.wagon_speed_limits) {
		SetDParam(0, rvi->max_speed);
		DrawString(x,y, STR_PURCHASE_INFO_SPEED, 0);
		y += 10;
	}

	/* Additional text from NewGRF */
	y += ShowAdditionalText(x, y, w, engine_number);
	y += ShowRefitOptionsList(x, y, w, engine_number);
}

void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
{
	Vehicle *v, *found;

	if (!success) return;

	// find a locomotive in the depot.
	found = NULL;
	FOR_ALL_VEHICLES(v) {
		if (v->type == VEH_Train && IsFrontEngine(v) &&
				v->tile == tile &&
				v->u.rail.track == 0x80) {
			if (found != NULL) return; // must be exactly one.
			found = v;
		}
	}

	// if we found a loco,
	if (found != NULL) {
		found = GetLastVehicleInChain(found);
		// put the new wagon at the end of the loco.
		DoCommandP(0, _new_vehicle_id | (found->index << 16), 0, NULL, CMD_MOVE_RAIL_VEHICLE);
		RebuildVehicleLists();
	}
}

void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2)
{
	const Vehicle *v;

	if (!success) return;

	v = GetVehicle(_new_vehicle_id);
	if (tile == _backup_orders_tile) {
		_backup_orders_tile = 0;
		RestoreVehicleOrders(v, _backup_orders_data);
	}
	ShowTrainViewWindow(v);
}

void CcCloneTrain(bool success, TileIndex tile, uint32 p1, uint32 p2)
{
	if (success) ShowTrainViewWindow(GetVehicle(_new_vehicle_id));
}

static void engine_drawing_loop(const EngineList *engines, int x, int *y, EngineID sel, EngineID position, int16 show_max)
{
	int count = min(EngList_Count(engines), show_max);
	for (; position < count; *y += 14, position++) {
		EngineID id = (*engines)[position];
		DrawString(x + 59, *y + 2, GetCustomEngineName(id), sel == id ? 0xC : 0x10);
		DrawTrainEngine(x + 29, *y + 6, id, GetEnginePalette(id, _local_player));
	}
}

static void GenerateBuildList(Window *w)
{
	EngineID eid, sel_id;
	int num_engines = 0;
	int num_wagons  = 0;
	buildvehicle_d *bv = &WP(w, buildvehicle_d);

	bv->filter.railtype = (w->window_number == 0) ? RAILTYPE_END : GetRailType(w->window_number);

	EngList_RemoveAll(&bv->eng_list);

	/* Make list of all available train engines and wagons.
	 * Also check to see if the previously selected engine is still available,
	 * and if not, reset selection to INVALID_ENGINE. This could be the case
	 * when engines become obsolete and are removed */
	for (sel_id = INVALID_ENGINE, eid = 0; eid < NUM_TRAIN_ENGINES; eid++) {
		const Engine *e = GetEngine(eid);
		const RailVehicleInfo *rvi = RailVehInfo(eid);

		if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(e->railtype, bv->filter.railtype)) continue;
		if (!IsEngineBuildable(eid, VEH_Train, _local_player)) continue;

		EngList_Add(&bv->eng_list, eid);
		if ((rvi->flags & RVI_WAGON) == 0) {
			num_engines++;
		} else {
			num_wagons++;
		}

		if (eid == bv->sel_engine) sel_id = eid;
	}

	bv->sel_engine = sel_id;

	// make engines first, and then wagons, sorted by ListPositionOfEngine()
	_internal_sort_order = false;
	EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter);

	// and then sort engines
	_internal_sort_order = bv->descending_sort_order;
	EngList_SortPartial(&bv->eng_list, _engine_sorter[bv->sort_criteria], 0, num_engines);

	// and finally sort wagons
	EngList_SortPartial(&bv->eng_list, _engine_sorter[bv->sort_criteria], num_engines, num_wagons);
}

static void DrawTrainBuildWindow(Window *w)
{
	const buildvehicle_d *bv = &WP(w, buildvehicle_d);
	int x = 1;
	int y = 27;
	EngineID selected_id = bv->sel_engine;
	int max = w->vscroll.pos + w->vscroll.cap;

	SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in

	SetVScrollCount(w, EngList_Count(&bv->eng_list));
	SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES);
	DrawWindowWidgets(w);

	/* Draw the engines */
	engine_drawing_loop(&bv->eng_list, x, &y, selected_id, w->vscroll.pos, max);

	if (selected_id != INVALID_ENGINE) {
		const RailVehicleInfo *rvi = RailVehInfo(selected_id);
		const Widget *wi = &w->widget[BUILD_TRAIN_WIDGET_PANEL];

		if (rvi->flags & RVI_WAGON) {
			DrawTrainWagonPurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, selected_id);
		} else {
			DrawTrainEnginePurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, selected_id);
		}
	}
	DrawString(85, 15, _engine_sort_listing[bv->sort_criteria], 0x10);
	DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, 0x10);
}

static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
{
	buildvehicle_d *bv = &WP(w, buildvehicle_d);
	switch (e->event) {
		case WE_CREATE:
			EngList_Create(&bv->eng_list);
			bv->sel_engine            = INVALID_ENGINE;
			bv->sort_criteria         = _last_sort_criteria;
			bv->descending_sort_order = _last_sort_order;
			GenerateBuildList(w);
			/* Select the first engine in the list as default when opening the window */
			if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0];
			break;

		case WE_INVALIDATE_DATA:
			GenerateBuildList(w);
			break;

		case WE_DESTROY:
			EngList_Destroy(&bv->eng_list);
			break;

		case WE_PAINT:
			DrawTrainBuildWindow(w);
			break;

		case WE_CLICK: {
			switch (e->we.click.widget) {
				case BUILD_TRAIN_WIDGET_SORT_ASCENDING_DESCENDING:
					bv->descending_sort_order ^= true;
					_last_sort_order = bv->descending_sort_order;
					GenerateBuildList(w);
					SetWindowDirty(w);
					break;

				case BUILD_TRAIN_WIDGET_SORT_TEXT: case BUILD_TRAIN_WIDGET_SORT_DROPDOWN:/* Select sorting criteria dropdown menu */
					ShowDropDownMenu(w, _engine_sort_listing, bv->sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0);
					return;

				case BUILD_TRAIN_WIDGET_LIST: {
					uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos;
					uint num_items = EngList_Count(&bv->eng_list);
					bv->sel_engine = (i < num_items) ? bv->eng_list[i] : INVALID_ENGINE;
					SetWindowDirty(w);
					break;
				}

				case BUILD_TRAIN_WIDGET_BUILD: {
					EngineID sel_eng = bv->sel_engine;
					if (sel_eng != INVALID_ENGINE)
						DoCommandP(w->window_number, sel_eng, 0, (RailVehInfo(sel_eng)->flags & RVI_WAGON) ? CcBuildWagon : CcBuildLoco, CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE));
					break;
				}

				case BUILD_TRAIN_WIDGET_RENAME: {
					EngineID sel_eng = bv->sel_engine;
					if (sel_eng != INVALID_ENGINE) {
						bv->rename_engine = sel_eng;
						ShowQueryString(GetCustomEngineName(sel_eng), STR_886A_RENAME_TRAIN_VEHICLE_TYPE, 31, 160, w->window_class, w->window_number, CS_ALPHANUMERAL);
					}
					break;
				}
			}
		}
		break;

		case WE_ON_EDIT_TEXT: {
			if (e->we.edittext.str[0] != '\0') {
				_cmd_text = e->we.edittext.str;
				DoCommandP(0, bv->rename_engine, 0, NULL, CMD_RENAME_ENGINE | CMD_MSG(STR_886B_CAN_T_RENAME_TRAIN_VEHICLE));
			}
			break;
		}

		case WE_DROPDOWN_SELECT: /* we have selected a dropdown item in the list */
			if (bv->sort_criteria != e->we.dropdown.index) {
				bv->sort_criteria = _last_sort_criteria = e->we.dropdown.index;
				GenerateBuildList(w);
				SetWindowDirty(w);
			}
			break;

		case WE_RESIZE: {
			if (e->we.sizing.diff.y == 0) break;

			w->vscroll.cap += e->we.sizing.diff.y / 14;
			w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1;
			break;
		}
	}
}

static const WindowDesc _new_rail_vehicle_desc = {
	WDP_AUTO, WDP_AUTO, 228, 252,
	WC_BUILD_VEHICLE,0,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
	_new_rail_vehicle_widgets,
	NewRailVehicleWndProc
};

void ShowBuildTrainWindow(TileIndex tile)
{
	Window *w;

	DeleteWindowById(WC_BUILD_VEHICLE, tile);

	w = AllocateWindowDescFront(&_new_rail_vehicle_desc, tile);
	w->vscroll.cap = 8;
	w->widget[BUILD_TRAIN_WIDGET_LIST].data = (w->vscroll.cap << 8) + 1;

	w->resize.step_height = 14;
	w->resize.height = w->height - 14 * 4; // Minimum of 4 vehicles in the display

	if (tile != 0) {
		w->caption_color = GetTileOwner(tile);
		WP(w,buildvehicle_d).filter.railtype = GetRailType(tile);
	} else {
		w->caption_color = _local_player;
		WP(w,buildvehicle_d).filter.railtype = RAILTYPE_END;
	}
}

/**
 * Get the number of pixels for the given wagon length.
 * @param len Length measured in 1/8ths of a standard wagon.
 * @return Number of pixels across.
 */
int WagonLengthToPixels(int len) {
	return (len * _traininfo_vehicle_width) / 8;
}

void DrawTrainImage(const Vehicle *v, int x, int y, int count, int skip, VehicleID selection)
{
	DrawPixelInfo tmp_dpi, *old_dpi;
	int dx = -(skip * 8) / _traininfo_vehicle_width;
	/* Position of highlight box */
	int highlight_l = 0;
	int highlight_r = 0;

	if (!FillDrawPixelInfo(&tmp_dpi, x - 2, y - 1, count + 1, 14)) return;

	count = (count * 8) / _traininfo_vehicle_width;

	old_dpi = _cur_dpi;
	_cur_dpi = &tmp_dpi;

	do {
		int width = v->u.rail.cached_veh_length;

		if (dx + width > 0) {
			if (dx <= count) {
				PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
				DrawSprite(GetTrainImage(v, DIR_W) | pal, 16 + WagonLengthToPixels(dx), 7 + (is_custom_sprite(RailVehInfo(v->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
				if (v->index == selection) {
					/* Set the highlight position */
					highlight_l = WagonLengthToPixels(dx) + 1;
					highlight_r = WagonLengthToPixels(dx + width) + 1;
				}
			}
		}
		dx += width;

		v = v->next;
	} while (dx < count && v != NULL);

	if (highlight_l != highlight_r) {
		/* Draw the highlight. Now done after drawing all the engines, as
		 * the next engine after the highlight could overlap it. */
		DrawFrameRect(highlight_l, 0, highlight_r, 13, 15, FR_BORDERONLY);
	}

	_cur_dpi = old_dpi;
}

static const Widget _train_view_widgets[] = {
{   WWT_CLOSEBOX,  RESIZE_NONE, 14,   0,  10,   0,  13, STR_00C5,                STR_018B_CLOSE_WINDOW },
{    WWT_CAPTION, RESIZE_RIGHT, 14,  11, 237,   0,  13, STR_882E,                STR_018C_WINDOW_TITLE_DRAG_THIS },
{  WWT_STICKYBOX,    RESIZE_LR, 14, 238, 249,   0,  13, 0x0,                     STR_STICKY_BUTTON },
{      WWT_PANEL,    RESIZE_RB, 14,   0, 231,  14, 121, 0x0,                     STR_NULL },
{      WWT_INSET,    RESIZE_RB, 14,   2, 229,  16, 119, 0x0,                     STR_NULL },
{    WWT_PUSHBTN,   RESIZE_RTB, 14,   0, 237, 122, 133, 0x0,                     STR_8846_CURRENT_TRAIN_ACTION_CLICK },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  14,  31, SPR_CENTRE_VIEW_VEHICLE, STR_8848_CENTER_MAIN_VIEW_ON_TRAIN },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  32,  49, SPR_SEND_TRAIN_TODEPOT,  STR_8849_SEND_TRAIN_TO_DEPOT },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  50,  67, SPR_IGNORE_SIGNALS,      STR_884A_FORCE_TRAIN_TO_PROCEED },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  68,  85, SPR_FORCE_VEHICLE_TURN,  STR_884B_REVERSE_DIRECTION_OF_TRAIN },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  86, 103, SPR_SHOW_ORDERS,         STR_8847_SHOW_TRAIN_S_ORDERS },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249, 104, 121, SPR_SHOW_VEHICLE_DETAILS,STR_884C_SHOW_TRAIN_DETAILS },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  68,  85, SPR_REFIT_VEHICLE,       STR_RAIL_REFIT_VEHICLE_TO_CARRY },
{ WWT_PUSHIMGBTN,    RESIZE_LR, 14, 232, 249,  32,  49, SPR_CLONE_TRAIN,         STR_CLONE_TRAIN_INFO },
{      WWT_PANEL,   RESIZE_LRB, 14, 232, 249, 122, 121, 0x0,                     STR_NULL },
{  WWT_RESIZEBOX,  RESIZE_LRTB, 14, 238, 249, 122, 133, 0x0,                     STR_NULL },
{ WIDGETS_END }
};

static void ShowTrainDetailsWindow(const Vehicle *v);

static void TrainViewWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
	case WE_PAINT: {
		const Vehicle *v, *u;
		StringID str;
		bool is_localplayer;

		v = GetVehicle(w->window_number);

		is_localplayer = v->owner == _local_player;
		SetWindowWidgetDisabledState(w,  7, !is_localplayer);
		SetWindowWidgetDisabledState(w,  8, !is_localplayer);
		SetWindowWidgetDisabledState(w,  9, !is_localplayer);
		SetWindowWidgetDisabledState(w, 13, !is_localplayer);

		/* Disable cargo refit button, until we know we can enable it below. */
		DisableWindowWidget(w, 12);

		if (is_localplayer) {
			/* See if any vehicle can be refitted */
			for (u = v; u != NULL; u = u->next) {
				if (EngInfo(u->engine_type)->refit_mask != 0 ||
						(!(RailVehInfo(v->engine_type)->flags & RVI_WAGON) && v->cargo_cap != 0)) {
					EnableWindowWidget(w, 12);
					/* We have a refittable carriage, bail out */
					break;
				}
			}
		}

		/* draw widgets & caption */
		SetDParam(0, v->string_id);
		SetDParam(1, v->unitnumber);
		DrawWindowWidgets(w);

		if (v->u.rail.crash_anim_pos != 0) {
			str = STR_8863_CRASHED;
		} else if (v->breakdown_ctr == 1) {
			str = STR_885C_BROKEN_DOWN;
		} else if (v->vehstatus & VS_STOPPED) {
			if (v->u.rail.last_speed == 0) {
				if (v->u.rail.cached_power == 0) {
					str = STR_TRAIN_NO_POWER;
				} else {
					str = STR_8861_STOPPED;
				}
			} else {
				SetDParam(0, v->u.rail.last_speed);
				str = STR_TRAIN_STOPPING + _patches.vehicle_speed;
			}
		} else {
			switch (v->current_order.type) {
			case OT_GOTO_STATION: {
				str = STR_HEADING_FOR_STATION + _patches.vehicle_speed;
				SetDParam(0, v->current_order.dest);
				SetDParam(1, v->u.rail.last_speed);
			} break;

			case OT_GOTO_DEPOT: {
				Depot *dep = GetDepot(v->current_order.dest);
				SetDParam(0, dep->town_index);
				if (HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT) && !HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
					str = STR_HEADING_FOR_TRAIN_DEPOT + _patches.vehicle_speed;
				} else {
					str = STR_HEADING_FOR_TRAIN_DEPOT_SERVICE + _patches.vehicle_speed;
				}
				SetDParam(1, v->u.rail.last_speed);
			} break;

			case OT_LOADING:
			case OT_LEAVESTATION:
				str = STR_882F_LOADING_UNLOADING;
				break;

			case OT_GOTO_WAYPOINT: {
				SetDParam(0, v->current_order.dest);
				str = STR_HEADING_FOR_WAYPOINT + _patches.vehicle_speed;
				SetDParam(1, v->u.rail.last_speed);
				break;
			}

			default:
				if (v->num_orders == 0) {
					str = STR_NO_ORDERS + _patches.vehicle_speed;
					SetDParam(0, v->u.rail.last_speed);
				} else {
					str = STR_EMPTY;
				}
				break;
			}
		}

		/* draw the flag plus orders */
		DrawSprite(v->vehstatus & VS_STOPPED ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, 2, w->widget[5].top + 1);
		DrawStringCenteredTruncated(w->widget[5].left + 8, w->widget[5].right, w->widget[5].top + 1, str, 0);
		DrawWindowViewport(w);
	}	break;

	case WE_CLICK: {
		int wid = e->we.click.widget;
		Vehicle *v = GetVehicle(w->window_number);

		switch (wid) {
		case 5: /* start/stop train */
			DoCommandP(v->tile, v->index, 0, NULL, CMD_START_STOP_TRAIN | CMD_MSG(STR_883B_CAN_T_STOP_START_TRAIN));
			break;
		case 6: /* center main view */
			ScrollMainWindowTo(v->x_pos, v->y_pos);
			break;
		case 7: /* goto depot */
			/* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
			DoCommandP(v->tile, v->index, _ctrl_pressed ? DEPOT_SERVICE : 0, NULL, CMD_SEND_TRAIN_TO_DEPOT | CMD_NO_TEST_IF_IN_NETWORK | CMD_MSG(STR_8830_CAN_T_SEND_TRAIN_TO_DEPOT));
			break;
		case 8: /* force proceed */
			DoCommandP(v->tile, v->index, 0, NULL, CMD_FORCE_TRAIN_PROCEED | CMD_MSG(STR_8862_CAN_T_MAKE_TRAIN_PASS_SIGNAL));
			break;
		case 9: /* reverse direction */
			DoCommandP(v->tile, v->index, 0, NULL, CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_8869_CAN_T_REVERSE_DIRECTION));
			break;
		case 10: /* show train orders */
			ShowOrdersWindow(v);
			break;
		case 11: /* show train details */
			ShowTrainDetailsWindow(v);
			break;
		case 12:
			ShowVehicleRefitWindow(v, INVALID_VEH_ORDER_ID);
			break;
		case 13:
			DoCommandP(v->tile, v->index, _ctrl_pressed ? 1 : 0, NULL, CMD_CLONE_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE));
			break;
		}
	} break;

	case WE_RESIZE:
		w->viewport->width          += e->we.sizing.diff.x;
		w->viewport->height         += e->we.sizing.diff.y;
		w->viewport->virtual_width  += e->we.sizing.diff.x;
		w->viewport->virtual_height += e->we.sizing.diff.y;
		break;

	case WE_DESTROY:
		DeleteWindowById(WC_VEHICLE_REFIT, w->window_number);
		DeleteWindowById(WC_VEHICLE_ORDERS, w->window_number);
		DeleteWindowById(WC_VEHICLE_DETAILS, w->window_number);
		break;

	case WE_MOUSELOOP: {
		const Vehicle *v = GetVehicle(w->window_number);
		bool train_stopped = CheckTrainStoppedInDepot(v)  >= 0;

		/* Widget 7 (send to depot) must be hidden if the train is already stopped in hangar.
		 * Widget 13 (clone) should then be shown, since cloning is allowed only while in depot and stopped.
		 * This sytem allows to have two buttons, on top of each other.
		 * The same system applies to widget 9 and 12, reverse direction and refit*/
		if (train_stopped != IsWindowWidgetHidden(w, 7) || train_stopped == IsWindowWidgetHidden(w, 13)) {
			SetWindowWidgetHiddenState(w,  7, train_stopped);  // send to depot
			SetWindowWidgetHiddenState(w,  9, train_stopped);  // reverse direction
			SetWindowWidgetHiddenState(w, 12, !train_stopped); // refit
			SetWindowWidgetHiddenState(w, 13, !train_stopped); // clone
			SetWindowDirty(w);
		}
		break;
	}

	}
}

static const WindowDesc _train_view_desc = {
	WDP_AUTO, WDP_AUTO, 250, 134,
	WC_VEHICLE_VIEW,0,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
	_train_view_widgets,
	TrainViewWndProc
};

void ShowTrainViewWindow(const Vehicle *v)
{
	Window *w = AllocateWindowDescFront(&_train_view_desc,v->index);

	if (w != NULL) {
		w->caption_color = v->owner;
		AssignWindowViewport(w, 3, 17, 0xE2, 0x66, w->window_number | (1 << 31), 0);
	}
}

static void TrainDetailsCargoTab(const Vehicle *v, int x, int y)
{
	if (v->cargo_cap != 0) {
		uint num = v->cargo_count;
		StringID str = STR_8812_EMPTY;

		if (num != 0) {
			SetDParam(0, v->cargo_type);
			SetDParam(1, num);
			SetDParam(2, v->cargo_source);
			SetDParam(3, _patches.freight_trains);
			str = FreightWagonMult(v) > 1 ? STR_FROM_MULT : STR_8813_FROM;
		}
		DrawString(x, y, str, 0);
	}
}

static void TrainDetailsInfoTab(const Vehicle *v, int x, int y)
{
	if (RailVehInfo(v->engine_type)->flags & RVI_WAGON) {
		SetDParam(0, GetCustomEngineName(v->engine_type));
		SetDParam(1, v->value);
		DrawString(x, y, STR_882D_VALUE, 0x10);
	} else {
		SetDParam(0, GetCustomEngineName(v->engine_type));
		SetDParam(1, v->build_year);
		SetDParam(2, v->value);
		DrawString(x, y, STR_882C_BUILT_VALUE, 0x10);
	}
}

static void TrainDetailsCapacityTab(const Vehicle *v, int x, int y)
{
	if (v->cargo_cap != 0) {
		SetDParam(0, v->cargo_type);
		SetDParam(1, v->cargo_cap);
		SetDParam(2, _patches.freight_trains);
		DrawString(x, y, FreightWagonMult(v) > 1 ? STR_CAPACITY_MULT : STR_013F_CAPACITY, 0);
	}
}


static void DrawTrainDetailsWindow(Window *w)
{
	byte det_tab = WP(w, traindetails_d).tab;
	const Vehicle *v;
	const Vehicle *u;
	AcceptedCargo act_cargo;
	AcceptedCargo max_cargo;
	uint i;
	int num;
	int x;
	int y;
	int sel;

	num = 0;
	u = v = GetVehicle(w->window_number);
	if (det_tab == 3) { // Total cargo tab
		for (i = 0; i < lengthof(act_cargo); i++) {
			act_cargo[i] = 0;
			max_cargo[i] = 0;
		}

		do {
			act_cargo[u->cargo_type] += u->cargo_count;
			max_cargo[u->cargo_type] += u->cargo_cap;
		} while ((u = u->next) != NULL);

		/* Set scroll-amount seperately from counting, as to not compute num double
		 * for more carriages of the same type
		 */
		for (i = 0; i != NUM_CARGO; i++) {
			if (max_cargo[i] > 0) num++; // only count carriages that the train has
		}
		num++; // needs one more because first line is description string
	} else {
		do {
			if (!IsArticulatedPart(u) || u->cargo_cap != 0) num++;
		} while ((u = u->next) != NULL);
	}

	SetVScrollCount(w, num);

	DisableWindowWidget(w, det_tab + 9);
	SetWindowWidgetDisabledState(w, 2, v->owner != _local_player);

	/* disable service-scroller when interval is set to disabled */
	SetWindowWidgetDisabledState(w, 6, !_patches.servint_trains);
	SetWindowWidgetDisabledState(w, 7, !_patches.servint_trains);

	SetDParam(0, v->string_id);
	SetDParam(1, v->unitnumber);
	DrawWindowWidgets(w);

	SetDParam(1, v->age / 366);

	x = 2;

	SetDParam(0, (v->age + 365 < v->max_age) ? STR_AGE : STR_AGE_RED);
	SetDParam(2, v->max_age / 366);
	SetDParam(3, GetTrainRunningCost(v) >> 8);
	DrawString(x, 15, STR_885D_AGE_RUNNING_COST_YR, 0);

	SetDParam(2, v->u.rail.cached_max_speed);
	SetDParam(1, v->u.rail.cached_power);
	SetDParam(0, v->u.rail.cached_weight);
	DrawString(x, 25, STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED, 0);

	SetDParam(0, v->profit_this_year);
	SetDParam(1, v->profit_last_year);
	DrawString(x, 35, STR_885F_PROFIT_THIS_YEAR_LAST_YEAR, 0);

	SetDParam(0, 100 * (v->reliability>>8) >> 8);
	SetDParam(1, v->breakdowns_since_last_service);
	DrawString(x, 45, STR_8860_RELIABILITY_BREAKDOWNS, 0);

	SetDParam(0, v->service_interval);
	SetDParam(1, v->date_of_last_service);
	DrawString(x + 11, 57 + (w->vscroll.cap * 14), _patches.servint_ispercent ? STR_SERVICING_INTERVAL_PERCENT : STR_883C_SERVICING_INTERVAL_DAYS, 0);

	y = 57;
	sel = w->vscroll.pos;

	// draw the first 3 details tabs
	if (det_tab != 3) {
		x = 1;
		for (;;) {
			if (--sel < 0 && sel >= -w->vscroll.cap) {
				int dx = 0;
				int px;
				int py;

				u = v;
				do {
					PalSpriteID pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
					DrawSprite(GetTrainImage(u, DIR_W) | pal, x + 14 + WagonLengthToPixels(dx), y + 6 + (is_custom_sprite(RailVehInfo(u->engine_type)->image_index) ? _traininfo_vehicle_pitch : 0));
					dx += u->u.rail.cached_veh_length;
					u = u->next;
				} while (u != NULL && IsArticulatedPart(u) && u->cargo_cap == 0);

				px = x + WagonLengthToPixels(dx) + 2;
				py = y + 2;
				switch (det_tab) {
					default: NOT_REACHED();
					case 0: TrainDetailsCargoTab(   v, px, py); break;
					case 1:
						// Only show name and value for the 'real' part
						if (!IsArticulatedPart(v)) {
							TrainDetailsInfoTab(v, px, py);
						}
						break;
					case 2: TrainDetailsCapacityTab(v, px, py); break;
				}
				y += 14;

				v = u;
			} else {
				// Move to the next line
				do {
					v = v->next;
				} while (v != NULL && IsArticulatedPart(v) && v->cargo_cap == 0);
			}
			if (v == NULL) return;
		}
	} else {
		// draw total cargo tab
		DrawString(x, y + 2, STR_013F_TOTAL_CAPACITY_TEXT, 0);
		for (i = 0; i != NUM_CARGO; i++) {
			if (max_cargo[i] > 0 && --sel < 0 && sel > -w->vscroll.cap) {
				y += 14;
				SetDParam(0, i);            // {CARGO} #1
				SetDParam(1, act_cargo[i]); // {CARGO} #2
				SetDParam(2, i);            // {SHORTCARGO} #1
				SetDParam(3, max_cargo[i]); // {SHORTCARGO} #2
				SetDParam(4, _patches.freight_trains);
				DrawString(x, y + 2, FreightWagonMult(v) > 1 ? STR_TOTAL_CAPACITY_MULT : STR_013F_TOTAL_CAPACITY, 0);
			}
		}
	}
}

static void TrainDetailsWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
	case WE_PAINT:
		DrawTrainDetailsWindow(w);
		break;
	case WE_CLICK: {
		int mod;
		const Vehicle *v;
		switch (e->we.click.widget) {
		case 2: /* name train */
			v = GetVehicle(w->window_number);
			SetDParam(0, v->unitnumber);
			ShowQueryString(v->string_id, STR_8865_NAME_TRAIN, 31, 150, w->window_class, w->window_number, CS_ALPHANUMERAL);
			break;
		case 6: /* inc serv interval */
			mod = _ctrl_pressed? 5 : 10;
			goto do_change_service_int;

		case 7: /* dec serv interval */
			mod = _ctrl_pressed? -5 : -10;
do_change_service_int:
			v = GetVehicle(w->window_number);

			mod = GetServiceIntervalClamped(mod + v->service_interval);
			if (mod == v->service_interval) return;

			DoCommandP(v->tile, v->index, mod, NULL, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
			break;
		/* details buttons*/
		case 9:  // Cargo
		case 10: // Information
		case 11: // Capacities
		case 12: // Total cargo
			EnableWindowWidget(w,  9);
			EnableWindowWidget(w, 10);
			EnableWindowWidget(w, 11);
			EnableWindowWidget(w, 12);
			EnableWindowWidget(w, e->we.click.widget);
			WP(w,traindetails_d).tab = e->we.click.widget - 9;
			SetWindowDirty(w);
			break;
		}
	} break;

	case WE_ON_EDIT_TEXT:
		if (e->we.edittext.str[0] != '\0') {
			_cmd_text = e->we.edittext.str;
			DoCommandP(0, w->window_number, 0, NULL,
				CMD_NAME_VEHICLE | CMD_MSG(STR_8866_CAN_T_NAME_TRAIN));
		}
		break;

	case WE_RESIZE:
		if (e->we.sizing.diff.y == 0) break;

		w->vscroll.cap += e->we.sizing.diff.y / 14;
		w->widget[4].data = (w->vscroll.cap << 8) + 1;
		break;
	}
}

static const Widget _train_details_widgets[] = {
{   WWT_CLOSEBOX, RESIZE_NONE,   14,   0,  10,   0,  13, STR_00C5,             STR_018B_CLOSE_WINDOW},
{    WWT_CAPTION, RESIZE_NONE,   14,  11, 329,   0,  13, STR_8802_DETAILS,     STR_018C_WINDOW_TITLE_DRAG_THIS},
{ WWT_PUSHTXTBTN, RESIZE_NONE,   14, 330, 369,   0,  13, STR_01AA_NAME,        STR_8867_NAME_TRAIN},
{      WWT_PANEL, RESIZE_NONE,   14,   0, 369,  14,  55, 0x0,                  STR_NULL},
{     WWT_MATRIX, RESIZE_BOTTOM, 14,   0, 357,  56, 139, 0x601,                STR_NULL},
{  WWT_SCROLLBAR, RESIZE_BOTTOM, 14, 358, 369,  56, 139, 0x0,                  STR_0190_SCROLL_BAR_SCROLLS_LIST},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14,   0,  10, 140, 145, STR_0188,             STR_884D_INCREASE_SERVICING_INTERVAL},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14,   0,  10, 146, 151, STR_0189,             STR_884E_DECREASE_SERVICING_INTERVAL},
{      WWT_PANEL, RESIZE_TB,     14,  11, 369, 140, 151, 0x0,                  STR_NULL},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14,   0,  89, 152, 163, STR_013C_CARGO,       STR_884F_SHOW_DETAILS_OF_CARGO_CARRIED},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14,  90, 178, 152, 163, STR_013D_INFORMATION, STR_8850_SHOW_DETAILS_OF_TRAIN_VEHICLES},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14, 179, 268, 152, 163, STR_013E_CAPACITIES,  STR_8851_SHOW_CAPACITIES_OF_EACH},
{ WWT_PUSHTXTBTN, RESIZE_TB,     14, 269, 357, 152, 163, STR_013E_TOTAL_CARGO, STR_8852_SHOW_TOTAL_CARGO},
{  WWT_RESIZEBOX, RESIZE_TB,     14, 358, 369, 152, 163, 0x0,                  STR_RESIZE_BUTTON},
{   WIDGETS_END},
};


static const WindowDesc _train_details_desc = {
	WDP_AUTO, WDP_AUTO, 370, 164,
	WC_VEHICLE_DETAILS,WC_VEHICLE_VIEW,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
	_train_details_widgets,
	TrainDetailsWndProc
};


static void ShowTrainDetailsWindow(const Vehicle *v)
{
	Window *w;
	VehicleID veh = v->index;

	DeleteWindowById(WC_VEHICLE_ORDERS, veh);
	DeleteWindowById(WC_VEHICLE_DETAILS, veh);

	w = AllocateWindowDescFront(&_train_details_desc, veh);

	w->caption_color = v->owner;
	w->vscroll.cap = 6;
	w->widget[4].data = (w->vscroll.cap << 8) + 1;

	w->resize.step_height = 14;
	w->resize.height = w->height - 14 * 2; /* Minimum of 4 wagons in the display */

	WP(w,traindetails_d).tab = 0;
}