-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
163 lines (132 loc) · 5.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// main.c
// c64carbon
//
// Created by Alan Steremberg on 11/17/07.
// Copyright __MyCompanyName__ 2007. All rights reserved.
//
#if 0
#include <Carbon/Carbon.h>
static OSStatus AppEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon );
static OSStatus HandleNew();
static OSStatus WindowEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon );
static IBNibRef sNibRef;
//--------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
OSStatus err;
static const EventTypeSpec kAppEvents[] =
{
{ kEventClassCommand, kEventCommandProcess }
};
// Create a Nib reference, passing the name of the nib file (without the .nib extension).
// CreateNibReference only searches into the application bundle.
err = CreateNibReference( CFSTR("main"), &sNibRef );
require_noerr( err, CantGetNibRef );
// Once the nib reference is created, set the menu bar. "MainMenu" is the name of the menu bar
// object. This name is set in InterfaceBuilder when the nib is created.
err = SetMenuBarFromNib( sNibRef, CFSTR("MenuBar") );
require_noerr( err, CantSetMenuBar );
// Install our handler for common commands on the application target
InstallApplicationEventHandler( NewEventHandlerUPP( AppEventHandler ),
GetEventTypeCount( kAppEvents ), kAppEvents,
0, NULL );
// Create a new window. A full-fledged application would do this from an AppleEvent handler
// for kAEOpenApplication.
HandleNew();
// Run the event loop
RunApplicationEventLoop();
CantSetMenuBar:
CantGetNibRef:
return err;
}
//--------------------------------------------------------------------------------------------
static OSStatus
AppEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon )
{
OSStatus result = eventNotHandledErr;
switch ( GetEventClass( inEvent ) )
{
case kEventClassCommand:
{
HICommandExtended cmd;
verify_noerr( GetEventParameter( inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof( cmd ), NULL, &cmd ) );
switch ( GetEventKind( inEvent ) )
{
case kEventCommandProcess:
switch ( cmd.commandID )
{
case kHICommandNew:
result = HandleNew();
break;
// Add your own command-handling cases here
default:
break;
}
break;
}
break;
}
default:
break;
}
return result;
}
//--------------------------------------------------------------------------------------------
DEFINE_ONE_SHOT_HANDLER_GETTER( WindowEventHandler )
//--------------------------------------------------------------------------------------------
static OSStatus
HandleNew()
{
OSStatus err;
WindowRef window;
static const EventTypeSpec kWindowEvents[] =
{
{ kEventClassCommand, kEventCommandProcess }
};
// Create a window. "MainWindow" is the name of the window object. This name is set in
// InterfaceBuilder when the nib is created.
err = CreateWindowFromNib( sNibRef, CFSTR("MainWindow"), &window );
require_noerr( err, CantCreateWindow );
// Install a command handler on the window. We don't use this handler yet, but nearly all
// Carbon apps will need to handle commands, so this saves everyone a little typing.
InstallWindowEventHandler( window, GetWindowEventHandlerUPP(),
GetEventTypeCount( kWindowEvents ), kWindowEvents,
window, NULL );
// Position new windows in a staggered arrangement on the main screen
RepositionWindow( window, NULL, kWindowCascadeOnMainScreen );
// The window was created hidden, so show it
ShowWindow( window );
CantCreateWindow:
return err;
}
//--------------------------------------------------------------------------------------------
static OSStatus
WindowEventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon )
{
OSStatus err = eventNotHandledErr;
switch ( GetEventClass( inEvent ) )
{
case kEventClassCommand:
{
HICommandExtended cmd;
verify_noerr( GetEventParameter( inEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof( cmd ), NULL, &cmd ) );
switch ( GetEventKind( inEvent ) )
{
case kEventCommandProcess:
switch ( cmd.commandID )
{
// Add your own command-handling cases here
default:
break;
}
break;
}
break;
}
default:
break;
}
return err;
}
#endif