Change Caret and caret Colour for specific gadget

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Change Caret and caret Colour for specific gadget

Post by IdeasVacuum »

I have a StringGadget() whose background colour is a murky mustard. The default caret is almost impossible to see, so I would like to force the caret to be jet black.

My experiments didn't achieve this because Windows decides the caret colour contrast, even if a bitmap is used for the caret. So, the compromise would be to make the caret a 'fat block' instead of a slender line. Not too bad, a fat block is easier to see - but two problems:

1) As soon as the StringGadget() is clicked, the default Windows caret (slender line) is restored. If the String Gadget already contains text, we are back to square one - difficult to see the caret.

2) Caret position via the API is controlled with pixel input rather than character count, so placing the caret at the end of the text is tricky.

At the moment, the solution is to change the background colour of the StringGadget(), but..........

Code: Select all

Enumeration
#Win
#Str
#Car
EndEnumeration

;LoadImage(#Car, "C:\Caret.bmp")

iFlags.i = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
pt.POINT

If OpenWindow(#Win, 0, 0, 300, 100, "Change Caret and Caret Colour?", iFlags)

               StringGadget(#Str, 10, 50, 280, 20, "")
            ;SetGadgetColor(#Str, #PB_Gadget_BackColor, RGB(136,136,064))
              DestroyCaret_()
              ;CreateCaret_(GadgetID(#Str), ImageID(#Car), 8, 16)
               CreateCaret_(GadgetID(#Str), #Null, 10, 16)
               GetCaretPos_(@pt)
               ;pt\x = pt\x + 25
              ;SetCaretPos_(pt\x, pt\y)
         SetCaretBlinkTime_(600)
                 ShowCaret_(GadgetID(#Str))
EndIf

Repeat
Until WaitWindowEvent(1) = #PB_Event_CloseWindow

DestroyCaret_()

End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Change Caret and caret Colour for specific gadget

Post by RASHAD »

Hi

Code: Select all

CreateImage(0, 16, 20)
StartDrawing(ImageOutput(0))
    Box(0,0,12,20,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
StopDrawing()

Font1=LoadFont(#PB_Any,"Courier",14)

    OpenWindow(0,0,0,300,190,"",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

    StringGadget(0,20,20,260,24,"")
    StringGadget(1,20,60,260,25,"")
    SetGadgetFont(1,FontID(Font1))
   
    StringGadget(2,20,100,260,25,"")
    SetGadgetFont(2,FontID(Font1))
   
    StringGadget(3,20,140,260,25,"")
    SetGadgetColor(3,#PB_Gadget_BackColor,RGB(136,136,064))
    SetGadgetColor(3,#PB_Gadget_FrontColor,#Yellow)
    SetGadgetFont(3,FontID(Font1))
       
    Repeat
        Event=WaitWindowEvent()
        If Event=#PB_Event_Gadget
            If EventGadget()=0
                DestroyCaret_()
                CreateCaret_(GadgetID(0),0,1,14)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=1
                DestroyCaret_()
                CreateCaret_(GadgetID(1),1,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=2
                DestroyCaret_()
                CreateCaret_(GadgetID(2),0,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=3
                DestroyCaret_()
                CreateCaret_(GadgetID(3),ImageID(0),0,0)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
        EndIf
    Until Event=#PB_Event_CloseWindow
Edit : Code updated
Edit 2 :Updated again
Last edited by RASHAD on Thu Jan 23, 2014 12:55 am, edited 3 times in total.
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Change Caret and caret Colour for specific gadget

Post by Kwai chang caine »

Nice RASHAD...you have not loose the hand :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change Caret and caret Colour for specific gadget

Post by IdeasVacuum »

That's brilliant Rashad 8)
Just goes to show what can be done, when you know what you are doing!
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Change Caret and caret Colour for specific gadget

Post by ts-soft »

good one, but the destroycaret is missing:

Code: Select all

            If EventGadget()=1
              Select EventType()
                Case  #PB_EventType_Focus
                  CreateCaret_(GadgetID(1),1,10,20)
                  SetCaretBlinkTime_(500) ; immobile = -1, normal blinking  = 500
                  ShowCaret_(GadgetID(1))
                Case #PB_EventType_LostFocus
                  DestroyCaret_()
              EndSelect
            EndIf
greetings Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Change Caret and caret Colour for specific gadget

Post by Kwai chang caine »

Thanks TSSOFT i have add your modification 8)
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change Caret and caret Colour for specific gadget

Post by IdeasVacuum »

....ah, but it doesn't solve the contrast problem. Try the StringGadgets with a back colour of RGB(136,136,064) as per my original (crap) code. The caret is displayed in a pale colour when black would be much better.....
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Change Caret and caret Colour for specific gadget

Post by RASHAD »

Previous post updated
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Change Caret and caret Colour for specific gadget

Post by IdeasVacuum »

Box(0,0,12,20,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
aha ~ thanks Rashad, works perfectly. Not even described on MSDN!
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Change Caret and caret Colour for specific gadget

Post by RASHAD »

Hi
Solved an ugly effect for the Caret at the end of the StringGadget()

Code: Select all

CreateImage(0, 12, 19)
StartDrawing(ImageOutput(0))
    Box(0,0,12,19,(#Yellow ! RGB(136,136,064))) ;The req color xor The back color
StopDrawing()

Font1=LoadFont(#PB_Any,"Courier",14)

    OpenWindow(0,0,0,300,190,"",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

    StringGadget(0,20,20,260,24,"")
    StringGadget(1,20,60,260,25,"")
    SendMessage_(GadgetID(1), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|9 << 16)
    SetGadgetFont(1,FontID(Font1))
   
    StringGadget(2,20,100,260,25,"")
    SendMessage_(GadgetID(2), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|9 << 16)
    SetGadgetFont(2,FontID(Font1))
   
    StringGadget(3,20,140,260,25,"")
    SendMessage_(GadgetID(3), #EM_SETMARGINS, #EC_RIGHTMARGIN, 0|11 << 16)
    SetGadgetColor(3,#PB_Gadget_BackColor,RGB(136,136,064))
    SetGadgetColor(3,#PB_Gadget_FrontColor,#Yellow)
    SetGadgetFont(3,FontID(Font1))
   
    Repeat
        Event=WaitWindowEvent()
        If Event=#PB_Event_Gadget
            If EventGadget()=0
                DestroyCaret_()
                CreateCaret_(GadgetID(0),0,1,14)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=1
                DestroyCaret_()
                CreateCaret_(GadgetID(1),1,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=2
                DestroyCaret_()
                CreateCaret_(GadgetID(2),0,10,20)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
            If EventGadget()=3
                DestroyCaret_()
                CreateCaret_(GadgetID(3),ImageID(0),0,0)
                SetCaretBlinkTime_(500) ; immobile = -1, normal blinking = 500
                ShowCaret_(0)
            EndIf
        EndIf
    Until Event=#PB_Event_CloseWindow
Egypt my love
Post Reply