Using colours in Editor gadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1051
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Using colours in Editor gadget

Post by marcoagpinto »

Hello!

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

If so, how do I do it?

Thanks!
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Using colours in Editor gadget

Post by Caronte3D »

Take alook to this thread:
viewtopic.php?t=80300
User avatar
marcoagpinto
Addict
Addict
Posts: 1051
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Using colours in Editor gadget

Post 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:
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Using colours in Editor gadget

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Using colours in Editor gadget

Post 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
Egypt my love
Axolotl
Addict
Addict
Posts: 837
Joined: Wed Dec 31, 2008 3:36 pm

Re: Using colours in Editor gadget

Post 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.
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).
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Using colours in Editor gadget

Post 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
BERESHEIT
Post Reply