File: window.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (418 lines) | stat: -rw-r--r-- 12,498 bytes parent folder | download
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#if defined(Hiro_Window)

@implementation CocoaWindow : NSWindow

-(id) initWith:(hiro::mWindow&)windowReference {
  window = &windowReference;

  NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;
  if(window->state.resizable) style |= NSResizableWindowMask;

  if(self = [super initWithContentRect:NSMakeRect(0, 0, 640, 480) styleMask:style backing:NSBackingStoreBuffered defer:YES]) {
    [self setDelegate:self];
    [self setReleasedWhenClosed:NO];
    [self setAcceptsMouseMovedEvents:YES];
    [self setTitle:@""];

    NSBundle* bundle = [NSBundle mainBundle];
    NSDictionary* dictionary = [bundle infoDictionary];
    NSString* applicationName = [dictionary objectForKey:@"CFBundleDisplayName"];
    if(applicationName == nil) applicationName = [NSString stringWithUTF8String:hiro::Application::state.name];

    menuBar = [[NSMenu alloc] init];

    NSMenuItem* item;
    string text;

    rootMenu = [[NSMenu alloc] init];
    item = [[[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""] autorelease];
    [item setSubmenu:rootMenu];
    [menuBar addItem:item];

    item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"About %@ ...", applicationName] action:@selector(menuAbout) keyEquivalent:@""] autorelease];
    [item setTarget:self];
    [rootMenu addItem:item];
    [rootMenu addItem:[NSMenuItem separatorItem]];

    item = [[[NSMenuItem alloc] initWithTitle:@"Preferences ..." action:@selector(menuPreferences) keyEquivalent:@""] autorelease];
    [item setTarget:self];
    [rootMenu addItem:item];

    string result = nall::execute("defaults", "read", "/Library/Preferences/com.apple.security", "GKAutoRearm").strip();
    if(result != "0") {
      disableGatekeeperAutoRearm = [[[NSMenuItem alloc] initWithTitle:@"Disable Gatekeeper Auto-Rearm" action:@selector(menuDisableGatekeeperAutoRearm) keyEquivalent:@""] autorelease];
      [disableGatekeeperAutoRearm setTarget:self];
      [rootMenu addItem:disableGatekeeperAutoRearm];
    }

    [rootMenu addItem:[NSMenuItem separatorItem]];

    NSMenu* servicesMenu = [[[NSMenu alloc] initWithTitle:@"Services"] autorelease];
    item = [[[NSMenuItem alloc] initWithTitle:@"Services" action:nil keyEquivalent:@""] autorelease];
    [item setTarget:self];
    [item setSubmenu:servicesMenu];
    [rootMenu addItem:item];
    [rootMenu addItem:[NSMenuItem separatorItem]];
    [NSApp setServicesMenu:servicesMenu];

    item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"Hide %@", applicationName] action:@selector(hide:) keyEquivalent:@""] autorelease];
    [item setTarget:NSApp];
    [rootMenu addItem:item];

    item = [[[NSMenuItem alloc] initWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""] autorelease];
    [item setTarget:NSApp];
    [rootMenu addItem:item];

    item = [[[NSMenuItem alloc] initWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""] autorelease];
    [item setTarget:NSApp];
    [rootMenu addItem:item];

    [rootMenu addItem:[NSMenuItem separatorItem]];

    item = [[[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"Quit %@", applicationName] action:@selector(menuQuit) keyEquivalent:@""] autorelease];
    [item setTarget:self];
    [rootMenu addItem:item];

    statusBar = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)];
    [statusBar setAlignment:NSLeftTextAlignment];
    [statusBar setBordered:YES];
    [statusBar setBezeled:YES];
    [statusBar setBezelStyle:NSTextFieldSquareBezel];
    [statusBar setEditable:NO];
    [statusBar setHidden:YES];

    [[self contentView] addSubview:statusBar positioned:NSWindowBelow relativeTo:nil];
  }

  return self;
}

-(BOOL) canBecomeKeyWindow {
  return YES;
}

-(BOOL) canBecomeMainWindow {
  return YES;
}

-(void) windowDidBecomeMain:(NSNotification*)notification {
  if(window->state.menuBar) {
    [NSApp setMainMenu:menuBar];
  }
}

-(void) windowDidMove:(NSNotification*)notification {
  if(auto p = window->self()) p->moveEvent();
}

-(void) windowDidResize:(NSNotification*)notification {
  if(auto p = window->self()) p->sizeEvent();
}

-(BOOL) windowShouldClose:(id)sender {
  if(window->state.onClose) window->doClose();
  else window->setVisible(false);
  if(window->state.modal && !window->visible()) window->setModal(false);
  return NO;
}

-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender {
  return DropPathsOperation(sender);
}

-(BOOL) performDragOperation:(id<NSDraggingInfo>)sender {
  lstring paths = DropPaths(sender);
  if(paths.empty()) return NO;
  window->doDrop(paths);
  return YES;
}

-(NSMenu*) menuBar {
  return menuBar;
}

-(void) menuAbout {
  hiro::Application::Cocoa::doAbout();
}

-(void) menuPreferences {
  hiro::Application::Cocoa::doPreferences();
}

-(void) menuDisableGatekeeperAutoRearm {
  NSAlert* alert = [[[NSAlert alloc] init] autorelease];
  [alert setMessageText:@"Disable Gatekeeper Auto-Rearm"];

  nall::execute("sudo", "defaults", "write", "/Library/Preferences/com.apple.security", "GKAutoRearm", "-bool", "NO");
  if(nall::execute("defaults", "read", "/Library/Preferences/com.apple.security", "GKAutoRearm").strip() == "0") {
    [alert setAlertStyle:NSInformationalAlertStyle];
    [alert setInformativeText:@"Gatekeeper's automatic 30-day rearm behavior has been disabled successfully."];
    [disableGatekeeperAutoRearm setHidden:YES];
  } else {
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert setInformativeText:@"Error: failed to disable Gatekeeper's automatic rearm behavior."];
  }

  [alert addButtonWithTitle:@"Ok"];
  [alert runModal];
}

-(void) menuQuit {
  hiro::Application::Cocoa::doQuit();
}

-(NSTextField*) statusBar {
  return statusBar;
}

@end

namespace hiro {

auto pWindow::construct() -> void {
  @autoreleasepool {
    cocoaWindow = [[CocoaWindow alloc] initWith:self()];

    static bool once = true;
    if(once) {
      once = false;
      [NSApp setMainMenu:[cocoaWindow menuBar]];
    }
  }
}

auto pWindow::destruct() -> void {
  @autoreleasepool {
    [cocoaWindow release];
  }
}

auto pWindow::append(sLayout layout) -> void {
  layout->setGeometry(self().geometry().setPosition(0, 0));
  statusBarReposition();
}

auto pWindow::append(sMenuBar menuBar) -> void {
}

auto pWindow::append(sStatusBar statusBar) -> void {
  statusBar->setEnabled(statusBar->enabled(true));
  statusBar->setFont(statusBar->font(true));
  statusBar->setText(statusBar->text());
  statusBar->setVisible(statusBar->visible(true));
}

auto pWindow::focused() const -> bool {
  @autoreleasepool {
    return [cocoaWindow isMainWindow] == YES;
  }
}

auto pWindow::frameMargin() const -> Geometry {
  @autoreleasepool {
    NSRect frame = [cocoaWindow frameRectForContentRect:NSMakeRect(0, 0, 640, 480)];
    return {abs(frame.origin.x), (int)(frame.size.height - 480), (int)(frame.size.width - 640), abs(frame.origin.y)};
  }
}

auto pWindow::remove(sLayout layout) -> void {
  @autoreleasepool {
    [[cocoaWindow contentView] setNeedsDisplay:YES];
  }
}

auto pWindow::remove(sMenuBar menuBar) -> void {
}

auto pWindow::remove(sStatusBar statusBar) -> void {
  @autoreleasepool {
    [[cocoaWindow statusBar] setHidden:YES];
  }
}

auto pWindow::setBackgroundColor(Color color) -> void {
  @autoreleasepool {
    [cocoaWindow
      setBackgroundColor:[NSColor
        colorWithCalibratedRed:color.red() / 255.0
        green:color.green() / 255.0
        blue:color.blue() / 255.0
        alpha:color.alpha() / 255.0
      ]
    ];
  }
}

auto pWindow::setDroppable(bool droppable) -> void {
  @autoreleasepool {
    if(droppable) {
      [cocoaWindow registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
    } else {
      [cocoaWindow unregisterDraggedTypes];
    }
  }
}

auto pWindow::setFocused() -> void {
  @autoreleasepool {
    [cocoaWindow makeKeyAndOrderFront:nil];
  }
}

auto pWindow::setFullScreen(bool fullScreen) -> void {
  @autoreleasepool {
    if(fullScreen) {
      windowedGeometry = state().geometry;
      [NSApp setPresentationOptions:NSApplicationPresentationFullScreen];
      [cocoaWindow setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
      [cocoaWindow toggleFullScreen:nil];
      state().geometry = _geometry();
    } else {
      [cocoaWindow toggleFullScreen:nil];
      [cocoaWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault];
      [NSApp setPresentationOptions:NSApplicationPresentationDefault];
      state().geometry = windowedGeometry;
    }
  }
}

auto pWindow::setGeometry(Geometry geometry) -> void {
  lock();

  @autoreleasepool {
    [cocoaWindow
      setFrame:[cocoaWindow
        frameRectForContentRect:NSMakeRect(
          geometry.x(), Desktop::size().height() - geometry.y() - geometry.height(),
          geometry.width(), geometry.height() + statusBarHeight()
        )
      ]
      display:YES
    ];

    if(auto& layout = state().layout) {
      layout->setGeometry(self().geometry().setPosition(0, 0));
    }

    statusBarReposition();
  }

  unlock();
}

auto pWindow::setModal(bool modal) -> void {
  @autoreleasepool {
    if(modal == true) {
      [NSApp runModalForWindow:cocoaWindow];
    } else {
      [NSApp stopModal];
      NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined location:NSMakePoint(0, 0) modifierFlags:0 timestamp:0.0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
      [NSApp postEvent:event atStart:true];
    }
  }
}

auto pWindow::setResizable(bool resizable) -> void {
  @autoreleasepool {
    NSUInteger style = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask;
    if(resizable) style |= NSResizableWindowMask;
    [cocoaWindow setStyleMask:style];
  }
}

auto pWindow::setTitle(const string& text) -> void {
  @autoreleasepool {
    [cocoaWindow setTitle:[NSString stringWithUTF8String:text]];
  }
}

auto pWindow::setVisible(bool visible) -> void {
  @autoreleasepool {
    if(visible) [cocoaWindow makeKeyAndOrderFront:nil];
    else [cocoaWindow orderOut:nil];
  }
}

auto pWindow::moveEvent() -> void {
  if(!locked() && !self().fullScreen() && self().visible()) {
    @autoreleasepool {
      NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
      area.size.height -= statusBarHeight();
      state().geometry.setX(area.origin.x);
      state().geometry.setY(Desktop::size().height() - area.origin.y - area.size.height);
    }
  }

  if(!locked()) self().doMove();
}

auto pWindow::sizeEvent() -> void {
  if(!locked() && !self().fullScreen() && self().visible()) {
    @autoreleasepool {
      NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
      area.size.height -= statusBarHeight();
      state().geometry.setWidth(area.size.width);
      state().geometry.setHeight(area.size.height);
    }
  }

  if(auto& layout = state().layout) {
    layout->setGeometry(self().geometry().setPosition(0, 0));
  }

  statusBarReposition();

  if(!locked()) self().doSize();
}

auto pWindow::statusBarHeight() -> uint {
  if(auto& statusBar = state().statusBar) {
    if(statusBar->visible()) {
      return pFont::size(statusBar->font(true), " ").height() + 6;
    }
  }
  return 0;
}

auto pWindow::statusBarReposition() -> void {
  @autoreleasepool {
    NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
    [[cocoaWindow statusBar] setFrame:NSMakeRect(0, 0, area.size.width, statusBarHeight())];
    [[cocoaWindow contentView] setNeedsDisplay:YES];
  }
}

auto pWindow::_append(mWidget& widget) -> void {
  @autoreleasepool {
    if(auto pWidget = widget.self()) {
      [pWidget->cocoaView removeFromSuperview];
      [[cocoaWindow contentView] addSubview:pWidget->cocoaView positioned:NSWindowAbove relativeTo:nil];
      pWidget->setGeometry(widget.geometry());
      [[cocoaWindow contentView] setNeedsDisplay:YES];
    }
  }
}

auto pWindow::_geometry() -> Geometry {
  @autoreleasepool {
    NSRect area = [cocoaWindow contentRectForFrameRect:[cocoaWindow frame]];
    area.size.height -= statusBarHeight();
    return {
      (int)area.origin.x, (int)(Monitor::geometry(Monitor::primary()).height() - area.origin.y - area.size.height),
      (int)area.size.width, (int)area.size.height
    };
  }
}

/*
auto pWindow::remove(Widget& widget) -> void {
  @autoreleasepool {
    [widget.p.cocoaView removeFromSuperview];
    [[cocoaWindow contentView] setNeedsDisplay:YES];
  }
}
*/

}

#endif