TGUI 1.11
Loading...
Searching...
No Matches
ChildWindow.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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/CopiedSharedPtr.hpp>
29#include <TGUI/Container.hpp>
30#include <TGUI/Widgets/Button.hpp>
31#include <TGUI/Renderers/ChildWindowRenderer.hpp>
32
33#include <limits>
34
36
37namespace tgui
38{
42 class TGUI_API ChildWindow : public Container
43 {
44 public:
45
46 using Ptr = std::shared_ptr<ChildWindow>;
47 using ConstPtr = std::shared_ptr<const ChildWindow>;
48
49 static constexpr const char StaticWidgetType[] = "ChildWindow";
50
52 using TitleAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
53
55 enum TitleButton : unsigned int
56 {
57 None = 0,
58 Close = 1 << 0,
59 Maximize = 1 << 1,
60 Minimize = 1 << 2
61 };
62
65 enum class CloseBehavior
66 {
70 };
71
79 ChildWindow(const char* typeName = StaticWidgetType, bool initRenderer = true);
80
84 ChildWindow(const ChildWindow&);
85
89 ChildWindow(ChildWindow&&) noexcept;
90
94 ChildWindow& operator= (const ChildWindow&);
95
99 ChildWindow& operator= (ChildWindow&&) noexcept;
100
106 TGUI_NODISCARD static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
107
115 TGUI_NODISCARD static ChildWindow::Ptr copy(const ChildWindow::ConstPtr& childWindow);
116
121 TGUI_NODISCARD ChildWindowRenderer* getSharedRenderer() override;
122 TGUI_NODISCARD const ChildWindowRenderer* getSharedRenderer() const override;
123
129 TGUI_NODISCARD ChildWindowRenderer* getRenderer() override;
130
142 void setPosition(const Layout2d& position) override;
143 using Widget::setPosition;
144
153 void setSize(const Layout2d& size) override;
154 using Widget::setSize;
155
162 TGUI_NODISCARD Vector2f getInnerSize() const override;
163
171 virtual void setClientSize(const Layout2d& size);
172
180 TGUI_NODISCARD Vector2f getClientSize() const;
181
190 void setMaximumSize(Vector2f size);
191
199 TGUI_NODISCARD Vector2f getMaximumSize() const;
200
209 void setMinimumSize(Vector2f size);
210
218 TGUI_NODISCARD Vector2f getMinimumSize() const;
219
225 void setTitle(const String& title);
226
232 TGUI_NODISCARD const String& getTitle() const;
233
239 void setTitleTextSize(unsigned int size);
240
245 TGUI_NODISCARD unsigned int getTitleTextSize() const;
246
253
260
272 void setTitleButtons(unsigned int buttons);
273
279 TGUI_NODISCARD unsigned int getTitleButtons() const;
280
290
299 TGUI_NODISCARD CloseBehavior getCloseBehavior() const;
300
310 void close();
311
318 TGUI_DEPRECATED("Use parent->remove(childWindow) instead") void destroy();
319
325 void setResizable(bool resizable = true);
326
332 TGUI_NODISCARD bool isResizable() const;
333
340 void setPositionLocked(bool positionLocked = true);
341
346 TGUI_NODISCARD bool isPositionLocked() const;
347
354 void setKeepInParent(bool enabled = true);
355
356#ifndef TGUI_REMOVE_DEPRECATED_CODE
363 TGUI_DEPRECATED("Use getKeepInParent instead") TGUI_NODISCARD bool isKeptInParent() const;
364#endif
365
374 TGUI_NODISCARD bool getKeepInParent() const;
375
382 TGUI_NODISCARD Vector2f getChildWidgetsOffset() const override;
383
389 void setParent(Container* parent) override;
390
396 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
397
401 bool leftMousePressed(Vector2f pos) override;
402
406 void leftMouseReleased(Vector2f pos) override;
407
411 void rightMousePressed(Vector2f pos) override;
412
416 void rightMouseReleased(Vector2f pos) override;
417
421 void mouseMoved(Vector2f pos) override;
422
426 void keyPressed(const Event::KeyEvent& event) override;
427
437 bool canHandleKeyPress(const Event::KeyEvent& event) override;
438
442 void mouseNoLongerOnWidget() override;
443
447 void leftMouseButtonNoLongerDown() override;
448
455 void draw(BackendRenderTarget& target, RenderStates states) const override;
456
458 protected:
459
461 // Updates the title bar texture, text and buttons after the title bar height has changed.
463 void updateTitleBarHeight();
464
466 // Returns the size of the title bar without its borders
468 TGUI_NODISCARD Vector2f getInnerTitleBarSize() const;
469
471 // Returns the size of the client area plus the client padding around it
473 TGUI_NODISCARD Vector2f getInnerSizeWithPadding() const;
474
476 // Returns the difference between the child window size and its client size.
477 // This returns the combined size of the title bar, borders and padding.
479 TGUI_NODISCARD Vector2f getDecorationSize() const;
480
482 // Updates the mouse cursor for resizable child windows
484 void updateResizeMouseCursor(Vector2f mousePos);
485
495 TGUI_NODISCARD Signal& getSignal(String signalName) override;
496
502 void rendererChanged(const String& property) override;
503
507 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
508
512 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
513
517 void mouseEnteredWidget() override;
518
522 void mouseLeftWidget() override;
523
525 // Makes a copy of the widget
527 TGUI_NODISCARD Widget::Ptr clone() const override;
528
530 private:
531
533 // Helper function to connect the title button callbacks from the various constuctors and assignment operators
535 void connectTitleButtonCallbacks();
536
538 public:
539
540 Signal onMousePress = {"MousePressed"};
544 SignalChildWindow onEscapeKeyPress = {"EscapeKeyPressed"};
545
551
553 protected:
554
555 enum ResizeDirection
556 {
557 ResizeNone = 0,
558 ResizeLeft = 1,
559 ResizeTop = 2,
560 ResizeRight = 4,
561 ResizeBottom = 8
562 };
563
565 protected:
566
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
609 };
610
612}
613
615
616#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:540
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.
ChildWindowRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
ChildWindow(const ChildWindow &)
Copy constructor.
unsigned int getTitleTextSize() const
Returns the character size of the title.
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:66
@ Remove
parent->remove(childWindow) is called after the onClose callback is called
Definition ChildWindow.hpp:69
@ Hide
childWindow->setVisible(false) is called after the onClose callback is called
Definition ChildWindow.hpp:68
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:543
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:46
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:542
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:541
TitleButton
Title buttons (use bitwise OR to combine)
Definition ChildWindow.hpp:56
@ None
No buttons.
Definition ChildWindow.hpp:57
@ Maximize
Include a maximize button.
Definition ChildWindow.hpp:59
@ Close
Include a close button.
Definition ChildWindow.hpp:58
@ Minimize
Include a minimize button.
Definition ChildWindow.hpp:60
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
ChildWindow(ChildWindow &&) noexcept
Move constructor.
HorizontalAlignment TitleAlignment
Title alignments, possible options for the setTitleAlignment function.
Definition ChildWindow.hpp:52
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:544
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.
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition ChildWindow.hpp:49
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:47
bool isResizable() const
Checks whether the child window can be resized by dragging its borders or not.
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:321
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:438
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:269
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
The parent class for every widget.
Definition Widget.hpp:84
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:60
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38