Page 1 of 2

Select text / get selection for StringGadget/EditorGadget

Posted: Sat Oct 01, 2011 12:58 pm
by Danilo
Example using WinAPI:

Code: Select all

  ; Shows possible flags of StringGadget in action...
  If OpenWindow(0, 0, 0, 322, 205, "StringGadget Flags", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 8,  10, 306, 20, "Normal StringGadget...")
    StringGadget(1, 8,  35, 306, 20, "1234567", #PB_String_Numeric)
    StringGadget(2, 8,  60, 306, 20, "Readonly StringGadget", #PB_String_ReadOnly)
    StringGadget(3, 8,  85, 306, 20, "lowercase...", #PB_String_LowerCase)
    StringGadget(4, 8, 110, 306, 20, "uppercase...", #PB_String_UpperCase)
    StringGadget(5, 8, 140, 306, 20, "Borderless StringGadget", #PB_String_BorderLess)
    StringGadget(6, 8, 170, 306, 20, "Password", #PB_String_Password)
    Repeat
        Select WaitWindowEvent()
            Case #PB_Event_CloseWindow
                Break
            Case #PB_Event_Gadget
                If EventType() = #PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
                    SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
                EndIf
         EndSelect
    ForEver
  EndIf
Set and get the selection for both, StringGadget and EditorGadget on all platforms.

Code: Select all

GetGadgetAttribute(#Gadget,#PB_Editor_SelectionStart)
GetGadgetAttribute(#Gadget,#PB_Editor_SelectionEnd)
GetGadgetAttribute(#Gadget,#PB_Editor_SelectedText)

SetGadgetAttribute(#Gadget,#PB_Editor_SelectionStart, startSelectionPosition)
SetGadgetAttribute(#Gadget,#PB_Editor_SelectionEnd, endSelectionPosition)

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Thu Dec 22, 2011 10:13 pm
by pcfreak
+1

Definitely a need feature in my opinion.

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Mon Dec 30, 2013 11:35 pm
by IdeasVacuum
Would be very useful to have this feature - certainly Windows Users expect to:

Select some text; Right mouse -click copy to clipboard.

Windows API:

Code: Select all

Procedure.s GetSelectedEditorText(iEdId.i)
;-----------------------------------------
Protected sSelected.s, Range.CHARRANGE, iSize.i 

               SendMessage_(GadgetID(iEdId), #EM_EXGETSEL, 0, Range)

                      iSize = (Range\cpMax - Range\cpMin)
                  sSelected = Space((iSize * 2) + 1)
          
               SendMessage_(GadgetID(iEdId),#EM_GETSELTEXT, 0, sSelected)

               ProcedureReturn(sSelected)
EndProcedure
Edit: Related to that would be to add/insert text too, à la Windows API:

Code: Select all

SendMessage_(GadgetID(#MyEditor), #WM_PASTE,0,0)

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 31, 2013 12:25 am
by Andre
+1

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 31, 2013 8:12 am
by davido
+1

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 31, 2013 11:28 am
by Shardik
To select text or obtain selected text in a StringGadget you may use this cross-platform solution.

+1 for implementing this feature natively in PB...

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Fri Mar 28, 2014 7:15 pm
by paulr
+1 from me.

Can I also suggest:

Code: Select all

GetGadgetAttribute(#Gadget,#PB_Editor_TextLength)
...to retrieve the number of characters in the text box?

I know this can currently be achieved with Len(GetGadgetText(#Gadget)), but the above would hopefully avoid processing the whole string every time it's called.

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Sun Apr 13, 2014 7:57 am
by Maitre_Kanter
+1 for adding this feature natively in PureBasic

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Mon Dec 07, 2015 11:49 pm
by Topilno
Danilo wrote: Set and get the selection for both, StringGadget and EditorGadget on all platforms.

Code: Select all

GetGadgetAttribute(#Gadget,#PB_Editor_SelectionStart)
GetGadgetAttribute(#Gadget,#PB_Editor_SelectionEnd)
GetGadgetAttribute(#Gadget,#PB_Editor_SelectedText)

SetGadgetAttribute(#Gadget,#PB_Editor_SelectionStart, startSelectionPosition)
SetGadgetAttribute(#Gadget,#PB_Editor_SelectionEnd, endSelectionPosition)
I'm trying this with PB 5.40/5.41b2 (EditorGadget) but it's not working.
Can't find #PB_Editor_SelectionStart and #PB_Editor_SelectionEnd.
Are they still supported, perhaps with a new name, or are they gone?
Thanks

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Mon Dec 07, 2015 11:58 pm
by paulr
This thread is a wish list, this feature doesn't exist yet. We can only hope it will one day!

In the meantime, Shardik's link above is helpful.

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 08, 2015 12:20 am
by Topilno
paulr wrote:This thread is a wish list, this feature doesn't exist yet. We can only hope it will one day!

In the meantime, Shardik's link above is helpful.
Thank you very much. Googled the code, and it looked so real i didn't realize it was from a wishlist.

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 08, 2015 9:25 am
by Torp
+1

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Tue Dec 08, 2015 11:13 am
by Mesa
+1

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Fri Feb 22, 2019 10:16 am
by collectordave
Has this been implemented?

Just been trying the code from shardik and have it working on an editor gadget.

just need to check end of line stuff.

regards

CD

Re: Select text / get selection for StringGadget/EditorGadge

Posted: Sun Sep 22, 2019 3:04 am
by BarryG
+1. Please add them. And thanks, IdeasVacuum, for making it possible in the meantime.

But how do I replace selected text without putting it in the clipboard with #WM_PASTE?

[Edit] Just found it in another post:

Code: Select all

SendMessage_(GadgetID(gadget),#EM_REPLACESEL,0,@text$)
But this doesn't let me press Ctrl+Z to undo the replace, like pasting does. What to do?