Cleaned up the TextField.Invalid() popover on Mac OS X a fair bit.
This commit is contained in:
parent
8a4218b109
commit
83f423a43e
|
@ -168,46 +168,11 @@ void textFieldSetText(id t, char *text)
|
||||||
|
|
||||||
id textfieldOpenInvalidPopover(id textfield, char *reason)
|
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;
|
NSPopover *popover;
|
||||||
NSViewController *vc;
|
|
||||||
|
|
||||||
vc = [NSViewController new];
|
popover = (NSPopover *) newWarningPopover(reason);
|
||||||
[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 showRelativeToRect:NSZeroRect ofView:toNSView(textfield) preferredEdge:NSMaxYEdge];
|
[popover showRelativeToRect:NSZeroRect ofView:toNSView(textfield) preferredEdge:NSMaxYEdge];
|
||||||
|
|
||||||
// and beep
|
|
||||||
NSBeep();
|
NSBeep();
|
||||||
|
|
||||||
return (id) popover;
|
return (id) popover;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -143,4 +143,7 @@ extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t);
|
||||||
/* dialog_darwin.m */
|
/* dialog_darwin.m */
|
||||||
extern void openFile(id, void *);
|
extern void openFile(id, void *);
|
||||||
|
|
||||||
|
/* warningpopover_darwin.m */
|
||||||
|
extern id newWarningPopover(char *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue