Splitted Slider

Mac OSX specific forum
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Splitted Slider

Post by Wolfram »

A simple custom slider

Code: Select all



ImportC ""
  class_addMethod(Class.I, Selector.I, *Callback, Types.P-ASCII)
  objc_allocateClassPair(ModelClass.I, NewClassName.P-ASCII, ExtraBytes.I)
  objc_registerClassPair(NewClass.I)
  object_setClass(ObjectToModify.I, NewClass.I)
  sel_registerName(MethodName.P-ASCII)
EndImport


Global Window_0, Slider
Global boundsRect.NSRect, frameRect.NSRect
 


ProcedureC drawBarInside(obj, sel, x.f, y.f, w.f, h.f, flipped, d1.d,d2.d,d3.d,d4.d, x_.d, y_.d, w_.d, h_.d)
  
  
  Protected NSView.i
  CocoaMessage(@NSView,0,"NSView focusView")
  CocoaMessage(0, NSView, "lockFocus")
  
  rect.NSRect
  
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    ;     rect\origin\x = x_
    ;     rect\origin\y = y_
    rect\size\width = w_
    ;     rect\size\height = h_
  CompilerElse
    ;     rect\origin\x = x
    ;     rect\origin\y = y
    rect\size\width = w
    ;     rect\size\height = h  
  CompilerEndIf
  rect\origin\x =2.5
  rect\origin\y =7.5
  rect\size\height =3.5
  barRadius.CGFloat = 3
  rect\size\width -2
  
  ;// Knob position depending on control min/max value And current control value.
  doubleValue.d
  CocoaMessage(@doubleValue, obj, "doubleValue")
  min.d
  CocoaMessage(@min, obj, "minValue")
  max.d
  CocoaMessage(@max, obj, "maxValue")
  
  value.d =(doubleValue - min) / (max - min) * 0.91
  
  ;// Left Part Width
  Protected controlView.i
  controlView = CocoaMessage(0, obj, "controlView", 0)
  LeftFrame.NSRect
  CocoaMessage(LeftFrame, controlView, "frame")
  
  finalWidth.CGFloat =value * (LeftFrame\size\width ) 
  finalWidth +7
  
  ;// Left Part Rect
  leftRect.NSRect
  leftRect = rect
  leftRect\size\width = finalWidth
  rect\origin\x =finalWidth
  rect\size\width - finalWidth
  bg = CocoaMessage(0, 0, "NSBezierPath bezierPathWithRoundedRect:@", @rect, "xRadius:@", @barRadius, "yRadius:@", @barRadius)
  Color = CocoaMessage(0, 0, "NSColor orangeColor")
  CocoaMessage(0, Color, "setFill")
  CocoaMessage(0, bg, "fill")
  
  
  ;// Draw Right Part
  active = CocoaMessage(0, 0, "NSBezierPath bezierPathWithRoundedRect:@", @leftRect, "xRadius:@", @barRadius, "yRadius:@", @barRadius)
  Color = CocoaMessage(0, 0, "NSColor purpleColor")
  CocoaMessage(0, Color, "setFill")
  CocoaMessage(0, active, "fill")
  
  CocoaMessage(0, NSView, "unlockFocus")
EndProcedure


Procedure OpenWindow_0(x = 0, y = 0, width = 260, height = 130)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Splitted Slider", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Slider = TrackBarGadget(#PB_Any, 20, 45, 220, 20, 1, 127, #PB_TrackBar_Vertical)

  
  SliderCell = CocoaMessage(0, GadgetID(Slider), "cell")
  CustomSliderCell = objc_allocateClassPair_(CocoaMessage(0, SliderCell, "class"), "CustomSliderCell", 0)
  
  ; ----- Set callback for drawing the track
  class_addMethod_(CustomSliderCell, sel_registerName_("drawBarInside:flipped:"),
                  @drawBarInside(), "v@:{CGRect={CGPoint=dd}{CGSize=dd}}c")
  
  ; ----- Set callback for drawing the knob
;   class_addMethod_(CustomSliderCell, sel_registerName_("drawKnob:"),
;                    @drawKnob(), "v@:{CGRect={CGPoint=dd}{CGSize=dd}}")
  
  objc_registerClassPair_(CustomSliderCell)
  object_setClass_(SliderCell, CustomSliderCell)
  
  
EndProcedure


Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Slider

      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure


OpenWindow_0()


Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False

End
macOS Catalina 10.15.7