-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
generator.cpp
1288 lines (1040 loc) · 31.5 KB
/
generator.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) 2008 - 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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/generator_private.hpp"
#include "gui/widgets/window.hpp"
#include "wml_exception.hpp"
#include <numeric>
namespace gui2
{
namespace policy
{
/***** ***** ***** ***** Minimum selection ***** ***** ***** *****/
namespace minimum_selection
{
void one_item::set_item_shown(const unsigned index, const bool show)
{
if(show && get_selected_item_count() == 0) {
do_select_item(index);
} else if(!show && is_selected(index)) {
do_deselect_item(index);
for(unsigned i = 1; i < get_item_count(); ++i) {
unsigned new_index = (index + i) % get_item_count();
if(get_item_shown(new_index)) {
do_select_item(new_index);
break;
}
}
}
}
void one_item::create_item(const unsigned index)
{
if(get_selected_item_count() == 0) {
do_select_item(index);
}
}
bool one_item::deselect_item(const unsigned index)
{
if(get_selected_item_count() > 1) {
do_deselect_item(index);
return true;
}
return false;
}
void one_item::delete_item(const unsigned index)
{
/** @todo do_select_item needs to test for shown flag. */
if(is_selected(index)) {
do_deselect_item(index);
if(get_selected_item_count() == 0) {
// Are there items left?
const unsigned item_count = get_item_count();
const unsigned visible_index = get_ordered_index(index);
if(item_count > 1) {
// Is the last item deselected?
if(visible_index == item_count - 1) {
// Select the second last.
do_select_item(get_item_at_ordered(visible_index - 1));
} else {
// Select the next item.
do_select_item(get_item_at_ordered(visible_index + 1));
}
}
}
}
}
void no_item::set_item_shown(const unsigned index, const bool show)
{
if(!show && is_selected(index)) {
do_deselect_item(index);
}
}
} // namespace minimum_selection
/***** ***** ***** ***** Placement ***** ***** ***** *****/
namespace placement
{
horizontal_list::horizontal_list() : placed_(false)
{
}
void horizontal_list::create_item(const unsigned /*index*/)
{
if(!placed_) {
return;
}
/** @todo implement. */
assert(false);
}
point horizontal_list::calculate_best_size() const
{
// The best size is the sum of the widths and the greatest height.
point result(0, 0);
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
const point best_size = item(i).get_best_size();
result.x += best_size.x;
if(best_size.y > result.y) {
result.y = best_size.y;
}
}
return result;
}
void horizontal_list::place(const point& origin, const point& size)
{
/*
* - Set every item to its best size.
* - The origin gets increased with the width of the last item.
* - No item should be higher as the size.
* - In the end the origin should be the sum or the origin and the wanted
* width.
*/
point current_origin = origin;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
point best_size = grid.get_best_size();
assert(best_size.y <= size.y);
// FIXME should we look at grow factors???
best_size.y = size.y;
grid.place(current_origin, best_size);
current_origin.x += best_size.x;
}
if(current_origin.x != origin.x + size.x) {
ERR_GUI_L << "Failed to fit horizontal list to requested rect; expected right edge was " << origin.x + size.x;
ERR_GUI_L << ", actual right edge was " << current_origin.x;
ERR_GUI_L << " (left edge is " << origin.x << ")\n";
}
}
void horizontal_list::set_origin(const point& origin)
{
point current_origin = origin;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
grid.set_origin(current_origin);
current_origin.x += grid.get_width();
}
}
void horizontal_list::set_visible_rectangle(const SDL_Rect& rectangle)
{
/*
* Note for most implementations this function could work only for the
* independent class it probably fails. Evaluate to make a generic
* function in the generator template class and call it from the wanted
* placement functions.
*/
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item_ordered(i);
grid.set_visible_rectangle(rectangle);
}
}
widget* horizontal_list::find_at(const point& coordinate,
const bool must_be_active)
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
const widget* horizontal_list::find_at(const point& coordinate,
const bool must_be_active) const
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
const widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
void horizontal_list::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(int i = get_ordered_index(get_item_count() - 1); i >= 0; i--) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item(get_item_at_ordered(i)).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void horizontal_list::handle_key_right_arrow(SDL_Keymod /*modifier*/,
bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(size_t i = get_ordered_index(0); i < get_item_count(); i++) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item(get_item_at_ordered(i)).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
vertical_list::vertical_list() : placed_(false)
{
}
void vertical_list::create_item(const unsigned /*index*/)
{
if(!placed_) {
return;
}
/** @todo implement. */
assert(false);
}
point vertical_list::calculate_best_size() const
{
// The best size is the sum of the heights and the greatest width.
point result(0, 0);
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
const point best_size = item(i).get_best_size();
if(best_size.x > result.x) {
result.x = best_size.x;
}
result.y += best_size.y;
}
return result;
}
void vertical_list::place(const point& origin, const point& size)
{
/*
* - Set every item to its best size.
* - The origin gets increased with the height of the last item.
* - No item should be wider as the size.
* - In the end the origin should be the sum or the origin and the wanted
* height.
*/
point current_origin = origin;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
point best_size = grid.get_best_size();
assert(best_size.x <= size.x);
// FIXME should we look at grow factors???
best_size.x = size.x;
grid.place(current_origin, best_size);
current_origin.y += best_size.y;
}
if(current_origin.y != origin.y + size.y) {
ERR_GUI_L << "Failed to fit vertical list to requested rect; expected bottom edge was " << origin.y + size.y;
ERR_GUI_L << ", actual bottom edge was " << current_origin.y;
ERR_GUI_L << " (top edge is " << origin.y << ")\n";
}
}
void vertical_list::set_origin(const point& origin)
{
point current_origin = origin;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
grid.set_origin(current_origin);
current_origin.y += grid.get_height();
}
}
void vertical_list::set_visible_rectangle(const SDL_Rect& rectangle)
{
/*
* Note for most implementations this function could work only for the
* independent class it probably fails. Evaluate to make a generic
* function in the generator template class and call it from the wanted
* placement functions.
*/
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.set_visible_rectangle(rectangle);
}
}
widget* vertical_list::find_at(const point& coordinate,
const bool must_be_active)
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
const widget* vertical_list::find_at(const point& coordinate,
const bool must_be_active) const
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
const widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
void vertical_list::handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(int i = get_ordered_index(get_item_count() - 1); i >= 0; i--) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item_ordered(i).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void vertical_list::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(size_t i = get_ordered_index(0); i < get_item_count(); i++) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item_ordered(i).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
table::table() : placed_(false)//, n_cols_(2)
{
}
void table::create_item(const unsigned /*index*/)
{
if(!placed_) {
return;
}
/** @todo implement. */
assert(false);
}
point table::calculate_best_size() const
{
// The best size is the one that minimizes aspect ratio of the enclosing rect
// We first calculate the best size of each item,
// then find the number of rows that minimizes the aspect ratio
// We try a number of columns from 1 up to sqrt(visible_items) + 2
size_t n_items = get_item_count();
size_t max_cols = sqrt(n_items) + 2;
std::vector<point> item_sizes;
for(size_t i = 0; i < n_items; i++) {
if(get_item_shown(i)) {
item_sizes.push_back(item(i).get_best_size());
}
}
if(item_sizes.empty()) {
return point();
}
std::vector<point> best_sizes(1);
best_sizes[0] = std::accumulate(item_sizes.begin(), item_sizes.end(), point(), [](point a, point b) {
return point(std::max(a.x, b.x), a.y + b.y);
});
int max_xtra = std::min_element(item_sizes.begin(), item_sizes.end(), [](point a, point b) {
return a.x < b.x;
})->x / 2;
for(size_t cells_in_1st_row = 2; cells_in_1st_row <= max_cols; cells_in_1st_row++) {
int row_min_width = std::accumulate(item_sizes.begin(), item_sizes.begin() + cells_in_1st_row, 0, [](int a, point b) {
return a + b.x;
});
int row_max_width = row_min_width + max_xtra;
int row = 0;
point row_size, total_size;
for(size_t n = 0; n < item_sizes.size(); n++) {
if(row_size.x + item_sizes[n].x > row_max_width) {
// Start new row
row++;
total_size.y += row_size.y;
if(total_size.x < row_size.x) {
total_size.x = row_size.x;
}
row_size = point();
}
row_size.x += item_sizes[n].x;
if(row_size.y < item_sizes[n].y) {
row_size.y = item_sizes[n].y;
}
}
total_size.y += row_size.y;
if(total_size.x < row_size.x) {
total_size.x = row_size.x;
}
best_sizes.push_back(total_size);
}
return *std::min_element(best_sizes.begin(), best_sizes.end(), [](point p1, point p2) {
return std::max<double>(p1.x, p1.y) / std::min<double>(p1.x, p1.y) < std::max<double>(p2.x, p2.y) / std::min<double>(p2.x, p2.y);
});
}
void table::place(const point& origin, const point& size)
{
/*
* - Set every item to its best size.
* - The origin gets increased with the height of the last item.
* - No item should be wider as the size.
* - In the end the origin should be the sum of the origin and the wanted
* height.
*/
// TODO: Make sure all cells in a row are the same height
point current_origin = origin;
int row_height = 0;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
point best_size = grid.get_best_size();
// FIXME should we look at grow factors???
if(current_origin.x + best_size.x > origin.x + size.x) {
current_origin.x = origin.x;
current_origin.y += row_height;
row_height = 0;
}
grid.place(current_origin, best_size);
current_origin.x += best_size.x;
if(best_size.y > row_height) {
row_height = best_size.y;
}
}
// TODO: If size is wider than best_size, the matrix will take too much vertical space.
// This block is supposed to correct for that, but doesn't work properly.
// To be more specific, it requires invalidating the layout to take effect.
if(current_origin.y + row_height != origin.y + size.y) {
point better_size = size;
better_size.y -= current_origin.y + row_height - origin.y;
set_layout_size(better_size);
}
}
void table::set_origin(const point& origin)
{
point current_origin = origin;
size_t row_height = 0;
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
grid& grid = item_ordered(i);
if(current_origin.x + grid.get_width() > origin.x + get_width()) {
current_origin.x = origin.x;
current_origin.y += row_height;
row_height = 0;
}
grid.set_origin(current_origin);
current_origin.x += grid.get_width();
if(grid.get_height() > row_height) {
row_height = grid.get_height();
}
}
}
void table::set_visible_rectangle(const SDL_Rect& rectangle)
{
/*
* Note for most implementations this function could work only for the
* independent class it probably fails. Evaluate to make a generic
* function in the generator template class and call it from the wanted
* placement functions.
*/
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.set_visible_rectangle(rectangle);
}
}
widget* table::find_at(const point& coordinate,
const bool must_be_active)
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
const widget* table::find_at(const point& coordinate,
const bool must_be_active) const
{
assert(get_window());
for(size_t i = 0; i < get_item_count(); ++i) {
if(!get_item_shown(i)) {
continue;
}
const widget* widget = item(i).find_at(coordinate, must_be_active);
if(widget) {
return widget;
}
}
return nullptr;
}
void table::handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(int i = get_ordered_index(get_item_count() - 1); i >= 0; i--) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item_ordered(i).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void table::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(size_t i = get_ordered_index(0); i < get_item_count(); i++) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item_ordered(i).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void table::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(int i = get_ordered_index(get_item_count() - 1); i >= 0; i--) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item(get_item_at_ordered(i)).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void table::handle_key_right_arrow(SDL_Keymod /*modifier*/,
bool& handled)
{
if(get_item_count() == 0) {
return;
}
if(get_selected_item_count() == 0) {
for(size_t i = get_ordered_index(0); i < get_item_count(); i++) {
if(get_item_shown(get_item_at_ordered(i))) {
// TODO: Check if active?
handled = true;
select_item(get_item_at_ordered(i), true);
break;
}
}
return;
}
// NOTE maybe this should only work if we can select only one item...
handled = true;
for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) {
if(!get_item_shown(get_item_at_ordered(i))) {
continue;
}
// NOTE we check the first widget to be active since grids have no
// active flag. This method might not be entirely reliable.
styled_widget* control = dynamic_cast<styled_widget*>(item(get_item_at_ordered(i)).get_widget(0, 0));
if(control && control->get_active()) {
select_item(get_item_at_ordered(i), true);
return;
}
}
}
void independent::request_reduce_width(const unsigned maximum_width)
{
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.request_reduce_width(maximum_width);
}
}
void independent::request_reduce_height(const unsigned maximum_height)
{
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.request_reduce_height(maximum_height);
}
}
point independent::calculate_best_size() const
{
/*
* The best size is the combination of the greatest width and greatest
* height.
*/
point result(0, 0);
for(size_t i = 0; i < get_item_count(); ++i) {
const grid& grid = item(i);
const point best_size = grid.get_best_size();
if(best_size.x > result.x) {
result.x = best_size.x;
}
if(best_size.y > result.y) {
result.y = best_size.y;
}
}
return result;
}
void independent::place(const point& origin, const point& size)
{
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.place(origin, size);
}
}
void independent::set_origin(const point& origin)
{
/*
* Set the origin for every item.
*
* @todo evaluate whether setting it only for the visible item is better
* and what the consequences are.
*/
for(size_t i = 0; i < get_item_count(); ++i) {
grid& grid = item(i);
grid.set_origin(origin);
}
}
widget* independent::find_at(const point& coordinate,
const bool must_be_active)
{
assert(get_window());
const int selected_item = get_selected_item();
if(selected_item < 0) {
return nullptr;
}
grid& grid = item(selected_item);
return grid.find_at(coordinate, must_be_active);
}
const widget* independent::find_at(const point& coordinate,
const bool must_be_active) const
{
assert(get_window());
const int selected_item = get_selected_item();
if(selected_item < 0) {
return nullptr;
}