| 1 |
#if defined(Hiro_VerticalScrollBar) |
| 2 |
|
| 3 |
@implementation CocoaVerticalScrollBar |
| 4 |
|
| 5 |
-(id) initWith:(hiro::mVerticalScrollBar&)verticalScrollBarReference { |
| 6 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 1)]) { |
| 7 |
verticalScrollBar = &verticalScrollBarReference; |
| 8 |
|
| 9 |
[self setTarget:self]; |
| 10 |
[self setAction:@selector(scroll:)]; |
| 11 |
|
| 12 |
[self setControlSize:NSControlSizeRegular]; |
| 13 |
[self setScrollerStyle:NSScrollerStyleLegacy]; |
| 14 |
[self setEnabled:YES]; |
| 15 |
|
| 16 |
[self update]; |
| 17 |
} |
| 18 |
return self; |
| 19 |
} |
| 20 |
|
| 21 |
-(void) update { |
| 22 |
f64 d = 1.0 / verticalScrollBar->state.length; |
| 23 |
f64 f = d * verticalScrollBar->state.position; |
| 24 |
|
| 25 |
[self setDoubleValue:f]; |
| 26 |
[self setKnobProportion:d]; |
| 27 |
} |
| 28 |
|
| 29 |
-(IBAction) scroll:(id)sender { |
| 30 |
auto& state = verticalScrollBar->state; |
| 31 |
|
| 32 |
switch([self hitPart]) { |
| 33 |
case NSScrollerIncrementPage: |
| 34 |
if(state.position < state.length - 1) state.position++; |
| 35 |
[self update]; |
| 36 |
break; |
| 37 |
|
| 38 |
case NSScrollerDecrementPage: |
| 39 |
if(state.position) state.position--; |
| 40 |
[self update]; |
| 41 |
break; |
| 42 |
|
| 43 |
case NSScrollerKnob: |
| 44 |
state.position = [self doubleValue] * state.length; |
| 45 |
break; |
| 46 |
} |
| 47 |
|
| 48 |
verticalScrollBar->doChange(); |
| 49 |
} |
| 50 |
|
| 51 |
@end |
| 52 |
|
| 53 |
namespace hiro { |
| 54 |
|
| 55 |
auto pVerticalScrollBar::construct() -> void { |
| 56 |
cocoaView = cocoaVerticalScrollBar = [[CocoaVerticalScrollBar alloc] initWith:self()]; |
| 57 |
pWidget::construct(); |
| 58 |
|
| 59 |
setLength(state().length); |
| 60 |
setPosition(state().position); |
| 61 |
} |
| 62 |
|
| 63 |
auto pVerticalScrollBar::destruct() -> void { |
| 64 |
[cocoaView removeFromSuperview]; |
| 65 |
} |
| 66 |
|
| 67 |
auto pVerticalScrollBar::minimumSize() const -> Size { |
| 68 |
return {(s32)[NSScroller scrollerWidthForControlSize:NSControlSizeRegular scrollerStyle:NSScrollerStyleLegacy], 32}; |
| 69 |
} |
| 70 |
|
| 71 |
auto pVerticalScrollBar::setLength(u32 length) -> void { |
| 72 |
[(CocoaVerticalScrollBar*)cocoaView update]; |
| 73 |
} |
| 74 |
|
| 75 |
auto pVerticalScrollBar::setPosition(u32 position) -> void { |
| 76 |
[(CocoaVerticalScrollBar*)cocoaView update]; |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
#endif |