Debian Codegraph @

phoenix/cocoa/widget/tab-frame.cpp

raw
1 @implementation CocoaTabFrame : NSTabView
2
3 -(id) initWith:(phoenix::TabFrame&)tabFrameReference {
4 if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
5 tabFrame = &tabFrameReference;
6
7 [self setDelegate:self];
8 }
9 return self;
10 }
11
12 -(void) tabView:(NSTabView*)tabView didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
13 tabFrame->state.selection = [tabView indexOfTabViewItem:tabViewItem];
14 tabFrame->p.synchronizeLayout();
15 if(tabFrame->onChange) tabFrame->onChange();
16 }
17
18 @end
19
20 @implementation CocoaTabFrameItem : NSTabViewItem
21
22 -(id) initWith:(phoenix::TabFrame&)tabFrameReference {
23 if(self = [super initWithIdentifier:nil]) {
24 tabFrame = &tabFrameReference;
25 cocoaTabFrame = tabFrame->p.cocoaTabFrame;
26 }
27 return self;
28 }
29
30 -(NSSize) sizeOfLabel:(BOOL)shouldTruncateLabel {
31 NSSize sizeOfLabel = [super sizeOfLabel:shouldTruncateLabel];
32 signed selection = [cocoaTabFrame indexOfTabViewItem:self];
33 if(selection < 0) return sizeOfLabel; //should never happen
34 if(tabFrame->state.image[selection].empty() == false) {
35 unsigned iconSize = phoenix::Font::size(tabFrame->font(), " ").height;
36 sizeOfLabel.width += iconSize + 2;
37 }
38 return sizeOfLabel;
39 }
40
41 -(void) drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect {
42 signed selection = [cocoaTabFrame indexOfTabViewItem:self];
43 if(selection >= 0 && tabFrame->state.image[selection].empty() == false) {
44 unsigned iconSize = phoenix::Font::size(tabFrame->font(), " ").height;
45 NSImage* image = NSMakeImage(tabFrame->state.image[selection]);
46
47 [[NSGraphicsContext currentContext] saveGraphicsState];
48 NSRect targetRect = NSMakeRect(tabRect.origin.x, tabRect.origin.y + 2, iconSize, iconSize);
49 [image drawInRect:targetRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
50 [[NSGraphicsContext currentContext] restoreGraphicsState];
51
52 tabRect.origin.x += iconSize + 2;
53 tabRect.size.width -= iconSize + 2;
54 }
55 [super drawLabel:shouldTruncateLabel inRect:tabRect];
56 }
57
58 @end
59
60 namespace phoenix {
61
62 void pTabFrame::append(string text, const image& image) {
63 @autoreleasepool {
64 CocoaTabFrameItem* item = [[CocoaTabFrameItem alloc] initWith:tabFrame];
65 [item setLabel:[NSString stringWithUTF8String:text]];
66 [cocoaView addTabViewItem:item];
67 tabs.append(item);
68 }
69 }
70
71 void pTabFrame::remove(unsigned selection) {
72 @autoreleasepool {
73 CocoaTabFrameItem* item = tabs[selection];
74 [cocoaView removeTabViewItem:item];
75 tabs.remove(selection);
76 }
77 }
78
79 void pTabFrame::setEnabled(bool enabled) {
80 for(auto& layout : tabFrame.state.layout) {
81 if(layout) layout->setEnabled(layout->enabled());
82 }
83 pWidget::setEnabled(enabled);
84 }
85
86 void pTabFrame::setGeometry(Geometry geometry) {
87 pWidget::setGeometry({
88 geometry.x - 7, geometry.y - 5,
89 geometry.width + 14, geometry.height + 6
90 });
91 geometry.x += 1, geometry.width -= 2;
92 geometry.y += 22, geometry.height -= 32;
93 for(auto& layout : tabFrame.state.layout) {
94 if(layout == nullptr) continue;
95 layout->setGeometry(geometry);
96 }
97 synchronizeLayout();
98 }
99
100 void pTabFrame::setImage(unsigned selection, const image& image) {
101 }
102
103 void pTabFrame::setSelection(unsigned selection) {
104 @autoreleasepool {
105 CocoaTabFrameItem* item = tabs[selection];
106 [cocoaView selectTabViewItem:item];
107 }
108 synchronizeLayout();
109 }
110
111 void pTabFrame::setText(unsigned selection, string text) {
112 @autoreleasepool {
113 CocoaTabFrameItem* item = tabs[selection];
114 [item setLabel:[NSString stringWithUTF8String:text]];
115 }
116 }
117
118 void pTabFrame::setVisible(bool visible) {
119 for(auto& layout : tabFrame.state.layout) {
120 if(layout) layout->setVisible(layout->visible());
121 }
122 pWidget::setVisible(visible);
123 }
124
125 void pTabFrame::constructor() {
126 @autoreleasepool {
127 cocoaView = cocoaTabFrame = [[CocoaTabFrame alloc] initWith:tabFrame];
128 }
129 }
130
131 void pTabFrame::destructor() {
132 @autoreleasepool {
133 [cocoaView release];
134 }
135 }
136
137 void pTabFrame::synchronizeLayout() {
138 unsigned selection = 0;
139 for(auto& layout : tabFrame.state.layout) {
140 if(layout) layout->setVisible(selection == tabFrame.state.selection);
141 selection++;
142 }
143 }
144
145 }