Page 1 of 1

[SOLVED] EditorGadget and line colorization

Posted: Sat Jun 15, 2024 3:00 pm
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 

Re: EditorGadget and line colorization

Posted: Sat Jun 15, 2024 3:21 pm
by PeDe
You need at least a 'SendMessage_(GadgetID(0), #EM_SETTEXTMODE, #TM_RICHTEXT, 0)'.

Peter

Re: EditorGadget and line colorization

Posted: Sat Jun 15, 2024 5:12 pm
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...

Re: EditorGadget and line colorization

Posted: Sat Jun 15, 2024 5:30 pm
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 

Re: EditorGadget and line colorization

Posted: Sat Jun 15, 2024 6:29 pm
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:

Re: EditorGadget and line colorization

Posted: Sat Jun 15, 2024 6:47 pm
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

Re: EditorGadget and line colorization

Posted: Tue Aug 20, 2024 7:45 pm
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.

Re: EditorGadget and line colorization

Posted: Wed Aug 21, 2024 5:43 am
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

Re: EditorGadget and line colorization

Posted: Wed Aug 21, 2024 3:35 pm
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

Re: EditorGadget and line colorization

Posted: Wed Aug 21, 2024 11:23 pm
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.