2 * Copyright (C) 2007 by Mark Pustjens <pustjens@dds.nl>
3 * Copyright (C) 2010-2013 by CenterIM developers
5 * This file is part of CenterIM.
7 * CenterIM is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CenterIM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * HorizontalListBox class implementation.
29 #include "HorizontalListBox.h"
37 HorizontalListBox::HorizontalListBox(int w
, int h
)
38 : AbstractListBox(w
, h
), children_width(0), autosize_children_count(0)
42 VerticalLine
*HorizontalListBox::insertSeparator(size_t pos
)
44 VerticalLine
*l
= new VerticalLine(AUTOSIZE
);
45 insertWidget(pos
, *l
);
49 VerticalLine
*HorizontalListBox::appendSeparator()
51 VerticalLine
*l
= new VerticalLine(AUTOSIZE
);
56 void HorizontalListBox::insertWidget(size_t pos
, Widget
& widget
)
58 Container::insertWidget(pos
, widget
, UNSETPOS
, UNSETPOS
);
60 if (!widget
.isVisible())
63 // calculate the expected width by the widget
64 int w
= widget
.getWidth();
65 int autosize_change
= 0;
67 w
= widget
.getWishWidth();
74 updateChildren(w
, autosize_change
);
77 void HorizontalListBox::appendWidget(Widget
& widget
)
79 insertWidget(children
.size(), widget
);
82 void HorizontalListBox::updateArea()
84 int autosize_width
= 1;
85 int autosize_width_extra
= 0;
86 if (autosize_children_count
&& children_width
< real_width
) {
87 int space
= real_width
- (children_width
- autosize_children_count
);
88 autosize_width
= space
/ autosize_children_count
;
89 autosize_width_extra
= space
% autosize_children_count
;
93 for (Children::iterator i
= children
.begin(); i
!= children
.end(); i
++) {
95 bool is_visible
= widget
->isVisible();
97 // position the widget correctly
98 widget
->setRealPosition(x
, 0);
100 // calculate the real width
101 int w
= widget
->getWidth();
103 w
= widget
->getWishWidth();
106 if (is_visible
&& autosize_width_extra
) {
107 autosize_width_extra
--;
113 // calculate the real height
114 int h
= widget
->getHeight();
116 h
= widget
->getWishHeight();
123 widget
->setRealSize(w
, h
);
129 // make sure that the currently focused widget is visible
133 void HorizontalListBox::onChildMoveResize(Widget
& activator
,
134 const Rect
& oldsize
, const Rect
& newsize
)
137 assert(newsize
.getLeft() == UNSETPOS
&& newsize
.getTop() == UNSETPOS
);
139 if (!activator
.isVisible())
142 int old_width
= oldsize
.getWidth();
143 int new_width
= newsize
.getWidth();
145 if (old_width
== new_width
)
148 int autosize_change
= 0;
149 if (old_width
== AUTOSIZE
) {
150 old_width
= activator
.getWishWidth();
151 if (old_width
== AUTOSIZE
) {
156 if (new_width
== AUTOSIZE
) {
157 new_width
= activator
.getWishWidth();
158 if (new_width
== AUTOSIZE
) {
164 updateChildren(new_width
- old_width
, autosize_change
);
167 void HorizontalListBox::onChildWishSizeChange(Widget
& activator
,
168 const Size
& oldsize
, const Size
& newsize
)
170 if (!activator
.isVisible() || activator
.getWidth() != AUTOSIZE
)
173 // the widget is visible and is autosized
174 int old_width
= oldsize
.getWidth();
175 int new_width
= newsize
.getWidth();
177 if (old_width
== new_width
)
180 updateChildren(new_width
- old_width
, 0);
183 void HorizontalListBox::onChildVisible(Widget
& activator
, bool visible
)
185 // the widget is being hidden or deleted
186 int w
= activator
.getWidth();
187 int sign
= visible
? 1 : -1;
188 int autosize_change
= 0;
190 w
= activator
.getWishWidth();
193 autosize_change
= sign
;
196 updateChildren(sign
* w
, autosize_change
);
199 void HorizontalListBox::updateChildren(int children_width_change
,
200 int autosize_children_count_change
)
202 // set new children data
203 children_width
+= children_width_change
;
204 assert(children_width
>= 0);
205 autosize_children_count
+= autosize_children_count_change
;
206 assert(autosize_children_count
>= 0);
208 // reposition all child widgets
210 signal_children_width_change(*this, children_width
);
213 } // namespace CppConsUI
215 /* vim: set tabstop=2 shiftwidth=2 textwidth=78 expandtab : */