Page 1 of 1

Getting rich text from EditorGadget

Posted: Fri Sep 22, 2017 12:48 pm
by Dude
Hi dudes! :)

I know how to set RTF (rich text) in an EditorGadget, but I need to be able to get that text in its raw form again later.

Below is what I want to achieve. How can it be done? Thank you in advance.

Code: Select all

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1\pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

OpenWindow(0,200,200,200,100,"test",#PB_Window_SystemMenu)

EditorGadget(0,10,10,180,50)
SetGadgetText(0,rtf$)

t$=GetGadgetText(0)
TextGadget(1,10,70,180,20,t$) ; Should show: {\rtf1\ansi\ ...

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Fri Sep 22, 2017 1:52 pm
by RSBasic

Re: Getting rich text from EditorGadget

Posted: Fri Sep 22, 2017 2:37 pm
by Dude
Is there a way without using files? Because that seems like an overkill, and my app doesn't use files.

Re: Getting rich text from EditorGadget

Posted: Fri Sep 22, 2017 2:48 pm
by RASHAD

Code: Select all

Global plTxt$,str.EDITSTREAM

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1\pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

Procedure StreamCB(dwCookie, pbBuff, cb, pcb)
  If cb
    plTxt$ + PeekS(pbBuff, cb, #PB_Ascii)
  EndIf
  pcb = cb
EndProcedure 

OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
TextGadget(1,10,70,180,100,"") ; Should show: {\rtf1\ansi\ ...

SetGadgetText(0,rtf$)

str\dwCookie = 0
str\dwError = 0
str\pfnCallback = @StreamCB()
SendMessage_(GadgetID(0), #EM_STREAMOUT, #SF_RTF, @str)
SetGadgetText(1, plTxt$)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Fri Sep 22, 2017 9:21 pm
by juror
You can also get it via the clipboard, but that will modify clipboard content.

Re: Getting rich text from EditorGadget

Posted: Sat Sep 23, 2017 4:08 am
by Dude
Hi juror; the clipboard isn't an option, for the reason you noted. :)

Rashad, thank you again for helping me. 8) I made your example into some nice self-contained code:

Code: Select all

Global EditorRawRichText$

Procedure GetEditorRawRichTextCB(dwCookie,pbBuff,cb,pcb)
  If cb
    EditorRawRichText$+PeekS(pbBuff,cb,#PB_Ascii)
  EndIf
  pcb=cb
EndProcedure

Procedure.s GetEditorRawRichText(gad)
  EditorRawRichText$=""
  str.EDITSTREAM\dwCookie=0
  str\dwError=0
  str\pfnCallback=@GetEditorRawRichTextCB()
  SendMessage_(GadgetID(gad),#EM_STREAMOUT,#SF_RTF,@str)
  ProcedureReturn EditorRawRichText$
EndProcedure

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1\pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

OpenWindow(0,0,0,200,150,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
SetGadgetText(0,rtf$)

TextGadget(1,10,70,180,100,"") ; Shows: {\rtf1\ansi\ ...
SetGadgetText(1,GetEditorRawRichText(0))

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Mon Oct 09, 2023 11:05 am
by firace
RASHAD wrote: Fri Sep 22, 2017 2:48 pm

Code: Select all

Global plTxt$,str.EDITSTREAM

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1\pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

Procedure StreamCB(dwCookie, pbBuff, cb, pcb)
  If cb
    plTxt$ + PeekS(pbBuff, cb, #PB_Ascii)
  EndIf
  pcb = cb
EndProcedure 

OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
TextGadget(1,10,70,180,100,"") ; Should show: {\rtf1\ansi\ ...

SetGadgetText(0,rtf$)

str\dwCookie = 0
str\dwError = 0
str\pfnCallback = @StreamCB()
SendMessage_(GadgetID(0), #EM_STREAMOUT, #SF_RTF, @str)
SetGadgetText(1, plTxt$)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Nice code!
But I can't manage to display accented characters properly in recent PB versions - any ideas? (The below code *should* display 'Repééé')

Code: Select all

Global plTxt$,str.EDITSTREAM

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1 Rep\'e9\'e9\'e9\par \pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

Procedure StreamCB(dwCookie, pbBuff, cb, pcb)
  If cb
    plTxt$ + PeekS(pbBuff, cb, #PB_Ascii)
  EndIf
  pcb = cb
EndProcedure 

OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
TextGadget(1,10,70,180,100,"") ; Should show: {\rtf1\ansi\ ...

SetGadgetText(0,rtf$)

str\dwCookie = 0
str\dwError = 0
str\pfnCallback = @StreamCB()
SendMessage_(GadgetID(0), #EM_STREAMOUT, #SF_RTF, @str)
SetGadgetText(1, plTxt$)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Mon Oct 09, 2023 12:18 pm
by Olli
I'm trying to forge the original work...

Code: Select all

Global plTxt$,str.EDITSTREAM

rtf$="{\rtf1\ansi\ansicpg1252\deff0\uc1 Rep\'e9\'e9\'e9\par \pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

Procedure StreamCB(dwCookie, pbBuff, cb, pcb)
  If cb
    plTxt$ + PeekS(pbBuff, cb)
  EndIf
  pcb = cb
EndProcedure 

OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
TextGadget(1,10,70,180,100,"") ; Should show: {\rtf1\ansi\ ...

SetGadgetText(0,rtf$)

str\dwCookie = 0
str\dwError = 0
str\pfnCallback = @StreamCB()
SendMessage_(GadgetID(0), #EM_STREAMOUT, #SF_RTF, @str)
SetGadgetText(1, plTxt$)

Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Mon Oct 09, 2023 1:31 pm
by firace
OK, managed to make it work using API (#WM_SETTEXT) instead of SetGadgetText...

Re: Getting rich text from EditorGadget

Posted: Mon Oct 09, 2023 2:32 pm
by RASHAD
Simple and direct
No need for Stream process

Code: Select all

rtf$ = "{\rtf1\ansi\ansicpg1252\deff0\uc1 Rep\'e9\'e9\'e9\par \pard\sa200\sl276\b\f0\fs22 do\b0  \i re\i0  \ul mi}"

OpenWindow(0,0,0,200,200,"test",#PB_Window_SystemMenu| #PB_Window_ScreenCentered)

EditorGadget(0,10,10,180,50)
TextGadget(1,10,70,180,100,"")

*Buffer = AllocateMemory(Len(rtf$)+1)
 PokeS(*Buffer, rtf$, Len(rtf$)+1,#PB_Ascii)  
 SendMessage_(GadgetID(0),#EM_REPLACESEL,0,PeekS(*Buffer ,Len(rtf$)+1,#PB_Unicode))
 
 SetGadgetText(1, rtf$)   
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: Getting rich text from EditorGadget

Posted: Mon Oct 09, 2023 2:51 pm
by Olli
I failed ! Thank you Rashad !