Show cursor on exit
[utui.git] / layout.h
blobf00579cb22c3f9bd494768184806f1b09bc68e4b
1 // This file is part of the utui library, a terminal UI framework.
2 //
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // layout.h
7 //
9 #ifndef LAYOUT_H_75D6286B51F3FD37071DA7E92C97A86E
10 #define LAYOUT_H_75D6286B51F3FD37071DA7E92C97A86E
12 #include "autowig.h"
14 //----------------------------------------------------------------------
16 /// Specifies layout axis.
17 enum ELayoutDir {
18 ld_Horizontal,
19 ld_Vertical
22 /// Specifies gravity for alignment.
23 enum ELayoutAlign {
24 la_Left,
25 la_Top = la_Left,
26 la_Center,
27 la_Right,
28 la_Bottom = la_Right
31 //----------------------------------------------------------------------
33 /// \class CLayout layout.h layout.h
34 ///
35 /// \brief Helps windows layout children.
36 ///
37 /// Each CLayout maintains a non-owning list of other layout items, which may
38 /// be of type CLayout or \ref CWindow. Rectangles passed to layout items for
39 /// measurement and resizing are not relative to this object's rect. This can
40 /// allow nesting of CLayout items.
41 ///
42 class CLayout {
43 public:
44 typedef vector<CLayout*> itemvec_t;
45 typedef const Rect& rcrect_t;
46 typedef Rect& rrect_t;
47 typedef const Point2d& rcpos_t;
48 public:
49 CLayout (void);
50 virtual ~CLayout (void);
51 inline void AddLayoutItem (CLayout* pi) { m_Items.push_back (pi); }
52 virtual void OnResize (rcrect_t r);
53 virtual void SizeHints (rrect_t wr) const;
54 protected:
55 inline size_t NLI (void) const { return (m_Items.size()); }
56 inline CLayout& LI (uoff_t i) { return (*m_Items[i]); }
57 inline const CLayout& LI (uoff_t i) const { return (*m_Items[i]); }
58 void DeleteAllLayoutItems (void);
59 private:
60 itemvec_t m_Items; ///< Vector of all the items to be laid out (non-owning)
63 //----------------------------------------------------------------------
65 /// \class CTileLayout layout.h layout.h
66 /// \brief Tiles items along a single axis.
67 class CTileLayout : public CLayout {
68 public:
69 CTileLayout (ELayoutDir dir = ld_Horizontal, uint8_t spacing = 1, uint8_t lmargin = 0, uint8_t rmargin = 0);
70 protected:
71 virtual void OnResize (rcrect_t r);
72 virtual void SizeHints (rrect_t wr) const;
73 private:
74 uint8_t m_Dir; ///< Alignment axis direction. See \ref ELayoutDir
75 uint8_t m_Spacing; ///< Interitem spacing.
76 uint8_t m_LMargin; ///< Margin to add at the beginning of the axis.
77 uint8_t m_RMargin; ///< Margin to add at the end of the axis.
80 #define LAYOUT_TILE(ncw, dir, spacing, lmargin, rmargin) \
81 { ncw, &TWidgetFactory<CTileLayout>, WIDIVAL_AB(dir, spacing, lmargin, rmargin) },
82 template <> inline pwidget_t TWidgetFactory<CTileLayout> (widival_t v)
83 { return (new CTileLayout (ELayoutDir(v.ab[0]), v.ab[1], v.ab[2], v.ab[3])); }
85 //----------------------------------------------------------------------
87 /// \class CAlignLayout layout.h layout.h
88 /// \brief Aligns one item left, center, or right along one axis.
89 class CAlignLayout : public CLayout {
90 public:
91 CAlignLayout (ELayoutDir dir = ld_Horizontal, ELayoutAlign where = la_Left, uint8_t lmargin = 0, uint8_t rmargin = 0);
92 protected:
93 virtual void OnResize (rcrect_t r);
94 virtual void SizeHints (rrect_t wr) const;
95 private:
96 uint8_t m_Dir; ///< Alignment axis direction. See \ref ELayoutDir
97 uint8_t m_Where; ///< Type of alignment to perform. See \ref ELayoutAlign
98 uint8_t m_LMargin; ///< Margin to add at the beginning of the axis.
99 uint8_t m_RMargin; ///< Margin to add at the end of the axis.
102 #define LAYOUT_ALIGN(ncw, dir, where, lmargin, rmargin) \
103 { ncw, &TWidgetFactory<CAlignLayout>, WIDIVAL_AB(dir, where, lmargin, rmargin) },
104 template <> inline pwidget_t TWidgetFactory<CAlignLayout> (widival_t v)
105 { return (new CAlignLayout (ELayoutDir(v.ab[0]), ELayoutAlign(v.ab[1]), v.ab[2], v.ab[3])); }
107 //----------------------------------------------------------------------
109 /// \class CBorderLayout layout.h layout.h
110 /// \brief Embeds one item inside a border of width 1.
111 class CBorderLayout : public CLayout {
112 protected:
113 virtual void OnResize (rcrect_t r);
114 virtual void SizeHints (rrect_t wr) const;
117 #define LAYOUT_BORDER(ncw) \
118 { ncw, &TWidgetFactory<CBorderLayout>, {0} },
119 template <> inline pwidget_t TWidgetFactory<CBorderLayout> (widival_t)
120 { return (new CBorderLayout); }
122 //----------------------------------------------------------------------
124 #endif