Simple GUI Library
box_container.h
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file box_container.h
4 * @date 2021-11-09
5 *
6 * @copyright Copyright (c) 2021
7 */
8
9#pragma once
10
11#include <unordered_map>
12#include "container.h"
13#include "sys/syscall.h"
14
15namespace Sgl
16{
17 /**
18 * @brief Basic list-like container which lays out children in a line one after another.
19 *
20 * Direction specifies how to lay out children - vertically vs. horizontally and
21 * normal vs. reverse order (still-in-progress).
22 *
23 * Spacing specifies how much space to put between children (@note: t)
24 */
25 class BoxContainer : public Container
26 {
27 public:
28 enum class Direction
29 {
30 LEFT_TO_RIGHT, TOP_TO_BOTTOM
31 };
32
33 enum class GrowPriority
34 {
35 NEVER, ALWAYS
36 };
37
38 public:
39 BoxContainer() = default;
40 BoxContainer(Direction direction);
41 virtual ~BoxContainer() override = default;
42
43 Direction getDirection() const;
44 void setDirection(Direction direction);
45
46 bool getFillAcross() const;
47 void setFillAcross(bool fillAcross);
48
49 int32_t getSpacing() const;
50 void setSpacing(int32_t spacing);
51
52 void setGrowPriority(Component* component, GrowPriority priority);
53 GrowPriority getGrowPriority(Component* component) const;
54
55 void pushBackSpacer(uint32_t weight = 1);
56 void pushFrontSpacer(uint32_t weight = 1);
57
58 private:
59 struct Spacer
60 {
61 Spacer(Component* prevComponent = nullptr, uint32_t weight = 1)
62 : prevComponent(prevComponent), weight(weight) {}
63
64 Component* prevComponent = nullptr; ///< If nullptr than at the beginning.
65 uint32_t weight = 1;
66 };
67
68 private:
69 Direction m_Direction = Direction::LEFT_TO_RIGHT;
70 bool m_FillAcross = false;
71 int32_t m_Spacing = 0;
72
73 std::list<Spacer> m_Spacers;
74
75 /**
76 * By default, only components with GrowPriority::ALWAYS will share free space.
77 * Neither components with GrowPriority::NEVER, nor components with no GrowPriority
78 * set will get any free space.
79 */
80 std::unordered_map<Component*, GrowPriority> m_GrowPriorities;
81
82 private:
83 virtual void layoutChildren() override;
84
85 virtual int32_t computeCustomPrefWidth(int32_t height = -1) const override;
86 virtual int32_t computeCustomPrefHeight(int32_t width = -1) const override;
87
88 virtual int32_t computeCustomMinWidth(int32_t height = -1) const override;
89 virtual int32_t computeCustomMinHeight(int32_t width = -1) const override;
90
91 enum class SizeDimension { WIDTH, HEIGHT };
92
93 int32_t computeTotalPrefSizeWithSpacing(SizeDimension sizeDimension) const;
94 int32_t computeMaximumPrefSize(SizeDimension sizeDimension) const;
95
96 void splitFreeSpace(int32_t freeSpace, size_t countSpacers, size_t countGrowingComponents,
97 int32_t* totalSpacersSpace, int32_t* totalGrowingComponentsSpace) const;
98
99 uint32_t computeTotalSpacersWeight() const;
100 int32_t computeSpacerSize(Component* prev, uint32_t totalWeight, int32_t totalSpacersSize) const;
101 void mergeSpacers(Component* prev);
102
103 size_t computeGrowingComponentsCount(GrowPriority priority) const;
104 };
105
106 class HBox : public BoxContainer
107 {
108 public:
109 HBox();
110 virtual ~HBox() override = default;
111 };
112
113 class VBox : public BoxContainer
114 {
115 public:
116 VBox();
117 virtual ~VBox() override = default;
118 };
119}
Basic list-like container which lays out children in a line one after another.
Definition: box_container.h:26