StyledTextCtrl - styling *only* the selected text?

248 views
Skip to first unread message

bw_77

unread,
Nov 30, 2009, 2:06:14 AM11/30/09
to wxPython-users
Hi everyone,

I am new to Python and wxPython, and I have been writing a series of
small text editors to help myself learn.

However, I have hit a major road block. I can't figure out a way to
make my toolbar buttons for "bold" and "italicize" apply to *only* the
currently highlighted/selected text in my StyledTextCtrl. I'm even
beginning to wonder if this is even possible.

I've spent the last several days looking at code on the web. I've also
looked at the wxPython demo.

Here is the code that I last tried, but it doesn't do anything:


import wx
import wx.stc as stc
import wx.py.dispatcher as dispatcher

class EditorFrame(wx.Frame):

def __init__(self, parent, id, title):

wx.Frame.__init__(self, parent, id, title, size=(600, 500))

...

self.textCtrl = stc.StyledTextCtrl(self)

self.textCtrl.SetFocus()

self.textCtrl.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:
Courier New, size: 10, fore: #000000")
self.textCtrl.StyleClearAll()
self.textCtrl.StyleSetSpec(1, "face: Courier New, size: 10,
fore: #000000, back: #FFFFFF")
self.textCtrl.StyleSetSpec(2, "face: Courier New, size: 10,
bold, fore: #000000, back: #FFFFFF")

...

def OnBold(self, event):

frm, to = self.textCtrl.GetSelection()

# remove all styling
self.textCtrl.StartStyling(frm, 0xff)
self.textCtrl.SetStyling(len(self.textCtrl.GetSelectedText()),
stc.STC_STYLE_DEFAULT)

# set current selection to bold
self.textCtrl.StartStyling(frm, 0xff)
self.textCtrl.SetStyling(len(self.textCtrl.GetSelectedText()),
2) # style 2 is bold

...

app = wx.App()
EditorFrame(None, -1, 'wxPython Editor')
app.MainLoop()



-bw_77

Robin Dunn

unread,
Nov 30, 2009, 3:01:39 AM11/30/09
to wxpytho...@googlegroups.com
On 11/29/09 11:06 PM, bw_77 wrote:
> Hi everyone,
>
> I am new to Python and wxPython, and I have been writing a series of
> small text editors to help myself learn.
>
> However, I have hit a major road block. I can't figure out a way to
> make my toolbar buttons for "bold" and "italicize" apply to *only* the
> currently highlighted/selected text in my StyledTextCtrl. I'm even
> beginning to wonder if this is even possible.

Since there are a limited number of styles, the STC isn't really well
suited for general text editors. A RichTextCtrl would be better. That
said, to do it with a STC you would need to make a new style that adds
bold to whatever is already there in that range (assuming that there is
only one style run in the selection. If there's more than one then
you'll need to repeat with each affected style.)


--
Robin Dunn
Software Craftsman
http://wxPython.org

bw_77

unread,
Nov 30, 2009, 8:08:39 PM11/30/09
to wxPython-users
Thanks very much for your reply. I've switched over to a
RichTextCtrl, and you are right - they are much better suited for
writing text editors. The only thing that I miss from the
StyledTextCtrl is that really useful stc.FindText() method, which made
it really easy to write the handler for the Find/Replace dialog.

-bw_77
Reply all
Reply to author
Forward
0 new messages