FifeGUI 0.2.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#include "fifechan/widgets/tab.hpp"
6
7#include "fifechan/font.hpp"
8#include "fifechan/graphics.hpp"
9#include "fifechan/widgets/tabbedarea.hpp"
10
11namespace fcn
12{
13 Tab::Tab()
14 {
15 addMouseListener(this);
16 setLayout(Container::LayoutPolicy::Horizontal);
17 setPadding(6);
18 }
19
20 Tab::~Tab() = default;
21
23 {
25 if (mTabbedArea != nullptr) {
26 mTabbedArea->adjustTabPositions();
27 }
28 }
29
31 {
32 mTabbedArea = tabbedArea;
33 }
34
36 {
37 return mTabbedArea;
38 }
39
41 {
42 Rectangle rec;
43 rec.x = getBorderSize() + getPaddingLeft();
44 rec.y = getBorderSize() + getPaddingTop();
47 return rec;
48 }
49
50 void Tab::draw(Graphics* graphics)
51 {
52 Color const & faceColor = getBaseColor();
53 int const alpha = getBaseColor().a;
54 Color highlightColor = faceColor + 0x303030;
55 highlightColor.a = alpha;
56 Color shadowColor = faceColor - 0x303030;
57 shadowColor.a = alpha;
58
59 Color baseColor;
60
61 if ((mTabbedArea != nullptr && mTabbedArea->isTabSelected(this)) || mHasMouse) {
62 // Draw a border.
63 graphics->setColor(highlightColor);
64 graphics->drawLine(0, 0, getWidth() - 1, 0);
65 graphics->drawLine(0, 1, 0, getHeight() - 1);
66 graphics->setColor(shadowColor);
67 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
68
69 baseColor = getBaseColor();
70 } else {
71 // Draw a border.
72 graphics->setColor(shadowColor);
73 graphics->drawLine(0, 0, getWidth() - 1, 0);
74 graphics->drawLine(0, 1, 0, getHeight() - 1);
75 graphics->drawLine(getWidth() - 1, 1, getWidth() - 1, getHeight() - 1);
76
77 baseColor = getBaseColor();
78 baseColor.a = alpha;
79 }
80
81 // Push a clip area so the other drawings don't need to worry
82 // about the border.
83 graphics->pushClipArea(Rectangle(1, 1, getWidth() - 2, getHeight() - 1));
84 ClipRectangle const & currentClipArea = graphics->getCurrentClipArea();
85
86 graphics->setColor(baseColor);
87 graphics->fillRectangle(0, 0, currentClipArea.width, currentClipArea.height);
88
89 if (mTabbedArea != nullptr && mTabbedArea->isFocused() && mTabbedArea->isTabSelected(this)) {
90 graphics->setColor(Color(0x000000));
91 graphics->drawRectangle(2, 2, currentClipArea.width - 4, currentClipArea.height - 4);
92 }
93
94 graphics->popClipArea();
95 }
96
97 void Tab::mouseEntered(MouseEvent& /*mouseEvent*/)
98 {
99 mHasMouse = true;
100 }
101
102 void Tab::mouseExited(MouseEvent& /*mouseEvent*/)
103 {
104 mHasMouse = false;
105 }
106} // namespace fcn
A rectangle specifically used for clipping rendering regions.
Color.
Definition color.hpp:56
uint8_t a
Alpha color component (0-255).
Definition color.hpp:322
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:57
virtual void popClipArea()
Removes the top most clip area from the stack.
Definition graphics.cpp:55
virtual ClipRectangle const & getCurrentClipArea()
Gets the current clip area.
Definition graphics.cpp:65
virtual bool pushClipArea(Rectangle area)
Pushes a clip area onto the stack.
Definition graphics.cpp:18
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:20
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:40
bool mHasMouse
True if the tab has the mouse, false otherwise.
Definition tab.hpp:76
void mouseExited(MouseEvent &mouseEvent) override
Called when the mouse has exited the widget area.
Definition tab.cpp:102
void setTabbedArea(TabbedArea *tabbedArea)
Sets the tabbed area the tab should be a part of.
Definition tab.cpp:30
void adjustSize() override
Adjusts the size of the tab to fit the caption.
Definition tab.cpp:22
void mouseEntered(MouseEvent &mouseEvent) override
Called when the mouse has entered into the widget area.
Definition tab.cpp:97
TabbedArea * mTabbedArea
Holds the tabbed area the tab is a part of.
Definition tab.hpp:81
TabbedArea * getTabbedArea()
Gets the tabbed are the tab is a part of.
Definition tab.cpp:35
void draw(Graphics *graphics) override
Draws the widget.
Definition tab.cpp:50
A container organizing content into selectable tabs.
Color const & getBaseColor() const
Gets the base color.
Definition widget.cpp:596
int getWidth() const
Gets the width of the widget.
Definition widget.cpp:170
unsigned int getPaddingLeft() const
Gets the left padding.
Definition widget.cpp:477
void addMouseListener(MouseListener *mouseListener)
Adds a mouse listener to the widget.
Definition widget.cpp:754
unsigned int getPaddingTop() const
Gets the top padding.
Definition widget.cpp:447
unsigned int getBorderSize() const
Gets the size of the widget's border.
Definition widget.cpp:381
unsigned int getPaddingBottom() const
Gets the bottom padding.
Definition widget.cpp:467
int getHeight() const
Gets the height of the widget.
Definition widget.cpp:183
unsigned int getPaddingRight() const
Gets the right padding.
Definition widget.cpp:457
void setPadding(unsigned int padding)
Sets all 4 paddings to one value.
Definition widget.cpp:434