-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
window.cpp
1685 lines (1423 loc) · 46 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
/*
Copyright (C) 2007 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Implementation of window.hpp.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/window_private.hpp"
#include "config.hpp"
#include "cursor.hpp"
#include "display.hpp"
#include "events.hpp"
#include "floating_label.hpp"
#include "formula/callable.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "gui/auxiliary/typed_formula.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/event/distributor.hpp"
#include "gui/core/event/handler.hpp"
#include "gui/core/event/message.hpp"
#include "gui/core/log.hpp"
#include "gui/core/layout_exception.hpp"
#include "gui/core/point.hpp"
#include "gui/core/window_builder.hpp"
#include "gui/dialogs/title_screen.hpp"
#include "gui/dialogs/tooltip.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/container_base.hpp"
#include "gui/widgets/text_box_base.hpp"
#include "gui/core/register_widget.hpp"
#include "gui/widgets/grid.hpp"
#include "gui/widgets/helper.hpp"
#include "gui/widgets/panel.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/widget.hpp"
#include "gui/widgets/window.hpp"
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
#include "gui/widgets/debug.hpp"
#endif
#include "preferences/general.hpp"
#include "preferences/display.hpp"
#include "sdl/rect.hpp"
#include "sdl/surface.hpp"
#include "tstring.hpp"
#include "formula/variant.hpp"
#include "video.hpp"
#include "wml_exception.hpp"
#include "utils/functional.hpp"
namespace wfl { class function_symbol_table; }
namespace gui2 { class button; }
static lg::log_domain log_gui("gui/layout");
#define ERR_GUI LOG_STREAM(err, log_gui)
#define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
#define LOG_HEADER LOG_SCOPE_HEADER + ':'
#define LOG_IMPL_SCOPE_HEADER \
window.get_control_type() + " [" + window.id() + "] " + __func__
#define LOG_IMPL_HEADER LOG_IMPL_SCOPE_HEADER + ':'
namespace gui2
{
// ------------ WIDGET -----------{
namespace implementation
{
/** @todo See whether this hack can be removed. */
// Needed to fix a compiler error in REGISTER_WIDGET.
class builder_window : public builder_styled_widget
{
public:
builder_window(const config& cfg) : builder_styled_widget(cfg)
{
}
using builder_styled_widget::build;
widget* build() const
{
return nullptr;
}
};
} // namespace implementation
REGISTER_WIDGET(window)
namespace
{
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
const unsigned SHOW = debug_layout_graph::SHOW;
const unsigned LAYOUT = debug_layout_graph::LAYOUT;
#else
// values are irrelavant when DEBUG_WINDOW_LAYOUT_GRAPHS is not defined.
const unsigned SHOW = 0;
const unsigned LAYOUT = 0;
#endif
/**
* The interval between draw events.
*
* When the window is shown this value is set, the callback function always
* uses this value instead of the parameter send, that way the window can stop
* drawing when it wants.
*/
static int draw_interval = 0;
/**
* SDL_AddTimer() callback for the draw event.
*
* When this callback is called it pushes a new draw event in the event queue.
*
* @returns The new timer interval, 0 to stop.
*/
static Uint32 draw_timer(Uint32, void*)
{
// DBG_GUI_E << "Pushing draw event in queue.\n";
SDL_Event event;
SDL_UserEvent data;
data.type = DRAW_EVENT;
data.code = 0;
data.data1 = nullptr;
data.data2 = nullptr;
event.type = DRAW_EVENT;
event.user = data;
SDL_PushEvent(&event);
return draw_interval;
}
/**
* SDL_AddTimer() callback for delay_event.
*
* @param event The event to push in the event queue.
*
* @return The new timer interval (always 0).
*/
static Uint32 delay_event_callback(const Uint32, void* event)
{
SDL_PushEvent(static_cast<SDL_Event*>(event));
delete static_cast<SDL_Event*>(event);
return 0;
}
/**
* Allows an event to be delayed a certain amount of time.
*
* @note the delay is the minimum time, after the time has passed the event
* will be pushed in the SDL event queue, so it might delay more.
*
* @param event The event to delay.
* @param delay The number of ms to delay the event.
*/
static void delay_event(const SDL_Event& event, const Uint32 delay)
{
SDL_AddTimer(delay, delay_event_callback, new SDL_Event(event));
}
/**
* Adds a SHOW_HELPTIP event to the SDL event queue.
*
* The event is used to show the helptip for the currently focused widget.
*/
static bool helptip()
{
DBG_GUI_E << "Pushing SHOW_HELPTIP_EVENT event in queue.\n";
SDL_Event event;
SDL_UserEvent data;
data.type = SHOW_HELPTIP_EVENT;
data.code = 0;
data.data1 = nullptr;
data.data2 = nullptr;
event.type = SHOW_HELPTIP_EVENT;
event.user = data;
SDL_PushEvent(&event);
return true;
}
/**
* Small helper class to get an unique id for every window instance.
*
* This is used to send event to the proper window, this allows windows to post
* messages to themselves and let them delay for a certain amount of time.
*/
class manager
{
manager();
public:
static manager& instance();
void add(window& window);
void remove(window& window);
unsigned get_id(window& window);
window* get_window(const unsigned id);
private:
// The number of active window should be rather small
// so keep it simple and don't add a reverse lookup map.
std::map<unsigned, window*> windows_;
};
manager::manager() : windows_()
{
}
manager& manager::instance()
{
static manager window_manager;
return window_manager;
}
void manager::add(window& win)
{
static unsigned id;
++id;
windows_[id] = &win;
}
void manager::remove(window& win)
{
for(std::map<unsigned, window*>::iterator itor = windows_.begin();
itor != windows_.end();
++itor) {
if(itor->second == &win) {
windows_.erase(itor);
return;
}
}
assert(false);
}
unsigned manager::get_id(window& win)
{
for(std::map<unsigned, window*>::iterator itor = windows_.begin();
itor != windows_.end();
++itor) {
if(itor->second == &win) {
return itor->first;
}
}
assert(false);
return 0;
}
window* manager::get_window(const unsigned id)
{
std::map<unsigned, window*>::iterator itor = windows_.find(id);
if(itor == windows_.end()) {
return nullptr;
} else {
return itor->second;
}
}
} // namespace
window::window(CVideo& video, const builder_window::window_resolution* definition)
: panel(implementation::builder_window(::config {"definition", definition->definition}), get_control_type())
, cursor::setter(cursor::NORMAL)
, video_(video)
, status_(NEW)
, show_mode_(none)
, retval_(NONE)
, owner_(nullptr)
, need_layout_(true)
, variables_()
, invalidate_layout_blocked_(false)
, suspend_drawing_(true)
, restore_(true)
, is_toplevel_(!is_in_dialog())
, restorer_()
, automatic_placement_(definition->automatic_placement)
, horizontal_placement_(definition->horizontal_placement)
, vertical_placement_(definition->vertical_placement)
, maximum_width_(definition->maximum_width)
, maximum_height_(definition->maximum_height)
, x_(definition->x)
, y_(definition->y)
, w_(definition->width)
, h_(definition->height)
, reevaluate_best_size_(definition->reevaluate_best_size)
, functions_(definition->functions)
, tooltip_(definition->tooltip)
, helptip_(definition->helptip)
, click_dismiss_(false)
, enter_disabled_(false)
, escape_disabled_(false)
, linked_size_()
, mouse_button_state_(0) /**< Needs to be initialized in @ref show. */
, dirty_list_()
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
, debug_layout_(new debug_layout_graph(this))
#endif
, event_distributor_(new event::distributor(*this, event::dispatcher::front_child))
, exit_hook_([](window&)->bool { return true; })
, callback_next_draw_(nullptr)
{
manager::instance().add(*this);
connect();
if (!video.faked())
{
connect_signal<event::DRAW>(std::bind(&window::draw, this));
}
connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(
&window::signal_handler_sdl_video_resize, this, _2, _3, _5));
connect_signal<event::SDL_ACTIVATE>(std::bind(
&event::distributor::initialize_state, event_distributor_.get()));
connect_signal<event::SDL_LEFT_BUTTON_UP>(
std::bind(&window::signal_handler_click_dismiss,
this,
_2,
_3,
_4,
SDL_BUTTON_LMASK),
event::dispatcher::front_child);
connect_signal<event::SDL_MIDDLE_BUTTON_UP>(
std::bind(&window::signal_handler_click_dismiss,
this,
_2,
_3,
_4,
SDL_BUTTON_MMASK),
event::dispatcher::front_child);
connect_signal<event::SDL_RIGHT_BUTTON_UP>(
std::bind(&window::signal_handler_click_dismiss,
this,
_2,
_3,
_4,
SDL_BUTTON_RMASK),
event::dispatcher::front_child);
connect_signal<event::SDL_KEY_DOWN>(
std::bind(
&window::signal_handler_sdl_key_down, this, _2, _3, _5, _6, true),
event::dispatcher::back_post_child);
connect_signal<event::SDL_KEY_DOWN>(std::bind(
&window::signal_handler_sdl_key_down, this, _2, _3, _5, _6, false));
connect_signal<event::MESSAGE_SHOW_TOOLTIP>(
std::bind(&window::signal_handler_message_show_tooltip,
this,
_2,
_3,
_5),
event::dispatcher::back_pre_child);
connect_signal<event::MESSAGE_SHOW_HELPTIP>(
std::bind(&window::signal_handler_message_show_helptip,
this,
_2,
_3,
_5),
event::dispatcher::back_pre_child);
connect_signal<event::REQUEST_PLACEMENT>(
std::bind(
&window::signal_handler_request_placement, this, _2, _3),
event::dispatcher::back_pre_child);
register_hotkey(hotkey::GLOBAL__HELPTIP, std::bind(gui2::helptip));
}
window::~window()
{
/*
* We need to delete our children here instead of waiting for the grid to
* automatically do it. The reason is when the grid deletes its children
* they will try to unregister them self from the linked widget list. At
* this point the member of window are destroyed and we enter UB. (For
* some reason the bug didn't trigger on g++ but it does on MSVC.
*/
for(unsigned row = 0; row < get_grid().get_rows(); ++row) {
for(unsigned col = 0; col < get_grid().get_cols(); ++col) {
get_grid().remove_child(row, col);
}
}
/*
* The tip needs to be closed if the window closes and the window is
* not a tip. If we don't do that the tip will unrender in the next
* window and cause drawing glitches.
* Another issue is that on smallgui and an MP game the tooltip not
* unrendered properly can capture the mouse and make playing impossible.
*/
if(show_mode_ == modal) {
dialogs::tip::remove();
}
manager::instance().remove(*this);
#ifdef DEBUG_WINDOW_LAYOUT_GRAPHS
delete debug_layout_;
#endif
}
window* window::window_instance(const unsigned handle)
{
return manager::instance().get_window(handle);
}
void window::update_screen_size()
{
// Only if we're the toplevel window we need to update the size, otherwise
// it's done in the resize event.
if(draw_interval == 0) {
const SDL_Rect rect = screen_area();
settings::screen_width = rect.w;
settings::screen_height = rect.h;
settings::gamemap_width = settings::screen_width;
settings::gamemap_height = settings::screen_height;
display* display = display::get_singleton();
if(display) {
const SDL_Rect rect_gm = display->map_outside_area();
if(rect_gm.w && rect_gm.h) {
settings::gamemap_width = rect_gm.w;
settings::gamemap_height = rect_gm.h;
settings::gamemap_x_offset = rect_gm.x;
}
}
}
}
/*WIKI
* @page = GUIToolkitWML
* @order = 3_widget_window_2
*
* List if the id's that have generate a return value:
* * ok confirms the dialog.
* * cancel cancels the dialog.
*
*/
window::retval window::get_retval_by_id(const std::string& id)
{
// Note it might change to a map later depending on the number
// of items.
if(id == "ok") {
return OK;
} else if(id == "cancel" || id == "quit") {
return CANCEL;
} else {
return NONE;
}
}
void window::show_tooltip(/*const unsigned auto_close_timeout*/)
{
log_scope2(log_gui_draw, "Window: show as tooltip.");
generate_dot_file("show", SHOW);
assert(status_ == NEW);
set_mouse_behavior(event::dispatcher::none);
set_want_keyboard_input(false);
show_mode_ = tooltip;
/*
* Before show has been called, some functions might have done some testing
* on the window and called layout, which can give glitches. So
* reinvalidate the window to avoid those glitches.
*/
invalidate_layout();
suspend_drawing_ = false;
}
void window::show_non_modal(/*const unsigned auto_close_timeout*/)
{
log_scope2(log_gui_draw, "Window: show non modal.");
generate_dot_file("show", SHOW);
assert(status_ == NEW);
set_mouse_behavior(event::dispatcher::hit);
show_mode_ = modeless;
/*
* Before show has been called, some functions might have done some testing
* on the window and called layout, which can give glitches. So
* reinvalidate the window to avoid those glitches.
*/
invalidate_layout();
suspend_drawing_ = false;
events::pump();
}
int window::show(const bool restore, const unsigned auto_close_timeout)
{
/*
* Removes the old tip if one shown. The show_tip doesn't remove
* the tip, since it's the tip.
*/
dialogs::tip::remove();
show_mode_ = modal;
restore_ = restore;
/**
* Helper class to set and restore the drawing interval.
*
* We need to make sure we restore the value when the function ends, be it
* normally or due to an exception.
*/
class draw_interval_setter
{
public:
draw_interval_setter() : interval_(draw_interval)
{
if(interval_ == 0) {
draw_interval = 20;
SDL_AddTimer(draw_interval, draw_timer, nullptr);
// There might be some time between creation and showing so
// reupdate the sizes.
update_screen_size();
}
}
~draw_interval_setter()
{
draw_interval = interval_;
}
private:
int interval_;
};
log_scope2(log_gui_draw, LOG_SCOPE_HEADER);
generate_dot_file("show", SHOW);
assert(status_ == NEW);
draw_interval_setter draw_interval_setter;
/*
* Before show has been called, some functions might have done some testing
* on the window and called layout, which can give glitches. So
* reinvalidate the window to avoid those glitches.
*/
invalidate_layout();
suspend_drawing_ = false;
if(auto_close_timeout) {
// Make sure we're drawn before we try to close ourselves, which can
// happen if the timeout is small.
draw();
SDL_Event event;
SDL_UserEvent data;
data.type = CLOSE_WINDOW_EVENT;
data.code = manager::instance().get_id(*this);
data.data1 = nullptr;
data.data2 = nullptr;
event.type = CLOSE_WINDOW_EVENT;
event.user = data;
delay_event(event, auto_close_timeout);
}
try
{
// Start our loop drawing will happen here as well.
bool mouse_button_state_initialized = false;
for(status_ = SHOWING; status_ != CLOSED;) {
// process installed callback if valid, to allow e.g. network
// polling
events::pump();
if(!mouse_button_state_initialized) {
/*
* The state must be initialize when showing the dialogue.
* However when initialized before this point there were random
* errors. This only happened when the 'click' was done fast; a
* slower click worked properly.
*
* So it seems the events need to be processed before SDL can
* return the proper button state. When initializing here all
* works fine.
*/
mouse_button_state_ = SDL_GetMouseState(nullptr, nullptr);
mouse_button_state_initialized = true;
}
if(status_ == REQUEST_CLOSE) {
status_ = exit_hook_(*this) ? CLOSED : SHOWING;
}
// Add a delay so we don't keep spinning if there's no event.
SDL_Delay(10);
}
}
catch(...)
{
/**
* @todo Clean up the code duplication.
*
* In the future the restoring shouldn't be needed so the duplication
* doesn't hurt too much but keep this todo as a reminder.
*/
suspend_drawing_ = true;
// restore area
if(restore_) {
SDL_Rect rect = get_rectangle();
sdl_blit(restorer_, 0, video_.getSurface(), &rect);
font::undraw_floating_labels(video_.getSurface());
}
throw;
}
suspend_drawing_ = true;
// restore area
if(restore_) {
SDL_Rect rect = get_rectangle();
sdl_blit(restorer_, 0, video_.getSurface(), &rect);
font::undraw_floating_labels(video_.getSurface());
}
if(text_box_base* tb = dynamic_cast<text_box_base*>(event_distributor_->keyboard_focus())) {
tb->interrupt_composition();
}
return retval_;
}
void window::draw()
{
/***** ***** ***** ***** Init ***** ***** ***** *****/
// Prohibited from drawing?
if(suspend_drawing_) {
return;
}
surface& frame_buffer = video_.getSurface();
/***** ***** Layout and get dirty list ***** *****/
if(need_layout_) {
// Restore old surface. In the future this phase will not be needed
// since all will be redrawn when needed with dirty rects. Since that
// doesn't work yet we need to undraw the window.
if(restore_ && restorer_) {
SDL_Rect rect = get_rectangle();
sdl_blit(restorer_, 0, frame_buffer, &rect);
}
layout();
// Get new surface for restoring
SDL_Rect rect = get_rectangle();
// We want the labels underneath the window so draw them and use them
// as restore point.
if(is_toplevel_) {
font::draw_floating_labels(frame_buffer);
}
if(restore_) {
restorer_ = get_surface_portion(frame_buffer, rect);
}
// Need full redraw so only set ourselves dirty.
dirty_list_.emplace_back(1, this);
} else {
// Let widgets update themselves, which might dirty some things.
layout_children();
// Now find the widgets that are dirty.
std::vector<widget*> call_stack;
if(!new_widgets) {
populate_dirty_list(*this, call_stack);
} else {
/* Force to update and redraw the entire screen */
dirty_list_.clear();
dirty_list_.emplace_back(1, this);
}
}
if (dirty_list_.empty()) {
return;
}
for(auto & item : dirty_list_)
{
assert(!item.empty());
const SDL_Rect dirty_rect
= new_widgets ? screen_area()
: item.back()->get_dirty_rectangle();
// For testing we disable the clipping rect and force the entire screen to
// update. This way an item rendered at the wrong place is directly visible.
#if 0
dirty_list_.clear();
dirty_list_.emplace_back(1, this);
#else
clip_rect_setter clip(frame_buffer, &dirty_rect);
#endif
/*
* The actual update routine does the following:
* - Restore the background.
*
* - draw [begin, end) the back ground of all widgets.
*
* - draw the children of the last item in the list, if this item is
* a container it's children get a full redraw. If it's not a
* container nothing happens.
*
* - draw [rbegin, rend) the fore ground of all widgets. For items
* which have two layers eg window or panel it draws the foreground
* layer. For other widgets it's a nop.
*
* Before drawing there needs to be determined whether a dirty widget
* really needs to be redrawn. If the widget doesn't need to be
* redrawing either being not visibility::visible or has status
* widget::redraw_action::none. If it's not drawn it's still set not
* dirty to avoid it keep getting on the dirty list.
*/
for(std::vector<widget*>::iterator itor = item.begin();
itor != item.end();
++itor) {
if((**itor).get_visible() != widget::visibility::visible
|| (**itor).get_drawing_action()
== widget::redraw_action::none) {
for(std::vector<widget*>::iterator citor = itor;
citor != item.end();
++citor) {
(**citor).set_is_dirty(false);
}
item.erase(itor, item.end());
break;
}
}
// Restore.
if(restore_) {
SDL_Rect rect = get_rectangle();
sdl_blit(restorer_, 0, frame_buffer, &rect);
}
// Background.
for(std::vector<widget*>::iterator itor = item.begin();
itor != item.end();
++itor) {
(**itor).draw_background(frame_buffer, 0, 0);
}
// Children.
if(!item.empty()) {
item.back()->draw_children(frame_buffer, 0, 0);
}
// Foreground.
for(std::vector<widget*>::reverse_iterator ritor = item.rbegin();
ritor != item.rend();
++ritor) {
(**ritor).draw_foreground(frame_buffer, 0, 0);
(**ritor).set_is_dirty(false);
}
}
dirty_list_.clear();
std::vector<widget*> call_stack;
populate_dirty_list(*this, call_stack);
assert(dirty_list_.empty());
if(callback_next_draw_ != nullptr) {
callback_next_draw_();
callback_next_draw_ = nullptr;
}
}
void window::undraw()
{
if(restore_ && restorer_) {
SDL_Rect rect = get_rectangle();
sdl_blit(restorer_, 0, video_.getSurface(), &rect);
// Since the old area might be bigger as the new one, invalidate
// it.
}
}
window::invalidate_layout_blocker::invalidate_layout_blocker(window& window)
: window_(window)
{
assert(!window_.invalidate_layout_blocked_);
window_.invalidate_layout_blocked_ = true;
}
window::invalidate_layout_blocker::~invalidate_layout_blocker()
{
assert(window_.invalidate_layout_blocked_);
window_.invalidate_layout_blocked_ = false;
}
void window::invalidate_layout()
{
if(!invalidate_layout_blocked_) {
need_layout_ = true;
}
}
widget* window::find_at(const point& coordinate, const bool must_be_active)
{
return panel::find_at(coordinate, must_be_active);
}
const widget* window::find_at(const point& coordinate,
const bool must_be_active) const
{
return panel::find_at(coordinate, must_be_active);
}
widget* window::find(const std::string& id, const bool must_be_active)
{
return container_base::find(id, must_be_active);
}
const widget* window::find(const std::string& id, const bool must_be_active)
const
{
return container_base::find(id, must_be_active);
}
void window::init_linked_size_group(const std::string& id,
const bool fixed_width,
const bool fixed_height)
{
assert(fixed_width || fixed_height);
assert(!has_linked_size_group(id));
linked_size_[id] = linked_size(fixed_width, fixed_height);
}
bool window::has_linked_size_group(const std::string& id)
{
return linked_size_.find(id) != linked_size_.end();
}
void window::add_linked_widget(const std::string& id, widget* wgt)
{
assert(wgt);
if(!has_linked_size_group(id)) {
ERR_GUI << "Unknown linked group '" << id << "'; skipping\n";
return;
}
std::vector<widget*>& widgets = linked_size_[id].widgets;
if(std::find(widgets.begin(), widgets.end(), wgt) == widgets.end()) {
widgets.push_back(wgt);
}
}
void window::remove_linked_widget(const std::string& id, const widget* wgt)
{
assert(wgt);
if(!has_linked_size_group(id)) {
return;
}
std::vector<widget*>& widgets = linked_size_[id].widgets;
std::vector<widget*>::iterator itor
= std::find(widgets.begin(), widgets.end(), wgt);
if(itor != widgets.end()) {
widgets.erase(itor);
assert(std::find(widgets.begin(), widgets.end(), wgt)
== widgets.end());
}
}
void window::layout()
{
/***** Initialize. *****/
const auto conf = cast_config_to<window_definition>();
assert(conf);
log_scope2(log_gui_layout, LOG_SCOPE_HEADER);
const point mouse = get_mouse_position();
variables_.add("mouse_x", wfl::variant(mouse.x));
variables_.add("mouse_y", wfl::variant(mouse.y));
variables_.add("window_width", wfl::variant(0));
variables_.add("window_height", wfl::variant(0));
variables_.add("size_request_mode", wfl::variant("maximum"));
get_screen_size_variables(variables_);
const int maximum_width = automatic_placement_ ? maximum_width_
? std::min(maximum_width_, settings::screen_width)
: settings::screen_width
: w_(variables_, &functions_);
const int maximum_height = automatic_placement_ ? maximum_height_
? std::min(maximum_height_, settings::screen_height)
: settings::screen_height
: h_(variables_, &functions_);
/***** Handle click dismiss status. *****/
button* click_dismiss_button = nullptr;
if((click_dismiss_button
= find_widget<button>(this, "click_dismiss", false, false))) {
click_dismiss_button->set_visible(widget::visibility::invisible);
}
if(click_dismiss_) {
button* btn = find_widget<button>(this, "ok", false, false);
if(btn) {
btn->set_visible(widget::visibility::invisible);
click_dismiss_button = btn;
}
VALIDATE(click_dismiss_button,
_("Click dismiss needs a 'click_dismiss' or 'ok' button."));
}
/***** Layout. *****/
layout_initialize(true);
generate_dot_file("layout_initialize", LAYOUT);
layout_linked_widgets();
try
{
window_implementation::layout(*this, maximum_width, maximum_height);
}
catch(layout_exception_resize_failed&)
{
/** @todo implement the scrollbars on the window. */
std::stringstream sstr;
sstr << __FILE__ << ":" << __LINE__ << " in function '" << __func__
<< "' found the following problem: Failed to size window;"
<< " wanted size " << get_best_size() << " available size "
<< maximum_width << ',' << maximum_height << " screen size "