Debian Codegraph @

phoenix/cocoa/widget/label.cpp

raw
1 @implementation CocoaLabel : NSTextField
2
3 -(id) initWith:(phoenix::Label&)labelReference {
4 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
5 label = &labelReference;
6
7 [self setAlignment:NSLeftTextAlignment];
8 [self setBordered:NO];
9 [self setDrawsBackground:NO];
10 [self setEditable:NO];
11 }
12 return self;
13 }
14
15 @end
16
17 namespace phoenix {
18
19 Size pLabel::minimumSize() {
20 Size size = Font::size(label.font(), label.state.text);
21 return {size.width, size.height};
22 }
23
24 void pLabel::setGeometry(Geometry geometry) {
25 //NSTextField does not support vertical text centering:
26 //simulate this by adjusting the geometry placement (reduce height, move view down)
27 unsigned height = Font::size(label.font(), " ").height;
28 unsigned widgetHeight = geometry.height + 4; //+4 compensates for margin adjust
29 auto offset = geometry;
30
31 if(widgetHeight > height) {
32 unsigned diff = widgetHeight - height;
33 offset.y += diff >> 1;
34 offset.height -= diff >> 1;
35 }
36
37 pWidget::setGeometry({
38 offset.x - 3, offset.y - 3,
39 offset.width + 6, offset.height + 6
40 });
41 }
42
43 void pLabel::setText(string text) {
44 @autoreleasepool {
45 [cocoaView setStringValue:[NSString stringWithUTF8String:text]];
46 }
47 }
48
49 void pLabel::constructor() {
50 @autoreleasepool {
51 cocoaView = cocoaLabel = [[CocoaLabel alloc] initWith:label];
52 setText(label.state.text);
53 }
54 }
55
56 void pLabel::destructor() {
57 @autoreleasepool {
58 [cocoaView release];
59 }
60 }
61
62 }