Code: Select all
;############################################
;Content
;EditorGadget print on a standard printer
;
;Author : Andreas
;June 2003
;extended by Wichtel and Joro
;
;all formatting in the editorgadget will be
;printed
;############################################
Global GadgetNr,GadgetNr_hWnd
#Margin_left=80
#Margin_right=81
#Margin_top=82
#Margin_bottom=83
Structure textlength
flags.w
codepage.b
EndStructure
;#######################################################################
;the 2 Procedures are based on El_Choni's example
Procedure StreamFileIn_Callback(hFile, pbBuff, cb, pcb)
ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure
Procedure FileStreamIn(FileID.l, File.s, Gadget.l,Option.l)
Protected File
Protected FileID
Protected Gadget
Protected StreamData.EDITSTREAM
If ReadFile(FileID, File)
StreamData\dwCookie = UseFile(FileID)
StreamData\dwError = #Null
StreamData\pfnCallback = @StreamFileIn_Callback()
SendMessage_(GadgetNr_hWnd, #EM_STREAMIN, Option, @StreamData)
CloseFile(FileID)
EndIf
EndProcedure
;#######################################################################
;structure to set the documentproperties
Structure DInfo1
cbSize.l
lpszDocname.l
lpszOutput.l
lpszDataType.l
fwType.w
EndStructure
Procedure PrintWindow()
WindowWidth = 200
WindowHeight = 40
If OpenWindow(10, 0, 0, WindowWidth, WindowHeight, #PB_Window_WindowCentered, "Printing")
SetWindowPos_(WindowID(),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE)
CreateGadgetList(WindowID(10))
TextGadget(0, 10, 10, 180, 20,"printing page")
EndIf
EndProcedure
Procedure EditorPrint(document.s)
Protected DC ,a
lppd.PRINTDLGAPI; structure for the Api printer selection
lppd\lStructsize=SizeOf(PRINTDLGAPI)
lppd\flags=#PD_ALLPAGES| #PD_HIDEPRINTTOFILE | #PD_NOSELECTION | #PD_RETURNDC |#PD_PRINTSETUP
PrintDlg_(lppd); Printerselection API
DC=lppd\hdc; here the device context is coming back
If DC<>-1
mydevmode.DEVMODE
*Pointer.DEVMODE
*Pointer=GlobalLock_(lppd\hdevmode)
paperlength=*Pointer\dmPaperlength
paperwidth=*Pointer\dmPaperwidth
ppmm.f=(*Pointer\dmYResolution/10)/2.54 ; pixel per millimeter
GlobalFree_(hdevmode)
cRect.RECT
FormatRange.FORMATRANGE
Docinfo.DInfo1
Docinfo\cbSize = SizeOf(DInfo1)
Docinfo\lpszDocname = @document.s
Docinfo\lpszOutput = #Null
Docinfo\lpszDataType = #Null
Docinfo\fwType = 0
PrintWindow()
SetGadgetText(0, "preparing for printing")
Result=StartDoc_(DC,Docinfo)
LastChar = 0
leftmargin=GetGadgetState(#Margin_left)
rightmargin=GetGadgetState(#Margin_right)
topmargin=GetGadgetState(#Margin_top)
bottommargin=GetGadgetState(#Margin_bottom)
;ActivateGadget(GadgetNr); seems that it sometimes crashes without activating , you can try to leave it
long.textlength
long\flags=0
MaxLen=SendMessage_(GadgetID(GadgetNr),#EM_GETTEXTLENGTHEX ,@long,0)
OldMapMode = GetMapMode_(DC)
SetMapMode_(DC,#MM_TWIPS)
OffsetX = leftmargin*ppmm.f-GetDeviceCaps_(DC,#PHYSICALOFFSETX)
If OffsetX < GetDeviceCaps_(DC,#PHYSICALOFFSETX):OffsetX=GetDeviceCaps_(DC,#PHYSICALOFFSETX):EndIf
OffsetY=-topmargin*ppmm.f+GetDeviceCaps_(DC,#PHYSICALOFFSETY)
If OffsetY > -GetDeviceCaps_(DC,#PHYSICALOFFSETY): OffsetY = -GetDeviceCaps_(DC,#PHYSICALOFFSETY):EndIf
HorzRes=((paperwidth/10-rightmargin))*ppmm.f
If HorzRes > GetDeviceCaps_(DC,#HORZRES) :HorzRes=GetDeviceCaps_(DC,#HORZRES):EndIf
VertRes=-(paperlength/10)*ppmm.f+bottommargin*ppmm.f
If VertRes < -GetDeviceCaps_(DC,#VERTRES):VertRes=-GetDeviceCaps_(DC,#VERTRES):EndIf
SetRect_(cRect,OffsetX,OffsetY,HorzRes,VertRes)
DPtoLP_(DC,cRect,2)
SetMapMode_(DC,OldMapMode)
FormatRange\hdc = DC
FormatRange\hdcTarget = DC
FormatRange\rc\left = cRect\left
FormatRange\rc\top = cRect\top
FormatRange\rc\right = cRect\right
FormatRange\rc\bottom = cRect\bottom
FormatRange\rcPage\left = cRect\left
FormatRange\rcPage\top = cRect\top
FormatRange\rcPage\right = cRect\right
FormatRange\rcPage\bottom = cRect\bottom
a = 1
Repeat
SetGadgetText(0, "Printing page : "+ Str(a))
Result=StartPage_(DC)
FormatRange\chrg\cpMax = -1
LastChar = SendMessage_(GadgetNr_hWnd,#EM_FORMATRANGE,#True,@FormatRange)
FormatRange\chrg\cpMin = LastChar
SendMessage_(GadgetNr_hWnd,#EM_DISPLAYBAND,0,cRect)
a + 1
EndPage_(DC)
Until LastChar >= MaxLen Or LastChar = -1
SetGadgetText(0, "Fertig")
CloseWindow(10)
EndDoc_(DC)
SendMessage_(GadgetNr_hWnd,#EM_FORMATRANGE,0,0)
EndIf
EndProcedure
If OpenWindow(0, 10, 10, 640, 480, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "PureBasic Window")
If CreateGadgetList(WindowID())
GadgetNr_hWnd=EditorGadget(1, 0, 0, WindowWidth()-100,WindowHeight())
GadgetNr=1
ButtonGadget(2, WindowWidth()-90,10,80,24,"Print")
ButtonGadget(3, WindowWidth()-90,40,80,24,"Open document")
TextGadget(10,WindowWidth()-100,95, 100, 20,"margin in mm")
SpinGadget(#Margin_left, WindowWidth()-90,150, 50, 20, 0, 100)
SetGadgetState(#Margin_left,20)
SetGadgetText(#Margin_left,"0")
TextGadget(11,WindowWidth()-90,125, 80, 20,"left")
SpinGadget(#Margin_right, WindowWidth()-90,200, 50, 20, 0, 100)
SetGadgetState(#Margin_right,20)
SetGadgetText(#Margin_right,"0")
TextGadget(12,WindowWidth()-90,175, 80, 20,"right")
SpinGadget(#Margin_top, WindowWidth()-90,250, 50, 20, 0, 100)
SetGadgetState(#Margin_top,20)
SetGadgetText(#Margin_top,"0")
TextGadget(13,WindowWidth()-90,225, 80, 20,"top")
SpinGadget(#Margin_bottom, WindowWidth()-90,300, 50, 20, 0, 100)
SetGadgetState(#Margin_bottom,20)
SetGadgetText(#Margin_bottom,"0")
TextGadget(14,WindowWidth()-90,275, 80, 20,"bottom")
EndIf
Repeat
EventID.l = WaitWindowEvent()
If EventID = #PB_Event_CloseWindow
Quit = 1
EndIf
If EventID = #PB_EventGadget
Select EventGadgetID()
Case 2
EditorPrint(DName$)
Case 3
DName$ = OpenFileRequester("Open File :","*.txt", "Text-Files ( *.txt) | *.txt | Rich Text Files (*.rtf)|*.rtf", 0)
If UCase(GetExtensionPart(DName$)) = "RTF"
FileStreamIn(0,DName$, 1,#SF_RTF)
Else
FileStreamIn(0,DName$, 1,#SF_TEXT)
EndIf
Case #Margin_left
If spinc=2
spinc=0
Else
spin=GetGadgetState(#Margin_left)
spinc=spinc+1
SetGadgetText(#Margin_left,Str(spin))
WindowEvent()
EndIf
Case #Margin_right
If spinc=2
spinc=0
Else
spin=GetGadgetState(#Margin_right)
spinc=spinc+1
SetGadgetText(#Margin_right,Str(spin))
WindowEvent()
EndIf
Case #Margin_top
If spinc=2
spinc=0
Else
spin=GetGadgetState(#Margin_top)
spinc=spinc+1
SetGadgetText(#Margin_top,Str(spin))
WindowEvent()
EndIf
Case #Margin_bottom
If spinc=2
spinc=0
Else
spin=GetGadgetState(#Margin_bottom)
spinc=spinc+1
SetGadgetText(#Margin_bottom,Str(spin))
WindowEvent()
EndIf
EndSelect
EndIf
Until Quit = 1
EndIf
End