a last addition: filterBoxGadgetSetColor()
Code: Select all
;Program: FilterBoxGadget.pbi
;Author: intratec, modified by Demivec
;version: 3
#FilterBoxPointCount = 5
#FilterBoxPointRadius = 5
CompilerIf Defined(PB_Gadget_DisableColor, #PB_Constant) = 0
#PB_Gadget_DisableColor = 20
CompilerEndIf
Structure filterBoxGadgetStr
dataPoint.POINT[#FilterBoxPointCount]
width.i
height.i
disabled.i
activePoint.i
frontColor.i
backColor.i
disableColor.i
EndStructure
Procedure filterBoxGadgetDraw(gadgetNo)
Protected *filterBoxGadget.filterBoxGadgetStr
Protected i
If IsGadget(gadgetNo)
*filterBoxGadget = GetGadgetData(gadgetNo)
With *filterBoxGadget
If StartDrawing(CanvasOutput(gadgetNo))
If \disabled
Box(0, 0, \width, \height, \disableColor)
Else
Box(0, 0, \width, \height, \backColor)
EndIf
For i = 0 To #FilterBoxPointCount - 1
Circle(\dataPoint[i]\x, \dataPoint[i]\y, #FilterBoxPointRadius, \frontColor)
If i < #FilterBoxPointCount - 1
LineXY(\dataPoint[i]\x, \dataPoint[i]\y, \dataPoint[i + 1]\x, \dataPoint[i + 1]\y, \frontColor)
EndIf
Next
StopDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure filterBoxGadget(gadgetNo, x, y, width, height, fgc = $0000FF, bgc = $FFFFFF, dc = $E0E0E0)
Protected *filterBoxGadget.filterBoxGadgetStr
Protected i
If gadgetNo = #PB_Any Or IsGadget(gadgetNo) = 0
gadgetNo = CanvasGadget(#PB_Any, x, y, width, height, #PB_Canvas_Border)
If gadgetNo
*filterBoxGadget = AllocateMemory(SizeOf(filterBoxGadgetStr))
SetGadgetData(gadgetNo, *filterBoxGadget)
;gadget output will be smaller if borders are drawn
StartDrawing(CanvasOutput(gadgetNo))
width = OutputWidth()
height = OutputHeight()
StopDrawing()
With *filterBoxGadget
\width = width
\height = height
\dataPoint[0]\x = 0
\dataPoint[0]\y = height / 2
For i = 1 To #FilterBoxPointCount - 2
\dataPoint[i]\x = ((0.0 + width - #FilterBoxPointRadius) / (#FilterBoxPointCount - 1)) * i
\dataPoint[i]\y = height / 2
Next
\dataPoint[#FilterBoxPointCount - 1]\x = width - 1
\dataPoint[#FilterBoxPointCount - 1]\y = height / 2
\disabled = #False
\activePoint = -1
\frontColor = fgc
\backColor = bgc
\disableColor = dc
EndWith
EndIf
EndIf
filterBoxGadgetDraw(gadgetNo)
ProcedureReturn gadgetNo
EndProcedure
Procedure filterBoxGadgetEvent(gadgetNo)
Protected *filterBoxGadget.filterBoxGadgetStr
Protected result
Protected xPos, yPos
Protected i
result = 0
*filterBoxGadget = GetGadgetData(gadgetNo)
With *filterBoxGadget
If Not \disabled
xPos = GetGadgetAttribute(gadgetNo, #PB_Canvas_MouseX)
yPos = GetGadgetAttribute(gadgetNo, #PB_Canvas_MouseY)
If xPos < 0 : xPos = 0 : EndIf
If xPos > \width - 1: xPos = \width - 1: EndIf
If yPos < 0 : yPos = 0 : EndIf
If yPos > \height - 1 : yPos = \height - 1 : EndIf
Select EventType()
Case #PB_EventType_LeftButtonDown
If \activePoint = -1
For i = 0 To #FilterBoxPointCount - 1
If (\dataPoint[i]\x - #FilterBoxPointRadius) < xPos And (\dataPoint[i]\x + #FilterBoxPointRadius) > xPos
If (\dataPoint[i]\y - #FilterBoxPointRadius) < yPos And (\dataPoint[i]\y + #FilterBoxPointRadius) > yPos
\activePoint = i
SetGadgetAttribute(gadgetNo, #PB_Canvas_Cursor, #PB_Cursor_Hand)
Break
EndIf
EndIf
Next
EndIf
Case #PB_EventType_MouseMove
If \activePoint <> -1
\dataPoint[\activePoint]\y = yPos
If \activePoint > 0 And \activePoint < #FilterBoxPointCount - 1
If xPos < #FilterBoxPointRadius: xPos = #FilterBoxPointRadius: EndIf
If xPos > \width - #FilterBoxPointRadius: xPos = \width - #FilterBoxPointRadius: EndIf
If xPos >= \dataPoint[\activePoint - 1]\x
If xPos <= \dataPoint[\activePoint + 1]\x
\dataPoint[\activePoint]\x = xPos
Else
\dataPoint[\activePoint]\x = \dataPoint[\activePoint + 1]\x
EndIf
Else
\dataPoint[\activePoint]\x = \dataPoint[\activePoint - 1]\x
EndIf
EndIf
filterBoxGadgetDraw(gadgetNo)
EndIf
Case #PB_EventType_LeftButtonUp
If \activePoint <> -1
\activePoint = -1
SetGadgetAttribute(gadgetNo, #PB_Canvas_Cursor, #PB_Cursor_Default)
result = 1
EndIf
EndSelect
EndIf
EndWith
ProcedureReturn result
EndProcedure
Procedure disableFilterBoxGadget(gadgetNo, state)
Protected *filterBoxGadget.filterBoxGadgetStr
*filterBoxGadget= GetGadgetData(gadgetNo)
If state
If Not *filterBoxGadget\disabled
*filterBoxGadget\disabled = #True
filterBoxGadgetDraw(gadgetNo)
EndIf
Else
If *filterBoxGadget\disabled
*filterBoxGadget\disabled = #False
filterBoxGadgetDraw(gadgetNo)
EndIf
EndIf
EndProcedure
Procedure filterBoxGadgetGetState(gadgetNo, Array dataPoint.POINT(1))
Protected *filterBoxGadget.filterBoxGadgetStr
Protected result, i
result = #False
If IsGadget(gadgetNo)
*filterBoxGadget = GetGadgetData(gadgetNo)
Dim dataPoint(#FilterBoxPointCount - 1)
For i = 0 To #FilterBoxPointCount - 1
dataPoint(i)\x = *filterBoxGadget\dataPoint[i]\x
dataPoint(i)\y = *filterBoxGadget\dataPoint[i]\y
Next
result = #True
EndIf
ProcedureReturn result
EndProcedure
Procedure filterBoxGadgetSetColor(gadgetNo, attribute, value)
Protected *filterBoxGadget.filterBoxGadgetStr
If IsGadget(gadgetNo)
*filterBoxGadget = GetGadgetData(gadgetNo)
With *filterBoxGadget
Select attribute
Case #PB_Gadget_FrontColor : \frontColor = value
Case #PB_Gadget_BackColor : \backColor = value
Case #PB_Gadget_DisableColor : \disableColor = value
EndSelect
EndWith
filterBoxGadgetDraw(gadgetNo)
EndIf
EndProcedure
Code: Select all
XIncludeFile "FilterBoxGadget.pbi"
Dim filter.POINT(#FilterBoxPointCount - 1)
OpenWindow(0, 0, 0, 430, 340, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
no1 = FilterBoxGadget(#PB_Any, 10, 10, 200, 100)
no2 = FilterBoxGadget(#PB_Any, 10, 120, 200, 100)
no3 = FilterBoxGadget(#PB_Any, 220, 120, 200, 100)
no4 = FilterBoxGadget(#PB_Any, 10, 230, 200, 100)
no5 = FilterBoxGadget(#PB_Any, 220, 230, 200, 100)
filterBoxGadgetSetColor(no2, #PB_Gadget_FrontColor, $FF0000)
filterBoxGadgetSetColor(no2, #PB_Gadget_BackColor, $00FFFF)
disableFilterBoxGadget(no5, #True)
exit = #False
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case no1
If FilterBoxGadgetEvent(no1) = 1
If FilterBoxGadgetGetState(no1, filter())
For i = 0 To #FilterBoxPointCount - 1
Debug "P" + Str(i + 1) + " (" + Str(filter(i)\x) + "/" + Str(filter(i)\y) + ")"
Next i
EndIf
EndIf
Case no2 : FilterBoxGadgetEvent(no2)
Case no3 : FilterBoxGadgetEvent(no3)
Case no4 : FilterBoxGadgetEvent(no4)
Case no5 : FilterBoxGadgetEvent(no5)
EndSelect
Case #PB_Event_CloseWindow
exit = #True
EndSelect
Until exit


