wxStyledTextCtrl questions

Hi,

I'm looking for a simple sample of highlighting one's language with the
wxStyledTextCtrl. Even if the language syntax is trivial, ie highlight only the
numbers and some directives inside the current document.

One more thing : I saw that the character "§" takes 2 colums when I make my stc
show the current cursor position, is that a bug ?

Thanks a lot.

Robin Dunn <robin <at> alldunn.com> writes:

Basically all that is required is to set the lexer to STC_LEX_CONTAINER,
and then respond to the EVT_STC_STYLENEEDED event where you need to set
the styling info up to the position specified in the event object. I
don't have any good examples of this, but be sure to read the Scintialla
docs to get a better idea of how the underlying code works. Translating
that to wxSTC and Python shouldn't be too hard.

Scintilla Documentation

Hi Robin,

That's what I've done (thanks to the Yellowbrain site, but some features
are missing and it's outdated now), and I don't understood weel the
StartStyling(pos,mask) method
(see http://www.yellowbrain.com/stc/styling.html#start).

In fact, my onStyleNeeded() method calls another one in a thread :

def colorizeThread(self, expression, text):
    for regex,style in expression:
        iterator = regex.finditer(text)
        for match in iterator:
            self.StartStyling(match.start(), 0xff)
            self.SetStyling(match.end() - match.start()+1, style)
    return

each element in 'expression' is a regular expression associated with a given
stc style, 'text' is the content of my stc.

I think the problems comes from my following question : (but maybe
I'm totally wrong from start ??)

>
> One more thing : I saw that the character "§" takes 2 colums when
> I make my stc show the current cursor position, is that a bug ?

The STC stores the document data internally as utf-8 so what you are
seeing as column offsets is actually related to character positions in
this buffer where some characters can occupy more than one byte.

Ok I see, now in the previous method, the match.start() and match.end()
won't give good positions in my current stc if some characters take 2
bytes and that's why I get bad highlighting I suppose, right ?

If so, how can I solve this ?

Thanks again,
Kib.

kib wrote:

One more thing : I saw that the character "§" takes 2 colums when I make my stc show the current cursor position, is that a bug ?

The STC stores the document data internally as utf-8 so what you are seeing as column offsets is actually related to character positions in this buffer where some characters can occupy more than one byte.

Ok I see, now in the previous method, the match.start() and match.end() won't give good positions in my current stc if some characters take 2 bytes and that's why I get bad highlighting I suppose, right ?

If so, how can I solve this ?

The STC class has some helper methods that allow you to get/set the text as UTF-8 strings, so you can either use those to assist you in working with the text, or you can make your code aware of how unicode values are converted to/from UTF-8 strings and react accordingly. Sorry, but I don't have any examples for either of these approaches.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!