| 1 |
#if defined(Hiro_Label) |
| 2 |
|
| 3 |
@implementation CocoaLabel |
| 4 |
{ |
| 5 |
NSColor *_foregroundColor; |
| 6 |
} |
| 7 |
|
| 8 |
-(id) initWith:(hiro::mLabel&)labelReference { |
| 9 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 10 |
label = &labelReference; |
| 11 |
} |
| 12 |
return self; |
| 13 |
} |
| 14 |
|
| 15 |
-(void) resetCursorRects { |
| 16 |
if(auto mouseCursor = NSMakeCursor(label->mouseCursor())) { |
| 17 |
[self addCursorRect:self.bounds cursor:mouseCursor]; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
-(void) drawRect:(NSRect)dirtyRect { |
| 22 |
if(auto backgroundColor = label->backgroundColor()) { |
| 23 |
NSColor* color = NSMakeColor(backgroundColor); |
| 24 |
[color setFill]; |
| 25 |
NSRect frame = self.bounds; |
| 26 |
NSRectFill(frame); |
| 27 |
} |
| 28 |
|
| 29 |
NSFont* font = hiro::pFont::create(label->font(true)); |
| 30 |
NSColor* color = _foregroundColor; |
| 31 |
if(!color) { |
| 32 |
color = NSMakeColor(hiro::SystemColor::Label); |
| 33 |
} |
| 34 |
|
| 35 |
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 36 |
paragraphStyle.alignment = NSTextAlignmentLeft; |
| 37 |
paragraphStyle.lineBreakMode = NSLineBreakByClipping; |
| 38 |
NSDictionary* attributes = @{ |
| 39 |
NSFontAttributeName:font, |
| 40 |
NSForegroundColorAttributeName:color, |
| 41 |
NSParagraphStyleAttributeName:paragraphStyle |
| 42 |
}; |
| 43 |
|
| 44 |
auto size = hiro::pFont::size(label->font(true), label->text()); |
| 45 |
auto alignment = label->alignment(); |
| 46 |
if(!alignment) alignment = {0.0, 0.5}; |
| 47 |
|
| 48 |
auto geometry = label->geometry(); |
| 49 |
NSRect rect = {{geometry.x(), geometry.y()}, {geometry.width(), geometry.height()}}; |
| 50 |
rect.origin.x = max(0, (geometry.width() - size.width()) * alignment.horizontal()); |
| 51 |
rect.origin.y = max(0, (geometry.height() - size.height()) * alignment.vertical()); |
| 52 |
rect.size.width = min(geometry.width(), size.width()); |
| 53 |
rect.size.height = min(geometry.height(), size.height()); |
| 54 |
|
| 55 |
NSString* string = [NSString stringWithUTF8String:label->text()]; |
| 56 |
[string drawInRect:rect withAttributes:attributes]; |
| 57 |
} |
| 58 |
|
| 59 |
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown { |
| 60 |
if(isDown) { |
| 61 |
switch([event buttonNumber]) { |
| 62 |
case 0: return label->doMousePress(hiro::Mouse::Button::Left); |
| 63 |
case 1: return label->doMousePress(hiro::Mouse::Button::Right); |
| 64 |
case 2: return label->doMousePress(hiro::Mouse::Button::Middle); |
| 65 |
} |
| 66 |
} else { |
| 67 |
switch([event buttonNumber]) { |
| 68 |
case 0: return label->doMouseRelease(hiro::Mouse::Button::Left); |
| 69 |
case 1: return label->doMouseRelease(hiro::Mouse::Button::Right); |
| 70 |
case 2: return label->doMouseRelease(hiro::Mouse::Button::Middle); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
-(void) mouseEntered:(NSEvent*)event { |
| 76 |
label->doMouseEnter(); |
| 77 |
} |
| 78 |
|
| 79 |
-(void) mouseExited:(NSEvent*)event { |
| 80 |
label->doMouseLeave(); |
| 81 |
} |
| 82 |
|
| 83 |
-(void) mouseMove:(NSEvent*)event { |
| 84 |
if([event window] == nil) return; |
| 85 |
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil]; |
| 86 |
label->doMouseMove({(s32)location.x, (s32)([self frame].size.height - 1 - location.y)}); |
| 87 |
} |
| 88 |
|
| 89 |
-(void) mouseDown:(NSEvent*)event { |
| 90 |
[self mouseButton:event down:YES]; |
| 91 |
} |
| 92 |
|
| 93 |
-(void) mouseUp:(NSEvent*)event { |
| 94 |
[self mouseButton:event down:NO]; |
| 95 |
} |
| 96 |
|
| 97 |
-(void) mouseDragged:(NSEvent*)event { |
| 98 |
[self mouseMove:event]; |
| 99 |
} |
| 100 |
|
| 101 |
-(void) rightMouseDown:(NSEvent*)event { |
| 102 |
[self mouseButton:event down:YES]; |
| 103 |
} |
| 104 |
|
| 105 |
-(void) rightMouseUp:(NSEvent*)event { |
| 106 |
[self mouseButton:event down:NO]; |
| 107 |
} |
| 108 |
|
| 109 |
-(void) rightMouseDragged:(NSEvent*)event { |
| 110 |
[self mouseMove:event]; |
| 111 |
} |
| 112 |
|
| 113 |
-(void) otherMouseDown:(NSEvent*)event { |
| 114 |
[self mouseButton:event down:YES]; |
| 115 |
} |
| 116 |
|
| 117 |
-(void) otherMouseUp:(NSEvent*)event { |
| 118 |
[self mouseButton:event down:NO]; |
| 119 |
} |
| 120 |
|
| 121 |
-(void) otherMouseDragged:(NSEvent*)event { |
| 122 |
[self mouseMove:event]; |
| 123 |
} |
| 124 |
|
| 125 |
namespace hiro { |
| 126 |
|
| 127 |
auto pLabel::construct() -> void { |
| 128 |
cocoaView = cocoaLabel = [[CocoaLabel alloc] initWith:self()]; |
| 129 |
pWidget::construct(); |
| 130 |
|
| 131 |
setAlignment(state().alignment); |
| 132 |
setBackgroundColor(state().backgroundColor); |
| 133 |
setForegroundColor(state().foregroundColor); |
| 134 |
setText(state().text); |
| 135 |
} |
| 136 |
|
| 137 |
auto pLabel::destruct() -> void { |
| 138 |
[cocoaView removeFromSuperview]; |
| 139 |
} |
| 140 |
|
| 141 |
auto pLabel::minimumSize() const -> Size { |
| 142 |
return pFont::size(self().font(true), state().text); |
| 143 |
} |
| 144 |
|
| 145 |
auto pLabel::setAlignment(Alignment alignment) -> void { |
| 146 |
[cocoaView setNeedsDisplay:YES]; |
| 147 |
} |
| 148 |
|
| 149 |
auto pLabel::setBackgroundColor(Color color) -> void { |
| 150 |
[cocoaView setNeedsDisplay:YES]; |
| 151 |
} |
| 152 |
|
| 153 |
auto pLabel::setForegroundColor(Color color) -> void { |
| 154 |
((CocoaLabel *)cocoaView)->_foregroundColor = color? NSMakeColor(color): nil; |
| 155 |
[cocoaView setNeedsDisplay:YES]; |
| 156 |
} |
| 157 |
|
| 158 |
auto pLabel::setForegroundColor(SystemColor color) -> void { |
| 159 |
((CocoaLabel *)cocoaView)->_foregroundColor = NSMakeColor(color); |
| 160 |
[cocoaView setNeedsDisplay:YES]; |
| 161 |
} |
| 162 |
|
| 163 |
auto pLabel::setText(const string& text) -> void { |
| 164 |
[cocoaView setNeedsDisplay:YES]; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
@end |
| 170 |
#endif |