2013-08-09 00:01:20 +00:00
|
|
|
// Aseprite UI Library
|
2015-06-23 17:00:00 +00:00
|
|
|
// Copyright (C) 2001-2013, 2015 David Capello
|
2010-09-27 22:18:17 +00:00
|
|
|
//
|
2014-03-29 23:08:05 +00:00
|
|
|
// This file is released under the terms of the MIT license.
|
|
|
|
// Read LICENSE.txt for more information.
|
2007-09-18 23:57:02 +00:00
|
|
|
|
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"
|
2015-12-04 00:50:05 +00:00
|
|
|
#include "ui/size_hint_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
|
|
|
}
|
|
|
|
|
2015-12-04 00:50:05 +00:00
|
|
|
void Box::onSizeHint(SizeHintEvent& ev)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2015-06-24 01:46:05 +00:00
|
|
|
#define ADD_CHILD_SIZE(w, h) { \
|
|
|
|
if (getAlign() & HOMOGENEOUS) \
|
|
|
|
prefSize.w = MAX(prefSize.w, childSize.w); \
|
|
|
|
else \
|
|
|
|
prefSize.w += childSize.w; \
|
|
|
|
prefSize.h = MAX(prefSize.h, childSize.h); \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
#define FINAL_ADJUSTMENT(w) { \
|
|
|
|
if (getAlign() & HOMOGENEOUS) \
|
|
|
|
prefSize.w *= visibleChildren; \
|
|
|
|
prefSize.w += childSpacing() * (visibleChildren-1); \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
int visibleChildren = 0;
|
2015-12-03 22:46:13 +00:00
|
|
|
for (auto child : children()) {
|
2015-06-23 19:07:41 +00:00
|
|
|
if (!child->hasFlags(HIDDEN))
|
2015-06-24 01:46:05 +00:00
|
|
|
++visibleChildren;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
2012-01-05 22:45:03 +00:00
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
Size prefSize(0, 0);
|
2015-12-03 22:46:13 +00:00
|
|
|
for (auto child : children()) {
|
2015-06-23 19:07:41 +00:00
|
|
|
if (child->hasFlags(HIDDEN))
|
2007-09-18 23:57:02 +00:00
|
|
|
continue;
|
|
|
|
|
2015-12-04 00:50:05 +00:00
|
|
|
Size childSize = child->sizeHint();
|
2015-06-24 01:46:05 +00:00
|
|
|
if (getAlign() & HORIZONTAL) {
|
|
|
|
ADD_CHILD_SIZE(w, h);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-24 01:46:05 +00:00
|
|
|
ADD_CHILD_SIZE(h, w);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
if (visibleChildren > 0) {
|
|
|
|
if (getAlign() & HORIZONTAL) {
|
|
|
|
FINAL_ADJUSTMENT(w);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-24 01:46:05 +00:00
|
|
|
FINAL_ADJUSTMENT(h);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
prefSize.w += border().width();
|
|
|
|
prefSize.h += border().height();
|
2011-01-24 22:48:09 +00:00
|
|
|
|
2015-12-04 00:50:05 +00:00
|
|
|
ev.setSizeHint(prefSize);
|
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
|
|
|
{
|
2015-06-24 01:46:05 +00:00
|
|
|
#define LAYOUT_CHILDREN(x, w) { \
|
|
|
|
availExtraSize = availSize.w - prefSize.w; \
|
|
|
|
availSize.w -= childSpacing() * (visibleChildren-1); \
|
|
|
|
if (getAlign() & HOMOGENEOUS) \
|
|
|
|
homogeneousSize = availSize.w / visibleChildren; \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
2015-06-24 01:46:05 +00:00
|
|
|
Rect childPos(getChildrenBounds()); \
|
|
|
|
int i = 0, j = 0; \
|
2015-12-03 22:46:13 +00:00
|
|
|
for (auto child : children()) { \
|
2015-06-24 01:46:05 +00:00
|
|
|
if (child->hasFlags(HIDDEN)) \
|
|
|
|
continue; \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
2015-06-24 01:46:05 +00:00
|
|
|
int size = 0; \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
2015-06-24 01:46:05 +00:00
|
|
|
if (getAlign() & HOMOGENEOUS) { \
|
|
|
|
if (i < visibleChildren-1) \
|
|
|
|
size = homogeneousSize; \
|
|
|
|
else \
|
|
|
|
size = availSize.w; \
|
|
|
|
} \
|
|
|
|
else { \
|
2015-12-04 00:50:05 +00:00
|
|
|
size = child->sizeHint().w; \
|
2012-01-05 22:45:03 +00:00
|
|
|
\
|
2015-06-24 01:46:05 +00:00
|
|
|
if (child->isExpansive()) { \
|
|
|
|
int extraSize = (availExtraSize / (expansiveChildren-j)); \
|
|
|
|
size += extraSize; \
|
|
|
|
availExtraSize -= extraSize; \
|
|
|
|
if (++j == expansiveChildren) \
|
|
|
|
size += availExtraSize; \
|
2012-01-05 22:45:03 +00:00
|
|
|
} \
|
|
|
|
} \
|
2015-06-24 01:46:05 +00:00
|
|
|
\
|
|
|
|
childPos.w = MAX(1, size); \
|
|
|
|
child->setBounds(childPos); \
|
|
|
|
childPos.x += size + childSpacing(); \
|
|
|
|
availSize.w -= size; \
|
|
|
|
++i; \
|
2012-01-05 22:45:03 +00:00
|
|
|
} \
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 20:56:27 +00:00
|
|
|
setBoundsQuietly(ev.getBounds());
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
int visibleChildren = 0;
|
|
|
|
int expansiveChildren = 0;
|
2015-12-03 22:46:13 +00:00
|
|
|
for (auto child : children()) {
|
2015-06-23 19:07:41 +00:00
|
|
|
if (!child->hasFlags(HIDDEN)) {
|
2015-06-24 01:46:05 +00:00
|
|
|
++visibleChildren;
|
2012-04-05 22:00:19 +00:00
|
|
|
if (child->isExpansive())
|
2015-06-24 01:46:05 +00:00
|
|
|
++expansiveChildren;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
if (visibleChildren > 0) {
|
2015-12-04 00:50:05 +00:00
|
|
|
Size prefSize(sizeHint());
|
2015-06-24 01:46:05 +00:00
|
|
|
Size availSize(getChildrenBounds().getSize());
|
|
|
|
int homogeneousSize = 0;
|
|
|
|
int availExtraSize = 0;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2015-06-24 01:46:05 +00:00
|
|
|
prefSize.w -= border().width();
|
|
|
|
prefSize.h -= border().height();
|
|
|
|
|
|
|
|
if (getAlign() & HORIZONTAL) {
|
|
|
|
LAYOUT_CHILDREN(x, w);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LAYOUT_CHILDREN(y, h);
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
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
|