Page 1 of 1

Gadget with text coloring (?)

Posted: Tue Mar 12, 2024 10:20 am
by AZJIO
What are the ways to provide information with code highlighting?

I used RTF earlier, but it's Windows only. I searched the forum ("Rich Linux") and found only bold text.

The Scintilla gadget increases the size of the executable file to 2.6 MB.

WebGadget - there used to be a lot of talk that it doesn't work on Linux and additional packages need to be installed. But perhaps this is only necessary for compilation.

PBEdit - 980 kb on Linux

There was also an idea to make an individual module for each operating system, for example, for Windows, text is processed in RTF, and for Linux in Scintilla. I like it when the program weighs 200-300kb.

Re: Gadget with text coloring (?)

Posted: Tue Mar 12, 2024 1:50 pm
by Shardik
AZJIO wrote: Tue Mar 12, 2024 10:20 am I used RTF earlier, but it's Windows only. I searched the forum ("Rich Linux") and found only bold text.
MacOS also supports RTF but Linux doesn't. In your posted link I have linked to my source code in the German forum and explained it. In the posted images for Linux, RaspberryPi, MacOS and Windows I have demonstrated that my source code supports not only bold text but also italics, strikethrough and underlined. And a look into the data section of my source code should show you that it should be easy to separately add further attributes for RTF (MacOS and Windows) and Linux.

Re: Gadget with text coloring (?)

Posted: Sat Apr 27, 2024 11:02 am
by AZJIO
Shardik

Code: Select all

      SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, -1, -1)
      SendMessage_(GadgetID(EditorGadgetID), #EM_REPLACESEL, 0,
        AttributeTextASCII)
I tried, but this method works slowly and everything blinks at the moment the style of each word changes. I used it like this:

Code: Select all

ForEach TotalList()
	RTFcode$ + "\cf2" + TotalList()\path + "\cf0" + #CRLF$
	ForEach TotalList()\find()
		RTFcode$ + TotalList()\find()\stringStart
		RTFcode$ + "\cf1" + TotalList()\find()\found + "\cf0"
		RTFcode$ + TotalList()\find()\stringEnd + #CRLF$
	Next
	RTFcode$ + #CRLF$
Next
complete example

Code: Select all

; Generating RTF code from search results located in an array
If ListSize(TotalList())
	ForEach TotalList()
		i + 1
		RTFcode$ + #CRLF$ + Chr(1) + "cf2 " + Str(i) + ". " + Mid(TotalList()\path, LenForTrim) + Chr(1) + "cf0 " + #CRLF$ ; path
		j = 0
		ForEach TotalList()\find()
			j + 1
			RTFcode$ + Chr(1) + "cf3 " + Str(j) + " -->| " + Chr(1) + "cf0 " + TotalList()\find()\stringStart +
			              Chr(1) + "cf1 " + TotalList()\find()\found +
			              Chr(1) + "cf0 " + TotalList()\find()\stringEnd + #CRLF$
		Next
; 				RTFcode$ + #CRLF$
	Next
EndIf

; Escape metacharacters in the text that RTF will consider as code
RTFcode$ = ReplaceString(RTFcode$, "\", "\\", #PB_String_CaseSensitive)
RTFcode$ = ReplaceString(RTFcode$, "{", "\{", #PB_String_CaseSensitive)
RTFcode$ = ReplaceString(RTFcode$, "}", "\}", #PB_String_CaseSensitive)
; Restoring the previously imported RTF code
RTFcode$ = ReplaceString(RTFcode$, Chr(1), "\", #PB_String_CaseSensitive)
; replacing unreadable characters with questions, otherwise the text is not inserted into RichEdit after the first unreadable character encountered
re = CreateRegularExpression(#PB_Any, "[\001-\007\010\016\017\020-\027\030-\037\177]+") ; I removed \000 because it won’t appear here
If re
	RTFcode$ = ReplaceRegularExpression(re, RTFcode$, Chr($25A1))
	FreeRegularExpression(re)
EndIf
RTFcode$ = ReplaceString(RTFcode$, #CRLF$, "\par " + #CRLF$, #PB_String_CaseSensitive) ; replace word wrap with paragraph metacharacter
RTFcode$ = ReplaceString(RTFcode$, #TAB$, "\tab ", #PB_String_CaseSensitive) ; replace tabs with tab metacharacter

; add a header to the processed code

If iniFontSize
	Font = iniFontSize * 2
Else
	Font = 18
EndIf
; \fswiss instead of \fnil
RTFColorTable$ = "\red255\green0\blue0;" + ; red
                 "\red0\green0\blue255;" + ; blue
                 "\red99\green99\blue99;" ; grey
RTFcode$ = "{\rtf1\ansi\ansicpg1251\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset204 Arial;}}" + ; Arial font
        "{\colortbl;" + RTFColorTable$ + "}{\*\generator Riched20 10.0.16299}\viewkind4\uc1\pard\f0\fs" + Font + ; font size 18
        RTFcode$ + "}"

SetGadgetText(#Edit, RTFcode$)
I don't know if I should get GetACP_() instead of ansicpg1251. When I change the code page to 1252, the operating system still uses 1251 by default.

Using the TextReplace program, I made the output in RTF (Windows), in Scintilla (Linux), in HTML (Windows, Linux)

I tried your code on Linux, but for some reason when I re-create a child window with text, the text is not highlighted.

Code: Select all

If ListSize(TotalList())
	ForEach TotalList()
		i + 1
		AddTextWithAttributes(#Edit, #CRLF$ + "[bu]" + Str(i) + ". " + Mid(TotalList()\path, LenForTrim) + "[bu]" + #CRLF$)
		j = 0
		ForEach TotalList()\find()
			j + 1
			AddTextWithAttributes(#Edit, Str(j) + " -->| " + TotalList()\find()\stringStart + 
			                             "[bu]" + TotalList()\find()\found + "[bu]" +
			                             TotalList()\find()\stringEnd + #CRLF$)
		Next
	Next
EndIf