Files @ r28195:34539f38800e
Branch filter:

Location: cpp/openttd-patchpack/source/src/osk_gui.cpp - annotation

Tyler Trahan
Codechange: Use consistent name for bay road stops

As of #10494, this is how we describe original dead-end road stops.
  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
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9179:660680ae613d
r20640:cf721180dc1a
r25061:ef97237ddb50
r8737:36bd0e7321ea
r18670:f122c356353c
r18670:f122c356353c
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r25468:f59ee36e2847
r27737:728d55b97775
r8737:36bd0e7321ea
r15175:66e0817dc450
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9179:660680ae613d
r9239:98232327a7cb
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r27316:2b81a994658e
r13345:b00589315ee3
r9179:660680ae613d
r20280:ca1fc41725ff
r9179:660680ae613d
r9179:660680ae613d
r23607:36c15679007d
r9179:660680ae613d
r13560:251ed15297e0
r23607:36c15679007d
r9179:660680ae613d
r27384:b919ab98bb53
r27384:b919ab98bb53
r19787:20d2473dd1cb
r9179:660680ae613d
r19787:20d2473dd1cb
r19819:048b8ca075ad
r9179:660680ae613d
r9179:660680ae613d
r27316:2b81a994658e
r9179:660680ae613d
r20280:ca1fc41725ff
r19819:048b8ca075ad
r13345:b00589315ee3
r9179:660680ae613d
r18692:3617c7d8c6ef
r9179:660680ae613d
r13345:b00589315ee3
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r13345:b00589315ee3
r9179:660680ae613d
r13345:b00589315ee3
r13345:b00589315ee3
r9179:660680ae613d
r18692:3617c7d8c6ef
r20121:ff969d694f85
r9179:660680ae613d
r20121:ff969d694f85
r13345:b00589315ee3
r18692:3617c7d8c6ef
r18692:3617c7d8c6ef
r13345:b00589315ee3
r13345:b00589315ee3
r23499:f9bf6ad58697
r13345:b00589315ee3
r18692:3617c7d8c6ef
r13345:b00589315ee3
r13345:b00589315ee3
r23499:f9bf6ad58697
r13345:b00589315ee3
r18692:3617c7d8c6ef
r13345:b00589315ee3
r18692:3617c7d8c6ef
r25320:6bbcfa7c9984
r9179:660680ae613d
r9179:660680ae613d
r27942:f7389062d120
r9179:660680ae613d
r9179:660680ae613d
r18692:3617c7d8c6ef
r27737:728d55b97775
r8737:36bd0e7321ea
r20121:ff969d694f85
r8737:36bd0e7321ea
r19817:11d26f5e68c7
r8737:36bd0e7321ea
r9179:660680ae613d
r9179:660680ae613d
r19818:f59a0cfcd577
r9179:660680ae613d
r8737:36bd0e7321ea
r9179:660680ae613d
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9179:660680ae613d
r18692:3617c7d8c6ef
r19817:11d26f5e68c7
r9179:660680ae613d
r9179:660680ae613d
r18692:3617c7d8c6ef
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r8737:36bd0e7321ea
r18692:3617c7d8c6ef
r9179:660680ae613d
r13345:b00589315ee3
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r18692:3617c7d8c6ef
r9179:660680ae613d
r13345:b00589315ee3
r9179:660680ae613d
r9179:660680ae613d
r9179:660680ae613d
r18692:3617c7d8c6ef
r19817:11d26f5e68c7
r9179:660680ae613d
r8737:36bd0e7321ea
r18692:3617c7d8c6ef
r19817:11d26f5e68c7
r9179:660680ae613d
r8737:36bd0e7321ea
r18692:3617c7d8c6ef
r19817:11d26f5e68c7
r9179:660680ae613d
r8737:36bd0e7321ea
r18692:3617c7d8c6ef
r27316:2b81a994658e
r9179:660680ae613d
r19776:433be3248044
r19776:433be3248044
r9217:67c24e396019
r9217:67c24e396019
r9179:660680ae613d
r9179:660680ae613d
r25564:c875d92c537a
r9179:660680ae613d
r9179:660680ae613d
r18692:3617c7d8c6ef
r19776:433be3248044
r19776:433be3248044
r9179:660680ae613d
r9217:67c24e396019
r9179:660680ae613d
r27316:2b81a994658e
r19407:8e8cbec18948
r19817:11d26f5e68c7
r25564:c875d92c537a
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9179:660680ae613d
r11030:4d8ca8b9bddb
r11030:4d8ca8b9bddb
r23499:f9bf6ad58697
r11030:4d8ca8b9bddb
r27943:23c15817f0a5
r27943:23c15817f0a5
r27943:23c15817f0a5
r27943:23c15817f0a5
r27943:23c15817f0a5
r9179:660680ae613d
r8737:36bd0e7321ea
r27942:f7389062d120
r9399:b351385f4246
r17476:d3b7a183536d
r18692:3617c7d8c6ef
r19817:11d26f5e68c7
r9399:b351385f4246
r19819:048b8ca075ad
r27570:dc26d10816f1
r19819:048b8ca075ad
r21436:38079fc3bcd8
r27570:dc26d10816f1
r19819:048b8ca075ad
r9179:660680ae613d
r8737:36bd0e7321ea
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r25328:d832bbf309b2
r13060:e57594b0ca84
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r11775:30c13822d978
r27737:728d55b97775
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r24597:afde5721a3b6
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r18692:3617c7d8c6ef
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r25328:d832bbf309b2
r18692:3617c7d8c6ef
r25328:d832bbf309b2
r11775:30c13822d978
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r25328:d832bbf309b2
r18692:3617c7d8c6ef
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r25328:d832bbf309b2
r18692:3617c7d8c6ef
r25328:d832bbf309b2
r11775:30c13822d978
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r12393:6cdec1f68c3f
r11775:30c13822d978
r13580:2739496cc702
r11775:30c13822d978
r25328:d832bbf309b2
r25328:d832bbf309b2
r25328:d832bbf309b2
r25328:d832bbf309b2
r25328:d832bbf309b2
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r27245:43b55d4c0b1f
r13745:49505a0400d8
r18692:3617c7d8c6ef
r11775:30c13822d978
r13745:49505a0400d8
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r11775:30c13822d978
r28078:5e5d6c0447b1
r27724:fe6470f73b2e
r8737:36bd0e7321ea
r19745:d8f95208ad9e
r27861:6269475166e0
r11368:058349c3a02c
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r27457:19610be749a0
r27457:19610be749a0
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r27457:19610be749a0
r27457:19610be749a0
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r27457:19610be749a0
r8739:43248b3c4490
r8737:36bd0e7321ea
r27457:19610be749a0
r8737:36bd0e7321ea
r8739:43248b3c4490
r8739:43248b3c4490
r8739:43248b3c4490
r8739:43248b3c4490
r8739:43248b3c4490
r8739:43248b3c4490
r8739:43248b3c4490
r8737:36bd0e7321ea
r27457:19610be749a0
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r27457:19610be749a0
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r27147:5d938ed2c7b5
r27147:5d938ed2c7b5
r27147:5d938ed2c7b5
r27147:5d938ed2c7b5
r27147:5d938ed2c7b5
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r9314:0cd85d317c52
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r19787:20d2473dd1cb
r8737:36bd0e7321ea
r25565:6a5de7df7ea1
r8737:36bd0e7321ea
r8737:36bd0e7321ea
r19776:433be3248044
r8737:36bd0e7321ea
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r23838:bfeaabaa7b1d
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r19787:20d2473dd1cb
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r23607:36c15679007d
r13233:9fcdcfbe5992
r27316:2b81a994658e
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r13233:9fcdcfbe5992
r19819:048b8ca075ad
r19819:048b8ca075ad
r19819:048b8ca075ad
r23023:7b8669afd1db
r19819:048b8ca075ad
r23023:7b8669afd1db
r19819:048b8ca075ad
r19819:048b8ca075ad
r19819:048b8ca075ad
r19819:048b8ca075ad
r23607:36c15679007d
r19819:048b8ca075ad
/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file osk_gui.cpp The On Screen Keyboard GUI */

#include "stdafx.h"
#include "string_func.h"
#include "strings_func.h"
#include "debug.h"
#include "window_func.h"
#include "gfx_func.h"
#include "querystring_gui.h"
#include "video/video_driver.hpp"
#include "zoom_func.h"

#include "widgets/osk_widget.h"

#include "table/sprites.h"
#include "table/strings.h"

#include "safeguards.h"

std::string _keyboard_opt[2];
static char32_t _keyboard[2][OSK_KEYBOARD_ENTRIES];

enum KeyStateBits {
	KEYS_NONE,
	KEYS_SHIFT,
	KEYS_CAPS
};
static byte _keystate = KEYS_NONE;

struct OskWindow : public Window {
	StringID caption;      ///< the caption for this window.
	QueryString *qs;       ///< text-input
	int text_btn;          ///< widget number of parent's text field
	Textbuf *text;         ///< pointer to parent's textbuffer (to update caret position)
	std::string orig_str;  ///< Original string.
	bool shift;            ///< Is the shift effectively pressed?

	OskWindow(WindowDesc *desc, Window *parent, int button) : Window(desc)
	{
		this->parent = parent;
		assert(parent != nullptr);

		NWidgetCore *par_wid = parent->GetWidget<NWidgetCore>(button);
		assert(par_wid != nullptr);

		assert(parent->querystrings.count(button) != 0);
		this->qs         = parent->querystrings.find(button)->second;
		this->caption = (par_wid->widget_data != STR_NULL) ? par_wid->widget_data : this->qs->caption;
		this->text_btn   = button;
		this->text       = &this->qs->text;
		this->querystrings[WID_OSK_TEXT] = this->qs;

		/* make a copy in case we need to reset later */
		this->orig_str = this->qs->text.buf;

		this->InitNested(0);
		this->SetFocusedWidget(WID_OSK_TEXT);

		/* Not needed by default. */
		this->DisableWidget(WID_OSK_SPECIAL);

		this->UpdateOskState();
	}

	/**
	 * Only show valid characters; do not show characters that would
	 * only insert a space when we have a spacebar to do that or
	 * characters that are not allowed to be entered.
	 */
	void UpdateOskState()
	{
		this->shift = HasBit(_keystate, KEYS_CAPS) ^ HasBit(_keystate, KEYS_SHIFT);

		for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) {
			this->SetWidgetDisabledState(WID_OSK_LETTERS + i,
					!IsValidChar(_keyboard[this->shift][i], this->qs->text.afilter) || _keyboard[this->shift][i] == ' ');
		}
		this->SetWidgetDisabledState(WID_OSK_SPACE, !IsValidChar(' ', this->qs->text.afilter));

		this->SetWidgetLoweredState(WID_OSK_SHIFT, HasBit(_keystate, KEYS_SHIFT));
		this->SetWidgetLoweredState(WID_OSK_CAPS, HasBit(_keystate, KEYS_CAPS));
	}

	void SetStringParameters(int widget) const override
	{
		if (widget == WID_OSK_CAPTION) SetDParam(0, this->caption);
	}

	void DrawWidget(const Rect &r, int widget) const override
	{
		if (widget < WID_OSK_LETTERS) return;

		widget -= WID_OSK_LETTERS;
		DrawCharCentered(_keyboard[this->shift][widget], r, TC_BLACK);
	}

	void OnClick([[maybe_unused]] Point pt, int widget, [[maybe_unused]] int click_count) override
	{
		/* clicked a letter */
		if (widget >= WID_OSK_LETTERS) {
			char32_t c = _keyboard[this->shift][widget - WID_OSK_LETTERS];

			if (!IsValidChar(c, this->qs->text.afilter)) return;

			if (this->qs->text.InsertChar(c)) this->OnEditboxChanged(WID_OSK_TEXT);

			if (HasBit(_keystate, KEYS_SHIFT)) {
				ToggleBit(_keystate, KEYS_SHIFT);
				this->UpdateOskState();
				this->SetDirty();
			}
			return;
		}

		switch (widget) {
			case WID_OSK_BACKSPACE:
				if (this->qs->text.DeleteChar(WKC_BACKSPACE)) this->OnEditboxChanged(WID_OSK_TEXT);
				break;

			case WID_OSK_SPECIAL:
				/*
				 * Anything device specific can go here.
				 * The button itself is hidden by default, and when you need it you
				 * can not hide it in the create event.
				 */
				break;

			case WID_OSK_CAPS:
				ToggleBit(_keystate, KEYS_CAPS);
				this->UpdateOskState();
				this->SetDirty();
				break;

			case WID_OSK_SHIFT:
				ToggleBit(_keystate, KEYS_SHIFT);
				this->UpdateOskState();
				this->SetDirty();
				break;

			case WID_OSK_SPACE:
				if (this->qs->text.InsertChar(' ')) this->OnEditboxChanged(WID_OSK_TEXT);
				break;

			case WID_OSK_LEFT:
				if (this->qs->text.MovePos(WKC_LEFT)) this->InvalidateData();
				break;

			case WID_OSK_RIGHT:
				if (this->qs->text.MovePos(WKC_RIGHT)) this->InvalidateData();
				break;

			case WID_OSK_OK:
				if (!this->qs->orig.has_value() || this->qs->text.buf != this->qs->orig) {
					/* pass information by simulating a button press on parent window */
					if (this->qs->ok_button >= 0) {
						this->parent->OnClick(pt, this->qs->ok_button, 1);
						/* Window gets deleted when the parent window removes itself. */
						return;
					}
				}
				this->Close();
				break;

			case WID_OSK_CANCEL:
				if (this->qs->cancel_button >= 0) { // pass a cancel event to the parent window
					this->parent->OnClick(pt, this->qs->cancel_button, 1);
					/* Window gets deleted when the parent window removes itself. */
					return;
				} else { // or reset to original string
					qs->text.Assign(this->orig_str);
					qs->text.MovePos(WKC_END);
					this->OnEditboxChanged(WID_OSK_TEXT);
					this->Close();
				}
				break;
		}
	}

	void OnEditboxChanged(int widget) override
	{
		if (widget == WID_OSK_TEXT) {
			this->SetWidgetDirty(WID_OSK_TEXT);
			this->parent->OnEditboxChanged(this->text_btn);
			this->parent->SetWidgetDirty(this->text_btn);
		}
	}

	void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
	{
		if (!gui_scope) return;
		this->SetWidgetDirty(WID_OSK_TEXT);
		this->parent->SetWidgetDirty(this->text_btn);
	}

	void OnFocusLost(bool closing) override
	{
		VideoDriver::GetInstance()->EditBoxLostFocus();
		if (!closing) this->Close();
	}
};

static const int HALF_KEY_WIDTH = 7;  // Width of 1/2 key in pixels.
static const int INTER_KEY_SPACE = 2; // Number of pixels between two keys.

static const int TOP_KEY_PADDING = 2; // Vertical padding for the top row of keys.
static const int KEY_PADDING = 6;     // Vertical padding for remaining key rows.

/**
 * Add a key widget to a row of the keyboard.
 * @param hor     Row container to add key widget to.
 * @param pad_y   Vertical padding of the key (all keys in a row should have equal padding).
 * @param num_half Number of 1/2 key widths that this key has.
 * @param widtype Widget type of the key. Must be either \c NWID_SPACER for an invisible key, or a \c WWT_* widget.
 * @param widnum  Widget number of the key.
 * @param widdata Data value of the key widget.
 * @param biggest_index Collected biggest widget index so far.
 * @note Key width is measured in 1/2 keys to allow for 1/2 key shifting between rows.
 */
static void AddKey(NWidgetHorizontal *hor, int pad_y, int num_half, WidgetType widtype, int widnum, uint16_t widdata, int *biggest_index)
{
	int key_width = HALF_KEY_WIDTH + (INTER_KEY_SPACE + HALF_KEY_WIDTH) * (num_half - 1);

	if (widtype == NWID_SPACER) {
		if (!hor->IsEmpty()) key_width += INTER_KEY_SPACE;
		NWidgetSpacer *spc = new NWidgetSpacer(key_width, 0);
		spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
		hor->Add(spc);
	} else {
		if (!hor->IsEmpty()) {
			NWidgetSpacer *spc = new NWidgetSpacer(INTER_KEY_SPACE, 0);
			spc->SetMinimalTextLines(1, pad_y, FS_NORMAL);
			hor->Add(spc);
		}
		NWidgetLeaf *leaf = new NWidgetLeaf(widtype, COLOUR_GREY, widnum, widdata, STR_NULL);
		leaf->SetMinimalSize(key_width, 0);
		leaf->SetMinimalTextLines(1, pad_y, FS_NORMAL);
		hor->Add(leaf);
	}

	*biggest_index = std::max(*biggest_index, widnum);
}

/** Construct the top row keys (cancel, ok, backspace). */
static NWidgetBase *MakeTopKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontal();

	AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN,    WID_OSK_CANCEL,    STR_BUTTON_CANCEL, biggest_index);
	AddKey(hor, TOP_KEY_PADDING, 6 * 2, WWT_TEXTBTN,    WID_OSK_OK,        STR_BUTTON_OK,     biggest_index);
	AddKey(hor, TOP_KEY_PADDING, 2 * 2, WWT_PUSHIMGBTN, WID_OSK_BACKSPACE, SPR_OSK_BACKSPACE, biggest_index);
	return hor;
}

/** Construct the row containing the digit keys. */
static NWidgetBase *MakeNumberKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontalLTR();

	for (int widnum = WID_OSK_NUMBERS_FIRST; widnum <= WID_OSK_NUMBERS_LAST; widnum++) {
		AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
	}
	return hor;
}

/** Construct the qwerty row keys. */
static NWidgetBase *MakeQwertyKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontalLTR();

	AddKey(hor, KEY_PADDING, 3, WWT_PUSHIMGBTN, WID_OSK_SPECIAL, SPR_OSK_SPECIAL, biggest_index);
	for (int widnum = WID_OSK_QWERTY_FIRST; widnum <= WID_OSK_QWERTY_LAST; widnum++) {
		AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
	}
	AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0, biggest_index);
	return hor;
}

/** Construct the asdfg row keys. */
static NWidgetBase *MakeAsdfgKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontalLTR();

	AddKey(hor, KEY_PADDING, 4, WWT_IMGBTN, WID_OSK_CAPS, SPR_OSK_CAPS, biggest_index);
	for (int widnum = WID_OSK_ASDFG_FIRST; widnum <= WID_OSK_ASDFG_LAST; widnum++) {
		AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
	}
	return hor;
}

/** Construct the zxcvb row keys. */
static NWidgetBase *MakeZxcvbKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontalLTR();

	AddKey(hor, KEY_PADDING, 3, WWT_IMGBTN, WID_OSK_SHIFT, SPR_OSK_SHIFT, biggest_index);
	for (int widnum = WID_OSK_ZXCVB_FIRST; widnum <= WID_OSK_ZXCVB_LAST; widnum++) {
		AddKey(hor, KEY_PADDING, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
	}
	AddKey(hor, KEY_PADDING, 1, NWID_SPACER, 0, 0, biggest_index);
	return hor;
}

/** Construct the spacebar row keys. */
static NWidgetBase *MakeSpacebarKeys(int *biggest_index)
{
	NWidgetHorizontal *hor = new NWidgetHorizontal();

	AddKey(hor, KEY_PADDING,  8, NWID_SPACER, 0, 0, biggest_index);
	AddKey(hor, KEY_PADDING, 13, WWT_PUSHTXTBTN, WID_OSK_SPACE, STR_EMPTY, biggest_index);
	AddKey(hor, KEY_PADDING,  3, NWID_SPACER, 0, 0, biggest_index);
	AddKey(hor, KEY_PADDING,  2, WWT_PUSHIMGBTN, WID_OSK_LEFT,  SPR_OSK_LEFT, biggest_index);
	AddKey(hor, KEY_PADDING,  2, WWT_PUSHIMGBTN, WID_OSK_RIGHT, SPR_OSK_RIGHT, biggest_index);
	return hor;
}


static const NWidgetPart _nested_osk_widgets[] = {
	NWidget(WWT_CAPTION, COLOUR_GREY, WID_OSK_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL), SetTextStyle(TC_WHITE),
	NWidget(WWT_PANEL, COLOUR_GREY),
		NWidget(WWT_EDITBOX, COLOUR_GREY, WID_OSK_TEXT), SetMinimalSize(252, 12), SetPadding(2, 2, 2, 2),
	EndContainer(),
	NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(5, 2, 3),
		NWidgetFunction(MakeTopKeys), SetPadding(0, 3, 0, 3),
		NWidgetFunction(MakeNumberKeys), SetPadding(0, 3, 0, 3),
		NWidgetFunction(MakeQwertyKeys), SetPadding(0, 3, 0, 3),
		NWidgetFunction(MakeAsdfgKeys), SetPadding(0, 3, 0, 3),
		NWidgetFunction(MakeZxcvbKeys), SetPadding(0, 3, 0, 3),
		NWidgetFunction(MakeSpacebarKeys), SetPadding(0, 3, 0, 3),
	EndContainer(),
};

static WindowDesc _osk_desc(__FILE__, __LINE__,
	WDP_CENTER, nullptr, 0, 0,
	WC_OSK, WC_NONE,
	0,
	std::begin(_nested_osk_widgets), std::end(_nested_osk_widgets)
);

/**
 * Retrieve keyboard layout from language string or (if set) config file.
 * Also check for invalid characters.
 */
void GetKeyboardLayout()
{
	std::string keyboard[2];
	std::string errormark[2]; // used for marking invalid chars
	bool has_error = false; // true when an invalid char is detected

	keyboard[0] = _keyboard_opt[0].empty() ? GetString(STR_OSK_KEYBOARD_LAYOUT) : _keyboard_opt[0];
	keyboard[1] = _keyboard_opt[1].empty() ? GetString(STR_OSK_KEYBOARD_LAYOUT_CAPS) : _keyboard_opt[1];

	for (uint j = 0; j < 2; j++) {
		auto kbd = keyboard[j].begin();
		bool ended = false;
		for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) {
			_keyboard[j][i] = Utf8Consume(kbd);

			/* Be lenient when the last characters are missing (is quite normal) */
			if (_keyboard[j][i] == '\0' || ended) {
				ended = true;
				_keyboard[j][i] = ' ';
				continue;
			}

			if (IsPrintable(_keyboard[j][i])) {
				errormark[j] += ' ';
			} else {
				has_error = true;
				errormark[j] += '^';
				_keyboard[j][i] = ' ';
			}
		}
	}

	if (has_error) {
		ShowInfo("The keyboard layout you selected contains invalid chars. Please check those chars marked with ^.");
		ShowInfo("Normal keyboard:  {}", keyboard[0]);
		ShowInfo("                  {}", errormark[0]);
		ShowInfo("Caps Lock:        {}", keyboard[1]);
		ShowInfo("                  {}", errormark[1]);
	}
}

/**
 * Show the on-screen keyboard (osk) associated with a given textbox
 * @param parent pointer to the Window where this keyboard originated from
 * @param button widget number of parent's textbox
 */
void ShowOnScreenKeyboard(Window *parent, int button)
{
	CloseWindowById(WC_OSK, 0);

	GetKeyboardLayout();
	new OskWindow(&_osk_desc, parent, button);
}

/**
 * Updates the original text of the OSK so when the 'parent' changes the
 * original and you press on cancel you won't get the 'old' original text
 * but the updated one.
 * @param parent window that just updated its original text
 * @param button widget number of parent's textbox to update
 */
void UpdateOSKOriginalText(const Window *parent, int button)
{
	OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
	if (osk == nullptr || osk->parent != parent || osk->text_btn != button) return;

	osk->orig_str = osk->qs->text.buf;

	osk->SetDirty();
}

/**
 * Check whether the OSK is opened for a specific editbox.
 * @param w Window to check for
 * @param button Editbox of \a w to check for
 * @return true if the OSK is opened for \a button.
 */
bool IsOSKOpenedFor(const Window *w, int button)
{
	OskWindow *osk = dynamic_cast<OskWindow *>(FindWindowById(WC_OSK, 0));
	return osk != nullptr && osk->parent == w && osk->text_btn == button;
}