Page 1 of 1

Create your own customized TrackBars

Posted: Tue Mar 22, 2011 10:09 pm
by Shardik
PB's TrackBarGadget is a bit limited. Using the API you can customize your TrackBars
like that:

Image

The drawback of implementing custom Gadgets is that you can't use PB's standard
functions like GetGadgetState() to read changed settings, so you have to program your
own functions again using the API.

Take a look into my code example that generated the above snapshot. To create a
custom TrackBar you have to call the procedure CreateCustomTrackBar() and to read
the current value of a TrackBar you have to call the function GetControl32BitValue()
instead of GetGadgetState(). Not too complicated, isn't it? :wink:

Code: Select all

ImportC ""
  CreateSliderControl(WindowRef.L, *BoundsRectangle, InitialValue.L, Minimum.L, Maximum.L, Orientation.L, TickMarksCount.U, LiveTracking.L, LiveTrackingCallback.L, *ControlRef)
  GetControl32BitValue(ControlRef.L)
  ShowControl(ControlRef.L)
EndImport

#NumCustomTrackBars = 5

; ----- Control Slider Orientation Constants

#kControlSliderPointsDownOrRight = 0
#kControlSliderPointsUpOrLeft    = 1
#kControlSliderDoesNotPoint      = 2

Structure Rect
  Top.W
  Left.W
  Bottom.W
  Right.W
EndStructure

Define Bounds.Rect
Define Info.S

Dim CustomTrackBar(#NumCustomTrackBars - 1)

ProcedureC CreateCustomTrackBar(WindowRef.L, x.W, y.W, Width.W, Height.W, Orientation.L, InitialValue.L, Minimum.L, Maximum.L)
  Protected Bounds.Rect
  Protected GadgetRef.L

  Bounds\Left   = x
  Bounds\Top    = y
  Bounds\Right  = x + Width
  Bounds\Bottom = y + Height

  If CreateSliderControl(WindowRef, @Bounds, InitialValue, Minimum, Maximum, Orientation, Maximum - Minimum + 1, #False, 0, @GadgetRef) = 0
    ShowControl(GadgetRef)
    ProcedureReturn GadgetRef
  EndIf
EndProcedure 

OpenWindow(0, 200, 100, 530, 200, "Custom TrackBars created via API", #PB_Window_SystemMenu)

; ----- Create 1st TrackBar (horizontal) with tick marks below

CustomTrackBar(0) = CreateCustomTrackBar(WindowID(0), 70, 20, 190, 20, #kControlSliderPointsUpOrLeft, 5, 0, 10)
TextGadget(0, 280, 20, 220, 20, "With tick marks above slider")

; ----- Create 2nd TrackBar (horizontal) with tick marks above

CustomTrackBar(1) = CreateCustomTrackBar(WindowID(0), 70, 60, 190, 20, #kControlSliderPointsDownOrRight, 10, 0, 20)
TextGadget(1, 280, 60, 220, 20, "With tick marks below slider")

; ----- Create 3rd TrackBar (horizontal) without tick marks

CustomTrackBar(2) = CreateCustomTrackBar(WindowID(0), 70, 100, 190, 20, #kControlSliderDoesNotPoint, 5, 0, 10)
TextGadget(2, 280, 100, 220, 20, "Without tick marks and pointer")

; ----- Create 4th TrackBar (vertical) with tick marks at right side

CustomTrackBar(3) = CreateCustomTrackBar(WindowID(0), 10, 10, 20, 180, #kControlSliderPointsDownOrRight, 10, 0, 20)
TextGadget(3, 40, 175, 220, 20, "With tick marks at right side")

; ----- Create 5th TrackBar (vertical) with tick marks at right side

CustomTrackBar(4) = CreateCustomTrackBar(WindowID(0), 500, 10, 20, 180, #kControlSliderPointsDownOrRight, 10, 0, 20)
TextGadget(4, 275, 175, 220, 20, "With tick marks at left side", #PB_Text_Right)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

; ----- Read the current values of the custom TrackBarGadgets and display them

For i = 1 To #NumCustomTrackBars
  Info + "TrackBar " + Str(i) + ": " + Str(GetControl32BitValue(CustomTrackBar(i - 1))) + #CR$
Next i

CloseWindow(0)

MessageRequester("TrackBar settings", Info)
Update: I changed Procedure to ProcedureC to be compatible with Lion and I corrected the comment for the 5th TrackBar.

Re: Create your own customized TrackBars

Posted: Tue Mar 22, 2011 11:44 pm
by jamirokwai
Cool finding, Shardik. Thanks for sharing!

Re: Create your own customized TrackBars

Posted: Fri Mar 25, 2011 4:35 am
by lexvictory
If you want to use the standard gadget commands on it, there is a helper include in TailBite that lets you create custom gadgets. I don't know that I've tested it on OS X, but it should work without much modification..

Re: Create your own customized TrackBars

Posted: Mon Oct 10, 2011 8:55 pm
by Shardik
While reading Apple's HIToolbox Release Notes for Mac OS X v10.5 recently I
detected new API functions (introduced with Leopard) that enable us to modify
PB's TrackBarGadget to display tick marks and to change the pointer orientation
without needing to define the TrackBar with the API function CreateSliderControl()
as demonstrated in my first posting. My new code example therefore reveals how
to toggle these features of PB's TrackBarGadget during runtime:

Code: Select all

ImportC ""
  HISliderSetTickMarkCount(HIViewRef.L, NumTicks.L)
  HISliderSetThumbOrientation(HIViewRef.L, Orientation.L)
EndImport

#kControlSliderPointsDownOrRight = 0
#kControlSliderPointsUpOrLeft    = 1
#kControlSliderDoesNotPoint      = 2

OpenWindow(0, 270, 100, 370, 130, "Toggle TrackBar features")
TrackBarGadget(0, 170, 55, 180, 20, 0, 10, #PB_TrackBar_Ticks)
SetGadgetState(0, 5)
CheckBoxGadget(1, 10, 10, 180, 20, "Display Tick Marks")
OptionGadget(2, 10, 40, 90, 20, "Point down")
OptionGadget(3, 10, 70, 90, 20, "Point up")
OptionGadget(4, 10, 100, 90, 20, "No pointer")
SetGadgetState(2, #True)

TickMarksAreDisplayed = #False

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          If TickMarksAreDisplayed
            HISliderSetTickMarkCount(GadgetID(0), 0)
          Else
            HISliderSetTickMarkCount(GadgetID(0), 11)
          EndIf

          TickMarksAreDisplayed ! 1
        Case 2
          HISliderSetThumbOrientation(GadgetID(0), #kControlSliderPointsDownOrRight)
          ResizeGadget(0, #PB_Ignore, 55, #PB_Ignore, #PB_Ignore)
          DisableGadget(1, #False)
        Case 3
          HISliderSetThumbOrientation(GadgetID(0), #kControlSliderPointsUpOrLeft)
          ResizeGadget(0, #PB_Ignore, 49, #PB_Ignore, #PB_Ignore)
          DisableGadget(1, #False)
        Case 4
          HISliderSetThumbOrientation(GadgetID(0), #kControlSliderDoesNotPoint)
          HISliderSetTickMarkCount(GadgetID(0), 0)
          ResizeGadget(0, #PB_Ignore, 52, #PB_Ignore, #PB_Ignore)
          TickMarksAreDisplayed = #False
          SetGadgetState(1, #False)
          DisableGadget(1, #True)
      EndSelect

      Draw1Control_(GadgetID(0))
  EndSelect
ForEver

Re: Create your own customized TrackBars

Posted: Mon Oct 10, 2011 9:39 pm
by USCode
The limited functionality of PB's gadgets is of course limited by PB's cross-platform-ness and the capabilities of ALL the platforms.
Very cool, thanks for sharing.

Re: Create your own customized TrackBars

Posted: Tue Oct 11, 2011 8:18 am
by Shardik
USCode wrote:The limited functionality of PB's gadgets is of course limited by PB's cross-platform-ness and the capabilities of ALL the platforms.
I am fully aware of the fact that the functionality of PB's gadgets are limited because
all gadget features have to be supported on all platforms. The reason to buy my first
Mac ever in the last year was to be able to develop cross-platform applications on one
machine for all 3 platforms. And while trying to display tick marks in a cross-platform
app on the Mac I found out that the flag #PB_TrackBar_Ticks doesn't work and I posted
this bug report. While delving deeper into Apple's documentation to find a workaround
for the missing tick marks in the Mac version of my app I found several additional
interesting features of the TrackBarGadget (Slider in Mac speak) and posted it so that
other Mac only programmers might benefit from my findings and give the devs a hint
for repairing the tick marks bug in the Mac version... :wink:

Re: Create your own customized TrackBars

Posted: Tue Oct 11, 2011 8:29 am
by J. Baker
Shardik wrote:... (Slider in Mac speak) ...
So if your slider is facing up, does that make it a "back slider"? :D
Sorry, I couldn't resist. Thanks for posting the code! ;)

Re: Create your own customized TrackBars

Posted: Tue Oct 11, 2011 12:23 pm
by ar-s
it looks good but i've got an error.
Structure or interface or prototype already declared : rect

Re: Create your own customized TrackBars

Posted: Tue Oct 11, 2011 8:26 pm
by Shardik
ar-s wrote:it looks good but i've got an error.
Structure or interface or prototype already declared : rect
Did you try the example code from my first posting in Windows?
I obtain exactly your error message when trying to run the example
in Windows 7. But my example runs just fine on Mac OS X 10.6.8
(Snow Leopard). This is the Mac OS X subforum... :wink: