Otclient  14/8/2020
uihorizontallayout.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 "uihorizontallayout.h"
24 #include "uiwidget.h"
26 
27 
29 {
30  UIBoxLayout::applyStyle(styleNode);
31 
32  for(const OTMLNodePtr& node : styleNode->children()) {
33  if(node->tag() == "align-right")
34  setAlignRight(node->value<bool>());
35  }
36 }
37 
39 {
40  UIWidgetPtr parentWidget = getParentWidget();
41  if(!parentWidget)
42  return false;
43  UIWidgetList widgets = parentWidget->getChildren();
44 
45  if(m_alignRight)
46  std::reverse(widgets.begin(), widgets.end());
47 
48  bool changed = false;
49  Rect paddingRect = parentWidget->getPaddingRect();
50  Point pos = (m_alignRight) ? paddingRect.topRight() : paddingRect.topLeft();
51  int preferredWidth = 0;
52  int gap;
53 
54  for(const UIWidgetPtr& widget : widgets) {
55  if(!widget->isExplicitlyVisible())
56  continue;
57 
58  Size size = widget->getSize();
59 
60  gap = (m_alignRight) ? -(widget->getMarginRight()+widget->getWidth()) : widget->getMarginLeft();
61  pos.x += gap;
62  preferredWidth += gap;
63 
64  if(widget->isFixedSize()) {
65  if(widget->getTextAlign() & Fw::AlignTop) {
66  pos.y = paddingRect.top() + widget->getMarginTop();
67  } else if(widget->getTextAlign() & Fw::AlignBottom) {
68  pos.y = paddingRect.bottom() - widget->getHeight() - widget->getMarginBottom();
69  pos.y = std::max<int>(pos.y, paddingRect.top());
70  } else { // center it
71  pos.y = paddingRect.top() + (paddingRect.height() - (widget->getMarginTop() + widget->getHeight() + widget->getMarginBottom()))/2;
72  pos.y = std::max<int>(pos.y, paddingRect.top());
73  }
74  } else {
75  // expand height
76  size.setHeight(paddingRect.height() - (widget->getMarginTop() + widget->getMarginBottom()));
77  pos.y = paddingRect.top() + (paddingRect.height() - size.height())/2;
78  }
79 
80  if(widget->setRect(Rect(pos - parentWidget->getVirtualOffset(), size)))
81  changed = true;
82 
83  gap = (m_alignRight) ? -widget->getMarginLeft() : (widget->getWidth() + widget->getMarginRight());
84  gap += m_spacing;
85  pos.x += gap;
86  preferredWidth += gap;
87  }
88 
89  preferredWidth -= m_spacing;
90  preferredWidth += parentWidget->getPaddingLeft() + parentWidget->getPaddingRight();
91 
92  if(m_fitChildren && preferredWidth != parentWidget->getWidth()) {
93  // must set the preferred width later
95  parentWidget->setWidth(preferredWidth);
96  });
97  }
98 
99  return changed;
100 }
UIHorizontalLayout::internalUpdate
bool internalUpdate()
Definition: uihorizontallayout.cpp:38
UIWidget::getPaddingRect
Rect getPaddingRect()
Definition: uiwidget.cpp:1054
eventdispatcher.h
TPoint::y
T y
Definition: point.h:83
TRect< int >
g_dispatcher
EventDispatcher g_dispatcher
Definition: eventdispatcher.cpp:28
UIHorizontalLayout::setAlignRight
void setAlignRight(bool aliginRight)
Definition: uihorizontallayout.h:35
uihorizontallayout.h
UIWidget::getPaddingLeft
int getPaddingLeft()
Definition: uiwidget.h:393
OTMLNode::children
OTMLNodeList children()
Definition: otmlnode.cpp:170
TRect::bottom
T bottom() const
Definition: rect.h:55
uiwidget.h
TRect::topRight
TPoint< T > topRight() const
Definition: rect.h:62
UIBoxLayout::m_spacing
int m_spacing
Definition: uiboxlayout.h:45
TRect::topLeft
TPoint< T > topLeft() const
Definition: rect.h:60
TSize::setHeight
void setHeight(T h)
Definition: size.h:48
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::getPaddingRight
int getPaddingRight()
Definition: uiwidget.h:391
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::getWidth
int getWidth()
Definition: uiwidget.h:355
UIWidget::setWidth
void setWidth(int width)
Definition: uiwidget.h:301
UIWidget::getChildren
UIWidgetList getChildren()
Definition: uiwidget.h:257
UIBoxLayout::m_fitChildren
stdext::boolean< false > m_fitChildren
Definition: uiboxlayout.h:44
TSize::height
int height() const
Definition: size.h:44
UIHorizontalLayout::m_alignRight
stdext::boolean< false > m_alignRight
Definition: uihorizontallayout.h:43
stdext::shared_object_ptr< OTMLNode >
Fw::AlignTop
@ AlignTop
Definition: const.h:196
TRect::height
T height() const
Definition: rect.h:70
TRect::top
T top() const
Definition: rect.h:53
Fw::AlignBottom
@ AlignBottom
Definition: const.h:197
TPoint< int >
UIHorizontalLayout::applyStyle
void applyStyle(const OTMLNodePtr &styleNode)
Definition: uihorizontallayout.cpp:28
TSize< int >
UIWidgetList
std::deque< UIWidgetPtr > UIWidgetList
Definition: declarations.h:53