Files @ r7361:4526784d7450
Branch filter:

Location: cpp/openttd-patchpack/source/src/industry_gui.cpp

peter1138
(svn r10724) -Fix (r10723): Toolbar spacing was off-by-one
  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
/* $Id$ */

/** @file industry_gui.cpp */

#include "stdafx.h"
#include "openttd.h"
#include "debug.h"
#include "functions.h"
#include "strings.h"
#include "table/strings.h"
#include "table/sprites.h"
#include "map.h"
#include "gui.h"
#include "window.h"
#include "gfx.h"
#include "command.h"
#include "viewport.h"
#include "industry.h"
#include "town.h"
#include "variables.h"
#include "helpers.hpp"
#include "cargotype.h"
#include "newgrf.h"
#include "newgrf_callbacks.h"
#include "newgrf_industries.h"
#include "newgrf_text.h"
#include "date.h"

extern Industry *CreateNewIndustry(TileIndex tile, IndustryType type);

/**
 * Search callback function for TryBuildIndustry
 * @param tile to test
 * @param data that is passed by the caller.  In this case, the type of industry been tested
 * @return the success (or not) of the operation
 */
static bool SearchTileForIndustry(TileIndex tile, uint32 data)
{
	return CreateNewIndustry(tile, data) != NULL;
}

/**
 * Perform a 9*9 tiles circular search around a tile
 * in order to find a suitable zone to create the desired industry
 * @param tile to start search for
 * @param type of the desired industry
 * @return the success (or not) of the operation
 */
static bool TryBuildIndustry(TileIndex tile, int type)
{
	return CircularTileSearch(tile, 9, SearchTileForIndustry, type);
}
bool _ignore_restrictions;

enum {
	DYNA_INDU_MATRIX_WIDGET = 2,
	DYNA_INDU_INFOPANEL = 4,
	DYNA_INDU_FUND_WIDGET,
	DYNA_INDU_RESIZE_WIDGET,
};

/** Attached struct to the window extended data */
struct fnd_d {
	int index;             ///< index of the element in the matrix
	IndustryType select;   ///< industry corresponding to the above index
	uint16 callback_timer; ///< timer counter for callback eventual verification
	bool timer_enabled;    ///< timer can be used
};
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(fnd_d));

/** Helper struct holding the available industries for current situation */
static struct IndustryData {
	uint16 count;                               ///< How many industries are loaded
	IndustryType index[NUM_INDUSTRYTYPES + 1];  ///< Type of industry, in the order it was loaded
	StringID text[NUM_INDUSTRYTYPES + 1];       ///< Text coming from CBM_IND_FUND_MORE_TEXT (if ever)
	bool enabled[NUM_INDUSTRYTYPES + 1];        ///< availability state, coming from CBID_INDUSTRY_AVAILABLE (if ever)
} _fund_gui;

static void BuildDynamicIndustryWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
		case WE_CREATE: {
			IndustryType ind;
			const IndustrySpec *indsp;

			/* Shorten the window to the equivalant of the additionnal purchase
			 * info coming from the callback.  SO it will only be available to tis full
			 * height when newindistries are loaded */
			if (!_loaded_newgrf_features.has_newindustries) {
				w->widget[DYNA_INDU_INFOPANEL].bottom -= 44;
				w->widget[DYNA_INDU_FUND_WIDGET].bottom -= 44;
				w->widget[DYNA_INDU_FUND_WIDGET].top -= 44;
				w->widget[DYNA_INDU_RESIZE_WIDGET].bottom -= 44;
				w->widget[DYNA_INDU_RESIZE_WIDGET].top -= 44;
				w->resize.height = w->height -= 44;
			}

			WP(w, fnd_d).timer_enabled = _loaded_newgrf_features.has_newindustries;

			/* Initilialize structures */
			memset(&_fund_gui.index, 0xFF, NUM_INDUSTRYTYPES);
			memset(&_fund_gui.text, STR_NULL, NUM_INDUSTRYTYPES);
			memset(&_fund_gui.enabled, false, NUM_INDUSTRYTYPES);
			_fund_gui.count = 0;

			w->vscroll.cap = 8; // rows in grid, same in scroller
			w->resize.step_height = 13;

			if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
				_fund_gui.index[_fund_gui.count] = INVALID_INDUSTRYTYPE;
				_fund_gui.count++;
				WP(w, fnd_d).timer_enabled = false;
			}

			/* We'll perform two distinct loops, one for secondary industries, and the other one for
			 * primary ones. Each loop will fill the _fund_gui structure. */
			for (ind = 0; ind < NUM_INDUSTRYTYPES; ind++) {
				indsp = GetIndustrySpec(ind);
				if (indsp->enabled && (!indsp->IsRawIndustry() || _game_mode == GM_EDITOR)) {
					_fund_gui.index[_fund_gui.count] = ind;
					_fund_gui.enabled[_fund_gui.count] = CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
					_fund_gui.count++;
				}
			}

			if (_patches.raw_industry_construction != 0 && _game_mode != GM_EDITOR) {
				for (ind = 0; ind < NUM_INDUSTRYTYPES; ind++) {
					indsp = GetIndustrySpec(ind);
					if (indsp->enabled && indsp->IsRawIndustry()) {
						_fund_gui.index[_fund_gui.count] = ind;
						_fund_gui.enabled[_fund_gui.count] = CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
						_fund_gui.count++;
					}
				}
			}

			/* first indutry type is selected.
			 * I'll be damned if there are none available ;) */
			WP(w, fnd_d).index = 0;
			WP(w, fnd_d).select = _fund_gui.index[0];
			WP(w, fnd_d).callback_timer = DAY_TICKS;
		} break;

		case WE_PAINT: {
			const IndustrySpec *indsp = (WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(WP(w, fnd_d).select);
			StringID str = STR_4827_REQUIRES;
			int x_str = w->widget[DYNA_INDU_INFOPANEL].left + 3;
			int y_str = w->widget[DYNA_INDU_INFOPANEL].top + 3;
			const Widget *wi = &w->widget[DYNA_INDU_INFOPANEL];
			int max_width = wi->right - wi->left - 4;

			/* Raw industries might be prospected. Show this fact by changing the string
			 * In Editor, you just build, while ingame, or you fund or you prospect */
			if (_game_mode == GM_EDITOR) {
				/* We've chosen many random industries but no industries have been specified */
				if (indsp == NULL) _fund_gui.enabled[WP(w, fnd_d).index] = _opt.diff.number_industries != 0;
				w->widget[DYNA_INDU_FUND_WIDGET].data = STR_BUILD_NEW_INDUSTRY;
			} else {
				w->widget[DYNA_INDU_FUND_WIDGET].data = (_patches.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_PROSPECT_NEW_INDUSTRY : STR_FUND_NEW_INDUSTRY;
			}
			SetWindowWidgetDisabledState(w, DYNA_INDU_FUND_WIDGET, !_fund_gui.enabled[WP(w, fnd_d).index]);

			SetVScrollCount(w, _fund_gui.count);

			DrawWindowWidgets(w);

			/* and now with the matrix painting */
			for (byte i = 0; i < w->vscroll.cap && ((i + w->vscroll.pos) < _fund_gui.count); i++) {
				int offset = i * 13;
				int x = 3;
				int y = 16;
				bool selected = WP(w, fnd_d).index == i + w->vscroll.pos;

				if (_fund_gui.index[i + w->vscroll.pos] == INVALID_INDUSTRYTYPE) {
					DrawString(21, y + offset, STR_MANY_RANDOM_INDUSTRIES, selected ? 12 : 6);
					continue;
				}
				const IndustrySpec *indsp = GetIndustrySpec(_fund_gui.index[i + w->vscroll.pos]);

				/* Draw the name of the industry in white is selected, otherwise, in orange */
				DrawString(20,     y + offset, indsp->name, selected ? 12 : 6);
				GfxFillRect(x,     y + 1 + offset,  x + 10, y + 7 + offset, selected ? 15 : 0);
				GfxFillRect(x + 1, y + 2 + offset,  x +  9, y + 6 + offset, indsp->map_colour);
			}

			if (WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) {
				DrawStringMultiLine(x_str, y_str, STR_RANDOM_INDUSTRIES_TIP, max_width, wi->bottom - wi->top - 40);
				break;
			}

			if (_game_mode != GM_EDITOR) {
				SetDParam(0, indsp->GetConstructionCost());
				DrawStringTruncated(x_str, y_str, STR_482F_COST, 0, max_width);
				y_str += 11;
			}

			/* Draw the accepted cargos, if any. Otherwhise, will print "Nothing" */
			if (indsp->accepts_cargo[0] != CT_INVALID) {
				SetDParam(0, GetCargo(indsp->accepts_cargo[0])->name);
				if (indsp->accepts_cargo[1] != CT_INVALID) {
					SetDParam(1, GetCargo(indsp->accepts_cargo[1])->name);
					str = STR_4828_REQUIRES;
					if (indsp->accepts_cargo[2] != CT_INVALID) {
						SetDParam(2, GetCargo(indsp->accepts_cargo[2])->name);
						str = STR_4829_REQUIRES;
					}
				}
			} else {
				SetDParam(0, STR_00D0_NOTHING);
			}
			DrawStringTruncated(x_str, y_str, str, 0, max_width);

			y_str += 11;
			/* Draw the produced cargos, if any. Otherwhise, will print "Nothing" */
			str = STR_4827_PRODUCES;
			if (indsp->produced_cargo[0] != CT_INVALID) {
				SetDParam(0, GetCargo(indsp->produced_cargo[0])->name);
				if (indsp->produced_cargo[1] != CT_INVALID) {
					SetDParam(1, GetCargo(indsp->produced_cargo[1])->name);
					str = STR_4828_PRODUCES;
				}
			} else {
				SetDParam(0, STR_00D0_NOTHING);
			}
			DrawStringTruncated(x_str, y_str, str, 0, max_width);

			/* Get the additional purchase info text, if it has not already been */
			if (_fund_gui.text[WP(w, fnd_d).index] == STR_NULL) {   // Have i been called already?
				if (HASBIT(indsp->callback_flags, CBM_IND_FUND_MORE_TEXT)) {          // No. Can it be called?
					uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_FUND_MORE_TEXT, 0, 0, NULL, WP(w, fnd_d).select, INVALID_TILE);
					if (callback_res != CALLBACK_FAILED) {  // Did it failed?
						StringID newtxt = GetGRFStringID(indsp->grf_prop.grffile->grfid, 0xD000 + callback_res);  // No. here's the new string
						_fund_gui.text[WP(w, fnd_d).index] = newtxt;   // Store it for further usage
					}
				}
			}

			y_str += 11;
			/* Draw the Additional purchase text, provided by newgrf callback, if any.
			 * Otherwhise, will print Nothing */
			str = _fund_gui.text[WP(w, fnd_d).index];
			if (str != STR_NULL && str != STR_UNDEFINED) {
				SetDParam(0, str);
				DrawStringMultiLine(x_str, y_str, STR_JUST_STRING, max_width, wi->bottom - wi->top - 40);
			}
		} break;

		case WE_CLICK:
			switch (e->we.click.widget) {
				case DYNA_INDU_MATRIX_WIDGET: {
					const IndustrySpec *indsp;
					int y = (e->we.click.pt.y - w->widget[DYNA_INDU_MATRIX_WIDGET].top) / 13 + w->vscroll.pos ;

					if (y >= 0 && y < _fund_gui.count) { // Is it within the boundaries of available data?
						WP(w, fnd_d).index = y;
						WP(w, fnd_d).select = _fund_gui.index[WP(w, fnd_d).index];
						indsp = (WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(WP(w, fnd_d).select);

						SetWindowDirty(w);

						if ((_game_mode != GM_EDITOR && _patches.raw_industry_construction == 2 && indsp != NULL && indsp->IsRawIndustry()) ||
								WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) {
							/* Reset the button state if going to prospecting or "build many industries" */
							RaiseWindowButtons(w);
							ResetObjectToPlace();
						}
					}
				} break;

				case DYNA_INDU_FUND_WIDGET: {
					if (WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) {
						HandleButtonClick(w, DYNA_INDU_FUND_WIDGET);

						if (GetNumTowns() == 0) {
							ShowErrorMessage(STR_0286_MUST_BUILD_TOWN_FIRST, STR_CAN_T_GENERATE_INDUSTRIES, 0, 0);
						} else {
							extern void GenerateIndustries();
							_generating_world = true;
							GenerateIndustries();
							_generating_world = false;
						}
					} else if (_game_mode != GM_EDITOR && _patches.raw_industry_construction == 2 && GetIndustrySpec(WP(w, fnd_d).select)->IsRawIndustry()) {
						DoCommandP(0, WP(w, fnd_d).select, 0, NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));
						HandleButtonClick(w, DYNA_INDU_FUND_WIDGET);
					} else {
						HandlePlacePushButton(w, DYNA_INDU_FUND_WIDGET, SPR_CURSOR_INDUSTRY, 1, NULL);
					}
				} break;
			}
			break;

		case WE_RESIZE: {
			/* Adjust the number of items in the matrix depending of the rezise */
			w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
			w->widget[DYNA_INDU_MATRIX_WIDGET].data = (w->vscroll.cap << 8) + 1;
		} break;

		case WE_PLACE_OBJ: {
			/* We do not need to protect ourselves against "Random Many Industries" in this mode */
			const IndustrySpec *indsp = GetIndustrySpec(WP(w, fnd_d).select);

			if (_game_mode == GM_EDITOR) {
				/* Show error if no town exists at all */
				if (GetNumTowns() == 0) {
					SetDParam(0, indsp->name);
					ShowErrorMessage(STR_0286_MUST_BUILD_TOWN_FIRST, STR_0285_CAN_T_BUILD_HERE, e->we.place.pt.x, e->we.place.pt.y);
					return;
				}

				_current_player = OWNER_NONE;
				_generating_world = true;
				_ignore_restrictions = true;
				if (!TryBuildIndustry(e->we.place.tile, WP(w, fnd_d).select)) {
					SetDParam(0, indsp->name);
					ShowErrorMessage(_error_message, STR_0285_CAN_T_BUILD_HERE, e->we.place.pt.x, e->we.place.pt.y);
				}

				_ignore_restrictions = false;
				_generating_world = false;
			} else DoCommandP(e->we.place.tile, WP(w, fnd_d).select, 0, NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));

			/* Whatever the outcome of the actions, just reset the cursor and the system */
			ResetObjectToPlace();
		} break;

		case WE_TICK:
			if (!WP(w, fnd_d).timer_enabled) break;
			if (--WP(w, fnd_d).callback_timer == 0) {
				/* We have just passed another day.
				 * See if we need to update availability of currently selected industry */
				WP(w, fnd_d).callback_timer = DAY_TICKS;  //restart counter

				const IndustrySpec *indsp = GetIndustrySpec(WP(w, fnd_d).select);

				if (indsp->enabled) {
					bool call_back_result = CheckIfCallBackAllowsAvailability(WP(w, fnd_d).select, IACT_USERCREATION);

					/* Only if result does match the previous state would it require a redraw. */
					if (call_back_result != _fund_gui.enabled[WP(w, fnd_d).index]) {
						_fund_gui.enabled[WP(w, fnd_d).index] = call_back_result;
						SetWindowDirty(w);
					}
				}
			}
			break;

		case WE_TIMEOUT:
		case WE_ABORT_PLACE_OBJ:
			RaiseWindowButtons(w);
			break;
	}
}

static const Widget _build_dynamic_industry_widgets[] = {
{   WWT_CLOSEBOX,    RESIZE_NONE,    7,     0,    10,     0,    13, STR_00C5,                       STR_018B_CLOSE_WINDOW},
{    WWT_CAPTION,   RESIZE_RIGHT,    7,    11,   169,     0,    13, STR_0314_FUND_NEW_INDUSTRY,     STR_018C_WINDOW_TITLE_DRAG_THIS},
{     WWT_MATRIX,      RESIZE_RB,    7,     0,   157,    14,   118, 0x801,                          STR_INDUSTRY_SELECTION_HINT},
{  WWT_SCROLLBAR,     RESIZE_LRB,    7,   158,   169,    14,   118, 0x0,                            STR_0190_SCROLL_BAR_SCROLLS_LIST},
{      WWT_PANEL,     RESIZE_RTB,    7,     0,   169,   119,   199, 0x0,                            STR_NULL},
{    WWT_TEXTBTN,     RESIZE_RTB,    7,     0,   157,   200,   211, STR_FUND_NEW_INDUSTRY,          STR_NULL},
{  WWT_RESIZEBOX,    RESIZE_LRTB,    7,   158,   169,   200,   211, 0x0,                            STR_RESIZE_BUTTON},
{   WIDGETS_END},
};

static const WindowDesc _build_industry_dynamic_desc = {
	WDP_AUTO, WDP_AUTO, 170, 212, 170, 212,
	WC_BUILD_INDUSTRY, WC_NONE,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
	_build_dynamic_industry_widgets,
	BuildDynamicIndustryWndProc,
};

void ShowBuildIndustryWindow()
{
	if (_game_mode != GM_EDITOR && !IsValidPlayer(_current_player)) return;
	AllocateWindowDescFront(&_build_industry_dynamic_desc, 0);
}

static void UpdateIndustryProduction(Industry *i);

static inline bool isProductionMinimum(const Industry *i, int pt)
{
	return i->production_rate[pt] == 1;
}

static inline bool isProductionMaximum(const Industry *i, int pt)
{
	return i->production_rate[pt] == 255;
}

static inline bool IsProductionAlterable(const Industry *i)
{
	const IndustrySpec *ind = GetIndustrySpec(i->type);
	return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
			(ind->accepts_cargo[0] == CT_INVALID || ind->accepts_cargo[0] == CT_VALUABLES));
}

/** Information to store about the industry window */
struct indview_d : public vp_d {
	byte editbox_line;        ///< The line clicked to open the edit box
	byte clicked_line;        ///< The line of the button that has been clicked
	byte clicked_button;      ///< The button that has been clicked (to raise)
	byte production_offset_y; ///< The offset of the production texts/buttons
};
assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(indview_d));


static void IndustryViewWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
	case WE_CREATE: {
		/* Count the number of lines that we need to resize the GUI with */
		const IndustrySpec *ind = GetIndustrySpec(GetIndustry(w->window_number)->type);
		int lines = -3;

		if (HASBIT(ind->callback_flags, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(ind->callback_flags, CBM_IND_PRODUCTION_256_TICKS)) {
			for (uint j = 0; j < 3 && ind->accepts_cargo[j] != CT_INVALID; j++) {
				if (j == 0) lines++;
				lines++;
			}
		} else if (ind->accepts_cargo[0] != CT_INVALID) {
			lines++;
		}

		for (uint j = 0; j < 2 && ind->produced_cargo[j] != CT_INVALID; j++) {
			if (j == 0) {
				if (ind->accepts_cargo[0] != CT_INVALID) lines++;
				lines++;
			}
			lines++;
		}

		if (HASBIT(ind->callback_flags, CBM_IND_WINDOW_MORE_TEXT)) lines += 2;

		for (uint j = 5; j <= 7; j++) {
			if (j != 5) w->widget[j].top += lines * 10;
			w->widget[j].bottom += lines * 10;
		}
		w->height += lines * 10;
	} break;

	case WE_PAINT: {
		Industry *i = GetIndustry(w->window_number);
		const IndustrySpec *ind = GetIndustrySpec(i->type);
		int y = 111;

		SetDParam(0, w->window_number);
		DrawWindowWidgets(w);

		if (HASBIT(ind->callback_flags, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(ind->callback_flags, CBM_IND_PRODUCTION_256_TICKS)) {
			for (uint j = 0; j < 3 && ind->accepts_cargo[j] != CT_INVALID; j++) {
				if (j == 0) {
					DrawString(2, y, STR_INDUSTRY_WINDOW_WAITING_FOR_PROCESSING, 0);
					y += 10;
				}
				SetDParam(0, ind->accepts_cargo[j]);
				SetDParam(1, i->incoming_cargo_waiting[j]);
				DrawString(4, y, STR_INDUSTRY_WINDOW_WAITING_STOCKPILE_CARGO, 0);
				y += 10;
			}
		} else if (ind->accepts_cargo[0] != CT_INVALID) {
			StringID str;

			SetDParam(0, GetCargo(ind->accepts_cargo[0])->name);
			str = STR_4827_REQUIRES;
			if (ind->accepts_cargo[1] != CT_INVALID) {
				SetDParam(1, GetCargo(ind->accepts_cargo[1])->name);
				str = STR_4828_REQUIRES;
				if (ind->accepts_cargo[2] != CT_INVALID) {
					SetDParam(2, GetCargo(ind->accepts_cargo[2])->name);
					str = STR_4829_REQUIRES;
				}
			}
			DrawString(2, y, str, 0);
			y += 10;
		}

		for (uint j = 0; j < 2 && ind->produced_cargo[j] != CT_INVALID; j++) {
			if (j == 0) {
				if (ind->accepts_cargo[0] != CT_INVALID) y += 10;
				DrawString(2, y, STR_482A_PRODUCTION_LAST_MONTH, 0);
				y += 10;
				WP(w, indview_d).production_offset_y = y;
			}

			SetDParam(0, ind->produced_cargo[j]);
			SetDParam(1, i->last_month_production[j]);

			SetDParam(2, i->last_month_pct_transported[j] * 100 >> 8);
			DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), y, STR_482B_TRANSPORTED, 0);
			/* Let's put out those buttons.. */
			if (IsProductionAlterable(i)) {
				DrawArrowButtons(5, y, 3, (WP(w, indview_d).clicked_line == j + 1) ? WP(w, indview_d).clicked_button : 0,
						!isProductionMinimum(i, j), !isProductionMaximum(i, j));
			}
			y += 10;
		}

		/* Get the extra message for the GUI */
		if (HASBIT(ind->callback_flags, CBM_IND_WINDOW_MORE_TEXT)) {
			uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->xy);
			if (callback_res != CALLBACK_FAILED) {
				StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
				if (message != STR_NULL && message != STR_UNDEFINED) {
					y += 10;
					DrawString(2, y, message, 0);
				}
			}
		}

		DrawWindowViewport(w);
	} break;

	case WE_CLICK: {
		Industry *i;

		switch (e->we.click.widget) {
		case 5: {
			int line, x;

			i = GetIndustry(w->window_number);

			/* We should work if needed.. */
			if (!IsProductionAlterable(i)) return;
			x = e->we.click.pt.x;
			line = (e->we.click.pt.y - WP(w, indview_d).production_offset_y) / 10;
			if (e->we.click.pt.y >= WP(w, indview_d).production_offset_y && IS_INT_INSIDE(line, 0, 2) &&
					GetIndustrySpec(i->type)->produced_cargo[line] != CT_INVALID) {
				if (IS_INT_INSIDE(x, 5, 25) ) {
					/* Clicked buttons, decrease or increase production */
					if (x < 15) {
						if (isProductionMinimum(i, line)) return;
						i->production_rate[line] = max(i->production_rate[line] / 2, 1);
					} else {
						if (isProductionMaximum(i, line)) return;
						i->production_rate[line] = minu(i->production_rate[line] * 2, 255);
					}

					UpdateIndustryProduction(i);
					SetWindowDirty(w);
					w->flags4 |= 5 << WF_TIMEOUT_SHL;
					WP(w, indview_d).clicked_line = line + 1;
					WP(w, indview_d).clicked_button = (x < 15 ? 1 : 2);
				} else if (IS_INT_INSIDE(x, 34, 160)) {
					/* clicked the text */
					WP(w, indview_d).editbox_line = line;
					SetDParam(0, i->production_rate[line] * 8);
					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_GAME_PRODUCTION, 10, 100, w, CS_ALPHANUMERAL);
				}
			}
		} break;
		case 6:
			i = GetIndustry(w->window_number);
			ScrollMainWindowToTile(i->xy + TileDiffXY(1, 1));
		} break;

		}
		break;
	case WE_TIMEOUT:
		WP(w, indview_d).clicked_line = 0;
		WP(w, indview_d).clicked_button = 0;
		SetWindowDirty(w);
		break;

	case WE_ON_EDIT_TEXT:
		if (e->we.edittext.str[0] != '\0') {
			Industry* i = GetIndustry(w->window_number);
			int line = WP(w, indview_d).editbox_line;

			i->production_rate[line] = clampu(atoi(e->we.edittext.str), 0, 255);
			UpdateIndustryProduction(i);
			SetWindowDirty(w);
		}
	}
}

static void UpdateIndustryProduction(Industry *i)
{
	const IndustrySpec *ind = GetIndustrySpec(i->type);

	for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
		if (ind->produced_cargo[j] != CT_INVALID) {
			i->last_month_production[j] = 8 * i->production_rate[j];
		}
	}
}

static const Widget _industry_view_widgets[] = {
{   WWT_CLOSEBOX,   RESIZE_NONE,     9,     0,    10,     0,    13, STR_00C5,          STR_018B_CLOSE_WINDOW},
{    WWT_CAPTION,   RESIZE_NONE,     9,    11,   247,     0,    13, STR_4801,          STR_018C_WINDOW_TITLE_DRAG_THIS},
{  WWT_STICKYBOX,   RESIZE_NONE,     9,   248,   259,     0,    13, 0x0,               STR_STICKY_BUTTON},
{      WWT_PANEL,   RESIZE_NONE,     9,     0,   259,    14,   105, 0x0,               STR_NULL},
{      WWT_INSET,   RESIZE_NONE,     9,     2,   257,    16,   103, 0x0,               STR_NULL},
{      WWT_PANEL,   RESIZE_NONE,     9,     0,   259,   106,   147, 0x0,               STR_NULL},
{ WWT_PUSHTXTBTN,   RESIZE_NONE,     9,     0,   129,   148,   159, STR_00E4_LOCATION, STR_482C_CENTER_THE_MAIN_VIEW_ON},
{      WWT_PANEL,   RESIZE_NONE,     9,   130,   259,   148,   159, 0x0,               STR_NULL},
{   WIDGETS_END},
};

static const WindowDesc _industry_view_desc = {
	WDP_AUTO, WDP_AUTO, 260, 160, 260, 160,
	WC_INDUSTRY_VIEW, WC_NONE,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
	_industry_view_widgets,
	IndustryViewWndProc
};

void ShowIndustryViewWindow(int industry)
{
	Window *w = AllocateWindowDescFront(&_industry_view_desc, industry);

	if (w != NULL) {
		w->flags4 |= WF_DISABLE_VP_SCROLL;
		WP(w, indview_d).editbox_line = 0;
		WP(w, indview_d).clicked_line = 0;
		WP(w, indview_d).clicked_button = 0;
		AssignWindowViewport(w, 3, 17, 0xFE, 0x56, GetIndustry(w->window_number)->xy + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
	}
}

enum {
	DIRECTORY_INDU_SORTBYNAME = 3,
	DIRECTORY_INDU_SORTBYTYPE,
	DIRECTORY_INDU_SORTBYPROD,
	DIRECTORY_INDU_SORTBYTRANSPORT,
	DIRECTORY_INDU_SHOWINDU = 8,
};

static const Widget _industry_directory_widgets[] = {
{   WWT_CLOSEBOX,   RESIZE_NONE,    13,     0,    10,     0,    13, STR_00C5,                STR_018B_CLOSE_WINDOW},
{    WWT_CAPTION,   RESIZE_NONE,    13,    11,   495,     0,    13, STR_INDUSTRYDIR_CAPTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
{  WWT_STICKYBOX,   RESIZE_NONE,    13,   496,   507,     0,    13, 0x0,                     STR_STICKY_BUTTON},
{ WWT_PUSHTXTBTN,   RESIZE_NONE,    13,     0,   100,    14,    25, STR_SORT_BY_NAME,        STR_SORT_ORDER_TIP},
{ WWT_PUSHTXTBTN,   RESIZE_NONE,    13,   101,   200,    14,    25, STR_SORT_BY_TYPE,        STR_SORT_ORDER_TIP},
{ WWT_PUSHTXTBTN,   RESIZE_NONE,    13,   201,   300,    14,    25, STR_SORT_BY_PRODUCTION,  STR_SORT_ORDER_TIP},
{ WWT_PUSHTXTBTN,   RESIZE_NONE,    13,   301,   400,    14,    25, STR_SORT_BY_TRANSPORTED, STR_SORT_ORDER_TIP},
{      WWT_PANEL,   RESIZE_NONE,    13,   401,   495,    14,    25, 0x0,                     STR_NULL},
{      WWT_PANEL, RESIZE_BOTTOM,    13,     0,   495,    26,   189, 0x0,                     STR_200A_TOWN_NAMES_CLICK_ON_NAME},
{  WWT_SCROLLBAR, RESIZE_BOTTOM,    13,   496,   507,    14,   177, 0x0,                     STR_0190_SCROLL_BAR_SCROLLS_LIST},
{  WWT_RESIZEBOX,     RESIZE_TB,    13,   496,   507,   178,   189, 0x0,                     STR_RESIZE_BUTTON},
{   WIDGETS_END},
};

static uint _num_industry_sort;

static char _bufcache[96];
static const Industry* _last_industry;

static byte _industry_sort_order;

static int CDECL GeneralIndustrySorter(const void *a, const void *b)
{
	const Industry* i = *(const Industry**)a;
	const Industry* j = *(const Industry**)b;
	const IndustrySpec *ind_i = GetIndustrySpec(i->type);
	const IndustrySpec *ind_j = GetIndustrySpec(j->type);
	int r;

	switch (_industry_sort_order >> 1) {
		default: NOT_REACHED();
		case 0: /* Sort by Name (handled later) */
			r = 0;
			break;

		case 1: /* Sort by Type */
			r = i->type - j->type;
			break;

		case 2: /* Sort by Production */
			if (ind_i->produced_cargo[0] == CT_INVALID) {
				r = (ind_j->produced_cargo[0] == CT_INVALID ? 0 : -1);
			} else {
				if (ind_j->produced_cargo[0] == CT_INVALID) {
					r = 1;
				} else {
					r =
						(i->last_month_production[0] + i->last_month_production[1]) -
						(j->last_month_production[0] + j->last_month_production[1]);
				}
			}
			break;

		case 3: /* Sort by transported fraction */
			if (ind_i->produced_cargo[0] == CT_INVALID) {
				r = (ind_j->produced_cargo[0] == CT_INVALID ? 0 : -1);
			} else {
				if (ind_j->produced_cargo[0] == CT_INVALID) {
					r = 1;
				} else {
					int pi;
					int pj;

					pi = i->last_month_pct_transported[0] * 100 >> 8;
					if (ind_i->produced_cargo[1] != CT_INVALID) {
						int p = i->last_month_pct_transported[1] * 100 >> 8;
						if (p < pi) pi = p;
					}

					pj = j->last_month_pct_transported[0] * 100 >> 8;
					if (ind_j->produced_cargo[1] != CT_INVALID) {
						int p = j->last_month_pct_transported[1] * 100 >> 8;
						if (p < pj) pj = p;
					}

					r = pi - pj;
				}
			}
			break;
	}

	/* default to string sorting if they are otherwise equal */
	if (r == 0) {
		char buf1[96];

		SetDParam(0, i->town->index);
		GetString(buf1, STR_TOWN, lastof(buf1));

		if (j != _last_industry) {
			_last_industry = j;
			SetDParam(0, j->town->index);
			GetString(_bufcache, STR_TOWN, lastof(_bufcache));
		}
		r = strcmp(buf1, _bufcache);
	}

	if (_industry_sort_order & 1) r = -r;
	return r;
}

/**
 * Makes a sorted industry list.
 * When there are no industries, the list has to be made. This so when one
 * starts a new game without industries after playing a game with industries
 * the list is not populated with invalid industries from the previous game.
 */
static void MakeSortedIndustryList()
{
	const Industry* i;
	int n = 0;

	/* Create array for sorting */
	_industry_sort = ReallocT(_industry_sort, GetMaxIndustryIndex() + 1);
	if (_industry_sort == NULL) error("Could not allocate memory for the industry-sorting-list");

	/* Don't attempt a sort if there are no industries */
	if (GetNumIndustries() != 0) {
		FOR_ALL_INDUSTRIES(i) _industry_sort[n++] = i;
		qsort((void*)_industry_sort, n, sizeof(_industry_sort[0]), GeneralIndustrySorter);
	}

	_num_industry_sort = n;
	_last_industry = NULL; // used for "cache"

	DEBUG(misc, 3, "Resorting industries list");
}


static void IndustryDirectoryWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
	case WE_PAINT: {
		int n;
		uint p;
		static const uint16 _indicator_positions[4] = {88, 187, 284, 387};

		if (_industry_sort_dirty) {
			_industry_sort_dirty = false;
			MakeSortedIndustryList();
		}

		SetVScrollCount(w, _num_industry_sort);

		DrawWindowWidgets(w);
		DoDrawString(_industry_sort_order & 1 ? DOWNARROW : UPARROW, _indicator_positions[_industry_sort_order >> 1], 15, 0x10);

		p = w->vscroll.pos;
		n = 0;

		while (p < _num_industry_sort) {
			const Industry* i = _industry_sort[p];
			const IndustrySpec *ind = GetIndustrySpec(i->type);

			SetDParam(0, i->index);
			if (ind->produced_cargo[0] != CT_INVALID) {
				SetDParam(1, ind->produced_cargo[0]);
				SetDParam(2, i->last_month_production[0]);

				if (ind->produced_cargo[1] != CT_INVALID) {
					SetDParam(3, ind->produced_cargo[1]);
					SetDParam(4, i->last_month_production[1]);
					SetDParam(5, i->last_month_pct_transported[0] * 100 >> 8);
					SetDParam(6, i->last_month_pct_transported[1] * 100 >> 8);
					DrawString(4, 28 + n * 10, STR_INDUSTRYDIR_ITEM_TWO, 0);
				} else {
					SetDParam(3, i->last_month_pct_transported[0] * 100 >> 8);
					DrawString(4, 28 + n * 10, STR_INDUSTRYDIR_ITEM, 0);
				}
			} else {
				DrawString(4, 28 + n * 10, STR_INDUSTRYDIR_ITEM_NOPROD, 0);
			}
			p++;
			if (++n == w->vscroll.cap) break;
		}
	} break;

	case WE_CLICK:
		switch (e->we.click.widget) {
			case DIRECTORY_INDU_SORTBYNAME: {
				_industry_sort_order = _industry_sort_order == 0 ? 1 : 0;
				_industry_sort_dirty = true;
				SetWindowDirty(w);
			} break;

			case DIRECTORY_INDU_SORTBYTYPE: {
				_industry_sort_order = _industry_sort_order == 2 ? 3 : 2;
				_industry_sort_dirty = true;
				SetWindowDirty(w);
			} break;

			case DIRECTORY_INDU_SORTBYPROD: {
				_industry_sort_order = _industry_sort_order == 4 ? 5 : 4;
				_industry_sort_dirty = true;
				SetWindowDirty(w);
			} break;

			case DIRECTORY_INDU_SORTBYTRANSPORT: {
				_industry_sort_order = _industry_sort_order == 6 ? 7 : 6;
				_industry_sort_dirty = true;
				SetWindowDirty(w);
			} break;

			case DIRECTORY_INDU_SHOWINDU: {
				int y = (e->we.click.pt.y - 28) / 10;
				uint16 p;

				if (!IS_INT_INSIDE(y, 0, w->vscroll.cap)) return;
				p = y + w->vscroll.pos;
				if (p < _num_industry_sort) {
					ScrollMainWindowToTile(_industry_sort[p]->xy);
				}
			} break;
		}
		break;

	case WE_4:
		SetWindowDirty(w);
		break;

	case WE_RESIZE:
		w->vscroll.cap += e->we.sizing.diff.y / 10;
		break;
	}
}


/* Industry List */
static const WindowDesc _industry_directory_desc = {
	WDP_AUTO, WDP_AUTO, 508, 190, 508, 190,
	WC_INDUSTRY_DIRECTORY, WC_NONE,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
	_industry_directory_widgets,
	IndustryDirectoryWndProc
};


void ShowIndustryDirectory()
{
	Window *w = AllocateWindowDescFront(&_industry_directory_desc, 0);

	if (w != NULL) {
		w->vscroll.cap = 16;
		w->resize.height = w->height - 6 * 10; // minimum 10 items
		w->resize.step_height = 10;
		SetWindowDirty(w);
	}
}