Skip to content

Instantly share code, notes, and snippets.

@unxmaal
Created December 25, 2023 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unxmaal/08f77af0eb89491d9eb37360d8548234 to your computer and use it in GitHub Desktop.
Save unxmaal/08f77af0eb89491d9eb37360d8548234 to your computer and use it in GitHub Desktop.
# Carbon Events API C++ Project for Mac OS 9
## Project Files
1. **Main.cpp** - Main application source file.
2. **HelloWorld.r** - Resource file for defining UI elements.
## Main.cpp
```cpp
#include <Carbon/Carbon.h>
// Function Prototypes
OSStatus MyWindowEventHandler(EventHandlerCallRef callRef, EventRef event, void *userData);
void CreateMyWindow();
void CreateButton(WindowPtr window);
int main(int argc, char* argv[]) {
IBNibRef nibRef;
OSStatus err;
// Initialize the Toolbox
err = MoreMasterPointers(64);
if (err == noErr) {
err = InitCursor();
}
// Create the main window
CreateMyWindow();
// Run the event loop
RunApplicationEventLoop();
return 0;
}
void CreateMyWindow() {
Rect windowRect = {100, 100, 300, 400}; // Position and dimensions of the window
WindowRef window;
CreateNewWindow(kDocumentWindowClass, kWindowStandardDocumentAttributes, &windowRect, &window);
SetWindowTitleWithCFString(window, CFSTR("Hello World Window"));
ShowWindow(window);
// Create a button
CreateButton(window);
// Set up event handler for the window
EventTypeSpec eventTypes[] = {
{ kEventClassCommand, kEventCommandProcess }
};
InstallWindowEventHandler(window, NewEventHandlerUPP(MyWindowEventHandler), GetEventTypeCount(eventTypes), eventTypes, (void *)window, NULL);
}
void CreateButton(WindowPtr window) {
ControlRef button;
Rect buttonBounds = { 110, 110, 140, 180 }; // Position and dimensions of the button
CreatePushButtonControl(window, &buttonBounds, CFSTR("Click Me"), &button);
SetControlCommandID(button, 'bttn');
}
OSStatus MyWindowEventHandler(EventHandlerCallRef callRef, EventRef event, void *userData) {
OSStatus err = eventNotHandledErr;
HICommand command;
if (GetEventClass(event) == kEventClassCommand && GetEventKind(event) == kEventCommandProcess) {
GetEventParameter(event, kEventParamDirectObject, typeHICommand, NULL, sizeof(command), NULL, &command);
if (command.commandID == 'bttn') {
// Code to display "Hello"
CFUserNotificationDisplayNotice(0, kCFUserNotificationNoteAlertLevel, NULL, NULL, NULL, CFSTR("Hello"), CFSTR("Button clicked!"), NULL);
err = noErr;
}
}
return err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment