-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFl_Group.cxx
1038 lines (879 loc) · 31 KB
/
Fl_Group.cxx
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
//
// Group widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
// https://www.fltk.org/bugs.php
//
// Fl_Group is the basic container type in FLTK. Other container types
// (classes) are usually subclasses of Fl_Group.
// Fl_Window itself is a subclass of this, and most of the event
// handling is designed so windows themselves work correctly.
#include <FL/Fl_Group.H>
#include "Fl_Window_Driver.H"
#include <FL/Fl_Rect.H>
#include <FL/fl_draw.H>
#include <stdlib.h> // malloc etc.
Fl_Group* Fl_Group::current_;
// Hack: A single child is stored in the pointer to the array, while
// multiple children are stored in an allocated array:
/**
Returns a pointer to the array of children.
\note This pointer is only valid until the next time a child
is added or removed.
*/
Fl_Widget*const* Fl_Group::array() const {
return children_ <= 1 ? &child1_ : array_;
}
/**
Searches the child array for the widget and returns the index.
Returns children() if the widget is NULL or not found.
*/
int Fl_Group::find(const Fl_Widget* o) const {
Fl_Widget*const* a = array();
int i; for (i=0; i < children_; i++) if (*a++ == o) break;
return i;
}
// Some (* which? *) compilers / toolchains can't export the static
// class member: current_, so these methods can't be inlined...
/**
Sets the current group so you can build the widget
tree by just constructing the widgets.
begin() is automatically called by the constructor for Fl_Group (and thus for
Fl_Window as well). begin() <I>is exactly the same as</I> current(this).
<I>Don't forget to end() the group or window!</I>
*/
void Fl_Group::begin() {current_ = this;}
/**
<I>Exactly the same as</I> current(this->parent()). Any new widgets
added to the widget tree will be added to the parent of the group.
*/
void Fl_Group::end() {current_ = parent();}
/**
Returns the currently active group.
The Fl_Widget constructor automatically does current()->add(widget) if this
is not null. To prevent new widgets from being added to a group, call
Fl_Group::current(0).
*/
Fl_Group *Fl_Group::current() {return current_;}
/**
Sets the current group.
\see Fl_Group::current()
*/
void Fl_Group::current(Fl_Group *g) {current_ = g;}
extern Fl_Widget* fl_oldfocus; // set by Fl::focus
// For back-compatibility, we must adjust all events sent to child
// windows so they are relative to that window.
static int send(Fl_Widget* o, int event) {
if (!o->as_window()) return o->handle(event);
switch ( event )
{
case FL_DND_ENTER: /* FALLTHROUGH */
case FL_DND_DRAG:
// figure out correct type of event:
event = (o->contains(Fl::belowmouse())) ? FL_DND_DRAG : FL_DND_ENTER;
}
int save_x = Fl::e_x; Fl::e_x -= o->x();
int save_y = Fl::e_y; Fl::e_y -= o->y();
int ret = o->handle(event);
Fl::e_y = save_y;
Fl::e_x = save_x;
switch ( event )
{
case FL_ENTER: /* FALLTHROUGH */
case FL_DND_ENTER:
// Successful completion of FL_ENTER means the widget is now the
// belowmouse widget, but only call Fl::belowmouse if the child
// widget did not do so:
if (!o->contains(Fl::belowmouse())) Fl::belowmouse(o);
break;
}
return ret;
}
// translate the current keystroke into up/down/left/right for navigation:
static int navkey() {
// The app may want these for hotkeys, check key state
if (Fl::event_state(FL_CTRL | FL_ALT | FL_META)) return 0;
switch (Fl::event_key()) {
case 0: // not an FL_KEYBOARD/FL_SHORTCUT event
break;
case FL_Tab:
if (!Fl::event_state(FL_SHIFT)) return FL_Right;
return FL_Left;
case FL_Right:
return FL_Right;
case FL_Left:
return FL_Left;
case FL_Up:
return FL_Up;
case FL_Down:
return FL_Down;
}
return 0;
}
int Fl_Group::handle(int event) {
Fl_Widget*const* a = array();
int i;
Fl_Widget* o;
switch (event) {
case FL_FOCUS:
switch (navkey()) {
default:
if (savedfocus_ && savedfocus_->take_focus()) return 1;
case FL_Right:
case FL_Down:
for (i = children(); i--;) if ((*a++)->take_focus()) return 1;
break;
case FL_Left:
case FL_Up:
for (i = children(); i--;) if (a[i]->take_focus()) return 1;
break;
}
return 0;
case FL_UNFOCUS:
savedfocus_ = fl_oldfocus;
return 0;
case FL_KEYBOARD:
return navigation(navkey());
case FL_SHORTCUT:
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && Fl::event_inside(o) && send(o,FL_SHORTCUT))
return 1;
}
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && !Fl::event_inside(o) && send(o,FL_SHORTCUT))
return 1;
}
if ((Fl::event_key() == FL_Enter || Fl::event_key() == FL_KP_Enter)) return navigation(FL_Down);
return 0;
case FL_ENTER:
case FL_MOVE:
for (i = children(); i--;) {
o = a[i];
if (o->visible() && Fl::event_inside(o)) {
if (o->contains(Fl::belowmouse())) {
return send(o,FL_MOVE);
} else {
Fl::belowmouse(o);
if (send(o,FL_ENTER)) return 1;
}
}
}
Fl::belowmouse(this);
return 1;
case FL_DND_ENTER:
case FL_DND_DRAG:
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && Fl::event_inside(o)) {
if (o->contains(Fl::belowmouse())) {
return send(o,FL_DND_DRAG);
} else if (send(o,FL_DND_ENTER)) {
if (!o->contains(Fl::belowmouse())) Fl::belowmouse(o);
return 1;
}
}
}
Fl::belowmouse(this);
return 0;
case FL_PUSH:
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && Fl::event_inside(o)) {
Fl_Widget_Tracker wp(o);
if (send(o,FL_PUSH)) {
if (Fl::pushed() && wp.exists() && !o->contains(Fl::pushed())) Fl::pushed(o);
return 1;
}
}
}
return 0;
case FL_RELEASE:
case FL_DRAG:
o = Fl::pushed();
if (o == this) return 0;
else if (o) send(o,event);
else {
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && Fl::event_inside(o)) {
if (send(o,event)) return 1;
}
}
}
return 0;
case FL_MOUSEWHEEL:
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && Fl::event_inside(o) && send(o,FL_MOUSEWHEEL))
return 1;
}
for (i = children(); i--;) {
o = a[i];
if (o->takesevents() && !Fl::event_inside(o) && send(o,FL_MOUSEWHEEL))
return 1;
}
return 0;
case FL_DEACTIVATE:
case FL_ACTIVATE:
for (i = children(); i--;) {
o = *a++;
if (o->active()) o->handle(event);
}
return 1;
case FL_SHOW:
case FL_HIDE:
for (i = children(); i--;) {
o = *a++;
if (event == FL_HIDE && o == Fl::focus()) {
// Give up input focus...
int old_event = Fl::e_number;
o->handle(Fl::e_number = FL_UNFOCUS);
Fl::e_number = old_event;
Fl::focus(0);
}
if (o->visible()) o->handle(event);
}
return 1;
default:
// For all other events, try to give to each child, starting at focus:
for (i = 0; i < children(); i ++)
if (Fl::focus_ == a[i]) break;
if (i >= children()) i = 0;
if (children()) {
for (int j = i;;) {
if (a[j]->takesevents()) if (send(a[j], event)) return 1;
j++;
if (j >= children()) j = 0;
if (j == i) break;
}
}
return 0;
}
}
// try to move the focus in response to a keystroke:
int Fl_Group::navigation(int key) {
if (children() <= 1) return 0;
int i;
for (i = 0; ; i++) {
if (i >= children_) return 0;
if (array_[i]->contains(Fl::focus())) break;
}
Fl_Widget *previous = array_[i];
for (;;) {
switch (key) {
case FL_Right:
case FL_Down:
i++;
if (i >= children_) {
if (parent()) return 0;
i = 0;
}
break;
case FL_Left:
case FL_Up:
if (i) i--;
else {
if (parent()) return 0;
i = children_-1;
}
break;
default:
return 0;
}
Fl_Widget* o = array_[i];
if (o == previous) return 0;
switch (key) {
case FL_Down:
case FL_Up:
// for up/down, the widgets have to overlap horizontally:
if (o->x() >= previous->x()+previous->w() ||
o->x()+o->w() <= previous->x()) continue;
}
if (o->take_focus()) return 1;
}
}
////////////////////////////////////////////////////////////////
Fl_Group::Fl_Group(int X,int Y,int W,int H,const char *l)
: Fl_Widget(X,Y,W,H,l) {
align(FL_ALIGN_TOP);
children_ = 0;
array_ = 0;
savedfocus_ = 0;
resizable_ = this;
bounds_ = 0; // this is allocated when first resize() is done
sizes_ = 0; // see bounds_ (FLTK 1.3 compatibility)
// Subclasses may want to construct child objects as part of their
// constructor, so make sure they are add()'d to this object.
// But you must end() the object!
begin();
}
/**
Deletes all child widgets from memory recursively.
This method differs from the remove() method in that it
affects all child widgets and deletes them from memory.
The resizable() widget of the Fl_Group is set to the Fl_Group itself.
\internal If the Fl_Group widget contains the Fl::focus() or the
Fl::pushed() widget these are set to sensible values (other widgets
or the Fl_Group widget itself).
\see Fl_Group::remove(int), Fl_Group::delete_child(int), Fl_Group::~Fl_Group()
*/
void Fl_Group::clear() {
savedfocus_ = 0;
resizable_ = this;
init_sizes();
// we must change the Fl::pushed() widget, if it is one of
// the group's children. Otherwise fl_fix_focus() would send lots
// of events to children that are about to be deleted anyway.
Fl_Widget *pushed = Fl::pushed(); // save pushed() widget
if (contains(pushed)) pushed = this; // set it to be the group, if it's a child
Fl::pushed(this); // for fl_fix_focus etc.
// Implementation note (AlbrechtS, Nov. 01, 2022):
// For some obscure reason the order of all children had been
// reversed in FLTK 1.3.x so the first child would be deleted
// first but this is no longer done since FLTK 1.4.0.
// Reasoning:
// (1) it is supposedly better to remove children in the
// order "last in, first out"
// (2) it would not be compatible with the new subclass
// notification feature Fl_Group::on_remove().
// See git commit a918292547cfb154 or earlier for removed code.
// End of implementation note.
// Okay, now it is safe to destroy the children. Children are
// removed and deleted in the order from last child to first
// child which is much faster than the other way around and
// should be the "natural order" (last in, first out).
while (children_) { // delete all children
int idx = children_-1; // last child's index
Fl_Widget* w = child(idx); // last child widget
if (w->parent()==this) { // should always be true
if (children_>2) { // optimized removal
w->parent_ = 0; // reset child's parent
on_remove(idx);
children_--; // update counter
} else { // slow removal
remove(idx);
}
delete w; // delete the child
} else { // should never happen
remove(idx); // remove it anyway
}
}
if (pushed != this) Fl::pushed(pushed); // reset pushed() widget
}
/**
The destructor <I>also deletes all the children</I>. This allows a
whole tree to be deleted at once, without having to keep a pointer to
all the children in the user code.
It is allowed that the Fl_Group and all of its children are automatic
(local) variables, but you must declare the Fl_Group \e first, so that
it is destroyed last.
If you add static or automatic (local) variables to an Fl_Group, then it
is your responsibility to remove (or delete) all such static or automatic
child widgets \e \b before destroying the group - otherwise the group will
attempt to call delete operator on them leading to undefined behavior!
*/
Fl_Group::~Fl_Group() {
if (current_ == this)
end();
clear();
}
/**
Allow derived groups to act when a widget is added as a child.
Widgets derived from Fl_Group may store additional data for their children.
Overriding this method will allow derived classes to generate these data
structures just before the child is added.
This method usually returns the same index that was given in the parameters.
By setting a new index, the position of other widgets in the child pointer
array can be preserved (e.g. Fl_Scroll keeps its scroll bars as the last
two children).
By returning -1, Fl_Group::insert will not add the child to
array_. This is not recommended, but Fl_Table does something similar to
forward children to a hidden group.
\param candidate the candidate will be added to the child array_ after this
method returns.
\param index add the child at this position in the array_
\return index to position the child as planned
\return a new index to force the child to a different position
\return -1 to keep the group from adding the candidate
*/
int Fl_Group::on_insert(Fl_Widget *candidate, int index) {
(void)candidate;
return index;
}
/**
Allow derived groups to act when a widget is moved within the group.
Widgets derived from Fl_Group may store additional data for their children.
Overriding this method will allow derived classes to move these data
structures just before the child itself is moved.
This method usually returns the new index that was given in the
parameters. By setting a different destination index, the position of other
widgets in the child pointer array can be preserved.
By returning -1, Fl_Group::insert will not move the child.
\param oldIndex the current index of the child that will be moved
\param newIndex the new index of the child
\return \p newIndex to position the child as planned
\return a different index to force the child to a different position
\return -1 to keep the group from moving the child
*/
int Fl_Group::on_move(int oldIndex, int newIndex) {
(void)oldIndex;
return newIndex;
}
/**
The widget is removed from its current group (if any) and then
inserted into this group. It is put at index n - or at the end,
if n >= children(). This can also be used to rearrange
the widgets inside a group.
*/
void Fl_Group::insert(Fl_Widget &o, int index) {
if (o.parent()) {
Fl_Group* g = o.parent();
int n = g->find(o);
if (g == this) {
// avoid expensive remove() and add() if we just move a widget within the group
index = on_move(n, index);
if (index < 0) return; // don't move: requested by subclass
if (index > children_)
index = children_;
if (index > n) index--; // compensate for removal and re-insertion
if (index == n) return; // same position; this includes (children_ == 1)
if (index > n)
memmove(array_+n, array_+(n+1), (index-n) * sizeof(Fl_Widget*));
else
memmove(array_+(index+1), array_+index, (n-index) * sizeof(Fl_Widget*));
array_[index] = &o;
init_sizes();
return;
}
g->remove(n);
}
index = on_insert(&o, index);
if (index == -1) return;
o.parent_ = this;
if (children_ == 0) { // use array pointer to point at single child
child1_ = &o;
} else if (children_ == 1) { // go from 1 to 2 children
Fl_Widget* t = child1_;
array_ = (Fl_Widget**)malloc(2*sizeof(Fl_Widget*));
if (index) {array_[0] = t; array_[1] = &o;}
else {array_[0] = &o; array_[1] = t;}
} else {
if (!(children_ & (children_-1))) // double number of children
array_ = (Fl_Widget**)realloc((void*)array_,
2*children_*sizeof(Fl_Widget*));
int j; for (j = children_; j > index; j--) array_[j] = array_[j-1];
array_[j] = &o;
}
children_++;
init_sizes();
}
/**
The widget is removed from its current group (if any) and then added
to the end of this group.
*/
void Fl_Group::add(Fl_Widget &o) {insert(o, children_);}
/**
Allow derived groups to act when a child widget is removed from the group.
Widgets derived from Fl_Group may store additional data for their children.
Overriding this method will allow derived classes to remove these data
structures just before the child is removed.
\param index remove the child at this position in the array_
*/
void Fl_Group::on_remove(int index) {
(void)index;
}
/**
Removes the widget at \p index from the group but does not delete it.
This method does nothing if \p index is out of bounds.
This method differs from the clear() method in that it only affects
a single widget and does not delete it from memory.
\since FLTK 1.3.0
*/
void Fl_Group::remove(int index) {
if (index < 0 || index >= children_) return;
on_remove(index);
Fl_Widget &o = *child(index);
if (&o == savedfocus_) savedfocus_ = 0;
if (&o == resizable_) resizable_ = this;
if (o.parent_ == this) { // this should always be true
o.parent_ = 0;
}
// remove the widget from the group
children_--;
if (children_ == 1) { // go from 2 to 1 child
Fl_Widget *t = array_[!index];
free((void*)array_);
child1_ = t;
} else if (children_ > 1) { // delete from array
for (; index < children_; index++) array_[index] = array_[index+1];
}
init_sizes();
}
/**
Removes a widget from the group but does not delete it.
This method does nothing if the widget is not a child of the group.
This method differs from the clear() method in that it only affects
a single widget and does not delete it from memory.
\note If you have the child's index anyway, use remove(int index)
instead, because this doesn't need a child lookup in the group's
table of children. This can be much faster, if there are lots of
children.
*/
void Fl_Group::remove(Fl_Widget &o) {
if (!children_) return;
int i = find(o);
if (i < children_) remove(i);
}
/**
Removes the widget at \p index from the group and deletes it.
This method does nothing if \p index is out of bounds.
This method differs from the remove() method in that it deletes
the widget from memory. Since this method is virtual it can be
reimplemented in subclasses with additional requirements and
consequences. See the documentation of subclasses.
Many subclasses don't need to reimplement this method.
\note This method \b may refuse to remove and delete the widget
if it is an essential part of the Fl_Group, for instance
a scrollbar in an Fl_Scroll group. In this case the widget is
neither removed nor deleted.
This method does not call init_sizes() or redraw(). This is left
to user code if necessary.
Returns 0 if the widget was removed and deleted.
Return values \> 0 are reserved for use by FLTK core widgets.
Return values \< 0 are free to be used by user defined widgets.
\todo Reimplementation of Fl_Group::delete_child(int) in more FLTK
subclasses. This is not yet complete.
\param[in] index index of child to be removed
\returns success (0) or error code
\retval 0 success
\retval 1 index out of range
\retval 2 widget not allowed to be removed (see note)
\retval >2 reserved for FLTK use
\since FLTK 1.4.0
*/
int Fl_Group::delete_child(int index) {
if (index < 0 || index >= children_)
return 1;
Fl_Widget *w = child(index);
remove(index);
delete w;
return 0;
}
/**
Resets the internal array of widget sizes and positions.
The Fl_Group widget keeps track of the original widget sizes and
positions when resizing occurs so that if you resize a window back to
its original size the widgets will be in the correct places. If you
rearrange the widgets in your group, call this method to register the
new arrangement with the Fl_Group that contains them.
If you add or remove widgets, this will be done automatically.
\note The internal array of widget sizes and positions will be allocated
and filled when the next resize() occurs. For more information on
the contents and structure of the bounds() array see bounds().
\see bounds()
\see sizes() (deprecated)
*/
void Fl_Group::init_sizes() {
delete[] bounds_;
bounds_ = 0;
delete[] sizes_; // FLTK 1.3 compatibility
sizes_ = 0; // FLTK 1.3 compatibility
}
/**
Returns the internal array of widget sizes and positions.
If the bounds() array does not exist, it will be allocated and filled
with the current widget sizes and positions.
The bounds() array stores the initial positions of widgets as Fl_Rect's.
The size of the array is children() + 2.
- The first Fl_Rect is the group,
- the second is the resizable (clipped to the group),
- the rest are the children.
This is a convenient order for the resize algorithm.
If the group and/or the resizable() is a Fl_Window (or subclass) then
the x() and y() coordinates of their respective Fl_Rect's are zero.
\note You should never need to use this \e protected method directly,
unless you have special needs to rearrange the children of a
Fl_Group. Fl_Tile uses this to rearrange its widget positions.
The returned array should be considered read-only. Do not change
its contents. If you need to rearrange children in a group, do
so by resizing the children and call init_sizes().
\#include \<FL/Fl_Rect.H\> if you want to access the bounds() array in
your derived class. Fl_Rect.H is intentionally not included by
Fl_Group.H to avoid unnecessary dependencies.
\returns Array of Fl_Rect's with widget positions and sizes. The
returned array is only valid until init_sizes() is called
or widgets are added to or removed from the group.
\see init_sizes()
\since FLTK 1.4.0
\internal Notes to developers:
- If you change this be sure to fix Fl_Tile which also uses this array!
- Do not \#include Fl_Rect.H in Fl_Group.H because this would introduce
lots of unnecessary dependencies on Fl_Rect.H.
*/
Fl_Rect* Fl_Group::bounds() {
if (!bounds_) {
Fl_Rect* p = bounds_ = new Fl_Rect[children_+2];
// first thing in bounds array is the group's size:
if (as_window())
p[0] = Fl_Rect(w(),h()); // x = y = 0
else
p[0] = Fl_Rect(this);
// next is the resizable's size:
int left = p->x(); // init to the group's position and size
int top = p->y();
int right = p->r();
int bottom = p->b();
Fl_Widget* r = resizable();
if (r && r != this) { // then clip the resizable to it
int t;
t = r->x(); if (t > left) left = t;
t +=r->w(); if (t < right) right = t;
t = r->y(); if (t > top) top = t;
t +=r->h(); if (t < bottom) bottom = t;
}
p[1] = Fl_Rect(left, top, right-left, bottom-top);
// next is all the children's sizes:
p += 2;
Fl_Widget*const* a = array();
for (int i=children_; i--;) {
*p++ = Fl_Rect(*a++);
}
}
return bounds_;
}
/** Returns the internal array of widget sizes and positions.
For backward compatibility with FLTK versions before 1.4.
The sizes() array stores the initial positions of widgets as
(left, right, top, bottom) quads. The first quad is the group, the
second is the resizable (clipped to the group), and the rest are the
children. If the group and/or the resizable() is a Fl_Window, then
the first (left) and third (top) entries of their respective quads
(x,y) are zero.
\deprecated Deprecated since 1.4.0. Please use bounds() instead.
\note This method will be removed in a future FLTK version (1.5.0 or higher).
\returns Array of int's with widget positions and sizes. The returned
array is only valid until init_sizes() is called or widgets
are added to or removed from the group.
\note Since FLTK 1.4.0 the returned array is a \b read-only and re-ordered
copy of the internal bounds() array. Do not change its contents.
If you need to rearrange children in a group, do so by resizing
the children and call init_sizes().
\see bounds()
*/
int* Fl_Group::sizes()
{
if (sizes_) return sizes_;
// allocate new sizes_ array and copy bounds_ over to sizes_
int* pi = sizes_ = new int[4*(children_+2)];
Fl_Rect *rb = bounds();
for (int i = 0; i < children_+2; i++, rb++) {
*pi++ = rb->x();
*pi++ = rb->r();
*pi++ = rb->y();
*pi++ = rb->b();
}
return sizes_;
}
/**
Resizes the Fl_Group widget and all of its children.
The Fl_Group widget first resizes itself, and then it moves and resizes
all its children according to the rules documented for
Fl_Group::resizable(Fl_Widget*)
\sa Fl_Group::resizable(Fl_Widget*)
\sa Fl_Group::resizable()
\sa Fl_Widget::resize(int,int,int,int)
*/
void Fl_Group::resize(int X, int Y, int W, int H) {
int dx = X - x();
int dy = Y - y();
int dw = W - w();
int dh = H - h();
Fl_Rect* p = bounds(); // save initial sizes and positions
Fl_Widget::resize(X, Y, W, H); // make new xywh values visible for children
// Part 1: no resizable() or both width and height didn't change,
// just move the children.
// This case covers also window rescaling where dw == dh == 0.
if (!resizable() || (dw==0 && dh==0)) {
// top window and subwindows must not change the position of their children
if (as_window())
dx = dy = 0;
// Check if there's anything to do, otherwise don't call resize().
// Note that subwindows require resize() even if their relative position
// didn't change, at least on macOS, if it's a rescale.
if (Fl_Window::is_a_rescale() || dx || dy) {
Fl_Widget*const* a = array();
for (int i = children_; i--;) {
Fl_Widget* o = *a++;
o->resize(o->x() + dx, o->y() + dy, o->w(), o->h());
}
}
} // End of part 1
// Part 2: here we definitely have a resizable() widget, resize children
else if (children_) {
// get changes in size/position from the initial size:
dx = X - p->x();
dw = W - p->w();
dy = Y - p->y();
dh = H - p->h();
if (as_window())
dx = dy = 0;
p++;
// Developer note:
// The following code uses T = top, L = left, R = right, and B = bottom
// widget bounds. T and L are equivalent to x() and y(), whereas
// R = x() + w() and B = y() + h(), respectively, i.e. the next pixel
// beyond the widget border.
// RL, RR, RT, and RB are those values of the resizable widget.
// get initial size of resizable():
int RL = p->x();
int RR = RL + p->w();
int RT = p->y();
int RB = RT + p->h();
p++;
// resize children
Fl_Widget*const* a = array();
for (int i = children_; i--; p++) {
Fl_Widget* o = *a++;
int L = p->x();
int R = L + p->w();
int T = p->y();
int B = T + p->h();
// widget resizing code from Francois Ostiguy (since FLTK 1.4.0)
if (L >= RR) L += dw;
else if (L > RL) L += dw * (L-RL) / (RR-RL);
if (R >= RR) R += dw;
else if (R > RL) R += dw * (R-RL) / (RR-RL);
if (T >= RB) T += dh;
else if (T > RT) T += dh * (T-RT) / (RB-RT);
if (B >= RB) B += dh;
else if (B > RT) B += dh * (B-RT) / (RB-RT);
o->resize(L+dx, T+dy, R-L, B-T);
}
} // End of part 2: we have a resizable() widget
}
/**
Draws all children of the group.
This is useful, if you derived a widget from Fl_Group and want to draw a special
border or background. You can call draw_children() from the derived draw() method
after drawing the box, border, or background.
*/
void Fl_Group::draw_children() {
Fl_Widget*const* a = array();
if (clip_children()) {
fl_push_clip(x() + Fl::box_dx(box()),
y() + Fl::box_dy(box()),
w() - Fl::box_dw(box()),
h() - Fl::box_dh(box()));
}
if (damage() & ~FL_DAMAGE_CHILD) { // redraw the entire thing:
for (int i=children_; i--;) {
Fl_Widget& o = **a++;
draw_child(o);
draw_outside_label(o);
}
} else { // only redraw the children that need it:
for (int i=children_; i--;) update_child(**a++);
}
if (clip_children()) fl_pop_clip();
}
void Fl_Group::draw() {
if (damage() & ~FL_DAMAGE_CHILD) { // redraw the entire thing:
draw_box();
draw_label();
}
draw_children();
}
/**
Draws a child only if it needs it.
This draws a child widget, if it is not clipped \em and if any damage() bits
are set. The damage bits are cleared after drawing.
\sa Fl_Group::draw_child(Fl_Widget& widget) const
*/
void Fl_Group::update_child(Fl_Widget& widget) const {
if (widget.damage() && widget.visible() && widget.type() < FL_WINDOW &&
fl_not_clipped(widget.x(), widget.y(), widget.w(), widget.h())) {
widget.draw();
widget.clear_damage();
}
}
/**
Forces a child to redraw.
This draws a child widget, if it is not clipped.
The damage bits are cleared after drawing.
*/
void Fl_Group::draw_child(Fl_Widget& widget) const {
if (widget.visible() && widget.type() < FL_WINDOW &&
fl_not_clipped(widget.x(), widget.y(), widget.w(), widget.h())) {
// The following call clears all damage flags and then *sets* FL_DAMAGE_ALL
widget.clear_damage(FL_DAMAGE_ALL);
widget.draw();
widget.clear_damage();
}
}
/** Parents normally call this to draw outside labels of child widgets. */
void Fl_Group::draw_outside_label(const Fl_Widget& widget) const {
if (!widget.visible()) return;
// skip any labels that are inside the widget:
if (!(widget.align()&15) || (widget.align() & FL_ALIGN_INSIDE)) return;
// invent a box that is outside the widget:
Fl_Align a = widget.align();
int X = widget.x();
int Y = widget.y();
int W = widget.w();
int H = widget.h();
int wx, wy;
if (const_cast<Fl_Group*>(this)->as_window()) {
wx = wy = 0;
} else {
wx = x(); wy = y();
}
if ( (a & FL_ALIGN_POSITION_MASK) == FL_ALIGN_LEFT_TOP ) {