TGUI  1.5
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
72 ChildWindow(const char* typeName = StaticWidgetType, bool initRenderer = true);
73
78
83
87 ChildWindow& operator= (const ChildWindow&);
88
92 ChildWindow& operator= (ChildWindow&&) noexcept;
93
99 TGUI_NODISCARD static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
100
108 TGUI_NODISCARD static ChildWindow::Ptr copy(const ChildWindow::ConstPtr& childWindow);
109
114 TGUI_NODISCARD ChildWindowRenderer* getSharedRenderer() override;
115 TGUI_NODISCARD const ChildWindowRenderer* getSharedRenderer() const override;
116
122 TGUI_NODISCARD ChildWindowRenderer* getRenderer() override;
123
135 void setPosition(const Layout2d& position) override;
136 using Widget::setPosition;
137
146 void setSize(const Layout2d& size) override;
147 using Widget::setSize;
148
155 TGUI_NODISCARD Vector2f getInnerSize() const override;
156
164 virtual void setClientSize(const Layout2d& size);
165
173 TGUI_NODISCARD Vector2f getClientSize() const;
174
183 void setMaximumSize(Vector2f size);
184
192 TGUI_NODISCARD Vector2f getMaximumSize() const;
193
202 void setMinimumSize(Vector2f size);
203
211 TGUI_NODISCARD Vector2f getMinimumSize() const;
212
218 void setTitle(const String& title);
219
225 TGUI_NODISCARD const String& getTitle() const;
226
232 void setTitleTextSize(unsigned int size);
233
238 TGUI_NODISCARD unsigned int getTitleTextSize() const;
239
245 void setTitleAlignment(HorizontalAlignment alignment);
246
252 TGUI_NODISCARD HorizontalAlignment getTitleAlignment() const;
253
265 void setTitleButtons(unsigned int buttons);
266
272 TGUI_NODISCARD unsigned int getTitleButtons() const;
273
282 void close();
283
290 void destroy();
291
297 void setResizable(bool resizable = true);
298
304 TGUI_NODISCARD bool isResizable() const;
305
312 void setPositionLocked(bool positionLocked = true);
313
318 TGUI_NODISCARD bool isPositionLocked() const;
319
326 void setKeepInParent(bool enabled = true);
327
328#ifndef TGUI_REMOVE_DEPRECATED_CODE
335 TGUI_DEPRECATED("Use getKeepInParent instead") TGUI_NODISCARD bool isKeptInParent() const;
336#endif
337
346 TGUI_NODISCARD bool getKeepInParent() const;
347
354 TGUI_NODISCARD Vector2f getChildWidgetsOffset() const override;
355
361 void setParent(Container* parent) override;
362
368 TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override;
369
373 bool leftMousePressed(Vector2f pos) override;
374
378 void leftMouseReleased(Vector2f pos) override;
379
383 void rightMousePressed(Vector2f pos) override;
384
388 void rightMouseReleased(Vector2f pos) override;
389
393 void mouseMoved(Vector2f pos) override;
394
398 void keyPressed(const Event::KeyEvent& event) override;
399
409 bool canHandleKeyPress(const Event::KeyEvent& event) override;
410
414 void mouseNoLongerOnWidget() override;
415
419 void leftMouseButtonNoLongerDown() override;
420
427 void draw(BackendRenderTarget& target, RenderStates states) const override;
428
430 protected:
431
433 // Updates the title bar texture, text and buttons after the title bar height has changed.
435 void updateTitleBarHeight();
436
438 // Updates the mouse cursor for resizable child windows
440 void updateResizeMouseCursor(Vector2f mousePos);
441
451 TGUI_NODISCARD Signal& getSignal(String signalName) override;
452
458 void rendererChanged(const String& property) override;
459
463 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
464
468 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
469
473 void mouseEnteredWidget() override;
474
478 void mouseLeftWidget() override;
479
481 // Makes a copy of the widget
483 TGUI_NODISCARD Widget::Ptr clone() const override;
484
486 private:
487
489 // Helper function to connect the title button callbacks from the various constuctors and assignment operators
491 void connectTitleButtonCallbacks();
492
494 public:
495
496 Signal onMousePress = {"MousePressed"};
497 SignalChildWindow onClose = {"Closed"};
498 SignalChildWindow onMinimize = {"Minimized"};
499 SignalChildWindow onMaximize = {"Maximized"};
500 SignalChildWindow onEscapeKeyPress = {"EscapeKeyPressed"};
501
506 SignalTyped<bool*> onClosing = {"Closing"};
507
509 protected:
510
511 enum ResizeDirection
512 {
513 ResizeNone = 0,
514 ResizeLeft = 1,
515 ResizeTop = 2,
516 ResizeRight = 4,
517 ResizeBottom = 8
518 };
519
521 protected:
522
523 Text m_titleText;
524 Vector2f m_draggingPosition;
525 Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
526 Vector2f m_minimumSize = {0, 0};
527 Layout* m_decorationLayoutX = nullptr;
528 Layout* m_decorationLayoutY = nullptr;
529 HorizontalAlignment m_titleAlignment = HorizontalAlignment::Center;
530 unsigned int m_titleButtons = TitleButton::Close;
531 unsigned int m_titleTextSize = 0;
532 Cursor::Type m_currentChildWindowMouseCursor = Cursor::Type::Arrow;
533
534 CopiedSharedPtr<Button> m_closeButton;
535 CopiedSharedPtr<Button> m_minimizeButton;
536 CopiedSharedPtr<Button> m_maximizeButton;
537
538 bool m_mouseDownOnTitleBar = false;
539 bool m_keepInParent = false;
540
541 bool m_positionLocked = false;
542 bool m_resizable = false;
543 int m_resizeDirection = ResizeNone;
544
545 Sprite m_spriteTitleBar;
546 Sprite m_spriteBackground;
547
548 // Cached renderer properties
549 Borders m_bordersCached;
550 Color m_borderColorCached;
551 Color m_borderColorFocusedCached;
552 Color m_titleColorCached;
553 Color m_titleBarColorCached;
554 Color m_backgroundColorCached;
555 float m_titleBarHeightCached = 20;
556 float m_borderBelowTitleBarCached = 0;
557 float m_distanceToSideCached = 0;
558 float m_paddingBetweenButtonsCached = 0;
559 float m_minimumResizableBorderWidthCached = 10;
560 bool m_showTextOnTitleButtonsCached = false;
561
563 };
564
566}
567
569
570#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
TGUI_NODISCARD 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.
ChildWindow(const ChildWindow &)
Copy constructor.
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.
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.
TGUI_NODISCARD Vector2f getChildWidgetsOffset() const override
Returns the distance between the position of the container and a widget that would be drawn inside th...
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.
TGUI_NODISCARD bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
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.
TGUI_NODISCARD bool getKeepInParent() const
Returns whether the child window is kept inside its parent.
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition ChildWindow.hpp:49
Container widget.
Definition Container.hpp:48
Class to store the position or size of a widget.
Definition Layout.hpp:313
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:61
KeyPressed event parameters.
Definition Event.hpp:168
States used for drawing.
Definition RenderStates.hpp:38