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
39namespace Pt {
40
41namespace Forms {
42
52{
53 public:
57 : _isPressed(false)
58 {}
59
63 {}
64
67 const String& text() const
68 { return _text; }
69
72 void setText(const String& s)
73 { _text = s; }
74
77 const Gfx::RectF& geometry() const
78 { return _geometry; }
79
82 void setGeometry(const Gfx::RectF& r)
83 { _geometry = r; }
84
87 const Gfx::SizeF& measuredSize() const
88 { return _measuredSize; }
89
92 void setMeasuredSize(const Gfx::SizeF& s)
93 { _measuredSize = s; }
94
97 const Gfx::PointF& textPos() const
98 { return _textPos; }
99
102 void setTextPos(const Gfx::PointF& p)
103 { _textPos = p; }
104
107 bool isPressed() const
108 { return _isPressed; }
109
112 void setPressed(bool b)
113 { _isPressed = b; }
114
115 private:
116 String _text;
117 Gfx::SizeF _measuredSize;
118 Gfx::RectF _geometry;
119 Gfx::PointF _textPos;
120 bool _isPressed;
121};
122
154class PT_FORMS_API TabView : public Control
155{
156 public:
157 typedef Control Base;
158
159 public:
163
166 virtual ~TabView();
167
170 bool empty() const;
171
174 std::size_t size() const;
175
178 void addTab(Control& control, const Pt::String& title);
179
182 void removeTab(std::size_t n);
183
186 std::size_t current() const;
187
190 void setCurrent(std::size_t n);
191
194 void setText(std::size_t n, const Pt::String& title);
195
196 public:
199 void setBackground(const Gfx::Brush& b);
200
204
207 void setContour(const Gfx::Pen& p);
208
211 void setFrame(bool enable);
212
216
219 void setFont(const Gfx::Font& font);
220
223 void setFontSize(std::size_t size);
224
228
232
235 void setTextColor(const Gfx::Color& color);
236
239 void setAccentColor(const Gfx::Color& color);
240
246
247 protected:
250 virtual void onProcessMouseEvent(const MouseEvent& ev);
251
254 virtual void onProcessTouchEvent(const TouchEvent& ev);
255
258 virtual void onInvalidate();
259
262 virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
263
266 virtual void onLayout(const Gfx::RectF& rect);
267
270 virtual void onPaint(PaintContext& context, const Gfx::RectF& updateRect);
271
274 virtual void onPaintBackground(PaintContext& context,
275 const Gfx::RectF& contentRect,
276 const TabViewState& state);
277
280 virtual void onPaintChrome(PaintContext& context,
281 const Gfx::RectF& contentRect,
282 const Gfx::RectF& activeTabRect,
283 const TabViewState& state);
284
285 private:
286 std::size_t hitTab(const Gfx::PointF& pos) const;
287
290 Gfx::SizeF measureTabs(PaintSurface& surface);
291
294 void layoutTabs(PaintSurface& surface, const Gfx::RectF& rect);
295
298 void renderTabs(PaintContext& context, bool enabled);
299
302 void renderTab(PaintContext& context,
303 const Gfx::RectF& tabRect,
304 const Pt::String& text,
305 const Gfx::PointF& textPos,
306 const TabViewItemState& state);
307
308 const Gfx::RectF& currentTabRect() const;
309
310 void onControlRemoved(std::size_t n);
311
312 private:
313 StackLayout _stack;
314 std::vector<TabViewItem> _tabs;
315 std::size_t _current;
316 Gfx::RectF _tabBarRect;
317
318 TabViewStyler _tabViewStyler;
319 bool _hasBackground;
320 bool _hasFrame;
321};
322
323} // namespace
324
325} // namespace
326
327#endif
Control()
Creates an unparented control.
Pointer movement or button change.
Definition MouseEvent.h:134
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Forms render target for painting.
Definition PaintSurface.h:50
Constraint used when measuring a control.
Definition SizePolicy.h:48
Shows one child at a time.
Definition StackLayout.h:65
Transient visual state of a tab.
Definition TabViewStyler.h:84
void setPressed(bool b)
Sets whether the tab is the current tab.
Definition TabView.h:112
void setGeometry(const Gfx::RectF &r)
Sets the tab rectangle to r.
Definition TabView.h:82
const Gfx::PointF & textPos() const
Returns the position of the tab title.
Definition TabView.h:97
TabViewItem()
Constructs an empty item.
Definition TabView.h:56
bool isPressed() const
Returns true if the tab is the current tab.
Definition TabView.h:107
void setText(const String &s)
Sets the tab title to s.
Definition TabView.h:72
const Gfx::RectF & geometry() const
Returns the tab rectangle.
Definition TabView.h:77
~TabViewItem()
Destroys the item.
Definition TabView.h:62
void setMeasuredSize(const Gfx::SizeF &s)
Sets the measured size of the tab label to s.
Definition TabView.h:92
void setTextPos(const Gfx::PointF &p)
Sets the position of the tab title to p.
Definition TabView.h:102
const String & text() const
Returns the tab title.
Definition TabView.h:67
const Gfx::SizeF & measuredSize() const
Returns the measured size of the tab label.
Definition TabView.h:87
Renders the look of a tab view.
Definition TabViewStyler.h:147
Transient visual state of a tab view.
Definition TabViewStyler.h:47
Binds a tab view to the current style renderer.
Definition TabViewStyler.h:259
virtual void onPaint(PaintContext &context, const Gfx::RectF &updateRect)
Paints the background, frame, and tabs.
void removeTab(std::size_t n)
Removes the tab at index n.
void setCurrent(std::size_t n)
Shows the tab at index n.
virtual ~TabView()
Destroys the tab view. Attached controls are not destroyed.
void setRenderer(TabViewRenderer *renderer)
Assigns renderer as the family renderer.
Gfx::Font font() const
Returns the effective font.
bool empty() const
Returns true if the view has no tabs.
void addTab(Control &control, const Pt::String &title)
Adds caller-owned control as a tab with title.
void setFrame(bool enable)
Sets whether the view paints a frame.
virtual void onLayout(const Gfx::RectF &rect)
Places the tabs and current page in rect.
virtual void onPaintBackground(PaintContext &context, const Gfx::RectF &contentRect, const TabViewState &state)
Paints the tab view background for state.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
virtual void onProcessMouseEvent(const MouseEvent &ev)
Selects a tab from a pointer press.
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the tabs and current page.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
std::size_t current() const
Returns the index of the current tab.
void setBackground(bool enable)
Sets whether the view paints a background.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
TabView()
Creates an empty tab view.
virtual void onPaintChrome(PaintContext &context, const Gfx::RectF &contentRect, const Gfx::RectF &activeTabRect, const TabViewState &state)
Paints the tab view frame for state.
std::size_t size() const
Returns the number of tabs.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
virtual void onProcessTouchEvent(const TouchEvent &ev)
Selects a tab from a touch press.
virtual void onInvalidate()
Binds the styler.
void setAccentColor(const Gfx::Color &color)
Sets the widget-local accent color to color.
void setText(std::size_t n, const Pt::String &title)
Sets the title of the tab at index n to title.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Touch press, move, or release.
Definition TouchEvent.h:51
PaintSurface & surface()
Returns this view's local paint-surface adapter.
virtual void enable(bool isEnable=true)
Requests that the widget be enabled or disabled.
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Font family, size and style.
Definition Font.h:63
Slant
Describes the requested font slant.
Definition Font.h:83
Weight
Describes the requested font weight.
Definition Font.h:68
Outline color, width and style.
Definition Pen.h:59
Unicode capable basic_string.
Definition Api-String.h:67
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33