Code: Select all
;AZJIO Example Scintilla
#SC_C_CP_UTF8=65001 ;???
;#SCI_SSCI_SETADDITIONALCARETSBLINK=
;#SCI_SSCI_SETADDITIONALSELPHA=
;#SCI_SSCI_SETADDITIONALSELBACK=
;#SCI_SSCI_SETADDITIONALSELFORE=
;#SCI_SSCI_SETRECTANGULARSELECTIONMODIFIER=
EnableExplicit
InitScintilla()
; #MENU_EXTEND_SELECTION=10
Define txt$, txtLen, marginWidth, Gadget
Declare Color(*regex, regexLength, n)
Declare Color2(*regex, regexLength, n)
Declare MakeUTF8Text(text.s)
Declare MakeScintillaText(text.s, *sciLength.Integer=0)
Declare OpenFileR(*sciLength.Integer=0)
Declare SciNotification(Gadget, *scinotify.SCNotification)
Declare ReadFileR(Path$)
; Procedure ExtendScintillaSelection()
; Protected mainSel, selStart, selEnd
; mainSel=ScintillaSendMessage(0, #SCI_GETMAINSELECTION)
; selStart=ScintillaSendMessage(0, #SCI_GETSELECTIONNSTART, mainSel)
; selEnd=ScintillaSendMessage(0, #SCI_GETSELECTIONNEND, mainSel)
;
; EndProcedure.
txt$="Insert test "+#LF$+
"you can use this code as an example to work with Scintilla "+#LF$+
"link http://purebasic.info/phpBB3ex/index.php "+#LF$+
"This is search, markers, styling, etc. 2222"+#LF$
; Define i
; For i = 1 To 500
; txt$+#LF$+"Line: "+Str(i)
; Next
txtLen=StringByteLength(txt$, #PB_UTF8)
OpenWindow(100, 0, 0, 900, 600, "Scintilla Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget)
; Creating a gadget the size of the entire window
ScintillaGadget(0, 0, 0, WindowWidth(100), WindowHeight(100), @SciNotification())
; Sets text mode
ScintillaSendMessage(0, #SCI_SETWRAPMODE, #SC_WRAP_NONE) ; without line break
ScintillaSendMessage(0, #SCI_SETCODEPAGE, #SC_C_CP_UTF8) ; UTF-8 encoding
; ScintillaSendMessage(0, #SCI_SETVIRTUALSPACEOPTIONS, #SCVS_RECTANGULARSELECTION | #SCVS_USERACCESSIBLE) ; allow cursor and selection outside of end of line
; Sets current line highlighting
ScintillaSendMessage(0, #SCI_SETCARETLINEVISIBLE, 1) ; highlight the current string
ScintillaSendMessage(0, #SCI_SETCARETLINEVISIBLE, 1) ; highlights always, even if focus is lost
; ScintillaSendMessage(0, #SCI_SETCARETLINEBACKALPHA, 255) ; the current line's backlight transparency (0-255), 255 is 100% transparent
ScintillaSendMessage(0, #SCI_SETCARETLINEBACK, RGB(0, 0, 0)) ; highlight string colour
; Sets text style
ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, MakeUTF8Text("Arial")) ; the selection rectangle works better with the monospace font Courier New
ScintillaSendMessage(0, #SCI_STYLESETBACK, #STYLE_DEFAULT, RGB(63, 63, 63)) ; the background colour
ScintillaSendMessage(0, #SCI_STYLESETFORE, #STYLE_DEFAULT, RGB(153, 153, 153)) ; the colour of text
ScintillaSendMessage(0, #SCI_STYLECLEARALL)
; Sets indent size and style of line number column
ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_LINENUMBER, MakeUTF8Text("Arial")) ; the font of line numbers
ScintillaSendMessage(0, #SCI_STYLESETBACK, #STYLE_LINENUMBER, RGB(33, 33, 33)) ; the background colour of the lines number field
ScintillaSendMessage(0, #SCI_STYLESETFORE, #STYLE_LINENUMBER, RGB(153, 153, 153)) ; the colour of the text of the line number field
marginWidth=ScintillaSendMessage(0, #SCI_TEXTWIDTH, #STYLE_LINENUMBER, MakeUTF8Text("_999")) ; the width of the row number field
ScintillaSendMessage(0, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 0, marginWidth) ; Sets the width of the row number field
marginWidth=0
ScintillaSendMessage(0, #SCI_SETMARGINMASKN, 2, #SC_MASK_FOLDERS) ; Set margin for the convolution margin
ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 2, marginWidth) ; Sets the width of the convolution margin
ScintillaSendMessage(0, #SCI_SETMARGINSENSITIVEN, 2, #True) ; Set sensitivity of the margin to click
; Set cursor text style And selection
ScintillaSendMessage(0, #SCI_SETCARETSTICKY, 1) ; makes always visible (?)
ScintillaSendMessage(0, #SCI_SETCARETWIDTH, 1) ; the text cursor thickness
ScintillaSendMessage(0, #SCI_SETCARETFORE, RGB(255, 255, 255)) ; the colour of the text cursor
ScintillaSendMessage(0, #SCI_SETSELALPHA, 100) ; the transparency of the selection
ScintillaSendMessage(0, #SCI_SETSELBACK, 1, RGB(255, 255, 255)) ; the background colour of the selection
ScintillaSendMessage(0, #SCI_SETSELFORE, 1, RGB(160, 160, 160)) ; highlight text colour
; Sets additional styles If multiple cursors And selections are used
ScintillaSendMessage(0, #SCI_SETADDITIONALCARETFORE, RGB(255, 160, 160)) ; the colour of the additional text cursor
;;ScintillaSendMessage(0, #SCI_SSCI_SETADDITIONALCARETSBLINK, 1) ; the additional text cursor is blinking
;;ScintillaSendMessage(0, #SCI_SSCI_SETADDITIONALSELPHA, 100) ; the transparency of the additional selection
;;ScintillaSendMessage(0, #SCI_SSCI_SETADDITIONALSELBACK, RGB(255, 255, 100)) ; the background colour of the additional selection
;;ScintillaSendMessage(0, #SCI_SSCI_SETADDITIONALSELFORE, RGB(255, 255, 130)) ; the colour of the text of the additional selection
; Enables multiple cursor
;;ScintillaSendMessage(0, #SCI_SSCI_SETRECTANGULARSELECTIONMODIFIER, #SCMOD_ALT) ; highlight while holding down the Alt key
ScintillaSendMessage(0, #SCI_SETMULTIPLESELECTION, 1) ; allows to select several times by holding down CTRL or CMD
ScintillaSendMessage(0, #SCI_SETMULTIPASTE, #SC_MULTIPASTE_EACH) ; multiple insertion of text
ScintillaSendMessage(0, #SCI_SETADDITIONALSELECTIONTYPING, 1) ; allows inputting text, moving cursor, etc. in multiple places at once
; ScintillaSendMessage(0, #SCI_USEPOPUP, 1)
; ScintillaSendMessage(0, #SCI_SETVSCROLLBAR, 1) ; Vertical scrollbar always
; Allow hotkey CTRL + D for additional auto-allocate.
; AddKeyboardShortcut(100, #PB_Shortcut_Control | #PB_Shortcut_D, #MENU_EXTEND_SELECTION)
; BindEvent(#PB_Event_Menu, ExtendScintillaSelection())
ScintillaSendMessage(0, #SCI_SETTEXT, 0, MakeUTF8Text(txt$)) ; Set gadget text
ScintillaSendMessage(0, #SCI_GOTOPOS, txtLen) ; Cursor at end of text
SetActiveGadget(0) ; Set gadget active, input focus
; These constants will be used for the syntax highlighting.
Enumeration 0
#LexerState_Space
#LexerState_Number
#LexerState_Keyword
#LexerState_String
#LexerState_Preprocessor
#LexerState_Operator
#LexerState_Comment
#LexerState_FoldKeyword
EndEnumeration
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Comment, RGB(113, 174, 113)) ; The colour of the comment
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Number, $ABCEE3) ; The colour of the numbers, BGR
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Keyword, $FF9F00) ; The colour of the keyword, BGR
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Preprocessor, $DE97D9) ; The colour of the preprocessor, BGR
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Operator, $8080FF) ; The colour of the preprocessor, BGR
ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_FoldKeyword, RGB(0, 136, 0)) ; The colour of the keywords with the minimize.
ScintillaSendMessage(0, #SCI_STYLESETBOLD, #LexerState_Number, 1) ; Emphasize numbers in bold type
ScintillaSendMessage(0, #SCI_STYLESETITALIC, #LexerState_Comment, 1) ; Emphasize comments in slanted font
; MessageRequester(Str(#STYLE_LINENUMBER) , Str(#STYLE_DEFAULT)) ; here we check constants of other styles, if they do not interfere
; For RGB from web
; Global num.i = Val("$71AE71")
; Global num_text.s = Str(Red(num)) + ", " + Str(Green(num)) + ", " + Str(Blue(num))
; Debug num_text ; Displays the colour in RGB function format
; Trying to apply the style (positioning and length issue)
ScintillaSendMessage(0, #SCI_STARTSTYLING, 50, 0) ; start position (from 50th)
ScintillaSendMessage(0, #SCI_SETSTYLING, 14, #LexerState_Comment) ; width and style number
If CreateMenu(0, WindowID(100)) ; Here start menu creation....
MenuTitle("Menu")
MenuItem(1, "Open file")
MenuItem(2, "Insert current date")
MenuItem(3, "Paste from clipboard")
MenuBar() ; separator
MenuItem(4, "Exit")
MenuItem(5, "Find numbers (indicators)")
MenuItem(6, "Find numbers (style)")
MenuItem(7, "Clear Style")
EndIf
Global Event, em, Text$, TextLength, regex$
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_SizeWindow
ResizeGadget(0, #PB_Ignore, #PB_Ignore, WindowWidth(100), WindowHeight(100))
Case #PB_Event_Menu
em = EventMenu()
Select em
Case 1
OpenFileR()
Case 2
Text$ = FormatDate("%dd.%mm.%yyyy", Date()) ;Returns current date
; ScintillaSendMessage(0, #SCI_APPENDTEXT, Len(Text$), MakeUTF8Text(Text$)) ; does not work with UTF-8
; ScintillaSendMessage(0, #SCI_INSERTTEXT, txtLen, MakeUTF8Text(Text$)) ; the problem is to measure the length of the text in Scintilla each time
ScintillaSendMessage(0, #SCI_APPENDTEXT, TextLength, MakeScintillaText(Text$, @TextLength))
Case 3
; Text$ = "pasted text" ; before pasting it is brought to the UTF-8 format
Text$ = GetClipboardText()
ScintillaSendMessage(0, #SCI_APPENDTEXT, TextLength, MakeScintillaText(Text$, @TextLength))
Case 4
Break
Case 5
regex$ = "\d+"
Color(MakeScintillaText(regex$, @TextLength), Len(regex$), 7)
Case 6
regex$ = "\d+"
Color2(MakeScintillaText(regex$, @TextLength), Len(regex$), 7)
Case 7
ScintillaSendMessage(0, #SCI_CLEARDOCUMENTSTYLE)
EndSelect
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
FreeMenu(0)
CloseWindow(100)
End
; Notifications
ProcedureDLL SciNotification(Gadget, *scinotify.SCNotification)
Protected Path$, pos
; Select Gadget
; Case 0 ; Notify Gadget 0 (Scintilla)
With *scinotify
Select \nmhdr\code
Case #SCN_URIDROPPED ; throw files in window
; Debug PeekS(*scinotify.SCNotification\text, -1, #PB_Ascii)
Path$ = PeekS(*scinotify.SCNotification\text, -1, #PB_UTF8 | #PB_ByteLength)
Path$ = PeekS(*scinotify.SCNotification\text, -1, #PB_Ascii)
pos = FindString(Path$, Chr(10)) ; trimming, leaving 1 file
If pos
Path$ = LSet(Path$ , pos-2)
EndIf
Path$ = RemoveString(Path$ , "file://", #PB_String_CaseSensitive, 1, 1) ; delete 1 time
Debug Path$
ReadFileR(Path$)
EndSelect
EndWith
; EndSelect
EndProcedure
; Convert text to text and paste into Scintilla
Procedure MakeUTF8Text(text.s)
Static buffer.s
buffer=Space(StringByteLength(text, #PB_UTF8))
PokeS(@buffer, text, -1, #PB_UTF8)
ProcedureReturn @buffer
EndProcedure
Procedure MakeScintillaText(text.s, *sciLength.Integer=0)
Static sciLength : sciLength=StringByteLength(text, #PB_UTF8) ; #TextEncoding
Static sciText.s : sciText = Space(sciLength)
If *sciLength : *sciLength\i=sciLength : EndIf ;<--- Returns the length of the scintilla buffer (required for a specific scintilla command)
PokeS(@sciText, text, -1, #PB_UTF8) ;--- #TextEncoding
ProcedureReturn @sciText
EndProcedure
Procedure OpenFileR(*sciLength.Integer=0)
; File path request
Protected Path$ = OpenFileRequester("Select file","", "Text|*.txt;*.bat;*.pb|All files (*.*)|*.*",0)
If Path$ ; since sciLength = 0 this is the override criterion for the following function
ReadFileR(Path$)
EndIf
EndProcedure
Procedure ReadFileR1(Path$) ; I replaced my own variant with the variant from the reference below
Protected sizefile, *MemoryBuffer
sizefile = FileSize(Path$) ; determine the data size in bytes
If sizefile => 0 ; file exists
*MemoryBuffer = AllocateMemory(sizefile) ; allocation of memory for the file size
ReadFile(0 , Path$, #PB_UTF8)
ReadData(0, *MemoryBuffer, sizefile) ; read file into memory
CloseFile(0) ; close file
ScintillaSendMessage(0, #SCI_SETTEXT, 0, *MemoryBuffer)
ScintillaSendMessage(0, #SCN_UPDATEUI, #SC_UPDATE_V_SCROLL) ; not true, scroll update
FreeMemory(*MemoryBuffer)
EndIf
EndProcedure
Procedure ReadFileR(Path$)
Protected length, *MemoryID
If ReadFile(0, Path$)
length = Lof(0) ; get length of the opened file
*MemoryID = AllocateMemory(length) ; allocate necessary memory
If *MemoryID
ReadData(0, *MemoryID, length) ; read all data in the memory block
EndIf
CloseFile(0)
ScintillaSendMessage(0, #SCI_SETTEXT, 0, *MemoryID)
FreeMemory(*MemoryID)
EndIf
EndProcedure
; highlighting through the indicators
Procedure Color(*regex, regexLength, n)
Protected txtLen, StartPos, EndPos, firstMatchPos
; Sets search mode (REGEX + POSIX braces)
ScintillaSendMessage(0, #SCI_SETSEARCHFLAGS, #SCFIND_REGEXP | #SCFIND_POSIX)
; Sets target search range
txtLen = ScintillaSendMessage(0, #SCI_GETTEXTLENGTH) ; gets the text length
ScintillaSendMessage(0, #SCI_INDICSETSTYLE, n, 6) ; #INDIC_TEXTFORE = 17 creates an indicator with number 7 (occupied by default 0, 1, 2)
ScintillaSendMessage(0, #SCI_INDICSETFORE, n, $71AE71) ; assign colour of indicator number 7 - green
EndPos = 0
Repeat
ScintillaSendMessage(0, #SCI_SETTARGETSTART, EndPos) ; from the beginning (set the search area) using the position of the previous search end
ScintillaSendMessage(0, #SCI_SETTARGETEND, txtLen) ; to the end by the text length
firstMatchPos=ScintillaSendMessage(0, #SCI_SEARCHINTARGET, regexLength, *regex) ; returns the position of the first found. The parameters are length and pointer
If firstMatchPos>-1 ; if greater than -1, that is found
StartPos=ScintillaSendMessage(0, #SCI_GETTARGETSTART) ; it gets position of the first found
EndPos=ScintillaSendMessage(0, #SCI_GETTARGETEND) ; gets position of the end of found
ScintillaSendMessage(0, #SCI_SETINDICATORCURRENT, n) ; makes the indicator number 7 current
ScintillaSendMessage(0, #SCI_INDICATORFILLRANGE, StartPos, EndPos - StartPos) ; it selects text using the current indicator
Else
Break
EndIf
ForEver
EndProcedure
; highlighting via style
Procedure Color2(*regex, regexLength, n)
Protected txtLen, StartPos, EndPos, firstMatchPos
; Sets search mode (REGEX + POSIX curly brackets)
ScintillaSendMessage(0, #SCI_SETSEARCHFLAGS, #SCFIND_REGEXP | #SCFIND_POSIX)
; Sets target search range
txtLen = ScintillaSendMessage(0, #SCI_GETTEXTLENGTH) ; gets the text length
EndPos = 0
Repeat
ScintillaSendMessage(0, #SCI_SETTARGETSTART, EndPos) ; from the beginning (set the search area) using the position of the previous search end
ScintillaSendMessage(0, #SCI_SETTARGETEND, txtLen) ; to the end by the text length
firstMatchPos=ScintillaSendMessage(0, #SCI_SEARCHINTARGET, regexLength, *regex) ; returns the position of the first found. The parameters are length and pointer
If firstMatchPos>-1 ; if greater than -1, that is found
StartPos=ScintillaSendMessage(0, #SCI_GETTARGETSTART) ; it gets position of the first found
EndPos=ScintillaSendMessage(0, #SCI_GETTARGETEND) ; gets position of the end of found
ScintillaSendMessage(0, #SCI_STARTSTYLING, StartPos, 0) ; the start position (from the 50th)
ScintillaSendMessage(0, #SCI_SETSTYLING, EndPos - StartPos, #LexerState_Number) ; width and style number
Else
Break
EndIf
ForEver
EndProcedure