File: tab-frame.cpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (166 lines) | stat: -rw-r--r-- 4,649 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#if defined(Hiro_TabFrame)

@implementation CocoaTabFrame : NSTabView

-(id) initWith:(hiro::mTabFrame&)tabFrameReference {
  if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) {
    tabFrame = &tabFrameReference;

    [self setDelegate:self];
  }
  return self;
}

-(void) tabView:(NSTabView*)tabView didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
  tabFrame->self()->_synchronizeLayout();
  tabFrame->doChange();
}

@end

@implementation CocoaTabFrameItem : NSTabViewItem

-(id) initWith:(hiro::mTabFrame&)tabFrameReference {
  if(self = [super initWithIdentifier:nil]) {
    tabFrame = &tabFrameReference;
    cocoaTabFrame = tabFrame->self()->cocoaTabFrame;
  }
  return self;
}

-(NSSize) sizeOfLabel:(BOOL)shouldTruncateLabel {
  NSSize sizeOfLabel = [super sizeOfLabel:shouldTruncateLabel];
  int selection = [cocoaTabFrame indexOfTabViewItem:self];
  if(selection >= 0) {
    if(auto item = tabFrame->item(selection)) {
      if(item->state.icon) {
        uint iconSize = hiro::pFont::size(tabFrame->font(true), " ").height();
        sizeOfLabel.width += iconSize + 2;
      }
    }
  }
  return sizeOfLabel;
}

-(void) drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect {
  int selection = [cocoaTabFrame indexOfTabViewItem:self];
  if(selection >= 0) {
    if(auto item = tabFrame->item(selection)) {
      if(item->state.icon) {
        uint iconSize = hiro::pFont::size(tabFrame->font(true), " ").height();
        NSImage* image = NSMakeImage(item->state.icon);

        [[NSGraphicsContext currentContext] saveGraphicsState];
        NSRect targetRect = NSMakeRect(tabRect.origin.x, tabRect.origin.y + 2, iconSize, iconSize);
        [image drawInRect:targetRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil];
        [[NSGraphicsContext currentContext] restoreGraphicsState];

        tabRect.origin.x += iconSize + 2;
        tabRect.size.width -= iconSize + 2;
      }
    }
  }
  [super drawLabel:shouldTruncateLabel inRect:tabRect];
}

@end

namespace hiro {

auto pTabFrame::construct() -> void {
  @autoreleasepool {
    cocoaView = cocoaTabFrame = [[CocoaTabFrame alloc] initWith:self()];
    pWidget::construct();

    setNavigation(state().navigation);
  }
}

auto pTabFrame::destruct() -> void {
  @autoreleasepool {
    [cocoaView removeFromSuperview];
    [cocoaView release];
  }
}

auto pTabFrame::append(sTabFrameItem item) -> void {
  @autoreleasepool {
    if(auto p = item->self()) {
      p->cocoaTabFrameItem = [[CocoaTabFrameItem alloc] initWith:self()];
      [p->cocoaTabFrameItem setLabel:[NSString stringWithUTF8String:item->state.text]];
      [cocoaView addTabViewItem:p->cocoaTabFrameItem];
    }
  }
}

auto pTabFrame::remove(sTabFrameItem item) -> void {
  @autoreleasepool {
    if(auto p = item->self()) {
      [cocoaView removeTabViewItem:p->cocoaTabFrameItem];
    }
  }
}

auto pTabFrame::setEnabled(bool enabled) -> void {
  pWidget::setEnabled(enabled);
  for(auto& item : state().items) {
    if(auto& layout = item->state.layout) {
      if(auto self = layout->self()) self->setEnabled(layout->enabled(true));
    }
  }
}

auto pTabFrame::setFont(const Font& font) -> void {
  pWidget::setFont(font);
  for(auto& item : state().items) {
    if(auto& layout = item->state.layout) {
      if(auto self = layout->self()) self->setFont(layout->font(true));
    }
  }
}

auto pTabFrame::setGeometry(Geometry geometry) -> void {
  pWidget::setGeometry({
    geometry.x() - 7, geometry.y() - 5,
    geometry.width() + 14, geometry.height() + 6
  });
  geometry.setGeometry({
    geometry.x() + 1, geometry.y() + 22,
    geometry.width() - 2, geometry.height() - 32
  });
  for(auto& item : state().items) {
    if(auto& layout = item->state.layout) {
      layout->setGeometry(geometry);
    }
  }
  _synchronizeLayout();
}

auto pTabFrame::setNavigation(Navigation navigation) -> void {
}

auto pTabFrame::setVisible(bool visible) -> void {
  pWidget::setVisible(visible);
  for(auto& item : state().items) {
    if(auto& layout = item->state.layout) {
      if(auto self = layout->self()) self->setVisible(layout->visible(true));
    }
  }
}

auto pTabFrame::_synchronizeLayout() -> void {
  @autoreleasepool {
    NSTabViewItem* tabViewItem = [cocoaView selectedTabViewItem];
    int selected = tabViewItem ? [cocoaView indexOfTabViewItem:tabViewItem] : -1;
    for(auto& item : state().items) {
      item->state.selected = item->offset() == selected;
      if(auto& layout = item->state.layout) {
        if(auto self = layout->self()) self->setVisible(layout->visible(true) && item->selected());
      }
    }
  }
}

}

#endif