TGUI 1.13
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/Renderers/TabsRenderer.hpp>
29#include <TGUI/Text.hpp>
30#include <TGUI/Widget.hpp>
31
33
34namespace tgui
35{
42 class TGUI_API TabsBase : public Widget
43 {
44 public:
45 using Ptr = std::shared_ptr<TabsBase>;
46 using ConstPtr = std::shared_ptr<const TabsBase>;
47
55 explicit TabsBase(const char* typeName, bool initRenderer);
56
61 [[nodiscard]] TabsRenderer* getSharedRenderer() override;
62 [[nodiscard]] const TabsRenderer* getSharedRenderer() const override;
63
69 [[nodiscard]] TabsRenderer* getRenderer() override;
70
78 void setEnabled(bool enabled) override;
79
90 std::size_t add(const String& text, bool select = true);
91
99 void insert(std::size_t index, const String& text, bool select = true);
100
108 [[nodiscard]] String getText(std::size_t index) const;
109
118 bool changeText(std::size_t index, const String& text);
119
130 bool changeTextById(const String& id, const String& text);
131
140 void setTabId(std::size_t index, const String& id);
141
151 [[nodiscard]] String getTabId(std::size_t index) const;
152
162 [[nodiscard]] int getIndexById(const String& id) const;
163
173 bool select(const String& text);
174
183 bool select(std::size_t index);
184
197 bool selectById(const String& id);
198
202 void deselect();
203
210 bool remove(const String& text);
211
218 bool remove(std::size_t index);
219
227 bool removeById(const String& id);
228
232 void removeAll();
233
240 [[nodiscard]] String getSelected() const;
241
248 [[nodiscard]] int getSelectedIndex() const;
249
258 [[nodiscard]] String getSelectedId() const;
259
265 [[nodiscard]] int getHoveredIndex() const;
266
272 void setTabVisible(std::size_t index, bool visible);
273
279 [[nodiscard]] bool getTabVisible(std::size_t index) const;
280
286 void setTabEnabled(std::size_t index, bool enabled);
287
293 [[nodiscard]] bool getTabEnabled(std::size_t index) const;
294
300 [[nodiscard]] std::size_t getTabsCount() const;
301
307 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
308
312 void rightMousePressed(Vector2f pos) override;
313
317 void mouseNoLongerOnWidget() override;
318
320
321 protected:
331 [[nodiscard]] Signal& getSignal(String signalName) override;
332
334 // Recalculates the size of each tab image.
336 virtual void recalculateTabsSize() = 0;
337
339 // Update the colors of the text that is drawn on the tabs
341 void updateTextColors();
342
348 void rendererChanged(const String& property) override;
349
353 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
354
358 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
359
363 void updateTextSize() override;
364
366
367 public:
368 SignalString onTabSelect = {"TabSelected"};
369 SignalString onTabRightClick = {"TabRightClicked"};
370
372
373 protected:
374 int m_selectedTab = -1;
375 int m_hoveringTab = -1;
376
377 struct Tab
378 {
379 bool visible;
380 bool enabled;
381 float width;
382 Text text;
383 String id;
384 };
385 std::vector<Tab> m_tabs;
386
387 Sprite m_spriteTab;
388 Sprite m_spriteTabHover;
389 Sprite m_spriteSelectedTab;
390 Sprite m_spriteSelectedTabHover;
391 Sprite m_spriteDisabledTab;
392
393 // Cached renderer properties
394 Borders m_bordersCached;
395 Color m_borderColorCached;
396 Color m_borderColorHoverCached;
397 Color m_selectedBorderColorCached;
398 Color m_selectedBorderColorHoverCached;
399 Color m_backgroundColorCached;
400 Color m_backgroundColorHoverCached;
401 Color m_backgroundColorDisabledCached;
402 Color m_selectedBackgroundColorCached;
403 Color m_selectedBackgroundColorHoverCached;
404 Color m_textColorCached;
405 Color m_textColorHoverCached;
406 Color m_textColorDisabledCached;
407 Color m_selectedTextColorCached;
408 Color m_selectedTextColorHoverCached;
409 float m_distanceToSideCached = 0;
410 float m_roundedBorderRadiusCached = 0;
411
413 };
414
416} // namespace tgui
417
419
420#endif // TGUI_TABS_BASE_HPP
Wrapper for colors.
Definition Color.hpp:63
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:94
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.
bool selectById(const String &id)
Selects the tab with a given id.
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.
void setTabId(std::size_t index, const String &id)
Sets a unique id for a tab.
SignalString onTabRightClick
Right mouse pressed on a tab. Optional parameter: selected item.
Definition TabsBase.hpp:369
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.
bool changeTextById(const String &id, const String &text)
Changes the text of one of the tabs.
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:46
int getSelectedIndex() const
Gets the index of the currently selected tab.
int getIndexById(const String &id) const
Returns the index of a tab that is uniquely identified by the given id.
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...
String getTabId(std::size_t index) const
Returns the unique id for a tab.
void removeAll()
Removes all tabs.
std::shared_ptr< TabsBase > Ptr
Shared widget pointer.
Definition TabsBase.hpp:45
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.
String getSelectedId() const
Gets the id assigned to the currently selected tab.
SignalString onTabSelect
A tab that was selected. Optional parameter: selected item.
Definition TabsBase.hpp:368
bool removeById(const String &id)
Removes a tab with a given id.
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:37
SignalTyped< const String & > SignalString
Signal with one "String" as optional unbound parameter.
Definition Signal.hpp:413
Definition TabsBase.hpp:378