STC coloring

This is part of a very long project, but the code I am using is essentially a copy of PyShell. Just add this method to wx.py.shell.Shell and try calling it somewhere.

def writeErr(self, error) :
‘’’
This will write the line in red, so that it appears as an error message
‘’’
STC_ERROR = 36
self.StyleSetSpec(STC_ERROR, “fore:RED”)

    #FIXME: this doesn't work.
     p1 =

self.GetCurrentPos()
self.AddText(error)
self.EnsureCaretVisible()
self.StartStyling(p1, 0xFF)
self.SetStyling(len(error) ,Styling.STC_ERROR)

···

----- Original Message ----
From: “Rickey, Kyle W” Kyle.Rickey@bakerhughes.com
To: wxPython-users@lists.wxwidgets.org
Sent: Tuesday, January 29, 2008 10:44:03 AM
Subject: RE: [wxPython-users] STC coloring

Ben, I haven’t tried messing with a lexar, could you post a working
sample of your code?

-Kyle Rickey

From: Ben Kaplan [mailto:bskaplan14@yahoo.com]
Sent: Tuesday, January 29, 2008 9:36 AM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] STC coloring

Well, I tried it, but it doesn’t work quite right. I’m using a lexer to
color the rest of the text. The lexer styles are applied after I set
the styling on the error message. The line flashes red, then goes to the
lexer’s style. Is there any way to have the lexer ignore specific
areas?
----- Original Message ----
From: “Rickey, Kyle W” Kyle.Rickey@bakerhughes.com
To: wxPython-users@lists.wxwidgets.org
Sent: Tuesday, January 29, 2008 10:16:23 AM
Subject: RE: [wxPython-users] STC coloring

I’ve done something similar in my code, I hope this helps.

import wx, wx.stc as STC, sys

class RedirectOutput:
def init(self, frame, console):
self.frame = frame
if console == “out”:#sys.stdout
self.style = 1#black
elif console == “err”:#sys.stderr

self.style = 2#red

def write(self, what):
    ##Do not put a print statement here!!##
    self.frame.log.SetReadOnly(False)
    start = self.frame.log.GetCurrentPos()
    self.frame.log.StartStyling(start, 0xff)
    self.frame.log.AddText(str(what))
    self.frame.log.EnsureCaretVisible()
    self.frame.log.SetStyling(len(what), self.style)
    self.frame.log.SetReadOnly(True)

class Main(wx.Frame):
def init(self):
wx.Frame.init(self, None, -1, title=“Test
Redirect”)
panel = wx.Panel(self, -1)

    self.log = STC.StyledTextCtrl(panel, -1)
    self.log.SetUseHorizontalScrollBar(False)
    self.log.SetMarginWidth(1, 0)
    self.log.SetWrapMode(1)#Turns on word wrap

    self.log.StyleClearAll()
    self.log.StyleSetSpec(1, "fore:BLACK")
    self.log.StyleSetSpec(2, "fore:RED")
   
    sys.stdout = RedirectOutput(self, "out")
    sys.stderr = RedirectOutput(self, "err")
   
     b1 =

wx.Button(panel, -1, “Print Something”)
b2 = wx.Button(panel, -1, “Raise Exception”)

    b1.Bind(wx.EVT_BUTTON, self.OnButton1)
    b2.Bind(wx.EVT_BUTTON, self.OnButton2)
   
    b_sizer = wx.BoxSizer(wx.HORIZONTAL)
    b_sizer.Add(b1)
    b_sizer.Add(b2)
   
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(b_sizer)
    sizer.Add(self.log, 1, wx.EXPAND)
   
    panel.SetSizer(sizer)

self.Center()
self.Show()

def OnButton1(self, event):
    print "this is a test and should be black"
def OnButton2(self, event):
    raise AttributeError

app = wx.App(0)
frame = Main()
app.MainLoop()

-Kyle Rickey


From: Ben Kaplan [mailto:bskaplan14@yahoo.com]
Sent: Tuesday, January 29, 2008 8:52 AM
To: wxPython Users
Subject: [wxPython-users] STC coloring

I’m trying to get an effect similar to the one in this screenshot of
Dr.
Scheme, where the error message is printed in red and the rest of the
text control is styled as usual. I have the rest of it styled
correctly,
but I can’t figure out how to write a message using a style different
from the lexer. Can anyone help me with this?


Never miss a thing. Make Yahoo your homepage.


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


Looking for last minute shopping deals? Find them fast with Yahoo!
Search.


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


Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.