import wx

# This dialog works with the printout classes
#in conjunction with this event handler method and variables in your Frame instance
# self.PrintType=None
# self.copies=1 
#~ def OnPrint(self, evt):
	#~ if self.bmp:
		#~ global image_rotated
		#~ dialog=PrintSizeDialog(self, -1, 'Print Size')
		#~ ImageRotated=0
		#~ self.OnPrintPreview(None)
	#~ else:
		#~ wx.MessageBox("You can't print an image when there isn't one loaded!", 'No Image')
# I bound this to a menu item, but it could be bound to pratically anything
		
class PrintSizeDialog (wx.Dialog):
	def __init__(self, parent, id, title):
		wx.Dialog.__init__(self, parent, id, title, size=(250, 200))
		self.copies=0
		options=['Full Page','Half Page','Wallet Prints']
		self.parent = parent
		self.combo=wx.ComboBox(self, -1, size=(150, -1), choices=options, style= wx.CB_READONLY)
		copies_text=wx.StaticText(self, -1, "Number of Copies:")
		self.sc = wx.SpinCtrl(self, -1, "",)
		self.sc.SetRange(1,100)
		self.sc.SetValue(1)
		
		static_box=wx.StaticBox(self, -1, 'Size')
		static_box_sizer=wx.StaticBoxSizer(static_box, wx.VERTICAL)
		#static_box=wx.StaticBox(self, -1, 'Size')
		#static_box_sizer.Add(static_box, 2, 5)
		static_box_sizer.Add(wx.Size(10,10))
		static_box_sizer.Add(self.combo, 1,wx.ALIGN_CENTRE ,  -5)
		static_box_sizer.Add(copies_text,1,wx.ALIGN_CENTRE ,  5)
		static_box_sizer.Add(self.sc,1,wx.ALIGN_CENTRE ,  -5)
		static_box_sizer.Add(wx.Size(10,40)) 
		#static_box=wx.StaticBox(self, -1, 'Size')
		#static_box_sizer.Add(static_box, 2, 5)
		self.okay=wx.Button(self, 1, 'Ok')
		self.cancel=wx.Button(self, 1, 'Cancel')
		button_sizer=wx.BoxSizer(wx.HORIZONTAL)
		
		button_sizer.Add(self.okay, 0, 5)
		button_sizer.Add(self.cancel, 1, 5)
		box_sizer=wx.BoxSizer(wx.VERTICAL)
		box_sizer.Add(static_box_sizer, 0, wx.EXPAND, 5)
		box_sizer.Add(button_sizer, 1,wx.BOTTOM | wx.ALIGN_RIGHT,5)
		self.SetSizer(box_sizer)
		
		self.sc.Bind(wx.EVT_SPINCTRL, self.OnSpin)
		self.printList= [FullImagePrint, HalfPagePrint, WalletImagePrint]
		self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
		self.okay.Bind(wx.EVT_BUTTON, self.OnOkay)
		self.cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
		self.printType=None
		self.copynum=1
		self.Centre()
		self.ShowModal()
		self.Destroy()
		
	def OnSelect(self, evt):
		selected=evt.GetSelection()
		self.printType=self.printList[selected]
		
	def OnSpin(self, evt):
		self.copynum=self.sc.GetValue()
		
	def OnOkay(self,evt):
		self.parent.PrintType=self.printType
		self.parent.copies=self.copynum
		self.Close()
		
	def OnCancel(self,evt):
		self.Close()
		
		
		
		


# Here are the Printout subclasses
class FullImagePrint(wx.Printout):
		def __init__(self, canvas,prints=1):
			wx.Printout.__init__(self)
			self.canvas=canvas
			self.prints=prints
		def OnBeginDocument(self, start, end):
			return super(FullImagePrint, self).OnBeginDocument(start, end)
			
		def OnEndDocument(self):
			super(FullImagePrint, self).OnEndDocument()
			
		def OnBeginPrinting(self):
			super(FullImagePrint, self).OnBeginPrinting()
			
		def OnEndPrinting(self):
			super(FullImagePrint, self).OnEndPrinting()
		
		def OnPreparePrinting(self):
			super(FullImagePrint, self).OnPreparePrinting()
			
		def HasPage(self, page):
			if page<=self.prints:
				return True
			else:
				return False
			
		def GetPageInfo(self):
			return (1,self.prints,1,self.prints)
			
		def OnPrintPage(self, page):
			if not self.canvas.GetHeight() > self.canvas.GetWidth():
				image=wx.Bitmap.ConvertToImage(self.canvas)
				image=image.Rotate90()
				self.canvas=image.ConvertToBitmap()
			self.dc=self.GetDC()

			maxX ,maxY =self.canvas.GetSize()

			# Let's have at least 50 device units margin
			marginX = 50
			marginY = 50

			# 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 = self.dc.GetSize()

			# 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.canvas.GetSize()[0] * actualScale))/7.5
			posY = (h - (self.canvas.GetSize()[1] * actualScale))/30

			# Set the scale and origin
			self.dc.SetUserScale(actualScale, actualScale)
			self.dc.SetDeviceOrigin(int(posX), int(posY))

			#-------------------------------------------

			self.dc.DrawBitmap(self.canvas,posX,posY, False)
			

			return True
			
class WalletImagePrint(wx.Printout):
	def __init__(self, canvas, prints=1):
		wx.Printout.__init__(self)
		self.canvas=canvas
		self.prints=prints
		self.pages=0
		self.rotated=0
	def OnBeginDocument(self, start, end):
		return super(WalletImagePrint, self).OnBeginDocument(start, end)
		
	def OnEndDocument(self):
		super(WalletImagePrint, self).OnEndDocument()
		
	def OnBeginPrinting(self):
		super(WalletImagePrint, self).OnBeginPrinting()
		
	def OnEndPrinting(self):
		super(WalletImagePrint, self).OnEndPrinting()
	
	def OnPreparePrinting(self):
		super(WalletImagePrint, self).OnPreparePrinting()
		
	def HasPage(self, page):

		if page<=self.pages:
			return True
		else: 
			return False
	def GetPageInfo(self):
		if self.prints>9:
			pdiv=self.prints/9
			psub=pdiv*9
			pages=self.prints-psub
			if pages:
				self.pages=pdiv+1
			else:
				self.pages=pdiv
		return (1,self.pages,1,self.pages)	
	def OnPrintPage(self, page):
		
		if not self.canvas.GetHeight() > self.canvas.GetWidth():
			image=wx.Bitmap.ConvertToImage(self.canvas)
			image=image.Rotate90()
			self.canvas=image.ConvertToBitmap()
		self.dc=self.GetDC()
		posDivrX=1.4
		posDivrY=32
		maxX ,maxY =self.canvas.GetSize()

		# Let's have at least 50 device units margin
		marginX = 0
		marginY = 0

		# 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 = self.dc.GetSize()

		# Calculate a suitable scaling factor
		scaleX = float(w) / (maxX*3.8)
		scaleY = float(h) / (maxY*3.5)

		# Use x or y scaling factor, whichever fits on the DC
		actualScale = min(scaleX, scaleY)
		if page==1:
			if self.prints>9:
				prints=9
			else: 
				prints=self.prints
		else:
			pdiv=self.prints/9
			psub=self.prints-(pdiv*9)
			if psub:
				prints=psub
			else:
				prints=9
		for x in range(prints):
			
			if x==3:
				posDivrX= 2.5
				
				
			if x== 6:
				posDivrX=10
				
				
			# Calculate the position on the DC for centering the graphic
			posX = (w - (self.canvas.GetSize()[0] * actualScale))/posDivrX
			posY = (h - (self.canvas.GetSize()[1] * actualScale))/posDivrY

			# Set the scale and origin
			self.dc.SetUserScale(actualScale, actualScale)
			self.dc.SetDeviceOrigin(int(posX), int(posY))

			#-------------------------------------------

			self.dc.DrawBitmap(self.canvas,posX,posY, False)
			if posDivrY==32:
				posDivrY= 2.5
			elif posDivrY== 2.5:
				posDivrY= 1.25
			elif posDivrY== 1.25:
				posDivrY= 32
		return True
class HalfPagePrint(wx.Printout):
	def __init__(self, canvas, prints=1):
		wx.Printout.__init__(self)
		self.canvas=canvas
		self.prints=prints
		self.pages=0
		self.rotated=0
	def OnBeginDocument(self, start, end):
		return super(HalfPagePrint, self).OnBeginDocument(start, end)
		
	def OnEndDocument(self):
		super(HalfPagePrint, self).OnEndDocument()
		
	def OnBeginPrinting(self):
		super(HalfPagePrint, self).OnBeginPrinting()
		
	def OnEndPrinting(self):
		super(HalfPagePrint, self).OnEndPrinting()
	
	def OnPreparePrinting(self):
		super(HalfPagePrint, self).OnPreparePrinting()
		
	def HasPage(self, page):

		if page<=self.pages:
			return True
		else: 
			return False
	def GetPageInfo(self):
		if self.prints>2:
			pdiv=self.prints/2
			psub=pdiv*2
			pages=self.prints-psub
			if pages:
				self.pages=pdiv+1
			else:
				self.pages=pdiv
		return (1,self.pages,1,self.pages)	
	def OnPrintPage(self, page):
		if not self.canvas.GetHeight() < self.canvas.GetWidth():
			image=wx.Bitmap.ConvertToImage(self.canvas)
			image=image.Rotate90()
			self.canvas=image.ConvertToBitmap()

		self.dc=self.GetDC()
		# Get the size of the DC in pixels
		w, h = self.dc.GetSize()
		#set various pos and size variables
		posDivrX=3
		posDivrY=32
		maxX ,maxY =self.canvas.GetSize()
		

		# Let's have at least 0 device units margin
		marginX =0
		marginY = 0

		# Add the margin to the graphic size
		maxX = maxX + (2 * marginX)
		maxY = maxY + (2 * marginY)

		

		# Calculate a suitable scaling factor
		scaleX = float(w) / (maxX*1)
		scaleY = float(h) / (maxY*2)

		# Use x or y scaling factor, whichever fits on the DC
		actualScale = min(scaleX, scaleY)
		if page==1:
			if self.prints>2:
				prints=2
			else: 
				prints=self.prints
		else:
			pdiv=self.prints/2
			psub=self.prints-(pdiv*2)
			if psub:
				prints=psub
			else:
				prints=2
		for x in range(prints):
			if x:
				posDivrY=1.5
			# Calculate the position on the DC for centering the graphic
			posX = (w - (self.canvas.GetSize()[0] * actualScale))/posDivrX
			posY = (h - (self.canvas.GetSize()[1] * actualScale))/posDivrY

			# Set the scale and origin
			self.dc.SetUserScale(actualScale, actualScale)
			self.dc.SetDeviceOrigin(int(posX), int(posY))

			#-------------------------------------------

			self.dc.DrawBitmap(self.canvas,posX,posY, False)

		return True