With something like the following you can get a bitmap of a scrolled
window and put this into a device context. Your not limited to
drawing the bitmap on the device context, but could draw other things
as well. Take a look at the wxpython demo.
#!/usr/bin/env python
printScrolledWindow.py
“”"
printScrolledWindow.py
Prints a scrolled window.
Inputs: ScrolledWindow
Ouputs: Printout
“”"
from wxPython.wx import *
from math import *
def getBitmapFromScrolledPanel(panel):
xScroll,yScroll = panel.GetScrollPixelsPerUnit()
panel.SetScrollRate(1,1)
xSource = ySource = xDest = yDest = 0.0
visibleWidth, visibleHeight = panel.GetSizeTuple()
virtualWidth, virtualHeight = panel.GetVirtualSize()
bm = wxEmptyBitmap(virtualWidth, virtualHeight)
tempDC = wxMemoryDC()
tempDC.SelectObject(bm)
visibleWidth = visibleWidth - 17
visibleHeight = visibleHeight - 17
xUnits = visibleWidth
yUnits = visibleHeight
#print "XUNITS: " + str(xUnits)
#print "YUNITS: " + str(yUnits)
xSegments = int(ceil(float(virtualWidth)/float(xUnits)))
ySegments = int(ceil(float(virtualHeight)/float(yUnits)))
#print "XSEGMENTS: " + str(xSegments)
#print "YSEGMENTS: " + str(ySegments)
for x in range(xSegments):
if ((x * xUnits) + xUnits) < virtualWidth:
xDest = xUnits * x
else:
xDest = xDest + (virtualWidth - xUnits)
panel.Scroll(xDest, -1)
#print "XDEST: " + str(xDest)
for y in range(ySegments):
if ((y * yUnits) + yUnits) < virtualHeight:
yDest = yUnits * y
else:
yDest = (yUnits * y) - ((yUnits * (y + 1)) - virtualHeight)
panel.Scroll(-1, yDest)
#print "YDEST: " + str(yDest)
panel.Update()
panelDC = wxClientDC(panel)
tempDC.Blit(xDest, yDest, visibleWidth, visibleHeight, panelDC,
xSource, ySource)
panel.SetScrollRate(xScroll, yScroll)
#print bm.GetWidth()
#print bm.GetHeight()
#print panel.GetVirtualSize()
return bm
class scrolledPrintout(wxPrintout):
def init(self, panel, title):
self.bitmap = getBitmapFromScrolledPanel(panel)
wxPrintout.init(self, title)
def OnPrintPage(self, pgno):
dc = self.GetDC()
dc.StartDoc("Start")
dc.StartPage()
printerWidth, printerHeight = dc.GetSizeTuple()
bmWidth = self.bitmap.GetWidth()
bmHeight = self.bitmap.GetHeight()
scaleWidth = float(printerWidth/bmWidth)
scaleHeight = float(printerHeight/bmHeight)
dc.SetUserScale(min(scaleWidth, scaleHeight), min(scaleWidth, scaleHeight))
dc.DrawBitmap(self.bitmap, 0,0)
#bm.SaveFile("aBitmap.bmp", wxBITMAP_TYPE_BMP )
dc.EndPage()
dc.EndDoc()
def OnPrintPage(self, pgno):
dc = self.GetDC()
···
#-------------------------------------------
# One possible method of setting scaling factors...
maxX = self.bitmap.GetWidth()
maxY = self.bitmap.GetHeight()
# Let's have at least 50 device units margin
marginX = 15
marginY = 15
# Add the margin to the graphic size
maxX = maxX + (2 * marginX)
maxY = maxY + (2 * marginY)
# Get the size of the DC in pixels
(w, h) = dc.GetSizeTuple()
# Calculate a suitable scaling factor
scaleX = float(w) / maxX
scaleY = float(h) / maxY
# Use x or y scaling factor, whichever fits on the DC
actualScale = min(scaleX, scaleY)
# Calculate the position on the DC for centering the graphic
posX = (w - (self.bitmap.GetWidth() * actualScale)) / 2.0
posY = (h - (self.bitmap.GetHeight() * actualScale)) / 2.0
# Set the scale and origin
dc.SetUserScale(actualScale, actualScale)
dc.SetDeviceOrigin(int(posX), int(posY))
#-------------------------------------------
dc.DrawBitmap(self.bitmap, 0,0)
dc.DrawText("Page: %d" % pgno, marginX/2, maxY-marginY)
return True
def GetPageInfo(self):
return (1, 1, 1, 1)
On 10/2/05, Timothy Smith timothy@open-networks.net wrote:
Matt Kubilus wrote:
Simply layout your graph in whatever format looks good in a scrolled
window. Then capture the device context of that frame, put it into a
printerDC (i’m assuming you can do this with a postscriptDC as well).
Then convert the postscript file into a pdf. You can put just about
anything into a device context. I could probably dig up a sample when
I get to my desk on monday.
–Matt
that would be great
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org
–
Don’t be a pioneeer. A pioneer is the guy with the arrow through his chest. – John J. Rakos