Create your own customized TrackBars

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Create your own customized TrackBars

Post 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.
Last edited by Shardik on Tue Oct 11, 2011 8:34 pm, edited 1 time in total.
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Create your own customized TrackBars

Post by jamirokwai »

Cool finding, Shardik. Thanks for sharing!
Regards,
JamiroKwai
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: Create your own customized TrackBars

Post 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..
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Create your own customized TrackBars

Post 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
USCode
Addict
Addict
Posts: 923
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle

Re: Create your own customized TrackBars

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Create your own customized TrackBars

Post 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:
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Create your own customized TrackBars

Post 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! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 344
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: Create your own customized TrackBars

Post by ar-s »

it looks good but i've got an error.
Structure or interface or prototype already declared : rect
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Create your own customized TrackBars

Post 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:
Post Reply