| 1 |
@implementation CocoaHorizontalScroller : NSScroller |
| 2 |
|
| 3 |
-(id) initWith:(phoenix::HorizontalScroller&)horizontalScrollerReference { |
| 4 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 1, 0)]) { |
| 5 |
horizontalScroller = &horizontalScrollerReference; |
| 6 |
|
| 7 |
[self setTarget:self]; |
| 8 |
[self setAction:@selector(scroll:)]; |
| 9 |
|
| 10 |
[self setControlSize:NSRegularControlSize]; |
| 11 |
[self setScrollerStyle:NSScrollerStyleLegacy]; |
| 12 |
[self setEnabled:YES]; |
| 13 |
|
| 14 |
[self update]; |
| 15 |
} |
| 16 |
return self; |
| 17 |
} |
| 18 |
|
| 19 |
-(void) update { |
| 20 |
double d = 1.0 / horizontalScroller->state.length; |
| 21 |
double f = d * horizontalScroller->state.position; |
| 22 |
|
| 23 |
[self setDoubleValue:f]; |
| 24 |
[self setKnobProportion:d]; |
| 25 |
} |
| 26 |
|
| 27 |
-(IBAction) scroll:(id)sender { |
| 28 |
auto& state = horizontalScroller->state; |
| 29 |
|
| 30 |
switch([self hitPart]) { |
| 31 |
case NSScrollerIncrementLine: |
| 32 |
case NSScrollerIncrementPage: |
| 33 |
if(state.position < state.length - 1) state.position++; |
| 34 |
[self update]; |
| 35 |
break; |
| 36 |
|
| 37 |
case NSScrollerDecrementLine: |
| 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 |
if(horizontalScroller->onChange) horizontalScroller->onChange(); |
| 49 |
} |
| 50 |
|
| 51 |
@end |
| 52 |
|
| 53 |
namespace phoenix { |
| 54 |
|
| 55 |
Size pHorizontalScroller::minimumSize() { |
| 56 |
@autoreleasepool { |
| 57 |
return {32, [NSScroller scrollerWidthForControlSize:NSRegularControlSize scrollerStyle:NSScrollerStyleLegacy]}; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void pHorizontalScroller::setLength(unsigned length) { |
| 62 |
@autoreleasepool { |
| 63 |
[cocoaView update]; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void pHorizontalScroller::setPosition(unsigned position) { |
| 68 |
@autoreleasepool { |
| 69 |
[cocoaView update]; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void pHorizontalScroller::constructor() { |
| 74 |
@autoreleasepool { |
| 75 |
cocoaView = cocoaHorizontalScroller = [[CocoaHorizontalScroller alloc] initWith:horizontalScroller]; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void pHorizontalScroller::destructor() { |
| 80 |
@autoreleasepool { |
| 81 |
[cocoaView release]; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
} |