--- --- TGUI: include/TGUI/Widgets/Tabs.hpp Source File
TGUI  1.x-dev
Loading...
Searching...
No Matches
Tabs.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_TAB_HPP
26#define TGUI_TAB_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/TabsRenderer.hpp>
30#include <TGUI/Text.hpp>
31
33
34TGUI_MODULE_EXPORT namespace tgui
35{
42 class TGUI_API Tabs : public Widget
43 {
44 public:
45
46 using Ptr = std::shared_ptr<Tabs>;
47 using ConstPtr = std::shared_ptr<const Tabs>;
48
49 static constexpr const char StaticWidgetType[] = "Tabs";
50
58 Tabs(const char* typeName = StaticWidgetType, bool initRenderer = true);
59
65 TGUI_NODISCARD static Tabs::Ptr create();
66
74 TGUI_NODISCARD static Tabs::Ptr copy(const Tabs::ConstPtr& tab);
75
80 TGUI_NODISCARD TabsRenderer* getSharedRenderer() override;
81 TGUI_NODISCARD const TabsRenderer* getSharedRenderer() const override;
82
88 TGUI_NODISCARD TabsRenderer* getRenderer() override;
89
100 void setSize(const Layout2d& size) override;
101 using Widget::setSize;
102
110 void setEnabled(bool enabled) override;
111
122 void setAutoSize(bool autoSize);
123
129 TGUI_NODISCARD bool getAutoSize() const;
130
141 std::size_t add(const String& text, bool select = true);
142
150 void insert(std::size_t index, const String& text, bool select = true);
151
159 TGUI_NODISCARD String getText(std::size_t index) const;
160
169 bool changeText(std::size_t index, const String& text);
170
180 bool select(const String& text);
181
190 bool select(std::size_t index);
191
195 void deselect();
196
203 bool remove(const String& text);
204
211 bool remove(std::size_t index);
212
216 void removeAll();
217
224 TGUI_NODISCARD String getSelected() const;
225
234 TGUI_NODISCARD int getSelectedIndex() const;
235
241 void setTabVisible(std::size_t index, bool visible);
242
248 TGUI_NODISCARD bool getTabVisible(std::size_t index) const;
249
255 void setTabEnabled(std::size_t index, bool enabled);
256
262 TGUI_NODISCARD bool getTabEnabled(std::size_t index) const;
263
272 void setTabHeight(float height);
273
284 void setMaximumTabWidth(float maximumWidth);
285
296 TGUI_NODISCARD float getMaximumTabWidth() const;
297
307 void setMinimumTabWidth(float minimumWidth);
308
318 TGUI_NODISCARD float getMinimumTabWidth() const;
319
325 TGUI_NODISCARD std::size_t getTabsCount() const;
326
332 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
333
337 bool leftMousePressed(Vector2f pos) override;
338
342 void mouseMoved(Vector2f pos) override;
343
347 void mouseNoLongerOnWidget() override;
348
355 void draw(BackendRenderTarget& target, RenderStates states) const override;
356
358 protected:
359
369 TGUI_NODISCARD Signal& getSignal(String signalName) override;
370
372 // Recalculates the size of each tab image.
374 void recalculateTabsWidth();
375
377 // Update the colors of the text that is drawn on the tabs
379 void updateTextColors();
380
386 void rendererChanged(const String& property) override;
387
391 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
392
396 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
397
401 void updateTextSize() override;
402
404 // Makes a copy of the widget
406 TGUI_NODISCARD Widget::Ptr clone() const override;
407
409 public:
410
411 SignalString onTabSelect = {"TabSelected"};
412
414 protected:
415
416 float m_maximumTabWidth = 0;
417 float m_minimumTabWidth = 0;
418 int m_selectedTab = -1;
419 int m_hoveringTab = -1;
420 bool m_autoSize = true;
421
422 struct Tab
423 {
424 bool visible;
425 bool enabled;
426 float width;
427 Text text;
428 };
429 std::vector<Tab> m_tabs;
430
431 Sprite m_spriteTab;
432 Sprite m_spriteTabHover;
433 Sprite m_spriteSelectedTab;
434 Sprite m_spriteSelectedTabHover;
435 Sprite m_spriteDisabledTab;
436
437 // Cached renderer properties
438 Borders m_bordersCached;
439 Color m_borderColorCached;
440 Color m_borderColorHoverCached;
441 Color m_selectedBorderColorCached;
442 Color m_selectedBorderColorHoverCached;
443 Color m_backgroundColorCached;
444 Color m_backgroundColorHoverCached;
445 Color m_backgroundColorDisabledCached;
446 Color m_selectedBackgroundColorCached;
447 Color m_selectedBackgroundColorHoverCached;
448 Color m_textColorCached;
449 Color m_textColorHoverCached;
450 Color m_textColorDisabledCached;
451 Color m_selectedTextColorCached;
452 Color m_selectedTextColorHoverCached;
453 float m_distanceToSideCached = 0;
454
456 };
457
459}
460
462
463#endif // TGUI_TAB_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Wrapper for colors.
Definition Color.hpp:73
Class to store the position or size of a widget.
Definition Layout.hpp:313
Definition Outline.hpp:38
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Definition Sprite.hpp:47
Wrapper class to store strings.
Definition String.hpp:96
Tabs widget.
Definition Tabs.hpp:43
TGUI_NODISCARD std::size_t getTabsCount() const
Returns the amount of tabs.
TGUI_NODISCARD int getSelectedIndex() const
Gets the index of the currently selected tab.
TGUI_NODISCARD float getMinimumTabWidth() const
Returns the minimum tab width of the tabs.
void setAutoSize(bool autoSize)
Changes whether the tabs are auto-sized or not.
TGUI_NODISCARD float getMaximumTabWidth() const
Returns the maximum tab width of the tabs.
std::size_t add(const String &text, bool select=true)
Adds a new tab.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void insert(std::size_t index, const String &text, bool select=true)
Inserts a new tab somewhere between the other tabs.
bool select(std::size_t index)
Selects the tab with a given index.
void setTabEnabled(std::size_t index, bool enabled)
Changes whether a tab is enabled.
TGUI_NODISCARD TabsRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setEnabled(bool enabled) override
Enables or disables the widget.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer)
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD String getSelected() const
Gets the text that is drawn on the currently selected tab.
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool select(const String &text)
Selects the tab with a given text.
bool changeText(std::size_t index, const String &text)
Changes the text of one of the tabs.
std::shared_ptr< const Tabs > ConstPtr
Shared constant widget pointer.
Definition Tabs.hpp:47
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
void setMaximumTabWidth(float maximumWidth)
Changes the maximum tab width of the tabs.
bool remove(std::size_t index)
Removes a tab with a given index.
void deselect()
Deselects the selected tab.
std::shared_ptr< Tabs > Ptr
Shared widget pointer.
Definition Tabs.hpp:46
void setTabHeight(float height)
Changes the height of the tabs.
TGUI_NODISCARD String getText(std::size_t index) const
Gets the text of one of the tabs.
void setMinimumTabWidth(float minimumWidth)
Changes the minimum tab width of the tabs.
TGUI_NODISCARD bool getTabVisible(std::size_t index) const
Returns whether the tab is shown or hidden.
void setSize(const Layout2d &size) override
Changes the area used by the tabs.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
static TGUI_NODISCARD Tabs::Ptr create()
Creates a new tabs widget.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
TGUI_NODISCARD std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
TGUI_NODISCARD bool getAutoSize() const
Returns whether the tabs are auto-sized or not.
static TGUI_NODISCARD Tabs::Ptr copy(const Tabs::ConstPtr &tab)
Makes a copy of another tab.
bool remove(const String &text)
Removes a tab with a given text.
void removeAll()
Removes all tabs.
void setTabVisible(std::size_t index, bool visible)
Changes whether a tab is visible.
TGUI_NODISCARD bool getTabEnabled(std::size_t index) const
Returns whether the tab is enabled or disabled.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
TGUI_NODISCARD TabsRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Definition TabsRenderer.hpp:35
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:48
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
States used for drawing.
Definition RenderStates.hpp:38
Definition Tabs.hpp:423