cog/ThirdParty/ToolTip/ToolTipWindow.m

117 lines
2.8 KiB
Matlab
Raw Normal View History

2009-02-09 05:53:38 +00:00
#import "ToolTipWindow.h"
@implementation ToolTipWindow
- (void)setBackgroundColor:(NSColor *)bgColor
{
backgroundColor = bgColor;
2009-02-09 05:53:38 +00:00
}
- (NSColor *)backgroundColor
{
return backgroundColor;
}
- (NSSize)suggestedSizeForTooltip:(id)tooltip
{
NSSize tipSize = NSZeroSize;
if ([tooltip isKindOfClass:[NSAttributedString class]]) {
tipSize = [tooltip size];
}
else if ([tooltip isKindOfClass:[NSString class]]){
tipSize = [tooltip sizeWithAttributes:textAttributes];
}
if (!NSEqualSizes(tipSize, NSZeroSize))
tipSize.width += 4;
return tipSize;
}
- (id)init
{
self = [super initWithContentRect:NSMakeRect(0,0,0,0)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
{ // window setup...
[self setAlphaValue:0.90];
[self setOpaque:NO];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor colorWithDeviceRed:1.0 green:0.96 blue:0.76 alpha:1.0]];
[self setLevel:NSStatusWindowLevel];
[self setHidesOnDeactivate:YES];
[self setIgnoresMouseEvents:YES];
[self setReleasedWhenClosed:NO];
}
{ // textfield setup...
NSTextField *field = [[NSTextField alloc] initWithFrame:NSMakeRect(0,0,0,0)];
[field setEditable:NO];
[field setSelectable:NO];
[field setBezeled:NO];
[field setBordered:NO];
[field setDrawsBackground:NO];
[field setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self setContentView:field];
[self setFrame:[self frameRectForContentRect:[field frame]] display:NO];
[field setStringValue:@" "]; // Just having at least 1 char to allow the next message...
textAttributes = [[field attributedStringValue] attributesAtIndex:0 effectiveRange:nil];
2009-02-09 05:53:38 +00:00
}
return self;
}
- (void)dealloc
{
if (closeTimer) {
[closeTimer invalidate];
}
}
- (id)toolTip { return tooltipObject; }
- (void)setToolTip:(id)tip
{
id contentView = [self contentView];
tooltipObject = tip;
2009-02-09 05:53:38 +00:00
if ([contentView isKindOfClass:[NSTextField class]]) {
if ([tip isKindOfClass:[NSString class]]) [contentView setStringValue:tip];
else
if ([tip isKindOfClass:[NSAttributedString class]]) [contentView setAttributedStringValue:tip];
}
}
- (void)orderFrontForDuration:(NSTimeInterval)duration
{
[self orderFront:nil];
if (closeTimer) { [closeTimer invalidate]; }
2009-02-09 05:53:38 +00:00
closeTimer = [NSTimer timerWithTimeInterval:duration target:self selector:@selector(close) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:closeTimer forMode:NSRunLoopCommonModes];
2009-02-09 05:53:38 +00:00
}
- (void)orderFront
{
if (closeTimer) { [closeTimer invalidate]; closeTimer = nil; }
2009-02-09 05:53:38 +00:00
[super orderFront:nil];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"TooltipWindow:\n%@", [[self contentView] stringValue]];
}
@end