Replacing Selected text with button event in a text control

Hi all,

I’m having a bit of trouble with my text ctrl where I want to prepend and append tags (e.g. [bold]text[/bold]) to a selected range of characters/words.

So far I can somewhat come up with the way to do it but the result is different each time :

def onToolbarBold(self, event): # wxGlade: KBEdit.<event_handler>
    #kb_data = self.text_ctrl_editor.GetValue()
    kb_hilight = self.text_ctrl_editor.GetStringSelection()
    kb_start = self.text_ctrl_editor.GetInsertionPoint()
    kb_stop = self.text_ctrl_editor.GetLastPosition()
    kb_start_data = self.text_ctrl_editor.GetString(0, kb_start)
    #data_replace = self.text_ctrl_editor.Replace

(kb_start,kb_stop,"%s" % kb_hilight)
#self.text_ctrl_editor.Remove(kb_start,kb_stop)
#print “%s %s %s” % (kb_start_data,kb_hilight,kb_end_data)
print kb_start_data

    print len(kb_hilight)
    print kb_start
    print kb_stop
    #print kb_stop

So basically you can see the mess im working with trying to get this working but my idea into it is …

  1. get beginning and end point from cursor which I haven’t quite figured out as I just realize GetLastPosition is the end of the string not the selection
  2. Add tags to beginnning and end of selected text using either Replace(…) or printing beginning of data - replacing selected text - then print the rest of the data.

So I guess my biggest problem is trying to figure out how to get both beginning cursor point and end point without chopping off the end of my strings when replacing selected text

any thoughts?

thanks

adam

adam <adam.stokes <at> gmail.com> writes:

Hi all,I'm having a bit of trouble with my text ctrl where I want to
prepend and append tags (e.g. [bold]text[/bold]) to a selected range of
characters/words.

...

   def onToolbarBold(self, event): # wxGlade: KBEdit.<event_handler>
       #kb_data = self.text_ctrl_editor.GetValue()
       kb_hilight = self.text_ctrl_editor.GetStringSelection()
       kb_start = self.text_ctrl_editor.GetInsertionPoint()
       kb_stop = self.text_ctrl_editor.GetLastPosition()
       kb_start_data = self.text_ctrl_editor.GetString(0, kb_start)
       #data_replace = self.text_ctrl_editor.Replace (kb_start,kb_stop,"[b]%s

[/b]" % kb_hilight)

       #self.text_ctrl_editor.Remove(kb_start,kb_stop)
       #print "%s [b]%s[/b] %s" % (kb_start_data,kb_hilight,kb_end_data)
       print kb_start_data
       print len(kb_hilight)
       print kb_start
       print kb_stop
       #print kb_stop

...

So I guess my biggest problem is trying to figure out how to get both
beginning cursor point and end point without chopping off the end of my
strings when replacing selected text

You can use GetSelection() to find the position of the highlighted text. So
your function might look something like this (untested):

def onToolbarBold(self, event):
    kb_hilight = self.text_ctrl_editor.GetStringSelection()
    kb_start, kb_stop = self.text_ctrl_editor.GetSelection()
    self.text_ctrl_editor.Replace(kb_start,kb_stop,"[b]%s[/b]" % kb_hilight)

In your real code, you might need to add a test to determine if any text is
selected before doing the replace -- it depends on what you want to do in this
case.

Brian

You can use GetSelection() to find the position of the highlighted text. So

your function might look something like this (untested):

def onToolbarBold(self, event):
kb_hilight = self.text_ctrl_editor.GetStringSelection()
kb_start, kb_stop = self.text_ctrl_editor.GetSelection()

self.text_ctrl_editor.Replace(kb_start,kb_stop,"[b]%s[/b]" % kb_hilight)

Awesome this worked like a champ thanks a lot Brian

···

In your real code, you might need to add a test to determine if any text is
selected before doing the replace – it depends on what you want to do in this
case.

Brian


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org


Adam Stokes <astokes.org;people.redhat.com/astokes>