-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
distributor.cpp
809 lines (675 loc) · 21.2 KB
/
distributor.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
/*
Copyright (C) 2009 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/core/event/distributor.hpp"
#include "events.hpp"
#include "gui/core/log.hpp"
#include "gui/core/timer.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/widget.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/text_box_base.hpp"
#include "sdl/userevent.hpp"
#include "utils/functional.hpp"
namespace gui2
{
namespace event
{
/**
* SDL_AddTimer() callback for the hover event.
*
* When this callback is called it pushes a new hover event in the event queue.
*
* @param interval The time parameter of SDL_AddTimer.
* @param param Pointer to a widget that's able to show the
* tooltip (will be used as a dispatcher).
*
* @returns The new timer interval, 0 to stop.
*/
#if 0
/**
* SDL_AddTimer() callback for the popup event.
*
* This event makes sure the popup is removed again.
*
* @param interval The time parameter of SDL_AddTimer.
* @param param Pointer to parameter structure.
*
* @returns The new timer interval, 0 to stop.
*/
static uint32_t popup_callback(uint32_t /*interval*/, void* /*param*/)
{
DBG_GUI_E << "Pushing popup removal event in queue.\n";
SDL_Event event;
sdl::UserEvent data(HOVER_REMOVE_POPUP_EVENT);
event.type = HOVER_REMOVE_POPUP_EVENT;
event.user = data;
SDL_PushEvent(&event);
return 0;
}
#endif
/**
* Small helper to keep a resource (boolean) locked.
*
* Some of the event handling routines can't be called recursively, this due to
* the fact that they are attached to the pre queue and when the forward an
* event the pre queue event gets triggered recursively causing infinite
* recursion.
*
* To prevent that those functions check the lock and exit when the lock is
* held otherwise grab the lock here.
*/
class resource_locker
{
public:
resource_locker(bool& locked) : locked_(locked)
{
assert(!locked_);
locked_ = true;
}
~resource_locker()
{
assert(locked_);
locked_ = false;
}
private:
bool& locked_;
};
/***** ***** ***** ***** mouse_motion ***** ***** ***** ***** *****/
#define LOG_HEADER "distributor mouse motion [" << owner_.id() << "]: "
mouse_motion::mouse_motion(widget& owner,
const dispatcher::queue_position queue_position)
: mouse_focus_(nullptr)
, mouse_captured_(false)
, owner_(owner)
, hover_timer_(0)
, hover_widget_(nullptr)
, hover_position_(0, 0)
, hover_shown_(true)
, signal_handler_sdl_mouse_motion_entered_(false)
{
owner.connect_signal<event::SDL_MOUSE_MOTION>(
std::bind(&mouse_motion::signal_handler_sdl_mouse_motion,
this,
_2,
_3,
_5),
queue_position);
owner.connect_signal<event::SDL_TOUCH_MOTION>(
std::bind(&mouse_motion::signal_handler_sdl_touch_motion,
this,
_2,
_3,
_5,
_6),
queue_position);
owner_.connect_signal<event::SDL_WHEEL_UP>(std::bind(
&mouse_motion::signal_handler_sdl_wheel, this, _2, _3, _5));
owner_.connect_signal<event::SDL_WHEEL_DOWN>(std::bind(
&mouse_motion::signal_handler_sdl_wheel, this, _2, _3, _5));
owner_.connect_signal<event::SDL_WHEEL_LEFT>(std::bind(
&mouse_motion::signal_handler_sdl_wheel, this, _2, _3, _5));
owner_.connect_signal<event::SDL_WHEEL_RIGHT>(std::bind(
&mouse_motion::signal_handler_sdl_wheel, this, _2, _3, _5));
owner.connect_signal<event::SHOW_HELPTIP>(
std::bind(&mouse_motion::signal_handler_show_helptip,
this,
_2,
_3,
_5),
queue_position);
}
mouse_motion::~mouse_motion()
{
stop_hover_timer();
}
void mouse_motion::capture_mouse(const bool capture)
{
assert(mouse_focus_);
mouse_captured_ = capture;
}
void mouse_motion::signal_handler_sdl_mouse_motion(const event::ui_event event,
bool& handled,
const point& coordinate)
{
if(signal_handler_sdl_mouse_motion_entered_) {
return;
}
resource_locker lock(signal_handler_sdl_mouse_motion_entered_);
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(mouse_captured_) {
assert(mouse_focus_);
if(!owner_.fire(event, *mouse_focus_, coordinate)) {
mouse_hover(mouse_focus_, coordinate);
}
} else {
widget* mouse_over = owner_.find_at(coordinate, true);
while(mouse_over && !mouse_over->can_mouse_focus() && mouse_over->parent()) {
mouse_over = mouse_over->parent();
}
if(mouse_over) {
DBG_GUI_E << LOG_HEADER << "Firing: " << event << ".\n";
if(owner_.fire(event, *mouse_over, coordinate)) {
return;
}
}
if(!mouse_focus_ && mouse_over) {
mouse_enter(mouse_over);
} else if(mouse_focus_ && !mouse_over) {
mouse_leave();
} else if(mouse_focus_ && mouse_focus_ == mouse_over) {
mouse_hover(mouse_over, coordinate);
} else if(mouse_focus_ && mouse_over) {
// moved from one widget to the next
mouse_leave();
mouse_enter(mouse_over);
} else {
assert(!mouse_focus_ && !mouse_over);
}
}
handled = true;
}
void mouse_motion::signal_handler_sdl_touch_motion(const event::ui_event event,
bool& handled,
const point& coordinate,
const point& distance)
{
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(mouse_captured_) {
assert(mouse_focus_);
owner_.fire(event, *mouse_focus_, coordinate, distance);
} else {
widget* mouse_over = owner_.find_at(coordinate, true);
if(mouse_over) {
owner_.fire(event, *mouse_over, coordinate, distance);
}
}
handled = true;
}
void mouse_motion::signal_handler_sdl_wheel(const event::ui_event event,
bool& handled,
const point& coordinate)
{
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(mouse_captured_) {
assert(mouse_focus_);
owner_.fire(event, *mouse_focus_, coordinate);
} else {
widget* mouse_over = owner_.find_at(coordinate, true);
if(mouse_over) {
owner_.fire(event, *mouse_over, coordinate);
}
}
handled = true;
}
void mouse_motion::signal_handler_show_helptip(const event::ui_event event,
bool& handled,
const point& coordinate)
{
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(mouse_captured_) {
assert(mouse_focus_);
if(owner_.fire(event, *mouse_focus_, coordinate)) {
stop_hover_timer();
}
} else {
widget* mouse_over = owner_.find_at(coordinate, true);
if(mouse_over) {
DBG_GUI_E << LOG_HEADER << "Firing: " << event << ".\n";
if(owner_.fire(event, *mouse_over, coordinate)) {
stop_hover_timer();
}
}
}
handled = true;
}
void mouse_motion::mouse_enter(widget* mouse_over)
{
DBG_GUI_E << LOG_HEADER << "Firing: " << event::MOUSE_ENTER << ".\n";
assert(mouse_over);
mouse_focus_ = mouse_over;
owner_.fire(event::MOUSE_ENTER, *mouse_over);
hover_shown_ = false;
start_hover_timer(mouse_over, get_mouse_position());
}
void mouse_motion::mouse_hover(widget* mouse_over, const point& coordinate)
{
DBG_GUI_E << LOG_HEADER << "Firing: " << event::MOUSE_MOTION << ".\n";
assert(mouse_over);
owner_.fire(event::MOUSE_MOTION, *mouse_over, coordinate);
if(hover_timer_) {
if((std::abs(hover_position_.x - coordinate.x) > 5)
|| (std::abs(hover_position_.y - coordinate.y) > 5)) {
stop_hover_timer();
start_hover_timer(mouse_over, coordinate);
}
}
}
void mouse_motion::show_tooltip()
{
DBG_GUI_E << LOG_HEADER << "Firing: " << event::SHOW_TOOLTIP << ".\n";
if(!hover_widget_) {
// See mouse_motion::stop_hover_timer.
ERR_GUI_E << LOG_HEADER << event::SHOW_TOOLTIP
<< " bailing out, no hover widget.\n";
return;
}
/*
* Ignore the result of the event, always mark the tooltip as shown. If
* there was no handler, there is no reason to assume there will be one
* next time.
*/
owner_.fire(SHOW_TOOLTIP, *hover_widget_, hover_position_);
hover_shown_ = true;
hover_timer_ = 0;
hover_widget_ = nullptr;
hover_position_ = point();
}
void mouse_motion::mouse_leave()
{
DBG_GUI_E << LOG_HEADER << "Firing: " << event::MOUSE_LEAVE << ".\n";
styled_widget* control = dynamic_cast<styled_widget*>(mouse_focus_);
if(!control || control->get_active()) {
owner_.fire(event::MOUSE_LEAVE, *mouse_focus_);
}
owner_.fire(NOTIFY_REMOVE_TOOLTIP, *mouse_focus_, nullptr);
mouse_focus_ = nullptr;
stop_hover_timer();
}
void mouse_motion::start_hover_timer(widget* widget, const point& coordinate)
{
assert(widget);
stop_hover_timer();
if(hover_shown_ || !widget->wants_mouse_hover()) {
return;
}
DBG_GUI_E << LOG_HEADER << "Start hover timer for widget '" << widget->id()
<< "' at address " << widget << ".\n";
hover_timer_
= add_timer(50, std::bind(&mouse_motion::show_tooltip, this));
if(hover_timer_) {
hover_widget_ = widget;
hover_position_ = coordinate;
} else {
ERR_GUI_E << LOG_HEADER << "Failed to add hover timer." << std::endl;
}
}
void mouse_motion::stop_hover_timer()
{
if(hover_timer_) {
assert(hover_widget_);
DBG_GUI_E << LOG_HEADER << "Stop hover timer for widget '"
<< hover_widget_->id() << "' at address " << hover_widget_
<< ".\n";
if(!remove_timer(hover_timer_)) {
ERR_GUI_E << LOG_HEADER << "Failed to remove hover timer."
<< std::endl;
}
hover_timer_ = 0;
hover_widget_ = nullptr;
hover_position_ = point();
}
}
/***** ***** ***** ***** mouse_button ***** ***** ***** ***** *****/
#undef LOG_HEADER
#define LOG_HEADER \
"distributor mouse button " << name_ << " [" << owner_.id() << "]: "
template<typename T>
mouse_button<T>::mouse_button(const std::string& name_, widget& owner,
const dispatcher::queue_position queue_position)
: mouse_motion(owner, queue_position)
, last_click_stamp_(0)
, last_clicked_widget_(nullptr)
, focus_(nullptr)
, name_(name_)
, is_down_(false)
, signal_handler_sdl_button_down_entered_(false)
, signal_handler_sdl_button_up_entered_(false)
{
owner_.connect_signal<T::sdl_button_down_event>(
std::bind(&mouse_button<T>::signal_handler_sdl_button_down,
this,
_2,
_3,
_5),
queue_position);
owner_.connect_signal<T::sdl_button_up_event>(
std::bind(&mouse_button<T>::signal_handler_sdl_button_up,
this,
_2,
_3,
_5),
queue_position);
}
template<typename T>
void mouse_button<T>::initialize_state(const bool is_down)
{
last_click_stamp_ = 0;
last_clicked_widget_ = nullptr;
focus_ = 0;
is_down_ = is_down;
}
template<typename T>
void mouse_button<T>::signal_handler_sdl_button_down(const event::ui_event event, bool& handled,
const point& coordinate)
{
if(signal_handler_sdl_button_down_entered_) {
return;
}
resource_locker lock(signal_handler_sdl_button_down_entered_);
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(is_down_) {
#ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
WRN_GUI_E << LOG_HEADER << event
<< ". The mouse button is already down, "
<< "we missed an event.\n";
#endif
return;
}
is_down_ = true;
if(mouse_captured_) {
assert(mouse_focus_);
focus_ = mouse_focus_;
DBG_GUI_E << LOG_HEADER << "Firing: " << T::sdl_button_down_event << ".\n";
if(!owner_.fire(T::sdl_button_down_event, *focus_, coordinate)) {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::button_down_event << ".\n";
owner_.fire(T::button_down_event, *mouse_focus_);
}
} else {
widget* mouse_over = owner_.find_at(coordinate, true);
if(!mouse_over) {
return;
}
if(mouse_over != mouse_focus_) {
#ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
WRN_GUI_E << LOG_HEADER << ". Mouse down on non focused widget "
<< "and mouse not captured, we missed events.\n";
#endif
mouse_focus_ = mouse_over;
}
focus_ = mouse_over;
DBG_GUI_E << LOG_HEADER << "Firing: " << T::sdl_button_down_event << ".\n";
if(!owner_.fire(T::sdl_button_down_event, *focus_, coordinate)) {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::button_down_event << ".\n";
owner_.fire(T::button_down_event, *focus_);
}
}
handled = true;
}
template<typename T>
void mouse_button<T>::signal_handler_sdl_button_up(const event::ui_event event, bool& handled,
const point& coordinate)
{
if(signal_handler_sdl_button_up_entered_) {
return;
}
resource_locker lock(signal_handler_sdl_button_up_entered_);
DBG_GUI_E << LOG_HEADER << event << ".\n";
if(!is_down_) {
WRN_GUI_E << LOG_HEADER << event
<< ". The mouse button is already up, we missed an event.\n";
return;
}
is_down_ = false;
if(focus_) {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::sdl_button_up_event << ".\n";
if(!owner_.fire(T::sdl_button_up_event, *focus_, coordinate)) {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::button_up_event << ".\n";
owner_.fire(T::button_up_event, *focus_);
}
}
widget* mouse_over = owner_.find_at(coordinate, true);
if(mouse_captured_) {
const unsigned mask = SDL_BUTTON_LMASK | SDL_BUTTON_MMASK
| SDL_BUTTON_RMASK;
if((SDL_GetMouseState(nullptr, nullptr) & mask) == 0) {
mouse_captured_ = false;
}
if(mouse_focus_ == mouse_over) {
mouse_button_click(mouse_focus_);
} else if(!mouse_captured_) {
mouse_leave();
if(mouse_over) {
mouse_enter(mouse_over);
}
}
} else if(focus_ && focus_ == mouse_over) {
mouse_button_click(focus_);
}
focus_ = nullptr;
handled = true;
}
template<typename T>
void mouse_button<T>::mouse_button_click(widget* widget)
{
uint32_t stamp = SDL_GetTicks();
if(last_click_stamp_ + settings::double_click_time >= stamp
&& last_clicked_widget_ == widget) {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::button_double_click_event << ".\n";
owner_.fire(T::button_double_click_event, *widget);
last_click_stamp_ = 0;
last_clicked_widget_ = nullptr;
} else {
DBG_GUI_E << LOG_HEADER << "Firing: " << T::button_click_event << ".\n";
owner_.fire(T::button_click_event, *widget);
last_click_stamp_ = stamp;
last_clicked_widget_ = widget;
}
}
/***** ***** ***** ***** distributor ***** ***** ***** ***** *****/
#undef LOG_HEADER
#define LOG_HEADER "distributor mouse motion [" << owner_.id() << "]: "
/**
* @todo Test whether the state is properly tracked when an input blocker is
* used.
*/
distributor::distributor(widget& owner,
const dispatcher::queue_position queue_position)
: mouse_motion(owner, queue_position)
, mouse_button_left("left", owner, queue_position)
, mouse_button_middle("middle", owner, queue_position)
, mouse_button_right("right", owner, queue_position)
#if 0
, hover_pending_(false)
, hover_id_(0)
, hover_box_()
, had_hover_(false)
, tooltip_(nullptr)
, help_popup_(nullptr)
#endif
, keyboard_focus_(nullptr)
, keyboard_focus_chain_()
{
if(SDL_WasInit(SDL_INIT_TIMER) == 0) {
if(SDL_InitSubSystem(SDL_INIT_TIMER) == -1) {
assert(false);
}
}
owner_.connect_signal<event::SDL_KEY_DOWN>(std::bind(
&distributor::signal_handler_sdl_key_down, this, _5, _6, _7));
owner_.connect_signal<event::SDL_TEXT_INPUT>(std::bind(
&distributor::signal_handler_sdl_text_input, this, _5, _6, _7));
owner_.connect_signal<event::SDL_TEXT_EDITING>(std::bind(
&distributor::signal_handler_sdl_text_editing, this, _5, _6, _7));
owner_.connect_signal<event::NOTIFY_REMOVAL>(std::bind(
&distributor::signal_handler_notify_removal, this, _1, _2));
initialize_state();
}
distributor::~distributor()
{
owner_.disconnect_signal<event::SDL_KEY_DOWN>(std::bind(
&distributor::signal_handler_sdl_key_down, this, _5, _6, _7));
owner_.disconnect_signal<event::SDL_TEXT_INPUT>(std::bind(
&distributor::signal_handler_sdl_text_input, this, _5, _6, _7));
owner_.disconnect_signal<event::SDL_TEXT_EDITING>(std::bind(
&distributor::signal_handler_sdl_text_editing, this, _5, _6, _7));
owner_.disconnect_signal<event::NOTIFY_REMOVAL>(std::bind(
&distributor::signal_handler_notify_removal, this, _1, _2));
}
void distributor::initialize_state()
{
const uint8_t button_state = SDL_GetMouseState(nullptr, nullptr);
mouse_button_left::initialize_state((button_state & SDL_BUTTON(1)) != 0);
mouse_button_middle::initialize_state((button_state & SDL_BUTTON(2)) != 0);
mouse_button_right::initialize_state((button_state & SDL_BUTTON(3)) != 0);
init_mouse_location();
}
widget* distributor::keyboard_focus() const
{
return keyboard_focus_;
}
void distributor::keyboard_capture(widget* widget)
{
if(keyboard_focus_) {
DBG_GUI_E << LOG_HEADER << "Firing: " << event::LOSE_KEYBOARD_FOCUS
<< ".\n";
owner_.fire(event::LOSE_KEYBOARD_FOCUS, *keyboard_focus_, nullptr);
}
keyboard_focus_ = widget;
if(keyboard_focus_) {
DBG_GUI_E << LOG_HEADER << "Firing: " << event::RECEIVE_KEYBOARD_FOCUS
<< ".\n";
owner_.fire(event::RECEIVE_KEYBOARD_FOCUS, *keyboard_focus_, nullptr);
}
}
void distributor::keyboard_add_to_chain(widget* widget)
{
assert(widget);
assert(std::find(keyboard_focus_chain_.begin(),
keyboard_focus_chain_.end(),
widget) == keyboard_focus_chain_.end());
keyboard_focus_chain_.push_back(widget);
}
void distributor::keyboard_remove_from_chain(widget* w)
{
assert(w);
std::vector<widget*>::iterator itor = std::find(
keyboard_focus_chain_.begin(), keyboard_focus_chain_.end(), w);
if(itor != keyboard_focus_chain_.end()) {
keyboard_focus_chain_.erase(itor);
}
}
template<typename Fcn, typename P1, typename P2, typename P3>
void distributor::signal_handler_keyboard_internal(event::ui_event evt, P1&& p1, P2&& p2, P3&& p3)
{
/** @todo Test whether recursion protection is needed. */
DBG_GUI_E << LOG_HEADER << evt << ".\n";
if(keyboard_focus_) {
// Attempt to cast to styled_widget, to avoid sending events if the
// widget is disabled. If the cast fails, we assume the widget
// is enabled and ready to receive events.
styled_widget* control = dynamic_cast<styled_widget*>(keyboard_focus_);
if(!control || control->get_active()) {
DBG_GUI_E << LOG_HEADER << "Firing: " << evt
<< ".\n";
if(owner_.fire(evt, *keyboard_focus_, p1, p2, p3)) {
return;
}
}
if(text_box_base* tb = dynamic_cast<text_box_base*>(keyboard_focus_)) {
if(tb->is_composing()) {
return; // Skip the keyboard chain if composition is in progress.
}
}
}
for(std::vector<widget*>::reverse_iterator ritor
= keyboard_focus_chain_.rbegin();
ritor != keyboard_focus_chain_.rend();
++ritor) {
if(*ritor == keyboard_focus_) {
continue;
}
if(*ritor == &owner_) {
/**
* @todo Make sure we're not in the event chain.
*
* No idea why we're here, but needs to be fixed, otherwise we keep
* calling this function recursively upon unhandled events...
*
* Probably added to make sure the window can grab the events and
* handle + block them when needed, this is no longer needed with
* the chain.
*/
continue;
}
// Attempt to cast to styled_widget, to avoid sending events if the
// widget is disabled. If the cast fails, we assume the widget
// is enabled and ready to receive events.
styled_widget* control = dynamic_cast<styled_widget*>(keyboard_focus_);
if(control != nullptr && !control->get_active()) {
continue;
}
DBG_GUI_E << LOG_HEADER << "Firing: " << evt << ".\n";
if(owner_.fire(evt, **ritor, p1, p2, p3)) {
return;
}
}
}
void distributor::signal_handler_sdl_key_down(const SDL_Keycode key, const SDL_Keymod modifier, const std::string& unicode)
{
signal_handler_keyboard_internal<signal_keyboard_function>(event::SDL_KEY_DOWN, key, modifier, unicode);
}
void distributor::signal_handler_sdl_text_input(const std::string& unicode, int32_t start, int32_t end)
{
signal_handler_keyboard_internal<signal_text_input_function>(event::SDL_TEXT_INPUT, unicode, start, end);
}
void distributor::signal_handler_sdl_text_editing(const std::string& unicode, int32_t start, int32_t end)
{
signal_handler_keyboard_internal<signal_text_input_function>(event::SDL_TEXT_EDITING, unicode, start, end);
}
void distributor::signal_handler_notify_removal(dispatcher& w,
const ui_event event)
{
DBG_GUI_E << LOG_HEADER << event << ".\n";
/**
* @todo Evaluate whether moving the cleanup parts in the subclasses.
*
* It might be cleaner to do it that way, but creates extra small
* functions...
*/
if(hover_widget_ == &w) {
stop_hover_timer();
}
if(mouse_button_left::last_clicked_widget_ == &w) {
mouse_button_left::last_clicked_widget_ = nullptr;
}
if(mouse_button_left::focus_ == &w) {
mouse_button_left::focus_ = nullptr;
}
if(mouse_button_middle::last_clicked_widget_ == &w) {
mouse_button_middle::last_clicked_widget_ = nullptr;
}
if(mouse_button_middle::focus_ == &w) {
mouse_button_middle::focus_ = nullptr;
}
if(mouse_button_right::last_clicked_widget_ == &w) {
mouse_button_right::last_clicked_widget_ = nullptr;
}
if(mouse_button_right::focus_ == &w) {
mouse_button_right::focus_ = nullptr;
}
if(mouse_focus_ == &w) {
mouse_focus_ = nullptr;
}
if(keyboard_focus_ == &w) {
keyboard_focus_ = nullptr;
}
const std::vector<widget*>::iterator itor
= std::find(keyboard_focus_chain_.begin(),
keyboard_focus_chain_.end(),
&w);
if(itor != keyboard_focus_chain_.end()) {
keyboard_focus_chain_.erase(itor);
}
}
} // namespace event
} // namespace gui2