-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
109 lines (90 loc) · 2.63 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
// Reference to the .rsrc file ID
#define GUI_IMAGE_ID 128
short quitting = 0;
void draw_window(void);
int
main() {
// Counter-clockwise, reverse of CSS
Rect win_bounds = {
50,
30,
0,
0
};
WindowPtr win;
char title[] = "GUI App Example";
// If we use a Pascal string, we need to structure it like this
//Str255 title = "\pOur app";
// These are all initializations of built-in libraries we need from MacTrap, etc.
InitGraf( & thePort);
InitFonts();
FlushEvents(everyEvent, 0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(0);
InitCursor();
MaxApplZone();
win_bounds.right = screenBits.bounds.right - 30;
win_bounds.bottom = screenBits.bounds.bottom - 10;
// Definition of our new window. CtoPstr converts our C char to a Pascal title
win = NewWindow(0 L, & win_bounds, CtoPstr(title), true,
documentProc, (WindowPtr) - 1 L, true, 0);
if (!win)
ExitToShell();
SetPort(win);
draw_window();
while (!quitting) {
EventRecord event;
WindowPtr event_win;
short event_in;
WaitNextEvent(everyEvent, & event, 10 L, 0);
event_in = FindWindow(event.where, & event_win);
switch (event.what) {
case mouseDown:
switch (event_in) {
case inSysWindow:
SystemClick( & event, event_win);
break;
case inDrag:
// Handles dragging the window
DragWindow(event_win, event.where, & screenBits.bounds);
break;
case inGoAway:
// Handles if you click and hold on the close button, but drag away, cancelling the action
if (TrackGoAway(event_win, event.where))
quitting = 1;
break;
}
break;
case updateEvt:
BeginUpdate(event_win);
// tells the system that you're going to redraw the window
draw_window();
EndUpdate(event_win);
break;
}
}
}
// This is a function that handles redrawing the window if you draw it off-screen, then back on-screen
void
draw_window(void) {
// Handles are a pointer to a pointer.
// You get a pointer to memory, but memory can move around. The data for pic
// will move around, such as with new memory allocation, or defragmentation
PicHandle pic;
Rect pic_rect = {
0
};
pic = GetPicture(GUI_IMAGE_ID);
if (!pic) {
quitting = 1;
return;
// We could also call ExitToShell here, but this ends the function more cleanly
}
// Windows don't actually exist, you're just drawing wherever the window's coordinates happen to be.
// Coords are relative to the port
pic_rect.right = ( ** (pic)).picFrame.right;
pic_rect.bottom = ( ** (pic)).picFrame.bottom;
DrawPicture(pic, & pic_rect);
}