reportlab/platypus/doctemplate.py
changeset 1324 3335a8e81e7b
parent 1268 158ffc339d38
child 1425 fa9f74f1a701
equal deleted inserted replaced
1323:4053387604f4 1324:3335a8e81e7b
     1 #copyright ReportLab Inc. 2000
     1 #copyright ReportLab Inc. 2000
     2 #see license.txt for license details
     2 #see license.txt for license details
     3 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab
     3 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab
     4 #$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.45 2001/10/01 09:58:42 rgbecker Exp $
     4 #$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.46 2001/10/10 10:08:20 rgbecker Exp $
     5 
     5 
     6 __version__=''' $Id: doctemplate.py,v 1.45 2001/10/01 09:58:42 rgbecker Exp $ '''
     6 __version__=''' $Id: doctemplate.py,v 1.46 2001/10/10 10:08:20 rgbecker Exp $ '''
     7 
     7 
     8 __doc__="""
     8 __doc__="""
     9 This module contains the core structure of platypus.
     9 This module contains the core structure of platypus.
    10 
    10 
    11 Platypus constructs documents.	Document styles are determined by DocumentTemplates.
    11 Platypus constructs documents.	Document styles are determined by DocumentTemplates.
    74 class ActionFlowable(Flowable):
    74 class ActionFlowable(Flowable):
    75 	'''This Flowable is never drawn, it can be used for data driven controls
    75 	'''This Flowable is never drawn, it can be used for data driven controls
    76 	   For example to change a page template (from one column to two, for example)
    76 	   For example to change a page template (from one column to two, for example)
    77 	   use NextPageTemplate which creates an ActionFlowable.
    77 	   use NextPageTemplate which creates an ActionFlowable.
    78 	'''
    78 	'''
    79 	def __init__(self,action=[]):
    79 	def __init__(self,action=()):
    80 		if type(action) not in (ListType, TupleType):
    80 		if type(action) not in (ListType, TupleType):
    81 			action = (action,)
    81 			action = (action,)
    82 		self.action = action
    82 		self.action = tuple(action)
    83 
    83 
    84 	def wrap(self, availWidth, availHeight):
    84 	def wrap(self, availWidth, availHeight):
    85 		'''Should never be called.'''
    85 		'''Should never be called.'''
    86 		raise NotImplementedError
    86 		raise NotImplementedError
    87 
    87 
   109 			raise t, "%s\n	 handle_%s args=%s"%(v,action,args)
   109 			raise t, "%s\n	 handle_%s args=%s"%(v,action,args)
   110 
   110 
   111 	def __call__(self):
   111 	def __call__(self):
   112 		return self
   112 		return self
   113 
   113 
   114 
   114 class NextFrameFlowable(ActionFlowable):
   115 FrameBreak = ActionFlowable('frameEnd')
   115 	def __init__(self,ix,resume=0):
       
   116 		ActionFlowable.__init__(self,('nextFrame',ix,resume))
       
   117 
       
   118 class CurrentFrameFlowable(ActionFlowable):
       
   119 	def __init__(self,ix,resume=0):
       
   120 		ActionFlowable.__init__(self,('currentFrame',ix,resume))
       
   121 
       
   122 class _FrameBreak(ActionFlowable):
       
   123 	'''
       
   124 	A special ActionFlowable that allows setting doc._nextFrameIndex
       
   125 
       
   126 	eg story.append(FrameBreak('mySpecialFrame'))
       
   127 	'''
       
   128 	def __call__(self,ix=None,resume=0):
       
   129 		r = self.__class__(self.action+(resume,))
       
   130 		r._ix = ix
       
   131 		return r
       
   132 
       
   133 	def apply(self,doc):
       
   134 		if getattr(self,'_ix',None): doc._nextFrameIndex = self._ix
       
   135 		ActionFlowable.apply(self,doc)
       
   136 
       
   137 FrameBreak = _FrameBreak('frameEnd')
   116 PageBegin = ActionFlowable('pageBegin')
   138 PageBegin = ActionFlowable('pageBegin')
   117 
   139 
   118 
   140 
   119 class NextPageTemplate(ActionFlowable):
   141 class NextPageTemplate(ActionFlowable):
   120 	"""When you get to the next page, use the template specified (change to two column, for example)  """
   142 	"""When you get to the next page, use the template specified (change to two column, for example)  """
   297 		shouldn't normally be called directly'''
   319 		shouldn't normally be called directly'''
   298 		self.page = self.page + 1
   320 		self.page = self.page + 1
   299 		self.pageTemplate.beforeDrawPage(self.canv,self)
   321 		self.pageTemplate.beforeDrawPage(self.canv,self)
   300 		self.pageTemplate.checkPageSize(self.canv,self)
   322 		self.pageTemplate.checkPageSize(self.canv,self)
   301 		self.pageTemplate.onPage(self.canv,self)
   323 		self.pageTemplate.onPage(self.canv,self)
       
   324 		for f in self.pageTemplate.frames: f._reset()
   302 		self.beforePage()
   325 		self.beforePage()
   303 		if hasattr(self,'_nextFrameIndex'):
   326 		if hasattr(self,'_nextFrameIndex'):
   304 			del self._nextFrameIndex
   327 			del self._nextFrameIndex
   305 		self.frame = self.pageTemplate.frames[0]
   328 		self.frame = self.pageTemplate.frames[0]
   306 		self.handle_frameBegin()
   329 		self.handle_frameBegin()
   326 		else:
   349 		else:
   327 			n = len(self._hanging)
   350 			n = len(self._hanging)
   328 			while len(self._hanging)==n:
   351 			while len(self._hanging)==n:
   329 				self.handle_frameEnd()
   352 				self.handle_frameEnd()
   330 
   353 
   331 	def handle_frameBegin(self,*args):
   354 	def handle_frameBegin(self,resume=0):
   332 		'''What to do at the beginning of a page'''
   355 		'''What to do at the beginning of a frame'''
   333 		self.frame._reset()
   356 		f = self.frame
   334 		if self.showBoundary or self.frame.showBoundary:
   357 		if f._atTop:
   335 			self.frame.drawBoundary(self.canv)
   358 			if self.showBoundary or self.frame.showBoundary:
   336 
   359 				self.frame.drawBoundary(self.canv)
   337 	def handle_frameEnd(self):
   360 
       
   361 	def handle_frameEnd(self,resume=0):
   338 		''' Handles the semantics of the end of a frame. This includes the selection of
   362 		''' Handles the semantics of the end of a frame. This includes the selection of
   339 			the next frame or if this is the last frame then invoke pageEnd.
   363 			the next frame or if this is the last frame then invoke pageEnd.
   340 		'''
   364 		'''
   341 		if hasattr(self,'_nextFrameIndex'):
   365 		if hasattr(self,'_nextFrameIndex'):
   342 			frame = self.pageTemplate.frames[self._nextFrameIndex]
   366 			frame = self.pageTemplate.frames[self._nextFrameIndex]
   343 			del self._nextFrameIndex
   367 			del self._nextFrameIndex
   344 			self.handle_frameBegin()
   368 			self.handle_frameBegin(resume)
   345 		elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
   369 		elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
   346 			self.handle_pageEnd()
   370 			self.handle_pageEnd()
   347 			self.frame = None
   371 			self.frame = None
   348 		else:
   372 		else:
   349 			f = self.frame
   373 			f = self.frame