Skip to content

Instantly share code, notes, and snippets.

@klaaspieter
Created January 13, 2010 14:06
Show Gist options
  • Save klaaspieter/276210 to your computer and use it in GitHub Desktop.
Save klaaspieter/276210 to your computer and use it in GitHub Desktop.
A hackish subclass of CPTextField to show hyperlinks
// We need this 'private' variables from the superclass
var _CPimageAndTextViewFrameSizeChangedFlag = 1 << 0,
_CPImageAndTextViewImageChangedFlag = 1 << 1,
_CPImageAndTextViewTextChangedFlag = 1 << 2,
_CPImageAndTextViewAlignmentChangedFlag = 1 << 3,
_CPImageAndTextViewVerticalAlignmentChangedFlag = 1 << 4,
_CPImageAndTextViewLineBreakModeChangedFlag = 1 << 5,
_CPImageAndTextViewTextColorChangedFlag = 1 << 6,
_CPImageAndTextViewFontChangedFlag = 1 << 7,
_CPImageAndTextViewTextShadowColorChangedFlag = 1 << 8,
_CPImageAndTextViewImagePositionChangedFlag = 1 << 9,
_CPImageAndTextViewImageScalingChangedFlag = 1 << 10,
_CPImageAndTextViewUnderlineColorChangedFlag = 1 << 11;
@implementation _CSLinkTextView : _CPImageAndTextView
{
CPColor _underlineColor;
CPString _link;
}
- (void)layoutSubviews
{
hasDOMTextElement = !!_DOMTextElement
if (!hasDOMTextElement)
{
_DOMTextElement = document.createElement("a");
_DOMTextElement.href = _link;
_DOMTextElement.target = @"_blank";
_DOMElement.appendChild(_DOMTextElement);
}
var textStyle = _DOMTextElement.style;
textStyle.position = "absolute";
textStyle.whiteSpace = "pre";
textStyle.cursor = "default";
textStyle.zIndex = 200;
textStyle.overflow = "hidden";
textStyle.color = [_textColor cssString];
textStyle.textDecoration = @"none";
// We have to set all these values now.
_flags |= _CPImageAndTextViewTextChangedFlag | _CPImageAndTextViewFontChangedFlag | _CPImageAndTextViewLineBreakModeChangedFlag;
if (_flags & _CPImageAndTextViewUnderlineColorChangedFlag)
_DOMElement.style.borderBottom = @"1px dotted " + [_underlineColor cssString];
[super layoutSubviews];
// Change this here because it the cursor style is override in super layoutSubviews
textStyle.cursor = @"pointer";
}
- (void)setUnderlineColor:(CPColor)underlineColor
{
_underlineColor = underlineColor;
_flags |= _CPImageAndTextViewUnderlineColorChangedFlag;
[self setNeedsLayout];
}
- (void)setLink:(CPString)link
{
if (link !== _link)
{
_link = link;
[self setNeedsLayout];
}
}
@end
@import "_CSLinkTextView.j"
@implementation CSLinkTextField : CPTextField
{
CPString _link;
}
+ (id)themeAttributes
{
return [CPDictionary dictionaryWithObjects:[nil]
forKeys:[@"underline-color"]];
}
- (CPView)createEphemeralSubviewNamed:(CPString)aName
{
if (aName === "content-view")
{
var view = [[_CSLinkTextView alloc] initWithFrame:CPRectMakeZero()];
return view;
}
return [super createEphemeralSubviewNamed:aName];
}
- (void)layoutSubviews
{
var contentView = [self layoutEphemeralSubviewNamed:@"content-view"
positioned:CPWindowAbove
relativeToEphemeralSubviewNamed:@"bezel-view"];
if (contentView)
{
[contentView setLink:_link];
[contentView setUnderlineColor:[self currentValueForThemeAttribute:@"underline-color"]];
}
[super layoutSubviews];
}
- (void)setLink:(CPString)link
{
if (_link !== link)
{
_link = link;
[self setNeedsLayout];
}
}
- (void)mouseEntered:(CPEvent)anEvent
{
[self highlight:YES];
}
- (void)mouseExited:(CPEvent)anEvent
{
[self highlight:NO];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment