Page 1 of 1

Re: StringGadget: Toggle Insert/Overwrite Mode

Posted: Tue Nov 15, 2011 7:25 am
by Danilo
Another thing you may like is a textual cue (tip that is displayed by the edit control to prompt the user for information).
It is displayed in background while there is no text in the StringGadget.

Requires WindowsXP or later and XP skin enabled in compiler options.

Code: Select all

Procedure SetStringGadgetTip(gadget,text$)
    Protected temp$
    CompilerIf #PB_Compiler_Unicode
        tmp$ = text$
    CompilerElse
        Protected len  = Len(text$)
        tmp$ = Space(len*2+2)
        PokeS(@tmp$,text$,len,#PB_Unicode)
    CompilerEndIf
    SendMessage_(GadgetID(gadget),#EM_SETCUEBANNER,#True,@tmp$)
EndProcedure

If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    StringGadget(0, 8,  10, 306, 20, "")
    SetStringGadgetTip(0,"Enter your name")

    StringGadget(1, 8,  35, 306, 20, "", #PB_String_Numeric)
    SetStringGadgetTip(1,"Enter your phone number")

    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: StringGadget: Toggle Insert/Overwrite Mode

Posted: Tue Nov 15, 2011 6:04 pm
by charvista
@Rashad Hi
As usually my thanks for your help. To get the position worked fine. However, I had trouble to force Insert/Overwrite on my String Gadgets. But never mind, I used a simple Toggle variable and it works fine now.

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

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))
    
    _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())
            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
        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
                        CurrentCaretPos=SendMessage_(GadgetID(CurrentGadget),#EM_GETSEL,0,0)>>16
                        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); set 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))
@Danilo
Much appreciated your code! This is how the word "Password" is showed in the input field when we start Windows Vista or 7.
I included it in my example - as an example! :mrgreen:
I wonder if we can use another font for the tip, or another color?

Cheers!

Re: StringGadget: Toggle Insert/Overwrite Mode

Posted: Wed Nov 16, 2011 11:58 am
by Lord
Hi charvista!

There is a little problem in your code.

If you leave your window, the variable Agad gets negative.
So an Array-Index out of bounds Error occurs if you use
_SGad(Agad) on line 70.
Just use

Code: Select all

        If CurrentGadget<>GetActiveGadget()
          Agad=GetGadgetArrayPos(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

Re: StringGadget: Toggle Insert/Overwrite Mode

Posted: Thu Nov 17, 2011 11:01 pm
by charvista
Hi Lord!
You are right! It was indeed a little problem! :mrgreen:
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!)
Enjoy!

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)
Cheers