TGUI 1.12
Loading...
Searching...
No Matches
TabsBase.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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_TABS_BASE_HPP
26#define TGUI_TABS_BASE_HPP
27
28#include <TGUI/Widget.hpp>
29#include <TGUI/Renderers/TabsRenderer.hpp>
30#include <TGUI/Text.hpp>
31
33
34namespace tgui
35{
42 class TGUI_API TabsBase : public Widget
43 {
44 public:
45
46 using Ptr = std::shared_ptr<TabsBase>;
47 using ConstPtr = std::shared_ptr<const TabsBase>;
48
56 explicit TabsBase(const char* typeName, bool initRenderer);
57
62 TGUI_NODISCARD TabsRenderer* getSharedRenderer() override;
63 TGUI_NODISCARD const TabsRenderer* getSharedRenderer() const override;
64
70 TGUI_NODISCARD TabsRenderer* getRenderer() override;
71
79 void setEnabled(bool enabled) override;
80
91 std::size_t add(const String& text, bool select = true);
92
100 void insert(std::size_t index, const String& text, bool select = true);
101
109 TGUI_NODISCARD String getText(std::size_t index) const;
110
119 bool changeText(std::size_t index, const String& text);
120
130 bool select(const String& text);
131
140 bool select(std::size_t index);
141
145 void deselect();
146
153 bool remove(const String& text);
154
161 bool remove(std::size_t index);
162
166 void removeAll();
167
174 TGUI_NODISCARD String getSelected() const;
175
184 TGUI_NODISCARD int getSelectedIndex() const;
185
191 TGUI_NODISCARD int getHoveredIndex() const;
192
198 void setTabVisible(std::size_t index, bool visible);
199
205 TGUI_NODISCARD bool getTabVisible(std::size_t index) const;
206
212 void setTabEnabled(std::size_t index, bool enabled);
213
219 TGUI_NODISCARD bool getTabEnabled(std::size_t index) const;
220
226 TGUI_NODISCARD std::size_t getTabsCount() const;
227
233 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
234
238 void rightMousePressed(Vector2f pos) override;
239
243 void mouseNoLongerOnWidget() override;
244
246 protected:
247
257 TGUI_NODISCARD Signal& getSignal(String signalName) override;
258
260 // Recalculates the size of each tab image.
262 virtual void recalculateTabsSize() = 0;
263
265 // Update the colors of the text that is drawn on the tabs
267 void updateTextColors();
268
274 void rendererChanged(const String& property) override;
275
279 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
280
284 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
285
289 void updateTextSize() override;
290
292 public:
293
294 SignalString onTabSelect = {"TabSelected"};
295 SignalString onTabRightClick = {"TabRightClicked"};
296
298 protected:
299
300 int m_selectedTab = -1;
301 int m_hoveringTab = -1;
302
303 struct Tab
304 {
305 bool visible;
306 bool enabled;
307 float width;
308 Text text;
309 };
310 std::vector<Tab> m_tabs;
311
312 Sprite m_spriteTab;
313 Sprite m_spriteTabHover;
314 Sprite m_spriteSelectedTab;
315 Sprite m_spriteSelectedTabHover;
316 Sprite m_spriteDisabledTab;
317
318 // Cached renderer properties
319 Borders m_bordersCached;
320 Color m_borderColorCached;
321 Color m_borderColorHoverCached;
322 Color m_selectedBorderColorCached;
323 Color m_selectedBorderColorHoverCached;
324 Color m_backgroundColorCached;
325 Color m_backgroundColorHoverCached;
326 Color m_backgroundColorDisabledCached;
327 Color m_selectedBackgroundColorCached;
328 Color m_selectedBackgroundColorHoverCached;
329 Color m_textColorCached;
330 Color m_textColorHoverCached;
331 Color m_textColorDisabledCached;
332 Color m_selectedTextColorCached;
333 Color m_selectedTextColorHoverCached;
334 float m_distanceToSideCached = 0;
335 float m_roundedBorderRadiusCached = 0;
336
338 };
339
341}
342
344
345#endif // TGUI_TABS_BASE_HPP
Wrapper for colors.
Definition Color.hpp:71
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Definition Sprite.hpp:45
Wrapper class to store strings.
Definition String.hpp:93
std::size_t getTabsCount() const
Returns the amount of tabs.
bool getTabEnabled(std::size_t index) const
Returns whether the tab is enabled or disabled.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
void setTabVisible(std::size_t index, bool visible)
Changes whether a tab is visible.
void deselect()
Deselects the selected tab.
bool getTabVisible(std::size_t index) const
Returns whether the tab is shown or hidden.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
int getHoveredIndex() const
Gets the index of the tab below the mouse cursor.
SignalString onTabRightClick
Right mouse pressed on a tab. Optional parameter: selected item.
Definition TabsBase.hpp:295
void setEnabled(bool enabled) override
Enables or disables the widget.
void insert(std::size_t index, const String &text, bool select=true)
Inserts a new tab somewhere between the other tabs.
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.
TabsRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
String getSelected() const
Gets the text that is drawn on the currently selected tab.
std::shared_ptr< const TabsBase > ConstPtr
Shared constant widget pointer.
Definition TabsBase.hpp:47
int getSelectedIndex() const
Gets the index of the currently selected tab.
bool remove(std::size_t index)
Removes a tab with a given index.
bool changeText(std::size_t index, const String &text)
Changes the text of one of the tabs.
bool remove(const String &text)
Removes a tab with a given text.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void removeAll()
Removes all tabs.
std::shared_ptr< TabsBase > Ptr
Shared widget pointer.
Definition TabsBase.hpp:46
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.
bool select(const String &text)
Selects the tab with a given text.
TabsRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
String getText(std::size_t index) const
Gets the text of one of the tabs.
std::size_t add(const String &text, bool select=true)
Adds a new tab.
SignalString onTabSelect
A tab that was selected. Optional parameter: selected item.
Definition TabsBase.hpp:294
Definition TabsRenderer.hpp:35
Backend-independent wrapper around the backend-specific text class.
Definition Text.hpp:53
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
SignalTyped< const String & > SignalString
Signal with one "String" as optional unbound parameter.
Definition Signal.hpp:426
Definition TabsBase.hpp:304