tab-frame.cpp 4.5 KB

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