Catch RTF from editor gadget
Posted: Sat Jun 24, 2006 5:22 pm
I need to be able to catch the RTF from my editor gadget and put it inside another editor gadget.
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Procedure StreamFileIn_Callback(hFile, pbBuff, cb, pcb)
ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure
Procedure StreamFileOut_Callback(hFile, pbBuff, cb, pcb)
ProcedureReturn WriteFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure
Procedure Editor_Load(EGid.l, File.s)
Protected StreamData.EDITSTREAM
Protected FileID.l
FileID = ReadFile(#PB_Any, File)
If FileID
StreamData\dwCookie = FileID(FileID)
StreamData\dwError = #Null
StreamData\pfnCallback = @StreamFileIn_Callback()
If UCase(GetExtensionPart(File)) = "RTF"
SendMessage_(GadgetID(EGid), #EM_STREAMIN, #SF_RTF, @StreamData)
Else
SendMessage_(GadgetID(EGid), #EM_STREAMIN, #SF_TEXT, @StreamData)
EndIf
CloseFile(FileID)
EndIf
EndProcedure
Procedure Editor_Save(EGid.l,File.s)
Protected StreamData.EDITSTREAM
Protected FileID.l
If File <> ""
FileID = CreateFile(#PB_Any, File)
If FileID
StreamData\dwCookie = FileID(FileID)
StreamData\dwError = #Null
StreamData\pfnCallback = @StreamFileOut_Callback()
If UCase(GetExtensionPart(File)) = "RTF"
SendMessage_(GadgetID(EGid), #EM_STREAMOUT, #SF_RTF, @StreamData)
Else
SendMessage_(GadgetID(EGid), #EM_STREAMOUT, #SF_TEXT, @StreamData)
EndIf
CloseFile(FileID)
EndIf
Else
ProcedureReturn 0
EndIf
EndProcedure
Code: Select all
Procedure EditorGadgetSelectAll(Gadget_ID)
; Select the whole Text of the EditorGadget
RangeAll.CHARRANGE\cpMin = 0
RangeAll\cpMax = -1
ProcedureReturn SendMessage_(GadgetID(Gadget_ID),#EM_EXSETSEL,0,@RangeAll) ; Select All
EndProcedure
Procedure EditorGadgetCOPY(Gadget_ID)
ProcedureReturn SendMessage_(GadgetID(Gadget_ID), #WM_COPY, 0, 0)
EndProcedure
Procedure EditorGadgetPASTE(Gadget_ID)
ProcedureReturn SendMessage_(GadgetID(Gadget_ID), #EM_PASTESPECIAL, 0, 0)
EndProcedure
Code: Select all
Procedure GetRTFCallback(dwCookie.l,pbBuff.l,cb.l,*pcb)
CopyMemory(pbBuff,dwCookie,cb)
EndProcedure
Procedure SetRTFCallback(dwCookie.l,pbBuff.l,cb.l,*pcb)
PokeS(pbBuff,PeekS(dwCookie))
EndProcedure
Procedure Editor_GetRTF(Gadget.l,Adress.l)
Protected Stream.EDITSTREAM
Stream\dwCookie = Adress
Stream\pfnCallback = @GetRTFCallback()
SendMessage_(GadgetID(Gadget),#EM_STREAMOUT,#SF_RTF,@Stream)
EndProcedure
Procedure Editor_SetRTF(Gadget.l,Adress.l)
Protected Stream.EDITSTREAM
Stream\dwCookie = Adress
Stream\pfnCallback = @SetRTFCallback()
SendMessage_(GadgetID(Gadget),#EM_STREAMIN,#SF_RTF,@Stream)
EndProcedure
Code: Select all
*MyString = AllocateMemory(200)
;Copy it to buffer
Editor_GetRTF(#Editor,*MyString)
ClearGadgetItemList(#Editor) ;Clear the gadget
;Write the buffer
Editor_SetRTF(#Editor,*MyString)
Code: Select all
SetGadgetText(#Editor,PeekS(*MyString))
Code: Select all
Procedure.l GetRTFCallback(dwCookie.l,pbBuff.l,cb.l,*pcb.LONG)
Static Length.l
Debug 1
If cb = 0
Length = 0 ;reset
Else
CopyMemory(pbBuff,dwCookie+Length,cb)
Length + cb
EndIf
*pcb\l = cb
ProcedureReturn 0
EndProcedure
Code: Select all
Define *Buffer = AllocateMemory(GetRTFSize())
GetRTF(#Editor,*Buffer)
Code: Select all
Procedure.l GetRTFCallback(dwCookie.l,pbBuff.l,cb.l,*pcb.LONG)
Protected result
Static Length.l
CopyMemory(pbBuff,dwCookie+Length,cb)
Length + cb
*pcb\l = cb
If cb = 0
result=1 ;All done! Returning 1 will cancel further calls to this proc.
EndIf
ProcedureReturn result
EndProcedure
I don't think so. Since editor gadgets can embed OLE objects (such as images, Excel sheets etc.) determining the actual size of the rtf content before streaming would be extremely difficult. Besides which, the editor gadget does not hold it's content in rtf format internally and only converts it when we stream the contents out as you are doing.Do you know any easy way of just getting the size of all the data that will be sendt?
Thanks, it's actually a chat program, and I want to use the same editor gadget for each tab and blablabla...srod wrote:You're welcome. Good luck with your project.
Code: Select all
Global RTFLength
Procedure.l GetRTFCallback(dwCookie.l,pbBuff.l,cb.l,*pcb.LONG)
;Protected Result.l
;If cb = 0
;Result = 1
;Else
CopyMemory(pbBuff,dwCookie+RTFLength,cb)
RTFLength + cb
;EndIf
*pcb\l = cb
ProcedureReturn 0;Result
EndProcedure
Procedure.l GetRTFLenCallback(dwCookie.l,pbBuff.l,cb.l,*pcb.LONG)
RTFLength + cb
*pcb\l = cb
ProcedureReturn 0
EndProcedure
Procedure GetRTF(Gadget.l,Adress.l)
Protected Stream.EDITSTREAM
Stream\dwCookie = Adress
Stream\pfnCallback = @GetRTFCallback()
RTFLength = 0
SendMessage_(GadgetID(Gadget),#EM_STREAMOUT,#SF_RTF,@Stream)
EndProcedure
Procedure.l GetRTFLen(Gadget.l)
Protected Stream.EDITSTREAM
Stream\pfnCallback = @GetRTFLenCallback()
RTFLength = 0
SendMessage_(GadgetID(Gadget),#EM_STREAMOUT,#SF_RTF,@Stream)
ProcedureReturn RTFLength
EndProcedure
FreeMemory(*Buffer)
*Buffer = AllocateMemory(GetRTFLen(#Editor))
GetRTF(#Editor,*Buffer)
ClearGadgetItemList(#Editor)
SetGadgetText(#Editor,PeekS(*Buffer))