reportlab/platypus/doctemplate.py
author rgbecker
Fri, 12 May 2000 14:45:31 +0000
changeset 199 7de4e498ce32
parent 197 b8ca098f5ec7
child 200 74da573e661f
permissions -rw-r--r--
Single actions only in ActionFlowables
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     1
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     2
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     3
#	ReportLab Public License Version 1.0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     4
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     5
#   Except for the change of names the spirit and intention of this
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     6
#   license is the same as that of Python
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     7
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     9
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    10
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    11
# All Rights Reserved
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    12
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    16
# copyright notice and this permission notice appear in supporting
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    17
# documentation, and that the name of ReportLab not be used
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    19
# without specific, written prior permission. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    20
# 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    21
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    22
# Disclaimer
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    23
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    31
#
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    32
###############################################################################
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    33
#	$Log: doctemplate.py,v $
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    34
#	Revision 1.2  2000/05/12 14:45:31  rgbecker
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    35
#	Single actions only in ActionFlowables
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    36
#
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    37
#	Revision 1.1  2000/05/12 12:53:33  rgbecker
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    38
#	Initial try at a document template class
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    39
#	
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    40
__version__=''' $Id: doctemplate.py,v 1.2 2000/05/12 14:45:31 rgbecker Exp $ '''
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    41
__doc__="""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    42
More complicated Document model
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    43
"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    44
from layout import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    45
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    46
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    47
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    48
class ActionFlowable(Flowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    49
	'''This Flowable is never drawn, it can be used for data driven controls'''
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    50
	def __init__(self,action=[]):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    51
		if type(action) not in (ListType, TupleType):
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    52
			action = (action,)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    53
		self.action = action
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    54
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    55
	def wrap(self, availWidth, availHeight):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    56
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    57
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    58
	def draw(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    59
		raise NotImplementedError
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    60
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    61
	def apply(self,doc):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    62
		action = self.action[0]
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    63
		args = tuple(self.action[1:])
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    64
		try:
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    65
			apply(getattr(doc,'handle_'+action), args)
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    66
		except AttributeError:
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    67
			raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    68
		except:
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    69
			t, v, None = sys.exc_info()
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    70
			raise t, "%s\n   handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    71
				
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    72
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    73
FrameBreak = ActionFlowable('frameBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    74
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    75
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    76
class NextPageTemplate(ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    77
	def __init__(self,pt):
199
7de4e498ce32 Single actions only in ActionFlowables
rgbecker
parents: 197
diff changeset
    78
		ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    79
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    80
class PageTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    81
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    82
	essentially a list of BasicFrames and an onPage routine to call at the start
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    83
	of a page when this is selected.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    84
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    85
	def __init__(self,id=None,frames=[],onPage=None):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    86
		if type(frames) not in (ListType,TupleType): frames = [frames]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    87
		assert filter(lambda x: not isinstance(x,BasicFrame), frames)==[], "frames argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    88
		self.id = id
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    89
		self.frames = frames
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    90
		self.onPage = onPage or _doNothing
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    91
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    92
class BaseDocTemplate:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    93
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    94
	First attempt at defining a document template class.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    95
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    96
	The basic idea is simple.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    97
	0)	The document has a list of data associated with it
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    98
		this data should derive from flowables. We'll have
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    99
		special classes like PageBreak, FrameBreak to do things
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   100
		like forcing a page end etc.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   101
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   102
	1)	The document has one or more page templates.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   103
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   104
	2)	Each page template has one or more frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   105
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   106
	3)	The document class provides base methods for handling the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   107
		story events and some reasonable methods for getting the
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   108
		story flowables into the frames.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   109
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   110
	4)	The document instances can override the base handler routines.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   111
	"""
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   112
	def __init__(self, filename, pagesize=DEFAULT_PAGE_SIZE, pageTemplates=[], showBoundary=0,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   113
				leftMargin=inch, rightMargin=inch, topMargin=inch, bottomMargin=inch):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   114
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   115
		self.pageTemplates = []
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   116
		self.addPageTemplates(pageTemplates)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   117
		self.filename = filename
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   118
		self.showBoundary = showBoundary
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   119
		self.leftMargin =  leftMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   120
		self.bottomMargin = bottomMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   121
		self.rightMargin = pagesize[0] - rightMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   122
		self.topMargin = pagesize[1] - topMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   123
		self.width = self.rightMargin - self.leftMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   124
		self.height = self.topMargin - self.bottomMargin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   125
		self.pagesize = pagesize
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   126
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   127
	def clean_hanging(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   128
		while len(self._hanging):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   129
			self.handle_flowable(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   130
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   131
	def addPageTemplates(self,pageTemplates):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   132
		if type(pageTemplates) not in (ListType,TupleType):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   133
			pageTemplates = [pageTemplates]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   134
		assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   135
		for t in pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   136
			self.pageTemplates.append(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   137
			
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   138
	def handle_documentBegin(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   139
		self._hanging = [PageBegin]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   140
		self.pageTemplate = self.pageTemplates[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   141
		self.page = 0
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   142
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   143
	def handle_pageBegin(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   144
		'''shouldn't normally be called directly'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   145
		self.page = self.page + 1
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   146
		self.pageTemplate.onPage(self.canv,self)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   147
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   148
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   149
		self.frame = self.pageTemplate.frames[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   150
		self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   151
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   152
	def handle_pageEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   153
		'''	show the current page
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   154
			check the next page template
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   155
			hang a page begin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   156
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   157
		self.canv.showPage()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   158
		if hasattr(self,'_nextPageTemplateIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   159
			self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   160
			del self._nextPageTemplateIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   161
		self._hanging.append(PageBegin)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   162
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   163
	def handle_pageBreak(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   164
		'''some might choose not to end all the frames'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   165
		if 1:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   166
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   167
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   168
			n = len(self._hanging)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   169
			while len(self._hanging)==n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   170
				self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   171
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   172
	def handle_frameBegin(self,*args):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   173
		self.frame._reset()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   174
		if self.showBoundary:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   175
			self.canv.rect(
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   176
						self.frame.x1,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   177
						self.frame.y1,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   178
						self.frame.x2 - self.frame.x1,
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   179
						self.frame.y2 - self.frame.y1
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   180
						)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   181
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   182
	def handle_frameEnd(self):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   183
		'''	Handles the semantics of the end of a frame. This includes the selection of
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   184
			the next frame or if this is the last frame then invoke pageEnd.
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   185
		'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   186
		if hasattr(self,'_nextFrameIndex'):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   187
			frame = self.pageTemplate.frames[self._nextFrameIndex]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   188
			del self._nextFrameIndex
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   189
		elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   190
			self.handle_pageEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   191
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   192
			f = self.frame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   193
			self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   194
			self.handle_frameBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   195
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   196
	def handle_nextPageTemplate(self,pt):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   197
		'''On endPage chenge to the page template with name or index pt'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   198
		if type(pt) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   199
			for t in self.pageTemplates:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   200
				if t.id == pt:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   201
					self._nextPageTemplateIndex = self.pageTemplates.index(t)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   202
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   203
			raise ValueError, "can't find template('%s')"%pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   204
		elif type(pt) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   205
			self._nextPageTemplateIndex = pt
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   206
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   207
			raise TypeError, "argument pt should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   208
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   209
	def handle_nextFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   210
		'''On endFrame chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   211
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   212
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   213
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   214
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   215
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   216
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   217
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   218
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   219
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   220
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   221
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   222
	def handle_currentFrame(self,fx):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   223
		'''chenge to the frame with name or index fx'''
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   224
		if type(fx) is StringType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   225
			for f in self.pageTemplate.frames:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   226
				if f.id == fx:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   227
					self._nextFrameIndex = self.pageTemplate.frames.index(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   228
					return
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   229
			raise ValueError, "can't find frame('%s')"%fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   230
		elif type(fx) is IntType:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   231
			self._nextFrameIndex = fx
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   232
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   233
			raise TypeError, "argument fx should be string or integer"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   234
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   235
	def handle_flowable(self,flowables):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   236
		f = flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   237
		del flowables[0]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   238
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   239
		if isinstance(f,PageBreak):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   240
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   241
		elif isinstance(f,ActionFlowable):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   242
			f.apply(self)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   243
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   244
			#general case we have to do something
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   245
			if not self.frame.add(f, self.canv, trySplit=1):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   246
				# see if this is a splittable thing
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   247
				S = self.frame.split(f)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   248
				n = len(S)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   249
				if n:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   250
					for f in xrange(n):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   251
						flowables.insert(f,S[f])	# put split flowables back on the list
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   252
				else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   253
					flowables.insert(0,f)			# put the flowable back
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   254
					self.handle_frameEnd()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   255
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   256
	_handle_documentBegin = handle_documentBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   257
	_handle_pageBegin = handle_pageBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   258
	_handle_pageEnd = handle_pageEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   259
	_handle_frameBegin = handle_frameBegin
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   260
	_handle_frameEnd = handle_frameEnd
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   261
	_handle_flowable = handle_flowable
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   262
	_handle_nextPageTemplate = handle_nextPageTemplate
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   263
	_handle_currentFrame = handle_currentFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   264
	_handle_nextFrame = handle_nextFrame
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   265
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   266
	def build(self, flowables):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   267
		assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error"
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   268
		self.canv = canvas.Canvas(self.filename)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   269
		self.handle_documentBegin()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   270
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   271
		while len(flowables):
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   272
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   273
			self.handle_flowable(flowables)
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   274
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   275
		if self._hanging!=[] and self._hanging[-1] is PageBegin:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   276
			del self._hanging[-1]
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   277
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   278
		else:
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   279
			self.clean_hanging()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   280
			self.handle_pageBreak()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   281
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   282
		self.canv.save()
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   283
		del self.frame, self.pageTemplate