TabView.h
1 /* Copyright (C) 2017 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef Pt_Forms_TabView_H
30 #define Pt_Forms_TabView_H
31 
32 #include <Pt/Forms/Control.h>
33 #include <Pt/Forms/TabViewStyler.h>
34 #include <Pt/Forms/StackLayout.h>
35 #include <Pt/Signal.h>
36 
37 #include <vector>
38 
39 namespace Pt {
40 
41 namespace Forms {
42 
46 {
47  public:
48  TabViewItem()
49  : _isPressed(false)
50  {}
51 
52  ~TabViewItem()
53  {}
54 
55  const String& text() const
56  { return _text; }
57 
58  void setText(const String& s)
59  { _text = s; }
60 
61  const Gfx::RectF& geometry() const
62  { return _geometry; }
63 
64  void setGeometry(const Gfx::RectF& r)
65  { _geometry = r; }
66 
67  const Gfx::SizeF& measuredSize() const
68  { return _measuredSize; }
69 
70  void setMeasuredSize(const Gfx::SizeF& s)
71  { _measuredSize = s; }
72 
73  const Gfx::PointF& textPos() const
74  { return _textPos; }
75 
76  void setTextPos(const Gfx::PointF& p)
77  { _textPos = p; }
78 
79  bool isPressed() const
80  { return _isPressed; }
81 
82  void setPressed(bool b)
83  { _isPressed = b; }
84 
85  private:
86  String _text;
87  Gfx::SizeF _measuredSize;
88  Gfx::RectF _geometry;
89  Gfx::PointF _textPos;
90  bool _isPressed;
91 };
92 
95 class PT_FORMS_API TabView : public Control
96 {
97  public:
98  typedef Control Base;
99 
100  public:
101  TabView();
102 
103  virtual ~TabView();
104 
105  bool empty() const;
106 
107  std::size_t size() const;
108 
109  void addTab(Control& control, const Pt::String& title);
110 
111  void removeTab(std::size_t n);
112 
113  std::size_t current() const;
114 
115  void setCurrent(std::size_t n);
116 
117  void setText(std::size_t n, const Pt::String& title);
118 
119  public:
120  void setBackground(const Gfx::Brush& b);
121 
122  void setBackground(bool enable);
123 
124  void setContour(const Gfx::Pen& p);
125 
126  void setFrame(bool enable);
127 
130  Gfx::Font font() const;
131 
132  void setFont(const Gfx::Font& font);
133 
134  void setFontSize(std::size_t size);
135 
136  void setFontWeight(Gfx::Font::Weight weight);
137 
138  void setFontSlant(Gfx::Font::Slant slant);
139 
140  void setTextColor(const Gfx::Color& color);
141 
144  void setAccentColor(const Gfx::Color& color);
145 
146  void setRenderer(TabViewRenderer* renderer);
147 
148  protected:
149  virtual void onProcessMouseEvent(const MouseEvent& ev);
150 
151  virtual void onProcessTouchEvent(const TouchEvent& ev);
152 
153  virtual void onInvalidate();
154 
155  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
156 
157  virtual void onLayout(const Gfx::RectF& rect);
158 
159  virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
160 
161  virtual void onPaintBackground(PaintContext& context,
162  const Gfx::RectF& contentRect,
163  const TabViewState& state);
164 
165  virtual void onPaintChrome(PaintContext& context,
166  const Gfx::RectF& contentRect,
167  const Gfx::RectF& activeTabRect,
168  const TabViewState& state);
169 
170  private:
171  std::size_t hitTab(const Gfx::PointF& pos) const;
172 
175  Gfx::SizeF measureTabs(PaintSurface& surface);
176 
179  void layoutTabs(PaintSurface& surface, const Gfx::RectF& rect);
180 
183  void renderTabs(PaintContext& context, bool enabled);
184 
187  void renderTab(PaintContext& context,
188  const Gfx::RectF& tabRect,
189  const Pt::String& text,
190  const Gfx::PointF& textPos,
191  const TabViewItemState& state);
192 
193  const Gfx::RectF& currentTabRect() const;
194 
195  void onControlRemoved(std::size_t n);
196 
197  private:
198  StackLayout _stack;
199  std::vector<TabViewItem> _tabs;
200  std::size_t _current;
201  Gfx::RectF _tabBarRect;
202 
203  TabViewStyler _tabViewStyler;
204  bool _hasBackground;
205  bool _hasFrame;
206 };
207 
208 } // namespace
209 
210 } // namespace
211 
212 #endif
Core module.
Definition: Allocator.h:33
Attributes for the drawing of outlines.
Definition: Pen.h:54
Paint surface.
Definition: PaintSurface.h:44
Slant
Describes the requested font slant.
Definition: Font.h:76
Paint context.
Definition: PaintContext.h:45
Size with floating-point width and height.
Definition: Size.h:47
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Renders tab view chrome and tab items.
Definition: TabViewStyler.h:89
Weight
Describes the requested font weight.
Definition: Font.h:61
Font request used for text drawing and measurement.
Definition: Font.h:56
Standard color type.
Definition: Color.h:47
Gfx::Font font() const
Returns the effective tab view font.
Binds tab view renderers and local style options.
Definition: TabViewStyler.h:160
Unicode capable basic_string.
Definition: Api-String.h:44
void setAccentColor(const Gfx::Color &color)
Sets the local accent color for the active tab.
Item for tab bars.
Definition: TabView.h:46
Fill description for shapes and text.
Definition: Brush.h:145
Rect with floating-point coordinates.
Definition: Rect.h:47
Tabbed view for controls.
Definition: TabView.h:96