forked from andlabs/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysdata_darwin.m
231 lines (190 loc) · 5.62 KB
/
sysdata_darwin.m
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 12 may 2014
#include "objc_darwin.h"
#import <Foundation/NSGeometry.h>
// see delegateuitask_darwin.m
// in this case, lots of headers include NSApplication.h
#import <AppKit/NSView.h>
#import <AppKit/NSControl.h>
#ifdef MAC_OS_X_VERSION_10_7
#undef MAC_OS_X_VERSION_MIN_REQUIRED
#undef MAC_OS_X_VERSION_MAX_ALLOWED
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_7
#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7
#endif
#import <AppKit/NSApplication.h>
#undef MAC_OS_X_VERSION_MIN_REQUIRED
#undef MAC_OS_X_VERSION_MAX_ALLOWED
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6
#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_6
#import <AppKit/NSWindow.h>
#import <AppKit/NSView.h>
#import <AppKit/NSFont.h>
#import <AppKit/NSControl.h>
#import <AppKit/NSButton.h>
#import <AppKit/NSTextField.h>
#import <AppKit/NSSecureTextField.h>
#import <AppKit/NSProgressIndicator.h>
#import <AppKit/NSScrollView.h>
extern NSRect dummyRect;
#define to(T, x) ((T *) (x))
#define toNSWindow(x) to(NSWindow, (x))
#define toNSView(x) to(NSView, (x))
#define toNSControl(x) to(NSControl, (x))
#define toNSButton(x) to(NSButton, (x))
#define toNSTextField(x) to(NSTextField, (x))
#define toNSProgressIndicator(x) to(NSProgressIndicator, (x))
#define toNSScrollView(x) to(NSScrollView, (x))
#define toNSInteger(x) ((NSInteger) (x))
#define fromNSInteger(x) ((intptr_t) (x))
#define inScrollView(x) ([toNSScrollView((x)) documentView])
#define areaInScrollView(x) inScrollView((x))
void addControl(id parentWindow, id control)
{
[[toNSWindow(parentWindow) contentView] addSubview:control];
}
void controlShow(id what)
{
[toNSView(what) setHidden:NO];
}
void controlHide(id what)
{
[toNSView(what) setHidden:YES];
}
#define systemFontOfSize(s) ([NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:(s)]])
void applyStandardControlFont(id what)
{
[toNSControl(what) setFont:systemFontOfSize(NSRegularControlSize)];
}
id makeWindow(id delegate)
{
NSWindow *w;
w = [[NSWindow alloc]
initWithContentRect:dummyRect
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
backing:NSBackingStoreBuffered
defer:YES]; // defer creation of device until we show the window
[w setDelegate:delegate];
// we do not need setAcceptsMouseMovedEvents: here since we are using a tracking rect in Areas for that
return w;
}
void windowShow(id window)
{
[toNSWindow(window) makeKeyAndOrderFront:window];
}
void windowHide(id window)
{
[toNSWindow(window) orderOut:window];
}
void windowSetTitle(id window, id title)
{
[toNSWindow(window) setTitle:title];
}
id windowTitle(id window)
{
return [toNSWindow(window) title];
}
id makeButton(void)
{
NSButton *button;
button = [[NSButton alloc]
initWithFrame:dummyRect];
[button setBezelStyle:NSRoundedBezelStyle];
return button;
}
void buttonSetTargetAction(id button, id delegate)
{
[toNSButton(button) setTarget:delegate];
[toNSButton(button) setAction:@selector(buttonClicked:)];
}
void buttonSetText(id button, id text)
{
[toNSButton(button) setTitle:text];
}
id buttonText(id button)
{
return [toNSButton(button) title];
}
id makeCheckbox(void)
{
NSButton *checkbox;
checkbox = [[NSButton alloc]
initWithFrame:dummyRect];
[checkbox setButtonType:NSSwitchButton];
return checkbox;
}
id makeLineEdit(BOOL password)
{
if (password)
return [[NSSecureTextField alloc]
initWithFrame:dummyRect];
return [[NSTextField alloc]
initWithFrame:dummyRect];
}
void lineeditSetText(id lineedit, id text)
{
[toNSTextField(lineedit) setStringValue:text];
}
id lineeditText(id lineedit)
{
return [toNSTextField(lineedit) stringValue];
}
id makeLabel(void)
{
NSTextField *label;
label = [[NSTextField alloc]
initWithFrame:dummyRect];
[label setEditable:NO];
[label setBordered:NO];
[label setDrawsBackground:NO];
// this disables both word wrap AND ellipsizing in one fell swoop
// we have to send to the control's cell for this
[[label cell] setLineBreakMode:NSLineBreakByClipping];
// for a multiline label, we either use WordWrapping and send setTruncatesLastVisibleLine: to disable ellipsizing OR use one of those ellipsizing styles
return label;
}
id makeProgressBar(void)
{
NSProgressIndicator *pbar;
pbar = [[NSProgressIndicator alloc]
initWithFrame:dummyRect];
[pbar setStyle:NSProgressIndicatorBarStyle];
[pbar setIndeterminate:NO];
[pbar stopAnimation:pbar];
return pbar;
}
void setRect(id what, intptr_t x, intptr_t y, intptr_t width, intptr_t height)
{
[toNSView(what) setFrame:NSMakeRect((CGFloat) x, (CGFloat) y, (CGFloat) width, (CGFloat) height)];
}
BOOL isCheckboxChecked(id checkbox)
{
return [toNSButton(checkbox) state] == NSOnState;
}
void windowSetContentSize(id window, intptr_t width, intptr_t height)
{
NSWindow *win;
win = toNSWindow(window);
// use -[NSWindow setContentSize:], which will resize the window without taking the titlebar as part of the given size and without needing us to consider the window's position (the function takes care of both for us)
[win setContentSize:NSMakeSize((CGFloat) width, (CGFloat) height)];
[win display]; // TODO needed?
}
void setProgress(id pbar, intptr_t percent)
{
NSProgressIndicator *p;
p = toNSProgressIndicator(pbar);
if (percent == -1) {
[p setIndeterminate:YES];
[p startAnimation:p];
return;
}
[p stopAnimation:p]; // will have no effect if we were already determinate
[p setIndeterminate:NO];
[p setDoubleValue:((double) percent)];
}
void setAreaSize(id scrollview, intptr_t width, intptr_t height)
{
NSView *area;
area = areaInScrollView(scrollview);
[area setFrame:NSMakeRect(0, 0, (CGFloat) width, (CGFloat) height)];
[area display]; // and redraw
}