6#include "fifechan/widgets/tabbedarea.hpp"
14#include "fifechan/exception.hpp"
15#include "fifechan/focushandler.hpp"
16#include "fifechan/font.hpp"
17#include "fifechan/graphics.hpp"
18#include "fifechan/widgets/container.hpp"
19#include "fifechan/widgets/tab.hpp"
23 TabbedArea::TabbedArea() : mTabContainer(new
Container()), mWidgetContainer(new
Container())
27 addMouseListener(
this);
29 mTabContainer->setOpaque(
false);
30 mTabContainer->setLayout(Container::LayoutPolicy::Horizontal);
32 mWidgetContainer->setLayout(Container::LayoutPolicy::Vertical);
33 mWidgetContainer->setPadding(6);
36 add(mWidgetContainer);
39 TabbedArea::~TabbedArea()
41 remove(mTabContainer);
42 remove(mWidgetContainer);
45 delete mWidgetContainer;
53 if (tab->
getLayout() == Container::LayoutPolicy::Absolute) {
57 mTabs.emplace_back(tab, widget);
68 if (index >=
mTabs.size()) {
77 int tabIndexToBeSelected = -1;
81 int const mTabsSize =
static_cast<int>(
mTabs.size());
82 if (index == mTabsSize - 1 && mTabsSize > 1) {
83 tabIndexToBeSelected = index - 1;
84 }
else if (mTabsSize == 1) {
85 tabIndexToBeSelected = -1;
87 tabIndexToBeSelected = index;
91 auto iter = std::ranges::find_if(
mTabs, [tab](std::pair<Tab*, Widget*>
const & p) {
92 return p.first == tab;
94 if (iter !=
mTabs.end()) {
99 auto iter2 = std::ranges::find_if(
mTabsToDelete, [tab](std::unique_ptr<Tab>
const & t) {
100 return t.get() == tab;
108 if (tabIndexToBeSelected == -1) {
124 if (index >=
mTabs.size()) {
138 if (index >=
mTabs.size()) {
151 for (i = 0; i <
mTabs.size(); i++) {
157 for (i = 0; i <
mTabs.size(); i++) {
158 if (
mTabs.at(i).first == tab) {
162 if (
mTabs.at(i).second !=
nullptr) {
163 if (
mTabs.at(i).second->getParent() !=
nullptr) {
164 if (
auto* parentContainer =
dynamic_cast<Container*
>(
mTabs.at(i).second->getParent())) {
165 parentContainer->remove(
mTabs.at(i).second);
177 auto it = std::ranges::find_if(
mTabs, [
this](
auto const & tab) {
181 if (it !=
mTabs.end()) {
182 return static_cast<int>(std::distance(
mTabs.begin(), it));
218 Color highlightColor = faceColor + 0x303030;
219 highlightColor.
a = alpha;
220 Color shadowColor = faceColor - 0x303030;
221 shadowColor.
a = alpha;
289 int maxTabHeight = 0;
293 for (
auto& mTab :
mTabs) {
296 maxTabWidth = std::max(mTab.first->getWidth(), maxTabWidth);
297 maxTabHeight = std::max(mTab.first->getHeight(), maxTabHeight);
300 if (
getLayout() == Container::LayoutPolicy::Vertical) {
304 }
else if (
getLayout() == Container::LayoutPolicy::Horizontal) {
314 int maxTabHeight = 0;
316 for (i = 0; i <
mTabs.size(); i++) {
317 maxTabWidth = std::max(
mTabs.at(i).first->getWidth(), maxTabWidth);
318 maxTabHeight = std::max(
mTabs.at(i).first->getHeight(), maxTabHeight);
321 if (
getLayout() == Container::LayoutPolicy::Vertical) {
323 for (i = 0; i <
mTabs.size(); i++) {
324 Tab* tab =
mTabs.at(i).first;
328 }
else if (
getLayout() == Container::LayoutPolicy::Horizontal) {
330 for (i = 0; i <
mTabs.size(); i++) {
331 Tab* tab =
mTabs.at(i).first;
393 if (keyEvent.
getKey().getValue() == fcn::Key::LEFT) {
404 }
else if (keyEvent.
getKey().getValue() == fcn::Key::RIGHT) {
408 if (std::cmp_greater_equal(index,
mTabs.size())) {
426 if (mouseEvent.
getButton() == MouseEvent::Button::Left) {
428 Tab* tab =
dynamic_cast<Tab*
>(widget);
430 if (tab !=
nullptr) {
445 Tab* tab =
dynamic_cast<Tab*
>(
event.getSource());
447 if (tab !=
nullptr) {
457 Tab* tab =
dynamic_cast<Tab*
>(source);
459 if (tab ==
nullptr) {
460 throwException(
"Received an action from a widget that's not a tab!");
471 for (
auto const & tabEntry :
mTabs) {
472 tabEntry.first->setBaseColor(color);
Represents an action trigger (e.g., button click).
uint8_t a
Alpha color component (0-255).
A composite widget capable of holding and managing child widgets.
virtual void setLayout(LayoutPolicy policy)
Sets the layout of the container.
LayoutPolicy
The layout policy of the container.
virtual LayoutPolicy getLayout() const
Gets the layout of the container.
Base class for all GUI event objects.
Widget * getSource() const
Gets the source widget of the event.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
virtual void drawLine(int x1, int y1, int x2, int y2)=0
Draws a line.
virtual void setColor(Color const &color)=0
Sets the color to use when drawing.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
Key const & getKey() const
Gets the key of the event.
Represents a mouse event.
int getX() const
Gets the x coordinate of the mouse event.
int getY() const
Gets the y coordinate of the mouse event.
MouseEvent::Button getButton() const
Gets the button of the mouse event.
Represents a rectangular area (X, Y, Width, Height).
int width
Holds the width of the rectangle.
int y
Holds the x coordinate of the rectangle.
int x
Holds the x coordinate of the rectangle.
int height
Holds the height of the rectangle.
void setTabbedArea(TabbedArea *tabbedArea)
Sets the tabbed area the tab should be a part of.
Container * mTabContainer
Holds the container for the tabs.
bool mOpaque
True if the tabbed area is opaque, false otherwise.
virtual bool isUniformSize() const
True if the tab container tries to expand the childs to a uniform size.
virtual unsigned int getHorizontalSpacing() const
Get the horizontal spacing between rows.
virtual unsigned int getVerticalSpacing() const
Get the vertical spacing between rows.
virtual int getSelectedTabIndex() const
Gets the index of the selected tab.
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
void mousePressed(MouseEvent &mouseEvent) override
Called when a mouse button has been pressed down on the widget area.
virtual void setUniformSize(bool uniform)
Enables or disables uniform sizing of child elements.
void setSize(int width, int height) override
Set the size (width and height) of the tabbed area in pixels.
virtual void death(Event const &event)
DeathListener callback invoked when a child widget dies.
virtual bool isTabSelected(unsigned int index) const
Checks if a tab given an index is selected or not.
virtual void setHorizontalSpacing(unsigned int spacing)
Set the horizontal spacing between columns.
void setDimension(Rectangle const &dimension) override
Set the area dimension for the tabbed area.
void resizeToContent(bool recursion=true) override
Resizes the widget's size to fit the content exactly, calls recursively all childs.
virtual void setSelectedTab(unsigned int index)
Sets a tab given an index to be selected.
void setLayout(Container::LayoutPolicy policy)
Sets the layout of the tabbedarea.
void setBaseColor(Color const &color) override
Set the base/background color used for the tabbed area.
virtual void addTab(Tab *tab, Widget *widget)
Adds a tab to the tabbed area.
void action(ActionEvent const &actionEvent) override
Handles an action event emitted by a widget.
void adjustTabPositions()
Adjusts the positions of the tabs.
void keyPressed(KeyEvent &keyEvent) override
Called if a key is pressed when the widget has keyboard focus.
Container * mWidgetContainer
Holds the container for the widgets.
Widget * getBackgroundWidget()
Get the background widget, or nullptr if none is set.
void adjustSize() override
Adjusts the size of the tab container and the widget container.
std::vector< std::unique_ptr< Tab > > mTabsToDelete
Stores tabs owned by this instance for automatic destruction.
void setWidth(int width) override
Set the width of the tabbed area in pixels.
Container::LayoutPolicy getLayout() const
Gets the layout of the tabbedarea.
std::vector< std::pair< Tab *, Widget * > > mTabs
Associates each tab with the widget displayed when it is selected.
void setBackgroundWidget(Widget *widget)
Set the background widget which is drawn behind tabs.
int getNumberOfTabs() const
Returns the number of tabs in this tabbed area.
virtual void removeTabWithIndex(unsigned int index)
Removes a tab from the tabbed area.
virtual void setVerticalSpacing(unsigned int spacing)
Set the vertical spacing between rows.
void setOpaque(bool opaque)
Sets the tabbed area to be opaque or not.
Tab * getSelectedTab() const
Gets the selected tab.
void setHeight(int height) override
Set the height of the tabbed area in pixels.
virtual void removeTab(Tab *tab)
Removes a tab from the tabbed area.
bool isOpaque() const
Checks if the tabbed area is opaque or not.
Tab * mSelectedTab
Holds the selected tab.
void draw(Graphics *graphics) override
Draws the widget.
void expandContent(bool recursion) override
Expands child widgets to fit this widget's size.
Used replacement tokens by configure_file():
void throwException(std::string const &message, std::source_location location=std::source_location::current())
Throw an Exception capturing the current source location.