TGUI  0.9.1
ChildWindow.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2021 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 
26 #ifndef TGUI_CHILD_WINDOW_HPP
27 #define TGUI_CHILD_WINDOW_HPP
28 
29 
30 #include <TGUI/CopiedSharedPtr.hpp>
31 #include <TGUI/Container.hpp>
32 #include <TGUI/Widgets/Button.hpp>
33 #include <TGUI/Renderers/ChildWindowRenderer.hpp>
34 #include <limits>
35 
37 
38 namespace tgui
39 {
43  class TGUI_API ChildWindow : public Container
44  {
45  public:
46 
47  typedef std::shared_ptr<ChildWindow> Ptr;
48  typedef std::shared_ptr<const ChildWindow> ConstPtr;
49 
50 
52  enum class TitleAlignment
53  {
54  Left,
55  Center,
56  Right
57  };
58 
59 
62  {
63  None = 0,
64  Close = 1 << 0,
65  Maximize = 1 << 1,
66  Minimize = 1 << 2
67  };
68 
76  ChildWindow(const char* typeName = "ChildWindow", bool initRenderer = true);
77 
78 
85  static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
86 
87 
97 
98 
104  const ChildWindowRenderer* getSharedRenderer() const;
105 
112  const ChildWindowRenderer* getRenderer() const;
113 
114 
127  void setPosition(const Layout2d& position) override;
128  using Widget::setPosition;
129 
130 
139  void setSize(const Layout2d& size) override;
140  using Widget::setSize;
141 
142 
149  Vector2f getInnerSize() const override;
150 
151 
160 
161 
170 
171 
181 
182 
191 
192 
202 
203 
212 
213 
220  void setTitle(const String& title);
221 
222 
229  const String& getTitle() const;
230 
231 
237  void setTitleTextSize(unsigned int size);
238 
239 
244  unsigned int getTitleTextSize() const;
245 
246 
254 
255 
263 
264 
277  void setTitleButtons(unsigned int buttons);
278 
279 
286  unsigned int getTitleButtons() const;
287 
288 
297  void close();
298 
299 
306  void destroy();
307 
308 
315  void setResizable(bool resizable = true);
316 
317 
324  bool isResizable() const;
325 
326 
333  void setPositionLocked(bool positionLocked = true);
334 
335 
340  bool isPositionLocked() const;
341 
342 
350  void setKeepInParent(bool enabled = true);
351 
352 
360  bool isKeptInParent() const;
361 
362 
370  Vector2f getChildWidgetsOffset() const override;
371 
372 
378  void setParent(Container* parent) override;
379 
380 
387  bool isMouseOnWidget(Vector2f pos) const override;
388 
392  void leftMousePressed(Vector2f pos) override;
393 
397  void leftMouseReleased(Vector2f pos) override;
398 
402  void rightMousePressed(Vector2f pos) override;
403 
407  void rightMouseReleased(Vector2f pos) override;
408 
412  void mouseMoved(Vector2f pos) override;
413 
417  void keyPressed(const Event::KeyEvent& event) override;
418 
422  void mouseNoLongerOnWidget() override;
423 
427  void leftMouseButtonNoLongerDown() override;
428 
429 
437  void draw(BackendRenderTargetBase& target, RenderStates states) const override;
438 
439 
441  protected:
442 
444  // Updates the title bar texture, text and buttons after the title bar height has changed.
446  void updateTitleBarHeight();
447 
448 
450  // Updates the mouse cursor for resizable child windows
452  void updateResizeMouseCursor(Vector2f mousePos);
453 
454 
464  Signal& getSignal(String signalName) override;
465 
466 
473  void rendererChanged(const String& property) override;
474 
475 
479  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
480 
481 
485  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
486 
487 
491  void mouseEnteredWidget() override;
492 
493 
497  void mouseLeftWidget() override;
498 
499 
501  // Makes a copy of the widget
503  Widget::Ptr clone() const override
504  {
505  return std::make_shared<ChildWindow>(*this);
506  }
507 
508 
510  public:
511 
512  Signal onMousePress = {"MousePressed"};
513  SignalChildWindow onClose = {"Closed"};
514  SignalChildWindow onMinimize = {"Minimized"};
515  SignalChildWindow onMaximize = {"Maximized"};
516  SignalChildWindow onEscapeKeyPress = {"EscapeKeyPressed"};
517 
522  SignalTyped<bool*> onClosing = {"Closing"};
523 
524 
526  protected:
527 
528  enum ResizeDirection
529  {
530  ResizeNone = 0,
531  ResizeLeft = 1,
532  ResizeTop = 2,
533  ResizeRight = 4,
534  ResizeBottom = 8
535  };
536 
537 
539  protected:
540 
541  Text m_titleText;
542  Vector2f m_draggingPosition;
543  Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
544  Vector2f m_minimumSize = {0, 0};
545  TitleAlignment m_titleAlignment = TitleAlignment::Center;
546  unsigned int m_titleButtons = TitleButton::Close;
547  unsigned int m_titleTextSize = 0;
548  Cursor::Type m_currentChildWindowMouseCursor = Cursor::Type::Arrow;
549 
550  CopiedSharedPtr<Button> m_closeButton;
551  CopiedSharedPtr<Button> m_minimizeButton;
552  CopiedSharedPtr<Button> m_maximizeButton;
553 
554  bool m_mouseDownOnTitleBar = false;
555  bool m_keepInParent = false;
556 
557  bool m_positionLocked = false;
558  bool m_resizable = false;
559  int m_resizeDirection = ResizeNone;
560 
561  Sprite m_spriteTitleBar;
562  Sprite m_spriteBackground;
563 
564  // Cached renderer properties
565  Borders m_bordersCached;
566  Color m_borderColorCached;
567  Color m_borderColorFocusedCached;
568  Color m_titleColorCached;
569  Color m_titleBarColorCached;
570  Color m_backgroundColorCached;
571  float m_titleBarHeightCached = 20;
572  float m_borderBelowTitleBarCached = 0;
573  float m_distanceToSideCached = 0;
574  float m_paddingBetweenButtonsCached = 0;
575  float m_minimumResizableBorderWidthCached = 10;
576  bool m_showTextOnTitleButtonsCached = false;
577 
579  };
580 
582 }
583 
585 
586 #endif // TGUI_CHILD_WINDOW_HPP
Base class for render targets.
Definition: BackendRenderTarget.hpp:48
Definition: ChildWindowRenderer.hpp:37
Child window widget.
Definition: ChildWindow.hpp:44
void setClientSize(Vector2f size)
Changes the client size of the child window.
void setPositionLocked(bool positionLocked=true)
Changes whether the child window can be moved by dragging its title bar or not.
TitleAlignment
Title alignments, possible options for the setTitleAlignment function.
Definition: ChildWindow.hpp:53
unsigned int getTitleTextSize() const
Returns the character size of the title.
const String & getTitle() const
Returns the title that is displayed in the title bar of the child window.
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.
std::shared_ptr< ChildWindow > Ptr
Shared widget pointer.
Definition: ChildWindow.hpp:47
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.
void setTitleAlignment(TitleAlignment alignment)
Changes the title alignment.
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...
TitleAlignment getTitleAlignment() const
Returns the title alignment.
bool isPositionLocked() const
Checks whether the child window can be moved by dragging its title bar or not.
ChildWindowRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Vector2f getClientSize() const
Returns the client size of the child window.
TitleButton
Title buttons (use bitwise OR to combine)
Definition: ChildWindow.hpp:62
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.
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.
ChildWindowRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void mouseLeftWidget() override
This function is called when the mouse leaves the widget.
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.
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.
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition: ChildWindow.hpp:48
void setKeepInParent(bool enabled=true)
Sets the child window to be kept inside its parent.
unsigned int getTitleButtons() const
Returns the title bar buttons.
void mouseEnteredWidget() override
This function is called when the mouse enters the widget.
void draw(BackendRenderTargetBase &target, RenderStates states) const override
Draw the widget to a render target.
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.
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.
Definition: ChildWindow.hpp:503
void setSize(const Layout2d &size) override
Changes the size of the child window.
static ChildWindow::Ptr copy(ChildWindow::ConstPtr childWindow)
Makes a copy of another child window.
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
Container widget.
Definition: Container.hpp:47
Type
List of available cursors.
Definition: Cursor.hpp:48
@ Arrow
Arrow cursor (default)
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:467
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:58
Wrapper class to store strings.
Definition: String.hpp:74
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:73
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:36
KeyPressed event parameters.
Definition: Event.hpp:167
States used for drawing.
Definition: RenderStates.hpp:39