wxScintilla - max highlighing

BlitzMax Modules Forums/Brucey's Modules/wxScintilla - max highlighing

Can anyone post an example using wxScintilla for blitzmax highlighting? (maybe the almighty brucey ;)

I looked at the sample for wxScintilla, as far as I can tell it contains no keyword highlighting functionality. (I can barely tell whats going on in the c++ code near the bottom of it aswell)

I've only got as far as creating the skeleton lexer (copied from another one), which should give some basic highlighting.
But... I haven't actually tried it yet :-p

However, I am using highlighting with my SQL app, and it's working great.

Here are some code snippets I'm using to get it working, based on wxCodeGen generating the basic stuff :

The codegen block:
		txtEditor = New wxScintilla.Create(m_panel1, wxID_ANY)

		txtEditor.SetUseTabs( True )
		txtEditor.SetTabWidth( 4 )
		txtEditor.SetIndent( 4 )
		txtEditor.SetTabIndents( True )
		txtEditor.SetBackSpaceUnIndents( True )
		txtEditor.SetViewEOL( False )
		txtEditor.SetViewWhiteSpace( False )
		txtEditor.SetMarginWidth( 2, 0 )
		txtEditor.SetIndentationGuides( False )
		txtEditor.SetMarginWidth( 1, 0 )
		txtEditor.SetMarginType( 0, wxSCI_MARGIN_NUMBER )
		txtEditor.SetMarginWidth( 0, txtEditor.TextWidth( wxSCI_STYLE_LINENUMBER, "_99999" ) )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDER, wxSCI_MARK_BOXPLUS )
		txtEditor.MarkerSetBackground( wxSCI_MARKNUM_FOLDER, New wxColour.CreateNamedColour( "BLACK" ) )
		txtEditor.MarkerSetForeground( wxSCI_MARKNUM_FOLDER, New wxColour.CreateNamedColour( "WHITE" ) )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDEROPEN, wxSCI_MARK_BOXMINUS )
		txtEditor.MarkerSetBackground( wxSCI_MARKNUM_FOLDEROPEN, New wxColour.CreateNamedColour( "BLACK" ) )
		txtEditor.MarkerSetForeground( wxSCI_MARKNUM_FOLDEROPEN, New wxColour.CreateNamedColour( "WHITE" ) )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDERSUB, wxSCI_MARK_EMPTY )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDEREND, wxSCI_MARK_BOXPLUS )
		txtEditor.MarkerSetBackground( wxSCI_MARKNUM_FOLDEREND, New wxColour.CreateNamedColour( "BLACK" ) )
		txtEditor.MarkerSetForeground( wxSCI_MARKNUM_FOLDEREND, New wxColour.CreateNamedColour( "WHITE" ) )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDEROPENMID, wxSCI_MARK_BOXMINUS )
		txtEditor.MarkerSetBackground( wxSCI_MARKNUM_FOLDEROPENMID, New wxColour.CreateNamedColour( "BLACK" ) )
		txtEditor.MarkerSetForeground( wxSCI_MARKNUM_FOLDEROPENMID, New wxColour.CreateNamedColour( "WHITE" ) )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDERMIDTAIL, wxSCI_MARK_EMPTY )
		txtEditor.MarkerDefine( wxSCI_MARKNUM_FOLDERTAIL, wxSCI_MARK_EMPTY )
		txtEditor.SetSelBackground( True, wxSystemSettings.GetColour( wxSYS_COLOUR_HIGHLIGHT ) )
		txtEditor.SetSelForeground( True, wxSystemSettings.GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) )


Keywords (a loooong string) :
Const SQL_KEYWORDS:String = "absolute action add admin after aggregate " + ..
	"alias all allocate alter and any are array as asc " + ..
.. etc


Some implementation :
		Local font:wxFont = New wxFont.CreateWithAttribs(10, wxTELETYPE, wxNORMAL, wxNORMAL)
		
		txtEditor.SetLexer(wxSCI_LEX_SQL)
		txtEditor.SetKeywords(0, SQL_KEYWORDS)

		txtEditor.StyleSetFontFont(wxSCI_STYLE_DEFAULT, font)

		txtEditor.StyleClearAll()

		txtEditor.StyleSetForeground( wxSCI_SQL_COMMENT, New wxColour.Create(0, 200, 0))
		txtEditor.StyleSetForeground( wxSCI_SQL_COMMENTLINE, New wxColour.Create(0, 200, 0))
		txtEditor.StyleSetForeground( wxSCI_SQL_COMMENTDOC, New wxColour.Create(0, 200, 0))
		txtEditor.StyleSetForeground( wxSCI_SQL_NUMBER, New wxColour.Create(180, 0, 180))
		
		txtEditor.StyleSetForeground( wxSCI_SQL_WORD, New wxColour.Create(0, 0, 200))
		'txtEditor.StyleSetBold(wxSCI_SQL_WORD, True)
		
		txtEditor.StyleSetForeground( wxSCI_SQL_STRING, New wxColour.Create(180, 0, 0))


These two lines :
txtEditor.SetLexer(wxSCI_LEX_SQL)
txtEditor.SetKeywords(0, SQL_KEYWORDS)

are important. They tell wxScintilla what lexer to use, and give it the keywords to colour.

That's about as far as I've gone with it to date. It's a big, complicated beast, extremely powerful, but with lots to get your head around.

The developers say you should look at the SciTE source, which they say is a reference implementation of Scintilla. Which is all very well if you can work out what's going on!

Perhaps we can all work it out together ;-)

Here's what I've come up with:
SuperStrict

Framework wx.wxApp
Import wx.wxScintilla
Import wx.wxFrame
Import wx.wxSystemSettings

New MyApp.Run()

Type MyApp Extends wxApp

	Field frame:AppFrame
	
		Method OnInit:Int()
		
			wxInitAllImageHandlers() 
			
			frame = AppFrame(New AppFrame.Create(,, "wxScintilla Sample")) 
			
			frame.layout() 
			frame.Show(True)
			SetTopWindow(frame)
			
		   Return True
		   
		End Method
		
End Type


Type AppFrame Extends wxFrame

	Field mEdit:wxScintilla
	
		Method OnInit() 
			
			mEdit = Edit(New Edit.Create(Self, - 1)) 
			mEdit.SetFocus()
			
		End Method
	
End Type

Type Edit Extends wxScintilla

		Method OnInit() 
			SetUseTabs(True) 
			SetTabWidth(4) 
			SetIndent(4) 
			SetTabIndents(True) 
			SetBackSpaceUnIndents(True) 
			SetViewEOL(False) 
			SetViewWhiteSpace(False) 
			SetMarginWidth(2, 0) 
			SetIndentationGuides(False) 
			SetMarginWidth(1, 0) 
			SetMarginType(0, wxSCI_MARGIN_NUMBER) 
			SetMarginWidth(0, TextWidth(wxSCI_STYLE_LINENUMBER, "_99999")) 
			
			MarkerDefine(wxSCI_MARKNUM_FOLDER, wxSCI_MARK_BOXPLUS) 
			MarkerSetBackground(wxSCI_MARKNUM_FOLDER, New wxColour.CreateNamedColour("BLACK")) 
			MarkerSetForeground(wxSCI_MARKNUM_FOLDER, New wxColour.CreateNamedColour("WHITE")) 
			
			MarkerDefine(wxSCI_MARKNUM_FOLDEROPEN, wxSCI_MARK_BOXMINUS) 
			MarkerSetBackground(wxSCI_MARKNUM_FOLDEROPEN, New wxColour.CreateNamedColour("BLACK")) 
			MarkerSetForeground(wxSCI_MARKNUM_FOLDEROPEN, New wxColour.CreateNamedColour("WHITE")) 
			
			MarkerDefine(wxSCI_MARKNUM_FOLDERSUB, wxSCI_MARK_EMPTY) 
			MarkerDefine(wxSCI_MARKNUM_FOLDEREND, wxSCI_MARK_BOXPLUS) 
			MarkerSetBackground(wxSCI_MARKNUM_FOLDEREND, New wxColour.CreateNamedColour("BLACK")) 
			MarkerSetForeground(wxSCI_MARKNUM_FOLDEREND, New wxColour.CreateNamedColour("WHITE")) 
			
			MarkerDefine(wxSCI_MARKNUM_FOLDEROPENMID, wxSCI_MARK_BOXMINUS) 
			MarkerSetBackground(wxSCI_MARKNUM_FOLDEROPENMID, New wxColour.CreateNamedColour("BLACK")) 
			MarkerSetForeground(wxSCI_MARKNUM_FOLDEROPENMID, New wxColour.CreateNamedColour("WHITE")) 
			 
			MarkerDefine(wxSCI_MARKNUM_FOLDERMIDTAIL, wxSCI_MARK_EMPTY) 
			MarkerDefine(wxSCI_MARKNUM_FOLDERTAIL, wxSCI_MARK_EMPTY) 
			
		  Local FONT:wxFont = New wxFont.CreateWithAttribs(10, wxTELETYPE, wxNORMAL, wxNORMAL) 
			' Brucey, can you get lexmax compiled into wxscintilla, and setup?
			SetLexer(wxSCI_LEX_BLITZBASIC) 
			SetKeyWords(0, BB_KEYWORDS) 
	
			StyleSetFontFont(wxSCI_STYLE_DEFAULT, FONT) 
	
			StyleClearAll() 
			
			StyleSetForeground(wxSCI_STYLE_DEFAULT, New wxColour.Create(27, 41, 65)) 
			StyleSetBackground(wxSCI_STYLE_DEFAULT, New wxColour.CreateNamedColour("BLACK")) 
			 
			' There are no specific blitzbasic consts so I set them to the vb/powerbasic set
			StyleSetForeground(wxSCI_B_DEFAULT, New wxColour.Create(27, 41, 65)) 
			StyleSetBackground(wxSCI_B_DEFAULT, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_COMMENT, New wxColour.Create(14, 78, 43)) 
			StyleSetBackground(wxSCI_B_COMMENT, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_NUMBER, New wxColour.Create(51, 89, 153)) 
			StyleSetBackground(wxSCI_B_NUMBER, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_KEYWORD, New wxColour.Create(0, 0, 255)) 
			StyleSetBackground(wxSCI_B_KEYWORD, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_IDENTIFIER, New wxColour.Create(27, 41, 65)) 
			StyleSetBackground(wxSCI_B_IDENTIFIER, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_STRING, New wxColour.Create(84, 22, 23)) 
			StyleSetBackground(wxSCI_B_STRING, New wxColour.CreateNamedColour("BLACK")) 
			 
			StyleSetForeground(wxSCI_B_OPERATOR, New wxColour.Create(49, 58, 89)) 
			StyleSetBackground(wxSCI_B_OPERATOR, New wxColour.CreateNamedColour("BLACK")) 
			
			StyleSetForeground(wxSCI_B_LABEL, New wxColour.Create(27, 41, 65)) 
			StyleSetBackground(wxSCI_B_LABEL, New wxColour.CreateNamedColour("BLACK")) 
			
			'UsePopUp(0)
			'SetLayoutCache(wxSCI_CACHE_PAGE)
			'SetBufferedDraw(1) 
	
		End Method
		
End Type

Const BB_KEYWORDS:String = "print input for next type endtype function endfunction method endmethod end"

Pardon the text colors, thats how I have them in BLIde.
Theres only a few things wrong with this, its using a blitzbasic lexer (comments are ; instead of ' etc) instead of a blitzmax lexer (I couldn't find it in wxscintilla source, though I did notice LexMax.cxx), and I was unable to find the style const for the caret position so its invisible (black) with the background.

its using a blitzbasic lexer

It's not really. I *borrowed* the Id for SCLEX_BLITZBASIC to use with my hacked lexer, which is still essentially the original basic lexer.

I've committed the comment character fix.

For the caret, how's about :
SetCaretForeground(New wxColour.CreateNamedColour("WHITE"))


LexMax.cxx is the file that will need changing for lexing properly. I imagine it will need quite a bit of work to get it just right.