Select text / get selection for StringGadget/EditorGadget

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Select text / get selection for StringGadget/EditorGadge

Post by wombats »

BarryG wrote:

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?
Try changing the 0 to 1.
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: Select text / get selection for StringGadget/EditorGadge

Post by BarryG »

wombats wrote:Try changing the 0 to 1.
Thank you! :D
wombats
Enthusiast
Enthusiast
Posts: 663
Joined: Thu Dec 29, 2011 5:03 pm

Re: Select text / get selection for StringGadget/EditorGadge

Post by wombats »

This is possible across Windows, macOS and GTK with API, but there isn't a way to set the selection (aside from selecting all of it) with QtScript on Qt. We can get the selection, however:

Code: Select all

CompilerIf Subsystem("qt")
  Runtime Procedure OnSelectionChanged()
    Protected txt.s
    txt = QtScript("gadget(0).selectedText")
    SetGadgetText(1, txt)
  EndProcedure  
  
  If OpenWindow(0, 0, 0, 320, 100, "FrameGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0, 10, 10, 300, 25, "hello")
    TextGadget(1, 10, 40, 300, 30, "")
    QtScript(~"gadget(0).selectionChanged.connect(function() { runtime.call(\"OnSelectionChanged()\"); })")    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
CompilerElse
  MessageRequester("", "Qt subsystem required.")
CompilerEndIf
Having the ability to set/get the selection as part of PB would be great.
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Select text / get selection for StringGadget/EditorGadge

Post by Olli »

Also vera gave a code, thanking the root coder of this tip. (thank you vera and omi !)

http://forums.purebasic.com/french/view ... &view=next
BarryG
Addict
Addict
Posts: 3293
Joined: Thu Apr 18, 2019 8:17 am

Re: Select text / get selection for StringGadget/EditorGadge

Post by BarryG »

IdeasVacuum wrote:

Code: Select all

Procedure.s GetSelectedEditorText(iEdId.i)
IdeasVacuum, your code doesn't seem to work anymore. Look at below. Any fix? Note: I'd like this to work with multi-line StringGadgets, too.

(And can Fred please finally add these features to the next release?)

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

OpenWindow(0,200,200,300,50,"test",#PB_Window_SystemMenu)
StringGadget(0,10,10,280,25,"Select one of these words and see Debug output")

Repeat

  ev=WaitWindowEvent()
  
  If ev=#WM_LBUTTONUP
    Debug ">"+GetSelectedEditorText(0)+"<"
  EndIf

Until ev=#PB_Event_CloseWindow
Post Reply