TKnob

Source: source/classes/tknob.prg  |  Command: include/tknob.ch

Inherits from: TControl

Contributed by Silvio Falconi.

TKnob is a modern custom rotary knob control for FiveWin / Harbour, designed for audio, MIDI and synthesizer style applications (mixers, drum machines, VST-like interfaces and touch screens). It is a pure Harbour/FiveWin implementation built on TControl — all drawing is done with GDI primitives, so no extra DLL is required.

Features

Key DATA Members

DATATypeDefaultDescription
nValueNumeric64Current value (clamped to nMin..nMax)
nMinNumeric0Minimum value
nMaxNumeric127Maximum value
nDefaultNumeric64Value restored on double click
cIndicatorChar"LED"Indicator style: "LED" or "LINE"
cModeChar"UNIPOLAR"Range mode: "UNIPOLAR" or "BIPOLAR"
lTicksLogical.F.Draw external tick marks around the ring
lActiveLogical.T.Enabled state (dimmed when .F.)
nWheelStep / nCtrlStepNumeric1Wheel step, and finer step while CTRL is held
oFont / oFontInnerObjectFonts for the external label and the inner value text
bSetGetBlockSetGet codeblock for variable binding (read/write)
bChangedBlockFired when the value changes; receives ( Self, nValue )
bActionBlockFired on toggle; receives ( Self, lActive )

Methods

MethodDescription
New( nTop, nLeft, bSetGet, oWnd, nWidth, nHeight, cMsg, nMin, nMax, cIndicator, cMode, lTicks, bAction, lDesign, oFont, oFontInner )Create a new knob control
SetValue( nValue )Set the value (clamped), fire bSetGet/bChanged and repaint
GetValue()Return the current nValue
SetMode( cMode )Switch between "UNIPOLAR" (0..127) and "BIPOLAR" (-30..+30) and reset range/default
Toggle()Flip the enabled state and fire bAction
Display() / Paint()Render the knob (ring, glow arc, body, indicator, value, label)
Destroy()Destroy the control

The @ ... KNOB Command

Include tknob.ch to use the declarative command:

@ <nRow>, <nCol> KNOB <oKnob>            ;
   VAR <nVar>                            ;
   OF <oWnd>                             ;
   SIZE <nWidth>, <nHeight>              ;
   LABEL <cMsg>                          ;
   MIN <nMin> MAX <nMax>                 ;
   [ LED | LINE ]                        ;
   [ UNIPOLAR | BIPOLAR ]                ;
   [ TICKS | NOTICKS ]                   ;
   FONT <oFont> FONTINNER <oFontInner>   ;
   [ ON CHANGE <uChange> ]               ;
   [ ACTION <uAction> ]

Example: Volume & Modulation Knobs

#include "FiveWin.ch"
#include "tknob.ch"

function Main()

   local oDlg, oFont, oFontInner
   local oKnobVol, oKnobMod, oSayVol, oSayMod
   local nVol := 0, nMod := 64

   DEFINE FONT oFont      NAME "Segoe UI" SIZE 0, -11 BOLD
   DEFINE FONT oFontInner NAME "Segoe UI" SIZE 0, -14 BOLD

   DEFINE DIALOG oDlg TITLE "TKnob" SIZE 780, 280 PIXEL

   @ 35, 150 SAY oSayVol PROMPT "VOL: " + AllTrim( Str( nVol ) ) ;
      SIZE 40, 10 PIXEL OF oDlg

   @ 35, 40 KNOB oKnobVol VAR nVol OF oDlg SIZE 76, 86 ;
      LABEL "VOL" MIN -30 MAX 30 LED BIPOLAR NOTICKS ;
      FONT oFont FONTINNER oFontInner ;
      ON CHANGE { |o,n| oSayVol:SetText( "VOL: " + AllTrim( Str( n ) ) ) }

   @ 35, 370 SAY oSayMod PROMPT "MOD: " + AllTrim( Str( nMod ) ) ;
      SIZE 50, 10 PIXEL OF oDlg

   @ 35, 200 KNOB oKnobMod VAR nMod OF oDlg SIZE 76, 86 ;
      LABEL "MOD" MIN 0 MAX 200 LINE UNIPOLAR TICKS ;
      FONT oFont FONTINNER oFontInner ;
      ON CHANGE { |o,n| oSayMod:SetText( "MOD: " + AllTrim( Str( n ) ) ) }

   ACTIVATE DIALOG oDlg CENTERED ;
      VALID ( oFont:End(), oFontInner:End(), .T. )

return nil

Notes

See Also