horizontal-scroll-bar.cpp 2.1 KB

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