Page 1 of 1

Looking for simple tutorial or template for code coloring in scintilla

Posted: Tue Apr 25, 2023 8:41 pm
by jacdelad
Hello,
is there a simple tutorial or some kind of template for code coloring and such for scintilla? I know it's a bit work, but I don't want to invent the wheel again if there's already something floating around (beside the code for the PureBasic-Editor).

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Tue Apr 25, 2023 9:11 pm
by Tenaja
There's 26 pages to sort through off you search scintilla gadget. I'm sure you'll be the best judge of what suits your needs.

Gosci ( goscintilla ... But can't recall if there's an underscore or not) by srod is a great library that makes word coloring super easy, and contains many many examples. I've used it with great success. With this library, you practically just set your key words and you are good to go. He doesn't pull together a full editor, but gives so many examples for individual commands that it's not too tedious.

Have fun!

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Tue Apr 25, 2023 9:13 pm
by Tenaja

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Tue Apr 25, 2023 9:15 pm
by Tenaja

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Tue Apr 25, 2023 9:42 pm
by jacdelad
Thanke Tenaja, this seems to be a great start!

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Wed Apr 26, 2023 12:04 am
by AZJIO

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Wed Apr 26, 2023 12:27 am
by jacdelad
Also interesting. Thanks AZJIO, I'll look into it.

Re: Looking for simple tutorial or template for code coloring in scintilla

Posted: Wed Apr 26, 2023 4:05 am
by AZJIO

Code: Select all

; AZJIO
EnableExplicit

#q$ = Chr(34)
#SciG = 0

Structure SciRegExp
	re.s
	id.i
	len.i
	*mem
EndStructure

Define *tmp
Global tmp
Global NewList regex.SciRegExp()

Declare SciNotification(Gadget, *scinotify.SCNotification)
Declare Color(List regex.SciRegExp(), posStart, posEnd)

If Not InitScintilla()
	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
		MessageRequester("", "Не инициализирован Scintilla.dll")
	CompilerEndIf
	End
EndIf

;- RegExp подсветка стандартного текста
AddElement(regex())
regex()\re = #q$ + "[^" + #q$ + "]+?" + #q$

AddElement(regex())
regex()\re = "[a-zA-Z]"

AddElement(regex())
regex()\re = "[\[\]]"

AddElement(regex())
regex()\re = "[()+?]"

AddElement(regex())
regex()\re = "\d+"


ForEach regex()
	regex()\len = Len(regex()\re)
	regex()\mem = UTF8(regex()\re)
	tmp + 1
	regex()\id = tmp
Next


If OpenWindow(0, 0, 0, 1024, 600, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	ScintillaGadget(#SciG, 0, 0, 1024, 600, @SciNotification())
	ScintillaSendMessage(#SciG, #SCI_SETWRAPMODE, #SC_WRAP_WORD) ; с переносами строк
	
	ScintillaSendMessage(#SciG, #SCI_SETMARGINWIDTHN, 1, 0)   ; Устанавливает ширину поля 1 (номеров строк)
	ScintillaSendMessage(#SciG, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 10) ; размер шрифта
; 	ScintillaSendMessage(#SciG, #SCI_STYLECLEARALL)        ; общий стиль для всех
; 	ScintillaSendMessage(#SciG, #SCI_STYLESETBACK, #STYLE_DEFAULT, $3f3f3f)    ; цвет фона
; 	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, #STYLE_DEFAULT, $aaaaaa) ; цвет текста
	
	; ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 0, $008800) ; = #STYLE_DEFAULT
	; #SCI_STYLESETBACK ?
	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 1, $aaaa00)
	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 2, $009900)
	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 3, $ff9900)
	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 4, $0000ff)
	ScintillaSendMessage(#SciG, #SCI_STYLESETFORE, 5, $ff00ff)

	*tmp = UTF8(~"Это простой ScintillaGadget с \"текстом\"..." + #LF$ + "Больше [текста]" + #LF$ + "Еще больше (текста), 333!")
	ScintillaSendMessage(#SciG, #SCI_SETTEXT, 0, *tmp)
	FreeMemory(*tmp)
	
	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
	ForEach regex()
		FreeMemory(regex()\mem)
	Next
EndIf

; Подсвечивание через стиль
Procedure Color(List regex.SciRegExp(), posStart, posEnd)
	Protected txtLen, StartPos, EndPos, firstMatchPos

	ForEach regex()
		EndPos = posStart
		Repeat
			ScintillaSendMessage(#SciG, #SCI_SETTARGETSTART, EndPos)    ; от начала (задаём область поиска) используя позицию конца предыдущего поиска
			ScintillaSendMessage(#SciG, #SCI_SETTARGETEND, posEnd)   ; до конца по длине текста
			firstMatchPos = ScintillaSendMessage(#SciG, #SCI_SEARCHINTARGET, regex()\len, regex()\mem) ; возвращает позицию первого найденного. В параметрах длина искомого и указатель
																									   ; 		Debug firstMatchPos
			If firstMatchPos > -1                    ; если больше -1, то есть найдено, то
				StartPos = ScintillaSendMessage(#SciG, #SCI_GETTARGETSTART)        ; получает позицию начала найденного
				EndPos = ScintillaSendMessage(#SciG, #SCI_GETTARGETEND)         ; получает позицию конца найденного
				ScintillaSendMessage(#SciG, #SCI_STARTSTYLING, StartPos)        ; позиция начала (с 50-го)
				ScintillaSendMessage(#SciG, #SCI_SETSTYLING, EndPos - StartPos, regex()\id)     ; ширина и номер стиля
	; 			Debug Str(StartPos) + " " + Str(EndPos) + " " + Str(EndPos - StartPos) + " " + Str(regex()\id)
			Else
				Break
			EndIf
		ForEver
	Next
EndProcedure

; Уведомления
Procedure SciNotification(Gadget, *scinotify.SCNotification)
	Protected posEnd, posStart, line
	With *scinotify
		Select \nmhdr\code
			Case #SCN_STYLENEEDED ; нужна стилизация, подсветка текста

				; Устанавливает целевой диапазон поиска
				posStart = ScintillaSendMessage(#SciG, #SCI_GETENDSTYLED)
				posEnd = *scinotify.SCNotification\Position
				line = ScintillaSendMessage(#SciG, #SCI_LINEFROMPOSITION, posStart) ; номер строки из позиции (в которой расположен курсор)
				posStart = ScintillaSendMessage(#SciG, #SCI_POSITIONFROMLINE, line) ; позиция начала указанного номера строки
				; Устанавливает режим поиска (REGEX + POSIX фигурные скобки)
				ScintillaSendMessage(#SciG, #SCI_SETSEARCHFLAGS, #SCFIND_REGEXP | #SCFIND_POSIX)
				
				; подсвечивает всё или строку в которой ввод
				Color(regex(), posStart, posEnd)

				; подкраска, чтобы прекратить досить подсветкой каждую секунду
				ScintillaSendMessage(#SciG, #SCI_STARTSTYLING, 2147483646) ; позиция больше документа (2Gb)
				ScintillaSendMessage(#SciG, #SCI_SETSTYLING, 0, 0)     ; ширина и номер стиля
		EndSelect
	EndWith
EndProcedure