Otclient  14/8/2020
uiverticallayout.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "uiverticallayout.h"
24 #include "uiwidget.h"
26 
28 {
29  UIBoxLayout::applyStyle(styleNode);
30 
31  for(const OTMLNodePtr& node : styleNode->children()) {
32  if(node->tag() == "align-bottom")
33  setAlignBottom(node->value<bool>());
34  }
35 }
36 
38 {
39  bool changed = false;
40 
41  UIWidgetPtr parentWidget = getParentWidget();
42  if(!parentWidget)
43  return false;
44 
45  UIWidgetList widgets = parentWidget->getChildren();
46 
47  if(m_alignBottom)
48  std::reverse(widgets.begin(), widgets.end());
49 
50  Rect paddingRect = parentWidget->getPaddingRect();
51  Point pos = (m_alignBottom) ? paddingRect .bottomLeft() : paddingRect.topLeft();
52  int preferredHeight = 0;
53  int gap;
54 
55  for(const UIWidgetPtr& widget : widgets) {
56  if(!widget->isExplicitlyVisible())
57  continue;
58 
59  Size size = widget->getSize();
60 
61  gap = (m_alignBottom) ? -(widget->getMarginBottom()+widget->getHeight()) : widget->getMarginTop();
62  pos.y += gap;
63  preferredHeight += gap;
64 
65  if(widget->isFixedSize()) {
66  // center it
67  if(widget->getTextAlign() & Fw::AlignLeft) {
68  pos.x = paddingRect.left() + widget->getMarginLeft();
69  } else if(widget->getTextAlign() & Fw::AlignLeft) {
70  pos.x = paddingRect.bottom() - widget->getHeight() - widget->getMarginBottom();
71  pos.x = std::max<int>(pos.x, paddingRect.left());
72  } else {
73  pos.x = paddingRect.left() + (paddingRect.width() - (widget->getMarginLeft() + widget->getWidth() + widget->getMarginRight()))/2;
74  pos.x = std::max<int>(pos.x, paddingRect.left());
75  }
76  } else {
77  // expand width
78  size.setWidth(paddingRect.width() - (widget->getMarginLeft() + widget->getMarginRight()));
79  pos.x = paddingRect.left() + (paddingRect.width() - size.width())/2;
80  }
81 
82  if(widget->setRect(Rect(pos - parentWidget->getVirtualOffset(), size)))
83  changed = true;
84 
85  gap = (m_alignBottom) ? -widget->getMarginTop() : (widget->getHeight() + widget->getMarginBottom());
86  gap += m_spacing;
87  pos.y += gap;
88  preferredHeight += gap;
89  }
90 
91  preferredHeight -= m_spacing;
92  preferredHeight += parentWidget->getPaddingTop() + parentWidget->getPaddingBottom();
93 
94  if(m_fitChildren && preferredHeight != parentWidget->getHeight()) {
95  // must set the preferred width later
97  parentWidget->setHeight(preferredHeight);
98  });
99  }
100 
101  return changed;
102 }
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
UIVerticalLayout::applyStyle
void applyStyle(const OTMLNodePtr &styleNode)
Definition: uiverticallayout.cpp:27
eventdispatcher.h
TPoint::y
T y
Definition: point.h:83
TSize::setWidth
void setWidth(T w)
Definition: size.h:47
TRect< int >
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
TRect::left
T left() const
Definition: rect.h:52
UIWidget::getHeight
int getHeight()
Definition: uiwidget.h:356
UIVerticalLayout::internalUpdate
bool internalUpdate()
Definition: uiverticallayout.cpp:37
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
TRect::bottom
T bottom() const
Definition: rect.h:55
uiwidget.h
TSize::width
int width() const
Definition: size.h:43
UIBoxLayout::m_spacing
int m_spacing
Definition: uiboxlayout.h:45
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
UIWidget::getPaddingBottom
int getPaddingBottom()
Definition: uiwidget.h:392
UIVerticalLayout::m_alignBottom
stdext::boolean< false > m_alignBottom
Definition: uiverticallayout.h:44
UIVerticalLayout::setAlignBottom
void setAlignBottom(bool aliginBottom)
Definition: uiverticallayout.h:36
EventDispatcher::addEvent
EventPtr addEvent(const std::function< void()> &callback, bool pushFront=false)
Definition: eventdispatcher.cpp:104
UIWidget::getVirtualOffset
Point getVirtualOffset()
Definition: uiwidget.h:266
UIWidget::setHeight
void setHeight(int height)
Definition: uiwidget.h:302
TPoint::x
T x
Definition: point.h:83
UIBoxLayout::applyStyle
void applyStyle(const OTMLNodePtr &styleNode)
Definition: uiboxlayout.cpp:31
UILayout::getParentWidget
UIWidgetPtr getParentWidget()
Definition: uilayout.h:46
UIWidget::getChildren
UIWidgetList getChildren()
Definition: uiwidget.h:257
UIBoxLayout::m_fitChildren
stdext::boolean< false > m_fitChildren
Definition: uiboxlayout.h:44
stdext::shared_object_ptr< OTMLNode >
Fw::AlignLeft
@ AlignLeft
Definition: const.h:194
TPoint< int >
TRect::width
T width() const
Definition: rect.h:69
UIWidget::getPaddingTop
int getPaddingTop()
Definition: uiwidget.h:390
TSize< int >
UIWidgetList
std::deque< UIWidgetPtr > UIWidgetList
Definition: declarations.h:53
uiverticallayout.h