Just didn't do all I wanted.
Regards
cd
First an update on the global module App.pbi it now includes a cross platform procedure to enumerate printers on windows and MAC, Then a printing module and a small test programme.
This is now cross platform works on windows and MAC. Many thanks to wilbert for helping me overcome my ignorance on the MAC.
If anyone can fill in the Linux blanks please post on this thread and I will update this post. The blanks are in the module CDPrint.pbi
This is very much just version 1
First the Global Module "App.pbi"
Code: Select all
UsePNGImageDecoder()
DeclareModule App
;All OS's
Macro FileExists(filename)
Bool(FileSize(fileName) > -1)
EndMacro
Global PPmm.i ;Used to store Screen Dots Per mm
Global NewList Printers.s()
;Constants for Flags
#OkOnly = 1
#OkCancel = 2
#YesNo = 4
#YesNoCancel = 8
#InformationIcon = 16
#WarningIcon = 32
#StopIcon = 64
;Constants for return values
#MsgOk = 1
#MsgYes = 2
#MsgNo = 3
#MsgCancel = 4
;Some Colours
#ColWhite = $FFFFFF
DataSection
Info:
IncludeBinary "information.png" ;Add your own image
Warn:
IncludeBinary "warning.png" ;Add your own image
Stop:
IncludeBinary "stop.png" ;Add your own image
EndDataSection
;OS Specific details
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
#DefaultFolder = "/Volumes" ;For explorertreegadget etc
ImportC ""
CFArrayGetCount(theArray.i)
CFArrayGetValueAtIndex(theArray.i, idx.i)
PMPaperGetHeight(paper, *paperHeight.Double)
PMPaperGetWidth(paper, *paperWidth.Double)
PMPaperGetPPDPaperName(paper.i, *paperName)
PMPrinterGetName(printer.i)
PMPrinterGetPaperList(printer.i, *paperList)
PMPrinterIsDefault(printer.i)
PMServerCreatePrinterList(server.i, *printerList)
EndImport
CompilerCase #PB_OS_Linux
#DefaultFolder = "/" ;For explorertreegadget etc
CompilerCase #PB_OS_Windows
#DefaultFolder = "C:\" ;For explorertreegadget etc
CompilerEndSelect
;App Global procedures
Declare.i Message(Title.s,Msg.s,Flags.i)
Declare EnumeratePrinters()
EndDeclareModule
Module App
Procedure.i Message(Title.s,Msg.s,Flags.i)
Define This_Window.i,btn1,btn2,btn3,txtMsg,RetVal,IconImage,Testimg
This_Window = OpenWindow(#PB_Any, 0, 0, 300, 130, Title, #PB_Window_Tool | #PB_Window_ScreenCentered)
;Add the buttons
If Flags & #YesNoCancel > 0
btn1 = ButtonGadget(#PB_Any, 30, 90, 80, 30, "Yes")
btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "No")
btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Cancel")
ElseIf Flags & #OkCancel > 0
btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "Ok")
btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Cancel")
ElseIf Flags & #YesNo > 0
btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "Yes")
btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "No")
Else
btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Ok")
EndIf
;Add the message image
IconImage = ImageGadget(#PB_Any, 20, 20, 50, 50, 0)
If Flags & #InformationIcon > 0
CatchImage(0, ?Info)
If IsImage(0)
SetGadgetState(IconImage,ImageID(0))
EndIf
EndIf
If Flags & #WarningIcon > 0
CatchImage(Testimg, ?Warn)
SetGadgetState(IconImage,ImageID(0))
EndIf
If Flags & #StopIcon > 0
CatchImage(0, ?Stop)
SetGadgetState(IconImage,ImageID(0))
EndIf
;Add the main message text
txtMsg = TextGadget(#PB_Any, 70, 10, 220, 70, Msg)
;Make sure it stays on top
StickyWindow(This_Window,#True)
;Start of message handling loop
;Not to be mixed up with the main application loop
;Allways include code to close this window!
Repeat
Event = WaitWindowEvent()
Select EventWindow()
Case This_Window ;Messages for this window only all others discarded
Select Event
Case #PB_Event_Gadget
Select EventGadget()
;Each button has a different meaning depending on
;the type of message box
Case btn1
If Flags&#YesNoCancel > 0
RetVal = #MsgYes
EndIf
CloseWindow(This_Window)
Case btn2
If Flags&#YesNoCancel > 0
RetVal = #MsgNo
ElseIf Flags&#YesNo > 0
RetVal = #MsgYes
ElseIf Flags&#OkCancel > 0
RetVal = #MsgOk
EndIf
CloseWindow(This_Window)
Case btn3
If Flags&#YesNoCancel > 0 Or Flags&#OkCancel > 0
RetVal = #MsgCancel
ElseIf Flags&#YesNo > 0
RetVal = #MsgNo
Else
RetVal = #MsgOk
EndIf
CloseWindow(This_Window)
EndSelect ;Eventgadget
EndSelect ;Event
EndSelect ;Eventwindow
Until Not IsWindow(This_Window)
ProcedureReturn RetVal
EndProcedure
Procedure EnumeratePrinters()
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
PMServerCreatePrinterList(0, @printers)
printerCount = CFArrayGetCount(printers)
For i = 0 To printerCount - 1
printer = CFArrayGetValueAtIndex(printers, i)
printerName.s = PeekS(CocoaMessage(0, PMPrinterGetName(printer), "UTF8String"), -1, #PB_UTF8)
AddElement(Printers())
Printers() = PeekS(CocoaMessage(0, PMPrinterGetName(printer), "UTF8String"), -1, #PB_UTF8)
Next i
CompilerCase #PB_OS_Linux
;Not Defined Yet
CompilerCase #PB_OS_Windows
enum.l=#PRINTER_ENUM_LOCAL|#PRINTER_ENUM_CONNECTIONS
Protected buf,cnt,mem,bytes,cntret,size,i
If EnumPrinters_(enum,0,1,0,0,@buf,@cnt)=0
mem=AllocateMemory(buf)
If mem And EnumPrinters_(enum,0,1,mem,buf,@bytes,@cntret)
size=SizeOf(PRINTER_INFO_1)
If cntret
For i=0 To cntret-1
AddElement(Printers())
Printers() = PeekS(PeekL(mem+i*size+ 8))
Next
EndIf
FreeMemory(mem)
ProcedureReturn 1
EndIf
EndIf
CompilerEndSelect
EndProcedure
EndModule
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Print/Preview Module
;
; FileName CDPrint.pbi
;
;
; ------------------------------------------------------------
;
;UseJPEGImageDecoder()
DeclareModule CDPrint
;Page Variables This will change as I proceed to maybe an array of a page structure
Global PageHeight.i ;Height of Selected Page in mm
Global PageWidth.i ;Width of Selected Page in mm
Global TopMargin.i ;Top Margin In mm
Global LeftMargin.i ;Left Margin In mm
Global BottomMargin.i ;Bottom Margin In mm
Global RightMargin.i ;Right Margin In mm
;Experimenting with Orientation to attempt to allow a mix of portrait and Landscape in the same document
Global Orientation.i = -1 ;Portrait or Landscape
Global Preview.i
GPageHeight.i ;mm To work out Graphic scale factor
GPageWidth.i ;mm To work out Graphic scale factor
Orientation.i ;Portrait or Landscape for print routines
TPageHeight.i ;Points To work out Text scale factor
TPageWidth.i ;Points To work out Text scale factor
Declare CDPrintEvents(Event)
Declare.l open(JobName.s,PageHeight,PageWidth)
Declare ShowPage(PageID)
Declare AddPage(PageHeight,PageWidth)
Declare Finished()
Declare PrintLine(Startx,Starty,Endx,Endy,LineWidth)
Declare PrintBox(Topx,Topy,Bottomx,Bottomy,LineWidth)
Declare PrintText(Startx,Starty,Font.s,Size.i,Text.s)
Declare PrintImage(Image.l,Topx.i,Topy.i,Width.i,Height.i)
Declare.f GettextWidthmm(text.s,FName.s,FSize.f)
Declare.f GettextHeightmm(text.s,FName.s,FSize.f)
Declare Page_Setup()
EndDeclareModule
Module CDPrint
;Structure For Usefull Printer Information
Structure PInfo
TopMargin.i
LeftMargin.i
BottomMargin.i
RightMargin.i
Width.i
Height.i
UsableWidth.i
UsableHeight.i
EndStructure
Global PrinterInfo.PInfo
Global PreviewWindow.l
Global Dim PageNos.i(1)
Global PageNo.i
Global GraphicScale.f
Global TextScale.f
Global Result
;PageSetup Form Variables
Global frm_Printer, btn_Cancel, btn_Ok, cmb_Printers, cmb_PaperSize, frm_Margins, str_Top, str_Left, String_0, str_Bottom, str_Right, String_0_Copy3, img_Back, img_Page, opt_Portrait, opt_Landscape, frm_Orientation
Procedure GetPrinterInfo()
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_MacOS
;The vectordrawing functions print correctly on the MAC so simply set all to zero
PrinterInfo\Width = 0
PrinterInfo\Height = 0
PrinterInfo\UsableWidth = 0
PrinterInfo\UsableHeight = 0
PrinterInfo\TopMargin = 0
PrinterInfo\LeftMargin = 0
PrinterInfo\BottomMargin = 0
PrinterInfo\RightMargin = 0
CompilerCase #PB_OS_Linux
;Not Defined Yet
CompilerCase #PB_OS_Windows
Define HDPmm.d
Define VDPmm.d
printer_DC.l = StartDrawing(PrinterOutput())
If printer_DC
HDPmm = GetDeviceCaps_(printer_DC,#LOGPIXELSX) / 25.4
VDPmm = GetDeviceCaps_(printer_DC,#LOGPIXELSY) / 25.4
PrinterInfo\Width = GetDeviceCaps_(printer_DC,#PHYSICALWIDTH) / HDPmm
PrinterInfo\Height = GetDeviceCaps_(printer_DC,#PHYSICALHEIGHT) / VDPmm
PrinterInfo\UsableWidth = GetDeviceCaps_(printer_DC,#HORZSIZE) ; Returns mm
PrinterInfo\UsableHeight = GetDeviceCaps_(printer_DC,#VERTSIZE); Returns mm
PrinterInfo\TopMargin = GetDeviceCaps_(printer_DC,#PHYSICALOFFSETY) / VDPmm
PrinterInfo\LeftMargin = GetDeviceCaps_(printer_DC,#PHYSICALOFFSETX) / HDPmm
PrinterInfo\BottomMargin = PrinterInfo\Height - PrinterInfo\UsableHeight - PrinterInfo\TopMargin
PrinterInfo\RightMargin = PrinterInfo\Width - PrinterInfo\UsableWidth - PrinterInfo\LeftMargin
EndIf
StopDrawing()
CompilerEndSelect
EndProcedure
Procedure.f GettextWidthmm(text.s,FName.s,FSize.f)
LoadFont(0,FName.s, FSize.f) ;Load Font In Points
ASize.f = FSize.f * 0.352777778 ;Convert Font Points To mm
VectorFont(FontID(0), ASize.f ) ;Use Font In mm Size
ProcedureReturn VectorTextWidth(text.s,#PB_VectorText_Visible) ;Width of text In mm
EndProcedure
Procedure.f GettextHeightmm(text.s,FName.s,FSize.f)
LoadFont(0,FName.s, FSize.f) ;Load Font In Points
ASize.f = FSize.f * 0.352777778 ;Convert Font Points To mm
VectorFont(FontID(0), ASize.f) ;Use Font In mm Size
ProcedureReturn VectorTextHeight(text.s,#PB_VectorText_Visible) ;Height of text In mm
EndProcedure
Procedure ShowPage(PageID)
If CDPrint::Preview.i = 1
SetGadgetState(66,PageID + 1)
SetGadgetState(34, ImageID(PageNos.i(PageID)))
;Centre image gadget in window area
If GadgetHeight(34) > GadgetWidth(34)
Left = (500 - GadgetWidth(34)) /2
Top = 25
Else
Left = 0
Top = ((500-GadgetHeight(34)) /2 ) + 25
EndIf
ResizeGadget(34,Left,Top,#PB_Ignore,#PB_Ignore)
EndIf
EndProcedure
Procedure.l Open(JobName.s,PageHeight,PageWidth)
TPageHeight.i = PageHeight * 2.834645669 ;mm To Points
TPageWidth.i = PageWidth * 2.834645669
If PageHeight > GPageWidth.i
GraphicScale.f = 500/PageHeight
TextScale.f = 500/TPageHeight.i
Else
GraphicScale.f = 500/PageWidth
TextScale.f = 500/TPagewidth.i
EndIf
If Preview.i = 1 ;If preview mode open Preview Window
PreviewWindow.l = OpenWindow(#PB_Any, #PB_Ignore,#PB_Ignore, 500, 535, "Print Preview - " + JobName.s)
If PreviewWindow.l > 0
SpinGadget (66, 450, 0, 50, 25, 0, 1000,#PB_Spin_Numeric)
SetGadgetState (66, 1)
ImageGadget(34, 5, 5, 50, 50, 0,#PB_Image_Raised)
;Set Page Counter To Zero And Create first Page Image
PageNo.i = 0
PageNos.i(PageNo.i) = CreateImage(#PB_Any, PageWidth * GraphicScale.f,PageHeight * GraphicScale.f, 32,RGB(255,255,255))
ShowPage(PageNo.i)
StartVectorDrawing(ImageVectorOutput(PageNos.i(PageNo.i) ))
;Set all printer margins to zero for preview
PrinterInfo\TopMargin = 0
PrinterInfo\LeftMargin = 0
PrinterInfo\BottomMargin = 0
PrinterInfo\RightMargin = 0
ProcedureReturn PreviewWindow.l
Else
;Tell The User We Failed
ProcedureReturn 0
EndIf
Else
;Start Printing
PrintRequester()
GetPrinterInfo()
result = StartPrinting(JobName.s)
;Check Printer Paper Size and Orientation
StartVectorDrawing(PrinterVectorOutput(#PB_Unit_Millimeter))
If Orientation = 0
RotateCoordinates(0,0,-90)
TranslateCoordinates(-297, 0)
EndIf
ProcedureReturn result
EndIf
EndProcedure
Procedure AddPage(PageHeight,PageWidth)
If Preview.i = 1
;Increase Last Page Number
PageNo.i = PageNo.i + 1
;Increase Handle Array by one
ReDim PageNos.i(PageNo.i)
;Show new page number in spin gadget
SetGadgetState(66,PageNo.i + 1)
SetGadgetText(66, Str(PageNo.i + 1))
;Create the image for the page and store handle in array
PageNos.i(PageNo.i) = CreateImage(#PB_Any, PageWidth * GraphicScale.f,PageHeight * GraphicScale.f, 32,RGB(255,255,255))
StartVectorDrawing(ImageVectorOutput(PageNos.i(PageNo.i) ))
Else
;Printer New Page
NewPrinterPage()
If Orientation = 0
RotateCoordinates(0,0,-90)
TranslateCoordinates(-297, 0)
EndIf
EndIf
EndProcedure
Procedure PrintLine(Startx,Starty,Endx,Endy,LineWidth)
If Preview.i = 1
;Scaled To Fit Preview Window
MovePathCursor(Startx * GraphicScale.f, Starty * GraphicScale.f)
AddPathLine(Endx * GraphicScale.f, Endy * GraphicScale.f, #PB_Path_Default)
VectorSourceColor(RGBA(0, 0, 0, 255))
StrokePath(LineWidth * GraphicScale.f, #PB_Path_RoundCorner)
Else
;Print Routine No Scaling but printer hard margins
Startx = Startx - PrinterInfo\LeftMargin
Starty = Starty - PrinterInfo\TopMargin
Endx = Endx - PrinterInfo\LeftMargin
Endy = Endy - PrinterInfo\TopMargin
MovePathCursor(Startx, Starty)
AddPathLine(Endx, Endy, #PB_Path_Default)
VectorSourceColor(RGBA(0, 0, 0, 255))
StrokePath(LineWidth,#PB_Path_RoundCorner)
EndIf
EndProcedure
Procedure PrintBox(Topx,Topy,Bottomx,Bottomy,LineWidth)
If Preview.i = 1
;Scaled To Fit Preview Image
AddPathBox(Topx * GraphicScale.f, Topy * GraphicScale.f, (Bottomx - Topx) * GraphicScale.f, (Bottomy - Topy) * GraphicScale.f)
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(LineWidth * GraphicScale.f)
Else
;Print Routine. No Scaling
Topx = Topx - PrinterInfo\LeftMargin
Topy = Topy - PrinterInfo\TopMargin
Bottomx = Bottomx - PrinterInfo\LeftMargin
Bottomy = Bottomy - PrinterInfo\TopMargin
AddPathBox(Topx, Topy , (Bottomx - Topx), (Bottomy - Topy))
VectorSourceColor(RGBA(255, 0, 0, 255))
StrokePath(LineWidth)
EndIf
EndProcedure
Procedure PrintText(Startx,Starty,Font.s,Size.i,Text.s)
;Scaled To Fit Preview Image
If Preview.i = 1
LoadFont(0, Font.s , Size.i )
ASize.f = Size.i * 0.352777778 ;Convert Font Points To mm
MovePathCursor(Startx * GraphicScale.f, Starty * GraphicScale.f)
VectorFont(FontID(0), ASize.f * GraphicScale.f)
VectorSourceColor(RGBA(0, 0, 0, 255))
DrawVectorText(Text.s)
Else
;Print Routine. No Scaling
Startx = Startx - PrinterInfo\LeftMargin
Starty = Starty - PrinterInfo\TopMargin
LoadFont(0, Font.s , Size.i )
ASize.f = Size.i * 0.352777778 ;Convert Font Points To mm
VectorFont(FontID(0), ASize.f)
VectorSourceColor(RGBA(0, 0, 0, 255))
MovePathCursor(Startx, Starty)
DrawVectorText(Text.s)
EndIf
EndProcedure
Procedure PrintImage(Image.l,Topx.i,Topy.i,Width.i,Height.i)
If Preview.i = 1
MovePathCursor(Topx * GraphicScale.f, Topy * GraphicScale.f)
DrawVectorImage(ImageID(Image.l),100,Width.i * GraphicScale.f,Height.i * GraphicScale.f)
Else
;Print Routine
Topx = Topx - PrinterInfo\LeftMargin
Topy = Topy - PrinterInfo\TopMargin
MovePathCursor(Topx, Topy)
DrawVectorImage(ImageID(Image),255,Width,Height)
EndIf
EndProcedure
Procedure Finished()
If Preview.i = 0
StopVectorDrawing()
StopPrinting()
Else
StopVectorDrawing()
EndIf
EndProcedure
Procedure DrawPageImage()
;Calculte Scaling
If PageHeight > PageWidth
GraphicScale.f = 190/PageHeight
Else
GraphicScale.f = 190/PageWidth
EndIf
;Create Page Image
PageImage = CreateImage(#PB_Any,PageWidth.i * GraphicScale.f ,PageHeight.i* GraphicScale.f , 32,RGB(255,255,255))
;Draw the page image
If StartDrawing(ImageOutput(PageImage))
;Add Margin Lines
FrontColor(RGB(255,0,0)) ; Red lines For Margins
;Top Margin
x = Val(GetGadgetText(str_Top))
LineXY(0,x*GraphicScale.f,190,x * GraphicScale.f)
;Left Margin
x = Val(GetGadgetText(str_Left))
LineXY(x*GraphicScale.f,0,x * GraphicScale.f, 190)
;Bottom Margin
x = Val(GetGadgetText(str_Bottom))
LineXY(0,(PageHeight.i-x)*GraphicScale.f,190,(PageHeight.i-x) * GraphicScale.f)
;Right Margin
x = Val(GetGadgetText(str_Right))
LineXY((PageWidth.i-x)*GraphicScale.f,0,(PageWidth.i-x) * GraphicScale.f, 190)
StopDrawing()
EndIf
;Show Page Image
SetGadgetState(img_Page,ImageID(PageImage))
;Centre Page Image
If PageHeight.i > PageWidth.i
left.i = (190 -GadgetWidth(img_Page))/2
Else
top.i = (190 -GadgetHeight(img_Page))/2
EndIf
ResizeGadget(img_Page, left.i + 330, top.i + 10, #PB_Ignore, #PB_Ignore)
EndProcedure
Procedure Page_Setup()
;Open The Page Setup Window
wnd_PageSetup = OpenWindow(#PB_Any, x, y, 540, 260, "", #PB_Window_TitleBar | #PB_Window_Tool | #PB_Window_ScreenCentered)
FrameGadget(#PB_Any, 10, 10, 200, 100, " Select Printer/Paper ")
btn_Cancel = ButtonGadget(#PB_Any, 430, 220, 100, 30, "Cancel")
btn_Ok = ButtonGadget(#PB_Any, 320, 220, 100, 30, "Ok")
cmb_Printers = ComboBoxGadget(#PB_Any, 20, 40, 180, 20)
DisableGadget(cmb_Printers, 1) ;Enable after loading available printers
cmb_PaperSize = ComboBoxGadget(#PB_Any, 20, 70, 180, 20)
FrameGadget(#PB_Any, 10, 110, 310, 100, " Margins")
TextGadget(#PB_Any, 130, 140, 30, 20, "mm")
TextGadget(#PB_Any, 130, 170, 30, 20, "mm")
TextGadget(#PB_Any, 170, 140, 50, 20, "Left", #PB_Text_Right)
TextGadget(#PB_Any, 20, 140, 50, 20, "Top", #PB_Text_Right)
TextGadget(#PB_Any, 280, 140, 30, 20, "mm")
TextGadget(#PB_Any, 280, 170, 30, 20, "mm")
TextGadget(#PB_Any, 20, 170, 50, 20, "Bottom", #PB_Text_Right)
TextGadget(#PB_Any, 170, 170, 50, 20, "Right", #PB_Text_Right)
str_Top = StringGadget(#PB_Any, 80, 140, 40, 20, "")
str_Bottom = StringGadget(#PB_Any, 80, 170, 40, 20, "")
str_Left = StringGadget(#PB_Any, 230, 140, 40, 20, "")
str_Right = StringGadget(#PB_Any, 230, 170, 40, 20, "")
img_Back = ImageGadget(#PB_Any, 330, 10, 190, 190, 0, #PB_Image_Border)
img_Page = ImageGadget(#PB_Any, 390, 90, 80, 60, 0)
opt_Portrait = OptionGadget(#PB_Any, 230, 50, 80, 20, "Portrait")
opt_Landscape = OptionGadget(#PB_Any, 230, 80, 80, 20, "Landscape")
FrameGadget(#PB_Any, 220, 10, 100, 100, " Orientation ")
PageBImage = CreateImage(#PB_Any,190 ,190 , 32,RGB(0,0,0))
SetGadgetState(img_Back,ImageID(PageBImage))
App::EnumeratePrinters()
ForEach App::Printers() ; Process all the elements...
AddGadgetItem(cmb_Printers,-1,App::Printers())
Next
DisableGadget(cmb_Printers, 0)
;Test List Only Set On Printer Selection Not yet Available
AddGadgetItem(cmb_PaperSize,0,"A4")
AddGadgetItem(cmb_PaperSize,0,"Letter")
SetGadgetText(cmb_PaperSize,"A4")
;Default To A4
PageWidth.i = 210
PageHeight.i = 297
TopMargin = 15
LeftMargin = 15
BottomMargin = 15
RightMargin = 15
Orientation = 1
SetGadgetState(opt_Portrait,1)
SetGadgetState(opt_Landscape,0)
If PageHeight.i < PageWidth.i
temp.i = PageWidth.i
PageWidth.i = PageHeight.i
PageHeight.i = temp.i
EndIf
SetGadgetText(str_Top,Str(TopMargin.i))
SetGadgetText(str_Left,Str(LeftMargin.i))
SetGadgetText(str_Bottom,Str(BottomMargin.i))
SetGadgetText(str_Right,Str(RightMargin.i))
DrawPageImage()
Repeat
Event = WaitWindowEvent()
Select event
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case btn_Ok
;App::PageWidth.i = PageWidth.i
;App::PageHeight.i = PageHeight.i
;App::TopMargin.i = TopMargin.i
;App::LeftMargin.i = LeftMargin.i
;App::BottomMargin.i = BottomMargin.i
;App::RightMargin.i = RightMargin.i
;App::Orientation.i = Orientation.i
CloseWindow(wnd_PageSetup)
Break
Case btn_Cancel
CloseWindow(wnd_PageSetup)
Break
Case str_Top
TopMargin.i = Val(GetGadgetText(str_Top))
DrawPageImage()
Case str_Left
LeftMargin.i = Val(GetGadgetText(str_Left))
DrawPageImage()
Case str_Bottom
BottomMargin.i = Val(GetGadgetText(str_Bottom))
DrawPageImage()
Case str_Right
RightMargin.i = Val(GetGadgetText(str_Right))
DrawPageImage()
Case cmb_Printers
;Printer selection
;Load List Of Supported Page Sizes And Place In Pages ComboBox
;Not Yet Available
Case cmb_PaperSize
;Get selected page size in mm
Select GetGadgetText(cmb_PaperSize)
Case "A4"
PageWidth.i = 210
PageHeight.i= 297
Case "Letter"
PageWidth.i = 216
PageHeight.i= 279
EndSelect
DrawPageImage()
Case opt_Portrait
If GetGadgetState(opt_Portrait) = 1
; Orientation.i = 1
If PageHeight.i < PageWidth.i
temp.i = PageWidth.i
PageWidth.i = PageHeight.i
PageHeight.i = temp.i
EndIf
EndIf
;Redraw Page Image
DrawPageImage()
Case opt_Landscape
If GetGadgetState(opt_Landscape) = 1
; Orientation.i = 0
If PageWidth.i < PageHeight.i
temp.i = PageWidth.i
PageWidth.i =PageHeight.i
PageHeight.i = temp.i
EndIf
EndIf
;Redraw Page Image
DrawPageImage()
EndSelect
EndSelect
ForEver
EndProcedure
Procedure CDPrintEvents(Event)
If event = #PB_Event_CloseWindow
CloseWindow(PreviewWindow.l)
EndIf
If Event = #PB_Event_Gadget
Select EventGadget()
Case 66
SetGadgetText(66, Str(GetGadgetState(66)))
If GetGadgetState(66) > 0 And GetGadgetState(66) -1 <= PageNo.i
ShowPage(GetGadgetState(66) -1)
ElseIf GetGadgetState(66) < 1
;Show Last Page No or first page Number in gadget
SetGadgetState(66,1)
Else
SetGadgetState(66,PageNo.i + 1 )
EndIf
EndSelect
EndIf
EndProcedure
EndModule
Code: Select all
;***********************************************************************
;Simple testing programme for PurePrint module
;Only an example
;Run This code to get all the rest
;
;***********************************************************************
;Only uses .jpg images
UseJPEGImageDecoder()
;Ensure these are included
IncludeFile "App.pbi"
IncludeFile "CDPrint.pbi"
Structure Page
Height.i
Width.i
Orientation.i
TopMargin.i
LeftMargin.i
BottomMargin.i
RightMargin.i
EndStructure
Global Dim Pages.Page(1)
Global Window_0
Global PageHeight
Global PageWidth
Global TopMargin
Global LeftMargin
Global BottomMargin
Global RightMargin
Global PageSetup
Global image.l
Global btnPreview,btnPrint
Global Print.l
Global btnPageSetup, txt_PageHeight, txt_PageWidth, str_TopMargin, str_LeftMargin, str_BottomMargin, str_PageWidth, str_RightMargin, str_PageHeight, txt_TopMargin, txt_LeftMargin, txt_BottomMargin, txt_RightMargin, btn_Exit
;Open the test window
Window_0 = OpenWindow(#PB_Any, x, y, 250, 340, "", #PB_Window_SystemMenu)
btnPageSetup = ButtonGadget(#PB_Any, 20, 10, 100, 30, "Page Setup")
btnPreview = ButtonGadget(#PB_Any, 130, 10, 50, 30, "Preview")
DisableGadget(btnPreview, 1)
btnPrint = ButtonGadget(#PB_Any, 190, 10, 50, 30, "Print")
DisableGadget(btnPrint, 1)
btn_Exit = ButtonGadget(#PB_Any, 65, 280, 120, 40, "Quit")
txt_PageHeight = TextGadget(#PB_Any, 50, 60, 80, 20, "Page Height", #PB_Text_Right)
txt_PageWidth = TextGadget(#PB_Any, 60, 90, 70, 20, "Page Width", #PB_Text_Right)
str_TopMargin = StringGadget(#PB_Any, 140, 120, 60, 20, "")
str_LeftMargin = StringGadget(#PB_Any, 140, 150, 60, 20, "")
str_BottomMargin = StringGadget(#PB_Any, 140, 180, 60, 20, "")
str_PageWidth = StringGadget(#PB_Any, 140, 90, 60, 20, "")
str_RightMargin = StringGadget(#PB_Any, 140, 210, 60, 20, "")
str_PageHeight = StringGadget(#PB_Any, 140, 60, 60, 20, "")
txt_TopMargin = TextGadget(#PB_Any, 60, 120, 70, 20, "Top Margin", #PB_Text_Right)
txt_LeftMargin = TextGadget(#PB_Any, 50, 150, 80, 20, "Left Margin", #PB_Text_Right)
txt_BottomMargin = TextGadget(#PB_Any, 40, 180, 90, 20, "Bottom Margin", #PB_Text_Right)
txt_RightMargin = TextGadget(#PB_Any, 50, 210, 80, 20, "Right Margin", #PB_Text_Right)
str_Orientation = StringGadget(#PB_Any, 140, 240, 60, 20, "")
;Test Purposes Only normally set by your programme
ReDim Pages(2)
Pages(0)\Height = 297 ;A4 Page Height
Pages(0)\Width = 210 ;A4 Page Width
Pages(0)\Orientation = 1 ;Portrait
Pages(0)\TopMargin = 15
Pages(0)\LeftMargin = 15
Pages(0)\BottomMargin = 15
Pages(0)\RightMargin = 15
Pages(1)\Height = 297 ;A4 Page Height
Pages(1)\Width = 210 ;A4 Page Width
Pages(1)\Orientation = 0 ;Landscape
Pages(1)\TopMargin = 15
Pages(1)\LeftMargin = 15
Pages(1)\BottomMargin = 15
Pages(1)\RightMargin = 15
;Test Purposes Only
Procedure PrintPages()
;****************************************************
;Procedure just to test print a couple of pages
;Replace with your own procedure
;Normally is a loop through all document commands
;Checking height of page left etc
;Allways check as landscape has a smaller height
;
;Looking to add a Print which pages dialog at some
;point To allow users To choose which pages To print
; This is where the call to that dialog will go
;
;****************************************************
For i = 0 To 1 ;Just two pages to test
;Set Page Properties
TopMargin = Pages(i)\TopMargin
LeftMargin = Pages(i)\LeftMargin
BottomMargin = Pages(i)\BottomMargin
RightMargin = Pages(i)\RightMargin
If Pages(i)\Orientation = 1
CDPrint::Orientation = 1
PageHeight = Pages(i)\Height
PageWidth = Pages(i)\Width
Else
CDPrint::Orientation = 0
PageHeight = Pages(i)\Width
PageWidth = Pages(i)\Height
EndIf
;Check if Print Started If Not Start The Print Job
If Print.l = 0
Print.l = CDPrint::open("Test",PageHeight,PageWidth)
Else
CDPrint::AddPage(PageHeight,PageWidth)
EndIf
;Print A Border around the margins
CDPrint::PrintBox(LeftMargin,RightMargin ,PageWidth - RightMargin ,PageHeight - BottomMargin,1)
;Print A Line 3mm Thick
CDPrint::PrintLine(20,50,PageWidth -20,50,3)
;Print some text
FontName$ = "Arial" ; set initial font (could also be blank)
FontSize = 14 ; set initial size (could also be null)
Result = FontRequester(FontName$, FontSize, #PB_FontRequester_Effects)
CDPrint::PrintText(50,100,SelectedFontName(),SelectedFontSize(),"Just Testing Page "+ Str(i + 1))
;Print some Centered Text
text$ = "Just Testing Centered"
widthtext.i = CDPrint::GettextWidthmm(text$,SelectedFontName(),SelectedFontSize())
x.i = (PageWidth - LeftMargin - RightMargin - widthtext.i)/2
CDPrint::PrintText(x.i + LeftMargin,130,SelectedFontName(),SelectedFontSize(),"Just Testing Centered")
;Select image to print for this page
;Remember to add decoders needed for image type
image$ = OpenFileRequester("SELECT IMAGE","","All supported formats|*.bmp;*.jpg; *.jpeg; *.wmf; *.emf; *.png;*.tif;*.tiff;*.tga|TGA image (*.tga)|*.tga|TIF image (*.tif)|*.tif|TIFF image (*.tiff)|*.tiff|PNG image (*.png)|*.png|BMP image (*.bmp)|*.bmp|JPEG image (*.jpg;*.jpeg)|*.jpg;*.jpeg|GIF image (*.gif)|*.gif",0)
If image$ <> ""
Image.l=LoadImage(#PB_Any, image$)
EndIf
If Image.l > 0
; Remember ImageVectorOutput automatically scales the image so maintain aspect ratio
If ImageHeight(image.l) > ImageWidth(image.l)
adjustedheight = 50 * (ImageHeight(image.l)/ImageWidth(image.l))
adjustedwidth = adjustedheight * ImageWidth(image.l) / ImageHeight(image.l)
Else
adjustedwidth = 40 * (ImageWidth(image.l)/ImageHeight(image.l))
adjustedheight = adjustedwidth * ImageHeight(image.l)/ImageWidth(image.l)
EndIf
;Depending on the image you may have to use the adjusted height in page calculations
;The image is allways drawn starting from top left so the position of an adjusted image may need to be changed
;to provide bottom aligned image printing or as here to make sure the image prints on the page
Define Topy.i = 150
If (Topy + adjustedheight ) > (Pageheight - BottomMargin)
;Move the image up to just squeeze it on the page inside the margin
Topy = Topy - ((Topy + adjustedheight ) - (Pageheight - BottomMargin) )
EndIf
CDPrint::PrintImage(Image.l,40,Topy,adjustedwidth,adjustedheight)
EndIf
Next i
;Finish The Print Job
CDPrint::Finished()
EndProcedure
Repeat
Event = WaitWindowEvent()
If EventWindow() = Print.l
CDPrint::CDPrintEvents(event)
EndIf
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case btnPageSetup
CDPrint::Page_Setup()
SetGadgetText(str_PageHeight,Str(CDPrint::PageHeight))
SetGadgetText(str_PageWidth,Str(CDPrint::PageWidth))
SetGadgetText(str_TopMargin,Str(CDPrint::TopMargin))
SetGadgetText(str_LeftMargin,Str(CDPrint::LeftMargin))
SetGadgetText(str_BottomMargin,Str(CDPrint::BottomMargin))
SetGadgetText(str_RightMargin,Str(CDPrint::RightMargin))
If CDPrint::Orientation = 1
SetGadgetText(str_Orientation,"Portrait")
Else
SetGadgetText(str_Orientation,"Landscape")
EndIf
If CDPrint::PageHeight > 0 And CDPrint::PageWidth > 0
DisableGadget(btnPreview, 0)
DisableGadget(btnPrint, 0)
EndIf
;Set your page variables here
;e.g. Pages(1)\TopMargin = CDPrint::TopMargin etc
Case btnPreview
CDPrint::Preview.i = 1 ; Set to preview document
PrintPages()
CDPrint::ShowPage(0)
Case btnPrint
CDPrint::Preview.i = 0 ; Set to print document
PrintPages()
Case btn_Exit
End
EndSelect
EndSelect
Until EventWindow() = Window_0 And Event = #PB_Event_CloseWindow ; If the user has pressed on the window close button
End