Some Color and Stuff for the EditorGadget...

Share your advanced PureBasic knowledge/code with the community.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Some Color and Stuff for the EditorGadget...

Post by freak »

Wrote that one today for somebody on the german forum.
Let's you modify the attributes of the selected Text in a EditorGadget()
I included the Editor_Seelect() function, so you can modify whatever you
want, by first selecting it.

Have fun...

Timo

Code: Select all

; Selects Text inside an EditorGadget
; Line numbers range from 0 to CountGadgetItems(#Gadget)-1
; Char numbers range from 1 to the length of a line
; Set Line numbers to -1 to indicate the last line, and Char 
; numbers to -1 to indicate the end of a line
; selecting from 0,1 to -1, -1 selects 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 

; Set the Text color for the Selection
; in RGB format
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

; Set Font Size for the Selection
; in pt
Procedure Editor_FontSize(Gadget, Fontsize.l)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_SIZE
  format\yHeight = FontSize*20
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

; Set Font for the Selection
; You must specify a font name, the font doesn't need
; to be loaded 
Procedure Editor_Font(Gadget, FontName.s)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_FACE
  PokeS(@format\szFaceName, FontName)
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

; Set Format of the Selection. This can be a combination of
; the following values:
; #CFM_BOLD
; #CFM_ITALIC
; #CFM_UNDERLINE
; #CFM_STRIKEOUT
Procedure Editor_Format(Gadget, Flags.l)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_ITALIC|#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE
  format\dwEffects = Flags
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure


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


#Editor = 1

If OpenWindow(0, 0, 0, 500, 500, #PB_Window_SystemMenu|#PB_Window_Screencentered, "EditorGadget")
  If CreateGadgetList(WindowID())
    
    EditorGadget(#Editor, 10, 10, 480, 480)
    
    AddGadgetItem(#Editor, 0, "This is a blue, bold and underlined big text")      
    AddGadgetItem(#Editor, 1, "Times new Roman, red, striked out and italic")    
    AddGadgetItem(#Editor, 2, "This is usual Text.")
    
    
    Editor_Select(#Editor, 0, 1, 0, -1)  ; select line 1
      Editor_Color(#Editor, RGB(0,0,255))
      Editor_FontSize(#Editor, 18)
      Editor_Format(#Editor, #CFM_UNDERLINE)
      
    Editor_Select(#Editor, 1, 1, 1, -1)  ; select line 2
      Editor_Color(#Editor, RGB(255,0,0))
      Editor_Font(#Editor, "Times New Roman")
      Editor_Format(#Editor, #CFM_ITALIC|#CFM_STRIKEOUT)
      
    Editor_Select(#Editor, 0, 0, 0, 0)   ; select nothing again

   
    Repeat
    Until WaitWindowEvent() = #PB_EventCloseWindow 
  EndIf
EndIf

End
[EDIT] replaced the Editor_Select() procedure, wasn't very good. [/EDIT]
quidquid Latine dictum sit altum videtur
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi,

have you any idea for changing the background color off a line ?

Best Regards,

Peter
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Here comes the background color...

Code: Select all

Structure CHARFORMAT2_
  cbSize.l
  dwMask.l  
  dwEffects.l  
  yHeight.l  
  yOffset.l  
  crTextColor.l  
  bCharSet.b  
  bPitchAndFamily.b  
  szFaceName.b[#LF_FACESIZE]  
  _wPad2.w  
  wWeight.w  
  sSpacing.w  
  crBackColor.l  
  lcid.l  
  dwReserved.l  
  sStyle.w  
  wKerning.w  
  bUnderlineType.b  
  bAnimation.b  
  bRevAuthor.b  
  bReserved1.b
EndStructure

Procedure Editor_BackColor(Gadget, Color.l)
  format.CHARFORMAT2_
  format\cbSize = SizeOf(CHARFORMAT2_)
  format\dwMask = $4000000  ; = #CFM_BACKCOLOR
  format\crBackColor = Color
  SendMessage_(GadgetID(Gadget), #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure
Note: this one needs RichEdit 2.0 to run.

Need anything else?

Timo
quidquid Latine dictum sit altum videtur
PWS32
User
User
Posts: 85
Joined: Sat May 10, 2003 1:02 pm
Location: Germany

Post by PWS32 »

Hi,

very very nice many thanks !!!, wath you mean with RichEdit 2.0 ? does this code not run under win98 or so ?

you have a idea to disable the edit, kopie and mark funktion in this gadget ?, im need this only for preview.

Best Regards,

Peter
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

I think you need RICHED20.DLL or RICHED32.DLL on your system (RICHED.DLL is RichEdit 1.0)
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

With this code (of freak), how can I save the text and restore it without losing the different fonts, colour,etc?


thank you!
PB 6.21 beta, PureVision User
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post by NoahPhense »

@timo

Unless you have already done so, I've created a lib from your stuff.
Would you like me to package it with a help file and post it?

- np
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

Hi, NoahPhense. Can you send me your lib?

thank you
PB 6.21 beta, PureVision User
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

With these functions, you can load/save the contents of the EditorGadget
in RTF format, which will save all colors/fonts as well.

NoahPhense: Sure you can post the code as a lib, but please keep the source
with it, so people can recompile the lib for future PB versions if neccesary.

Timo

Code: Select all

Procedure Editor_StreamCallback(dwCookie, *pbBuff, cb, *pcb.LONG)

  If dwCookie = 1 ; Save    
    WriteData(*pbBuff, cb)
    *pcb\l = cb
  
  Else ; Load
    length = Lof() - Loc()
    If length > cb
      ReadData(*pbBuff, cb)
      *pcb\l = cb
    Else
      ReadData(*pbBuff, length)
      *pcb\l = length
    EndIf
  
  EndIf

  ProcedureReturn 0
EndProcedure


Procedure Editor_Save(Gadget.l, FileName$)

  File = CreateFile(#PB_Any, FileName$)
  If File  
    stream.EDITSTREAM
    stream\dwCookie    = 1
    stream\pfnCallback = @Editor_StreamCallback()
    SendMessage_(GadgetID(Gadget), #EM_STREAMOUT, #SF_RTF, @stream)
    
    CloseFile(File)
  EndIf
  
EndProcedure


Procedure Editor_Load(Gadget.l, FileName$)

  File = ReadFile(#PB_Any, FileName$)
  If File
    stream.EDITSTREAM
    stream\dwCookie    = 0
    stream\pfnCallback = @Editor_StreamCallback()
    SendMessage_(GadgetID(Gadget), #EM_STREAMIN, #SF_RTF, @stream)
    
    CloseFile(File)
  EndIf
  
EndProcedure
quidquid Latine dictum sit altum videtur
zikitrake
Addict
Addict
Posts: 868
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

Freak, thank you 1000 times.


(A secret: I love you, community!) :kiss:
PB 6.21 beta, PureVision User
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Any updates needed for PB3.93? When trying the editor_load on regular text files, it'll load up blank.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

For streaming in text files, change

Code: Select all

SendMessage_(GadgetID(Gadget), #EM_STREAMIN, #SF_RTF, @stream)
to

Code: Select all

SendMessage_(GadgetID(Gadget), #EM_STREAMIN, #SF_TEXT, @stream)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

agh :) ok, I'll have to test the file before load to determine if it is pure text or rtf, thanks :)
User avatar
Hroudtwolf
Addict
Addict
Posts: 803
Joined: Sat Feb 12, 2005 3:35 am
Location: Germany(Hessen)
Contact:

Post by Hroudtwolf »

once more....

Code: Select all

Procedure  CutEditorText(gadget)
SendMessage_(GadgetID(gadget), #WM_CUT,0,0)    
EndProcedure



 Procedure  CopyEditorText(gadget)
 SendMessage_(GadgetID(gadget), #WM_COPY,0,0)    
 EndProcedure
 
 Procedure InsertEditorText(gadget,Text$)
  ProcedureReturn SendMessage_(GadgetID(gadget),#EM_REPLACESEL,0,Text$)
EndProcedure
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

This is great stuff. Thanks.
Post Reply