Search code examples
c++wxwidgetsscintillawxstyledtextctrl

wxStyledTextCtrl how to style keywords


Having some trouble getting wxStyledTextCtrl to colourise my word listings.

x->m_ctlEdit->SetKeyWords(0,"true false");
x->SetWordChars(wxT("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ._") );
x->StyleSetForeground(wxSTC_HPHP_WORD, wxColour(0x67,0xa6,0xff));

true and false receive no colouring this way, I've used the StyleSetForeground on many of the other definitions and it all works fine, just having trouble with the word lists.

As a second question, How do I seperate colours for different word listings? I'm aware I can set different keywords list with the number identifier, but how do I apply the styles per keyword list since the function doesn't take in an identifier?

Note: Using the HTML/PHP lexer that comes as a default option with wxStyledTextCtrl


Solution

  • For the wxSTC_LEX_HTML html lexer or the wxSTC_LEX_PHPSCRIPT php lexer, you need to specify key word set 4. So for example:

    x->m_ctlEdit->SetKeyWords(4,"true false");
    

    If you're using the html lexer, you can learn this by calling x->m_ctlEdit->DescribeKeyWordSets(); which will return the following list:

    • HTML elements and attributes
    • JavaScript keywords
    • VBScript keywords
    • Python keywords
    • PHP keywords
    • SGML and DTD keywords

    In this case, the 0-based index of the PHP keywords is 4, so this would be the number to pass in to the SetKeyWords method.

    However this way of checking this fails when using the PHP lexer since calling DescribeKeyWordSets will only return "PHP keywords". So you would think you should call SetKeyWords with 0, but in fact you still need to use 4 because the php script lexer is the same as the html lexer. That just seems to be an oddity of Scintilla.

    On an unrelated note, I think the call to SetWordChars is unnecessary. According to the documentation, that is for searching by words and not for keywords.

    As a second question, How do I seperate colours for different word listings?

    That depends on the lexer. For example, the C lexer offers the following keyword sets

    • Primary keywords and identifiers
    • Secondary keywords and identifiers
    • Documentation comment keywords
    • Global classes and typedefs
    • Preprocessor definitions

    which correspond to the lexer states wxSTC_C_WORD, wxSTC_C_WORD2,wxSTC_C_COMMENTDOCKEYWORD, etc.

    Unfortunately, as described above the html lexer only offers 1 keyword set for PHP.