Debian Codegraph @

phoenix/cocoa/window.cpp

raw
1 @implementation CocoaWindow : NSWindow
2
3 -(id) initWith:(phoenix::Window&)windowReference {
4 window = &windowReference;
5
6 NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;
7 if(window->state.resizable) style |= NSResizableWindowMask;
8
9 if(self = [super initWithContentRect:NSMakeRect(0, 0, 640, 480) styleMask:style backing:NSBackingStoreBuffered defer:YES]) {
10 [self setDelegate:self];
11 [self setReleasedWhenClosed:NO];
12 [self setAcceptsMouseMovedEvents:YES];
13 [self setTitle:@""];
14
15 NSBundle* bundle = [NSBundle mainBundle];
16 NSDictionary* dictionary = [bundle infoDictionary];
17 NSString* applicationName = [dictionary objectForKey:@"CFBundleDisplayName"];
18 if(applicationName == nil) applicationName = [NSString stringWithUTF8String:phoenix::applicationState.name];
19
20 menuBar = [[NSMenu alloc] init];
21
22 NSMenuItem* item;
23 string text;
24
25 rootMenu = [[NSMenu alloc] init];
26 item = [[[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""] autorelease];
27 [item setSubmenu:rootMenu];
28 [menuBar addItem:item];
29
30 item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"About %@ ...", applicationName] action:@selector(menuAbout) keyEquivalent:@""] autorelease];
31 [item setTarget:self];
32 [rootMenu addItem:item];
33 [rootMenu addItem:[NSMenuItem separatorItem]];
34
35 item = [[[NSMenuItem alloc] initWithTitle:@"Preferences" action:@selector(menuPreferences) keyEquivalent:@""] autorelease];
36 [item setTarget:self];
37 [rootMenu addItem:item];
38 [rootMenu addItem:[NSMenuItem separatorItem]];
39
40 NSMenu* servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease];
41 item = [[[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""] autorelease];
42 [item setTarget:self];
43 [item setSubmenu:servicesMenu];
44 [rootMenu addItem:item];
45 [rootMenu addItem:[NSMenuItem separatorItem]];
46 [NSApp setServicesMenu:servicesMenu];
47
48 item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"Hide %@", applicationName] action:@selector(hide:) keyEquivalent:@""] autorelease];
49 [item setTarget:NSApp];
50 [rootMenu addItem:item];
51
52 item = [[[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""] autorelease];
53 [item setTarget:NSApp];
54 [rootMenu addItem:item];
55
56 item = [[[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""] autorelease];
57 [item setTarget:NSApp];
58 [rootMenu addItem:item];
59 [rootMenu addItem:[NSMenuItem separatorItem]];
60
61 item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"Quit %@", applicationName] action:@selector(menuQuit) keyEquivalent:@""] autorelease];
62 [item setTarget:self];
63 [rootMenu addItem:item];
64
65 statusBar = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
66 [statusBar setAlignment:NSLeftTextAlignment];
67 [statusBar setBordered:YES];
68 [statusBar setBezeled:YES];
69 [statusBar setBezelStyle:NSTextFieldSquareBezel];
70 [statusBar setEditable:NO];
71 [statusBar setHidden:YES];
72
73 [[self contentView] addSubview:statusBar positioned:NSWindowBelow relativeTo:nil];
74 }
75
76 return self;
77 }
78
79 -(BOOL) canBecomeKeyWindow {
80 return YES;
81 }
82
83 -(BOOL) canBecomeMainWindow {
84 return YES;
85 }
86
87 -(void) windowDidBecomeMain:(NSNotification*)notification {
88 if(window->state.menu.size() > 0) {
89 [NSApp setMainMenu:menuBar];
90 }
91 }
92
93 -(void) windowDidMove:(NSNotification*)notification {
94 window->p.moveEvent();
95 }
96
97 -(void) windowDidResize:(NSNotification*)notification {
98 window->p.sizeEvent();
99 }
100
101 -(BOOL) windowShouldClose:(id)sender {
102 if(window->onClose) window->onClose();
103 else window->setVisible(false);
104 if(window->state.modal && !window->visible()) window->setModal(false);
105 return NO;
106 }
107
108 -(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender {
109 return DropPathsOperation(sender);
110 }
111
112 -(BOOL) performDragOperation:(id<NSDraggingInfo>)sender {
113 lstring paths = DropPaths(sender);
114 if(paths.empty()) return NO;
115 if(window->onDrop) window->onDrop(paths);
116 return YES;
117 }
118
119 -(NSMenu*) menuBar {
120 return menuBar;
121 }
122
123 -(void) menuAbout {
124 using phoenix::Application;
125 if(Application::Cocoa::onAbout) Application::Cocoa::onAbout();
126 }
127
128 -(void) menuPreferences {
129 using phoenix::Application;
130 if(Application::Cocoa::onPreferences) Application::Cocoa::onPreferences();
131 }
132
133 -(void) menuQuit {
134 using phoenix::Application;
135 if(Application::Cocoa::onQuit) Application::Cocoa::onQuit();
136 }
137
138 -(NSTextField*) statusBar {
139 return statusBar;
140 }
141
142 @end
143
144 namespace phoenix {
145
146 Window& pWindow::none() {
147 static Window* window = nullptr;
148 if(window == nullptr) window = new Window;
149 return *window;
150 }
151
152 void pWindow::append(Layout& layout) {
153 Geometry geometry = window.state.geometry;
154 geometry.x = geometry.y = 0;
155 layout.setGeometry(geometry);
156
157 statusBarReposition();
158 }
159
160 void pWindow::append(Menu& menu) {
161 @autoreleasepool {
162 [[cocoaWindow menuBar] addItem:menu.p.cocoaAction];
163 }
164 }
165
166 void pWindow::append(Widget& widget) {
167 if(widget.font().empty() && !window.state.widgetFont.empty()) {
168 widget.setFont(window.state.widgetFont);
169 }
170
171 @autoreleasepool {
172 [widget.p.cocoaView removeFromSuperview];
173 [[cocoaWindow contentView] addSubview:widget.p.cocoaView positioned:NSWindowAbove relativeTo:nil];
174 widget.p.setGeometry(widget.geometry());
175 [[cocoaWindow contentView] setNeedsDisplay:YES];
176 }
177 }
178
179 bool pWindow::focused() {
180 @autoreleasepool {
181 return [cocoaWindow isMainWindow] == YES;
182 }
183 }
184
185 Geometry pWindow::frameMargin() {
186 @autoreleasepool {
187 NSRect frame = [cocoaWindow frameRectForContentRect:NSMakeRect(0, 0, 640, 480)];
188 return {abs(frame.origin.x), frame.size.height - 480, frame.size.width - 640, abs(frame.origin.y)};
189 }
190 }
191
192 Geometry pWindow::geometry() {
193 @autoreleasepool {
194 NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
195 area.size.height -= statusBarHeight();
196 return {area.origin.x, Desktop::size().height - area.origin.y - area.size.height, area.size.width, area.size.height};
197 }
198 }
199
200 void pWindow::remove(Layout& layout) {
201 @autoreleasepool {
202 [[cocoaWindow contentView] setNeedsDisplay:YES];
203 }
204 }
205
206 void pWindow::remove(Menu& menu) {
207 @autoreleasepool {
208 [[cocoaWindow menuBar] removeItem:menu.p.cocoaAction];
209 }
210 }
211
212 void pWindow::remove(Widget& widget) {
213 @autoreleasepool {
214 [widget.p.cocoaView removeFromSuperview];
215 [[cocoaWindow contentView] setNeedsDisplay:YES];
216 }
217 }
218
219 void pWindow::setBackgroundColor(Color color) {
220 @autoreleasepool {
221 [cocoaWindow
222 setBackgroundColor:[NSColor
223 colorWithCalibratedRed:color.red / 255.0
224 green:color.green / 255.0
225 blue:color.blue / 255.0
226 alpha:color.alpha / 255.0
227 ]
228 ];
229 }
230 }
231
232 void pWindow::setDroppable(bool droppable) {
233 @autoreleasepool {
234 if(droppable) {
235 [cocoaWindow registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
236 } else {
237 [cocoaWindow unregisterDraggedTypes];
238 }
239 }
240 }
241
242 void pWindow::setFocused() {
243 @autoreleasepool {
244 [cocoaWindow makeKeyAndOrderFront:nil];
245 }
246 }
247
248 void pWindow::setFullScreen(bool fullScreen) {
249 @autoreleasepool {
250 if(fullScreen == true) {
251 [NSApp setPresentationOptions:NSApplicationPresentationFullScreen];
252 [cocoaWindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
253 [cocoaWindow toggleFullScreen:nil];
254 } else {
255 [cocoaWindow toggleFullScreen:nil];
256 [cocoaWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault];
257 [NSApp setPresentationOptions:NSApplicationPresentationDefault];
258 }
259 }
260 }
261
262 void pWindow::setGeometry(Geometry geometry) {
263 locked = true;
264
265 @autoreleasepool {
266 [cocoaWindow
267 setFrame:[cocoaWindow
268 frameRectForContentRect:NSMakeRect(
269 geometry.x, Desktop::size().height - geometry.y - geometry.height,
270 geometry.width, geometry.height + statusBarHeight()
271 )
272 ]
273 display:YES
274 ];
275
276 for(auto& layout : window.state.layout) {
277 Geometry geometry = this->geometry();
278 geometry.x = geometry.y = 0;
279 layout.setGeometry(geometry);
280 }
281
282 statusBarReposition();
283 }
284
285 locked = false;
286 }
287
288 void pWindow::setMenuFont(string font) {
289 }
290
291 void pWindow::setMenuVisible(bool visible) {
292 }
293
294 void pWindow::setModal(bool modal) {
295 @autoreleasepool {
296 if(modal == true) {
297 [NSApp runModalForWindow:cocoaWindow];
298 } else {
299 [NSApp stopModal];
300 NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:NSMakePoint(0, 0) modifierFlags:0 timestamp:0.0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
301 [NSApp postEvent:event atStart:true];
302 }
303 }
304 }
305
306 void pWindow::setResizable(bool resizable) {
307 @autoreleasepool {
308 NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;
309 if(resizable) style |= NSResizableWindowMask;
310 [cocoaWindow setStyleMask:style];
311 }
312 }
313
314 void pWindow::setStatusFont(string font) {
315 @autoreleasepool {
316 [[cocoaWindow statusBar] setFont:pFont::cocoaFont(font)];
317 }
318 statusBarReposition();
319 }
320
321 void pWindow::setStatusText(string text) {
322 @autoreleasepool {
323 [[cocoaWindow statusBar] setStringValue:[NSString stringWithUTF8String:text]];
324 }
325 }
326
327 void pWindow::setStatusVisible(bool visible) {
328 @autoreleasepool {
329 [[cocoaWindow statusBar] setHidden:!visible];
330 setGeometry(geometry());
331 }
332 }
333
334 void pWindow::setTitle(string text) {
335 @autoreleasepool {
336 [cocoaWindow setTitle:[NSString stringWithUTF8String:text]];
337 }
338 }
339
340 void pWindow::setVisible(bool visible) {
341 @autoreleasepool {
342 if(visible) [cocoaWindow makeKeyAndOrderFront:nil];
343 else [cocoaWindow orderOut:nil];
344 }
345 }
346
347 void pWindow::setWidgetFont(string font) {
348 }
349
350 void pWindow::constructor() {
351 @autoreleasepool {
352 cocoaWindow = [[CocoaWindow alloc] initWith:window];
353
354 NSColor* color = [[cocoaWindow backgroundColor] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
355 window.state.backgroundColor = Color(
356 (uint8_t)(255 * [color redComponent]),
357 (uint8_t)(255 * [color greenComponent]),
358 (uint8_t)(255 * [color blueComponent]),
359 (uint8_t)(255 * [color alphaComponent])
360 );
361 }
362 }
363
364 void pWindow::destructor() {
365 @autoreleasepool {
366 [cocoaWindow release];
367 }
368 }
369
370 void pWindow::moveEvent() {
371 if(locked == false && window.fullScreen() == false && window.visible() == true) {
372 Geometry geometry = this->geometry();
373 window.state.geometry.x = geometry.x;
374 window.state.geometry.y = geometry.y;
375 }
376
377 if(locked == false) {
378 if(window.onMove) window.onMove();
379 }
380 }
381
382 void pWindow::sizeEvent() {
383 if(locked == false && window.fullScreen() == false && window.visible() == true) {
384 Geometry geometry = this->geometry();
385 window.state.geometry.width = geometry.width;
386 window.state.geometry.height = geometry.height;
387 }
388
389 for(auto& layout : window.state.layout) {
390 Geometry geometry = this->geometry();
391 geometry.x = geometry.y = 0;
392 layout.setGeometry(geometry);
393 }
394
395 statusBarReposition();
396
397 if(locked == false) {
398 if(window.onSize) window.onSize();
399 }
400 }
401
402 unsigned pWindow::statusBarHeight() {
403 if(!window.state.statusVisible) return 0;
404 return Font::size(window.state.statusFont, " ").height + 6;
405 }
406
407 void pWindow::statusBarReposition() {
408 @autoreleasepool {
409 NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
410 [[cocoaWindow statusBar] setFrame:NSMakeRect(0, 0, area.size.width, statusBarHeight())];
411 [[cocoaWindow contentView] setNeedsDisplay:YES];
412 }
413 }
414
415 }