Inserting code into the Editor

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Code: Select all

OpenWindow(0,0,0,320,240,"",$CA0001)
CreateGadgetList(WindowID(0))
EditorGadget(0,0,0,320,200)
ButtonGadget(1,120,210,80,20,"Add France")
SetGadgetText(0, "The rain in Spain falls mainly on the plain.")

Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          SetActiveGadget(0)
          SendMessage_(GadgetID(0), #EM_SETSEL, 18,18)
          SendMessage_(GadgetID(0), #EM_REPLACESEL, 1, @" and France ")
      EndSelect
  EndSelect
Until ev = #WM_CLOSE
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Beat me by precisely 3.2 seconds! :)
I may look like a mule, but I'm not a complete ass.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Thanks, yes, I tried that, but
My problem: how do I deal with the returns entered into the editor gadget though.

Try typing, press return a few times, time some more and then try entering it on the 4th line or so.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

If I'm at the screen when a question gets posted, I don't lose too many! Some, but not many.

A scintilla gadget like the current PB editor uses will have its own messages you can send, but afaik it's a subclassed richedit control, so the normal messages should work too. I'm not too up on scintilla atm. It's on the todo list for someday as it looks quite interesting.
BERESHEIT
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

http://www.purebasic.fr/english/viewtop ... 6111#25039

Like that code that freak posted, but it doesn't work with editor gadgets with multiple lines, I can't figure out how to edit it to do so.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Sorry, I don't understand the problem. The same method works here to insert text on one of the empty lines after pressing return a few times.
BERESHEIT
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Code: Select all

Heres the code:

; 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, "test", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar)
    If CreateGadgetList(WindowID(#Window_0))
      TextGadget(#Gadget_1, 10, 10, 100, 30, "Enter String and select a piece:")
      EditorGadget(#original, 110, 10, 170, 133)
      ButtonGadget(#start, 10, 40, 70, 20, "make bold")
     
    EndIf
  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
End 
Try typing this in the box:
fdsfdsfjkdjsla


fjdsklfjdsl

dsada
And then put your cursor between s and a on the last line
and try entering the bold stuff
doesn't work right here...

I get:
jkjkl


jkl
<B></B>

hjk
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I'm not sure what you're after, but try this :

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, "test", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar) 
    If CreateGadgetList(WindowID(#Window_0)) 
      TextGadget(#Gadget_1, 10, 10, 100, 30, "Enter String and select a piece:") 
      EditorGadget(#original, 110, 10, 170, 133) 
      ButtonGadget(#start, 10, 40, 70, 20, "make bold") 
      
    EndIf 
  EndIf 
EndProcedure 


Open_Window_0() 

Repeat 
  Event = WaitWindowEvent() 
  
  If event = #PB_Event_Gadget And EventGadget() = #start 
  
    a$=""
    SendMessage_(GadgetID(#original), #EM_GETSEL, @startpos.l, @endpos.l) 
    If endpos > startpos
      a$=Space(endpos-startpos+1)
      SendMessage_(GadgetID(#original), #EM_GETSELTEXT, 0, @a$)
    EndIf
    a$ = "<B>"+a$+"</B>"
    SendMessage_(GadgetID(#original), #EM_REPLACESEL, #True, @a$)
    SetActiveGadget(#original)  ; activate Gadget again 
  EndIf 
  
Until Event = #PB_Event_CloseWindow 
End 
I may look like a mule, but I'm not a complete ass.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Post by yrreti »

Try this code selecting a position with the mouse and pasting.

Code: Select all

OpenWindow(0,0,0,320,240,"",$CA0001)
CreateGadgetList(WindowID(0))
EditorGadget(0,0,0,320,200)
ButtonGadget(1,60,210,220,20,"use mouse to select a place and press this")
SetGadgetText(0, "The rain in Spain falls mainly on the plain."+Chr(10)+"The rain in France falls mainly on the plain.")

Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_Gadget
      
 
    SendMessage_(GadgetID(0), #EM_GETSEL, @startpos.l, @endpos.l)
     
            
      Select EventGadget()
        Case 1
        	a$ = " [I put this here] "
          SetActiveGadget(0)
          SendMessage_(GadgetID(0), #EM_REPLACESEL, #True, @a$)         
      EndSelect
  EndSelect
Until ev = #WM_CLOSE
I still would like to know if it's possible to send code from an external app to the editor if it can be done?
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Thank you srod and yrreti they both worked perfect.

srod, How do you not know what I was after? :P
I was trying to enter text at the location of the cursor in a editor gadget. The code freak posted would not work with multi-line text, the string to be added at the cursor's position was not added there correctly.

Matt

Question:
SendMessage_(GadgetID(0), #EM_GETSEL, @startpos.l, @endpos.l)
what is that line doing in yrreti's code? I suppose nothing?
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Post by yrreti »

Your right, that line is not needed. It was just some code
from another project that I didn't remove and wasn't sure about.
Matt
Enthusiast
Enthusiast
Posts: 447
Joined: Sat May 21, 2005 1:08 am
Location: USA

Post by Matt »

Thanks, exactly what a I needed! :)
Post Reply