/
window.cpp
3548 lines (3056 loc) · 111 KB
/
window.cpp
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
/* $Id$ */
/*
* 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 window.cpp Windowing system, widgets and events */
#include "stdafx.h"
#include <stdarg.h>
#include "company_func.h"
#include "gfx_func.h"
#include "console_func.h"
#include "console_gui.h"
#include "viewport_func.h"
#include "progress.h"
#include "blitter/factory.hpp"
#include "zoom_func.h"
#include "vehicle_base.h"
#include "window_func.h"
#include "tilehighlight_func.h"
#include "network/network.h"
#include "querystring_gui.h"
#include "widgets/dropdown_func.h"
#include "strings_func.h"
#include "settings_type.h"
#include "settings_func.h"
#include "ini_type.h"
#include "newgrf_debug.h"
#include "hotkeys.h"
#include "toolbar_gui.h"
#include "statusbar_gui.h"
#include "error.h"
#include "game/game.hpp"
#include "video/video_driver.hpp"
#include "framerate_type.h"
#include "safeguards.h"
/** Values for _settings_client.gui.auto_scrolling */
enum ViewportAutoscrolling {
VA_DISABLED, //!< Do not autoscroll when mouse is at edge of viewport.
VA_MAIN_VIEWPORT_FULLSCREEN, //!< Scroll main viewport at edge when using fullscreen.
VA_MAIN_VIEWPORT, //!< Scroll main viewport at edge.
VA_EVERY_VIEWPORT, //!< Scroll all viewports at their edges.
};
static Point _drag_delta; ///< delta between mouse cursor and upper left corner of dragged window
static Window *_mouseover_last_w = NULL; ///< Window of the last OnMouseOver event.
static Window *_last_scroll_window = NULL; ///< Window of the last scroll event.
/** List of windows opened at the screen sorted from the front. */
Window *_z_front_window = NULL;
/** List of windows opened at the screen sorted from the back. */
Window *_z_back_window = NULL;
/** If false, highlight is white, otherwise the by the widget defined colour. */
bool _window_highlight_colour = false;
/*
* Window that currently has focus. - The main purpose is to generate
* #FocusLost events, not to give next window in z-order focus when a
* window is closed.
*/
Window *_focused_window;
Point _cursorpos_drag_start;
int _scrollbar_start_pos;
int _scrollbar_size;
byte _scroller_click_timeout = 0;
bool _scrolling_viewport; ///< A viewport is being scrolled with the mouse.
bool _mouse_hovering; ///< The mouse is hovering over the same point.
SpecialMouseMode _special_mouse_mode; ///< Mode of the mouse.
/**
* List of all WindowDescs.
* This is a pointer to ensure initialisation order with the various static WindowDesc instances.
*/
static SmallVector<WindowDesc*, 16> *_window_descs = NULL;
/** Config file to store WindowDesc */
char *_windows_file;
/** Window description constructor. */
WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width_trad, int16 def_height_trad,
WindowClass window_class, WindowClass parent_class, uint32 flags,
const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
default_pos(def_pos),
cls(window_class),
parent_cls(parent_class),
ini_key(ini_key),
flags(flags),
nwid_parts(nwid_parts),
nwid_length(nwid_length),
hotkeys(hotkeys),
pref_sticky(false),
pref_width(0),
pref_height(0),
default_width_trad(def_width_trad),
default_height_trad(def_height_trad)
{
if (_window_descs == NULL) _window_descs = new SmallVector<WindowDesc*, 16>();
*_window_descs->Append() = this;
}
WindowDesc::~WindowDesc()
{
_window_descs->Erase(_window_descs->Find(this));
}
/**
* Determine default width of window.
* This is either a stored user preferred size, or the build-in default.
* @return Width in pixels.
*/
int16 WindowDesc::GetDefaultWidth() const
{
return this->pref_width != 0 ? this->pref_width : ScaleGUITrad(this->default_width_trad);
}
/**
* Determine default height of window.
* This is either a stored user preferred size, or the build-in default.
* @return Height in pixels.
*/
int16 WindowDesc::GetDefaultHeight() const
{
return this->pref_height != 0 ? this->pref_height : ScaleGUITrad(this->default_height_trad);
}
/**
* Load all WindowDesc settings from _windows_file.
*/
void WindowDesc::LoadFromConfig()
{
IniFile *ini = new IniFile();
ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
if ((*it)->ini_key == NULL) continue;
IniLoadWindowSettings(ini, (*it)->ini_key, *it);
}
delete ini;
}
/**
* Sort WindowDesc by ini_key.
*/
static int CDECL DescSorter(WindowDesc * const *a, WindowDesc * const *b)
{
if ((*a)->ini_key != NULL && (*b)->ini_key != NULL) return strcmp((*a)->ini_key, (*b)->ini_key);
return ((*b)->ini_key != NULL ? 1 : 0) - ((*a)->ini_key != NULL ? 1 : 0);
}
/**
* Save all WindowDesc settings to _windows_file.
*/
void WindowDesc::SaveToConfig()
{
/* Sort the stuff to get a nice ini file on first write */
QSortT(_window_descs->Begin(), _window_descs->Length(), DescSorter);
IniFile *ini = new IniFile();
ini->LoadFromDisk(_windows_file, NO_DIRECTORY);
for (WindowDesc **it = _window_descs->Begin(); it != _window_descs->End(); ++it) {
if ((*it)->ini_key == NULL) continue;
IniSaveWindowSettings(ini, (*it)->ini_key, *it);
}
ini->SaveToDisk(_windows_file);
delete ini;
}
/**
* Read default values from WindowDesc configuration an apply them to the window.
*/
void Window::ApplyDefaults()
{
if (this->nested_root != NULL && this->nested_root->GetWidgetOfType(WWT_STICKYBOX) != NULL) {
if (this->window_desc->pref_sticky) this->flags |= WF_STICKY;
} else {
/* There is no stickybox; clear the preference in case someone tried to be funny */
this->window_desc->pref_sticky = false;
}
}
/**
* Compute the row of a widget that a user clicked in.
* @param clickpos Vertical position of the mouse click.
* @param widget Widget number of the widget clicked in.
* @param padding Amount of empty space between the widget edge and the top of the first row.
* @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
* @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
* @note The widget does not know where a list printed at the widget ends, so below a list is not a wrong position.
*/
int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
{
const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
if (line_height < 0) line_height = wid->resize_y;
if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
return (clickpos - (int)wid->pos_y - padding) / line_height;
}
/**
* Disable the highlighted status of all widgets.
*/
void Window::DisableAllWidgetHighlight()
{
for (uint i = 0; i < this->nested_array_size; i++) {
NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
if (nwid == NULL) continue;
if (nwid->IsHighlighted()) {
nwid->SetHighlighted(TC_INVALID);
this->SetWidgetDirty(i);
}
}
CLRBITS(this->flags, WF_HIGHLIGHTED);
}
/**
* Sets the highlighted status of a widget.
* @param widget_index index of this widget in the window
* @param highlighted_colour Colour of highlight, or TC_INVALID to disable.
*/
void Window::SetWidgetHighlight(byte widget_index, TextColour highlighted_colour)
{
assert(widget_index < this->nested_array_size);
NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
if (nwid == NULL) return;
nwid->SetHighlighted(highlighted_colour);
this->SetWidgetDirty(widget_index);
if (highlighted_colour != TC_INVALID) {
/* If we set a highlight, the window has a highlight */
this->flags |= WF_HIGHLIGHTED;
} else {
/* If we disable a highlight, check all widgets if anyone still has a highlight */
bool valid = false;
for (uint i = 0; i < this->nested_array_size; i++) {
NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
if (nwid == NULL) continue;
if (!nwid->IsHighlighted()) continue;
valid = true;
}
/* If nobody has a highlight, disable the flag on the window */
if (!valid) CLRBITS(this->flags, WF_HIGHLIGHTED);
}
}
/**
* Gets the highlighted status of a widget.
* @param widget_index index of this widget in the window
* @return status of the widget ie: highlighted = true, not highlighted = false
*/
bool Window::IsWidgetHighlighted(byte widget_index) const
{
assert(widget_index < this->nested_array_size);
const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget_index);
if (nwid == NULL) return false;
return nwid->IsHighlighted();
}
/**
* A dropdown window associated to this window has been closed.
* @param pt the point inside the window the mouse resides on after closure.
* @param widget the widget (button) that the dropdown is associated with.
* @param index the element in the dropdown that is selected.
* @param instant_close whether the dropdown was configured to close on mouse up.
*/
void Window::OnDropdownClose(Point pt, int widget, int index, bool instant_close)
{
if (widget < 0) return;
if (instant_close) {
/* Send event for selected option if we're still
* on the parent button of the dropdown (behaviour of the dropdowns in the main toolbar). */
if (GetWidgetFromPos(this, pt.x, pt.y) == widget) {
this->OnDropdownSelect(widget, index);
}
}
/* Raise the dropdown button */
NWidgetCore *nwi2 = this->GetWidget<NWidgetCore>(widget);
if ((nwi2->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
nwi2->disp_flags &= ~ND_DROPDOWN_ACTIVE;
} else {
this->RaiseWidget(widget);
}
this->SetWidgetDirty(widget);
}
/**
* Return the Scrollbar to a widget index.
* @param widnum Scrollbar widget index
* @return Scrollbar to the widget
*/
const Scrollbar *Window::GetScrollbar(uint widnum) const
{
return this->GetWidget<NWidgetScrollbar>(widnum);
}
/**
* Return the Scrollbar to a widget index.
* @param widnum Scrollbar widget index
* @return Scrollbar to the widget
*/
Scrollbar *Window::GetScrollbar(uint widnum)
{
return this->GetWidget<NWidgetScrollbar>(widnum);
}
/**
* Return the querystring associated to a editbox.
* @param widnum Editbox widget index
* @return QueryString or NULL.
*/
const QueryString *Window::GetQueryString(uint widnum) const
{
const SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
return query != this->querystrings.End() ? query->second : NULL;
}
/**
* Return the querystring associated to a editbox.
* @param widnum Editbox widget index
* @return QueryString or NULL.
*/
QueryString *Window::GetQueryString(uint widnum)
{
SmallMap<int, QueryString*>::Pair *query = this->querystrings.Find(widnum);
return query != this->querystrings.End() ? query->second : NULL;
}
/**
* Get the current input text if an edit box has the focus.
* @return The currently focused input text or NULL if no input focused.
*/
/* virtual */ const char *Window::GetFocusedText() const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetText();
}
return NULL;
}
/**
* Get the string at the caret if an edit box has the focus.
* @return The text at the caret or NULL if no edit box is focused.
*/
/* virtual */ const char *Window::GetCaret() const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetCaret();
}
return NULL;
}
/**
* Get the range of the currently marked input text.
* @param[out] length Length of the marked text.
* @return Pointer to the start of the marked text or NULL if no text is marked.
*/
/* virtual */ const char *Window::GetMarkedText(size_t *length) const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetMarkedText(length);
}
return NULL;
}
/**
* Get the current caret position if an edit box has the focus.
* @return Top-left location of the caret, relative to the window.
*/
/* virtual */ Point Window::GetCaretPosition() const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetCaretPosition(this, this->nested_focus->index);
}
Point pt = {0, 0};
return pt;
}
/**
* Get the bounding rectangle for a text range if an edit box has the focus.
* @param from Start of the string range.
* @param to End of the string range.
* @return Rectangle encompassing the string range, relative to the window.
*/
/* virtual */ Rect Window::GetTextBoundingRect(const char *from, const char *to) const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetBoundingRect(this, this->nested_focus->index, from, to);
}
Rect r = {0, 0, 0, 0};
return r;
}
/**
* Get the character that is rendered at a position by the focused edit box.
* @param pt The position to test.
* @return Pointer to the character at the position or NULL if no character is at the position.
*/
/* virtual */ const char *Window::GetTextCharacterAtPosition(const Point &pt) const
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) {
return this->GetQueryString(this->nested_focus->index)->GetCharAtPosition(this, this->nested_focus->index, pt);
}
return NULL;
}
/**
* Set the window that has the focus
* @param w The window to set the focus on
*/
void SetFocusedWindow(Window *w)
{
if (_focused_window == w) return;
/* Invalidate focused widget */
if (_focused_window != NULL) {
if (_focused_window->nested_focus != NULL) _focused_window->nested_focus->SetDirty(_focused_window);
}
/* Remember which window was previously focused */
Window *old_focused = _focused_window;
_focused_window = w;
/* So we can inform it that it lost focus */
if (old_focused != NULL) old_focused->OnFocusLost();
if (_focused_window != NULL) _focused_window->OnFocus();
}
/**
* Check if an edit box is in global focus. That is if focused window
* has a edit box as focused widget, or if a console is focused.
* @return returns true if an edit box is in global focus or if the focused window is a console, else false
*/
bool EditBoxInGlobalFocus()
{
if (_focused_window == NULL) return false;
/* The console does not have an edit box so a special case is needed. */
if (_focused_window->window_class == WC_CONSOLE) return true;
return _focused_window->nested_focus != NULL && _focused_window->nested_focus->type == WWT_EDITBOX;
}
/**
* Makes no widget on this window have focus. The function however doesn't change which window has focus.
*/
void Window::UnfocusFocusedWidget()
{
if (this->nested_focus != NULL) {
if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
/* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
this->nested_focus->SetDirty(this);
this->nested_focus = NULL;
}
}
/**
* Set focus within this window to the given widget. The function however doesn't change which window has focus.
* @param widget_index Index of the widget in the window to set the focus to.
* @return Focus has changed.
*/
bool Window::SetFocusedWidget(int widget_index)
{
/* Do nothing if widget_index is already focused, or if it wasn't a valid widget. */
if ((uint)widget_index >= this->nested_array_size) return false;
assert(this->nested_array[widget_index] != NULL); // Setting focus to a non-existing widget is a bad idea.
if (this->nested_focus != NULL) {
if (this->GetWidget<NWidgetCore>(widget_index) == this->nested_focus) return false;
/* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */
this->nested_focus->SetDirty(this);
if (this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
}
this->nested_focus = this->GetWidget<NWidgetCore>(widget_index);
return true;
}
/**
* Called when window looses focus
*/
void Window::OnFocusLost()
{
if (this->nested_focus != NULL && this->nested_focus->type == WWT_EDITBOX) VideoDriver::GetInstance()->EditBoxLostFocus();
}
/**
* Sets the enabled/disabled status of a list of widgets.
* By default, widgets are enabled.
* On certain conditions, they have to be disabled.
* @param disab_stat status to use ie: disabled = true, enabled = false
* @param widgets list of widgets ended by WIDGET_LIST_END
*/
void CDECL Window::SetWidgetsDisabledState(bool disab_stat, int widgets, ...)
{
va_list wdg_list;
va_start(wdg_list, widgets);
while (widgets != WIDGET_LIST_END) {
SetWidgetDisabledState(widgets, disab_stat);
widgets = va_arg(wdg_list, int);
}
va_end(wdg_list);
}
/**
* Sets the lowered/raised status of a list of widgets.
* @param lowered_stat status to use ie: lowered = true, raised = false
* @param widgets list of widgets ended by WIDGET_LIST_END
*/
void CDECL Window::SetWidgetsLoweredState(bool lowered_stat, int widgets, ...)
{
va_list wdg_list;
va_start(wdg_list, widgets);
while (widgets != WIDGET_LIST_END) {
SetWidgetLoweredState(widgets, lowered_stat);
widgets = va_arg(wdg_list, int);
}
va_end(wdg_list);
}
/**
* Raise the buttons of the window.
* @param autoraise Raise only the push buttons of the window.
*/
void Window::RaiseButtons(bool autoraise)
{
for (uint i = 0; i < this->nested_array_size; i++) {
if (this->nested_array[i] == NULL) continue;
WidgetType type = this->nested_array[i]->type;
if (((type & ~WWB_PUSHBUTTON) < WWT_LAST || type == NWID_PUSHBUTTON_DROPDOWN) &&
(!autoraise || (type & WWB_PUSHBUTTON) || type == WWT_EDITBOX) && this->IsWidgetLowered(i)) {
this->RaiseWidget(i);
this->SetWidgetDirty(i);
}
}
/* Special widgets without widget index */
NWidgetCore *wid = this->nested_root != NULL ? (NWidgetCore*)this->nested_root->GetWidgetOfType(WWT_DEFSIZEBOX) : NULL;
if (wid != NULL) {
wid->SetLowered(false);
wid->SetDirty(this);
}
}
/**
* Invalidate a widget, i.e. mark it as being changed and in need of redraw.
* @param widget_index the widget to redraw.
*/
void Window::SetWidgetDirty(byte widget_index) const
{
/* Sometimes this function is called before the window is even fully initialized */
if (this->nested_array == NULL) return;
this->nested_array[widget_index]->SetDirty(this);
}
/**
* A hotkey has been pressed.
* @param hotkey Hotkey index, by default a widget index of a button or editbox.
* @return #ES_HANDLED if the key press has been handled, and the hotkey is not unavailable for some reason.
*/
EventState Window::OnHotkey(int hotkey)
{
if (hotkey < 0) return ES_NOT_HANDLED;
NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
if (nw == NULL || nw->IsDisabled()) return ES_NOT_HANDLED;
if (nw->type == WWT_EDITBOX) {
if (this->IsShaded()) return ES_NOT_HANDLED;
/* Focus editbox */
this->SetFocusedWidget(hotkey);
SetFocusedWindow(this);
} else {
/* Click button */
this->OnClick(Point(), hotkey, 1);
}
return ES_HANDLED;
}
/**
* Do all things to make a button look clicked and mark it to be
* unclicked in a few ticks.
* @param widget the widget to "click"
*/
void Window::HandleButtonClick(byte widget)
{
this->LowerWidget(widget);
this->SetTimeout();
this->SetWidgetDirty(widget);
}
static void StartWindowDrag(Window *w);
static void StartWindowSizing(Window *w, bool to_left);
/**
* Dispatch left mouse-button (possibly double) click in window.
* @param w Window to dispatch event in
* @param x X coordinate of the click
* @param y Y coordinate of the click
* @param click_count Number of fast consecutive clicks at same position
*/
static void DispatchLeftClickEvent(Window *w, int x, int y, int click_count)
{
NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y);
WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY;
bool focused_widget_changed = false;
/* If clicked on a window that previously did dot have focus */
if (_focused_window != w && // We already have focus, right?
(w->window_desc->flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars
widget_type != WWT_CLOSEBOX) { // Don't change focused window if 'X' (close button) was clicked
focused_widget_changed = true;
SetFocusedWindow(w);
}
if (nw == NULL) return; // exit if clicked outside of widgets
/* don't allow any interaction if the button has been disabled */
if (nw->IsDisabled()) return;
int widget_index = nw->index; ///< Index of the widget
/* Clicked on a widget that is not disabled.
* So unless the clicked widget is the caption bar, change focus to this widget.
* Exception: In the OSK we always want the editbox to stay focussed. */
if (widget_type != WWT_CAPTION && w->window_class != WC_OSK) {
/* focused_widget_changed is 'now' only true if the window this widget
* is in gained focus. In that case it must remain true, also if the
* local widget focus did not change. As such it's the logical-or of
* both changed states.
*
* If this is not preserved, then the OSK window would be opened when
* a user has the edit box focused and then click on another window and
* then back again on the edit box (to type some text).
*/
focused_widget_changed |= w->SetFocusedWidget(widget_index);
}
/* Close any child drop down menus. If the button pressed was the drop down
* list's own button, then we should not process the click any further. */
if (HideDropDownMenu(w) == widget_index && widget_index >= 0) return;
if ((widget_type & ~WWB_PUSHBUTTON) < WWT_LAST && (widget_type & WWB_PUSHBUTTON)) w->HandleButtonClick(widget_index);
Point pt = { x, y };
switch (widget_type) {
case NWID_VSCROLLBAR:
case NWID_HSCROLLBAR:
ScrollbarClickHandler(w, nw, x, y);
break;
case WWT_EDITBOX: {
QueryString *query = w->GetQueryString(widget_index);
if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed);
break;
}
case WWT_CLOSEBOX: // 'X'
delete w;
return;
case WWT_CAPTION: // 'Title bar'
StartWindowDrag(w);
return;
case WWT_RESIZEBOX:
/* When the resize widget is on the left size of the window
* we assume that that button is used to resize to the left. */
StartWindowSizing(w, (int)nw->pos_x < (w->width / 2));
nw->SetDirty(w);
return;
case WWT_DEFSIZEBOX: {
if (_ctrl_pressed) {
w->window_desc->pref_width = w->width;
w->window_desc->pref_height = w->height;
} else {
int16 def_width = max<int16>(min(w->window_desc->GetDefaultWidth(), _screen.width), w->nested_root->smallest_x);
int16 def_height = max<int16>(min(w->window_desc->GetDefaultHeight(), _screen.height - 50), w->nested_root->smallest_y);
int dx = (w->resize.step_width == 0) ? 0 : def_width - w->width;
int dy = (w->resize.step_height == 0) ? 0 : def_height - w->height;
/* dx and dy has to go by step.. calculate it.
* The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
if (w->resize.step_width > 1) dx -= dx % (int)w->resize.step_width;
if (w->resize.step_height > 1) dy -= dy % (int)w->resize.step_height;
ResizeWindow(w, dx, dy, false);
}
nw->SetLowered(true);
nw->SetDirty(w);
w->SetTimeout();
break;
}
case WWT_DEBUGBOX:
w->ShowNewGRFInspectWindow();
break;
case WWT_SHADEBOX:
nw->SetDirty(w);
w->SetShaded(!w->IsShaded());
return;
case WWT_STICKYBOX:
w->flags ^= WF_STICKY;
nw->SetDirty(w);
if (_ctrl_pressed) w->window_desc->pref_sticky = (w->flags & WF_STICKY) != 0;
return;
default:
break;
}
/* Widget has no index, so the window is not interested in it. */
if (widget_index < 0) return;
/* Check if the widget is highlighted; if so, disable highlight and dispatch an event to the GameScript */
if (w->IsWidgetHighlighted(widget_index)) {
w->SetWidgetHighlight(widget_index, TC_INVALID);
Game::NewEvent(new ScriptEventWindowWidgetClick((ScriptWindow::WindowClass)w->window_class, w->window_number, widget_index));
}
w->OnClick(pt, widget_index, click_count);
}
/**
* Dispatch right mouse-button click in window.
* @param w Window to dispatch event in
* @param x X coordinate of the click
* @param y Y coordinate of the click
*/
static void DispatchRightClickEvent(Window *w, int x, int y)
{
NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
if (wid == NULL) return;
/* No widget to handle, or the window is not interested in it. */
if (wid->index >= 0) {
Point pt = { x, y };
if (w->OnRightClick(pt, wid->index)) return;
}
/* Right-click close is enabled and there is a closebox */
if (_settings_client.gui.right_mouse_wnd_close && w->nested_root->GetWidgetOfType(WWT_CLOSEBOX)) {
delete w;
} else if (_settings_client.gui.hover_delay_ms == 0 && wid->tool_tip != 0) {
GuiShowTooltips(w, wid->tool_tip, 0, NULL, TCC_RIGHT_CLICK);
}
}
/**
* Dispatch hover of the mouse over a window.
* @param w Window to dispatch event in.
* @param x X coordinate of the click.
* @param y Y coordinate of the click.
*/
static void DispatchHoverEvent(Window *w, int x, int y)
{
NWidgetCore *wid = w->nested_root->GetWidgetFromPos(x, y);
/* No widget to handle */
if (wid == NULL) return;
/* Show the tooltip if there is any */
if (wid->tool_tip != 0) {
GuiShowTooltips(w, wid->tool_tip);
return;
}
/* Widget has no index, so the window is not interested in it. */
if (wid->index < 0) return;
Point pt = { x, y };
w->OnHover(pt, wid->index);
}
/**
* Dispatch the mousewheel-action to the window.
* The window will scroll any compatible scrollbars if the mouse is pointed over the bar or its contents
* @param w Window
* @param nwid the widget where the scrollwheel was used
* @param wheel scroll up or down
*/
static void DispatchMouseWheelEvent(Window *w, NWidgetCore *nwid, int wheel)
{
if (nwid == NULL) return;
/* Using wheel on caption/shade-box shades or unshades the window. */
if (nwid->type == WWT_CAPTION || nwid->type == WWT_SHADEBOX) {
w->SetShaded(wheel < 0);
return;
}
/* Wheeling a vertical scrollbar. */
if (nwid->type == NWID_VSCROLLBAR) {
NWidgetScrollbar *sb = static_cast<NWidgetScrollbar *>(nwid);
if (sb->GetCount() > sb->GetCapacity()) {
sb->UpdatePosition(wheel);
w->SetDirty();
}
return;
}
/* Scroll the widget attached to the scrollbar. */
Scrollbar *sb = (nwid->scrollbar_index >= 0 ? w->GetScrollbar(nwid->scrollbar_index) : NULL);
if (sb != NULL && sb->GetCount() > sb->GetCapacity()) {
sb->UpdatePosition(wheel);
w->SetDirty();
}
}
/**
* Returns whether a window may be shown or not.
* @param w The window to consider.
* @return True iff it may be shown, otherwise false.
*/
static bool MayBeShown(const Window *w)
{
/* If we're not modal, everything is okay. */
if (!HasModalProgress()) return true;
switch (w->window_class) {
case WC_MAIN_WINDOW: ///< The background, i.e. the game.
case WC_MODAL_PROGRESS: ///< The actual progress window.
case WC_CONFIRM_POPUP_QUERY: ///< The abort window.
return true;
default:
return false;
}
}
/**
* Generate repaint events for the visible part of window w within the rectangle.
*
* The function goes recursively upwards in the window stack, and splits the rectangle
* into multiple pieces at the window edges, so obscured parts are not redrawn.
*
* @param w Window that needs to be repainted
* @param left Left edge of the rectangle that should be repainted
* @param top Top edge of the rectangle that should be repainted
* @param right Right edge of the rectangle that should be repainted
* @param bottom Bottom edge of the rectangle that should be repainted
*/
static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bottom)
{
const Window *v;
FOR_ALL_WINDOWS_FROM_BACK_FROM(v, w->z_front) {
if (MayBeShown(v) &&
right > v->left &&
bottom > v->top &&
left < v->left + v->width &&
top < v->top + v->height) {
/* v and rectangle intersect with each other */
int x;
if (left < (x = v->left)) {
DrawOverlappedWindow(w, left, top, x, bottom);
DrawOverlappedWindow(w, x, top, right, bottom);
return;
}
if (right > (x = v->left + v->width)) {
DrawOverlappedWindow(w, left, top, x, bottom);
DrawOverlappedWindow(w, x, top, right, bottom);
return;
}
if (top < (x = v->top)) {
DrawOverlappedWindow(w, left, top, right, x);
DrawOverlappedWindow(w, left, x, right, bottom);
return;
}
if (bottom > (x = v->top + v->height)) {
DrawOverlappedWindow(w, left, top, right, x);
DrawOverlappedWindow(w, left, x, right, bottom);
return;
}
return;
}
}
/* Setup blitter, and dispatch a repaint event to window *wz */
DrawPixelInfo *dp = _cur_dpi;
dp->width = right - left;
dp->height = bottom - top;
dp->left = left - w->left;
dp->top = top - w->top;
dp->pitch = _screen.pitch;
dp->dst_ptr = BlitterFactory::GetCurrentBlitter()->MoveTo(_screen.dst_ptr, left, top);
dp->zoom = ZOOM_LVL_NORMAL;
w->OnPaint();
}
/**
* From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
* These windows should be re-painted.
* @param left Left edge of the rectangle that should be repainted
* @param top Top edge of the rectangle that should be repainted
* @param right Right edge of the rectangle that should be repainted
* @param bottom Bottom edge of the rectangle that should be repainted
*/
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
{
Window *w;
DrawPixelInfo *old_dpi = _cur_dpi;
DrawPixelInfo bk;
_cur_dpi = &bk;
FOR_ALL_WINDOWS_FROM_BACK(w) {
if (MayBeShown(w) &&
right > w->left &&
bottom > w->top &&
left < w->left + w->width &&
top < w->top + w->height) {
/* Window w intersects with the rectangle => needs repaint */
DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height));
}
}
_cur_dpi = old_dpi;
}
/**
* Mark entire window as dirty (in need of re-paint)
* @ingroup dirty
*/
void Window::SetDirty() const
{
SetDirtyBlocks(this->left, this->top, this->left + this->width, this->top + this->height);
}
/**
* Re-initialize a window, and optionally change its size.
* @param rx Horizontal resize of the window.
* @param ry Vertical resize of the window.
* @note For just resizing the window, use #ResizeWindow instead.
*/
void Window::ReInit(int rx, int ry)
{
this->SetDirty(); // Mark whole current window as dirty.
/* Save current size. */
int window_width = this->width;
int window_height = this->height;
this->OnInit();
/* Re-initialize the window from the ground up. No need to change the nested_array, as all widgets stay where they are. */
this->nested_root->SetupSmallestSize(this, false);
this->nested_root->AssignSizePosition(ST_SMALLEST, 0, 0, this->nested_root->smallest_x, this->nested_root->smallest_y, _current_text_dir == TD_RTL);
this->width = this->nested_root->smallest_x;
this->height = this->nested_root->smallest_y;
this->resize.step_width = this->nested_root->resize_x;
this->resize.step_height = this->nested_root->resize_y;
/* Resize as close to the original size + requested resize as possible. */
window_width = max(window_width + rx, this->width);
window_height = max(window_height + ry, this->height);
int dx = (this->resize.step_width == 0) ? 0 : window_width - this->width;
int dy = (this->resize.step_height == 0) ? 0 : window_height - this->height;
/* dx and dy has to go by step.. calculate it.
* The cast to int is necessary else dx/dy are implicitly casted to unsigned int, which won't work. */
if (this->resize.step_width > 1) dx -= dx % (int)this->resize.step_width;
if (this->resize.step_height > 1) dy -= dy % (int)this->resize.step_height;