Page 1 of 2

Scintilla change STYLE of part of text on the fly [Resolved]

Posted: Fri Jul 26, 2024 3:22 pm
by Kwai chang caine
Hello at all

Very difficult for me this SCINTILLA lib, since 2 days i turn around :oops:
I have see it's impossible with Scintilla to simply select a part of text and send a color or a style to it :cry:
Apparently we must passing by the predefined STYLE :shock:

Like i don't whant to define in advance numerous styles, i say to me perhaps it's possible to change a STYLE already defined on the fly :idea:
So, in the code below , I tried to use the same style twice for two completely different configurations
But apparently, it's not possible too. Only the last STYLE is using :|

We would therefore have no other solution than creating a style for each case :
Black+Bold+Italic // Black+Bold+normal // Red + Bold +Italic //red + Bold + normal // Green + Bold +Italic //Green + Bold + normal..etc ... :shock:
This will make hundreds of differents cases of predefined STYLE to send to Scintilla :shock:

Code: Select all

InitScintilla("Scintilla.dll")
OpenWindow(0, 0, 0, 500, 300, "Essais Scintilla", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
ScintillaGadget(1, 0, 0, WindowWidth(0), WindowHeight(0) - 30, 0)

ScintillaSendMessage(1, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Courrier")
ScintillaSendMessage(1, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 15)
ScintillaSendMessage(1, #SCI_STYLECLEARALL)

For i = 1 To 300
 
 Ascii = Random(125, 32)
 
 If Sentence = 40
  Text$ + #CRLF$
  Sentence = 0
 Else
  Sentence + 1 
 EndIf
 
 Text$ + Chr(Ascii)
 
Next

ScintillaSendMessage(1, #SCI_SETTEXT, 0, UTF8(Text$))

For i = 1 To 300
 
 Count + 1

 If Count = 40
  
  Count = 0
  
  Select Color
  
   Case 0
    
    ScintillaSendMessage(1, #SCI_STYLESETITALIC, 1, #True)
    ScintillaSendMessage(1, #SCI_STYLESETFORE, 1, #Red)
    ScintillaSendMessage(1, #SCI_STYLESETBOLD, 1, #True)
      
    ScintillaSendMessage(1, #SCI_STARTSTYLING, i, 1)
    ScintillaSendMessage(1, #SCI_SETSTYLING, 10, 1)
            
    Color + 1
          
   Case 1
    
    ScintillaSendMessage(1, #SCI_STYLESETITALIC, 1, #False)
    ScintillaSendMessage(1, #SCI_STYLESETFORE, 1, #Blue)
    ScintillaSendMessage(1, #SCI_STYLESETBOLD, 1, #False)
      
    ScintillaSendMessage(1, #SCI_STARTSTYLING, i, 1)
    ScintillaSendMessage(1, #SCI_SETSTYLING, 20, 1)
    
    Color + 1
      
  EndSelect  
     
 EndIf 
 
Next 

Repeat  
  
 Evenement = WaitWindowEvent()
    
Until Evenement = #PB_Event_CloseWindow
Have a good day

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 4:38 pm
by Axolotl
You need different values for different styles (see constants #STYLE_X
Or (much better) got to the scintilla example (install folder.....)
probably not 100% correct, I have not checked the code at all.

Code: Select all

InitScintilla("Scintilla.dll")

#STYLE_1 = 1     ; <---- create some constants 
#STYLE_2 = 2      ; <---- create some constants 

OpenWindow(0, 0, 0, 500, 300, "Essais Scintilla", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
ScintillaGadget(1, 0, 0, WindowWidth(0), WindowHeight(0) - 30, 0)

ScintillaSendMessage(1, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Courrier")
ScintillaSendMessage(1, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 15)
ScintillaSendMessage(1, #SCI_STYLECLEARALL)

For i = 1 To 300
 
 Ascii = Random(125, 32)
 
 If Sentence = 40
  Text$ + #CRLF$
  Sentence = 0
 Else
  Sentence + 1 
 EndIf
 
 Text$ + Chr(Ascii)
 
Next

ScintillaSendMessage(1, #SCI_SETTEXT, 0, UTF8(Text$))

For i = 1 To 300
 
 Count + 1

 If Count = 40
  
  Count = 0
  
  Select Color
  
   Case 0
   ;ScintillaSendMessage(#GADGET_SciText, #SCI_STYLESETBOLD, #SYNTAX_Keyword, #True)
    
    ScintillaSendMessage(1, #SCI_STYLESETITALIC, #STYLE_1, #True)      ; <---- use the constants 
    ScintillaSendMessage(1, #SCI_STYLESETFORE, #STYLE_1, #Red)
    ScintillaSendMessage(1, #SCI_STYLESETBOLD, #STYLE_1, #True)
      
    ScintillaSendMessage(1, #SCI_STARTSTYLING, i, #STYLE_1)
    ScintillaSendMessage(1, #SCI_SETSTYLING, 10, #STYLE_1)
            
    Color + 1
          
   Case 1
    
    ScintillaSendMessage(1, #SCI_STYLESETITALIC, #STYLE_2, #False)  ;     ; <---- create some constants 
    ScintillaSendMessage(1, #SCI_STYLESETFORE, #STYLE_2, #Blue)
    ScintillaSendMessage(1, #SCI_STYLESETBOLD, #STYLE_2, #False)
      
    ScintillaSendMessage(1, #SCI_STARTSTYLING, i, #STYLE_2)
    ScintillaSendMessage(1, #SCI_SETSTYLING, 20, #STYLE_2)
    
    Color + 1
      
  EndSelect  
     
 EndIf 
 
Next 

Repeat  
  
 Evenement = WaitWindowEvent()
    
Until Evenement = #PB_Event_CloseWindow

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 4:58 pm
by Kwai chang caine
Hello Axolotl :D

Thanks for your answer 8)

Then if i have good understand, if i want choose all the thousands colors that exist in a configuration file, i haven't other choice, than create several thousands of constants :shock: It's nearly impossible :|

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:14 pm
by spikey
Kwai chang caine wrote: Fri Jul 26, 2024 4:58 pm :shock: It's nearly impossible :|
No, it's impossible! The Scintilla style table only supports 256 entries maximum. See https://www.scintilla.org/ScintillaDoc. ... Definition. It's a source code editing component, which has a limited number of different text types. It was never intended to be a word-processor.

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:20 pm
by Kwai chang caine
Hello Spikey :D

Yes you have right, i remember have read that yesterday, since all the time i search :wink: :|

Then ...that i not understand, how FRED do (for exampler) for give the choice to user in ScintillaGadget for colors of the KeyWords (for exampler too) in PB IDE, with the RequesterColor and his million of colors ?
Perhaps it's possible to delete the style, and recreate it immediately with the new parameters ? like that ...we use always the same style :idea:

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:29 pm
by spikey
Kwai chang caine wrote: Fri Jul 26, 2024 5:20 pm Then ...that i not understand, how FRED do (for exampler) for choose the color of the ScintillaGadget of PB IDE with the RequesterColor ?
There are fewer than 256 styles in use. The IDE defines about 40 styles in total. The ColorRequester returns a discrete colour number representing the component colour values and this is stored in the style table.

Each of the 256 table entries can potentially have its own font, size, style (bold/italic/regular), foreground colour and background colour set individually but you can't have more than 256 variations in total in a single gadget.

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:33 pm
by Axolotl
Probably I don't understand your problem.....
What about this:
A small array of colors (16) choose from Color Picker and .....

Code: Select all

;InitScintilla("Scintilla.dll")


OpenWindow(0, 0, 0, 500, 300, "Essais Scintilla", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
ScintillaGadget(1, 0, 0, WindowWidth(0), WindowHeight(0) - 30, 0)

;ScintillaSendMessage(1, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Courier")   ; <= Not working 
*t = UTF8("Courier")                                                      ; <= do it this way 
ScintillaSendMessage(1, #SCI_STYLESETFONT, #STYLE_DEFAULT, *t)            ; 
FreeMemory(*t)                                                            ; <= don't forget to free the memory 

ScintillaSendMessage(1, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 15)
ScintillaSendMessage(1, #SCI_STYLECLEARALL)

For i = 1 To 300
 Ascii = Random(125, 32)
 
 If Sentence = 40
  Text$ + #CRLF$
  Sentence = 0
 Else
  Sentence + 1 
 EndIf
 
 Text$ + Chr(Ascii)
 
Next

*t = UTF8(Text$)
ScintillaSendMessage(1, #SCI_SETTEXT, 0, *t)
FreeMemory(*t) 


; <== This is a dirty hack ==> 
Dim MyColor(16) 
MyColor( 0) = $FFF8F0 
MyColor( 1) = $FFFFF0 
MyColor( 2) = $FFFF00 
MyColor( 3) = $A09E5F 
MyColor( 4) = $E22B8A 
MyColor( 5) = $DCF8FF 
MyColor( 6) = $3C14DC 
MyColor( 7) = $8B0000 
MyColor( 8) = $8B8B00 
MyColor( 9) = $0B86B8 
MyColor(10) = $006400 
MyColor(11) = $A9A9A9 
MyColor(12) = $8B008B 
MyColor(13) = $FF901E 
MyColor(14) = $B48246 
MyColor(15) = $CD0000 
MyColor(16) = $800000

For i = 0 To 16 
 ;ScintillaSendMessage(1, #SCI_STYLESETITALIC, i, #True) 
  ScintillaSendMessage(1, #SCI_STYLESETFORE, i, MyColor(i)) 
  ScintillaSendMessage(1, #SCI_STYLESETBOLD, i, #True) 
Next i 




For i = 1 To 300 Step 20 

;  Count + 1
; 
;  If Count = 40
;   
;   Count = 0

  ScintillaSendMessage(1, #SCI_STARTSTYLING, i, color)  ; Color is the index of the Array (see Dim Mycolor() 
  ScintillaSendMessage(1, #SCI_SETSTYLING, 10, color)   ; 
  Color + 1
  
;   Select Color
;   
;    Case 0
;    ;ScintillaSendMessage(#GADGET_SciText, #SCI_STYLESETBOLD, #SYNTAX_Keyword, #True)
;     
;     ScintillaSendMessage(1, #SCI_STYLESETITALIC, #STYLE_1, #True)
;     ScintillaSendMessage(1, #SCI_STYLESETFORE, #STYLE_1, #Red)
;     ScintillaSendMessage(1, #SCI_STYLESETBOLD, #STYLE_1, #True)
;       
;     ScintillaSendMessage(1, #SCI_STARTSTYLING, i, #STYLE_1)
;     ScintillaSendMessage(1, #SCI_SETSTYLING, 10, #STYLE_1)
;             
;     Color + 1
;           
;    Case 1
;     
;     ScintillaSendMessage(1, #SCI_STYLESETITALIC, #STYLE_2, #False)
;     ScintillaSendMessage(1, #SCI_STYLESETFORE, #STYLE_2, #Blue)
;     ScintillaSendMessage(1, #SCI_STYLESETBOLD, #STYLE_2, #False)
;       
;     ScintillaSendMessage(1, #SCI_STARTSTYLING, i, #STYLE_2)
;     ScintillaSendMessage(1, #SCI_SETSTYLING, 20, #STYLE_2)
;     
;     Color + 1
;       
;   EndSelect  
;      
;  EndIf 
 
Next 


Repeat  
  
 Evenement = WaitWindowEvent()
    
Until Evenement = #PB_Event_CloseWindow

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:33 pm
by spikey
Kwai chang caine wrote: Fri Jul 26, 2024 5:20 pm Perhaps it's possible to delete the style, and recreate it immediately with the new parameters ? like that ...we use always the same style :idea:
Any other instances of the applied style elsewhere in the text buffer would be updated to reflect the style modification when the gadget is repainted. One style, one appearance.

But are you really going to have more than 256 unique styles? I can't think of any real life documentation I've ever seen which has more than 256 styles in a single document!

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:36 pm
by Kwai chang caine
@spikey
It's really not easy to understand :oops:
Have you a way for clear only one STYLE ?
I have found #SCI_STYLECLEARALL but with him
Image

@Axolotl
Thanks for your code 8) but here i have a grey windows :|

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:38 pm
by Kwai chang caine
Kwai chang caine wrote: Fri Jul 26, 2024 5:36 pm Thanks for your code 8) but here i have a grey windows :|
@Axolotl
I have found the problem
I decommented the InitScintilla() :wink:

Re: Scintilla change STYLE of part of text on the fly

Posted: Fri Jul 26, 2024 5:44 pm
by Kwai chang caine
Yeeees !!!!!!
Thanks a lot Axolotl for your magical code 8)

Image

I did not understand it's possible to give an array of colors at the place of the color parameter :oops:
Image

What a bouffon i'am :mrgreen:

With KCC it's not "My kingdom for an empire" 8)
But two days for an ARRAY :?

One thousand of STYLE !!!!...euh no.... :oops:
One thousand of thanks, at you two for your precious help
Have a very good end of the day :D

Re: Scintilla change STYLE of part of text on the fly [Resolved]

Posted: Fri Jul 26, 2024 6:43 pm
by AZJIO
Kwai chang caine wrote: Fri Jul 26, 2024 3:22 pm select a part of text and send a color or a style
SCI_INDICSETSTYLE

SCI_INDICSETSTYLE recolors SCI_SETSTYLING

Re: Scintilla change STYLE of part of text on the fly [Resolved]

Posted: Fri Jul 26, 2024 7:07 pm
by Kwai chang caine
Helo AZJIO :D

What a niece piece of code !!!
:shock:
This is trully high fashion :wink:
Image

I don't understand how you made this magical effect :oops:
But is it possible to also change the FORE, because in your code only the back is changed

Thanks a lot you too, for your splendid code 8)

Re: Scintilla change STYLE of part of text on the fly [Resolved]

Posted: Fri Jul 26, 2024 8:08 pm
by AZJIO
Kwai chang caine wrote: Fri Jul 26, 2024 7:07 pm But is it possible to also change the FORE,
You can choose from twenty styles. Replace #INDIC_STRAIGHTBOX with #INDIC_TEXTFORE

Re: Scintilla change STYLE of part of text on the fly [Resolved]

Posted: Sat Jul 27, 2024 10:28 am
by Kwai chang caine
Thanks for your answer 8)
Unfortunately if i replace #INDIC_STRAIGHTBOX with #INDIC_TEXTFORE
I not have a red fore, but an red underlined :|

Image

Decidedly ...it's strange a so much celebrate library, is also to much difficult to use and not have simple function like an standard Gadget Editor :|

Code: Select all

; AZJIO
; https://www.purebasic.fr/english/viewtopic.php?p=599333#p599333
; https://azjio.narod.ru/PureBasic/pb_user/userfunctions/SCI_INDICATOR.htm

EnableExplicit

#INDIC_TEXTFORE = 17

Enumeration
 #num_indicator
EndEnumeration

Define *Text, nLine, start, length

If Not InitScintilla()
 End
EndIf

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

 ScintillaGadget(0, 10, 10, 320, 70, 0)
 *Text = UTF8("Ceci est un simple ScintillaGadget avec du texte..." + #LF$ + " Plus de texte" + #LF$ + " Encore plus de texte !")
 ScintillaSendMessage(0, #SCI_SETTEXT, 0, *Text)
 FreeMemory(*Text)
 ;ScintillaSendMessage(0, #SCI_INDICSETSTYLE, #num_indicator, #INDIC_STRAIGHTBOX)        ; premier indicateur avec le style 8 (0-19)
 ScintillaSendMessage(0, #SCI_INDICSETSTYLE, #num_indicator, #INDIC_TEXTFORE)    
 ScintillaSendMessage(0, #SCI_INDICSETFORE, #num_indicator, #Red)                       ; premier indicateur de couleur rouge
 ;ScintillaSendMessage(0, #SCI_SETINDICATORCURRENT, #num_indicator, #INDIC_STRAIGHTBOX)  ; rend l'indicateur actuel 
 ScintillaSendMessage(0, #SCI_SETINDICATORCURRENT, #num_indicator, #INDIC_TEXTFORE) 
 ScintillaSendMessage(0, #SCI_INDICSETUNDER, #num_indicator, 1)                         ; indicateur sous le texte, c'est-à-dire ne l'observe pas
 ScintillaSendMessage(0, #SCI_INDICSETALPHA, #num_indicator, 127)                       ; Transparence 
; ScintillaSendMessage(0, #SCI_INDICGETOUTLINEALPHA, #num_indicator, 255)               ; Transparence de la bordure
 nLine = 1                                                                              ; le numéro de ligne commence à 0
 start = ScintillaSendMessage(0, #SCI_POSITIONFROMLINE, nLine)
 length = ScintillaSendMessage(0, #SCI_GETLINEENDPOSITION, nLine) - start
 ScintillaSendMessage(0, #SCI_INDICATORFILLRANGE, start, length)                        ; début et durée 
 ; ScintillaSendMessage(0, #SCI_INDICATORFILLRANGE, 46, 9)  ; début et longueur 
 
 Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf