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 "ckPlatform.h"
17#include "ckMacros.h"
18#include "ckObject.h"
19#include "ckProperty.h"
20#include "ckUtils.h"
21#include <exception>
22
27typedef int32_t CKError;
28
33#define CKPass 1
34
38class CKException : public std::exception {
39 public:
40 const char* message;
41 CKException(const char* msg)
42 : message(msg) {}
43 const char* what() {
44 return this->message;
45 }
46};
47
52enum class CKControlType {
53 Unknown = 0,
55 Label,
58 Canvas,
61};
62
67enum class CKMouseButton {
68 None = 0,
69 Left = 1,
70 Middle = 2,
71 Right = 3
72};
73
79 Left = 0,
80 Center = 1,
81 Right = 2
82};
83
88struct CKColor {
89 u_int8_t r = 0;
90 u_int8_t g = 0;
91 u_int8_t b = 0;
92 u_int8_t a = 255;
93
94 CKColor(u_int8_t red, u_int8_t green, u_int8_t blue)
95 : r(red), g(green), b(blue) {};
96
98 : r(0), g(0), b() {};
99
100 RGBColor ToOS() {
101 RGBColor toReturn;
102 toReturn.red = r * 255;
103 toReturn.green = g * 255;
104 toReturn.blue = b * 255;
105 return toReturn;
106 }
107
108 bool operator==(const CKColor& other) const {
109 return r == other.r && g == other.g && b == other.b;
110 }
111
112 bool operator!=(const CKColor& other) const {
113 return !(*this == other);
114 }
115};
116
122struct CKPoint {
123
126
127 CKPoint(int x, int y)
128 : x(x), y(y) {};
130 : x(0), y(0) {};
131
135 Point ToOS() const {
136 Point p;
137 p.h = x;
138 p.v = y;
139 return p;
140 }
141
145 Point* ToOSPtr() {
146 Point* p = (Point*)CKMalloc(sizeof(*p));
147 p->h = x;
148 p->v = y;
149 return p;
150 }
151
155 static CKPoint FromOS(Point p) {
156 CKPoint toReturn = {p.h, p.v};
157 return toReturn;
158 }
159
160 inline bool operator==(const CKPoint& other) const {
161 return this->x == other.x && this->y == other.y;
162 }
163
164 inline bool operator!=(const CKPoint& other) const {
165 return !(*this == other);
166 }
167
169 void Subscribe(std::function<void()> cb) {
170 x.onChange = cb;
171 y.onChange = cb;
172 }
173};
174
180struct CKSize {
181
184
185 CKSize(int w, int h)
186 : width(w), height(h) {};
187
189 : width(0), height(0) {};
190
191 Point ToOS() const {
192 Point p;
193 p.h = width;
194 p.v = height;
195 return p;
196 }
197
201 static CKSize FromOS(Point p) {
202 CKSize toReturn;
203 toReturn.width = p.h;
204 toReturn.height = p.v;
205 return toReturn;
206 }
207
208 inline bool operator==(const CKSize& other) const {
209 return this->width == other.width && this->height == other.height;
210 }
211
212 inline bool operator!=(const CKSize& other) const {
213 return !(*this == other);
214 }
215
217 void Subscribe(std::function<void()> cb) {
218 width.onChange = cb;
219 height.onChange = cb;
220 }
221};
222
228struct CKRect {
229
232
233 CKRect(const CKRect& f) {
234 this->origin = f.origin;
235 this->size = f.size;
236 }
237
239 this->origin = o;
240 this->size = s;
241 }
242
243 CKRect(int x, int y, int w, int h) {
244 this->origin = CKPoint(x, y);
245 this->size = CKSize(w, h);
246 }
247
248 CKRect(int w, int h) {
249 this->origin = CKPoint();
250 this->size = CKSize(w, h);
251 }
252
254 this->origin = CKPoint();
255 this->size = CKSize();
256 }
257
261 Rect ToOS() const {
262 Rect r;
263 r.left = this->origin->x;
264 r.right = this->origin->x + this->size->width;
265 r.top = this->origin->y;
266 r.bottom = this->origin->y + this->size->height;
267 return r;
268 }
269
273 Rect* ToOSCopy() {
274 Rect* toReturn = (Rect*)CKMalloc((sizeof(*toReturn)));
275 toReturn->left = this->origin->x;
276 toReturn->right = this->origin->x + this->size->width;
277 toReturn->top = this->origin->y;
278 toReturn->bottom = this->origin->y + this->size->height;
279 return toReturn;
280 }
281
285 static CKRect FromOS(Rect r) {
286 CKRect toReturn;
287 toReturn.origin = CKPoint(r.left, r.top);
288 toReturn.size = CKSize(r.right - r.left, r.bottom - r.top);
289 return toReturn;
290 }
291
296 if (p.x >= this->origin->x && p.x <= this->origin->x + this->size->width) {
297 if (p.y >= this->origin->y && p.y <= this->origin->y + this->size->height) {
298 return true;
299 }
300 }
301 return false;
302 }
303
305 return (origin->x < (r.origin->x + r.size->width)) &&
306 ((origin->x + size->width) > r.origin->x) &&
307 (origin->y < (r.origin->y + r.size->height)) &&
308 ((origin->y + size->height) > r.origin->y);
309 }
310
311 inline bool operator==(const CKRect& other) const {
312 return origin->x == other.origin->x &&
313 origin->y == other.origin->y &&
314 size->width == other.size->width &&
315 size->height == other.size->height;
316 }
317
318 inline bool operator!=(const CKRect& other) const {
319 return !(*this == other);
320 }
321
323 void Subscribe(std::function<void()> cb) {
324 origin.onChange = cb;
325 size.onChange = cb;
326 origin.get().Subscribe(cb);
327 size.get().Subscribe(cb);
328 }
329};
330
335enum class CKEventType {
339 nullEvent = 0,
340
344 click,
345
350
354 mouseDown,
355
359 mouseMove,
360
364 mouseUp,
365
369 keyDown,
370
374 keyUp,
375
379 moved,
380
384 resized,
385
389 deleted,
390
394 removed,
395
399 changed,
400
405
413
418
423
430
434 tcpError,
435
444 dataUndo,
445
454 dataCut,
455
464 dataCopy,
465
474 dataPaste,
475
485 dataClear,
486
487};
488
489class CKWindow;
490class CKControl;
491
496struct CKEvent {
497
501
502 bool shiftDown = false;
503 bool cmdDown = false;
504 bool optDown = false;
505 bool ctrlDown = false;
506
507 char key; // Keycode of the key being pressed/released
508 char character; // Character Code (A, B, C..) of the key pressed/released
509
510 const CKWindow* window = nullptr; // always set
511 const CKControl* control = nullptr; // can be nullptr for window-only events
512
513 CKError errCode; // Set on tcpError.
514
519
520 void fillFromOS(EventRecord e) {
521 shiftDown = (e.modifiers & shiftKey) == shiftKey;
522 cmdDown = (e.modifiers & cmdKey) == cmdKey;
523 optDown = (e.modifiers & optionKey) == optionKey;
524 ctrlDown = (e.modifiers & controlKey) == controlKey;
525 }
526};
527
532enum class CKSystemIcon {
533 noIcon = 0,
534 message = 1,
535 warning = 2,
536 error = 3,
537};
538
543enum class CKScrollType {
544 none = 0,
545 vertical = 1,
546 horizontal = 2,
547 both = 3
548};
Defines the base of all UI Controls.
Definition ckControl.h:89
Definition ckTypes.h:38
const char * what()
Definition ckTypes.h:43
const char * message
Definition ckTypes.h:40
CKException(const char *msg)
Definition ckTypes.h:41
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:91
#define CKMalloc(sz)
Use instead of malloc.
Definition ckMemory.h:54
CKControlType
Defines a control type (i.e. PushButton, Checkbox, etc.)
Definition ckTypes.h:52
CKTextJustification
Used for text labels, etc.
Definition ckTypes.h:78
CKEventType
Defines an event type for controls (and windows.)
Definition ckTypes.h:335
CKScrollType
Defines a scroll type.
Definition ckTypes.h:543
int32_t CKError
Return type for functions that might return an error. Also see: CKPass and CKErrorCode.
Definition ckTypes.h:27
CKSystemIcon
Defines a system icon type.
Definition ckTypes.h:532
CKMouseButton
Abstraction for mouse buttons.
Definition ckTypes.h:67
@ 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.
@ tcpError
TCP socket has an error.
@ 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:88
u_int8_t b
Definition ckTypes.h:91
CKColor()
Definition ckTypes.h:97
RGBColor ToOS()
Definition ckTypes.h:100
bool operator!=(const CKColor &other) const
Definition ckTypes.h:112
u_int8_t r
Definition ckTypes.h:89
bool operator==(const CKColor &other) const
Definition ckTypes.h:108
CKColor(u_int8_t red, u_int8_t green, u_int8_t blue)
Definition ckTypes.h:94
u_int8_t a
Definition ckTypes.h:92
u_int8_t g
Definition ckTypes.h:90
Defines an event raised by the framework, mostly for user actions.
Definition ckTypes.h:496
CKEvent(CKEventType type, CKPoint point)
Definition ckTypes.h:517
CKError errCode
Definition ckTypes.h:513
const CKWindow * window
Definition ckTypes.h:510
bool cmdDown
Definition ckTypes.h:503
CKPoint point
Definition ckTypes.h:499
char character
Definition ckTypes.h:508
void fillFromOS(EventRecord e)
Definition ckTypes.h:520
CKEvent(CKEventType type)
Definition ckTypes.h:515
const CKControl * control
Definition ckTypes.h:511
CKEventType type
Definition ckTypes.h:498
bool shiftDown
Definition ckTypes.h:502
char key
Definition ckTypes.h:507
CKMouseButton mouseButton
Definition ckTypes.h:500
bool optDown
Definition ckTypes.h:504
bool ctrlDown
Definition ckTypes.h:505
Defines a point on the screen.
Definition ckTypes.h:122
bool operator!=(const CKPoint &other) const
Definition ckTypes.h:164
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:169
CKPoint(int x, int y)
Definition ckTypes.h:127
CKProperty< int > x
Definition ckTypes.h:124
CKProperty< int > y
Definition ckTypes.h:125
Point * ToOSPtr()
Definition ckTypes.h:145
bool operator==(const CKPoint &other) const
Definition ckTypes.h:160
static CKPoint FromOS(Point p)
Definition ckTypes.h:155
Point ToOS() const
Definition ckTypes.h:135
CKPoint()
Definition ckTypes.h:129
Defines a rectangular area at a specific location.
Definition ckTypes.h:228
bool operator==(const CKRect &other) const
Definition ckTypes.h:311
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:323
CKRect(int x, int y, int w, int h)
Definition ckTypes.h:243
CKProperty< CKPoint > origin
Definition ckTypes.h:230
bool IntersectsPoint(CKPoint p)
Definition ckTypes.h:295
CKProperty< CKSize > size
Definition ckTypes.h:231
Rect * ToOSCopy()
Definition ckTypes.h:273
bool IntersectsRect(CKRect r)
Definition ckTypes.h:304
Rect ToOS() const
Definition ckTypes.h:261
CKRect(CKPoint o, CKSize s)
Definition ckTypes.h:238
bool operator!=(const CKRect &other) const
Definition ckTypes.h:318
CKRect(const CKRect &f)
Definition ckTypes.h:233
static CKRect FromOS(Rect r)
Definition ckTypes.h:285
CKRect(int w, int h)
Definition ckTypes.h:248
CKRect()
Definition ckTypes.h:253
Defines a rectangular area.
Definition ckTypes.h:180
bool operator!=(const CKSize &other) const
Definition ckTypes.h:212
CKProperty< int > width
Definition ckTypes.h:182
Point ToOS() const
Definition ckTypes.h:191
CKProperty< int > height
Definition ckTypes.h:183
void Subscribe(std::function< void()> cb)
Definition ckTypes.h:217
static CKSize FromOS(Point p)
Definition ckTypes.h:201
CKSize(int w, int h)
Definition ckTypes.h:185
CKSize()
Definition ckTypes.h:188
bool operator==(const CKSize &other) const
Definition ckTypes.h:208