Home » U++ Library support » U++ Library : Other (not classified elsewhere) » [Feature request] Relative positioning
Re: [Feature request] Relative positioning [message #48778 is a reply to message #48644] |
Thu, 14 September 2017 12:27 |
rafiwui
Messages: 105 Registered: June 2017 Location: Stuttgart, Germany
|
Experienced Member |
|
|
Working with my code I realized that there is a big weak point: No widget that is not a direct child of the TopWindow is getting resized.
So I reworked my script and came up with this (IMO better) solution:
#ifndef _AutoResizeCtrl_h_
#define _AutoResizeCtrl_h_
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#include <type_traits>
template<typename T>
class AutoResizeCtrl : public T
{
// Make sure the base class inherits or is Ctrl
static_assert(std::is_base_of<Ctrl, T>::value, "T must inherit from Ctrl");
private:
bool initialization;
bool addedChildren;
Rect initializationRect;
Vector<Rect> childrenInitializationRects;
public:
AutoResizeCtrl<T>()
: T()
{
initialization = true;
addedChildren = false;
}
virtual void Layout()
{
Rect currentRect = GetRect();
if(initialization)
initializationRect = currentRect;
float sizeXChange = (float)currentRect.Width() / (float)initializationRect.Width();
float sizeYChange = (float)currentRect.Height() / (float)initializationRect.Height();
Ctrl* pChild = GetFirstChild();
for(int i = 0; pChild != NULL; i++)
{
if(initialization)
{
childrenInitializationRects.Add(pChild->GetRect());
addedChildren = true;
}
float rectLeft = childrenInitializationRects[i].left * sizeXChange;
float rectTop = childrenInitializationRects[i].top * sizeYChange;
float rectSizeX = childrenInitializationRects[i].Width() * sizeXChange;
float rectSizeY = childrenInitializationRects[i].Height() * sizeYChange;
pChild->SetRect(rectLeft, rectTop,
rectSizeX, rectSizeY);
pChild->Layout();
pChild = pChild->GetNext();
}
initialization = !addedChildren;
}
};
#endif
For an example how to use it please check the attached package.
Greetings
Daniel
[Updated on: Thu, 14 September 2017 12:31] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Wed Apr 24 14:46:35 CEST 2024
Total time taken to generate the page: 0.02447 seconds
|