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#include <cctype>
39
40class CKObject;
41class CKWindow;
42class CKTimer;
43class CKNetBaseSocket;
45struct CKMenuBar;
46struct CKMenu;
47struct CKMenuItem;
48
58class CKApp {
59
60 public:
61 CKApp();
62 ~CKApp();
63
64 int CKLoop(int waitTime = 60);
65 void CKQuit();
66
69 void CKRemoveWindow(CKWindow* window);
70
71 void CKIncreaseWork();
72 void CKDecreaseWork();
73 void CKRestoreCursor();
74
75 CKWindow* CKNewMsgBoxPlain(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = nullptr, std::function<void(int button)> callback = 0);
76 CKWindow* CKNewMsgBoxNote(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = nullptr, std::function<void(int button)> callback = 0);
77 CKWindow* CKNewMsgBoxWarning(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = "Cancel", std::function<void(int button)> callback = 0);
78 CKWindow* CKNewMsgBoxError(const char* message, const char* title = nullptr, const char* btnOk = "OK", const char* btnCancel = "Cancel", std::function<void(int button)> callback = 0);
79
81
82 short CKFontToId(const char* font);
83
84 void CKAddTimer(CKTimer* timer, CKObject* owner = nullptr);
85 void CKRemoveTimer(CKTimer* timer);
87
89 void CKShowMenuBar();
90 void CKHideMenuBar();
92
93 private:
94 void __DoHousekeepingTasks();
95 void __DispatchEvent(EventRecord event);
96 inline void __HandleEvtKey(EventRecord event, bool isKeyUp, bool isAutoKey);
97 inline void __HandleEvtMouseDown(EventRecord event);
98 inline void __HandleEvtMouseUp(EventRecord event);
99 inline void __HandleEvtMouseMove(EventRecord event);
100 inline void __HandleEvtUpdate(EventRecord event);
101 inline void __HandleEvtActivate(EventRecord event);
102 inline void __HandleEvtOS(EventRecord event);
103 void __HandleMenuPropertyChange(const CKObject* obj, const char* propName);
104 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);
105
106 private:
107 int __workCount;
108 CKWindow* __lastMouseDownWindow;
109 std::vector<CKWindow*> __windows;
110 std::vector<CKWindow*> __gc_windows;
111 std::vector<CKTimer*> __timers;
112 CKMenuBar* __menubar = nullptr;
113
114 friend CKNetBaseSocket;
115 std::vector<CKNetBaseSocket*> __net_sockets;
116};
WindowPtr CKWindowPtr
Definition ckPlatform.h:62
Defines the main entry point for a Clapkit application.
Definition ckApp.h:58
void CKRemoveTimer(CKTimer *timer)
Stop and remove timer.
Definition ckApp.cpp:1003
~CKApp()
Definition ckApp.cpp:70
void CKQuit()
Definition ckApp.cpp:178
void CKAddTimer(CKTimer *timer, CKObject *owner=nullptr)
Add a new timer to the app, enabling (via Start) as you add it.
Definition ckApp.cpp:992
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:220
void CKIncreaseWork()
Show the 'Working' cursor.
Definition ckApp.cpp:458
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:321
CKWindow * CKTopMostWindow()
Return the top-most window we have.
Definition ckApp.cpp:505
void CKDecreaseWork()
Decrease work count by one, if zero, hide the 'Working' cursor.
Definition ckApp.cpp:467
int CKLoop(int waitTime=60)
Main event loop, handling events. Needs to be called as much as possible.
Definition ckApp.cpp:113
CKWindow * CKFindWindow(CKWindowPtr ptr)
Try to find a window by the WindowPtr. Probably only applies to Toolbox Mac.
Definition ckApp.cpp:243
void CKShowMenuBar()
If hidden, bring the menu bar back.
Definition ckApp.cpp:1130
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:1159
void CKRemoveWindow(CKWindow *window)
Remove and destroy window. You MUST use this function instead of deleting a window yourself.
Definition ckApp.cpp:267
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:302
CKError CKSetMenu(CKMenuBar *menu)
Set the application's menu bar.
Definition ckApp.cpp:1046
void CKHideMenuBar()
If shown, hide the menu bar.
Definition ckApp.cpp:1137
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:359
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:340
void CKRemoveTimersOfOwner(CKObject *owner)
Remove all timers of a specific owner.
Definition ckApp.cpp:1020
void CKRestoreCursor()
Changed the cursor yourself? Call this to get the default/waiting back.
Definition ckApp.cpp:481
short CKFontToId(const char *font)
Convert a font name to a font Id.
Definition ckApp.cpp:972
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:53
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:91
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
Initialization parameters for a CKWindow.
Definition ckWindow.h:46