Cleaned up the TextField.Invalid() popover on Mac OS X a fair bit.

This commit is contained in:
Pietro Gagliardi 2014-08-26 22:41:25 -04:00
parent 8a4218b109
commit 83f423a43e
3 changed files with 82 additions and 36 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -0,0 +1,78 @@
// 26 august 2014
#include "objc_darwin.h"
#include <Cocoa/Cocoa.h>
@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;
}