Page 1 of 1

Using colours in Editor gadget

Posted: Wed Dec 28, 2022 9:40 am
by marcoagpinto
Hello!

Is it possible to use colours in the text of Editor gadgets?

If so, how do I do it?

Thanks!

Re: Using colours in Editor gadget

Posted: Wed Dec 28, 2022 9:54 am
by Caronte3D
Take alook to this thread:
viewtopic.php?t=80300

Re: Using colours in Editor gadget

Posted: Wed Dec 28, 2022 10:08 am
by marcoagpinto
Caronte3D wrote: Wed Dec 28, 2022 9:54 am Take alook to this thread:
viewtopic.php?t=80300
Thank you, but it is too complex to be used.

I thought there would be simpler solutions.

:cry: :cry: :cry: :cry: :cry: :cry: :cry: :cry:

Re: Using colours in Editor gadget

Posted: Wed Dec 28, 2022 10:54 am
by Caronte3D
Maybe you can use a fragment of the code freak posted some time ago:

Code: Select all

Procedure Editor_Select(Gadget, LineStart.l, CharStart.l, LineEnd.l, CharEnd.l)   
  sel.CHARRANGE 
  sel\cpMin = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, LineStart, 0) + CharStart - 1
  
  If LineEnd = -1
    LineEnd = SendMessage_(GadgetID(Gadget), #EM_GETLINECOUNT, 0, 0)-1
  EndIf
  sel\cpMax = SendMessage_(GadgetID(Gadget), #EM_LINEINDEX, LineEnd, 0)
  
  If CharEnd = -1
    sel\cpMax + SendMessage_(GadgetID(Gadget), #EM_LINELENGTH, sel\cpMax, 0)
  Else
    sel\cpMax + CharEnd - 1
  EndIf
  SendMessage_(GadgetID(Gadget), #EM_EXSETSEL, 0, @sel) 
EndProcedure 

Procedure Editor_Color(Gadget, Color.l)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_COLOR
  format\crTextColor = Color
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure



; -------------------------------------------------------------
; Source Example:

#Editor = 1

If OpenWindow(0, 0, 0, 500, 500, "RTF Test",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    
    EditorGadget(#Editor, 10, 10, 480, 480)
    
    AddGadgetItem(#Editor, 0, "This is a blue text")      
    AddGadgetItem(#Editor, 1, "This is a red text")    
    
    Editor_Select(#Editor, 0, 1, 0, -1)  ; select line 1
      Editor_Color(#Editor, RGB(0,0,255))
      
    Editor_Select(#Editor, 1, 1, 1, -1)  ; select line 2
      Editor_Color(#Editor, RGB(255,0,0))
      
    Editor_Select(#Editor, 0, 0, 0, 0)   ; select nothing again
   
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf

End
Source: viewtopic.php?t=6666

Re: Using colours in Editor gadget

Posted: Wed Dec 28, 2022 11:10 am
by RASHAD
1- Use WordPad to create your file then save it as RTF
2- Load your RTF using your EditorGadget()

So you can change your file lately easily without any problem with your code

Re: Using colours in Editor gadget

Posted: Thu Dec 29, 2022 4:23 pm
by Axolotl
An EditorGadget() can work with RTf texts, as RASHAD wrote you can use WordPad for creating a text file and include it into your code like this:

Code: Select all

#EmbeddedTextFileName$ = "<Your-RTF-File-to-use>" 

Procedure.s GetRichText() 
  Protected rtftext$ 

  ; read rich text formatted text 
  rtftext$ = PeekS(?RtfDialogText, ?RtfDialogText_End - ?RtfDialogText, #PB_UTF8 | #PB_ByteLength) ; get text from embedded rtf file 

  ; replace Program constants 
  ; rtftext$ = ReplaceString(rtftext$, "$ProgramName$", #ProgramName$) 
  ; rtftext$ = ReplaceString(rtftext$, "$ProgramAuthor$", #ProgramAuthor$) 

  ProcedureReturn rtftext$ 
EndProcedure 

DataSection
  RtfDialogText:     ; start label 
    IncludeBinary(#EmbeddedTextFileName$) 
  RtfDialogText_End: ; end label 
EndDataSection

; schnipp 
; .. somewhere in your code 
  EditorGadget(#GADGET_edtDummy, 8, 8, 200, 120)  
  SetGadgetText(#GADGET_edtDummy, GetRichText()) 
; schnapp 

If you want create the text directly in your code, you should know some basics about the rtf format like this.

Code: Select all

Procedure.s GetRichText() 
  Protected rtftext$ 

  rtftext$ = "{\rtf1\ansi\deff0" + 
             "{\fonttbl; " + 
               "\f0 Segoe UI; " + 
               "\f1 Courier;" + 
             "}" + 
             "\viewkind0 " +  ; \viewkindN ..  0:None, 1:Page Layout view, 2:Outline view, 3:Master Document view, 4:Normal view, 5:Online Layout view 
             "\viewzk2 "   +  ; \viewzkN   .. zoom kind -> 0:None, 1:Full page, 2:Best fit 
  ; color table cf1;cf2;                  ; reset by cf0 
             "{\colortbl;" + 
               "\red0\green0\blue0;"   +  ; <--> black 
               "\red255\green0\blue0;" +  ; <--> red 
               "\red0\green0\blue139;" +  ; <--> kind of blue (??) 
               "\red0\green0\blue128;" +  ; <--> blue 
               "\red128\green128\blue128;" + ; <--> gray 
             "}"                       +  ; end of color definition block 
             "" ; end of header 

  ; start the content 
  rtftext$ + "\pard\cf3\fs40 <Here comes the text> \line\par "  ; \pard:default paragraph   \qc:Centered 
  rtftext$ + "\pard\cf0\fs18 " +  ; new paragraph  
             "here comes another text" + 
             "\pard" +  ; 
;                "{\i This} is a "          + #CRLF$ + ; 
;                "formatted {\b text}." + #CRLF$ + ; 
;                "\par" + #CRLF$ + ; 
;                "\cf2\f1 And here we have a colored text in courier :) \par " + #CRLF$ + 
;                "\cf3\f0\fs24 and another colored text. \par " + #CRLF$ + ; 
             "}" 
; end of rtf text 
  ProcedureReturn rtftext$ 
EndProcedure 
Keep in mind, that the EditorGadget is looking for the starting "{\rtf1" and ending "}" characters. Without the correct syntax you will see nothing.
This is the reason why you cannot use the AddGadgetItem() procedure to add more text lines. You have to use something else, like this.

Code: Select all

Procedure AddOutputColoredText(Gadget, Text.s, Color)  ; EditorGadget() 
  Protected hGad, format.CHARFORMAT2 
  
  format\cbSize      = SizeOf(CHARFORMAT2) 
  format\dwMask      = #CFM_COLOR 
  format\crTextColor = Color 
  
  hGad = GadgetID(Gadget) 
  SendMessage_(hGad, #EM_SETSEL, -1, -1)
  SendMessage_(hGad, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
  SendMessage_(hGad, #EM_REPLACESEL, 0, Text)
EndProcedure 
Happy coding and stay healthy.

Re: Using colours in Editor gadget

Posted: Mon Jan 02, 2023 3:12 am
by netmaestro
Simplest way would be to use a ScintillaGadget. This is the example from the PB doc for this gadget:

Code: Select all

If OpenWindow(0, 0, 0, 330, 90, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    
    If InitScintilla()
      ScintillaGadget(0, 10, 10, 320, 70, 0)
      
      ; Output set to red color
      ScintillaSendMessage(0, #SCI_STYLESETFORE, 0, RGB(255, 0, 0))
      
      ; Set the initial text to the ScintillaGadget
      *Text=UTF8("This is a simple ScintillaGadget with text...")
      ScintillaSendMessage(0, #SCI_SETTEXT, 0, *Text)
      FreeMemory(*Text) ; The buffer made by UTF8() has to be freed, to avoid memory leak
      
      ; Adding a second line of text with linebreak before
      Text$ = Chr(10) + "Second line"
      *Text=UTF8(Text$)
      ScintillaSendMessage(0, #SCI_APPENDTEXT, Len(Text$), *Text)
      FreeMemory(*Text)
    EndIf
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf