pythonwxpythonscintillasyntaxhighlighter

How do I enable C++ highlighting in the Scintilla control in the wxPython library?


I'm trying to enable C++ highlighting, but nothing is highlighted. My code:

self.m_pawnedit = wx.stc.StyledTextCtrl( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.BORDER_NONE)
self.m_pawnedit.SetUseTabs ( True )
self.m_pawnedit.SetTabWidth ( 4 )
self.m_pawnedit.SetIndent ( 4 )
self.m_pawnedit.SetTabIndents( True )
self.m_pawnedit.SetBackSpaceUnIndents( True )
self.m_pawnedit.SetViewEOL( False )
self.m_pawnedit.SetViewWhiteSpace( False )
self.m_pawnedit.SetMarginWidth( 2, 0 )
self.m_pawnedit.SetIndentationGuides( True )
self.m_pawnedit.SetReadOnly( False );
self.m_pawnedit.SetMarginType ( 1, wx.stc.STC_MARGIN_SYMBOL )
self.m_pawnedit.SetMarginMask ( 1, wx.stc.STC_MASK_FOLDERS )
self.m_pawnedit.SetMarginWidth ( 1, 16)
self.m_pawnedit.SetMarginSensitive( 1, True )
self.m_pawnedit.SetProperty ( "fold", "1" )
self.m_pawnedit.SetFoldFlags ( wx.stc.STC_FOLDFLAG_LINEBEFORE_CONTRACTED | wx.stc.STC_FOLDFLAG_LINEAFTER_CONTRACTED );
self.m_pawnedit.SetMarginType( 0, wx.stc.STC_MARGIN_NUMBER );
self.m_pawnedit.SetMarginWidth( 0, self.m_pawnedit.TextWidth( wx.stc.STC_STYLE_LINENUMBER, "_99999" ) )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDER, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDER, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDER, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPEN, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERSUB, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEREND, wx.stc.STC_MARK_BOXPLUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEREND, wx.BLACK )
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEREND, wx.WHITE )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.stc.STC_MARK_BOXMINUS )
self.m_pawnedit.MarkerSetBackground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.BLACK)
self.m_pawnedit.MarkerSetForeground( wx.stc.STC_MARKNUM_FOLDEROPENMID, wx.WHITE)
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERMIDTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.MarkerDefine( wx.stc.STC_MARKNUM_FOLDERTAIL, wx.stc.STC_MARK_EMPTY )
self.m_pawnedit.SetSelBackground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT ) )
self.m_pawnedit.SetSelForeground( True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT ) )
self.m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
self.m_pawnedit.SetHighlightGuide(1)

I tried to enable C++ highlighting. But it doesn't work. There are no errors when executing the code.


Solution

  • Solution.

    1. Setting the C++ lexer
    m_pawnedit.SetLexer(wx.stc.STC_LEX_CPP)
    
    1. Adding keywords
    kw = ['assert','break','continue','else','exit','for','if','return','try','new','while','public','const','using','int','namespace','enum','case','switch','default','goto','do','true','false','static','operator','char','bool']
     
    m_pawnedit.SetKeyWords(0, " ".join(kw))
    
    1. Setting styles for strings, characters, numbers, etc.
    faces = { 'times': 'Times New Roman',
                  'mono' : 'Consolas',
                  'helv' : 'Consolas',
                  'other': 'Consolas',
                  'size' : 9,
                  'size2': 9,
                 }
     
    #Set the default style
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT,     "face:%(helv)s,size:%(size)d" % faces)
    m_pawnedit.StyleClearAll()
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_DEFAULT,     "face:%(helv)s,size:%(size)d" % faces)
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
    #Specify the style for brackets
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
    m_pawnedit.StyleSetSpec(wx.stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
    #----
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For default words
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For // comments
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_COMMENT, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) #For /*Text*/ comments
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_NUMBER, "fore:#007F7F,size:%(size)d" % faces) #For numbers
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For strings
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) #For symbols
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_WORD, "fore:#0000FF,bold,size:%(size)d" % faces) #For keywords
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_PREPROCESSOR, "fore:#0000FF,bold,size:%(size)d" % faces) #For preprocessor
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_OPERATOR, "bold,size:%(size)d" % faces) #For operators
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) #For identifiers
    m_pawnedit.StyleSetSpec(wx.stc.STC_C_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) #For EOL
    
    1. Coloring
    m_pawnedit.Colourise(0,-1)
    

    All STC constants can be found in the file "stc.pyi" under "Python\Python3x\Lib\site-packages\wx"