[SOLVED] EditorGadget and line colorization

Just starting out? Need help? Post your questions and find answers here.
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

[SOLVED] EditorGadget and line colorization

Post by boddhi »

Hello,

In other codes, coloring the lines of an EditorGadget works in this way :

Code: Select all

  Format.CHARFORMAT
  Format\cbSize=SizeOf(CHARFORMAT)
  Format\dwMask=#CFM_COLOR
  Format\crTextColor=Color
  SendMessage_(GadgetID(EditorGadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@Format)
but not in the following and I don't understand why (it must be so big that I finally can't see it! :mrgreen: ).

Code: Select all

Procedure   Pc_SelectAndColorize(ArgGadget.u,ArgLine.l,ArgColor.l=-1)
  Protected.CHARRANGE Range
  Protected.CHARFORMAT Format
  Protected.l LineStart,TextLength
  Protected.s Text
  
  ; ******* These lines are here for debugging
  Static.l Line=-1
  Line+1
  If Line>5:Line=0:EndIf
  ArgLine=Line
  ; *******************************************
  
  LineStart=SendMessage_(GadgetID(ArgGadget),#EM_LINEINDEX,ArgLine,0)
  With Range
    \cpMin=LineStart
    \cpMax=LineStart+Len(GetGadgetItemText(ArgGadget,ArgLine))
  EndWith
  SendMessage_(GadgetID(ArgGadget),#EM_EXSETSEL,0,@Range)
    
  ; The code below doesn't work !!!
  If ArgColor>=0
    With Format
      \cbSize=SizeOf(CHARFORMAT)
      \dwMask=#CFM_COLOR
      \crTextColor=ArgColor
      SendMessage_(GadgetID(ArgGadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@Format)
    EndWith
  EndIf
EndProcedure

If OpenWindow(0, 0, 0, 322, 180, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  
  EditorGadget(0, 8, 8, 306, 133) 
  ButtonGadget(1, 121, 150, 80, 24,"Test")
  SendMessage_(GadgetID(0),#EM_SETTEXTMODE,#TM_RICHTEXT,0)

  For a = 0 To 5 
    AddGadgetItem(0, a, "Line "+Str(a)) 
  Next 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick
                ;Pc_SelectAndColorize(0,2,#Blue)
                Pc_SelectAndColorize(0,0,#Blue)
            EndSelect
        EndSelect
    EndSelect
  Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf 
Last edited by boddhi on Wed Aug 21, 2024 11:25 pm, edited 2 times in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: EditorGadget and line colorization

Post by PeDe »

You need at least a 'SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)'.

Peter
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: EditorGadget and line colorization

Post by boddhi »

PeDe wrote: You need at least a 'SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)'.
I forgot it! :oops: (Added in first post code)
But it doesn't work any better...
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: EditorGadget and line colorization

Post by PeDe »

It works here if the repeat loop is changed. You use 'WaitWindowEvent()' twice in a loop.

Peter

Code: Select all

  Define iEvent.i
  Repeat
    iEvent = WaitWindowEvent()
    Select iEvent
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick
                ;Pc_SelectAndColorize(0,2,#Blue)
                Pc_SelectAndColorize(0,0,#Blue)
            EndSelect
        EndSelect
    EndSelect
  Until iEvent = #PB_Event_CloseWindow 
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: EditorGadget and line colorization

Post by boddhi »

PeDe wrote: It works here if the repeat loop is changed. You use 'WaitWindowEvent()' twice in a loop.
Thanks Peter for your help but it doesn't work too and :
• In what way would a double WaitWindowEvent() block the colorization of the line? It does yet allow line selection!
If this were really the problem, neither operation (selection and colorization) would work.
• This is synthetic code, for the example. In my original code, there's no double call to WaitWindowEvent() :wink:
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: EditorGadget and line colorization

Post by PeDe »

With the double 'WaitWindowEvent()', the coloring of the line only works after the button has been clicked several times. The click event can be swallowed in the 'Until ...' part.

I have tested this with Windows 7/10 and PB v6.11 x64. The new code does what it should.

Peter
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: EditorGadget and line colorization

Post by boddhi »

Hello, I'm back about this thread! :mrgreen:

I'm going round in circles (thousands of ◯ :oops: :D )
Can someone explain to me why the following code doesn't work (colorization of a particular line)?

Code: Select all

Procedure   Pc_Coloration_LigneEditeur(ArgNoGadget.u,ArgNoLigne.l,ArgCarDepart.l=0,ArgNoCouleur.l=-1)
  Protected.CHARRANGE Plage
  Protected.CHARFORMAT Format
  Protected.l DebutLigne,LongChaine,CoulEditeur
  Protected.s Texte
  
  DebutLigne=SendMessage_(GadgetID(ArgNoGadget),#EM_LINEINDEX,ArgNoLigne,0)
  With Plage
    \cpMin=DebutLigne
    If ArgCarDepart>0:\cpMin+ArgCarDepart:EndIf
    Texte=GetGadgetItemText(ArgNoGadget,ArgNoLigne)
    LongChaine=Len(Texte)
    \cpMax=DebutLigne+LongChaine+1
  EndWith
  SendMessage_(GadgetID(ArgNoGadget),#EM_EXSETSEL,0,@Plage)
  If ArgNoCouleur>=0
    With Format
      \cbSize=SizeOf(CHARFORMAT)
      \dwMask=#CFM_COLOR;|#CFM_ITALIC|#CFM_BOLD
      \crTextColor=ArgNoCouleur
    EndWith
    With Plage
      \cpMin=\cpMax
    EndWith
    SendMessage_(GadgetID(ArgNoGadget),#EM_EXSETSEL,0,@Plage)
    With Format
      \crTextColor=CoulEditeur
      SendMessage_(GadgetID(ArgNoGadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@Format)
    EndWith
  EndIf
EndProcedure

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)  
  EditorGadget(0, 8, 8, 306, 133, #PB_Editor_ReadOnly|#PB_Editor_WordWrap) 
  SendMessage_(GadgetID(0),#EM_SETTEXTMODE,#TM_RICHTEXT,0)
  For a = 0 To 5 
    AddGadgetItem(0, a, "Ligne "+Str(a)) 
  Next 
  Pc_Coloration_LigneEditeur(0,3,0,#Red)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf 
 
Thanks in advance.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: EditorGadget and line colorization

Post by PeDe »

If I move the code in the procedure a little, it works.

Peter

Code: Select all

  If ArgNoCouleur>=0
    With Format
      \cbSize=SizeOf(CHARFORMAT)
      \dwMask=#CFM_COLOR;|#CFM_ITALIC|#CFM_BOLD
      \crTextColor=ArgNoCouleur
    EndWith
;     With Format
;       \crTextColor=CoulEditeur
;      EndWith
    SendMessage_(GadgetID(ArgNoGadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@Format)
    With Plage
      \cpMin=\cpMax
    EndWith
    SendMessage_(GadgetID(ArgNoGadget),#EM_EXSETSEL,0,@Plage)
  EndIf
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: EditorGadget and line colorization

Post by Axolotl »

PeDe gave you the solution already.

The error is that you did not update the gadget.
Or you have already reset the selection and the color before sending the update message.
Another Solution:
Copy Line 28 (this one)

Code: Select all

      SendMessage_(GadgetID(ArgNoGadget),#EM_SETCHARFORMAT,#SCF_SELECTION,@Format)
behind Line 20 (this one)

Code: Select all

      \crTextColor=ArgNoCouleur
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: EditorGadget and line colorization

Post by boddhi »

PeDe wrote: If I move the code in the procedure a little, it works.
Axolotl wrote: Another Solution:
Hello Peter & Axolotl

Thanks you both for your solutions and explanations.
It works well now :wink:
I rarely use EditorGadget and I'm going to have to familiarize myself with it and its dedicated APIs more thoroughly. :D :wink:
Thanks again.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
Post Reply