; the orginal source by MrMat, Sat Feb 11, 2006 ; http://www.purebasic.fr/english/viewtopic.php?t=19418&highlight=scintilla If InitScintilla("Scintilla.dll")=0 MessageRequester("Error","No Scintilla.dll",#MB_ICONSTOP) End EndIf XIncludeFile "SciLexer2.pbi" ;Modified to see if PB does this directly Enumeration #LexerState_Space #LexerState_Comment #LexerState_Keyword #LexerState_FoldKeyword EndEnumeration Procedure LoadFile() filename.s = OpenFileRequester("Open a file...", "", "PureBasic (*.pb)|*.pb|All files (*.*)|*.*", 0) If filename <> "" file.l = ReadFile(#PB_Any, filename) If file <> 0 len.l = Lof(file) *mem = AllocateMemory(len) If *mem ReadData(file, *mem, len) SCI_SetText(0, *mem) FreeMemory(*mem) EndIf CloseFile(file) EndIf EndIf EndProcedure Procedure SaveFile() filename.s = SaveFileRequester("Save a file...", "", "PureBasic (*.pb)|*.pb|All files (*.*)|*.*", 0) If filename <> "" file.l = CreateFile(#PB_Any, filename) If file <> 0 len.l = SCI_GetLength(0) + 1 *mem = AllocateMemory(len) If *mem SCI_GetText(0, len, *mem) WriteData(file, *mem, len) FreeMemory(*mem) EndIf CloseFile(file) EndIf EndIf EndProcedure Procedure FindText() text.s = InputRequester("Scintilla test", "Enter text to search for", "") If text <> "" current.l = SCI_GetCurrentPos(0) SCI_SetAnchor(0, current) SCI_SearchAnchor(0) start.l = SCI_SearchNext(0, 0, @text) If Pos <> -1 Anchor.l = SCI_GetAnchor(0) SCI_SetSel(0, start, Anchor) EndIf EndIf SetActiveGadget(0) EndProcedure Procedure GotoLine() line.s = InputRequester("Scintilla test", "Enter line to go to", "1") lineno.l = Val(line) If lineno > 0 SCI_GotoLine(0, lineno - 1) EndIf SetActiveGadget(0) EndProcedure Procedure ToggleCurrentFold() Pos.l = SCI_GetCurrentPos(0) line.l = SCI_LineFromPosition(0, Pos) SCI_ToggleFold(0, line) EndProcedure Procedure Highlight(startpos.l, endpos.l) If startpos = -1 endstyled.l = SCI_GetEndStyled(0) linenumber.l = SCI_LineFromPosition(0, endstyled) Else linenumber.l = SCI_LineFromPosition(0, startpos) EndIf If linenumber = 0 Level = #SC_FOLDLEVELBASE Else linenumber - 1 Level = SCI_GetFoldLevel(0, linenumber) & ~ #SC_FOLDLEVELHEADERFLAG EndIf thislevel.l = Level nextlevel.l = Level CurrentPos.l = SCI_PositionFromLine(0, linenumber) SCI_StartStyling(0, CurrentPos, $1F | #INDICS_MASK) state = #LexerState_Space startkeyword = CurrentPos keyword.s = "" While CurrentPos <= endpos oldstate = state Char.l = SCI_GetCharat(0, CurrentPos) If Char = ';' state = #LexerState_Comment ElseIf Char = 10 Or Char = 13 state = #LexerState_Space ElseIf state <> #LexerState_Comment If Char = 9 Or Char = ' ' Or Char = '.' state = #LexerState_Space Else state = #LexerState_Keyword keyword + Chr(Char) EndIf EndIf If oldstate <> state Or CurrentPos = endpos If oldstate = #LexerState_Keyword lkeyword.s = LCase(keyword) If lkeyword = "procedure" thislevel | #SC_FOLDLEVELHEADERFLAG nextlevel + 1 oldstate = #LexerState_FoldKeyword ElseIf lkeyword = "endprocedure" nextlevel - 1 If nextlevel < #SC_FOLDLEVELBASE nextlevel = #SC_FOLDLEVELBASE EndIf oldstate = #LexerState_FoldKeyword EndIf keyword = "" EndIf SCI_SetStyling(0, CurrentPos - startkeyword, oldstate) startkeyword = CurrentPos EndIf If Char = 10 Or CurrentPos = endpos SCI_SetFoldLevel(0, linenumber, thislevel) thislevel = nextlevel linenumber + 1 EndIf CurrentPos + 1 Wend EndProcedure Procedure ScintillaWindowCallback(ID,*notify.SCNotification) *lpnmhdr.NMHDR = *notify\nmhdr Select *lpnmhdr\code Case #SCN_STYLENEEDED Highlight(-1, *notify\Position) Case #SCN_MARGINCLICK modifiers = *notify\modifiers Position = *notify\Position margin = *notify\margin linenumber = SCI_LineFromPosition(0, Position) Select margin Case 2 SCI_ToggleFold(0, linenumber) EndSelect EndSelect EndProcedure OpenWindow(0, #PB_Ignore, 0, 600, 400, "Scintilla example") CreateMenu(0, WindowID(0)) MenuTitle("File") MenuItem(1, "Open...") MenuItem(2, "Save As...") MenuItem(3, "Quit") MenuTitle("Edit") MenuItem(4, "Find...") MenuItem(5, "Goto...") MenuItem(6, "Toggle current fold") CreateGadgetList(WindowID(0)) ScintillaGadget(0, 0, 0, 600, 400,@ScintillaWindowCallback()) ;SetWindowCallback(@ScintillaWindowCallback()) ; Choose a lexer SCI_SetLexer(0, #SCLEX_CONTAINER) ; Set default font SCI_StyleSetFont(0, #STYLE_DEFAULT, @"Courier New") SCI_StyleSetSize(0, #STYLE_DEFAULT, 16) SCI_StyleClearAll(0) ; Set caret line colour SCI_SetCaretLineBack(0, $EEEEFF) SCI_SetCaretLineVisible(0, #True) ; Set styles for custom lexer SCI_StyleSetFore(0, #LexerState_Comment, $BB00) SCI_StyleSetItalic(0, #LexerState_Comment, 1) SCI_StyleSetFore(0, #LexerState_Keyword, 0) SCI_StyleSetFore(0, #LexerState_FoldKeyword, $FF) ; Margins SCI_SetMarginTypen(0, 0, #SC_MARGIN_NUMBER) SCI_SetMarginMaskN(0, 2, #SC_MASK_FOLDERS) SCI_SetMarginWidthN(0, 0, 50) SCI_SetMarginWidthN(0, 2, 20) SCI_SetMarginSensitiveN(0, 2, #True) ; Choose folding icons SCI_MarkerDefine(0, #SC_MARKNUM_FOLDEROPEN, #SC_MARK_CIRCLEMINUS) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDER, #SC_MARK_CIRCLEPLUS) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDERSUB, #SC_MARK_VLINE) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDERTAIL, #SC_MARK_LCORNERCURVE) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDEREND, #SC_MARK_CIRCLEPLUSCONNECTED) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDEROPENMID, #SC_MARK_CIRCLEMINUSCONNECTED) SCI_MarkerDefine(0, #SC_MARKNUM_FOLDERMIDTAIL, #SC_MARK_TCORNERCURVE) ; Choose folding icon colours SCI_MarkerSetFore(0, #SC_MARKNUM_FOLDER, $FFFFFF) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDER, 0) SCI_MarkerSetFore(0, #SC_MARKNUM_FOLDEROPEN, $FFFFFF) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDEROPEN, 0) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDEROPENMID, 0) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDERSUB, 0) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDERTAIL, 0) SCI_MarkerSetBack(0, #SC_MARKNUM_FOLDERMIDTAIL, 0) ; Set some sample text text.s = "; A custom Scintilla lexer example" + #CRLF$ text + "Procedure hello()" + #CRLF$ text + " Debug(" + Chr(34) + "Woo" + Chr(34) + ")" + #CRLF$ text + "EndProcedure" SetGadgetText(0, text) RemoveKeyboardShortcut(0, #PB_Shortcut_Tab) RemoveKeyboardShortcut(0, #PB_Shortcut_Tab | #PB_Shortcut_Shift) quit.l = #False Repeat event.l = WaitWindowEvent() Select event Case #PB_Event_CloseWindow quit = #True Case #PB_Event_Menu Select EventMenu() Case 1 : LoadFile() Case 2 : SaveFile() Case 3 : quit = #True Case 4 : FindText() Case 5 : GotoLine() Case 6 : ToggleCurrentFold() EndSelect EndSelect Until quit