Print rtf formatted text from editorgadget?
Posted: Fri May 20, 2016 4:06 am
Is there a way (simple, like me) to print rtf formatted text from editorgadget?
http://www.purebasic.com
https://www.purebasic.fr/english/
That won't work. The idea is to print from the PB application which has an editorgadget that has rtf formatted text in it.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.
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
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
Not familiar with EMF. Does it convert in memory then print it?javabean wrote:How about this: http://www.purebasic.fr/english/viewtop ... 12&t=41547
Woohoo!!! Can't wait to try this, RASHAD, thank you once again. By the way, we still have to finish that other thing:):):)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
The OSX object mentioned in that thread was for Carbon, not Cocoa.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
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.RSBasic wrote:Overview: http://www.purebasic.fr/blog/?p=336