FifeGUI 0.3.0
A C++ GUI library designed for games.
tab.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2004 - 2008 Olof Naessén and Per Larsson
3// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
4
5// Corresponding header include
6#include "fifechan/widgets/tab.hpp"
7
8// Project headers (subdirs before local)
9#include "fifechan/font.hpp"
10#include "fifechan/graphics.hpp"
11#include "fifechan/widgets/tabbedarea.hpp"
12
13namespace fcn
14{
15 Tab::Tab()
16 {
17 addMouseListener(this);
18 setLayout(Container::LayoutPolicy::Horizontal);
19 setPadding(6);
20 }
21
22 Tab::~Tab() = default;
23
25 {
27 if (mTabbedArea != nullptr) {
28 mTabbedArea->adjustTabPositions();
29 }
30 }
31
33 {
34 mTabbedArea = tabbedArea;
35 }
36
38 {
39 return mTabbedArea;
40 }
41
43 {
44 Rectangle rec;
45 rec.x = getBorderSize() + getPaddingLeft();
46 rec.y = getBorderSize() + getPaddingTop();
49 return rec;
50 }
51
52 void Tab::draw(Graphics* graphics)
53 {
54 Color const & faceColor = getBaseColor();
55 int const alpha = getBaseColor().a;
56 Color highlightColor = faceColor + 0x303030;
57 highlightColor.a = alpha;
58 Color shadowColor = faceColor - 0x303030;
59 shadowColor.a = alpha;
60
61 Color baseColor;
62
63 if ((mTabbedArea != nullptr && mTabbedArea->isTabSelected(this)) || mHasMouse) {
64 // Draw a border.
65 graphics->setColor(highlightColor);
66 graphics->drawLine(0, 0, getWidth() - 1, 0);
67 graphics->drawLine(0, 1, 0, getHeight() - 1);
68 graphics->setColor(shadowColor);
69 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
70
71 baseColor = getBaseColor();
72 } else {
73 // Draw a border.
74 graphics->setColor(shadowColor);
75 graphics->drawLine(0, 0, getWidth() - 1, 0);
76 graphics->drawLine(0, 1, 0, getHeight() - 1);
77 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
78
79 baseColor = getBaseColor();
80 baseColor.a = alpha;
81 }
82
83 // Push a clip area so the other drawings don't need to worry
84 // about the border.
85 graphics->pushClipArea(Rectangle(1, 1, getWidth() - 2, getHeight() - 1));
86 ClipRectangle const & currentClipArea = graphics->getCurrentClipArea();
87
88 graphics->setColor(baseColor);
89 graphics->fillRectangle(0, 0, currentClipArea.width, currentClipArea.height);
90
91 if (mTabbedArea != nullptr && mTabbedArea->isFocused() && mTabbedArea->isTabSelected(this)) {
92 graphics->setColor(Color(0x000000));
93 graphics->drawRectangle(2, 2, currentClipArea.width - 4, currentClipArea.height - 4);
94 }
95
96 graphics->popClipArea();
97 }
98
99 void Tab::mouseEntered(MouseEvent& /*mouseEvent*/)
100 {
101 mHasMouse = true;
102 }
103
104 void Tab::mouseExited(MouseEvent& /*mouseEvent*/)
105 {
106 mHasMouse = false;
107 }
108} // namespace fcn
A rectangle specifically used for clipping rendering regions.
Color.
Definition color.hpp:58
uint8_t a
Alpha color component (0-255).
Definition color.hpp:350
virtual void setLayout(LayoutPolicy policy)
Sets the layout of the container.
void adjustSize() override
Adjust the size of the container after layout computations.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:65
virtual ClipRectangle const & getCurrentClipArea()
Gets the current clip area.
Definition graphics.cpp:74
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:25
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 drawRectangle(Rectangle const &rectangle)=0
Draws a simple, non-filled rectangle with a one pixel width.
virtual void fillRectangle(Rectangle const &rectangle)=0
Draws a filled rectangle.
Represents a mouse event.
Represents a rectangular area (X, Y, Width, Height).
Definition rectangle.hpp:22
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.
Rectangle getChildrenArea() override
Gets the area of the widget occupied by the widget's children.
Definition tab.cpp:42
bool mHasMouse
True if the tab has the mouse, false otherwise.
Definition tab.hpp:80
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
Definition tab.cpp:104
void setTabbedArea(TabbedArea *tabbedArea)
Sets the tabbed area the tab should be a part of.
Definition tab.cpp:32
void adjustSize() override
Adjusts the size of the tab to fit the caption.
Definition tab.cpp:24
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
Definition tab.cpp:99
TabbedArea * mTabbedArea
Holds the tabbed area the tab is a part of.
Definition tab.hpp:85
TabbedArea * getTabbedArea()
Gets the tabbed are the tab is a part of.
Definition tab.cpp:37
void draw(Graphics *graphics) override
Draws the widget.
Definition tab.cpp:52
A container organizing content into selectable tabs.
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:742
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:252
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:604
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:907
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:574
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:474
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:594
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:265
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:584
void setPadding(unsigned int padding)
Sets all 4 paddings to one value.
Definition widget.cpp:561
Used replacement tokens by configure_file():