1// Aseprite UI Library
2// Copyright (C) 2019-2022 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef UI_WINDOW_H_INCLUDED
9#define UI_WINDOW_H_INCLUDED
10#pragma once
11
12#include "gfx/point.h"
13#include "obs/signal.h"
14#include "ui/close_event.h"
15#include "ui/event.h"
16#include "ui/hit_test_event.h"
17#include "ui/widget.h"
18
19namespace ui {
20
21 class ButtonBase;
22 class Label;
23
24 class Window : public Widget {
25 public:
26 enum Type { DesktopWindow, WithTitleBar, WithoutTitleBar };
27
28 explicit Window(Type type, const std::string& text = "");
29 ~Window();
30
31 bool ownDisplay() const { return m_ownDisplay; }
32 Display* display() const;
33 void setDisplay(Display* display, const bool own);
34 bool hasDisplaySet() const { return m_display != nullptr; }
35
36 Widget* closer() const { return m_closer; }
37
38 void setAutoRemap(bool state);
39 void setMoveable(bool state);
40 void setSizeable(bool state);
41 void setOnTop(bool state);
42 void setWantFocus(bool state);
43
44 void remapWindow();
45 void centerWindow(Display* parentDisplay = nullptr);
46 void moveWindow(const gfx::Rect& rect);
47
48 // Expands or shrink the window to the given side (generally sizeHint())
49 void expandWindow(const gfx::Size& size);
50
51 void openWindow();
52 void openWindowInForeground();
53 void closeWindow(Widget* closer);
54
55 bool isTopLevel();
56 bool isForeground() const { return m_isForeground; }
57 bool isDesktop() const { return m_isDesktop; }
58 bool isOnTop() const { return m_isOnTop; }
59 bool isWantFocus() const { return m_isWantFocus; }
60 bool isSizeable() const { return m_isSizeable; }
61 bool isMoveable() const { return m_isMoveable; }
62
63 bool shouldCreateNativeWindow() const {
64 return !isDesktop();
65 }
66
67 HitTest hitTest(const gfx::Point& point);
68
69 // Last native window frame bounds. Saved just before we close the
70 // native window so we can save this information in the
71 // configuration file.
72 gfx::Rect lastNativeFrame() const { return m_lastFrame; }
73 void loadNativeFrame(const gfx::Rect& frame);
74
75 // Esc key closes the current window (presses the little close
76 // button decorator) only on foreground windows, but you can
77 // override this to allow this behavior in other kind of windows.
78 virtual bool shouldProcessEscKeyToCloseWindow() const {
79 return isForeground();
80 }
81
82 // Signals
83 obs::signal<void (Event&)> Open;
84 obs::signal<void (CloseEvent&)> Close;
85
86 protected:
87 ButtonBase* closeButton() { return m_closeButton; }
88
89 virtual bool onProcessMessage(Message* msg) override;
90 virtual void onInvalidateRegion(const gfx::Region& region) override;
91 virtual void onResize(ResizeEvent& ev) override;
92 virtual void onSizeHint(SizeHintEvent& ev) override;
93 virtual void onBroadcastMouseMessage(const gfx::Point& screenPos,
94 WidgetsList& targets) override;
95 virtual void onSetText() override;
96
97 // New events
98 virtual void onOpen(Event& ev);
99 virtual void onBeforeClose(CloseEvent& ev) {}
100 virtual void onClose(CloseEvent& ev);
101 virtual void onHitTest(HitTestEvent& ev);
102 virtual void onWindowResize();
103 virtual void onWindowMovement();
104 virtual void onBuildTitleLabel();
105
106 private:
107 void windowSetPosition(const gfx::Rect& rect);
108 int getAction(int x, int y);
109 void limitSize(int* w, int* h);
110 void moveWindow(const gfx::Rect& rect, bool use_blit);
111
112 Display* m_display;
113 Widget* m_closer;
114 Label* m_titleLabel;
115 ButtonBase* m_closeButton;
116 bool m_ownDisplay : 1;
117 bool m_isDesktop : 1;
118 bool m_isMoveable : 1;
119 bool m_isSizeable : 1;
120 bool m_isOnTop : 1;
121 bool m_isWantFocus : 1;
122 bool m_isForeground : 1;
123 bool m_isAutoRemap : 1;
124 int m_hitTest;
125 gfx::Rect m_lastFrame;
126 };
127
128} // namespace ui
129
130#endif
131