StringGadget Questions

Windows specific forum
Large
User
User
Posts: 56
Joined: Tue Apr 29, 2003 8:24 pm

StringGadget Questions

Post by Large »

I'm sure these are easy ones to answer.

1. If I have some text in a multiple line string gadget and I use the command setgadgettext it overwrites the content already in the string gadget, is their a way to append text to a string gadget or insert text into a string gadget at the cursor position ?

2. Is their a way of reading highlighted text in a multiple line string gadget, I'm also testing another language which has feature that does this, its called 'get chunk', is their a way to read highlighted text in Pure basic.

Any help would be greatly appreciated. :D

Kind regards
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

To answer part of your first question, read the gadget's text and append as you write to it. Something like...

SetGadgetText(#Gadget, GetGadgetText(#Gadget)+newtext)
Large
User
User
Posts: 56
Joined: Tue Apr 29, 2003 8:24 pm

Thanks but . . .

Post by Large »

dmoc that works great, all I need to know now is how to insert text at the cursor position.

I'm making an HTML editor which has a insert menu, you can select insert bold tags and it inserts <b></b> but at the moment it adds it to the end of the string gadget, I need it to insert it at the mouse position.

Kind regards
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Guess this does the trick...

Code: Select all

; PureBasic Visual Designer v3.70a Beta


;- Window Constants
;
#Window_0 = 0

;- Gadget Constants
;
#Gadget_1 = 0
#original = 1
#start = 2


Procedure Open_Window_0()
  If OpenWindow(#Window_0, 402, 160, 293, 79,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "New window ( 0 )")
    If CreateGadgetList(WindowID())
      TextGadget(#Gadget_1, 10, 10, 100, 30, "Enter String and select a piece:")
      StringGadget(#original, 110, 10, 170, 20, "")
      ButtonGadget(#start, 110, 40, 70, 20, "make bold")
      
    EndIf
  EndIf
EndProcedure


Open_Window_0()

Repeat
  Event = WaitWindowEvent()
  
  If event = #PB_EventGadget And EventGadgetID() = #start
  
    original.s = GetGadgetText(#original) ; get strings from Gadget
    new.s = ""
    
    ; get selected position from #original Gadget
    SendMessage_(GadgetID(#original), #EM_GETSEL, @startpos.l, @endpos.l)
    
    new = Left(original, startpos)  ; unselected part on the left
    new + "<B>"    ; bold mark
    new + Mid(original, startpos+1, endpos-startpos) ; marked text
    new + "</B>"   ; unbold mark
    new + Right(original, Len(original)-endpos) ; unselected part on the right
    
    SetGadgetText(#original, new) ; set new text


    ; set selection again (3 characters right of original position, because we
    ; added the <b>
    SendMessage_(GadgetID(#original), #EM_SETSEL, startpos+3, endpos+3) 
    ActivateGadget(#original)  ; activate Gadget again
  
  
  EndIf
  
Until Event = #PB_EventCloseWindow
End
Timo
quidquid Latine dictum sit altum videtur
Large
User
User
Posts: 56
Joined: Tue Apr 29, 2003 8:24 pm

Thanks for your reply but . . .

Post by Large »

Thanks for your reply freak but . . .

I sat down and learnt how to use the richedit library, it doe's just what I want in two lines !!! :lol:

Kind regards
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Thanks for your reply but . . .

Post by Dude »

Freak's tip updated for the latest PureBasic (v5.61):

Code: Select all

#Window_0 = 0

;- Gadget Constants
;
#Gadget_1 = 0
#original = 1
#start = 2


Procedure Open_Window_0()
  If OpenWindow(#Window_0, 402, 160, 293, 79, "New window ( 0 )", #PB_Window_SystemMenu)
    TextGadget(#Gadget_1, 10, 10, 100, 30, "Enter String and select a piece:")
    StringGadget(#original, 110, 10, 170, 20, "this is a little test")
    ButtonGadget(#start, 110, 40, 70, 20, "make bold")
  EndIf
EndProcedure


Open_Window_0()

Repeat
  Event = WaitWindowEvent()
  
  If event = #PB_Event_Gadget And EventGadget() = #start
    
    original.s = GetGadgetText(#original) ; get strings from Gadget
    new.s = ""
    
    ; get selected position from #original Gadget
    SendMessage_(GadgetID(#original), #EM_GETSEL, @startpos.l, @endpos.l)
    
    new = Left(original, startpos)  ; unselected part on the left
    new + "<B>"                     ; bold mark
    new + Mid(original, startpos+1, endpos-startpos) ; marked text
    new + "</B>"                                     ; unbold mark
    new + Right(original, Len(original)-endpos)      ; unselected part on the right
    
    SetGadgetText(#original, new) ; set new text
    
    
    ; set selection again (3 characters right of original position, because we
    ; added the <b>
    SendMessage_(GadgetID(#original), #EM_SETSEL, startpos+3, endpos+3)
    SetActiveGadget(#original)  ; activate Gadget again
    
  EndIf
  
Until Event = #PB_Event_CloseWindow
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re:

Post by Dude »

BTW, wasn't there a quicker way to append text to the end of a StringGadget without doing this:

Code: Select all

SetGadgetText(#Gadget, GetGadgetText(#Gadget)+newtext)
I'm sure I once did something with an API call to add text to the end, without reading the entire existing contents first, but I can't find anything in my sources now. I'm 99% sure it was something like SendMessage with #ES_SETTEXT or such. Anyone ever done it?
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: Thanks for your reply but . . .

Post by MarcosPC »

Dude wrote:Freak's tip updated for the latest PureBasic (v5.61):
Error on line 30:

SendMessage_() is not a function, array, list, map or macro.


How can I arrange this?
My purebasic is version demo 5.51.
Windows 7 64

Right now, I appreciate any help.

Using google translate.

__________________________________________________
Quote tag repaired
20.11.2018
RSBasic
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Thanks for your reply but . . .

Post by Bisonte »

MarcosPC wrote:My purebasic is version demo 5.51
The use of the Windows-API is not available in the Purebasic demo.
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
MarcosPC
User
User
Posts: 17
Joined: Mon Mar 19, 2018 10:03 pm

Re: StringGadget Questions

Post by MarcosPC »

Ok, thanks!
Post Reply