Custom slider gadget
Posted: Sun Jul 17, 2005 1:45 pm
Code updated For 5.20+
Hello, I've made the following slider gadget.
The idea is something similat to those used with paint programs:
Despite I've posted this code in Tricks 'n' Tips, I still ask you if is there a better way to do it.
I'm looking with a better integration with the other gadgets, ability to create many instances etc.
Great would be a cross platform compatibility too.
Perhaps should exist a library to ceate cross compatible custom gadtets?
Hello, I've made the following slider gadget.
The idea is something similat to those used with paint programs:
Code: Select all
#COLOR_BTNFACE=15
#grad_paddingleft=5
#grad_paddingright=30
Declare UpdateGradient(i,g)
Procedure GetMouseX(gadget)
GetCursorPos_(mouse.POINT)
MapWindowPoints_(0, GadgetID(gadget), mouse, 1)
ProcedureReturn mouse\x
EndProcedure
Procedure GetMouseY(gadget)
GetCursorPos_(mouse.POINT)
MapWindowPoints_(0, GadgetID(gadget), mouse, 1)
ProcedureReturn mouse\y
EndProcedure
Procedure min(n1,n2)
If n1<n2
ProcedureReturn n1
Else
ProcedureReturn n2
EndIf
EndProcedure
Procedure max(n1,n2)
If n1>n2
ProcedureReturn n1
Else
ProcedureReturn n2
EndIf
EndProcedure
Procedure GradientPickerInit(i,g,x,y,w,h)
alphaImg = CreateImage(i,w,h)
alphaGad = ImageGadget(g,x,y,w,h,colorsImg)
bgcolor=GetSysColor_(#COLOR_BTNFACE)
StartDrawing(ImageOutput(i))
Box(0,0,w,h,bgcolor)
gradientwidth=w-#grad_paddingleft-#grad_paddingright
For x=0 To gradientwidth
LineXY(x+#grad_paddingleft, 0, x+#grad_paddingleft, h-9, RGB(x*255/gradientwidth,x*255/gradientwidth,x*255/gradientwidth))
Next
StopDrawing()
SetGadgetState(g, alphaImg)
UpdateGradient(i,g)
EndProcedure
Procedure UpdateGradient(i,g)
alphaImg=ImageID(i)
iwid=ImageWidth(i)
value=min(max(GetMouseX(g),#grad_paddingleft), iwid-#grad_paddingright)
bgcolor=GetSysColor_(#COLOR_BTNFACE)
yy=ImageHeight(i)-9
xx=value
value-#grad_paddingleft
value=min(value,iwid-#grad_paddingleft-#grad_paddingright)
value=255*value/(iwid-#grad_paddingleft-#grad_paddingright)
StartDrawing(ImageOutput(i))
Box(0,yy,iwid,ImageHeight(i),bgcolor)
LineXY(xx, 12,xx-5, 17,000)
LineXY(xx-5, 17,xx+5, 17,000)
LineXY(xx+5, 17,xx, 12,000)
BackColor(RGB(Red(bgcolor),Green(bgcolor),Blue(bgcolor)))
DrawText(iwid-#grad_paddingright+6,0,Str(value)+" ")
StopDrawing()
SetGadgetState(g, alphaImg)
ProcedureReturn value
EndProcedure
#i_alpha1=0
#g_alpha1=0
hTWindow=OpenWindow(1,510,0,128,100,"test",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)
GradientPickerInit(#i_alpha1,#g_alpha1, 0, 50, 120, 20)
Repeat
wwe = WaitWindowEvent()
egid = EventGadget()
Select wwe:
Case #PB_Event_CloseWindow
End
Case $332c
Select egid
Case #g_alpha1
v=UpdateGradient(#i_alpha1,#g_alpha1)
EndSelect
EndSelect
ForEver
I'm looking with a better integration with the other gadgets, ability to create many instances etc.
Great would be a cross platform compatibility too.
Perhaps should exist a library to ceate cross compatible custom gadtets?