TGUI  1.6.1
Loading...
Searching...
No Matches
ChildWindow.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <limits>
35#endif
36
38
39TGUI_MODULE_EXPORT namespace tgui
40{
44 class TGUI_API ChildWindow : public Container
45 {
46 public:
47
48 using Ptr = std::shared_ptr<ChildWindow>;
49 using ConstPtr = std::shared_ptr<const ChildWindow>;
50
51 static constexpr const char StaticWidgetType[] = "ChildWindow";
52
54 using TitleAlignment TGUI_DEPRECATED("Use tgui::HorizontalAlignment instead") = HorizontalAlignment;
55
57 enum TitleButton : unsigned int
58 {
59 None = 0,
60 Close = 1 << 0,
61 Maximize = 1 << 1,
62 Minimize = 1 << 2
63 };
64
67 enum class CloseBehavior
68 {
69 None,
70 Hide,
71 Remove
72 };
73
81 ChildWindow(const char* typeName = StaticWidgetType, bool initRenderer = true);
82
87
92
96 ChildWindow& operator= (const ChildWindow&);
97
101 ChildWindow& operator= (ChildWindow&&) noexcept;
102
108 TGUI_NODISCARD static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
109
117 TGUI_NODISCARD static ChildWindow::Ptr copy(const ChildWindow::ConstPtr& childWindow);
118
123 TGUI_NODISCARD ChildWindowRenderer* getSharedRenderer() override;
124 TGUI_NODISCARD const ChildWindowRenderer* getSharedRenderer() const override;
125
131 TGUI_NODISCARD ChildWindowRenderer* getRenderer() override;
132
144 void setPosition(const Layout2d& position) override;
145 using Widget::setPosition;
146
155 void setSize(const Layout2d& size) override;
156 using Widget::setSize;
157
164 TGUI_NODISCARD Vector2f getInnerSize() const override;
165
173 virtual void setClientSize(const Layout2d& size);
174
182 TGUI_NODISCARD Vector2f getClientSize() const;
183
192 void setMaximumSize(Vector2f size);
193
201 TGUI_NODISCARD Vector2f getMaximumSize() const;
202
211 void setMinimumSize(Vector2f size);
212
220 TGUI_NODISCARD Vector2f getMinimumSize() const;
221
227 void setTitle(const String& title);
228
234 TGUI_NODISCARD const String& getTitle() const;
235
241 void setTitleTextSize(unsigned int size);
242
247 TGUI_NODISCARD unsigned int getTitleTextSize() const;
248
254 void setTitleAlignment(HorizontalAlignment alignment);
255
261 TGUI_NODISCARD HorizontalAlignment getTitleAlignment() const;
262
274 void setTitleButtons(unsigned int buttons);
275
281 TGUI_NODISCARD unsigned int getTitleButtons() const;
282
291 void setCloseBehavior(CloseBehavior behavior);
292
301 TGUI_NODISCARD CloseBehavior getCloseBehavior() const;
302
312 void close();
313
320 TGUI_DEPRECATED("Use parent->remove(childWindow) instead") void destroy();
321
327 void setResizable(bool resizable = true);
328
334 TGUI_NODISCARD bool isResizable() const;
335
342 void setPositionLocked(bool positionLocked = true);
343
348 TGUI_NODISCARD bool isPositionLocked() const;
349
356 void setKeepInParent(bool enabled = true);
357
358#ifndef TGUI_REMOVE_DEPRECATED_CODE
365 TGUI_DEPRECATED("Use getKeepInParent instead") TGUI_NODISCARD bool isKeptInParent() const;
366#endif
367
376 TGUI_NODISCARD bool getKeepInParent() const;
377
384 TGUI_NODISCARD Vector2f getChildWidgetsOffset() const override;
385
391 void setParent(Container* parent) override;
392
398 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
399
403 bool leftMousePressed(Vector2f pos) override;
404
408 void leftMouseReleased(Vector2f pos) override;
409
413 void rightMousePressed(Vector2f pos) override;
414
418 void rightMouseReleased(Vector2f pos) override;
419
423 void mouseMoved(Vector2f pos) override;
424
428 void keyPressed(const Event::KeyEvent& event) override;
429
439 bool canHandleKeyPress(const Event::KeyEvent& event) override;
440
444 void mouseNoLongerOnWidget() override;
445
449 void leftMouseButtonNoLongerDown() override;
450
457 void draw(BackendRenderTarget& target, RenderStates states) const override;
458
460 protected:
461
463 // Updates the title bar texture, text and buttons after the title bar height has changed.
465 void updateTitleBarHeight();
466
468 // Updates the mouse cursor for resizable child windows
470 void updateResizeMouseCursor(Vector2f mousePos);
471
481 TGUI_NODISCARD Signal& getSignal(String signalName) override;
482
488 void rendererChanged(const String& property) override;
489
493 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
494
498 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
499
503 void mouseEnteredWidget() override;
504
508 void mouseLeftWidget() override;
509
511 // Makes a copy of the widget
513 TGUI_NODISCARD Widget::Ptr clone() const override;
514
516 private:
517
519 // Helper function to connect the title button callbacks from the various constuctors and assignment operators
521 void connectTitleButtonCallbacks();
522
524 public:
525
526 Signal onMousePress = {"MousePressed"};
527 SignalChildWindow onClose = {"Closed"};
528 SignalChildWindow onMinimize = {"Minimized"};
529 SignalChildWindow onMaximize = {"Maximized"};
530 SignalChildWindow onEscapeKeyPress = {"EscapeKeyPressed"};
531
536 SignalTyped<bool*> onClosing = {"Closing"};
537
539 protected:
540
541 enum ResizeDirection
542 {
543 ResizeNone = 0,
544 ResizeLeft = 1,
545 ResizeTop = 2,
546 ResizeRight = 4,
547 ResizeBottom = 8
548 };
549
551 protected:
552
553 Text m_titleText;
554 Vector2f m_draggingPosition;
555 Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
556 Vector2f m_minimumSize = {0, 0};
557 Layout* m_decorationLayoutX = nullptr;
558 Layout* m_decorationLayoutY = nullptr;
559 HorizontalAlignment m_titleAlignment = HorizontalAlignment::Center;
560 unsigned int m_titleButtons = TitleButton::Close;
561 unsigned int m_titleTextSize = 0;
562 Cursor::Type m_currentChildWindowMouseCursor = Cursor::Type::Arrow;
563 CloseBehavior m_closeBehavior = CloseBehavior::Remove;
564
565 CopiedSharedPtr<Button> m_closeButton;
566 CopiedSharedPtr<Button> m_minimizeButton;
567 CopiedSharedPtr<Button> m_maximizeButton;
568
569 bool m_mouseDownOnTitleBar = false;
570 bool m_keepInParent = false;
571
572 bool m_positionLocked = false;
573 bool m_resizable = false;
574 int m_resizeDirection = ResizeNone;
575
576 Sprite m_spriteTitleBar;
577 Sprite m_spriteBackground;
578
579 // Cached renderer properties
580 Borders m_bordersCached;
581 Color m_borderColorCached;
582 Color m_borderColorFocusedCached;
583 Color m_titleColorCached;
584 Color m_titleBarColorCached;
585 Color m_backgroundColorCached;
586 float m_titleBarHeightCached = 20;
587 float m_borderBelowTitleBarCached = 0;
588 float m_distanceToSideCached = 0;
589 float m_paddingBetweenButtonsCached = 0;
590 float m_minimumResizableBorderWidthCached = 10;
591 bool m_showTextOnTitleButtonsCached = false;
592
594 };
595
597}
598
600
601#endif // TGUI_CHILD_WINDOW_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Definition ChildWindowRenderer.hpp:35
Child window widget.
Definition ChildWindow.hpp:45
ChildWindow(const ChildWindow &)
Copy constructor.
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.
CloseBehavior
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...
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:48
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 draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
TitleButton
Title buttons (use bitwise OR to combine)
Definition ChildWindow.hpp:58
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.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition ChildWindow.hpp:49
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
Container widget.
Definition Container.hpp:48
Class to store the position or size of a widget.
Definition Layout.hpp:323
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:440
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:271
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:62
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38