Clapkit
Loading...
Searching...
No Matches
ckWindow.h
Go to the documentation of this file.
1/*
2 *
3 * Clapkit
4 * ----------------------------------------------------------------------
5 * A wrapper for creating a 'generalized' app for Classic MacOS
6 * that (hopefully) can be ported easily to other platforms.
7 *
8 * CKWindow
9 * ----------------------------------------------------------------------
10 * Defines a window.
11 *
12 */
13
14#pragma once
15
16#include "ckApp.h"
17#include "ckControl.h"
18#include "ckObject.h"
19#include <MacWindows.h>
20#include <Quickdraw.h>
21#include <cstring>
22#include <vector>
23#include <optional>
24
29enum class CKWindowType {
30 Standard = 0,
32 Modal = 2,
33 Floating = 3,
34};
35
37
47 public:
49 std::optional<CKPoint> origin;
51 char* title = nullptr;
52
53 public:
62 this->size = size;
63 }
64
66 CKSafeCopyString(this->title, title);
67 return *this;
68 }
69
71 this->type = type;
72 return *this;
73 }
74
76 this->origin = point;
77 return *this;
78 }
79
81 this->origin.reset();
82 return *this;
83 }
84};
85
91class CKWindow : public CKObject {
92
93 public:
95 virtual ~CKWindow();
96 void Loop();
97
98 void SetTitle(const char* title);
99 char* GetTitle();
100
101 void Show();
102 void Hide();
103 void Focus();
104 void Center();
105 void Close();
106
107 bool AddControl(CKControl* control);
108 void RemoveControl(CKControl* control, bool free);
109 void Redraw(CKRect rectToRedraw);
111
112 void SetOwner(CKApp* owner);
113 CKApp* GetOwner();
114
116 const std::vector<CKControl*>& GetControls() const;
117
125 template <typename T>
126 std::vector<T*> GetControlsOfType() const {
127
128 std::vector<T*> out;
129 out.reserve(__controls.size());
130 for (auto* c : __controls) {
131 if (auto* t = dynamic_cast<T*>(c)) {
132 out.push_back(t);
133 }
134 }
135 return out;
136 }
137
138 bool ContainsControl(CKControl* control);
140 void SetActiveControl(CKControl* control);
141
142 virtual bool HandleEvent(const CKEvent& evt);
143
144 CKControl* GetLastControl() const;
145 void SetLastControl(CKControl* control);
146
147 const CKWindowPtr GetWindowPtr() const;
148
149 void DirtyArea(const CKRect r);
150
151 bool GetIsActive();
152
153 private:
154 void __InvalidateEntireWindow();
155
156 protected:
157 friend class CKApp;
158 void SetIsActive(bool active);
159 void __ReflectToOS();
160 virtual void RaisePropertyChange(const char* propertyName);
161
162 public:
168
171 int maximumWidth = 1000;
172 int maximumHeight = 1000;
173
179
180 private:
181 CKApp* __owner;
182 std::vector<CKControl*> __controls;
183 CKWindowPtr __windowPtr = nullptr;
184 CKControl* __activeTextInputControl = nullptr;
185 CKControl* __lastDownControl = nullptr;
186 bool __dead = false;
187 bool __isCurrentlyActive = false;
188 CKWindowType __type;
189 std::vector<CKRect> __controlDirtifiedAreas;
190 // Note: intentionally do not cache/force global hilite state; the OS
191 // manages it (active/inactive window, theme changes, etc.).
192};
WindowPtr CKWindowPtr
Definition ckPlatform.h:62
Defines the main entry point for a Clapkit application.
Definition ckApp.h:58
Defines the base of all UI Controls.
Definition ckControl.h:89
A base for controls that can have a "focus" - i.e. textfields.
Definition ck_pFocusableControl.h:22
Defines the base class for all controls and objects.
Definition ckObject.h:43
Defines an observable value.
Definition ckProperty.h:60
Defines a window. Window type (modal, document) is determined by CKWindowInitParams and CKWindowType.
Definition ckWindow.h:91
const CKWindowPtr GetWindowPtr() const
Definition ckWindow.cpp:557
void RemoveControl(CKControl *control, bool free)
Definition ckWindow.cpp:272
const std::vector< CKControl * > & GetControls() const
Get the list of controls in this window.
Definition ckWindow.cpp:458
void Center()
Definition ckWindow.cpp:200
bool AddControl(CKControl *control)
Definition ckWindow.cpp:235
CKProperty< CKRect > rect
Definition ckWindow.h:163
CKControl * GetLastControl() const
Definition ckWindow.cpp:547
CKProperty< CKColor > backgroundColor
Definition ckWindow.h:166
bool shouldReceiveMouseMoveEvents
True if we should receive mouseMove events. We are storing this as a hack to speed things up as HasHa...
Definition ckWindow.h:178
void SetIsActive(bool active)
Definition ckWindow.cpp:524
CKProperty< bool > visible
Definition ckWindow.h:164
void DirtyArea(const CKRect r)
Definition ckWindow.cpp:567
int minimumHeight
Definition ckWindow.h:170
CKProperty< bool > hasCustomBackgroundColor
Definition ckWindow.h:165
void SetLastControl(CKControl *control)
Definition ckWindow.cpp:552
void Redraw(CKRect rectToRedraw)
Definition ckWindow.cpp:308
int minimumWidth
Definition ckWindow.h:169
bool GetIsActive()
Definition ckWindow.cpp:562
void SetTitle(const char *title)
Definition ckWindow.cpp:140
void PreparePortForControlDraw()
Definition ckWindow.cpp:379
CKProperty< bool > closable
Definition ckWindow.h:167
CKApp * GetOwner()
Return the app this window is a part of.
Definition ckWindow.cpp:422
virtual ~CKWindow()
Definition ckWindow.cpp:102
void __ReflectToOS()
Definition ckWindow.cpp:626
char * GetTitle()
Definition ckWindow.cpp:152
int maximumHeight
Definition ckWindow.h:172
virtual void RaisePropertyChange(const char *propertyName)
Definition ckWindow.cpp:648
void Show()
Definition ckWindow.cpp:167
void Close()
Definition ckWindow.cpp:212
virtual bool HandleEvent(const CKEvent &evt)
Definition ckWindow.cpp:585
CKControl * FindControl(CKPoint point)
Definition ckWindow.cpp:435
void Loop()
Called from CKApp to do stuff like blinking the caret, etc.
Definition ckWindow.cpp:128
void Focus()
Make the window foremost window. Make visible if invisible.
Definition ckWindow.cpp:189
std::vector< T * > GetControlsOfType() const
Get the list of controls of type T in this window.
Definition ckWindow.h:126
void SetOwner(CKApp *owner)
Definition ckWindow.cpp:414
void SetActiveControl(CKControl *control)
Called by ckApp on a click event - to set the active control (like a textfield.)
Definition ckWindow.cpp:496
bool ContainsControl(CKControl *control)
Definition ckWindow.cpp:465
void Hide()
Definition ckWindow.cpp:179
int maximumWidth
Definition ckWindow.h:171
CKFocusableControl * GetActiveControl()
Called by ckApp to determine UI Changes needed, like enabling/disabling menu items.
Definition ckWindow.cpp:483
#define CKSafeCopyString(dest, src)
Copy a string (if it is a string) to a destination. Free the destination if there is already somethin...
Definition ckMemory.h:93
CKWindowType
Defines the type of the window.
Definition ckWindow.h:29
Defines an RGB color. A (Alpha) is usually not used in our case.
Definition ckTypes.h:88
Defines an event raised by the framework, mostly for user actions.
Definition ckTypes.h:496
Defines a point on the screen.
Definition ckTypes.h:122
Defines a rectangular area at a specific location.
Definition ckTypes.h:228
Defines a rectangular area.
Definition ckTypes.h:180
Initialization parameters for a CKWindow.
Definition ckWindow.h:46
CKWindowInitParams & SetType(CKWindowType type)
Definition ckWindow.h:70
CKWindowInitParams & UnsetOrigin()
Definition ckWindow.h:80
CKWindowType type
Definition ckWindow.h:50
std::optional< CKPoint > origin
Definition ckWindow.h:49
CKSize size
Definition ckWindow.h:48
CKWindowInitParams(CKSize size)
Initialization parameters for a CKWindow. You must set a size larger than 0x0. Title/type can be set ...
Definition ckWindow.h:61
char * title
Definition ckWindow.h:51
CKWindowInitParams & SetOrigin(CKPoint point)
Definition ckWindow.h:75
CKWindowInitParams & SetTitle(const char *title)
Definition ckWindow.h:65