2013-08-09 00:01:20 +00:00
|
|
|
// Aseprite UI Library
|
2013-01-27 15:13:13 +00:00
|
|
|
// Copyright (C) 2001-2013 David Capello
|
2010-09-27 22:18:17 +00:00
|
|
|
//
|
2013-08-09 00:01:20 +00:00
|
|
|
// This source file is distributed under MIT license,
|
|
|
|
// please read LICENSE.txt for more information.
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
/* Based on code from GTK+ 2.1.2 (gtk+/gtk/gtkhbox.c) */
|
|
|
|
|
2013-08-06 00:20:19 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-07-12 20:29:16 +00:00
|
|
|
#include "config.h"
|
2013-08-06 00:20:19 +00:00
|
|
|
#endif
|
2009-07-12 20:29:16 +00:00
|
|
|
|
2011-01-21 20:50:04 +00:00
|
|
|
#include "gfx/size.h"
|
2012-06-18 01:49:58 +00:00
|
|
|
#include "ui/box.h"
|
|
|
|
#include "ui/message.h"
|
|
|
|
#include "ui/preferred_size_event.h"
|
2013-05-11 20:56:27 +00:00
|
|
|
#include "ui/resize_event.h"
|
2012-06-18 01:49:58 +00:00
|
|
|
#include "ui/theme.h"
|
2010-09-25 19:22:32 +00:00
|
|
|
|
2012-06-18 01:02:54 +00:00
|
|
|
namespace ui {
|
|
|
|
|
2013-08-06 00:20:19 +00:00
|
|
|
using namespace gfx;
|
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
Box::Box(int align)
|
2013-04-04 01:07:24 +00:00
|
|
|
: Widget(kBoxWidget)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2011-01-24 22:48:09 +00:00
|
|
|
setAlign(align);
|
2011-02-15 12:00:29 +00:00
|
|
|
initTheme();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
void Box::onPreferredSize(PreferredSizeEvent& ev)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2012-01-05 22:45:03 +00:00
|
|
|
#define GET_CHILD_SIZE(w, h) \
|
|
|
|
{ \
|
|
|
|
if (getAlign() & JI_HOMOGENEOUS) \
|
|
|
|
w = MAX(w, reqSize.w); \
|
|
|
|
else \
|
|
|
|
w += reqSize.w; \
|
|
|
|
\
|
|
|
|
h = MAX(h, reqSize.h); \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 22:45:03 +00:00
|
|
|
#define FINAL_SIZE(w) \
|
|
|
|
{ \
|
|
|
|
if (getAlign() & JI_HOMOGENEOUS) \
|
|
|
|
w *= nvis_children; \
|
|
|
|
\
|
|
|
|
w += child_spacing * (nvis_children-1); \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
int w, h, nvis_children;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
nvis_children = 0;
|
2012-07-18 00:42:02 +00:00
|
|
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
|
|
|
Widget* child = *it;
|
2008-02-29 19:29:49 +00:00
|
|
|
if (!(child->flags & JI_HIDDEN))
|
2007-09-18 23:57:02 +00:00
|
|
|
nvis_children++;
|
|
|
|
}
|
2012-01-05 22:45:03 +00:00
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
w = h = 0;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-07-18 00:42:02 +00:00
|
|
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
|
|
|
Widget* child = *it;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2008-02-29 19:29:49 +00:00
|
|
|
if (child->flags & JI_HIDDEN)
|
2007-09-18 23:57:02 +00:00
|
|
|
continue;
|
|
|
|
|
2010-08-03 01:57:41 +00:00
|
|
|
Size reqSize = child->getPreferredSize();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
if (this->getAlign() & JI_HORIZONTAL) {
|
2007-09-18 23:57:02 +00:00
|
|
|
GET_CHILD_SIZE(w, h);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GET_CHILD_SIZE(h, w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvis_children > 0) {
|
2011-01-24 22:48:09 +00:00
|
|
|
if (this->getAlign() & JI_HORIZONTAL) {
|
2007-09-18 23:57:02 +00:00
|
|
|
FINAL_SIZE(w);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FINAL_SIZE(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
w += border_width.l + border_width.r;
|
|
|
|
h += border_width.t + border_width.b;
|
|
|
|
|
|
|
|
ev.setPreferredSize(Size(w, h));
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 20:56:27 +00:00
|
|
|
void Box::onResize(ResizeEvent& ev)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2012-01-05 22:45:03 +00:00
|
|
|
#define FIXUP(x, y, w, h, l, t, r, b) \
|
|
|
|
{ \
|
|
|
|
if (nvis_children > 0) { \
|
|
|
|
if (getAlign() & JI_HOMOGENEOUS) { \
|
2013-10-26 15:50:55 +00:00
|
|
|
width = (getBounds().w \
|
2012-01-05 22:45:03 +00:00
|
|
|
- this->border_width.l \
|
|
|
|
- this->border_width.r \
|
|
|
|
- this->child_spacing * (nvis_children - 1)); \
|
|
|
|
extra = width / nvis_children; \
|
|
|
|
} \
|
|
|
|
else if (nexpand_children > 0) { \
|
2013-10-26 15:50:55 +00:00
|
|
|
width = getBounds().w - reqSize.w; \
|
2012-01-05 22:45:03 +00:00
|
|
|
extra = width / nexpand_children; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
width = 0; \
|
|
|
|
extra = 0; \
|
|
|
|
} \
|
|
|
|
\
|
2013-10-26 15:50:55 +00:00
|
|
|
x = getBounds().x + this->border_width.l; \
|
|
|
|
y = getBounds().y + this->border_width.t; \
|
|
|
|
h = MAX(1, getBounds().h \
|
2012-01-05 22:45:03 +00:00
|
|
|
- this->border_width.t \
|
|
|
|
- this->border_width.b); \
|
|
|
|
\
|
2012-07-18 00:42:02 +00:00
|
|
|
UI_FOREACH_WIDGET(getChildren(), it) { \
|
|
|
|
child = *it; \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
|
|
|
if (!(child->flags & JI_HIDDEN)) { \
|
|
|
|
if (this->getAlign() & JI_HOMOGENEOUS) { \
|
|
|
|
if (nvis_children == 1) \
|
|
|
|
child_width = width; \
|
|
|
|
else \
|
|
|
|
child_width = extra; \
|
|
|
|
\
|
|
|
|
--nvis_children; \
|
|
|
|
width -= extra; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
reqSize = child->getPreferredSize(); \
|
|
|
|
\
|
|
|
|
child_width = reqSize.w; \
|
|
|
|
\
|
2012-04-05 22:00:19 +00:00
|
|
|
if (child->isExpansive()) { \
|
2012-01-05 22:45:03 +00:00
|
|
|
if (nexpand_children == 1) \
|
|
|
|
child_width += width; \
|
|
|
|
else \
|
|
|
|
child_width += extra; \
|
|
|
|
\
|
|
|
|
--nexpand_children; \
|
|
|
|
width -= extra; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
w = MAX(1, child_width); \
|
|
|
|
\
|
2013-05-11 20:56:27 +00:00
|
|
|
gfx::Rect cpos; \
|
|
|
|
if (getAlign() & JI_HORIZONTAL) \
|
|
|
|
cpos = gfx::Rect(x, y, w, h); \
|
2012-01-05 22:45:03 +00:00
|
|
|
else \
|
2013-05-11 20:56:27 +00:00
|
|
|
cpos = gfx::Rect(y, x, h, w); \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
2013-05-11 20:56:27 +00:00
|
|
|
child->setBounds(cpos); \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
|
|
|
x += child_width + this->child_spacing; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
Widget* child;
|
2007-09-18 23:57:02 +00:00
|
|
|
int nvis_children = 0;
|
|
|
|
int nexpand_children = 0;
|
|
|
|
int child_width;
|
|
|
|
int width;
|
|
|
|
int extra;
|
|
|
|
int x, y, w, h;
|
2013-05-11 20:56:27 +00:00
|
|
|
|
|
|
|
setBoundsQuietly(ev.getBounds());
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-07-18 00:42:02 +00:00
|
|
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
|
|
|
child = *it;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2008-02-29 19:29:49 +00:00
|
|
|
if (!(child->flags & JI_HIDDEN)) {
|
2007-09-18 23:57:02 +00:00
|
|
|
nvis_children++;
|
2012-04-05 22:00:19 +00:00
|
|
|
if (child->isExpansive())
|
2012-01-05 22:45:03 +00:00
|
|
|
nexpand_children++;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 20:56:27 +00:00
|
|
|
Size reqSize = getPreferredSize();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-01-24 22:48:09 +00:00
|
|
|
if (this->getAlign() & JI_HORIZONTAL) {
|
2007-09-18 23:57:02 +00:00
|
|
|
FIXUP(x, y, w, h, l, t, r, b);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
FIXUP(y, x, h, w, t, l, b, r);
|
|
|
|
}
|
|
|
|
}
|
2012-06-18 01:02:54 +00:00
|
|
|
|
2013-05-11 20:56:27 +00:00
|
|
|
void Box::onPaint(PaintEvent& ev)
|
|
|
|
{
|
|
|
|
getTheme()->paintBox(ev);
|
|
|
|
}
|
|
|
|
|
2012-06-18 01:02:54 +00:00
|
|
|
} // namespace ui
|