DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | // |
| 2 | // "$Id: Fl_Pack.cxx 7903 2010-11-28 21:06:39Z matt $" |
| 3 | // |
| 4 | // Packing widget for the Fast Light Tool Kit (FLTK). |
| 5 | // |
| 6 | // Copyright 1998-2010 by Bill Spitzak and others. |
| 7 | // |
| 8 | // This library is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU Library General Public |
| 10 | // License as published by the Free Software Foundation; either |
| 11 | // version 2 of the License, or (at your option) any later version. |
| 12 | // |
| 13 | // This library is distributed in the hope that it will be useful, |
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // Library General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU Library General Public |
| 19 | // License along with this library; if not, write to the Free Software |
| 20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | // USA. |
| 22 | // |
| 23 | // Please report all bugs and problems on the following page: |
| 24 | // |
| 25 | // http://www.fltk.org/str.php |
| 26 | // |
| 27 | |
| 28 | // Based on code by Curtis Edwards |
| 29 | // Group that compresses all it's children together and resizes to surround |
| 30 | // them on each redraw (only if box() is zero) |
| 31 | // Bugs: ? |
| 32 | |
| 33 | #include <FL/Fl.H> |
| 34 | #include <FL/Fl_Pack.H> |
| 35 | #include <FL/fl_draw.H> |
| 36 | |
| 37 | /** |
| 38 | Creates a new Fl_Pack widget using the given position, size, |
| 39 | and label string. The default boxtype is FL_NO_BOX. |
| 40 | <P>The destructor <I>also deletes all the children</I>. This allows a |
| 41 | whole tree to be deleted at once, without having to keep a pointer to |
| 42 | all the children in the user code. A kludge has been done so the |
| 43 | Fl_Pack and all of it's children can be automatic (local) |
| 44 | variables, but you must declare the Fl_Pack<I>first</I>, so |
| 45 | that it is destroyed last. |
| 46 | */ |
| 47 | Fl_Pack::Fl_Pack(int X, int Y, int W, int H,const char *l) |
| 48 | : Fl_Group(X, Y, W, H, l) { |
| 49 | resizable(0); |
| 50 | spacing_ = 0; |
| 51 | // type(VERTICAL); // already set like this |
| 52 | } |
| 53 | |
| 54 | void Fl_Pack::draw() { |
| 55 | int tx = x()+Fl::box_dx(box()); |
| 56 | int ty = y()+Fl::box_dy(box()); |
| 57 | int tw = w()-Fl::box_dw(box()); |
| 58 | int th = h()-Fl::box_dh(box()); |
| 59 | int rw, rh; |
| 60 | int current_position = horizontal() ? tx : ty; |
| 61 | int maximum_position = current_position; |
| 62 | uchar d = damage(); |
| 63 | Fl_Widget*const* a = array(); |
| 64 | if (horizontal()) { |
| 65 | rw = -spacing_; |
| 66 | rh = th; |
| 67 | |
| 68 | for (int i = children(); i--;) |
| 69 | if (child(i)->visible()) { |
| 70 | if (child(i) != this->resizable()) rw += child(i)->w(); |
| 71 | rw += spacing_; |
| 72 | } |
| 73 | } else { |
| 74 | rw = tw; |
| 75 | rh = -spacing_; |
| 76 | |
| 77 | for (int i = children(); i--;) |
| 78 | if (child(i)->visible()) { |
| 79 | if (child(i) != this->resizable()) rh += child(i)->h(); |
| 80 | rh += spacing_; |
| 81 | } |
| 82 | } |
| 83 | for (int i = children(); i--;) { |
| 84 | Fl_Widget* o = *a++; |
| 85 | if (o->visible()) { |
| 86 | int X,Y,W,H; |
| 87 | if (horizontal()) { |
| 88 | X = current_position; |
| 89 | W = o->w(); |
| 90 | Y = ty; |
| 91 | H = th; |
| 92 | } else { |
| 93 | X = tx; |
| 94 | W = tw; |
| 95 | Y = current_position; |
| 96 | H = o->h(); |
| 97 | } |
| 98 | // Last child, if resizable, takes all remaining room |
| 99 | if(i == 0 && o == this->resizable()) { |
| 100 | if(horizontal()) |
| 101 | W = tw - rw; |
| 102 | else |
| 103 | H = th - rh; |
| 104 | } |
| 105 | if (spacing_ && current_position>maximum_position && box() && |
| 106 | (X != o->x() || Y != o->y() || d&FL_DAMAGE_ALL)) { |
| 107 | fl_color(color()); |
| 108 | if (horizontal()) |
| 109 | fl_rectf(maximum_position, ty, spacing_, th); |
| 110 | else |
| 111 | fl_rectf(tx, maximum_position, tw, spacing_); |
| 112 | } |
| 113 | if (X != o->x() || Y != o->y() || W != o->w() || H != o->h()) { |
| 114 | o->resize(X,Y,W,H); |
| 115 | o->clear_damage(FL_DAMAGE_ALL); |
| 116 | } |
| 117 | if (d&FL_DAMAGE_ALL) { |
| 118 | draw_child(*o); |
| 119 | draw_outside_label(*o); |
| 120 | } else update_child(*o); |
| 121 | // child's draw() can change it's size, so use new size: |
| 122 | current_position += (horizontal() ? o->w() : o->h()); |
| 123 | if (current_position > maximum_position) |
| 124 | maximum_position = current_position; |
| 125 | current_position += spacing_; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (horizontal()) { |
| 130 | if (maximum_position < tx+tw && box()) { |
| 131 | fl_color(color()); |
| 132 | fl_rectf(maximum_position, ty, tx+tw-maximum_position, th); |
| 133 | } |
| 134 | tw = maximum_position-tx; |
| 135 | } else { |
| 136 | if (maximum_position < ty+th && box()) { |
| 137 | fl_color(color()); |
| 138 | fl_rectf(tx, maximum_position, tw, ty+th-maximum_position); |
| 139 | } |
| 140 | th = maximum_position-ty; |
| 141 | } |
| 142 | |
| 143 | tw += Fl::box_dw(box()); if (tw <= 0) tw = 1; |
| 144 | th += Fl::box_dh(box()); if (th <= 0) th = 1; |
| 145 | if (tw != w() || th != h()) { |
| 146 | Fl_Widget::resize(x(),y(),tw,th); |
| 147 | d = FL_DAMAGE_ALL; |
| 148 | } |
| 149 | if (d&FL_DAMAGE_ALL) { |
| 150 | draw_box(); |
| 151 | draw_label(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // |
| 156 | // End of "$Id: Fl_Pack.cxx 7903 2010-11-28 21:06:39Z matt $". |
| 157 | // |