Printer_Lib110 Question

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Printer_Lib110 Question

Post by Columbo »

Where do you place the PrinterLib110 folder in order to use it? Also perhaps, a small example of how to use it to print the contents of an EditorGadget?

Thank you.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Printer_Lib110 Question

Post by Columbo »

Nobody ever used this Lib? I simply want to know how send the contents of an EditorGadget to the default printer.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Printer_Lib110 Question

Post by firace »

Not familiar with this lib, but perhaps this topic will be helpful:

viewtopic.php?f=13&t=56975
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Printer_Lib110 Question

Post by Marc56us »

This lib has not been maintained for at least 7 years. I don't know if it's still working.
I've tried, it doesn't work anymore, you'd have to update a lot of things.
Try Example 8 "Example08 Drawtext.pb"
Printer_Lib.pb
Line 979: Structure field not found: dmOrientation.
( *PrinterParameters\dmOrientation=value)

We are now using PB's internal libs Printer and VectorDrawing

:wink:
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Printer_Lib110 Question

Post by Columbo »

Thanks for the responses. As mentioned above, I want to print the contents of the EditorGadget which will contain a code snippet. I tried playing a bit with the example in PB and while I got it to send the gadget text to the printer, it is printing the entire code list on 1 line and, of course, it runs off the page so I get only a part of the code. I can't figure out how to get it to print the code line by line as it is in the EditorGadget.

Here is what I tried:

Code: Select all


Procedure printer()
  gadgetString = GetGadgetText(#editor)
  
  If PrintRequester()       

  If StartPrinting(gadgetString)
  
    LoadFont(0, "Arial", 65)
    LoadFont(1, "Arial", 65)    
  
    If StartDrawing(PrinterOutput())
      
      BackColor(RGB(255, 255, 255)) ; Uses white as back color, usuful when printing on a white sheet
      FrontColor(RGB(0, 0, 0)) ; Use black for standard text color
      
      DrawingFont(FontID(0))
      DrawText(500, 500, gadgetString)     
        
      StopDrawing()
    EndIf
    
    StopPrinting()
  EndIf
EndIf  
EndProcedure
I would appreciate some guidance on this if possible.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Printer_Lib110 Question

Post by srod »

Assuming you just wish to print each line of text in the same font (as an Editor gadget can use multiple fonts via Rich Text) then you will need to draw/print each line separately.

You will need to check the text height (use TextHeight()) and use this to place each successive line of text. As you print, keep an eye on the page height (use PrinterPageHeight() or OutputHeight()) and issue a NewPrinterPage() when you run out of space on one page etc.

This should take care of basic printing of the unformatted text.

Now, I do seem to recall (a long time ago) seeing code in these forums which would print the rich text content of an Editor gadget, but I have no idea where it might be lurking!
I may look like a mule, but I'm not a complete ass.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Printer_Lib110 Question

Post by Columbo »

Thanks srod. I'm not familiar with TextHeight(), PrinterPageHeight() or OutputHeight() but I'll give that a try. I know how to read a text file line by line but how do you read a gadget's contents line by line?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Printer_Lib110 Question

Post by srod »

GetGadgetItemText() will do the job.
I may look like a mule, but I'm not a complete ass.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Printer_Lib110 Question

Post by Columbo »

Here is what I tried but it is still printing on 1 line.

Code: Select all


Procedure printer()
  lineNum = 0
  lineCount = CountGadgetItems(#editor)
  gadgetString = ""
  
  If PrintRequester()       

  If StartPrinting(gadgetString)
  
    LoadFont(0, "Arial", 65)
    LoadFont(1, "Arial", 65)    
  
    If StartDrawing(PrinterOutput())
      
      BackColor(RGB(255, 255, 255)) ; Uses white as back color, usuful when printing on a white sheet
      FrontColor(RGB(0, 0, 0)) ; Use black for standard text color
      
      DrawingFont(FontID(0))
      For lineNum = 0 To lineCount
        line = GetGadgetItemText(#editor, lineNum)
        DrawText(500, 500, line)  
      Next lineNum
        
      StopDrawing()
    EndIf
    
    StopPrinting()
  EndIf
EndIf  
EndProcedure

If I place a Debug line right after "line = GetGadgetItemText(#editor, lineNum)" I get this in the debug window (which is what I want):

Code: Select all


Procedure displayImages()
  filename1 = dbselected
  filename2 = dbselected
  
  path = GetPathPart(ProgramFilename())  
  imagePath1 = path + "dino_images\" + filename1 + ".jpg"
  imagePath2 = path + "dinosize\" + filename2 + "_size" + ".jpg" 
  
  LoadImage(1,imagePath1)
  LoadImage(2,imagePath2)
 PanelGadget(10, 885, 210, 355, 280)
   AddGadgetItem(10, -1, "Dinosaur Picture", 0)
    ImageGadget(1, 15, 10, 425, 260, ImageID(1))
   AddGadgetItem(10, -1, "Size Comaprison", 0)
    ImageGadget(2, 15, 10, 425, 260, ImageID(2))
    CloseGadgetList()
    
  SetGadgetState(1, ImageID(1))
SetGadgetState(2, ImageID(2))
   
EndProcedure

So, in the debug window it looks great but when printing, it almost looks like it prints 1 line on top of another.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4944
Joined: Sun Apr 12, 2009 6:27 am

Re: Printer_Lib110 Question

Post by RASHAD »

Use Drop down Menu
- Load
- Cut , Copy & Paste
- Save as RTF
- Save as TEXT
- Print

Code: Select all

#PD_ALLPAGES = $00000000
#PD_COLLATE = $10
#PD_DISABLEPRINTTOFILE = $80000
#PD_ENABLEPRINTHOOK = $1000
#PD_ENABLEPRINTTEMPLATE = $4000
#PD_ENABLEPRINTTEMPLATEHANDLE = $10000
#PD_ENABLESETUPHOOK = $2000
#PD_ENABLESETUPTEMPLATE = $8000
#PD_ENABLESETUPTEMPLATEHANDLE = $20000
#PD_HIDEPRINTTOFILE = $100000
#PD_NONETWORKBUTTON = $200000
#PD_NOPAGENUMS = $8
#PD_NOSELECTION = $4
#PD_NOWARNING = $80
#PD_PAGENUMS = $2
#PD_PRINTSETUP = $40
#PD_PRINTTOFILE = $20
#PD_RETURNDC = $100
#PD_RETURNDEFAULT = $400
#PD_RETURNIC = $200
#PD_SELECTION = $1
#PD_SHOWHELP = $800
#PD_USEDEVMODECOPIES = $40000
#PD_USEDEVMODECOPIESANDCOLLATE = $40000

Global HwndEditText,PrinterDC,EStream.EDITSTREAM

Procedure Sel_Text(Gadget, LineStart.l, CharStart.l, LineEnd.l, CharEnd.l)
  sel.CHARRANGE
  sel\cpMin = SendMessage_(HwndEditText, #EM_LINEINDEX, LineStart, 0) + CharStart - 1

  If LineEnd = -1
    LineEnd = SendMessage_(HwndEditText, #EM_GETLINECOUNT, 0, 0)-1
  EndIf
  sel\cpMax = SendMessage_(HwndEditText, #EM_LINEINDEX, LineEnd, 0)

  If CharEnd = -1
    sel\cpMax + SendMessage_(HwndEditText, #EM_LINELENGTH, sel\cpMax, 0)
  Else
    sel\cpMax + CharEnd - 1
  EndIf
  SendMessage_(HwndEditText, #EM_EXSETSEL, 0, @sel)
EndProcedure

Procedure Sel_FontName(Gadget, FontName.s)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_FACE
  PokeS(@format\szFaceName, FontName)
  SendMessage_(HwndEditText, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

Procedure Sel_FontColor(Gadget, Color)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_COLOR
  format\crTextColor = Color
  SendMessage_(HwndEditText, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

Procedure Sel_FontSize(Gadget, Fontsize)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_SIZE
  format\yHeight = FontSize*20
  SendMessage_(HwndEditText, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

Procedure Sel_CharFormat(Gadget, Flags)
  format.CHARFORMAT
  format\cbSize = SizeOf(CHARFORMAT)
  format\dwMask = #CFM_ITALIC|#CFM_BOLD|#CFM_STRIKEOUT|#CFM_UNDERLINE
  format\dwEffects = Flags
  SendMessage_(HwndEditText, #EM_SETCHARFORMAT, #SCF_SELECTION, @format)
EndProcedure

Procedure SetMargin(Gad, PageW,PageH, LM, TM, RM, BM)
  r.RECT
  r\left = LM
  r\top = TM
  r\right = PageW - RM
  r\bottom = PageH - BM 
  SendMessage_(HwndEditText, #EM_SETRECTNP, 0, r)
EndProcedure

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

Procedure PrintRichText(hWnd, hInst, rtfEdit, LM, TM, RM, BM)
  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_HIDEPRINTTOFILE | #PD_NONETWORKBUTTON |#PD_RETURNDC | #PD_PRINTSETUP
  pd\lpfnSetupHook = 0
  pd\lpSetupTemplateName = 0
  pd\lpfnPrintHook = 0
  pd\lpPrintTemplateName = 0
   
  If PrintDlg_(pd)
    PrinterDC = pd\hDC
  Else
    ProcedureReturn 1
  EndIf
 
  If PrinterDC
    cxPhysOffset = GetDeviceCaps_(PrinterDC, #PHYSICALOFFSETX)
    cyPhysOffset = GetDeviceCaps_(PrinterDC, #PHYSICALOFFSETY)
   
    cxPhys = GetDeviceCaps_(PrinterDC, #PHYSICALWIDTH)
    cyPhys = GetDeviceCaps_(PrinterDC, #PHYSICALHEIGHT)
    SendMessage_(rtfEdit, #EM_SETTARGETDEVICE, PrinterDC, cxPhys*20)

    fr.FORMATRANGE       
    fr\hdc = PrinterDC
    fr\hdcTarget = PrinterDC
   
    fr\chrg\cpMin = 0
    fr\chrg\cpMax = -1
       
    fr\rcPage\left = 0
    fr\rcPage\top = 0
    fr\rcpage\right = 0
    fr\rcPage\bottom = 0
   
    fr\rc\left   = LM*20
    fr\rc\top    = TM*20
    fr\rc\right  = cxPhys * 1440/ GetDeviceCaps_(PrinterDC, #LOGPIXELSX)- RM*20
    fr\rc\Bottom = cyPhys * 1440/ GetDeviceCaps_(PrinterDC, #LOGPIXELSY)- BM*20
   
    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, 0, 0)
    EndPage_(PrinterDC)
    EndDoc_(PrinterDC)
    DeleteDC_(PrinterDC)
  EndIf
EndProcedure
  
Procedure StreamInCallback(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 = @StreamInCallback()
    SendMessage_(HwndEditText, #EM_STREAMIN, uFormat, edstr)
    CloseFile(0)
  Else
    MessageRequester("Error", "Error Occured While Opening File", #PB_MessageRequester_Ok)
  EndIf
EndProcedure
; 
Procedure StreamOutCallback(dwCookie, *pbBuff, cb, *pcb.Long)    
  WriteData(dwCookie, *pbBuff, cb)
  *pcb\l = cb    
  ProcedureReturn 0
EndProcedure

Procedure SaveRTF( gad,FileName.s)
  Protected.EDITSTREAM stream    
  With stream
    \dwCookie = CreateFile(#PB_Any, FileName)
    If \dwCookie
      \pfnCallback = @StreamOutCallback()
      SendMessage_(HwndEditText, #EM_STREAMOUT, #SF_RTF, @stream)
      CloseFile(\dwCookie)
      SendMessage_(HwndEditText, #EM_EMPTYUNDOBUFFER, 0, 0) 
    EndIf
  EndWith
EndProcedure

Procedure SaveText(gad, FileName.s)
  Protected.EDITSTREAM stream
  Protected Flag.l = #SF_TEXT    
  CompilerIf #PB_Compiler_Unicode : Flag | #SF_UNICODE : CompilerEndIf    
  With stream
    \dwCookie = CreateFile(#PB_Any, FileName)
    If \dwCookie
      \pfnCallback = @StreamOutCallback()
      SendMessage_(HwndEditText, #EM_STREAMOUT, Flag, @stream)
      CloseFile(\dwCookie)
      SendMessage_(HwndEditText, #EM_EMPTYUNDOBUFFER, 0, 0) 
    EndIf
  EndWith
EndProcedure

Procedure WndProc(hWnd, uMsg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  Select uMsg
    Case #WM_SIZE
      MoveWindow_( HwndEditText,10,10,WindowWidth(0)-20,WindowHeight(0)-50,1)
      MoveWindow_( GadgetID(1),10,WindowHeight(0)-30,80,20,1)      
 
  EndSelect
  ProcedureReturn result
EndProcedure

Procedure CreateRTFEdit(x,y,w,h)  

  hInstance = GetModuleHandle_(0) 
   
  If OpenLibrary(0,"msftedit.dll")
    Class_Name$ = "RichEdit50W"
  ElseIf OpenLibrary(0,"Riched20.dll") And Class_Name$ = ""
    Class_Name$ = "RichEdit20W"
  ElseIf OpenLibrary(0,"Riched32.dll") And Class_Name$ = ""
    Class_Name$ = "RichEdit"
  Else
    MessageRequester("Error","Sorry ,RichEdit Can not be created",#MB_ICONWARNING)
  EndIf  
  HwndEditText = CreateWindowEx_(#WS_EX_STATICEDGE,Class_Name$,"", #WS_VISIBLE | #WS_CHILDWINDOW | #WS_HSCROLL  | #WS_VSCROLL | #ES_MULTILINE | #ES_NOHIDESEL | #ES_WANTRETURN, x,y,w,h,WindowID(0),300,hInstance,0) 
  SendMessage_(HwndEditText,#EM_SETLIMITTEXT,$7FFFFFFE,0)
EndProcedure

OpenWindow(0, 200, 50, 640, 400,"RTF Load,Save & Print", #PB_Window_SystemMenu|#PB_Window_ScreenCentered |  #PB_Window_SizeGadget)
  CreatePopupImageMenu(0,#PB_Menu_ModernLook) 
    MenuItem(0, "&Open")
    OpenSubMenu("&Save As..")
      MenuItem( 1, "RTF File")
      MenuItem( 2, "Plain Text")
    CloseSubMenu()
    MenuBar() 
    MenuItem(3, "&Cut")
    MenuItem(4, "&Copy")
    MenuItem(5, "&Paste")
    MenuBar()
    MenuItem(6, "&Print")
    MenuBar()
    MenuItem(7, "&Quit")
    
CreateRTFEdit(10, 10, 620,350)     
;EditorGadget(0, 10, 10, 620,350)
ButtonGadget(1,10,370,80,20,"Add Text")
;SendMessage_(HwndEditText, #EM_LIMITTEXT, -1, 0)

 a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
 a=a+"{\*\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(Len(a.s)+1)
 PokeS(*MemoryBuffer, a.s, Len(a.s)+1,#PB_Ascii)  
 SendMessage_(HwndEditText,#EM_REPLACESEL,0,PeekS(*MemoryBuffer ,Len(a.s)+1,#PB_Unicode))   

SetWindowCallback(@WndProc())
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
     
    Case #PB_Event_Menu
      Select EventMenu()
        Case 0
          FileName$ = OpenFileRequester("", "", "RTF (*.rtf|*.rtf|Text (*.txt)|*.txt;*.bat|All files (*.*)|*.*", 0)
          If FileName$
            loadFile(FileName$)
          EndIf
          
        Case 1
            FileName$ = SaveFileRequester("", "", "RTF (*.rtf|*.rtf|All files (*.*)|*.*", 0)
            If FileName$            
              SaveRTF( HwndEditText,  FileName$)
            EndIf
            
        Case 2
            FileName$ = SaveFileRequester("", "", "Text (*.txt)|*.txt|All files (*.*)|*.*", 0)
            If FileName$
              SaveText(HwndEditText,  FileName$)
            EndIf
            
        Case 3
            SendMessage_(HwndEditText,#WM_CUT,0,0)
            
        Case 4
            ClearClipboard()
            SendMessage_(HwndEditText,#WM_COPY,0,0)
              
        Case 5
            While WindowEvent(): Wend
            SendMessage_(HwndEditText,#WM_PASTE,0,0)
         
        Case 6
            PrintRichText(WindowID(0), GetModuleHandle_(0), HwndEditText, 10, 10, 20, 20)
         
        Case 7
            Quit = 1          
      EndSelect
      
    Case #WM_RBUTTONDOWN
      DisplayPopupMenu(0, WindowID(0))
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          
      EndSelect
  EndSelect
Until Quit = 1
DestroyWindow_(HwndEditText)  
CloseLibrary(0)
End  
Egypt my love
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Printer_Lib110 Question

Post by srod »

Columbo, you are drawing each line in exactly the same position which is why each line is being printed atop the previous!

Code: Select all

For lineNum = 0 To lineCount
  line = GetGadgetItemText(#editor, lineNum)
  DrawText(500, 500, line) 
Next lineNum
You are placing each line of text 500 pixels across, 500 pixels down!

By the way, you want to change the 'To' clause to :

Code: Select all

For lineNum = 0 To lineCount-1
Rashad has posted code which should print formatted text for you, but if you want just basic output using PB's printer lib then something like the following should do (untested) :

Code: Select all

Procedure printer()
  lineNum = 0
  lineCount = CountGadgetItems(#editor)
  gadgetString = ""
 
  If PrintRequester()       

  If StartPrinting(gadgetString)
 
    LoadFont(0, "Arial", 65)
    LoadFont(1, "Arial", 65)   
 
    If StartDrawing(PrinterOutput())
      
      BackColor(RGB(255, 255, 255)) ; Uses white as back color, usuful when printing on a white sheet
      FrontColor(RGB(0, 0, 0)) ; Use black for standard text color
     
      DrawingFont(FontID(0))
      textHeight = TextHeight("A")
      pageHeight = OutputHeight()
      vPos = 0
      For lineNum = 0 To lineCount-1
        ;Do we need an extra page?
          If vPos + textHeight >= pageHeight
            vPos = 0
            NewPrinterPage()
          EndIf
        line.s = GetGadgetItemText(#editor, lineNum)
        DrawText(50, vPos, line)
        vPos + TextHeight
      Next
      StopDrawing()
    EndIf
   
    StopPrinting()
  EndIf
EndIf 
EndProcedure
I may look like a mule, but I'm not a complete ass.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Printer_Lib110 Question

Post by Columbo »

Thanks RASHAD all I need is to press a button and have it print the contents of the EditorGadget.

Thanks srod. Your code does exactly what I wanted.

Thank you for you time and patience.

Cheers!
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
Post Reply