TGUI  0.9-dev
ChildWindow.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 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 
70  // Default constructor
72  ChildWindow(const String& title = "", unsigned int titleButtons = TitleButton::Close);
73 
74 
81  static ChildWindow::Ptr create(const String& title = "", unsigned int titleButtons = TitleButton::Close);
82 
83 
92  static ChildWindow::Ptr copy(ChildWindow::ConstPtr childWindow);
93 
94 
99  ChildWindowRenderer* getSharedRenderer();
100  const ChildWindowRenderer* getSharedRenderer() const;
101 
107  ChildWindowRenderer* getRenderer();
108  const ChildWindowRenderer* getRenderer() const;
109 
110 
123  void setPosition(const Layout2d& position) override;
124  using Widget::setPosition;
125 
126 
135  void setSize(const Layout2d& size) override;
136  using Widget::setSize;
137 
138 
146  void setClientSize(Vector2f size);
147 
148 
156  Vector2f getClientSize() const;
157 
158 
167  void setMaximumSize(Vector2f size);
168 
169 
177  Vector2f getMaximumSize() const;
178 
179 
188  void setMinimumSize(Vector2f size);
189 
190 
198  Vector2f getMinimumSize() const;
199 
200 
207  void setTitle(const String& title);
208 
209 
216  const String& getTitle() const;
217 
218 
224  void setTitleTextSize(unsigned int size);
225 
226 
231  unsigned int getTitleTextSize() const;
232 
233 
240  void setTitleAlignment(TitleAlignment alignment);
241 
242 
249  TitleAlignment getTitleAlignment() const;
250 
251 
264  void setTitleButtons(unsigned int buttons);
265 
266 
273  unsigned int getTitleButtons() const;
274 
275 
281  void close();
282 
283 
292  void destroy();
293 
294 
301  void setResizable(bool resizable = true);
302 
303 
310  bool isResizable() const;
311 
312 
319  void setPositionLocked(bool positionLocked = true);
320 
321 
326  bool isPositionLocked() const;
327 
328 
336  void setKeepInParent(bool enabled = true);
337 
338 
346  bool isKeptInParent() const;
347 
348 
356  Vector2f getChildWidgetsOffset() const override;
357 
358 
364  void setParent(Container* parent) override;
365 
366 
373  bool mouseOnWidget(Vector2f pos) const override;
374 
378  void leftMousePressed(Vector2f pos) override;
379 
383  void leftMouseReleased(Vector2f pos) override;
384 
388  void rightMousePressed(Vector2f pos) override;
389 
393  void rightMouseReleased(Vector2f pos) override;
394 
398  void mouseMoved(Vector2f pos) override;
399 
403  void keyPressed(const sf::Event::KeyEvent& event) override;
404 
408  void mouseNoLongerOnWidget() override;
409 
413  void leftMouseButtonNoLongerDown() override;
414 
415 
423  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
424 
425 
427  protected:
428 
430  // Updates the title bar texture, text and buttons after the title bar height has changed.
432  void updateTitleBarHeight();
433 
434 
444  Signal& getSignal(String signalName) override;
445 
446 
453  void rendererChanged(const String& property) override;
454 
455 
459  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
460 
461 
465  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
466 
467 
469  // Makes a copy of the widget
471  Widget::Ptr clone() const override
472  {
473  return std::make_shared<ChildWindow>(*this);
474  }
475 
476 
478  public:
479 
480  Signal onMousePress = {"MousePressed"};
481  SignalChildWindow onClose = {"Closed"};
482  SignalChildWindow onMinimize = {"Minimized"};
483  SignalChildWindow onMaximize = {"Maximized"};
484  SignalChildWindow onEscapeKeyPress = {"EscapeKeyPressed"};
485 
486 
488  protected:
489 
490  enum ResizeDirection
491  {
492  ResizeNone = 0,
493  ResizeLeft = 1,
494  ResizeTop = 2,
495  ResizeRight = 4,
496  ResizeBottom = 8
497  };
498 
499 
501  protected:
502 
503  Text m_titleText;
504  Vector2f m_draggingPosition;
505  Vector2f m_maximumSize = {std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()};
506  Vector2f m_minimumSize = {0, 0};
507  TitleAlignment m_titleAlignment = TitleAlignment::Center;
508  unsigned int m_titleButtons = TitleButton::Close;
509  unsigned int m_titleTextSize = 0;
510 
511  CopiedSharedPtr<Button> m_closeButton;
512  CopiedSharedPtr<Button> m_minimizeButton;
513  CopiedSharedPtr<Button> m_maximizeButton;
514 
515  bool m_mouseDownOnTitleBar = false;
516  bool m_keepInParent = false;
517 
518  bool m_positionLocked = false;
519  bool m_resizable = false;
520  int m_resizeDirection = ResizeNone;
521 
522  Sprite m_spriteTitleBar;
523  Sprite m_spriteBackground;
524 
525  // Cached renderer properties
526  Borders m_bordersCached;
527  Color m_borderColorCached;
528  Color m_borderColorFocusedCached;
529  Color m_titleColorCached;
530  Color m_titleBarColorCached;
531  Color m_backgroundColorCached;
532  float m_titleBarHeightCached = 20;
533  float m_borderBelowTitleBarCached = 0;
534  float m_distanceToSideCached = 0;
535  float m_paddingBetweenButtonsCached = 0;
536  float m_minimumResizableBorderWidthCached = 5;
537  bool m_showTextOnTitleButtonsCached = false;
538 
540  };
541 
543 }
544 
546 
547 #endif // TGUI_CHILD_WINDOW_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
Container widget.
Definition: Container.hpp:47
TitleButton
Title buttons (use bitwise OR to combine)
Definition: ChildWindow.hpp:61
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:621
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
virtual void setPosition(const Layout2d &position)
sets the position of the widget
Class to store the position or size of a widget.
Definition: Layout.hpp:258
TitleAlignment
Title alignments, possible options for the setTitleAlignment function.
Definition: ChildWindow.hpp:52
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: ChildWindow.hpp:471
std::shared_ptr< const ChildWindow > ConstPtr
Shared constant widget pointer.
Definition: ChildWindow.hpp:48
Child window widget.
Definition: ChildWindow.hpp:43
Wrapper class to store strings.
Definition: String.hpp:70
Definition: ChildWindowRenderer.hpp:36
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< ChildWindow > Ptr
Shared widget pointer.
Definition: ChildWindow.hpp:47