Skip to content

Instantly share code, notes, and snippets.

@davidcann
Created June 1, 2009 20:19
Show Gist options
  • Save davidcann/121718 to your computer and use it in GitHub Desktop.
Save davidcann/121718 to your computer and use it in GitHub Desktop.
/*
* DCInfoBubbleController.j
*
* Created by David Cann.
* Copyright 2008 __MyCompanyName__. All rights reserved.
*/
@import <Foundation/CPObject.j>
@import "DCLinkTextField.j"
// we use this class below and I didn't want to create a whole file for it
@implementation DCInfoBubbleWindow : CPPanel {
}
- (void)mouseMoved:(CPEvent)anEvent {
[[[[CPApplication sharedApplication] delegate] infoBubbleController] hide];
}
@end
@implementation DCInfoBubbleController : CPObject {
CPWindow panel;
DCInfoBubbleView view;
CPWindow downArrowWindow;
CPWindow upArrowWindow;
DCLinkTextField titleTextField;
float arrowOverlap;
CGPoint point;
float distance;
}
- (id)init {
self = [super init];
if (self) {
arrowOverlap = 8;
view = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 200, 90)];
titleTextField = [[DCLinkTextField alloc] initWithFrame:CGRectMake(10, 0, [view bounds].size.width - 20, [view bounds].size.height)];
[titleTextField setAutoresizingMask:CPViewWidthSizable | CPViewHeightSizable];
[titleTextField HTMLElement].style.color = "#EEEEEE";
[titleTextField HTMLElement].style.fontSize = "12px";
[titleTextField HTMLElement].style.fontFamily = "Helvetica, Sans-Serif";
[titleTextField HTMLElement].style.lineHeight = "1.5em";
[titleTextField HTMLElement].style.textAlign = "center";
[view addSubview:titleTextField];
panel = [[DCInfoBubbleWindow alloc] initWithContentRect:CGRectMake(0, 0, [view bounds].size.width, [view bounds].size.height) styleMask:CPHUDBackgroundWindowMask];
[panel setDelegate:self];
[panel setAcceptsMouseMovedEvents:YES];
[panel setContentView:view];
// down arrow
var downArrowImageView = [[CPImageView alloc] initWithFrame:CGRectMake(0, 0, 19, 11)];
[downArrowImageView setImage:[[CPImage alloc] initWithContentsOfFile:@"images/DCBubbleArrowDown.png"]];
downArrowWindow = [[CPWindow alloc] initWithContentRect:[downArrowImageView bounds] styleMask:CPBorderlessWindowMask];
[downArrowWindow setContentView:downArrowImageView];
// up arrow
var upArrowImageView = [[CPImageView alloc] initWithFrame:CGRectMake(0, 0, 19, 11)];
[upArrowImageView setImage:[[CPImage alloc] initWithContentsOfFile:@"images/DCBubbleArrowUp.png"]];
upArrowWindow = [[CPWindow alloc] initWithContentRect:[upArrowImageView bounds] styleMask:CPBorderlessWindowMask];
[upArrowWindow setContentView:upArrowImageView];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(hide)
name:"DCMouseDidExitWindowNotification"
object:nil];
}
return self;
}
- (void)showRelativeToPoint:(CGPoint)aPoint distance:(float)aDistance height:(float)theHeight title:(CPString)theTitle HTML:(CPString)theHTML {
point = aPoint;
distance = aDistance;
[titleTextField setHTML:theHTML];
[panel setTitle:theTitle];
var w = [panel frame].size.width;
var h = theHeight;
var x = point.x - (w / 2);
var y = point.y - (h / 2);
var isAbove = YES;
if (y - distance - (h / 2) >= 10) {
// try to show it above
y = y - distance - (h / 2) - [downArrowWindow frame].size.height + arrowOverlap;
} else {
// otherwise show it below
y = y + distance + (h / 2) + [upArrowWindow frame].size.height + 8 - arrowOverlap;
isAbove = NO;
}
[panel setFrame:CGRectMake(x, y, w, h)];
[panel orderFront:[[[CPApplication sharedApplication] delegate] theWindow]];
// show the appropriate arrow
if (isAbove) {
// down arrow
var w2 = [downArrowWindow frame].size.width;
var h2 = [downArrowWindow frame].size.height;
var x2 = x + (w / 2) - (w2 / 2);
var y2 = y + h - 2;
[downArrowWindow setFrame:CGRectMake(x2, y2, w2, h2)];
[downArrowWindow orderFront:panel];
[upArrowWindow orderOut:nil];
} else {
// position the up arrow
var w3 = [upArrowWindow frame].size.width;
var h3 = [upArrowWindow frame].size.height;
var x3 = x + (w / 2) - (w3 / 2);
var y3 = y - h3 + 2;
[upArrowWindow setFrame:CGRectMake(x3, y3, w3, h3)];
[upArrowWindow orderFront:panel];
[downArrowWindow orderOut:nil];
}
}
- (void)hide {
if ([panel isVisible]) {
[panel orderOut:nil];
[downArrowWindow orderOut:nil];
[upArrowWindow orderOut:nil];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment