Filter:   InfoImg
download Fl_Group.cxx
Language: C++
Copyright: Copyright 1998-2003 by Bill Spitzak and others.
LOC: 415
Project Info
Fast Light Tool Kit(fltk)
Server: SourceForge
Type: cvs
...Forge\f\fltk\fltk\fltk\src\
   allfiles.xbm
   ARRAY.h
   browser_minus.xbm
   browser_plus.xbm
   cmap.cxx
   CycleButton.cxx
   d1.xbm
   d1_mask.xbm
   dlload_osx.cxx
   dump_compose.c
   ew.xbm
   ew_mask.xbm
   fastarrow.h
   filename_absolute.cxx
   filename_expand.cxx
   filename_ext.cxx
   filename_isdir.cxx
   filename_list.cxx
   filename_match.cxx
   filename_setext.cxx
   Fl.cxx
   Fl_abort.cxx
   Fl_add_idle.cxx
   Fl_Adjuster.cxx
   Fl_Align_Group.cxx
   fl_arc.cxx
   fl_arci.cxx
   Fl_arg.cxx
   fl_ask.cxx
   Fl_Bar.cxx
   Fl_Bitmap.cxx
   fl_bmp.cxx
   Fl_Box.cxx
   Fl_Boxtype.cxx
   Fl_Browser.cxx
   Fl_Browser_load.cxx
   Fl_Button.cxx
   fl_call_main.c
   Fl_Check_Button.cxx
   Fl_Choice.cxx
   fl_clip.cxx
   Fl_Clock.cxx
   fl_cmap.h
   fl_color.cxx
   Fl_Color_Chooser.cxx
   fl_color_mac.cxx
   fl_color_win32.cxx
   fl_color_x.cxx
   Fl_compose.cxx
   fl_cursor.cxx
   fl_curve.cxx
   Fl_Dial.cxx
   fl_diamond_box.cxx
   Fl_display.cxx
   fl_dnd.cxx
   fl_dnd_mac.cxx
   fl_dnd_win32.cxx
   fl_dnd_x.cxx
   fl_draw.cxx
   fl_draw_image.cxx
   fl_draw_image_mac.cxx
   fl_draw_image_win32.cxx
   fl_draw_image_x.cxx
   fl_draw_pixmap.cxx
   fl_engraved_label.cxx
   fl_file_chooser.cxx
   Fl_FileBrowser.cxx
   Fl_FileChooser.cxx
   Fl_FileChooser.fl
   Fl_FileChooser2.cxx
   Fl_FileIcon.cxx
   Fl_FileIcon2.cxx
   Fl_FileInput.cxx
   Fl_Float_Input.cxx
   fl_font.cxx
   fl_font_mac.cxx
   fl_font_win32.cxx
   fl_font_x.cxx
   fl_font_xft.cxx
   Fl_get_key.cxx
   Fl_get_key_mac.cxx
   Fl_get_key_win32.cxx
   fl_gif.cxx
   fl_glyph.cxx
   Fl_Group.cxx
   Fl_Help_Dialog.cxx
   Fl_Help_View.cxx
   Fl_Highlight_Button.cxx
   Fl_Image.cxx
   Fl_Input.cxx
   Fl_Input_Browser.cxx
   Fl_Item.cxx
   Fl_key_name.cxx
   fl_labeltype.cxx
   Fl_Light_Button.cxx
   fl_list_fonts.cxx
   fl_list_fonts_mac.cxx
   fl_list_fonts_win32.cxx
   fl_list_fonts_x.cxx
   fl_load_plugin.cxx
   Fl_lock.cxx
   Fl_mac.cxx
   Fl_Menu.cxx
   Fl_Menu_.cxx
   Fl_Menu_add.cxx
   Fl_Menu_Bar.cxx
   Fl_Menu_Button.cxx
   Fl_Menu_global.cxx
   Fl_Menu_Item.cxx
   Fl_Menu_Window.cxx
   Fl_Multi_Image.cxx
   Fl_Numeric_Input.cxx
   Fl_Output.cxx
   fl_oval_box.cxx
   fl_overlay.cxx
   fl_overlay_visual.cxx
   Fl_own_colormap.cxx
   Fl_Pack.cxx
   Fl_Pixmap.cxx
   fl_plastic_box.cxx
   Fl_ProgressBar.cxx
   Fl_Radio_Button.cxx
   fl_rect.cxx
   Fl_Repeat_Button.cxx
   Fl_Return_Button.cxx
   fl_rgb.cxx
   Fl_RGB_Image.cxx
   Fl_Roller.cxx
   fl_round_box.cxx
   fl_rounded_box.cxx
   Fl_Scroll.cxx
   fl_scroll_area.cxx
   Fl_Scrollbar.cxx
   fl_set_fonts_mac.cxx
   fl_shadow_box.cxx
   Fl_Shaped_Window.cxx
   Fl_Shared_Image.cxx
   fl_show_colormap.cxx
   Fl_Single_Window.cxx
   Fl_Slider.cxx
   Fl_String_List.cxx
   Fl_Style.cxx
   Fl_Style_Set.cxx
   fl_symbols.cxx
   Fl_Tabs.cxx
   Fl_Text_Buffer.cxx
   Fl_Text_Display.cxx
   Fl_Text_Editor.cxx
   Fl_Tile.cxx
   Fl_Tiled_Image.cxx
   Fl_Tooltip.cxx
   Fl_Valuator.cxx
   Fl_Value_Input.cxx
   Fl_Value_Slider.cxx
   fl_vertex.cxx
   Fl_visual.cxx
   Fl_Widget.cxx
   Fl_win32.cxx
   Fl_Window.cxx
   Fl_Window_fullscreen.cxx
   Fl_Window_hotspot.cxx
   Fl_Window_iconize.cxx
   Fl_x.cxx
   Fl_XColor.h
   fl_xpm.cxx
   fltk_theme.cxx
   fltk_theme_win32.cxx
   fltk_theme_x.cxx
   IFont.h
   mediumarrow.h
   new.xbm
   ns.xbm
   ns_mask.xbm
   pktdef.h
   scandir.c
   scandir_win32.c
   shortcuts.cxx
   slowarrow.h
   string.c
   up.xbm
   utf.c
   ValueOutput.cxx
   vsnprintf.c
   wintab.h
   wintabx.h
   XColorMap.h
   xutf8.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
//
// "$Id: Fl_Group.cxx,v 1.142 2005/01/24 08:34:28 spitzak Exp $"
//
// Group widget for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2003 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//

/*! \class fltk::Group

The fltk::Group class is the FLTK container widget. It maintains an
array of child widgets. These children can themselves be any widget
including fltk::Group, nesting groups allows much more control over
layout and resize behavior. Nested groups are also necessary to group
radio buttons together.

By default fltk::Group preserves the positions and sizes of all it's
children, they do not move no matter what sizes or other children are
added or removed.

Setting resizable() will change the layout behavior so that it
responds to resizing by moving or resizing the children to fit. See
below for details.

You may want to use an fltk::Pack or a fltk::Scroll to get other
common layout behavior that can respond to changes in the sizes of
child widgets.

The most-used subclass of fltk::Group is fltk::Window, all the rules
about resizing apply to windows. Setting resizable() on a window will
allow the user to resize it. If you want different behavior (such as
from fltk::Pack) for your window you should make the window have that
as a single resizable child that fills it.

fltk::Menu is a subclass and thus all menus and browsers are groups
and the items in them are widgets.

*/

#include <config.h>
#include <fltk/Group.h>
#include <fltk/Box.h>
#include <fltk/draw.h>
#include <fltk/events.h>
#include <fltk/layout.h>
#include <fltk/damage.h>
#include <stdlib.h>

using namespace fltk;

////////////////////////////////////////////////////////////////

FL_API Group* Group::current_;

static void revert(Style* s) {
  s->color_ = GRAY75;
  s->box_ = NO_BOX;
}

// This style is unnamed since there is no reason for themes to change it:
extern NamedStyle* group_style;
static NamedStyle the_style(0, revert, &group_style);
NamedStyle* group_style = &the_style;

/*! Creates a new fltk::Group widget using the given position, size,
  and label string. The default boxtype is fltk::NO_BOX. */
Group::Group(int X,int Y,int W,int H,const char *l,bool begin)
: Widget(X,Y,W,H,l),
  children_(0),
  focus_index_(-1),
  array_(0),
  resizable_(0), // fltk 1.0 used (this)
  resize_align_(ALIGN_TOPLEFT|ALIGN_BOTTOMRIGHT),
  sizes_(0)
{
  type(GROUP_TYPE);
  style(::group_style);
  align(ALIGN_TOP);
  if (begin) this->begin();
}

/*! \e Deletes all children from the group and makes it empty.
  This calls the destructor on all the children!!! */
void Group::clear() {
  init_sizes();
  if (children_) {
    Widget*const* a = array_;
    Widget*const* e = a+children_;
    // clear everything now, in case fix_focus recursively calls us:
    children_ = 0;
    focus_index_ = -1;
    if (resizable_) resizable_ = this;
    // okay, now it is safe to destroy the children:
    while (e > a) {
      Widget* o = *--e;
      o->parent(0); // stops it from calling remove()
      delete o;
    }
    free((void*)a);
  }
}

/*! Calls clear(), and thus <i>deletes all child widgets</i> */
Group::~Group() {clear();}

/*! \fn Widget * Group::child(int n) const
  Returns a child, n >= 0 && n < children(). <i>No range checking is done!</i>
*/

/*! \fn int Group::children() const
  Returns how many child widgets the group has. */

/*! This does insert(w, find(beforethis)). This will append the
  widget if beforethis is not in the group. */
void Group::insert(Widget &o, int index) {
  if (o.parent()) {
    int n = o.parent()->find(o);
    if (o.parent() == this) {
      if (index > n) index--;
      if (index == n) return;
    }
    o.parent()->remove(n);
  }
  o.parent(this);
  if (children_ == 0) {
    // allocate for 1 child
    array_ = (Widget**)malloc(sizeof(Widget*));
    array_[0] = &o;
  } else {
    if (!(children_ & (children_-1))) // double number of children
      array_ = (Widget**)realloc((void*)array_,
				    2*children_*sizeof(Widget*));
    for (int j = children_; j > index; --j) array_[j] = array_[j-1];
    array_[index] = &o;
  }
  ++children_;
  init_sizes();
}

/*! \fn Group * Group::current()
  Returns the group being currently built. The fltk::Widget
  constructor automatically does current()->add(widget) if this is not
  null. To prevent new widgets from being added to a group, call
  Group::current(0).
*/

/*! \fn void Group::begin()

  begin() sets the current group so you can build the widget tree by
  just constructing the widgets. begin() is exactly the same as current(this).

  <i>Don't forget to end() the group or window!</i>
*/

/*! \fn void Group::end()
  end() is exactly the same as current(this->parent()). Any new
  widgets added to the widget tree will be added to the parent of the
  group.
*/

/*! The widget is removed from it's current group (if any) and then
  added to the end of this group. */
void Group::add(Widget &o) {
  insert(o, children_);
}

/*! Remove the indexed widget from the group. */
void Group::remove(int index) {
  if (index >= children_) return;
  Widget* o = array_[index];
  o->parent(0);
  children_--;
  for (int i=index; i < children_; ++i) array_[i] = array_[i+1];
  init_sizes();
}

/*! \fn void Group::remove(Widget& widget)
  Removes a widget from the group. This does nothing if the widget is
  not currently a child of this group. */

/*! Remove the indexed widget and insert the passed widget in it's place. */
void Group::replace(int index, Widget& o) {
  if (index >= children_) {add(o); return;}
  o.parent(this);
  array_[index]->parent(0);
  array_[index] = &o;
  init_sizes();
}

/*! \fn void Group::replace(Widget& old, Widget& nu)
  Find the \a old widget and remove it and insert the \a nu one. If \a old
  is not a child, the new widget is appended to the end of the
  list. */

void Group::swap(int indexA, int indexB) {
  if (indexA >= children_ || indexB >= children_) return;
  Widget* o = array_[indexA];
  array_[indexA] = array_[indexB];
  array_[indexB] = o;
  init_sizes();
}

/*! Searches the children for \a widget, returns the index of \a
  widget or of a parent of \a widget that is a child() of
  this. Returns children() if the widget is NULL or not found. */
int Group::find(const Widget* widget) const {
  for (;;) {
    if (!widget) return children_;
    if (widget->parent() == this) break;
    widget = widget->parent();
  }
  // Search backwards so if children are deleted in backwards order
  // they are found quickly:
  for (int index = children_; index--;)
    if (array_[index] == widget) return index;
  return children_;
}

////////////////////////////////////////////////////////////////
// Handle

/*! \fn int Group::focus_index() const

  The index of the widget that contains the focus. You can initialize
  this before the group is displayed. Changing it while it is
  displayed does not work, use widget->take_focus() instead.

  For some subclasses such as fltk::TabGroup, a negative number indicates
  that the group itself has the focus. In most cases any illegal value
  means it will search for any widget that accepts focus.

  This is also used by fltk::Menu to locate the item selected by
  the user. See fltk::Menu::get_item().
*/

/*! Turn Tab into Right or Left for keyboard navigation */
int Group::navigation_key() {
  switch (event_key()) {
  case TabKey:
    if (event_state(CTRL)) return 0;
    return event_state(SHIFT) ? LeftKey : RightKey;
  case UpKey:
  case DownKey:
  case LeftKey:
  case RightKey:
    return event_key();
  default:
    return 0;
  }
}

/*! Calls send() on some or all of the children widgets. */
int Group::handle(int event) {
  const int numchildren = children();
  int i;

  switch (event) {

  case FOCUS_CHANGE:
    // The focus is being changed to some widget inside this.
    focus_index_ = find(fltk::focus());
    return true;

  case FOCUS:
    // an attempt to give this widget focus:
    switch (navigation_key()) {
    default: {
      // try to give it to whatever child had focus last:
      if (focus_index_ >= 0 && focus_index_ < numchildren)
	if (child(focus_index_)->take_focus()) return true;
      // otherwise search for the widget that needs the focus. We
      // give it to the first one that returns 2, or the last one
      // that returns 1 if none return 2:
      int new_index = -1; int ret = 0;
      for (i = 0; i < numchildren; ++i) {
	Widget* w = child(i);
	if (w->takesevents() && w->tab_to_focus()) {
	  int n = w->handle(FOCUS);
	  if (n) {ret = n; new_index = i; if (n & 2) break;}
	}
      }
      focus_index_ = new_index;
      return ret;}
    case RightKey:
    case DownKey:
      for (i=0; i < numchildren; ++i)
	if (child(i)->tab_to_focus() && child(i)->take_focus()) return true;
      return false;
    case LeftKey:
    case UpKey:
      for (i = numchildren; i--;)
	if (child(i)->tab_to_focus() && child(i)->take_focus()) return true;
      return false;
    }

  case DRAG:
  case RELEASE:
  case LEAVE:
  case DND_LEAVE:
    // Ignore these. We handle them if the belowmouse of pushed widget
    // has been set to this. Subclasses may do something with these.
    // Definately do not pass them to child widgets!
    break;

  case KEY: {
    // keyboard navigation
    if (numchildren < 2) break;
    int key = navigation_key();
    if (!key) break;
    int previous = focus_index_;
    if (previous < 0 || previous >= numchildren) previous = 0;
    for (i = previous;;) {
      if (key == LeftKey || key == UpKey) {
	if (i) --i;
	else {
	  if (parent()) return false;
	  i = numchildren-1;
	}
      } else {
	++i;
	if (i >= numchildren) {
	  if (parent()) return false;
	  i = 0;
	}
      }
      if (i == previous) {
	// This appears to be for fixing bugs, where the focus gets lost.
	// The child should already have focus.
	focus_index_ = i;
	return child(i)->tab_to_focus() && child(i)->take_focus();
      }
      if (key == DownKey || key == UpKey) {
	// for up/down, the widgets have to overlap horizontally:
	Widget* o = child(i);
	Widget* p = child(previous);
	if (o->x() >= p->x()+p->w() || o->x()+o->w() <= p->x()) continue;
      }
      if (child(i)->tab_to_focus() && child(i)->take_focus()) return true;
    }}

  case PUSH:
  case ENTER:
  case MOVE:
  case DND_ENTER:
  case DND_DRAG:
    // search the children in backwards (top to bottom) order:
    for (i = numchildren; i--;) {
      Widget* child = this->child(i);
      // ignore widgets we are not pointing at:
      if (event_x() < child->x()) continue;
      if (event_x() >= child->x()+child->w()) continue;
      if (event_y() < child->y()) continue;
      if (event_y() >= child->y()+child->h()) continue;
      // see if it wants the event:
      if (child->send(event)) return true;
#if 0
      // quit when we reach a widget that claims mouse points at it,
      // so we don't pass the events to widgets "hidden" behind that one.
      if (event != ENTER && event != MOVE &&
	  child->contains(fltk::belowmouse())) return false;
#endif
    }
    return Widget::handle(event);

  default:
    // Try to give any other event to the focus:
    if (focus_index_ >= 0 && focus_index_ < numchildren)
      if (child(focus_index_)->send(event)) return true;
    // Then try all other children in top to bottom order:
    for (i = numchildren; i--;) if (i != focus_index_)
      if (child(i)->send(event)) return true;
    break;

  }
  return Widget::handle(event);
}

////////////////////////////////////////////////////////////////
// Layout

/*! \fn Widget* Group::resizable() const

  The resizable widget defines the resizing box for the group. When
  the group is resized it calculates a new size and position for all
  of its children. Widgets that are horizontally or vertically inside
  the dimensions of the box are scaled to the new size. Widgets
  outside the box are moved.

  Orignal size, the gray area is the resizable():
  \image html resizebox1.gif

  And here is the same Group resized larger:
  \image html resizebox2.gif

  The resizable may be set to the group itself, in which case all the
  contents are resized. If the resizable is NULL (the default) then
  all widgets remain a fixed size and distance from the top-left
  corner.

  It is possible to achieve any type of resize behavior by using an
  InvisibleBox as the resizable and/or by using a hierarchy of child
  fltk::Group's.
*/

/** Indicate that all the remembered child sizes should be reinitialized.

    The group remembers the initial size of itself and all it's children,
    so that the layout can be restored even if the group is resized so
    that some children go to zero or negative sizes. Normally these
    sizes are calculated the first time layout() is called, though you
    can do so by calling sizes() directly.

    Though this makes sense it often results in unexpected behavior
    when a program wants to rearrange the child widgets or change the
    size of a group to surround a new arrangement of child widgets. The
    widgets tend to snap back to a previous size.

    Calling init_sizes() "resets" the sizes array to the current group
    and children positions.  Actually it just deletes the sizes array,
    and it is not recreated until the next time layout is called. Call
    this if you manually adjust the sizes of the children, or attempt
    to change the size of the group without wanting the children to scale.

    This is automatically done when any child is added or removed.  */
void Group::init_sizes() {
  delete[] sizes_; sizes_ = 0;
  relayout();
}

/** This non-virtual override is for programs that set up a group's
    layout and then call resize() on it to set the correct size before
    it is displayed. What it does is remember the current sizes (the
    thing the init_sizes() method makes it forget) before calling the
    normal widget resize().

    This is a non-virtual override because in normal use fltk will call
    layout() anyway before any use of the widget, and Group's layout()
    initializes the sizes. This is only for programs that use resize()
    directly.
*/
bool Group::resize(int x, int y, int w, int h) {
  if (!sizes_ && resizable() && children_ && (w != this->w() || h != this->h()))
    layout(); // this is needed to recursively get inner groups...
  return Widget::resize(x,y,w,h);
}

/** Returns array of initial sizes of the widget and it's children.

    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.  This is a convienent order for the
    algorithim.

    This array is not calculated until the first time sizes() is
    called. It then returns the same array from then on. Calling
    init_sizes() deletes the array so the next call of this creates
    a new one.
*/
int* Group::sizes() {
  if (!sizes_) {
    int* p = sizes_ = new int[4*(children_+2)];
    // first thing in sizes array is the group's size:
    p[0] = x();
    p[1] = w();
    p[2] = y();
    p[3] = h();
    // next is the resizable's size:
    p[4] = 0; // init to the group's size
    p[5] = p[1];
    p[6] = 0;
    p[7] = p[3];
    Widget* r = resizable();
    if (r && r != this) { // then clip the resizable to it
      int t;
      t = r->x(); if (t > 0) p[4] = t;
      t +=r->w(); if (t < p[1]) p[5] = t;
      t = r->y(); if (t > 0) p[6] = t;
      t +=r->h(); if (t < p[3]) p[7] = t;
    }
    // next is all the children's sizes:
    p += 8;
    Widget*const* a = array_;
    Widget*const* e = a+children_;
    while (a < e) {
      Widget* o = *a++;
      *p++ = o->x();
      *p++ = o->x()+o->w();
      *p++ = o->y();
      *p++ = o->y()+o->h();
    }
  }
  return sizes_;
}

void Group::layout() {

  // Save the layout damage and then clear it. This is so layout() of a
  // child can turn it back on and subclasses like PackedGroup can detect that:
  int layout_damage = this->layout_damage();
  Widget::layout();

  if (resizable() && children_ && layout_damage&(LAYOUT_WH|LAYOUT_DAMAGE)) {
    int* p = sizes(); // initialize the size array

    // get changes in size from the initial size:
    int dw = w()-p[1];
    int dh = h()-p[3];

    p+=4;

    // Calculate a new size & position for every child widget:
    // get initial size of resizable():
    int IX = *p++;
    int IR = *p++;
    int IY = *p++;
    int IB = *p++;

    Widget*const* a = array_;
    Widget*const* e = a+children_;
    while (a < e) {
      Widget* o = *a++;
      int X = *p++;
      if (X >= IR) X += dw;
      else if (X > IX) {
	switch (resize_align_&12) {
	case 0: X = X + dw/2; break; // ALIGN_CENTER
	case 4: break; // ALIGN_LEFT
	case 8: X = X+dw; break; // ALIGN_RIGHT
	case 12: X = X + dw * (X-IX)/(IR-IX); break; // both
	}
      }
      int R = *p++;
      if (R >= IR) R += dw;
      else if (R > IX) {
	switch (resize_align_&12) {
	case 0: R = R + dw/2; break; // ALIGN_CENTER
	case 4: break; // ALIGN_LEFT
	case 8: R = R+dw; break; // ALIGN_RIGHT
	case 12: R = R + dw * (R-IX)/(IR-IX); // both
	}
      }
      int Y = *p++;
      if (Y >= IB) Y += dh;
      else if (Y > IY) {
	switch (resize_align_&3) {
	case 0: Y = Y + dh/2; break; // ALIGN_CENTER
	case 1: break; // ALIGN_TOP
	case 2: Y = Y+dh; break; // ALIGN_BOTTOM
	case 3: Y = Y + dh*(Y-IY)/(IB-IY); break; //both;
	}
      }
      int B = *p++;
      if (B >= IB) B += dh;
      else if (B > IY) {
	switch (resize_align_&3) {
	case 0: B = B + dh/2; break; // ALIGN_CENTER
	case 1: break; // ALIGN_TOP
	case 2: B = B+dh; break; // ALIGN_BOTTOM
	case 3: B = B + dh*(B-IY)/(IB-IY); break; //both;
	}
      }
      int flags = o->layout_damage();
      if (X != o->x()) {flags |= LAYOUT_X; o->x(X);}
      if (Y != o->y()) {flags |= LAYOUT_Y; o->y(Y);}
      if (R-X != o->w()) {flags |= LAYOUT_W; o->w(R-X);}
      if (B-Y != o->h()) {flags |= LAYOUT_H; o->h(B-Y);}
      o->layout_damage(flags);
    }
  }

  Widget*const* a = array_;
  Widget*const* e = a+children_;
  if ((layout_damage & LAYOUT_XY) && !is_window()) {
    // If this is not a Window and the xy position is changed, we must
    // call layout() on every child. This is necessary so that child
    // Windows will move to their new positions.
    while (a < e) {
      Widget* widget = *a++;
      widget->layout_damage(widget->layout_damage()|LAYOUT_XY);
      widget->layout();
    }
  } else {
    // Otherwise we only need to call layout on children with the
    // layout bit set:
    while (a < e) {
      Widget* widget = *a++;
      if (widget->layout_damage()) widget->layout();
    }
  }
}

////////////////////////////////////////////////////////////////
// Draw

// Widgets that want to outwit the clip-out can set this when they are
// drawn to indicate that they did the clip-out. Only TabGroup really uses
// this (and I'm not certain it has to), plus a bunch of back-compatability
// widgets that want to be "invisible" (they turn this on but don't draw
// anything). This is a pointer so if it is left on by a child widget
// it does not fool this into thinking the clipping is done.
Widget* fl_did_clipping;

void Group::draw() {
  int numchildren = children();
  if (damage() & ~DAMAGE_CHILD) {
#if USE_CLIPOUT
    // Non-blinky draw, draw the inside widgets first, clip their areas
    // out, and then draw the background:
    push_clip(Rectangle(w(), h()));
    int n; for (n = numchildren; n--;) {
      Widget& w = *child(n);
      fl_did_clipping = 0;
      draw_child(w);
      if (fl_did_clipping != &w) clipout(w.x(), w.y(), w.w(), w.h());
    }
    draw_box();
    draw_label();
    pop_clip();
    // labels are drawn without the clip for back compatability so they
    // can draw atop sibling widgets:
    for (n = 0; n < numchildren; n++) draw_outside_label(*child(n));
#else
    // blinky-draw:
    draw_box();
    draw_label();
    for (int n = 0; n < numchildren; n++) {
      Widget& w = *child(n);
      draw_child(w);
      draw_outside_label(w);
    }
#endif
  } else {
    // only some child widget has been damaged, draw them without
    // doing any clipping.  This is for maximum speed, even though
    // this may result in different output if this widget overlaps
    // another widget or a label.
    for (int n = 0; n < numchildren; n++) {
      Widget& w = *child(n);
      if (w.damage() & DAMAGE_CHILD_LABEL) {
	draw_outside_label(w);
	w.set_damage(w.damage() & ~DAMAGE_CHILD_LABEL);
      }
      update_child(w);
    }
  }
}

// Pieces of draw() that subclasses may want to use:

/*! Draw what would be in the area of the widget if the widget was not
  there. By calling this in draw(), a widgets can redraw as though
  they are partially transparent, or more complicated shapes than
  rectangles. Note that only parent widgets are drawn, not
  underlapping ones.

  If DAMAGE_EXPOSE is on in damage() then the window (or at least some
  region of it) is being completely redrawn. Normally FLTK will have
  already drawn the background, so to avoid redundant drawing this
  will return immediatly without drawing anything. However FLTK may be
  compiled with USE_CLIPOUT (an option to reduce blinking in
  single-buffered windows) and in that case the widget must draw any
  visible background. In this case this function always draws the
  background.
*/
void Widget::draw_background() const {
#if !USE_CLIPOUT
  if (damage()&DAMAGE_EXPOSE) return;
#endif
  if (!parent()) return;
  push_clip(Rectangle(w(),h()));
  push_matrix();
  translate(-x(), -y());
  if (!parent()->box()->fills_rectangle()) parent()->draw_background();
  parent()->draw_box();
  parent()->draw_label();
  pop_matrix();
  pop_clip();
}

/*! Force a child to draw, by turning on DAMAGE_ALL and DAMAGE_EXPOSE,
  and calling it's draw() after temporarily translating so 0,0 in
  drawing coordinates is the upper-left corner. It's damage is then set to 0.
*/
void Group::draw_child(Widget& w) const {
  if (w.visible() && !w.is_window()) {
    if (!not_clipped(w)) return;
    push_matrix();
    translate(w.x(), w.y());
    w.set_damage(DAMAGE_ALL|DAMAGE_EXPOSE);
    w.draw();
    w.set_damage(0);
    pop_matrix();
  }
}

/*! If the child's damage() is not zero, force it to update calling
  it's draw() after temporarily translating so 0,0 in drawing
  coordinates is the upper-left corner. It's damage is then set to 0.
*/
void Group::update_child(Widget& w) const {
  if (w.damage() && w.visible() && !w.is_window()) {
    if (!not_clipped(w)) return;
    push_matrix();
    translate(w.x(), w.y());
    w.draw();
    w.set_damage(0);
    pop_matrix();
  }
}

/** If this is a Group and not a Window, subtract x() and y() from the
    position of all children. This will fix the positions of widgets
    created for fltk1.1 that are inside a group.
*/
void Group::fix_old_positions() {
  if (is_window()) return; // in fltk 1.0 children of windows were relative
  for (int i = 0; i < children(); i++) {
    Widget& w = *(child(i));
    w.x(w.x()-x());
    w.y(w.y()-y());
  }
}

//
// End of "$Id: Fl_Group.cxx,v 1.142 2005/01/24 08:34:28 spitzak Exp $".
//