CanvasGadget for Custom Gadgets
CanvasGadget for Custom Gadgets
Is the canvas gadget a too much to be used as custom gadgets? I am trying to make my own gadgets and the canvas gadget is the only one that responds to keyboard and mouse events. In my program I use up to 30 or 40 gadgets in one window. Is that too many canvas gadgets in one window? Will it use too much memory when the program is running compared to other gadgets like buttons and text? Will doing this slow the program down more? Buttons, texts and image gadgets take up less memory right?
Re: CanvasGadget for Custom Gadgets
Hi,
it uses more RAM and codesize.
But it has nothing todo with speed.
PB programming is event driven, no event, no time is used.
If a gadget is not active it sends no events.
Bernd
it uses more RAM and codesize.
But it has nothing todo with speed.
PB programming is event driven, no event, no time is used.
If a gadget is not active it sends no events.
Bernd
Re: CanvasGadget for Custom Gadgets
Thanks Bernd. That is right. I checked the exe file size and the running memory. The file size is double when canvas gadget is used instead of button.infratec wrote:Hi,
it uses more RAM and codesize.
But it has nothing todo with speed.
PB programming is event driven, no event, no time is used.
If a gadget is not active it sends no events.
Bernd
Why does the program use more ram when the canvas gadget is bigger but not when the button is bigger? I made 2 codes: 1 with just window and button and 1 with window and canvas. Making the button bigger does not increase the ram use but making the canvas bigger increases the ram use.

Re: CanvasGadget for Custom Gadgets
The main use for CanvasGadget is to create custom gadgets. That is what it is there for.
About the memory use: Every CanvasGadget has an image that stores its content for double-buffering. This image must grow when the gadget grows. A button does not have that. Still, this memory use should be no problem on any kind of recent computer
About the memory use: Every CanvasGadget has an image that stores its content for double-buffering. This image must grow when the gadget grows. A button does not have that. Still, this memory use should be no problem on any kind of recent computer

quidquid Latine dictum sit altum videtur
-
- Addict
- Posts: 1580
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: CanvasGadget for Custom Gadgets
Without the support of transparency is hard to do. For example how to make a round button?freak wrote:The main use for CanvasGadget is to create custom gadgets.
Re: CanvasGadget for Custom Gadgets
infratec has posted code to make round LEDGadgets.User_Russian wrote:Without the support of transparency is hard to do. For example how to make a round button?freak wrote:The main use for CanvasGadget is to create custom gadgets.
LEDGadget() (the round ones)
http://www.purebasic.fr/english/viewtop ... 12&t=59084
Can transparency be added to it?
Think Unicode!
-
- Addict
- Posts: 1580
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: CanvasGadget for Custom Gadgets
Are you kidding?ElementE wrote:the round ones


The gadget is not transparent and square.
Code: Select all
;
; LEDGadget.pbi
;
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
Enumeration
#LEDGadget_OnColor
#LEDGadget_OffColor
#LEDGadget_BorderColor
#LEDGadget_BackColor
EndEnumeration
Structure LEDGadgetStructure
Radius.i
OnColor.i
OffColor.i
BackColor.i
ActualColor.i
BorderColor.i
OnImg.i
OffImg.i
EndStructure
Procedure LEDGadgetDraw(Gadget)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If StartDrawing(CanvasOutput(Gadget))
If \OnImg <> -1 And \OffImg <> -1
If \ActualColor = \OnColor
DrawImage(ImageID(\OnImg), 0, 0)
Else
DrawImage(ImageID(\OffImg), 0, 0)
EndIf
Else
Box(0, 0, OutputWidth(), OutputHeight(), \BackColor)
If \BorderColor <> -1
Circle(\Radius, \Radius, \Radius, \BorderColor)
Circle(\Radius, \Radius, \Radius - 2, \ActualColor)
Else
Circle(\Radius, \Radius, \Radius, \ActualColor)
EndIf
EndIf
StopDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure LEDGadgetSetState(Gadget, State.i)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If State
\ActualColor = \OnColor
Else
\ActualColor = \OffColor
EndIf
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadgetSetAttribute(Gadget, attribute, value)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
Select attribute
Case #LEDGadget_OnColor : \OnColor = value
Case #LEDGadget_OffColor : \OffColor = value
Case #LEDGadget_BorderColor : \BorderColor = value
Case #LEDGadget_BackColor : \BackColor = value
EndSelect
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadget(Gadget, x, y, radius = 5, OnColor = $0000FF, OffColor = $7F7F7F, BorderColor = -1, BackColor = $F0F0F0, OnImg=-1, OffImg=-1)
Protected Result.i, *LEDGadget.LEDGadgetStructure, Width.i, Height.i
If Gadget = #PB_Any Or IsGadget(Gadget) = 0
If OnImg > -1
Width = ImageWidth(OnImg)
Height = ImageHeight(OnImg)
Else
Width = radius * 2 + 1
Height = radius * 2 + 1
EndIf
If Gadget = #PB_Any
Gadget = CanvasGadget(#PB_Any, x, y, Width, Height)
Result = Gadget
Else
Result = CanvasGadget(Gadget, x, y, Width, Height)
EndIf
If Result
*LEDGadget = AllocateMemory(SizeOf(LEDGadgetStructure))
SetGadgetData(Gadget, *LEDGadget)
With *LEDGadget
If radius = - 1
\Radius = 5
Else
\Radius = radius
EndIf
If OnColor = - 1
\OnColor = $0000FF
Else
\OnColor = OnColor
EndIf
If OffColor = - 1
\OffColor = $7F7F7F
Else
\OffColor = OffColor
EndIf
If BorderColor <> 1
\BorderColor = BorderColor
EndIf
If BackColor = - 1
\BackColor = $F0F0F0
Else
\BackColor = BackColor
EndIf
\ActualColor = \OffColor
\OnImg = OnImg
\OffImg = OffImg
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define.i Exit, Event
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)
CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, $ECE9D8)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 0, 0))
FrontColor(RGB(255, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
CreateImage(1, 20, 20)
StartDrawing(ImageOutput(1))
Box(0, 0, 20, 20, $ECE9D8)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 128, 128))
FrontColor(RGB(0, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
LEDGadget(2, 10, 40, 10, -1, -1, -1, -1, 0, 1)
ButtonGadget(3, 40, 40, 40, 20, "On", #PB_Button_Toggle)
Exit = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If GetGadgetState(1)
SetGadgetText(1, "Off")
LEDGadgetSetState(0, #True)
Else
SetGadgetText(1, "On")
LEDGadgetSetState(0, #False)
EndIf
Case 3
If GetGadgetState(3)
SetGadgetText(3, "Off")
LEDGadgetSetState(2, #True)
Else
SetGadgetText(3, "On")
LEDGadgetSetState(2, #False)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit
CompilerEndIf
Re: CanvasGadget for Custom Gadgets
Simulate transparency ! Copy the background under the canvas and paint it on, or use as backgroundcolor the window color.... ! Nothing magicUser_Russian wrote:Without the support of transparency is hard to do. For example how to make a round button?freak wrote:The main use for CanvasGadget is to create custom gadgets.

-
- Addict
- Posts: 1580
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: CanvasGadget for Custom Gadgets
To do this in PB has a cross-platform function?Bisonte wrote:Copy the background under the canvas
Re: CanvasGadget for Custom Gadgets
Ähem... Yes :User_Russian wrote:To do this in PB has a cross-platform function?Bisonte wrote:Copy the background under the canvas
Code: Select all
StartDrawing(WindowOutput(#Window))
Image = GrabDrawingImage(#PB_Any, x, y, w, h)
StopDrawing()
Re: CanvasGadget for Custom Gadgets
Modified version:
If no image is used, now the color of the window is used as backgroundcolor.
Should be crossplatform.
Bernd
Code: Select all
;
; LEDGadget.pbi
;
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
Enumeration
#LEDGadget_OnColor
#LEDGadget_OffColor
#LEDGadget_BorderColor
#LEDGadget_BackColor
EndEnumeration
Structure LEDGadgetStructure
Radius.i
OnColor.i
OffColor.i
BackColor.i
ActualColor.i
BorderColor.i
OnImg.i
OffImg.i
EndStructure
Procedure LEDGadgetDraw(Gadget)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If StartDrawing(CanvasOutput(Gadget))
If \OnImg <> -1 And \OffImg <> -1
If \ActualColor = \OnColor
DrawImage(ImageID(\OnImg), 0, 0)
Else
DrawImage(ImageID(\OffImg), 0, 0)
EndIf
Else
Box(0, 0, OutputWidth(), OutputHeight(), \BackColor)
If \BorderColor <> -1
Circle(\Radius, \Radius, \Radius, \BorderColor)
Circle(\Radius, \Radius, \Radius - 2, \ActualColor)
Else
Circle(\Radius, \Radius, \Radius, \ActualColor)
EndIf
EndIf
StopDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure LEDGadgetSetState(Gadget, State.i)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If State
\ActualColor = \OnColor
Else
\ActualColor = \OffColor
EndIf
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadgetSetAttribute(Gadget, attribute, value)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
Select attribute
Case #LEDGadget_OnColor : \OnColor = value
Case #LEDGadget_OffColor : \OffColor = value
Case #LEDGadget_BorderColor : \BorderColor = value
Case #LEDGadget_BackColor : \BackColor = value
EndSelect
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadget(Gadget, x, y, radius = 5, OnColor = $0000FF, OffColor = $7F7F7F, BorderColor = -1, BackColor = -1, OnImg=-1, OffImg=-1)
Protected Result.i, *LEDGadget.LEDGadgetStructure, Width.i, Height.i
If Gadget = #PB_Any Or IsGadget(Gadget) = 0
If OnImg > -1
Width = ImageWidth(OnImg)
Height = ImageHeight(OnImg)
Else
Width = radius * 2 + 1
Height = radius * 2 + 1
EndIf
If Gadget = #PB_Any
Gadget = CanvasGadget(#PB_Any, x, y, Width, Height)
Result = Gadget
Else
Result = CanvasGadget(Gadget, x, y, Width, Height)
EndIf
If Result
*LEDGadget = AllocateMemory(SizeOf(LEDGadgetStructure))
SetGadgetData(Gadget, *LEDGadget)
With *LEDGadget
If radius = - 1
\Radius = 5
Else
\Radius = radius
EndIf
If OnColor = - 1
\OnColor = $0000FF
Else
\OnColor = OnColor
EndIf
If OffColor = - 1
\OffColor = $7F7F7F
Else
\OffColor = OffColor
EndIf
If BorderColor <> 1
\BorderColor = BorderColor
EndIf
If BackColor = -1
Debug y
StartDrawing(WindowOutput(GetActiveWindow()))
\BackColor = Point(x + (Width >> 1), y + (Height >> 1))
StopDrawing()
Else
\BackColor = BackColor
EndIf
\ActualColor = \OffColor
\OnImg = OnImg
\OffImg = OffImg
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define.i Exit, Event, BackgroundColor
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)
StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()
CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 0, 0))
FrontColor(RGB(255, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
CreateImage(1, 20, 20)
StartDrawing(ImageOutput(1))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 128, 128))
FrontColor(RGB(0, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
LEDGadget(2, 10, 40, 10, -1, -1, -1, -1, 0, 1)
ButtonGadget(3, 40, 40, 40, 20, "On", #PB_Button_Toggle)
Exit = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If GetGadgetState(1)
SetGadgetText(1, "Off")
LEDGadgetSetState(0, #True)
Else
SetGadgetText(1, "On")
LEDGadgetSetState(0, #False)
EndIf
Case 3
If GetGadgetState(3)
SetGadgetText(3, "Off")
LEDGadgetSetState(2, #True)
Else
SetGadgetText(3, "On")
LEDGadgetSetState(2, #False)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit
CompilerEndIf
Should be crossplatform.
Bernd
-
- Addict
- Posts: 1580
- Joined: Wed Nov 12, 2008 5:01 pm
- Location: Russia
Re: CanvasGadget for Custom Gadgets
Code: Select all
;
; LEDGadget.pbi
;
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
Enumeration
#LEDGadget_OnColor
#LEDGadget_OffColor
#LEDGadget_BorderColor
#LEDGadget_BackColor
EndEnumeration
Structure LEDGadgetStructure
Radius.i
OnColor.i
OffColor.i
BackColor.i
ActualColor.i
BorderColor.i
OnImg.i
OffImg.i
EndStructure
Procedure LEDGadgetDraw(Gadget)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If StartDrawing(CanvasOutput(Gadget))
If \OnImg <> -1 And \OffImg <> -1
If \ActualColor = \OnColor
DrawImage(ImageID(\OnImg), 0, 0)
Else
DrawImage(ImageID(\OffImg), 0, 0)
EndIf
Else
Box(0, 0, OutputWidth(), OutputHeight(), \BackColor)
If \BorderColor <> -1
Circle(\Radius, \Radius, \Radius, \BorderColor)
Circle(\Radius, \Radius, \Radius - 2, \ActualColor)
Else
Circle(\Radius, \Radius, \Radius, \ActualColor)
EndIf
EndIf
StopDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure LEDGadgetSetState(Gadget, State.i)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If State
\ActualColor = \OnColor
Else
\ActualColor = \OffColor
EndIf
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadgetSetAttribute(Gadget, attribute, value)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
Select attribute
Case #LEDGadget_OnColor : \OnColor = value
Case #LEDGadget_OffColor : \OffColor = value
Case #LEDGadget_BorderColor : \BorderColor = value
Case #LEDGadget_BackColor : \BackColor = value
EndSelect
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadget(Gadget, x, y, radius = 5, OnColor = $0000FF, OffColor = $7F7F7F, BorderColor = -1, BackColor = -1, OnImg=-1, OffImg=-1)
Protected Result.i, *LEDGadget.LEDGadgetStructure, Width.i, Height.i
If Gadget = #PB_Any Or IsGadget(Gadget) = 0
If OnImg > -1
Width = ImageWidth(OnImg)
Height = ImageHeight(OnImg)
Else
Width = radius * 2 + 1
Height = radius * 2 + 1
EndIf
If Gadget = #PB_Any
Gadget = CanvasGadget(#PB_Any, x, y, Width, Height)
Result = Gadget
Else
Result = CanvasGadget(Gadget, x, y, Width, Height)
EndIf
If Result
*LEDGadget = AllocateMemory(SizeOf(LEDGadgetStructure))
SetGadgetData(Gadget, *LEDGadget)
With *LEDGadget
If radius = - 1
\Radius = 5
Else
\Radius = radius
EndIf
If OnColor = - 1
\OnColor = $0000FF
Else
\OnColor = OnColor
EndIf
If OffColor = - 1
\OffColor = $7F7F7F
Else
\OffColor = OffColor
EndIf
If BorderColor <> 1
\BorderColor = BorderColor
EndIf
If BackColor = -1
Debug y
StartDrawing(WindowOutput(GetActiveWindow()))
\BackColor = Point(x + (Width >> 1), y + (Height >> 1))
StopDrawing()
Else
\BackColor = BackColor
EndIf
\ActualColor = \OffColor
\OnImg = OnImg
\OffImg = OffImg
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define.i Exit, Event, BackgroundColor
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))
ContainerGadget(100, 0, 0, 100, 90)
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)
StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()
CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 0, 0))
FrontColor(RGB(255, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
CreateImage(1, 20, 20)
StartDrawing(ImageOutput(1))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 128, 128))
FrontColor(RGB(0, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
LEDGadget(2, 10, 40, 10, -1, -1, -1, -1, 0, 1)
ButtonGadget(3, 40, 40, 40, 20, "On", #PB_Button_Toggle)
CloseGadgetList()
Exit = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If GetGadgetState(1)
SetGadgetText(1, "Off")
LEDGadgetSetState(0, #True)
Else
SetGadgetText(1, "On")
LEDGadgetSetState(0, #False)
EndIf
Case 3
If GetGadgetState(3)
SetGadgetText(3, "Off")
LEDGadgetSetState(2, #True)
Else
SetGadgetText(3, "On")
LEDGadgetSetState(2, #False)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit
CompilerEndIf
Code: Select all
;
; LEDGadget.pbi
;
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf
Enumeration
#LEDGadget_OnColor
#LEDGadget_OffColor
#LEDGadget_BorderColor
#LEDGadget_BackColor
EndEnumeration
Structure LEDGadgetStructure
Radius.i
OnColor.i
OffColor.i
BackColor.i
ActualColor.i
BorderColor.i
OnImg.i
OffImg.i
EndStructure
Procedure LEDGadgetDraw(Gadget)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If StartDrawing(CanvasOutput(Gadget))
If \OnImg <> -1 And \OffImg <> -1
If \ActualColor = \OnColor
DrawImage(ImageID(\OnImg), 0, 0)
Else
DrawImage(ImageID(\OffImg), 0, 0)
EndIf
Else
Box(0, 0, OutputWidth(), OutputHeight(), \BackColor)
If \BorderColor <> -1
Circle(\Radius, \Radius, \Radius, \BorderColor)
Circle(\Radius, \Radius, \Radius - 2, \ActualColor)
Else
Circle(\Radius, \Radius, \Radius, \ActualColor)
EndIf
EndIf
StopDrawing()
EndIf
EndWith
EndIf
EndProcedure
Procedure LEDGadgetSetState(Gadget, State.i)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
If State
\ActualColor = \OnColor
Else
\ActualColor = \OffColor
EndIf
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadgetSetAttribute(Gadget, attribute, value)
Protected *LEDGadget.LEDGadgetStructure
If IsGadget(Gadget)
*LEDGadget = GetGadgetData(Gadget)
With *LEDGadget
Select attribute
Case #LEDGadget_OnColor : \OnColor = value
Case #LEDGadget_OffColor : \OffColor = value
Case #LEDGadget_BorderColor : \BorderColor = value
Case #LEDGadget_BackColor : \BackColor = value
EndSelect
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndProcedure
Procedure LEDGadget(Gadget, x, y, radius = 5, OnColor = $0000FF, OffColor = $7F7F7F, BorderColor = -1, BackColor = -1, OnImg=-1, OffImg=-1)
Protected Result.i, *LEDGadget.LEDGadgetStructure, Width.i, Height.i
If Gadget = #PB_Any Or IsGadget(Gadget) = 0
If OnImg > -1
Width = ImageWidth(OnImg)
Height = ImageHeight(OnImg)
Else
Width = radius * 2 + 1
Height = radius * 2 + 1
EndIf
If Gadget = #PB_Any
Gadget = CanvasGadget(#PB_Any, x, y, Width, Height)
Result = Gadget
Else
Result = CanvasGadget(Gadget, x, y, Width, Height)
EndIf
If Result
*LEDGadget = AllocateMemory(SizeOf(LEDGadgetStructure))
SetGadgetData(Gadget, *LEDGadget)
With *LEDGadget
If radius = - 1
\Radius = 5
Else
\Radius = radius
EndIf
If OnColor = - 1
\OnColor = $0000FF
Else
\OnColor = OnColor
EndIf
If OffColor = - 1
\OffColor = $7F7F7F
Else
\OffColor = OffColor
EndIf
If BorderColor <> 1
\BorderColor = BorderColor
EndIf
If BackColor = -1
Debug y
StartDrawing(WindowOutput(GetActiveWindow()))
\BackColor = Point(x + (Width >> 1), y + (Height >> 1))
StopDrawing()
Else
\BackColor = BackColor
EndIf
\ActualColor = \OffColor
\OnImg = OnImg
\OffImg = OffImg
EndWith
LEDGadgetDraw(Gadget)
EndIf
EndIf
ProcedureReturn Result
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Define.i Exit, Event, BackgroundColor
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))
PanelGadget(100, 0, 0, 100, 90)
AddGadgetItem(100, -1, "Panel")
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)
StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()
CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 0, 0))
FrontColor(RGB(255, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
CreateImage(1, 20, 20)
StartDrawing(ImageOutput(1))
Box(0, 0, 20, 20, BackgroundColor)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 128, 128))
FrontColor(RGB(0, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()
LEDGadget(2, 10, 40, 10, -1, -1, -1, -1, 0, 1)
ButtonGadget(3, 40, 40, 40, 20, "On", #PB_Button_Toggle)
CloseGadgetList()
Exit = #False
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
If GetGadgetState(1)
SetGadgetText(1, "Off")
LEDGadgetSetState(0, #True)
Else
SetGadgetText(1, "On")
LEDGadgetSetState(0, #False)
EndIf
Case 3
If GetGadgetState(3)
SetGadgetText(3, "Off")
LEDGadgetSetState(2, #True)
Else
SetGadgetText(3, "On")
LEDGadgetSetState(2, #False)
EndIf
EndSelect
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit
CompilerEndIf
Re: CanvasGadget for Custom Gadgets
Hm....
just tested with Linux: not working
Because Point() doesn't return the right color.
And also SetWindowColor() has a border arround.
Bernd
just tested with Linux: not working

Because Point() doesn't return the right color.
And also SetWindowColor() has a border arround.
Bernd
Re: CanvasGadget for Custom Gadgets
Hello. Another approach with version 4.40 of Purebasic.
Code: Select all
code off topic
Last edited by falsam on Tue Nov 10, 2015 1:21 pm, edited 1 time in total.
➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti
Sorry for my bad english and the Dunning–Kruger effect
Re: CanvasGadget for Custom Gadgets
@falsam
your approach has nothing todo with a gadget.
At least if you want to place it in a panel or a container it fails.
Bernd
your approach has nothing todo with a gadget.
At least if you want to place it in a panel or a container it fails.
Bernd