Thank you for taking your time and for the correction.
I have corrected and added an old feature that also originates from Danilo: the ability to change the background color whenever the user changed the input contents.
Here I have added a gradual background color for the password. The longer the password, the better, thus the greener. The formule I used is not ideal for all color combinations (red to green gives black background - if someone has a better formula, please tell us!)
Code: Select all
Global Dim _SGad(2)
Procedure.i zInsState(TGad,InsState)
If InsState=1
Mode.s="OVR ("+Str(InsState)+")"
Else
Mode.s="INS ("+Str(InsState)+")"
EndIf
SetGadgetText(TGad,Mode)
EndProcedure
Procedure zGetGadgetArrayPos(CurrentGadget,Array SGad.i(1))
Ubound=ArraySize(SGad())
GadgetArrayPos=-1
For i=0 To Ubound
If SGad(i)=CurrentGadget
GadgetArrayPos=i
Break
EndIf
Next i
ProcedureReturn GadgetArrayPos
EndProcedure
Procedure SetStringGadgetTip(Gadget,Text$)
Protected temp$
CompilerIf #PB_Compiler_Unicode
tmp$=Text$
CompilerElse
Protected length=Len(Text$)
tmp$=Space(length*2+2)
PokeS(@tmp$,Text$,length,#PB_Unicode)
CompilerEndIf
SendMessage_(GadgetID(Gadget),#EM_SETCUEBANNER,#True,@tmp$)
EndProcedure
Procedure.d zStrength(StartColor,StopColor,MaxStrength,CurrentStrength)
ProcedureReturn StartColor+(StopColor-StartColor)/(MaxStrength-1)*(CurrentStrength-1)
EndProcedure
Font2=LoadFont(#PB_Any,"Courier",14)
CreateImage(0,18,20)
StartDrawing(ImageOutput(0))
Box(0,17,10,3,$FFFFFF)
StopDrawing()
Win=OpenWindow(#PB_Any,0,0,300,200,"Toggle Insert/Overwrite",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
AddKeyboardShortcut(Win, #PB_Shortcut_Insert,1045)
TGad=TextGadget(#PB_Any,200,150,100,20,"")
InsState.b=0; initialize with 0 (insert)
_SGad(0)=StringGadget(#PB_Any,10,10,280,26,"")
SetStringGadgetTip(_SGad(0),"Enter your name")
SetGadgetFont(_SGad(0),FontID(Font2))
_SGad(1)=StringGadget(#PB_Any,10,40,280,26,"",#PB_String_Password)
SetStringGadgetTip(_SGad(1),"Password")
SetGadgetFont(_SGad(1),FontID(Font2))
SendMessage_(GadgetID(_SGad(1)),#EM_SETLIMITTEXT,12,0); limit the input length to 12
; Check length of the password (initial)
If Len(GetGadgetText(_SGad(1)))=0; no password (yet)
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,$FFFFFF); background white
ElseIf Len(GetGadgetText(_SGad(1)))>0 And Len(GetGadgetText(_SGad(1)))<=4
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,$0000FF); background red
ElseIf Len(GetGadgetText(_SGad(1)))>4 And Len(GetGadgetText(_SGad(1)))<9
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,$00FFFF); background yellow
Else
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,$00FF00); background green
EndIf
_SGad(2)=StringGadget(#PB_Any,10,70,280,26,"",#PB_String_Numeric)
SetStringGadgetTip(_SGad(2),"Enter your phone number")
SetGadgetFont(_SGad(2),FontID(Font2))
SetActiveGadget(_SGad(0))
zInsState(TGad,InsState)
Repeat
Event=WaitWindowEvent()
If CurrentGadget<>GetActiveGadget()
Agad=zGetGadgetArrayPos(GetActiveGadget(),_SGad())
If Agad>-1
DestroyCaret_()
CreateCaret_(GadgetID(_SGad(Agad)),0,10,20); insert caret; second parameter: 0=solid block, 1=dotted block
SetCaretBlinkTime_(-1); immobile = -1, normal blinking = 500
ShowCaret_(GadgetID(_SGad(Agad)))
InsState=0
zInsState(TGad,InsState)
EndIf
EndIf
CurrentGadget=GetActiveGadget(); get gadget number
GadgetArrayPos=zGetGadgetArrayPos(CurrentGadget,_SGad()); get array pos based on gadget number
If Event=#PB_Event_Gadget
Select EventGadget()
Case _SGad(0),_SGad(1),_SGad(2)
If EventType()=#PB_EventType_Change
; Check length of the password
If Len(GetGadgetText(_SGad(1)))=0; no password (yet)
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,$FFFFFF); background white
Else
SetGadgetColor(_SGad(1),#PB_Gadget_BackColor,Int(zStrength($00FFFF,$00FF00,12,Len(GetGadgetText(_SGad(1)))))); gradual background
EndIf
; Check caret
CurrentCaretPos=SendMessage_(GadgetID(CurrentGadget),#EM_GETSEL,0,0)>>16; get the current caret position
Select InsState
Case 1; overwrite
V.s=GetGadgetText(_SGad(GadgetArrayPos))
V.s=Left(V.s,CurrentCaretPos)+Mid(V.s,CurrentCaretPos+2)
SetGadgetText(_SGad(GadgetArrayPos),V.s)
SendMessage_(GadgetID(CurrentGadget),#EM_SETSEL,CurrentCaretPos,CurrentCaretPos); put caret back where he was
EndSelect
EndIf
EndSelect
EndIf
If Event=#PB_Event_Menu
Select EventMenu()
Case 1045
If InsState=0
InsState=1
;DestroyCaret_()
CreateCaret_(GadgetID(CurrentGadget),ImageID(0),0,0); overwrite caret
SetCaretBlinkTime_(-1); immobile = -1, normal blinking = 500
ShowCaret_(GadgetID(CurrentGadget))
Else
InsState=0
;DestroyCaret_()
CreateCaret_(GadgetID(CurrentGadget),0,10,20); insert caret; second parameter: 0=solid block, 1=dotted block
SetCaretBlinkTime_(-1); immobile = -1, normal blinking = 500
ShowCaret_(GadgetID(CurrentGadget))
EndIf
zInsState(TGad,InsState)
EndSelect
EndIf
Until Event=#PB_Event_CloseWindow
CloseWindow(Win)