2014-08-26 21:41:25 -05:00
|
|
|
// 26 august 2014
|
|
|
|
|
|
|
|
#include "objc_darwin.h"
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
2014-08-27 11:11:55 -05:00
|
|
|
// We would be able to just use plain old NSPopover here, but alas that steals focus.
|
|
|
|
// NSPopovers are intended for interactive content, and Apple seems to be diligent in enforcing this rule, as the known techniques for preventing a NSPopover from stealing focus no longer work in 10.9.
|
|
|
|
// Let's just fake it with a window.
|
|
|
|
|
2014-09-23 13:53:03 -05:00
|
|
|
// TODO better would be to use NSImageNameInvalidDataFreestandingTemplate somehow
|
|
|
|
|
2014-08-27 15:01:47 -05:00
|
|
|
@interface goWarningPopover : NSWindow {
|
|
|
|
@public
|
|
|
|
id onBegin;
|
|
|
|
id onEnd;
|
2014-08-29 20:09:43 -05:00
|
|
|
id textfield;
|
2014-08-29 21:45:44 -05:00
|
|
|
NSTextView *tv;
|
2014-08-27 15:01:47 -05:00
|
|
|
}
|
2014-08-27 11:11:55 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation goWarningPopover
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super initWithContentRect:NSZeroRect
|
|
|
|
styleMask:NSBorderlessWindowMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
|
|
|
[self setOpaque:NO];
|
|
|
|
[self setHasShadow:YES];
|
|
|
|
[self setExcludedFromWindowsMenu:YES];
|
|
|
|
[self setMovableByWindowBackground:NO];
|
|
|
|
[self setLevel:NSPopUpMenuWindowLevel];
|
2014-08-27 15:01:47 -05:00
|
|
|
[self setHidesOnDeactivate:YES];
|
|
|
|
self->onBegin = nil;
|
|
|
|
self->onEnd = nil;
|
2014-08-27 11:11:55 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-08-27 15:01:47 -05:00
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
if (self->onBegin != nil) {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self->onBegin];
|
|
|
|
self->onBegin = nil;
|
|
|
|
}
|
|
|
|
if (self->onEnd != nil) {
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self->onEnd];
|
|
|
|
self->onEnd = nil;
|
|
|
|
}
|
2014-08-29 21:45:44 -05:00
|
|
|
if (self->tv != nil)
|
|
|
|
[self->tv removeObserver:self forKeyPath:@"delegate"];
|
2014-08-27 15:01:47 -05:00
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
2014-08-27 11:11:55 -05:00
|
|
|
- (BOOL)canBecomeKeyWindow
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeMainWindow
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2014-08-29 20:09:43 -05:00
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
|
|
|
{
|
2014-08-29 21:45:44 -05:00
|
|
|
if ([self->tv delegate] == self->textfield)
|
|
|
|
[self orderFront:self];
|
|
|
|
else
|
|
|
|
[self orderOut:self];
|
2014-08-29 20:09:43 -05:00
|
|
|
}
|
|
|
|
|
2014-08-27 11:11:55 -05:00
|
|
|
@end
|
|
|
|
|
2014-08-26 21:41:25 -05:00
|
|
|
@interface goWarningView : NSView {
|
|
|
|
@public
|
|
|
|
NSImageView *icon;
|
|
|
|
NSTextField *label;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation goWarningView
|
|
|
|
|
|
|
|
- (void)sizeToFitAndArrange
|
|
|
|
{
|
|
|
|
[self->label sizeToFit];
|
|
|
|
|
|
|
|
CGFloat labelheight, imageheight;
|
|
|
|
CGFloat targetwidth, imagewidth;
|
|
|
|
|
|
|
|
labelheight = [self->label frame].size.height;
|
|
|
|
imageheight = [[self->icon image] size].height;
|
|
|
|
imagewidth = [[self->icon image] size].width;
|
|
|
|
targetwidth = (imagewidth * labelheight) / imageheight;
|
|
|
|
|
|
|
|
[self->icon setFrameSize:NSMakeSize(targetwidth, labelheight)];
|
|
|
|
|
|
|
|
[self setFrameSize:NSMakeSize(targetwidth + [self->label frame].size.width, labelheight)];
|
|
|
|
[self->icon setFrameOrigin:NSMakePoint(0, 0)];
|
|
|
|
[self->label setFrameOrigin:NSMakePoint(targetwidth, 0)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
id newWarningPopover(char *text)
|
|
|
|
{
|
|
|
|
goWarningView *wv;
|
|
|
|
|
|
|
|
wv = [[goWarningView alloc] initWithFrame:NSZeroRect];
|
|
|
|
|
|
|
|
wv->icon = [[NSImageView alloc] initWithFrame:NSZeroRect];
|
|
|
|
[wv->icon setImage:[NSImage imageNamed:NSImageNameCaution]];
|
|
|
|
[wv->icon setImageFrameStyle:NSImageFrameNone];
|
2014-08-27 17:29:54 -05:00
|
|
|
[wv->icon setImageAlignment:NSImageAlignCenter];
|
2014-08-26 21:41:25 -05:00
|
|
|
[wv->icon setImageScaling:NSImageScaleProportionallyUpOrDown];
|
|
|
|
[wv->icon setEditable:NO];
|
|
|
|
[wv->icon setAnimates:NO];
|
|
|
|
[wv->icon setAllowsCutCopyPaste:NO];
|
|
|
|
[wv->icon setRefusesFirstResponder:YES];
|
|
|
|
|
|
|
|
wv->label = (NSTextField *) newLabel();
|
2014-08-27 12:17:31 -05:00
|
|
|
textfieldSetText((id) wv->label, text);
|
2014-08-26 21:41:25 -05:00
|
|
|
[wv->label setRefusesFirstResponder:YES];
|
|
|
|
|
|
|
|
[wv addSubview:wv->icon];
|
|
|
|
[wv addSubview:wv->label];
|
|
|
|
[wv sizeToFitAndArrange];
|
|
|
|
|
2014-08-27 11:11:55 -05:00
|
|
|
goWarningPopover *popover;
|
2014-08-26 21:41:25 -05:00
|
|
|
|
2014-08-27 11:11:55 -05:00
|
|
|
popover = [[goWarningPopover alloc] init]; // explicitly use our initializer
|
|
|
|
[[popover contentView] addSubview:wv];
|
2014-08-26 21:41:25 -05:00
|
|
|
[popover setContentSize:[wv frame].size];
|
|
|
|
|
|
|
|
return (id) popover;
|
|
|
|
}
|
2014-08-27 11:11:55 -05:00
|
|
|
|
|
|
|
void warningPopoverShow(id popover, id control)
|
|
|
|
{
|
|
|
|
goWarningPopover *p = (goWarningPopover *) popover;
|
|
|
|
NSView *v = (NSView *) control;
|
|
|
|
NSRect vr;
|
|
|
|
NSPoint vo;
|
|
|
|
|
2014-08-27 15:01:47 -05:00
|
|
|
// note that the frame is a rect of the superview
|
|
|
|
vr = [[v superview] convertRect:[v frame] toView:nil];
|
2014-08-27 11:11:55 -05:00
|
|
|
vo = [[v window] convertRectToScreen:vr].origin;
|
|
|
|
[p setFrameOrigin:NSMakePoint(vo.x, vo.y - [p frame].size.height)];
|
2014-08-29 20:09:43 -05:00
|
|
|
p->textfield = control;
|
2014-08-29 21:45:44 -05:00
|
|
|
p->tv = (NSTextView *) [[v window] fieldEditor:NO forObject:nil];
|
|
|
|
// thanks to http://stackoverflow.com/a/25562783/3408572 for suggesting KVO here
|
|
|
|
[p->tv addObserver:p forKeyPath:@"delegate" options:NSKeyValueObservingOptionNew context:NULL];
|
2014-08-27 11:11:55 -05:00
|
|
|
[p orderFront:p];
|
|
|
|
}
|