Clapkit
Loading...
Searching...
No Matches
ckApp.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 * CKApp
9 * ----------------------------------------------------------------------
10 * Defines an application.
11 *
12 */
13
14#pragma once
15
16// Need to move these somewhere else soon.
17#ifndef TARGET_OS_MAC
18#define TARGET_OS_MAC true
19// #define TARGET_API_MAC_OS8 true
20// #define TARGET_API_MAC_CARBON false
21#endif
22
23#include "ckMacros.h"
24#include "ckPlatform.h"
25#include "ckTypes.h"
26#include "ckUtils.h"
27#include <Dialogs.h>
28#include <Events.h>
29#include <Fonts.h>
30#include <MacTypes.h>
31#include <MacWindows.h>
32#include <Memory.h>
33#include <Menus.h>
34#include <Quickdraw.h>
35#include <functional>
36#include <stdlib.h>
37#include <vector>
38
39class CKObject;
40class CKWindow;
41class CKTimer;
42class CKNetBaseSocket;
44struct CKMenuBar;
45struct CKMenu;
46struct CKMenuItem;
47
57class CKApp {
58
59 public:
60 CKApp();
61 ~CKApp();
62
63 int CKLoop(int waitTime = 60);
64 void CKQuit();
65
68 void CKRemoveWindow(CKWindow* window);
69
70 void CKIncreaseWork();
71 void CKDecreaseWork();
72 void CKRestoreCursor();
73
74 CKWindow* CKNewMsgBoxPlain(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = nullptr, std::function<void(int button)> callback = 0);
75 CKWindow* CKNewMsgBoxNote(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = nullptr, std::function<void(int button)> callback = 0);
76 CKWindow* CKNewMsgBoxWarning(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = "Cancel", std::function<void(int button)> callback = 0);
77 CKWindow* CKNewMsgBoxError(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = "Cancel", std::function<void(int button)> callback = 0);
78
80
81 short CKFontToId(const char* font);
82
83 void CKAddTimer(CKTimer* timer, CKObject* owner = nullptr);
84 void CKRemoveTimer(CKTimer* timer);
86
88 void CKShowMenuBar();
89 void CKHideMenuBar();
91
92 private:
93 void __DoHousekeepingTasks();
94 void __DispatchEvent(EventRecord event);
95 inline void __HandleEvtKey(EventRecord event, bool isKeyUp, bool isAutoKey);
96 inline void __HandleEvtMouseDown(EventRecord event);
97 inline void __HandleEvtMouseUp(EventRecord event);
98 inline void __HandleEvtMouseMove(EventRecord event);
99 inline void __HandleEvtUpdate(EventRecord event);
100 inline void __HandleEvtActivate(EventRecord event);
101 inline void __HandleEvtOS(EventRecord event);
102 void __HandleMenuPropertyChange(const CKObject* obj, const char* propName);
103 CKWindow* __CreateAlertDialog(const char* title, const char* message, const CKSystemIcon icon, const char* btnOk = "OK", const char* btnCancel = 0, std::function<void(int button)> callback = 0);
104
105 private:
106 int __workCount;
107 CKWindow* __lastMouseDownWindow;
108 std::vector<CKWindow*> __windows;
109 std::vector<CKWindow*> __gc_windows;
110 std::vector<CKTimer*> __timers;
111 CKMenuBar* __menubar = nullptr;
112
113 friend CKNetBaseSocket;
114 std::vector<CKNetBaseSocket*> __net_sockets;
115};
WindowPtr CKWindowPtr
Definition ckPlatform.h:62
Defines the main entry point for a Clapkit application.
Definition ckApp.h:57
void CKRemoveTimer(CKTimer *timer)
Stop and remove timer.
Definition ckApp.cpp:964
~CKApp()
Definition ckApp.cpp:68
void CKQuit()
Definition ckApp.cpp:142
void CKAddTimer(CKTimer *timer, CKObject *owner=nullptr)
Add a new timer to the app, enabling (via Start) as you add it.
Definition ckApp.cpp:953
CKWindow * CKNewWindow(const CKWindowInitParams &params)
Create a new window and if successful, add it to the list of the windows the app has....
Definition ckApp.cpp:184
void CKIncreaseWork()
Show the 'Working' cursor.
Definition ckApp.cpp:421
CKWindow * CKNewMsgBoxPlain(const char *message, const char *title=nullptr, const char *btnOk="OK", const char *btnCancel=nullptr, std::function< void(int button)> callback=0)
Create and show an alert. Alerts are non-blocking. btnCancel is optional and only shown if set to a n...
Definition ckApp.cpp:285
CKWindow * CKTopMostWindow()
Return the top-most window we have.
Definition ckApp.cpp:468
void CKDecreaseWork()
Decrease work count by one, if zero, hide the 'Working' cursor.
Definition ckApp.cpp:430
int CKLoop(int waitTime=60)
Main event loop, handling events. Needs to be called as much as possible.
Definition ckApp.cpp:77
CKWindow * CKFindWindow(CKWindowPtr ptr)
Try to find a window by the WindowPtr. Probably only applies to Toolbox Mac.
Definition ckApp.cpp:207
void CKShowMenuBar()
If hidden, bring the menu bar back.
Definition ckApp.cpp:1091
CKApp()
The starting point of any Clapkit app. Initialize the app, set up menus, etc.
Definition ckApp.cpp:35
void CKUpdateMenuBarItems()
Called from CKWindow on focused-item change to update (i.e. enable/disable) the standard menu items (...
Definition ckApp.cpp:1120
void CKRemoveWindow(CKWindow *window)
Remove and destroy window. You MUST use this function instead of deleting a window yourself.
Definition ckApp.cpp:231
CKWindow * CKNewMsgBoxNote(const char *message, const char *title=nullptr, const char *btnOk="OK", const char *btnCancel=nullptr, std::function< void(int button)> callback=0)
Create and show an alert. Alerts are non-blocking. btnCancel is optional and only shown if set to a n...
Definition ckApp.cpp:266
CKError CKSetMenu(CKMenuBar *menu)
Set the application's menu bar.
Definition ckApp.cpp:1007
void CKHideMenuBar()
If shown, hide the menu bar.
Definition ckApp.cpp:1098
CKWindow * CKNewMsgBoxError(const char *message, const char *title=nullptr, const char *btnOk="OK", const char *btnCancel="Cancel", std::function< void(int button)> callback=0)
Create and show an alert. Alerts are non-blocking. btnCancel is optional and only shown if set to a n...
Definition ckApp.cpp:323
CKWindow * CKNewMsgBoxWarning(const char *message, const char *title=nullptr, const char *btnOk="OK", const char *btnCancel="Cancel", std::function< void(int button)> callback=0)
Create and show an alert. Alerts are non-blocking. btnCancel is optional and only shown if set to a n...
Definition ckApp.cpp:304
void CKRemoveTimersOfOwner(CKObject *owner)
Remove all timers of a specific owner.
Definition ckApp.cpp:981
void CKRestoreCursor()
Changed the cursor yourself? Call this to get the default/waiting back.
Definition ckApp.cpp:444
short CKFontToId(const char *font)
Convert a font name to a font Id.
Definition ckApp.cpp:933
Defines a menubar for the application.
Definition ckMenu.h:29
Defines an item on the menu (i.e. "Save", "Quit", etc..)
Definition ckMenu.h:95
Defines an item on the menubar. (e.g. "File", "Edit" ..)
Definition ckMenu.h:55
Defines the base of all TCP client/server sockets.
Definition ckNetBaseSocket.h:41
Defines the base class for all controls and objects.
Definition ckObject.h:43
Defines a timer that can execute code on a pre-defined interval.
Definition ckTimer.h:27
Defines a window. Window type (modal, document) is determined by CKWindowInitParams and CKWindowType.
Definition ckWindow.h:89
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
Initialization parameters for a CKWindow.
Definition ckWindow.h:44