| 1 |
#if defined(Hiro_TabFrame) |
| 2 |
|
| 3 |
@implementation CocoaTabFrame |
| 4 |
|
| 5 |
-(id) initWith:(hiro::mTabFrame&)tabFrameReference { |
| 6 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 7 |
tabFrame = &tabFrameReference; |
| 8 |
|
| 9 |
[self setDelegate:self]; |
| 10 |
} |
| 11 |
return self; |
| 12 |
} |
| 13 |
|
| 14 |
-(void) tabView:(NSTabView*)tabView didSelectTabViewItem:(NSTabViewItem*)tabViewItem { |
| 15 |
tabFrame->self()->_synchronizeSizable(); |
| 16 |
tabFrame->doChange(); |
| 17 |
} |
| 18 |
|
| 19 |
@end |
| 20 |
|
| 21 |
@implementation CocoaTabFrameItem : NSTabViewItem |
| 22 |
|
| 23 |
-(id) initWith:(hiro::mTabFrame&)tabFrameReference { |
| 24 |
if(self = [super initWithIdentifier:nil]) { |
| 25 |
tabFrame = &tabFrameReference; |
| 26 |
cocoaTabFrame = tabFrame->self()->cocoaTabFrame; |
| 27 |
} |
| 28 |
return self; |
| 29 |
} |
| 30 |
|
| 31 |
-(NSSize) sizeOfLabel:(BOOL)shouldTruncateLabel { |
| 32 |
NSSize sizeOfLabel = [super sizeOfLabel:shouldTruncateLabel]; |
| 33 |
s32 selection = [cocoaTabFrame indexOfTabViewItem:self]; |
| 34 |
if(selection >= 0) { |
| 35 |
if(auto item = tabFrame->item(selection)) { |
| 36 |
if(item->state.icon) { |
| 37 |
u32 iconSize = hiro::pFont::size(tabFrame->font(true), " ").height(); |
| 38 |
sizeOfLabel.width += iconSize + 2; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
return sizeOfLabel; |
| 43 |
} |
| 44 |
|
| 45 |
-(void) drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect { |
| 46 |
s32 selection = [cocoaTabFrame indexOfTabViewItem:self]; |
| 47 |
if(selection >= 0) { |
| 48 |
if(auto item = tabFrame->item(selection)) { |
| 49 |
if(item->state.icon) { |
| 50 |
u32 iconSize = hiro::pFont::size(tabFrame->font(true), " ").height(); |
| 51 |
NSImage* image = NSMakeImage(item->state.icon); |
| 52 |
|
| 53 |
[[NSGraphicsContext currentContext] saveGraphicsState]; |
| 54 |
NSRect targetRect = NSMakeRect(tabRect.origin.x, tabRect.origin.y + 2, iconSize, iconSize); |
| 55 |
[image drawInRect:targetRect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:1.0 respectFlipped:YES hints:nil]; |
| 56 |
[[NSGraphicsContext currentContext] restoreGraphicsState]; |
| 57 |
|
| 58 |
tabRect.origin.x += iconSize + 2; |
| 59 |
tabRect.size.width -= iconSize + 2; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
[super drawLabel:shouldTruncateLabel inRect:tabRect]; |
| 64 |
} |
| 65 |
|
| 66 |
@end |
| 67 |
|
| 68 |
namespace hiro { |
| 69 |
|
| 70 |
auto pTabFrame::construct() -> void { |
| 71 |
cocoaView = cocoaTabFrame = [[CocoaTabFrame alloc] initWith:self()]; |
| 72 |
pWidget::construct(); |
| 73 |
|
| 74 |
setNavigation(state().navigation); |
| 75 |
} |
| 76 |
|
| 77 |
auto pTabFrame::destruct() -> void { |
| 78 |
[cocoaView removeFromSuperview]; |
| 79 |
} |
| 80 |
|
| 81 |
auto pTabFrame::append(sTabFrameItem item) -> void { |
| 82 |
if(auto p = item->self()) { |
| 83 |
p->cocoaTabFrameItem = [[CocoaTabFrameItem alloc] initWith:self()]; |
| 84 |
[p->cocoaTabFrameItem setLabel:[NSString stringWithUTF8String:item->state.text]]; |
| 85 |
[(CocoaTabFrame*)cocoaView addTabViewItem:p->cocoaTabFrameItem]; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
auto pTabFrame::remove(sTabFrameItem item) -> void { |
| 90 |
if(auto p = item->self()) { |
| 91 |
[(CocoaTabFrame*)cocoaView removeTabViewItem:p->cocoaTabFrameItem]; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
auto pTabFrame::setEnabled(bool enabled) -> void { |
| 96 |
pWidget::setEnabled(enabled); |
| 97 |
for(auto& item : state().items) { |
| 98 |
if(auto& sizable = item->state.sizable) { |
| 99 |
if(auto self = sizable->self()) self->setEnabled(sizable->enabled(true)); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
auto pTabFrame::setFont(const Font& font) -> void { |
| 105 |
pWidget::setFont(font); |
| 106 |
for(auto& item : state().items) { |
| 107 |
if(auto& sizable = item->state.sizable) { |
| 108 |
if(auto self = sizable->self()) self->setFont(sizable->font(true)); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
auto pTabFrame::setGeometry(Geometry geometry) -> void { |
| 114 |
pWidget::setGeometry({ |
| 115 |
geometry.x() - 7, geometry.y() - 5, |
| 116 |
geometry.width() + 14, geometry.height() + 6 |
| 117 |
}); |
| 118 |
geometry.setGeometry({ |
| 119 |
geometry.x() + 1, geometry.y() + 22, |
| 120 |
geometry.width() - 2, geometry.height() - 32 |
| 121 |
}); |
| 122 |
for(auto& item : state().items) { |
| 123 |
if(auto& sizable = item->state.sizable) { |
| 124 |
sizable->setGeometry(geometry); |
| 125 |
} |
| 126 |
} |
| 127 |
_synchronizeSizable(); |
| 128 |
} |
| 129 |
|
| 130 |
auto pTabFrame::setNavigation(Navigation navigation) -> void { |
| 131 |
} |
| 132 |
|
| 133 |
auto pTabFrame::setVisible(bool visible) -> void { |
| 134 |
pWidget::setVisible(visible); |
| 135 |
for(auto& item : state().items) { |
| 136 |
if(auto& sizable = item->state.sizable) { |
| 137 |
if(auto self = sizable->self()) self->setVisible(sizable->visible(true)); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
auto pTabFrame::_synchronizeSizable() -> void { |
| 143 |
NSTabViewItem* tabViewItem = [(CocoaTabFrame*)cocoaView selectedTabViewItem]; |
| 144 |
s32 selected = tabViewItem ? [(CocoaTabFrame*)cocoaView indexOfTabViewItem:tabViewItem] : -1; |
| 145 |
for(auto& item : state().items) { |
| 146 |
item->state.selected = item->offset() == selected; |
| 147 |
if(auto& sizable = item->state.sizable) sizable->setVisible(item->selected()); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
#endif |