Page 1 of 3

Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 4:06 am
by Fangbeast
Is there a way (simple, like me) to print rtf formatted text from editorgadget?

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 5:54 am
by kernadec
Hi,
Format RTF two negative items because there are problems with unicode, linux not know rft

Best regard

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 5:56 am
by juror
The simple (like me) method I've used (I'm sure someone will have a better way) is to "copy" the rtf format text and output it into a rtf aware editor, such as wordpad or just an rtf file. I usually let the user handle the actual printing although you could spool from wordpad to a default printer if there is one set up.

Hopefully, many folks (Mr RASHAD) will have a better method. ;)

Windows only btw, but I'm sure you know that.

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 8:01 am
by Fangbeast
juror wrote:The simple (like me) method I've used (I'm sure someone will have a better way) is to "copy" the rtf format text and output it into a rtf aware editor, such as wordpad or just an rtf file. I usually let the user handle the actual printing although you could spool from wordpad to a default printer if there is one set up.
That won't work. The idea is to print from the PB application which has an editorgadget that has rtf formatted text in it.

I can't expect the user to manually copy text from the pb app to wordpad, I'll never stop hearing the moaning, bitching, complaining and whining from them!!!

:):)

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 8:35 am
by javabean

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 8:51 am
by RSBasic
Export RTF content:

Code: Select all

EnableExplicit

Define a
Define EDITSTREAM.EDITSTREAM

Procedure EditStreamCallback(dwCookie, pbBuff, cb, pcb)
  ProcedureReturn WriteFile_(dwCookie, pbBuff, cb, pcb, 0) ! 1
EndProcedure

If OpenWindow(0, 0, 0, 500, 400, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  EditorGadget(1, 0, 0, 500, 400, 0)
  
  For a=1 To 10
    AddGadgetItem(1, -1, "Item " + Str(a), 0, 0)
  Next
  
  ;Saving
  If CreateFile(1, "D:\RTFDocument.rtf")
    EDITSTREAM\dwCookie = FileID(1)
    EDITSTREAM\pfnCallback = @EditStreamCallback()
    SendMessage_(GadgetID(1), #EM_STREAMOUT, #SF_RTF, EDITSTREAM)
    
    CloseFile(1)  
  EndIf
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
(only Windows)

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 8:51 am
by RASHAD
Hi
Main coder : LuckyLuke
Modified by : RASHAD

1 - : Print direct what in the EditorGadget
2 - : Load & Print any RTF file
Ascii & Unicode compatible

Code: Select all

Global PrinterDC

Procedure  StartPrint(Doc.s)
    mDI.DOCINFO
    mDI\cbSize = SizeOf(DOCINFO)
    mDI\lpszDocName = @Doc
    mDI\lpszOutput = 0
    StartDoc_(PrinterDC,mDI)
EndProcedure

Procedure PrintRichText(hWnd, hInst, rtfEdit, LM, RM, TM, BM)
 
   fr.FORMATRANGE
   pd.PRINTDLG
   
   pd\lStructSize = SizeOf(PRINTDLG)
   pd\hwndOwner = hWnd
   pd\hDevMode = 0
   pd\hDevNames = 0
   pd\nFromPage = 0
   pd\nToPage = 0
   pd\nMinPage = 0
   pd\nMaxPage = 0
   pd\nCopies = 0
   pd\hInstance = hInst
   pd\Flags = #PD_RETURNDC | #PD_PRINTSETUP
   pd\lpfnSetupHook = 0
   pd\lpSetupTemplateName = 0
   pd\lpfnPrintHook = 0
   pd\lpPrintTemplateName = 0
   
   If PrintDlg_(pd)
     b = GlobalLock_(pd\hDevNames)       
     drv.s  = PeekS(b + PeekW(b))
     name.s = PeekS(b + PeekW(b + 2))
     PrinterDC = DefaultPrinter()
       
     fr\hdc = PrinterDC
     fr\hdcTarget = PrinterDC
     fr\chrg\cpMin = 0
     fr\chrg\cpMax = -1
     fr\rc\top = TM*1440
     fr\rcPage\top = fr\rc\top
     fr\rc\left = LM*1440
     fr\rcPage\left = fr\rc\left
 
     iWidthTwips = Int((GetDeviceCaps_(PrinterDC, #HORZRES)/GetDeviceCaps_(PrinterDC, #LOGPIXELSX))*1440)
     iHeightTwips = Int((GetDeviceCaps_(PrinterDC, #VERTRES )/GetDeviceCaps_(PrinterDC, #LOGPIXELSY))*1440)
     fr\rc\right = iWidthTwips-RM*1440
     fr\rcPage\right = fr\rc\right
     fr\rc\Bottom = iHeightTwips-BM*1440
     fr\rcPage\bottom = fr\rc\bottom
     
     StartPrint("RTF Printing")
     StartPage_(PrinterDC)

     iTextOut = 0
     iTextAmt = SendMessage_(rtfEdit, #WM_GETTEXTLENGTH, 0, 0)
      While iTextOut<iTextAmt
         iTextOut = SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, fr)           
         If iTextOut<iTextAmt                   
            EndPage_(PrinterDC)
            StartPage_(PrinterDC)
            fr\chrg\cpMin = iTextOut
            fr\chrg\cpMax = -1 
            iTextAmt = iTextAmt - iTextOut
            iTextOut = 0
         EndIf
      Wend
     SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, 0)
     EndPage_(PrinterDC)
     EndDoc_(PrinterDC)
     DeleteDC_(PrinterDC)
  EndIf
EndProcedure

Procedure StreamFileInCallback(hFile, pbBuff, cb, pcb)
  ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure

Procedure loadFile(pFilePath.s)
  If ReadFile(0, pFilePath)
    If GetExtensionPart(pFilePath)="rtf"
      uFormat = #SF_RTF
    Else
      uFormat = #SF_TEXT
    EndIf
    edstr.EDITSTREAM
    edstr\dwCookie = FileID(0)
    edstr\dwError = 0
    edstr\pfnCallback = @StreamFileInCallback()
    SendMessage_(GadgetID(0), #EM_STREAMIN, uFormat, edstr)
    CloseFile(0)
  Else
    MessageRequester("Error", "Error Occured While Opening File", #PB_MessageRequester_Ok)
  EndIf
EndProcedure

OpenWindow(0, 200, 50, 640, 400,"RTF PRINTING", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateMenu(0, WindowID(0))
  MenuTitle("&File")
    MenuItem(0, "&Open")
    MenuItem(1, "&Print")
    MenuItem(2, "&Quit")
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(StringByteLength(a, #PB_Ascii) + 1)
PokeS(*MemoryBuffer, a.s, -1, #PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 0
          FileName$ = OpenFileRequester("", "", "All files|*.*", 0)
          If FileName$
            loadFile(FileName$)
          EndIf
          
        Case 1
          PrintRichText(WindowID(0), GetModuleHandle_(0), GadgetID(0), 0, 0, 0, 0)
          
        Case 2
          Quit = 1
          
      EndSelect
  EndSelect 
Until Quit = 1

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 10:19 am
by Fangbeast
Not familiar with EMF. Does it convert in memory then print it?

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 10:20 am
by Fangbeast
RASHAD wrote:Hi
Main coder : LuckyLuke
Modified by : RASHAD

1 - : Print direct what in the EditorGadget
2 - : Load & Print any RTF file
Ascii & Unicode compatible

Code: Select all

Global PrinterDC

Procedure  StartPrint(Doc.s)
    mDI.DOCINFO
    mDI\cbSize = SizeOf(DOCINFO)
    mDI\lpszDocName = @Doc
    mDI\lpszOutput = 0
    StartDoc_(PrinterDC,mDI)
EndProcedure

Procedure PrintRichText(hWnd, hInst, rtfEdit, LM, RM, TM, BM)
 
   fr.FORMATRANGE
   pd.PRINTDLG
   
   pd\lStructSize = SizeOf(PRINTDLG)
   pd\hwndOwner = hWnd
   pd\hDevMode = 0
   pd\hDevNames = 0
   pd\nFromPage = 0
   pd\nToPage = 0
   pd\nMinPage = 0
   pd\nMaxPage = 0
   pd\nCopies = 0
   pd\hInstance = hInst
   pd\Flags = #PD_RETURNDC | #PD_PRINTSETUP
   pd\lpfnSetupHook = 0
   pd\lpSetupTemplateName = 0
   pd\lpfnPrintHook = 0
   pd\lpPrintTemplateName = 0
   
   If PrintDlg_(pd)
     b = GlobalLock_(pd\hDevNames)       
     drv.s  = PeekS(b + PeekW(b))
     name.s = PeekS(b + PeekW(b + 2))
     PrinterDC = DefaultPrinter()
       
     fr\hdc = PrinterDC
     fr\hdcTarget = PrinterDC
     fr\chrg\cpMin = 0
     fr\chrg\cpMax = -1
     fr\rc\top = TM*1440
     fr\rcPage\top = fr\rc\top
     fr\rc\left = LM*1440
     fr\rcPage\left = fr\rc\left
 
     iWidthTwips = Int((GetDeviceCaps_(PrinterDC, #HORZRES)/GetDeviceCaps_(PrinterDC, #LOGPIXELSX))*1440)
     iHeightTwips = Int((GetDeviceCaps_(PrinterDC, #VERTRES )/GetDeviceCaps_(PrinterDC, #LOGPIXELSY))*1440)
     fr\rc\right = iWidthTwips-RM*1440
     fr\rcPage\right = fr\rc\right
     fr\rc\Bottom = iHeightTwips-BM*1440
     fr\rcPage\bottom = fr\rc\bottom
     
     StartPrint("RTF Printing")
     StartPage_(PrinterDC)

     iTextOut = 0
     iTextAmt = SendMessage_(rtfEdit, #WM_GETTEXTLENGTH, 0, 0)
      While iTextOut<iTextAmt
         iTextOut = SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, fr)           
         If iTextOut<iTextAmt                   
            EndPage_(PrinterDC)
            StartPage_(PrinterDC)
            fr\chrg\cpMin = iTextOut
            fr\chrg\cpMax = -1 
            iTextAmt = iTextAmt - iTextOut
            iTextOut = 0
         EndIf
      Wend
     SendMessage_(rtfEdit, #EM_FORMATRANGE, 1, 0)
     EndPage_(PrinterDC)
     EndDoc_(PrinterDC)
     DeleteDC_(PrinterDC)
  EndIf
EndProcedure

Procedure StreamFileInCallback(hFile, pbBuff, cb, pcb)
  ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure

Procedure loadFile(pFilePath.s)
  If ReadFile(0, pFilePath)
    If GetExtensionPart(pFilePath)="rtf"
      uFormat = #SF_RTF
    Else
      uFormat = #SF_TEXT
    EndIf
    edstr.EDITSTREAM
    edstr\dwCookie = FileID(0)
    edstr\dwError = 0
    edstr\pfnCallback = @StreamFileInCallback()
    SendMessage_(GadgetID(0), #EM_STREAMIN, uFormat, edstr)
    CloseFile(0)
  Else
    MessageRequester("Error", "Error Occured While Opening File", #PB_MessageRequester_Ok)
  EndIf
EndProcedure

OpenWindow(0, 200, 50, 640, 400,"RTF PRINTING", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateMenu(0, WindowID(0))
  MenuTitle("&File")
    MenuItem(0, "&Open")
    MenuItem(1, "&Print")
    MenuItem(2, "&Quit")
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(StringByteLength(a, #PB_Ascii) + 1)
PokeS(*MemoryBuffer, a.s, -1, #PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
      Select EventMenu()
        Case 0
          FileName$ = OpenFileRequester("", "", "All files|*.*", 0)
          If FileName$
            loadFile(FileName$)
          EndIf
          
        Case 1
          PrintRichText(WindowID(0), GetModuleHandle_(0), GadgetID(0), 0, 0, 0, 0)
          
        Case 2
          Quit = 1
          
      EndSelect
  EndSelect 
Until Quit = 1
Woohoo!!! Can't wait to try this, RASHAD, thank you once again. By the way, we still have to finish that other thing:):):)

I compiled the latest form of my recipe code with some new things in it and haven't heard back from anyone so tell me if you want a copy of the source.

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 10:28 am
by Keya
btw is RTF (or any non-HTML/simplified "rich text") generally a Windows-only thing? or is there something similar for OSX/Linux?

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 10:39 am
by RSBasic
RTF (rich text format) developed by Microsoft.
EditorGadget uses RICHEDIT_CLASS in Windows and GtkTextView in Linux.
Overview: http://www.purebasic.fr/blog/?p=336

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 10:50 am
by Keya
and to finish the lineup "UserPane (rendering a TNXObject)" for OSX, from your link
sounds like itd require 3 code bases for "generalized multiOS richtext support"? i might stick to plain text for now :)

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 11:19 am
by wilbert
Keya wrote:and to finish the lineup "UserPane (rendering a TNXObject)" for OSX, from your link
sounds like itd require 3 code bases for "generalized multiOS richtext support"? i might stick to plain text for now :)
The OSX object mentioned in that thread was for Carbon, not Cocoa.
But OSX supports rtf also. There are some examples in the Cocoa tips & tricks thread.

Re: Print rtf formatted text from editorgadget?

Posted: Fri May 20, 2016 11:20 am
by Keya
ah ok cool! i will definitely have to check them out. thankyou wilbert and RSBasic and have a great weekend

Re: Print rtf formatted text from editorgadget?

Posted: Sat May 21, 2016 9:14 am
by Shardik
RSBasic wrote:Overview: http://www.purebasic.fr/blog/?p=336
Unfortunately the PB GUI objects table in freak's blog posting from 2010 is outdated. For Mac OS X only the underlying API objects of the Carbon framework (the last PB version to support it as a subsystem was 5.11) are shown and for Linux the underlying GUI objects for GTK3 (default since PB 5.40) are missing.

For those needing to know the underlying Mac OS X GUI objects for the Cocoa framework I have extended freak's original table in this posting.