Code: Select all
;-TOP
; Comment : Object SwitchGadget Number 42 ;)
; Author : mk-soft
; Version : v1.06.0
; Create : 01.05.2019
; Update : 18.01.2020
; OS : All
; Link BaseClass : https://www.purebasic.fr/english/viewtopic.php?f=12&t=64305
IncludeFile "Modul_BaseClassSmall.pb"
EnableExplicit
; *****************************************************************************
;- Module Public
DeclareModule SwitchGadget
UseModule BaseClass
Interface iSwitchGadget Extends iBaseClass
Resize(x, y, Width, Height)
Redraw(State = #True)
GetID()
SetColor(ColorType, Color)
GetColor(ColorType)
SetState(State)
GetState()
SetUserData(UserData)
GetUserData()
EndInterface
UnuseModule BaseClass
Declare Create(Gadget, x, y, Width, Height, FrontColor = #PB_Default, LineColor = #PB_Default, Flags = 0)
EndDeclareModule
;- Module Private
Module SwitchGadget
EnableExplicit
UseModule BaseClass
NewClass(iSwitchGadget)
Structure sSwitchGadget Extends sBaseClass
Gadget.i
UserData.i
; Params
x.i
y.i
Width.i
Height.i
LineColor.i
FrontColor.i
BackColor.i
Flags.i
; Data
Redraw.i
Event.i
State.i
EndStructure
Declare DrawGadget(*this)
;-- Public Object Function
Procedure Resize(*this.sSwitchGadget, x, y, Width, Height)
With *this
If x <> #PB_Ignore
\x = x
EndIf
If y <> #PB_Ignore
\y = y
EndIf
If Width <> #PB_Ignore
\Width = Width
EndIf
If Height <> #PB_Ignore
\Height = Height
EndIf
ResizeGadget(\Gadget, \x, \y, \Width, \Height)
EndWith
EndProcedure : AsMethode(Resize)
; ----
Procedure Redraw(*this.sSwitchGadget, State)
With *this
\Redraw = State
If \Redraw
DrawGadget(*this)
EndIf
EndWith
EndProcedure : AsMethode(Redraw)
; ----
Procedure GetID(*this.sSwitchGadget)
ProcedureReturn *this\Gadget
EndProcedure : AsMethode(GetID)
; ----
Procedure SetColor(*this.sSwitchGadget, ColorType, Color)
With *this
Select ColorType
Case #PB_Gadget_FrontColor
\FrontColor = Color
Case #PB_Gadget_BackColor
\BackColor = Color
Case #PB_Gadget_LineColor
\LineColor = Color
EndSelect
If \Redraw
DrawGadget(*this)
EndIf
EndWith
EndProcedure : AsMethode(SetColor)
Procedure GetColor(*this.sSwitchGadget, ColorType)
Protected color
With *this
Select ColorType
Case #PB_Gadget_FrontColor
color = \FrontColor
Case #PB_Gadget_BackColor
color = \BackColor
Case #PB_Gadget_LineColor
color = \LineColor
EndSelect
ProcedureReturn color
EndWith
EndProcedure : AsMethode(GetColor)
; ----
Procedure SetState(*this.sSwitchGadget, State)
With *this
\State = State
If \Redraw
DrawGadget(*this)
EndIf
EndWith
EndProcedure : AsMethode(SetState)
Procedure GetState(*this.sSwitchGadget)
With *this
ProcedureReturn \State
EndWith
EndProcedure : AsMethode(GetState)
; ----
Procedure SetUserData(*this.sSwitchGadget, UserData)
With *this
\UserData = UserData
EndWith
EndProcedure : AsMethode(SetUserData)
Procedure GetUserData(*this.sSwitchGadget)
With *this
ProcedureReturn \UserData
EndWith
EndProcedure : AsMethode(GetUserData)
;-- Drawing and Events Functions
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Procedure NSColorByNameToRGB(NSColorName.s)
Protected.cgfloat red, green, blue
Protected nscolorspace, rgb
nscolorspace = CocoaMessage(0, CocoaMessage(0, 0, "NSColor " + NSColorName), "colorUsingColorSpaceName:$", @"NSCalibratedRGBColorSpace")
If nscolorspace
CocoaMessage(@red, nscolorspace, "redComponent")
CocoaMessage(@green, nscolorspace, "greenComponent")
CocoaMessage(@blue, nscolorspace, "blueComponent")
rgb = RGB(red * 255.0, green * 255.0, blue * 255.0)
ProcedureReturn rgb
EndIf
EndProcedure
CompilerEndIf
Procedure.i BlendColor(Color1.i, Color2.i, Scale.i = 50) ; Thanks to Thorsten
Protected.f R1, G1, B1, R2, G2, B2
Protected.f Blend1 = Scale / 100
Protected.f Blend2 = 1.0 - Blend1
R1 = Red(Color1): G1 = Green(Color1): B1 = Blue(Color1)
R2 = Red(Color2): G2 = Green(Color2): B2 = Blue(Color2)
ProcedureReturn RGB(R1*Blend1 + R2 * Blend2, G1*Blend1 + G2 * Blend2, B1*Blend1 + B2 * Blend2)
EndProcedure
Procedure GetSystemColor(Type)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Select Type
Case #PB_Gadget_FrontColor
ProcedureReturn GetSysColor_(#COLOR_WINDOWTEXT)
Case #PB_Gadget_BackColor
ProcedureReturn GetSysColor_(#COLOR_3DFACE)
Case #PB_Gadget_LineColor
ProcedureReturn #Gray
EndSelect
CompilerCase #PB_OS_Linux
Select Type
Case #PB_Gadget_FrontColor
ProcedureReturn #Black
Case #PB_Gadget_BackColor
ProcedureReturn $F8F8F8
Case #PB_Gadget_LineColor
ProcedureReturn #Gray
EndSelect
CompilerCase #PB_OS_MacOS
Protected UserDefaults, NSString
Select Type
Case #PB_Gadget_FrontColor
ProcedureReturn NSColorByNameToRGB("controlTextColor")
Case #PB_Gadget_BackColor
UserDefaults = CocoaMessage(0, 0, "NSUserDefaults standardUserDefaults")
NSString = CocoaMessage(0, UserDefaults, "stringForKey:$", @"AppleInterfaceStyle")
If NSString And PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8) = "Dark"
ProcedureReturn BlendColor(NSColorByNameToRGB("controlBackgroundColor"), #White, 85)
Else
ProcedureReturn BlendColor(NSColorByNameToRGB("windowBackgroundColor"), #White, 85)
EndIf
Case #PB_Gadget_LineColor
ProcedureReturn #Gray
EndSelect
CompilerEndSelect
EndProcedure
; ----
Procedure DrawGadget(*this.sSwitchGadget)
Protected x, y, dx, dy, radius
Protected LineColor, FrontColor, BackColor, BlendColor
With *this
Select \Event
Case #PB_EventType_LeftButtonUp
If \State
\State = #False
Else
\State = #True
EndIf
\Event = 0
PostEvent(#PB_Event_Gadget, GetActiveWindow(), \Gadget, #PB_EventType_Change, \State)
EndSelect
If StartDrawing(CanvasOutput(\Gadget))
LineColor = \LineColor
FrontColor = \FrontColor
BackColor = \BackColor
If FrontColor = #PB_Default
FrontColor = $FF5050
EndIf
If LineColor = #PB_Default
LineColor = #Gray
EndIf
dx = DesktopScaledX(\Width)
dy = DesktopScaledY(\Height)
; Hintergrund
Box(0, 0, dx, dy, BackColor)
; Schalter
If dx > dy
If dy & 1 = 0
dy - 1
EndIf
; Draw Border
If \Flags = #PB_Canvas_Border
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, dx, dy, #Gray)
x = 2
y = 2
dx - 4
dy - 4
EndIf
radius = (dy - 2) / 2
If Not \State
DrawingMode(#PB_2DDrawing_Default)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dy - 4) / 2, (dy - 4) / 2, BlendColor(backColor, LineColor, 70))
DrawingMode(#PB_2DDrawing_Outlined)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dy - 4) / 2, (dy - 4) / 2, LineColor)
DrawingMode(#PB_2DDrawing_Default)
Circle(x + radius + 1, x + radius + 1, radius, LineColor)
Else
DrawingMode(#PB_2DDrawing_Default)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dy - 4) / 2, (dy - 4) / 2, BlendColor(backColor, FrontColor, 70))
DrawingMode(#PB_2DDrawing_Outlined)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dy - 4) / 2, (dy - 4) / 2, FrontColor)
DrawingMode(#PB_2DDrawing_Default)
Circle(x + dx - radius - 2, y + radius + 1, radius, FrontColor)
EndIf
Else
If dx & 1 = 0
dx - 1
EndIf
; Draw Border
If \Flags = #PB_Canvas_Border
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, dx, dy, #Gray)
x = 2
y = 2
dx - 4
dy - 4
EndIf
radius = (dx - 2) / 2
If Not \State
DrawingMode(#PB_2DDrawing_Default)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dx - 4) / 2, (dx - 4) / 2, BlendColor(BackColor, LineColor, 70))
DrawingMode(#PB_2DDrawing_Outlined)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dx - 4) / 2, (dx - 4) / 2, LineColor)
DrawingMode(#PB_2DDrawing_Default)
Circle(x + radius + 1, y + dy - radius - 2, radius, LineColor)
Else
DrawingMode(#PB_2DDrawing_Default)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dx - 4) / 2, (dx - 4) / 2, BlendColor(BackColor, FrontColor, 70))
DrawingMode(#PB_2DDrawing_Outlined)
RoundBox(x + 2, y + 2, dx - 4, dy - 4, (dx - 4) / 2, (dx - 4) / 2, FrontColor)
DrawingMode(#PB_2DDrawing_Default)
Circle(x + radius + 1, y + radius + 1, radius, FrontColor)
EndIf
EndIf
StopDrawing()
EndIf
EndWith
EndProcedure
; ----
Procedure DoEvents()
Protected *this.sSwitchGadget = GetGadgetData(EventGadget())
Protected event, update
With *this
If *this
event = EventType()
Select event
Case #PB_EventType_MouseEnter
Case #PB_EventType_MouseLeave
Case #PB_EventType_MouseMove
Case #PB_EventType_MouseWheel
Case #PB_EventType_LeftButtonDown : update = #True
Case #PB_EventType_LeftButtonUp : update = #True
Case #PB_EventType_LeftClick
Case #PB_EventType_LeftDoubleClick
Case #PB_EventType_RightButtonDown
Case #PB_EventType_RightButtonUp
Case #PB_EventType_RightClick
Case #PB_EventType_RightDoubleClick
Case #PB_EventType_MiddleButtonDown
Case #PB_EventType_MiddleButtonUp
Case #PB_EventType_Focus
Case #PB_EventType_LostFocus
Case #PB_EventType_KeyDown
Case #PB_EventType_KeyUp
Case #PB_EventType_Input
Case #PB_EventType_Resize : update = #True
\x = GadgetX(\Gadget)
\y = GadgetY(\Gadget)
\Width = GadgetWidth(\Gadget)
\Height = GadgetHeight(\Gadget)
EndSelect
If update
\Event = event
DrawGadget(*this)
EndIf
EndIf
EndWith
EndProcedure
;-- Object Functions
Procedure Initialize(*this.sSwitchGadget)
Protected result
With *this
result = CanvasGadget(\Gadget, \x, \y, \Width, \Height)
If result
If \Gadget = #PB_Any
\Gadget = result
EndIf
\Redraw = #True
\Event = #Null
\BackColor = GetSystemColor(#PB_Gadget_BackColor)
DrawGadget(*this)
SetGadgetData(\Gadget, *this)
BindGadgetEvent(\Gadget, @DoEvents())
EndIf
EndWith
EndProcedure : AsInitializeObject(Initialize)
; ----
Procedure Dispose(*this.sSwitchGadget)
With *this
If IsGadget(\Gadget)
FreeGadget(\Gadget)
EndIf
EndWith
EndProcedure : AsDisposeObject(Dispose)
; ----
Procedure Create(Gadget, x, y, Width, Height, FrontColor = #PB_Default, LineColor = #PB_Default, Flags = 0)
Protected *object.sSwitchGadget
With *object
AllocateObject(*object, sSwitchGadget)
If *object
\Gadget = Gadget
\x = x
\y = y
\Width = Width
\Height = Height
\FrontColor = FrontColor
\LineColor = LineColor
\Flags = Flags
EndIf
InitializeObject(*object)
ProcedureReturn *object
EndWith
EndProcedure
; ----
CheckInterface()
EndModule
; *****************************************************************************
;- Example
CompilerIf #PB_Compiler_IsMainFile
Enumeration Windows
#Main
EndEnumeration
Enumeration Gadgets 10
#Button10
#Button11
#Button12
EndEnumeration
Enumeration Status
#MainStatusBar
EndEnumeration
Procedure Main()
Protected.SwitchGadget::iSwitchGadget btn10, btn11, btn12
Protected i, state, color
Dim btn.SwitchGadget::iSwitchGadget(9)
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 480, 320, "Object SwitchGadget Number 42 ;)", #PB_Window_SystemMenu)
FrameGadget(#PB_Any, 5, 5, 470, 75, "Switches")
TextGadget(#PB_Any, 15, 25, 35, 20, "On")
TextGadget(#PB_Any, 15, 50, 35, 20, "Off")
For i = 0 To 9
btn(i) = SwitchGadget::Create(i, i * 30 + 50, 25, 24, 40, #Blue, #Gray, #PB_Canvas_Border)
Next
TextGadget(#PB_Any, 10, 94, 120, 20, "FrontColor Green")
TextGadget(#PB_Any, 10, 129, 120, 20, "LineColor Red")
btn10 = SwitchGadget::Create(#Button10, 140, 95, 40, 20)
btn11 = SwitchGadget::Create(#Button11, 140, 125, 40, 20)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 0 To 9
Case #Button10
If EventType() = #PB_EventType_Change
If btn10\GetState()
color = $008D00 ; #Green
Else
color = #PB_Default
EndIf
For i = 0 To 9
btn(i)\SetColor(#PB_Gadget_FrontColor, color)
Next
EndIf
Case #Button11
If EventType() = #PB_EventType_Change
If EventData() ; State
color = #Red
Else
color = #Gray
EndIf
For i = 0 To 9
btn(i)\SetColor(#PB_Gadget_LineColor, Color)
Next
EndIf
EndSelect
EndSelect
ForEver
For i = 0 To 9
btn(i)\Release()
Next
EndIf
EndProcedure : Main()
CompilerEndIf