From 83f423a43ef87c34e09470f36eefc17ac57033d6 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 26 Aug 2014 22:41:25 -0400 Subject: [PATCH] Cleaned up the TextField.Invalid() popover on Mac OS X a fair bit. --- redo/basicctrls_darwin.m | 37 +---------------- redo/objc_darwin.h | 3 ++ redo/warningpopover_darwin.m | 78 ++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 36 deletions(-) create mode 100644 redo/warningpopover_darwin.m diff --git a/redo/basicctrls_darwin.m b/redo/basicctrls_darwin.m index 1f4b570..8110fb8 100644 --- a/redo/basicctrls_darwin.m +++ b/redo/basicctrls_darwin.m @@ -168,46 +168,11 @@ void textFieldSetText(id t, char *text) id textfieldOpenInvalidPopover(id textfield, char *reason) { - // step 1: set up the display - NSTextField *label; - NSTextAttachmentCell *cell; - NSTextAttachment *attachment; - NSAttributedString *strImage; - NSAttributedString *strText; - NSFont *font; - NSMutableAttributedString *str; - - // method thanks to Anne in http://stackoverflow.com/a/5303517/3408572 - // TODO improve appearance - label = toNSTextField(newLabel()); - cell = [[NSTextAttachmentCell alloc] initImageCell:[NSImage imageNamed:NSImageNameCaution]]; - attachment = [NSTextAttachment new]; - [attachment setAttachmentCell:cell]; - strImage = [NSAttributedString attributedStringWithAttachment:attachment]; - font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]; - strText = [[NSAttributedString alloc] initWithString:[NSString stringWithUTF8String:reason] attributes:[[font fontDescriptor] fontAttributes]]; - str = [[NSMutableAttributedString alloc] initWithAttributedString:strImage]; - [str appendAttributedString:strText]; - [label setAttributedStringValue:str]; - - // step 2: set up the popover NSPopover *popover; - NSViewController *vc; - vc = [NSViewController new]; - [vc setView:label]; - popover = [NSPopover new]; - [popover setContentViewController:vc]; - [label sizeToFit]; - [popover setContentSize:[label frame].size]; - - // step 3: show the popover - // NSMaxYEdge is the bottom edge when looking (maximum edge in window coordinates) + popover = (NSPopover *) newWarningPopover(reason); [popover showRelativeToRect:NSZeroRect ofView:toNSView(textfield) preferredEdge:NSMaxYEdge]; - - // and beep NSBeep(); - return (id) popover; } diff --git a/redo/objc_darwin.h b/redo/objc_darwin.h index a8bf282..411b0cb 100644 --- a/redo/objc_darwin.h +++ b/redo/objc_darwin.h @@ -143,4 +143,7 @@ extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t); /* dialog_darwin.m */ extern void openFile(id, void *); +/* warningpopover_darwin.m */ +extern id newWarningPopover(char *); + #endif diff --git a/redo/warningpopover_darwin.m b/redo/warningpopover_darwin.m new file mode 100644 index 0000000..09be14b --- /dev/null +++ b/redo/warningpopover_darwin.m @@ -0,0 +1,78 @@ +// 26 august 2014 + +#include "objc_darwin.h" +#include + +@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]]; + // TODO verify against Interface Builder + [wv->icon setImageFrameStyle:NSImageFrameNone]; +// [wv->icon setImageAlignment:xxx]; + [wv->icon setImageScaling:NSImageScaleProportionallyUpOrDown]; + [wv->icon setEditable:NO]; + [wv->icon setAnimates:NO]; + [wv->icon setAllowsCutCopyPaste:NO]; + // TODO check other controls's values for this + [wv->icon setRefusesFirstResponder:YES]; + + wv->label = (NSTextField *) newLabel(); + // TODO rename to textfieldSetText + textFieldSetText((id) wv->label, text); + [wv->label setRefusesFirstResponder:YES]; + + [wv addSubview:wv->icon]; + [wv addSubview:wv->label]; + [wv sizeToFitAndArrange]; + + NSPopover *popover; + NSViewController *vc; + + vc = [NSViewController new]; + [vc setView:wv]; + popover = [NSPopover new]; + [popover setContentViewController:vc]; + [popover setContentSize:[wv frame].size]; + + return (id) popover; +}