-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
154 lines (72 loc) · 2.82 KB
/
main.c
File metadata and controls
154 lines (72 loc) · 2.82 KB
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
#include <objc/objc.h>
#include <objc/runtime.h>
#include <objc/message.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <stdbool.h>
typedef long int NSUInteger;
#define NSWindowStyleMaskTitled 15
#define NSWindowStyleMaskClosable 2
#define NSWindowStyleMaskResizable 0
#define NSBackingStoreBuffered 2
/*
NSUInteger backing = NSBackingStoreBuffered;
^~~~~~~~~~~~~~~~~~~~~~
kCGBackingStoreBuffered
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGWindow.h:28:5: note: 'kCGBackingStoreBuffered' declared here
kCGBackingStoreBuffered = 2
*/
// Helper function to send messages to Objective-C objects
id msgSend(id obj, SEL sel, ...) {
va_list args;
va_start(args, sel);
id result = ((id(*)(id, SEL, ...))objc_msgSend)(obj, sel, args);
va_end(args);
return result;
}
int main() {
// Initialize the application
id app = msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
if (!app) {
fprintf(stderr, "Failed to get sharedApplication\n");
return 1;
}
// Create a window
CGRect windowRect = {{100, 100}, {600, 400}}; // Correct CGRect creation
NSUInteger styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable;
NSUInteger backing = NSBackingStoreBuffered;
BOOL defer = NO;
id windowClass = objc_getClass("NSWindow");
if (!windowClass) {
fprintf(stderr, "Failed to get NSWindow class\n");
return 1;
}
id window = msgSend(msgSend(windowClass, sel_registerName("alloc")), sel_registerName("initWithContentRect:styleMask:backing:defer:"), windowRect, styleMask, backing, defer);
if (!window) {
fprintf(stderr, "Failed to create window\n");
return 1;
}
// Set the window title
CFStringRef title = CFSTR("Notepad Clone");
msgSend(window, sel_registerName("setTitle:"), title);
CFRelease(title); // Manual release
// Create a text view
CGRect textViewRect = {{10, 10}, {580, 380}}; // Correct CGRect creation
id textView = msgSend(msgSend((id)objc_getClass("NSTextView"), sel_registerName("alloc")), sel_registerName("initWithFrame:"), textViewRect);
if (!textView) {
fprintf(stderr, "Failed to create textView\n");
return 1;
}
// Add the text view to the window's content view
id contentView = msgSend(window, sel_registerName("contentView"));
if (!contentView) {
fprintf(stderr, "Failed to get contentView\n");
return 1;
}
msgSend(contentView, sel_registerName("addSubview:"), textView);
// Show the window
msgSend(window, sel_registerName("makeKeyAndOrderFront:"), nil);
// Run the application
msgSend(app, sel_registerName("run"));
return 0;
}