TGUI 1.13
Loading...
Searching...
No Matches
ChildWindow.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_CHILD_WINDOW_HPP
26#define TGUI_CHILD_WINDOW_HPP
27
28#include <TGUI/Container.hpp>
29#include <TGUI/CopiedSharedPtr.hpp>
30#include <TGUI/Renderers/ChildWindowRenderer.hpp>
31#include <TGUI/Widgets/Button.hpp>
32
33#include <limits>
34
36
37namespace tgui
38{
42 class TGUI_API ChildWindow : public Container
43 {
44 public:
45 using Ptr = std::shared_ptr<ChildWindow>;
46 using ConstPtr = std::shared_ptr<const ChildWindow>;
47
48 static constexpr char StaticWidgetType[] = "ChildWindow";
49
51 using TitleAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
52
54 enum TitleButton : unsigned int
55 {
56 None = 0,
57 Close = 1 << 0,
58 Maximize = 1 << 1,
59 Minimize = 1 << 2
60 };
61
64 enum class CloseBehavior
65 {
67 Hide,
68 Remove
69 };
70
78 explicit ChildWindow(const char* typeName = StaticWidgetType, bool initRenderer = true);
79
83 ChildWindow(const ChildWindow& other);
84
88 ChildWindow(ChildWindow&& other) noexcept;
89
93 ChildWindow& operator=(const ChildWindow& other);
94
98 ChildWindow& operator=(ChildWindow&& other) noexcept;
99
105 [[nodiscard]] static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
106
114 [[nodiscard]] static ChildWindow::Ptr copy(const ChildWindow::ConstPtr& childWindow);
115
120 [[nodiscard]] ChildWindowRenderer* getSharedRenderer() override;
121 [[nodiscard]] const ChildWindowRenderer* getSharedRenderer() const override;
122
128 [[nodiscard]] ChildWindowRenderer* getRenderer() override;
129
141 void setPosition(const Layout2d& position) override;
143
152 void setSize(const Layout2d& size) override;
153 using Widget::setSize;
154
161 [[nodiscard]] Vector2f getInnerSize() const override;
162
170 virtual void setClientSize(const Layout2d& size);
171
179 [[nodiscard]] Vector2f getClientSize() const;
180
189 void setMaximumSize(Vector2f size);
190
198 [[nodiscard]] Vector2f getMaximumSize() const;
199
208 void setMinimumSize(Vector2f size);
209
217 [[nodiscard]] Vector2f getMinimumSize() const;
218
224 void setTitle(const String& title);
225
231 [[nodiscard]] const String& getTitle() const;
232
238 void setTitleTextSize(unsigned int size);
239
244 [[nodiscard]] unsigned int getTitleTextSize() const;
245
252
259
271 void setTitleButtons(unsigned int buttons);
272
278 [[nodiscard]] unsigned int getTitleButtons() const;
279
289
298 [[nodiscard]] CloseBehavior getCloseBehavior() const;
299
309 void close();
310
317 TGUI_DEPRECATED("Use parent->remove(childWindow) instead") void destroy();
318
324 void setResizable(bool resizable = true);
325
331 [[nodiscard]] bool isResizable() const;
332
339 void setPositionLocked(bool positionLocked = true);
340
345 [[nodiscard]] bool isPositionLocked() const;
346
353 void setKeepInParent(bool enabled = true);
354
355#ifndef TGUI_REMOVE_DEPRECATED_CODE
362 TGUI_DEPRECATED("Use getKeepInParent instead") [[nodiscard]] bool isKeptInParent() const;
363#endif
364
373 [[nodiscard]] bool getKeepInParent() const;
374
381 [[nodiscard]] Vector2f getChildWidgetsOffset() const override;
382
388 void setParent(Container* parent) override;
389
395 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
396
400 bool leftMousePressed(Vector2f pos) override;
401
405 void leftMouseReleased(Vector2f pos) override;
406
410 void rightMousePressed(Vector2f pos) override;
411
415 void rightMouseReleased(Vector2f pos) override;
416
420 void mouseMoved(Vector2f pos) override;
421
425 void keyPressed(const Event::KeyEvent& event) override;
426
436 bool canHandleKeyPress(const Event::KeyEvent& event) override;
437
441 void mouseNoLongerOnWidget() override;
442
446 void leftMouseButtonNoLongerDown() override;
447
454 void draw(BackendRenderTarget& target, RenderStates states) const override;
455
457
458 protected:
460 // Updates the title bar texture, text and buttons after the title bar height has changed.
462 void updateTitleBarHeight();
463
465 // Returns the size of the title bar without its borders
467 [[nodiscard]] Vector2f getInnerTitleBarSize() const;
468
470 // Returns the size of the client area plus the client padding around it
472 [[nodiscard]] Vector2f getInnerSizeWithPadding() const;
473
475 // Returns the difference between the child window size and its client size.
476 // This returns the combined size of the title bar, borders and padding.
478 [[nodiscard]] Vector2f getDecorationSize() const;
479
481 // Updates the mouse cursor for resizable child windows
483 void updateResizeMouseCursor(Vector2f mousePos);
484
494 [[nodiscard]] Signal& getSignal(String signalName) override;
495
501 void rendererChanged(const String& property) override;
502
506 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
507
511 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
512
516 void mouseEnteredWidget() override;
517
521 void mouseLeftWidget() override;
522
524 // Makes a copy of the widget
526 [[nodiscard]] Widget::Ptr clone() const override;
527
529
530 private:
532 // Helper function to connect the title button callbacks from the various constuctors and assignment operators
534 void connectTitleButtonCallbacks();
535
537
538 public:
539 Signal onMousePress = {"MousePressed"};
544 "EscapeKeyPressed"};
545
551
553
554 protected:
555 enum ResizeDirection
556 {
557 ResizeNone = 0,
558 ResizeLeft = 1,
559 ResizeTop = 2,
560 ResizeRight = 4,
561 ResizeBottom = 8
562 };
563
565
566 protected:
567 Text m_titleText;
568 Vector2f m_draggingPosition;
569 Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
570 Vector2f m_minimumSize = {0, 0};
571 Layout* m_decorationLayoutX = nullptr;
572 Layout* m_decorationLayoutY = nullptr;
573 HorizontalAlignment m_titleAlignment = HorizontalAlignment::Center;
574 unsigned int m_titleButtons = TitleButton::Close;
575 unsigned int m_titleTextSize = 0;
576 Cursor::Type m_currentChildWindowMouseCursor = Cursor::Type::Arrow;
577 CloseBehavior m_closeBehavior = CloseBehavior::Remove;
578
579 CopiedSharedPtr<Button> m_closeButton;
580 CopiedSharedPtr<Button> m_minimizeButton;
581 CopiedSharedPtr<Button> m_maximizeButton;
582
583 bool m_mouseDownOnTitleBar = false;
584 bool m_keepInParent = false;
585
586 bool m_positionLocked = false;
587 bool m_resizable = false;
588 int m_resizeDirection = ResizeNone;
589
590 Sprite m_spriteTitleBar;
591 Sprite m_spriteBackground;
592
593 // Cached renderer properties
594 Borders m_bordersCached;
595 Padding m_clientPaddingCached;
596 Color m_borderColorCached;
597 Color m_borderColorFocusedCached;
598 Color m_titleColorCached;
599 Color m_titleBarColorCached;
600 Color m_backgroundColorCached;
601 float m_titleBarHeightCached = 20;
602 float m_borderBelowTitleBarCached = 0;
603 float m_distanceToSideCached = 0;
604 float m_paddingBetweenButtonsCached = 0;
605 float m_minimumResizableBorderWidthCached = 10;
606 bool m_showTextOnTitleButtonsCached = false;
607 };
608} // namespace tgui
609
610#endif // TGUI_CHILD_WINDOW_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition ChildWindowRenderer.hpp:35
Child window widget.
Definition ChildWindow.hpp:43
static ChildWindow::Ptr copy(const ChildWindow::ConstPtr &childWindow)
Makes a copy of another child window.
Signal onMousePress
The mouse went down on the widget.
Definition ChildWindow.hpp:539
void setTitleAlignment(HorizontalAlignment alignment)
Changes the title alignment.
CloseBehavior getCloseBehavior() const
Returns the behavior of closing the window.
void setPositionLocked(bool positionLocked=true)
Changes whether the child window can be moved by dragging its title bar or not.
ChildWindow(const ChildWindow &other)
Copy constructor.
ChildWindowRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition ChildWindow.hpp:48
unsigned int getTitleTextSize() const
Returns the character size of the title.
ChildWindow & operator=(const ChildWindow &other)
Overload of copy assignment operator.
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.
static ChildWindow::Ptr create(const String &title="", unsigned int titleButtons=TitleButton::Close)
Creates a new child window widget.
void setPosition(const Layout2d &position) override
Sets the position of the widget.
Vector2f getMaximumSize() const
Returns the maximum size of the child window.
void setTitleButtons(unsigned int buttons)
Changes the title buttons.
void setTitleTextSize(unsigned int size)
Changes the character size of the title.
CloseBehavior
Definition ChildWindow.hpp:65
Vector2f getChildWidgetsOffset() const override
Returns the distance between the position of the container and a widget that would be drawn inside th...
SignalChildWindow onMaximize
The window was maximized. Optional parameter: pointer to the window.
Definition ChildWindow.hpp:542
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
std::shared_ptr< ChildWindow > Ptr
Shared widget pointer.
Definition ChildWindow.hpp:45
bool isPositionLocked() const
Checks whether the child window can be moved by dragging its title bar or not.
Vector2f getClientSize() const
Returns the client size of the child window.
SignalTyped< bool * > onClosing
Definition ChildWindow.hpp:550
void setMinimumSize(Vector2f size)
Sets the minimum size of the child window.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
bool getKeepInParent() const
Returns whether the child window is kept inside its parent.
void destroy()
Closes the window.
Vector2f getInnerSize() const override
Returns the space available for widgets inside the container.
void setMaximumSize(Vector2f size)
Sets the maximum size of the child window.
void setCloseBehavior(CloseBehavior behavior)
Changes the behavior of closing the window.
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
SignalChildWindow onMinimize
The window was minimized. Optional parameter: pointer to the window.
Definition ChildWindow.hpp:541
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
SignalChildWindow onClose
The window was closed. Optional parameter: pointer to the window.
Definition ChildWindow.hpp:540
TitleButton
Title buttons (use bitwise OR to combine).
Definition ChildWindow.hpp:55
@ None
No buttons.
Definition ChildWindow.hpp:56
@ Maximize
Include a maximize button.
Definition ChildWindow.hpp:58
@ Close
Include a close button.
Definition ChildWindow.hpp:57
@ Minimize
Include a minimize button.
Definition ChildWindow.hpp:59
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
HorizontalAlignment TitleAlignment
Title alignments, possible options for the setTitleAlignment function.
Definition ChildWindow.hpp:51
ChildWindow(ChildWindow &&other) noexcept
Move constructor.
ChildWindowRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
HorizontalAlignment getTitleAlignment() const
Returns the title alignment.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
void setTitle(const String &title)
Changes the title that is displayed in the title bar of the child window.
SignalChildWindow onEscapeKeyPress
The escape key was pressed while the child window was focused. Optional parameter: pointer to the win...
Definition ChildWindow.hpp:543
void setKeepInParent(bool enabled=true)
Sets the child window to be kept inside its parent.
unsigned int getTitleButtons() const
Returns the title bar buttons.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
const String & getTitle() const
Returns the title that is displayed in the title bar of the child window.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
virtual void setClientSize(const Layout2d &size)
Changes the client size of the child window.
bool isKeptInParent() const
Tells whether the child window is kept inside its parent.
Vector2f getMinimumSize() const
Returns the minimum size of the child window.
void close()
Try to close the window.
void setResizable(bool resizable=true)
Changes whether the child window can be resized by dragging its borders or not.
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition ChildWindow.hpp:46
bool isResizable() const
Checks whether the child window can be resized by dragging its borders or not.
ChildWindow & operator=(ChildWindow &&other) noexcept
Overload of move assignment operator.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void setSize(const Layout2d &size) override
Changes the size of the child window.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
Class to store the position or size of a widget.
Definition Layout.hpp:320
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:425
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:250
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:60
KeyPressed event parameters.
Definition Event.hpp:166
States used for drawing.
Definition RenderStates.hpp:38