Clapkit
Loading...
Searching...
No Matches
ckTypes.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 * CKTypes
9 * ----------------------------------------------------------------------
10 * App-wide type definitions.
11 *
12 */
13
14#pragma once
15
16#include "ckMacros.h"
17#include "ckObject.h"
18#include "ckProperty.h"
19#include "ckUtils.h"
20#include <exception>
21
26typedef int32_t CKError;
27
32#define CKPass 1
33
37class CKException : public std::exception {
38 public:
39 const char* message;
40 CKException(const char* msg)
41 : message(msg) {}
42 const char* what() {
43 return this->message;
44 }
45};
46
51enum class CKControlType {
52 Unknown = 0,
54 Label,
57 Canvas,
60};
61
66enum class CKMouseButton {
67 None = 0,
68 Left = 1,
69 Middle = 2,
70 Right = 3
71};
72
78 Left = 0,
79 Center = 1,
80 Right = 2
81};
82
87struct CKColor {
88 u_int8_t r = 0;
89 u_int8_t g = 0;
90 u_int8_t b = 0;
91 u_int8_t a = 255;
92
93 CKColor(u_int8_t red, u_int8_t green, u_int8_t blue)
94 : r(red), g(green), b(blue) {};
95
97 : r(0), g(0), b() {};
98
99 RGBColor ToOS() {
100 RGBColor toReturn;
101 toReturn.red = r * 255;
102 toReturn.green = g * 255;
103 toReturn.blue = b * 255;
104 return toReturn;
105 }
106
107 bool operator==(const CKColor& other) const {
108 return r == other.r && g == other.g && b == other.b;
109 }
110
111 bool operator!=(const CKColor& other) const {
112 return !(*this == other);
113 }
114};
115
121struct CKPoint {
122
125
126 CKPoint(int x, int y)
127 : x(x), y(y) {};
129 : x(0), y(0) {};
130
134 Point ToOS() const {
135 Point p;
136 p.h = x;
137 p.v = y;
138 return p;
139 }
140
144 Point* ToOSPtr() {
145 Point* p = (Point*)CKMalloc(sizeof(*p));
146 p->h = x;
147 p->v = y;
148 return p;
149 }
150
154 static CKPoint FromOS(Point p) {
155 CKPoint toReturn = {p.h, p.v};
156 return toReturn;
157 }
158
159 inline bool operator==(const CKPoint& other) const {
160 return this->x == other.x && this->y == other.y;
161 }
162
163 inline bool operator!=(const CKPoint& other) const {
164 return !(*this == other);
165 }
166
168 void Subscribe(std::function<void()> cb) {
169 x.onChange = cb;
170 y.onChange = cb;
171 }
172};
173
179struct CKSize {
180
183
184 CKSize(int w, int h)
185 : width(w), height(h) {};
186
188 : width(0), height(0) {};
189
190 Point ToOS() const {
191 Point p;
192 p.h = width;
193 p.v = height;
194 return p;
195 }
196
200 static CKSize FromOS(Point p) {
201 CKSize toReturn;
202 toReturn.width = p.h;
203 toReturn.height = p.v;
204 return toReturn;
205 }
206
207 inline bool operator==(const CKSize& other) const {
208 return this->width == other.width && this->height == other.height;
209 }
210
211 inline bool operator!=(const CKSize& other) const {
212 return !(*this == other);
213 }
214
216 void Subscribe(std::function<void()> cb) {
217 width.onChange = cb;
218 height.onChange = cb;
219 }
220};
221
227struct CKRect {
228
231
232 CKRect(const CKRect& f) {
233 this->origin = f.origin;
234 this->size = f.size;
235 }
236
238 this->origin = o;
239 this->size = s;
240 }
241
242 CKRect(int x, int y, int w, int h) {
243 this->origin = CKPoint(x, y);
244 this->size = CKSize(w, h);
245 }
246
247 CKRect(int w, int h) {
248 this->origin = CKPoint();
249 this->size = CKSize(w, h);
250 }
251
253 this->origin = CKPoint();
254 this->size = CKSize();
255 }
256
260 Rect ToOS() const {
261 Rect r;
262 r.left = this->origin->x;
263 r.right = this->origin->x + this->size->width;
264 r.top = this->origin->y;
265 r.bottom = this->origin->y + this->size->height;
266 return r;
267 }
268
272 Rect* ToOSCopy() {
273 Rect* toReturn = (Rect*)CKMalloc((sizeof(*toReturn)));
274 toReturn->left = this->origin->x;
275 toReturn->right = this->origin->x + this->size->width;
276 toReturn->top = this->origin->y;
277 toReturn->bottom = this->origin->y + this->size->height;
278 return toReturn;
279 }
280
284 static CKRect FromOS(Rect r) {
285 CKRect toReturn;
286 toReturn.origin = CKPoint(r.left, r.top);
287 toReturn.size = CKSize(r.right - r.left, r.bottom - r.top);
288 return toReturn;
289 }
290
295 if (p.x >= this->origin->x && p.x <= this->origin->x + this->size->width) {
296 if (p.y >= this->origin->y && p.y <= this->origin->y + this->size->height) {
297 return true;
298 }
299 }
300 return false;
301 }
302
304 return (origin->x < (r.origin->x + r.size->width)) &&
305 ((origin->x + size->width) > r.origin->x) &&
306 (origin->y < (r.origin->y + r.size->height)) &&
307 ((origin->y + size->height) > r.origin->y);
308 }
309
310 inline bool operator==(const CKRect& other) const {
311 return origin->x == other.origin->x &&
312 origin->y == other.origin->y &&
313 size->width == other.size->width &&
314 size->height == other.size->height;
315 }
316
317 inline bool operator!=(const CKRect& other) const {
318 return !(*this == other);
319 }
320
322 void Subscribe(std::function<void()> cb) {
323 origin.onChange = cb;
324 size.onChange = cb;
325 origin.get().Subscribe(cb);
326 size.get().Subscribe(cb);
327 }
328};
329
334enum class CKEventType {
338 nullEvent = 0,
339
343 click,
344
349
353 mouseDown,
354
358 mouseMove,
359
363 mouseUp,
364
368 keyDown,
369
373 keyUp,
374
378 moved,
379
383 resized,
384
388 deleted,
389
393 removed,
394
398 changed,
399
404
412
417
422
429
438 dataUndo,
439
448 dataCut,
449
458 dataCopy,
459
468 dataPaste,
469
479 dataClear,
480
481};
482
483class CKWindow;
484class CKControl;
485
490struct CKEvent {
491
495
496 bool shiftDown = false;
497 bool cmdDown = false;
498 bool optDown = false;
499 bool ctrlDown = false;
500
501 char key; // Keycode of the key being pressed/released
502 char character; // Character Code (A, B, C..) of the key pressed/released
503
504 const CKWindow* window = nullptr; // always set
505 const CKControl* control = nullptr; // can be nullptr for window-only events
506
511
512 void fillFromOS(EventRecord e) {
513 shiftDown = (e.modifiers & shiftKey) == shiftKey;
514 cmdDown = (e.modifiers & cmdKey) == cmdKey;
515 optDown = (e.modifiers & optionKey) == optionKey;
516 ctrlDown = (e.modifiers & controlKey) == controlKey;
517 }
518};
519
524enum class CKSystemIcon {
525 noIcon = 0,
526 message = 1,
527 warning = 2,
528 error = 3,
529};
530
535enum class CKScrollType {
536 none = 0,
537 vertical = 1,
538 horizontal = 2,
539 both = 3
540};
Defines the base of all UI Controls.
Definition ckControl.h:89
Definition ckTypes.h:37
const char * what()
Definition ckTypes.h:42
const char * message
Definition ckTypes.h:39
CKException(const char *msg)
Definition ckTypes.h:40
Defines an observable value.
Definition ckProperty.h:60
const T & get() const
Definition ckProperty.h:68
std::function< void()> onChange
The function to be called when the value changes.
Definition ckProperty.h:169
Defines a window. Window type (modal, document) is determined by CKWindowInitParams and CKWindowType.
Definition ckWindow.h:89
#define CKMalloc(sz)
Use instead of malloc.
Definition ckMemory.h:54
CKControlType
Defines a control type (i.e. PushButton, Checkbox, etc.)
Definition ckTypes.h:51
CKTextJustification
Used for text labels, etc.
Definition ckTypes.h:77
CKEventType
Defines an event type for controls (and windows.)
Definition ckTypes.h:334
CKScrollType
Defines a scroll type.
Definition ckTypes.h:535
int32_t CKError
Return type for functions that might return an error. Also see: CKPass and CKErrorCode.
Definition ckTypes.h:26
CKSystemIcon
Defines a system icon type.
Definition ckTypes.h:524
CKMouseButton
Abstraction for mouse buttons.
Definition ckTypes.h:66
@ moved
(Window-only) Window has moved.
@ tcpDisconnected
TCP socket has disconnected.
@ keyDown
Keyboard key pressed.
@ dataPaste
User wants to perform a paste (Cmd + V) If no such event handler is installed, Clapkit will try to pe...
@ tcpConnectionFailed
TCP socket was unable to connect to the specified address.
@ mouseUp
Mouse button released.
@ dataCopy
User wants to perform a copy (Cmd + C) If no such event handler is installed, Clapkit will try to per...
@ resized
(Window-only) Window has been resized.
@ mouseDown
Mouse button pressed down on window or control.
@ changed
For textfields, etc where value can be changed.
@ dataClear
User wants to perform a 'clear' - i.e. delete all data on an user-editable control....
@ tcpBufferFull
TCP socket's incoming data buffer became full and you've missed incoming data - and will be missing d...
@ dataCut
User wants to perform a cut (Cmd + X) If no such event handler is installed, Clapkit will try to perf...
@ click
User clicked on window/control.
@ removed
Window has been closed or control removed from window.
@ nullEvent
Nothing.
@ doubleClick
User double-clicked on window/control.
@ mouseMove
Mouse is being dragged while clicking.
@ tcpReceivedData
TCP socket has data to read.
@ deleted
Window or control has been deleted/released.
@ keyUp
Keyboard key released.
@ tcpConnected
TCP socket has connected.
@ dataUndo
User wants to perform an undo (Cmd + Z) If no such event handler is installed, Clapkit will try to pe...
Defines an RGB color. A (Alpha) is usually not used in our case.
Definition ckTypes.h:87
u_int8_t b
Definition ckTypes.h:90
CKColor()
Definition ckTypes.h:96
RGBColor ToOS()
Definition ckTypes.h:99
bool operator!=(const CKColor &other) const
Definition ckTypes.h:111
u_int8_t r
Definition ckTypes.h:88
bool operator==(const CKColor &other) const
Definition ckTypes.h:107
CKColor(u_int8_t red, u_int8_t green, u_int8_t blue)
Definition ckTypes.h:93
u_int8_t a
Definition ckTypes.h:91
u_int8_t g
Definition ckTypes.h:89
Defines an event raised by the framework, mostly for user actions.
Definition ckTypes.h:490
CKEvent(CKEventType type, CKPoint point)
Definition ckTypes.h:509
const CKWindow * window
Definition ckTypes.h:504
bool cmdDown
Definition ckTypes.h:497
CKPoint point
Definition ckTypes.h:493
char character
Definition ckTypes.h:502
void fillFromOS(EventRecord e)
Definition ckTypes.h:512
CKEvent(CKEventType type)
Definition ckTypes.h:507
const CKControl * control
Definition ckTypes.h:505
CKEventType type
Definition ckTypes.h:492
bool shiftDown
Definition ckTypes.h:496
char key
Definition ckTypes.h:501
CKMouseButton mouseButton
Definition ckTypes.h:494
bool optDown
Definition ckTypes.h:498
bool ctrlDown
Definition ckTypes.h:499
Defines a point on the screen.
Definition ckTypes.h:121
bool operator!=(const CKPoint &other) const
Definition ckTypes.h:163
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:168
CKPoint(int x, int y)
Definition ckTypes.h:126
CKProperty< int > x
Definition ckTypes.h:123
CKProperty< int > y
Definition ckTypes.h:124
Point * ToOSPtr()
Definition ckTypes.h:144
bool operator==(const CKPoint &other) const
Definition ckTypes.h:159
static CKPoint FromOS(Point p)
Definition ckTypes.h:154
Point ToOS() const
Definition ckTypes.h:134
CKPoint()
Definition ckTypes.h:128
Defines a rectangular area at a specific location.
Definition ckTypes.h:227
bool operator==(const CKRect &other) const
Definition ckTypes.h:310
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:322
CKRect(int x, int y, int w, int h)
Definition ckTypes.h:242
CKProperty< CKPoint > origin
Definition ckTypes.h:229
bool IntersectsPoint(CKPoint p)
Definition ckTypes.h:294
CKProperty< CKSize > size
Definition ckTypes.h:230
Rect * ToOSCopy()
Definition ckTypes.h:272
bool IntersectsRect(CKRect r)
Definition ckTypes.h:303
Rect ToOS() const
Definition ckTypes.h:260
CKRect(CKPoint o, CKSize s)
Definition ckTypes.h:237
bool operator!=(const CKRect &other) const
Definition ckTypes.h:317
CKRect(const CKRect &f)
Definition ckTypes.h:232
static CKRect FromOS(Rect r)
Definition ckTypes.h:284
CKRect(int w, int h)
Definition ckTypes.h:247
CKRect()
Definition ckTypes.h:252
Defines a rectangular area.
Definition ckTypes.h:179
bool operator!=(const CKSize &other) const
Definition ckTypes.h:211
CKProperty< int > width
Definition ckTypes.h:181
Point ToOS() const
Definition ckTypes.h:190
CKProperty< int > height
Definition ckTypes.h:182
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:216
static CKSize FromOS(Point p)
Definition ckTypes.h:200
CKSize(int w, int h)
Definition ckTypes.h:184
CKSize()
Definition ckTypes.h:187
bool operator==(const CKSize &other) const
Definition ckTypes.h:207