]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/printout.py
944da991b66382ab5b1507af9121f08cf610e1eb
[wxWidgets.git] / wxPython / wxPython / lib / printout.py
1 #----------------------------------------------------------------------------
2 # Name: printout.py
3 # Purpose: preview and printing class -> table/grid printing
4 #
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
6 #
7 # Created:
8 # Version 0.72
9 # Date: Sept 8, 2001
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
12
13 import os, sys, string, copy
14
15 from wxPython.wx import *
16 import copy
17
18 class PrintBase:
19 def OutTextRegion(self, textout, txtdraw = TRUE):
20 textlines = string.splitfields(textout, '\n')
21 y = copy.copy(self.y) + self.pt_space_before
22 for text in textlines:
23 remain = 'X'
24 while remain != "":
25 vout, remain = self.SetFlow(text, self.region)
26 if self.draw == TRUE and txtdraw == TRUE:
27 test_out = self.TestFull(vout)
28 if self.align == wxALIGN_LEFT:
29 self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
30
31 elif self.align == wxALIGN_CENTRE:
32 diff = self.GetCellDiff(test_out, self.region)
33 self.DC.DrawText(test_out, self.indent+diff/2, y)
34
35 elif self.align == wxALIGN_RIGHT:
36 diff = self.GetCellDiff(test_out, self.region)
37 self.DC.DrawText(test_out, self.indent+diff, y)
38
39 else:
40 self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
41 text = remain
42 y = y + self.space
43 return y - self.space + self.pt_space_after
44
45 def GetCellDiff(self, text, width): # get the remaining cell size for adjustment
46 w, h = self.DC.GetTextExtent(text)
47 diff = width - w
48 if diff < 0:
49 diff = 0
50 return diff
51
52 def TestFull(self, text_test):
53 w, h = self.DC.GetTextExtent(text_test)
54 if w > self.region: # trouble fitting into cell
55 return self.SetChar(text_test, self.region) # fit the text to the cell size
56 else:
57 return text_test
58
59 def SetFlow(self, ln_text, width):
60 width = width - self.pcell_right_margin
61 text = ""
62 split = string.split(ln_text)
63 if len(split) == 1:
64 return ln_text, ""
65
66 cnt = 0
67 for word in split:
68 bword = " " + word # blank + word
69 length = len(bword)
70
71 w, h = self.DC.GetTextExtent(text + bword)
72 if w < width:
73 text = text + bword
74 cnt = cnt + 1
75 else:
76 remain = string.joinfields(split[cnt:],' ')
77 text = string.strip(text)
78 return text, remain
79
80 remain = string.joinfields(split[cnt:],' ')
81 vout = string.strip(text)
82 return vout, remain
83
84 def SetChar(self, ln_text, width): # truncate string to fit into width
85 width = width - self.pcell_right_margin - self.pcell_left_margin
86 text = ""
87 for val in ln_text:
88 w, h = self.DC.GetTextExtent(text + val)
89 if w > width:
90 text = text + ".."
91 return text # fitted text value
92 text = text + val
93 return text
94
95 def OutTextPageWidth(self, textout, y_out, align, indent, txtdraw = TRUE):
96 textlines = string.splitfields(textout, '\n')
97 y = copy.copy(y_out)
98
99 pagew = self.parent.page_width * self.pwidth # full page width
100 w, h = self.DC.GetTextExtent(textout)
101 y_line = h
102
103 for text in textlines:
104 remain = 'X'
105 while remain != "":
106 vout, remain = self.SetFlow(text, pagew)
107 if self.draw == TRUE and txtdraw == TRUE:
108 test_out = vout
109 if align == wxALIGN_LEFT:
110 self.DC.DrawText(test_out, indent, y)
111
112 elif align == wxALIGN_CENTRE:
113 diff = self.GetCellDiff(test_out, pagew)
114 self.DC.DrawText(test_out, indent+diff/2, y)
115
116 elif align == wxALIGN_RIGHT:
117 diff = self.GetCellDiff(test_out, pagew)
118 self.DC.DrawText(test_out, indent+diff, y)
119
120 else:
121 self.DC.DrawText(test_out, indent, y_out)
122 text = remain
123 y = y + y_line
124 return y - y_line
125
126 def SetPreview(self, preview):
127 self.preview = preview
128
129 def SetPSize(self, width, height):
130 self.pwidth = width/self.scale
131 self.pheight = height/self.scale
132
133 def SetScale(self, scale):
134 self.scale = scale
135
136 def SetPTSize(self, width, height):
137 self.ptwidth = width
138 self.ptheight = height
139
140 def getWidth(self):
141 return self.sizew
142
143 def getHeight(self):
144 return self.sizeh
145
146
147 class PrintTableDraw(wxScrolledWindow, PrintBase):
148 def __init__(self, parent, DC, size):
149 self.parent = parent
150 self.DC = DC
151 self.scale = parent.scale
152 self.width = size[0]
153 self.height = size[1]
154 self.SetDefaults()
155
156 def SetDefaults(self):
157 self.page = 1
158 self.total_pages = None
159
160 self.page_width = self.parent.page_width
161 self.page_height = self.parent.page_height
162
163 self.left_margin = self.parent.left_margin
164 self.right_margin = self.parent.right_margin
165
166 self.top_margin = self.parent.top_margin
167 self.bottom_margin = self.parent.bottom_margin
168 self.cell_left_margin = self.parent.cell_left_margin
169 self.cell_right_margin = self.parent.cell_right_margin
170
171 self.label_colour = self.parent.label_colour
172
173 self.row_line_colour = self.parent.row_line_colour
174 self.row_line_size = self.parent.row_line_size
175
176 self.row_def_line_colour = self.parent.row_def_line_colour
177 self.row_def_line_size = self.parent.row_def_line_size
178
179 self.column_line_colour = self.parent.column_line_colour
180 self.column_line_size = self.parent.column_line_size
181
182 self.column_def_line_size = self.parent.column_def_line_size
183 self.column_def_line_colour = self.parent.column_def_line_colour
184
185 self.text_font = self.parent.text_font
186 self.text_font_name = self.parent.text_font_name
187 self.text_font_colour = self.parent.text_font_colour
188
189 self.label_font = self.parent.label_font
190 self.label_font_name = self.parent.label_font_name
191 self.label_font_colour = self.parent.label_font_colour
192
193 def AdjustValues(self):
194 self.vertical_offset = self.pheight * self.parent.vertical_offset
195 self.horizontal_offset = self.pheight * self.parent.horizontal_offset
196
197 self.pcell_left_margin = self.pwidth * self.cell_left_margin
198 self.pcell_right_margin = self.pwidth * self.cell_right_margin
199 self.ptop_margin = self.pheight * self.top_margin
200 self.pbottom_margin = self.pheight * self.bottom_margin
201
202 self.pheader_margin = self.pheight * self.parent.header_margin
203 self.pfooter_margin = self.pheight * self.parent.footer_margin
204
205 self.cell_colour = self.parent.set_cell_colour
206 self.cell_text = self.parent.set_cell_text
207
208 self.column = []
209 self.column_align = []
210 self.column_bgcolour = []
211 self.column_txtcolour = []
212
213 set_column_align = self.parent.set_column_align
214 set_column_bgcolour = self.parent.set_column_bgcolour
215 set_column_txtcolour = self.parent.set_column_txtcolour
216
217 pos_x = self.left_margin * self.pwidth + self.horizontal_offset # left margin
218 self.column.append(pos_x)
219
220 if self.set_column == []:
221 table_width = self.page_width - self.left_margin - self.right_margin
222 width = table_width/(len(self.label))
223 for val in self.label:
224 column_width = width * self.pwidth
225 pos_x = pos_x + column_width
226 self.column.append(pos_x) # position of each column
227 else:
228 for val in self.set_column:
229 column_width = val * self.pwidth
230 pos_x = pos_x + column_width
231 self.column.append(pos_x) # position of each column
232
233 if pos_x > self.page_width * self.pwidth: # check if it fits in page
234 print "Warning, Too Wide for Page"
235 return
236
237 if self.label != []:
238 if len(self.column) -1 != len(self.label):
239 print "Column Settings Incorrect", "\nColumn Value: " + str(self.column), "\nLabel Value: " + str(self.label)
240 return
241
242 first_value = self.data[0]
243 column_total = len(first_value)
244 if column_total != len(self.column) -1:
245 print "Column Settings Incorrect", first_value, self.column
246 return
247
248 col = 0
249 for col in range(column_total):
250 try:
251 align = set_column_align[col] # check if custom column alignment
252 except:
253 align = wxALIGN_LEFT
254 self.column_align.append(align)
255
256 try:
257 colour = set_column_bgcolour[col] # check if custom column background colour
258 except:
259 colour = self.parent.column_colour
260 self.column_bgcolour.append(colour)
261
262 try:
263 colour = set_column_txtcolour[col] # check if custom column text colour
264 except:
265 colour = self.parent.text_font_colour
266 self.column_txtcolour.append(colour)
267
268 col = col + 1
269
270 def SetPointAdjust(self):
271 f = wxFont(10, wxSWISS, wxNORMAL, wxNORMAL) # setup using 10 point
272 self.DC.SetFont(f)
273 f.SetFaceName(self.text_font_name)
274 x, y = self.DC.GetTextExtent("W")
275
276 self.label_pt_space_before = self.parent.label_pt_adj_before * y/10 # extra spacing for label per point value
277 self.label_pt_space_after = self.parent.label_pt_adj_after * y/10
278
279 self.text_pt_space_before = self.parent.text_pt_adj_before * y/10 # extra spacing for row text per point value
280 self.text_pt_space_after = self.parent.text_pt_adj_after * y/10
281
282 def SetPage(self, page):
283 self.page = page
284
285 def SetColumns(self, col):
286 self.column = col
287
288 def OutCanvas(self):
289 self.AdjustValues()
290 self.SetPointAdjust()
291
292 self.y_start = self.ptop_margin + self.vertical_offset
293 self.y_end = self.parent.page_height * self.pheight - self.pbottom_margin + self.vertical_offset
294
295 self.text_font.SetFaceName(self.label_font_name)
296 self.DC.SetFont(self.label_font)
297 x, y = self.DC.GetTextExtent("W")
298 self.label_space = y
299
300 self.text_font.SetFaceName(self.text_font_name)
301 self.DC.SetFont(self.text_font)
302 x, y = self.DC.GetTextExtent("W")
303 self.space = y
304
305 if self.total_pages == None:
306 self.GetTotalPages() # total pages for display/printing
307
308 self.data_cnt = 0
309
310 cnt = 1 # get selected page
311 if cnt == self.page:
312 self.draw = TRUE
313 else:
314 self.draw = FALSE
315
316 while cnt < self.page:
317 self.OutPage()
318 cnt = cnt + 1
319
320 self.draw = TRUE
321 self.PrintHeader()
322 self.PrintFooter()
323 self.OutPage()
324
325
326 def GetTotalPages(self):
327 self.data_cnt = 0
328 self.draw = FALSE
329
330 cnt = 0
331 while 1:
332 test = self.OutPage()
333 if test == FALSE:
334 break
335 cnt = cnt + 1
336
337 self.total_pages = cnt + 1
338
339 def OutPage(self):
340 self.y = self.y_start
341 self.end_x = self.column[-1]
342
343 if self.data_cnt < len(self.data)-1: # if there data for display on the page
344 if self.label != []: # check if header defined
345 self.PrintLabel()
346 else:
347 return FALSE
348
349 for val in self.data:
350 try:
351 row_val = self.data[self.data_cnt]
352 except:
353 self.FinishDraw()
354 return FALSE
355
356 max_y = self.PrintRow(row_val, FALSE) # test to see if row will fit in remaining space
357 test = max_y + self.space
358
359 if test > self.y_end:
360 break
361
362 self.ColourRowCells(max_y-self.y+self.space) # colour the row/column
363 max_y = self.PrintRow(row_val, TRUE) # row fits - print text
364 self.DrawGridLine() # top line of cell
365 self.y = max_y + self.space
366
367 if self.y > self.y_end:
368 break
369
370 self.data_cnt = self.data_cnt + 1
371
372 self.FinishDraw()
373
374 if self.data_cnt == len(self.data): # last value in list
375 return FALSE
376
377 return TRUE
378
379
380 def PrintLabel(self):
381 self.pt_space_before = self.label_pt_space_before # set the point spacing
382 self.pt_space_after = self.label_pt_space_after
383
384 self.LabelColorRow(self.label_colour)
385 self.label_font.SetFaceName(self.label_font_name)
386 self.DC.SetFont(self.label_font)
387 self.DC.SetTextForeground(self.label_font_colour)
388
389 self.col = 0
390 max_y = 0
391 for vtxt in self.label:
392 self.region = self.column[self.col+1] - self.column[self.col]
393 self.indent = self.column[self.col]
394
395 self.align = wxALIGN_LEFT
396
397 max_out = self.OutTextRegion(vtxt, TRUE)
398 if max_out > max_y:
399 max_y = max_out
400 self.col = self.col + 1
401
402 self.DrawGridLine() # top line of label
403 self.y = max_y + self.label_space
404
405 def PrintHeader(self): # print the header array
406 if self.draw == FALSE:
407 return
408
409 for val in self.parent.header:
410 f = wxFont(val["Size"], wxSWISS, wxNORMAL, val["Attr"])
411 self.DC.SetFont(f)
412 fontname = val["Name"]
413
414 f.SetFaceName(fontname)
415 self.DC.SetTextForeground(val["Colour"])
416
417 header_indent = val["Indent"] * self.pwidth
418 self.OutTextPageWidth(val["Text"], self.pheader_margin, val["Align"], header_indent, TRUE)
419
420 def PrintFooter(self): # print the header array
421 if self.draw == FALSE:
422 return
423
424 footer_pos = self.parent.page_height * self.pheight - self.pfooter_margin + self.vertical_offset
425 for val in self.parent.footer:
426 f = wxFont(val["Size"], wxSWISS, wxNORMAL, val["Attr"])
427 self.DC.SetFont(f)
428 fontname = val["Name"]
429
430 f.SetFaceName(fontname)
431 self.DC.SetTextForeground(val["Colour"])
432
433 footer_indent = val["Indent"] * self.pwidth
434 ftype = val["Type"]
435 if ftype == "Pageof":
436 text = "Page " + str(self.page) + " of " + str(self.total_pages)
437 elif ftype == "Page":
438 text = "Page " + str(self.page)
439 elif ftype == "Num":
440 text = str(self.page)
441 else:
442 text = ""
443
444 self.OutTextPageWidth(text, footer_pos, val["Align"], footer_indent, TRUE)
445
446 def LabelColorRow(self, colour):
447 brush = wxBrush(colour, wxSOLID)
448 self.DC.SetBrush(brush)
449 height = self.label_space + self.label_pt_space_before + self.label_pt_space_after
450 self.DC.DrawRectangle(self.column[0], self.y, self.end_x-self.column[0]+1, height)
451
452 def ColourRowCells(self, height):
453 if self.draw == FALSE:
454 return
455
456 col = 0
457 for colour in self.column_bgcolour:
458 cellcolour = self.GetCellColour(self.data_cnt, col)
459 if cellcolour != None:
460 colour = cellcolour
461
462 brush = wxBrush(colour, wxSOLID)
463 self.DC.SetBrush(brush)
464 self.DC.SetPen(wxPen(wxNamedColour('WHITE'), 0))
465
466 start_x = self.column[col]
467 width = self.column[col+1] - start_x + 2
468 self.DC.DrawRectangle(start_x, self.y, width, height)
469 col = col + 1
470
471 def PrintRow(self, row_val, draw = TRUE, align = wxALIGN_LEFT):
472 self.text_font.SetFaceName(self.text_font_name)
473 self.DC.SetFont(self.text_font)
474
475 self.pt_space_before = self.text_pt_space_before # set the point spacing
476 self.pt_space_after = self.text_pt_space_after
477
478 self.col = 0
479 max_y = 0
480 self.DC.SetTextForeground(self.text_font_colour)
481 for vtxt in row_val:
482 self.region = self.column[self.col+1] - self.column[self.col]
483 self.indent = self.column[self.col]
484 self.align = self.column_align[self.col]
485
486 fcolour = self.column_txtcolour[self.col] # set font colour
487 celltext = self.GetCellText(self.data_cnt, self.col)
488 if celltext != None:
489 fcolour = celltext # override the column colour
490
491 self.DC.SetTextForeground(fcolour)
492
493 max_out = self.OutTextRegion(vtxt, draw)
494 if max_out > max_y:
495 max_y = max_out
496 self.col = self.col + 1
497 return max_y
498
499 def GetCellColour(self, row, col): # check if custom colour defined for the cell background
500 try:
501 set = self.cell_colour[row]
502 except:
503 return None
504 try:
505 colour = set[col]
506 return colour
507 except:
508 return None
509
510 def GetCellText(self, row, col): # check if custom colour defined for the cell text
511 try:
512 set = self.cell_text[row]
513 except:
514 return None
515 try:
516 colour = set[col]
517 return colour
518 except:
519 return None
520
521 def FinishDraw(self):
522 self.DrawGridLine() # draw last row line
523 self.DrawColumns() # draw all vertical lines
524
525 def DrawGridLine(self):
526 if self.draw == TRUE:
527 try:
528 size = self.row_line_size[self.data_cnt]
529 except:
530 size = self.row_def_line_size
531
532 try:
533 colour = self.row_line_colour[self.data_cnt]
534 except:
535 colour = self.row_def_line_colour
536
537 self.DC.SetPen(wxPen(colour, size))
538
539 y_out = self.y
540 # y_out = self.y + self.pt_space_before + self.pt_space_after # adjust for extra spacing
541 self.DC.DrawLine(self.column[0], y_out, self.end_x, y_out)
542
543 def DrawColumns(self):
544 if self.draw == TRUE:
545 col = 0
546 for val in self.column:
547 try:
548 size = self.column_line_size[col]
549 except:
550 size = self.column_def_line_size
551
552 try:
553 colour = self.column_line_colour[col]
554 except:
555 colour = self.column_def_line_colour
556
557 indent = val
558
559 self.DC.SetPen(wxPen(colour, size))
560 self.DC.DrawLine(indent, self.y_start, indent, self.y)
561 col = col + 1
562
563 def DrawText(self):
564 self.DoRefresh()
565
566 def DoDrawing(self, DC):
567 size = DC.GetSizeTuple()
568 self.DC = DC
569
570 DC.BeginDrawing()
571 self.DrawText()
572 DC.EndDrawing()
573
574 self.sizew = DC.MaxY()
575 self.sizeh = DC.MaxX()
576
577
578 class PrintTable:
579 def __init__(self, parentFrame=None):
580 self.data = []
581 self.set_column = []
582 self.label = []
583 self.header = []
584 self.footer = []
585
586 self.set_column_align = {}
587 self.set_column_bgcolour = {}
588 self.set_column_txtcolour = {}
589 self.set_cell_colour = {}
590 self.set_cell_text = {}
591 self.column_line_size = {}
592 self.column_line_colour = {}
593 self.row_line_size = {}
594 self.row_line_colour = {}
595
596 self.parentFrame = parentFrame
597 self.SetPreviewSize()
598
599 self.printData = wxPrintData()
600 self.scale = 1.0
601
602 self.SetParms()
603 self.SetColors()
604 self.SetFonts()
605 self.TextSpacing()
606
607 self.SetPrinterOffset()
608 self.SetHeaderValue()
609 self.SetFooterValue()
610 self.SetMargins()
611 self.SetPortrait()
612
613 def SetPreviewSize(self, position = wxPoint(0, 0), size="Full"):
614 if size == "Full":
615 r = wxGetClientDisplayRect()
616 self.preview_frame_size = wxSize(r.width, r.height)
617 self.preview_frame_pos = wxPoint(r.x, r.y)
618 else:
619 self.preview_frame_size = size
620 self.preview_frame_pos = position
621
622 def SetPaperId(self, paper):
623 self.printData.SetPaperId(paper)
624
625 def SetOrientation(self, orient):
626 self.printData.SetOrientation(orient)
627
628 def SetColors(self):
629 self.row_def_line_colour = wxNamedColour('BLACK')
630 self.row_def_line_size = 1
631
632 self.column_def_line_colour = wxNamedColour('BLACK')
633 self.column_def_line_size = 1
634 self.column_colour = wxNamedColour('WHITE')
635
636 self.label_colour = wxNamedColour('LIGHT GREY')
637
638 def SetFonts(self):
639 self.label_font_size = 12
640 self.label_font_attr = wxNORMAL
641 self.label_font_name = "Arial"
642 self.label_font_colour = wxNamedColour('BLACK')
643
644 self.text_font_size = 10
645 self.text_font_attr = wxNORMAL
646 self.text_font_name = "Arial"
647 self.text_font_colour = wxNamedColour('BLACK')
648
649 def TextSpacing(self):
650 self.label_pt_adj_before = 0 # point adjustment before and after the label text
651 self.label_pt_adj_after = 0
652
653 self.text_pt_adj_before = 0 # point adjustment before and after the row text
654 self.text_pt_adj_after = 0
655
656 def SetLabelSpacing(self, before, after): # method to set the label space adjustment
657 self.label_pt_adj_before = before
658 self.label_pt_adj_after = after
659
660 def SetRowSpacing(self, before, after): # method to set the row space adjustment
661 self.text_pt_adj_before = before
662 self.text_pt_adj_after = after
663
664 def SetPrinterOffset(self): # offset to adjust for printer
665 self.vertical_offset = -0.1
666 self.horizontal_offset = -0.1
667
668 def SetHeaderValue(self):
669 self.header_margin = 0.25
670 self.header_font_size = 12
671 self.header_font_colour = wxNamedColour('BLACK')
672 self.header_font_attr = wxBOLD
673 self.header_font_name = self.text_font_name
674 self.header_align = wxALIGN_CENTRE
675 self.header_indent = 0
676 self.header_type = None
677
678 def SetFooterValue(self):
679 self.footer_margin = 0.7
680 self.footer_font_size = 10
681 self.footer_font_colour = wxNamedColour('BLACK')
682 self.footer_font_attr = wxNORMAL
683 self.footer_font_name = self.text_font_name
684 self.footer_align = wxALIGN_CENTRE
685 self.footer_indent = 0
686 self.footer_type = "Pageof"
687
688 def SetMargins(self):
689 self.left_margin = 1.0
690 self.right_margin = 1.0 # only used if no column sizes
691
692 self.top_margin = 0.8
693 self.bottom_margin = 1.0
694 self.cell_left_margin = 0.1
695 self.cell_right_margin = 0.1
696
697 def SetPortrait(self):
698 self.printData.SetPaperId(wxPAPER_LETTER)
699 self.printData.SetOrientation(wxPORTRAIT)
700 self.page_width = 8.5
701 self.page_height = 11.0
702
703 def SetLandscape(self):
704 self.printData.SetOrientation(wxLANDSCAPE)
705 self.page_width = 11.0
706 self.page_height = 8.5
707
708 def SetParms(self):
709 self.ymax = 1
710 self.xmax = 1
711 self.page = 1
712 self.total_pg = 100
713
714 self.preview = None
715 self.page = 0
716
717 def SetColAlignment(self, col, align=wxALIGN_LEFT):
718 self.set_column_align[col] = align
719
720 def SetColBackgroundColour(self, col, colour):
721 self.set_column_bgcolour[col] = colour
722
723 def SetColTextColour(self, col, colour):
724 self.set_column_txtcolour[col] = colour
725
726 def SetCellColour(self, row, col, colour): # cell background colour
727 try:
728 set = self.set_cell_colour[row] # test if row already exists
729 try:
730 set[col] = colour # test if column already exists
731 except:
732 set = { col: colour } # create the column value
733 except:
734 set = { col: colour } # create the column value
735
736 self.set_cell_colour[row] = set # create dictionary item for colour settings
737
738 def SetCellText(self, row, col, colour): # font colour for custom cells
739 try:
740 set = self.set_cell_text[row] # test if row already exists
741 try:
742 set[col] = colour # test if column already exists
743 except:
744 set = { col: colour } # create the column value
745 except:
746 set = { col: colour } # create the column value
747
748 self.set_cell_text[row] = set # create dictionary item for colour settings
749
750 def SetColumnLineSize(self, col, size): # column line size
751 self.column_line_size[col] = size # create dictionary item for column line settings
752
753 def SetColumnLineColour(self, col, colour):
754 self.column_line_colour[col] = colour
755
756 def SetRowLineSize(self, row, size):
757 self.row_line_size[row] = size
758
759 def SetRowLineColour(self, row, colour):
760 self.row_line_colour[row] = colour
761
762 def SetHeader(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
763 set = { "Text": text }
764
765 if name == None:
766 set["Name"] = self.header_font_name
767 else:
768 set["Name"] = name
769
770 if size == None:
771 set["Size"] = self.header_font_size
772 else:
773 set["Size"] = size
774
775 if colour == None:
776 set["Colour"] = self.header_font_colour
777 else:
778 set["Colour"] = colour
779
780 if align == None:
781 set["Align"] = self.header_align
782 else:
783 set["Align"] = align
784
785 if indent == None:
786 set["Indent"] = self.header_indent
787 else:
788 set["Indent"] = indent
789
790 if attr == None:
791 set["Attr"] = self.header_font_attr
792 else:
793 set["Attr"] = attr
794
795 if type == None:
796 set["Type"] = self.header_type
797 else:
798 set["Type"] = type
799
800 self.header.append(set)
801
802 def SetFooter(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
803 set = { "Text": text }
804
805 if name == None:
806 set["Name"] = self.footer_font_name
807 else:
808 set["Name"] = name
809
810 if size == None:
811 set["Size"] = self.footer_font_size
812 else:
813 set["Size"] = size
814
815 if colour == None:
816 set["Colour"] = self.footer_font_colour
817 else:
818 set["Colour"] = colour
819
820 if align == None:
821 set["Align"] = self.footer_align
822 else:
823 set["Align"] = align
824
825 if indent == None:
826 set["Indent"] = self.footer_indent
827 else:
828 set["Indent"] = indent
829
830 if type == None:
831 set["Type"] = self.footer_type
832 else:
833 set["Type"] = type
834
835 if attr == None:
836 set["Attr"] = self.footer_font_attr
837 else:
838 set["Attr"] = attr
839
840 self.footer.append(set)
841
842 def Preview(self):
843 printout = SetPrintout(self)
844 printout2 = SetPrintout(self)
845 self.preview = wxPrintPreview(printout, printout2, self.printData)
846 if not self.preview.Ok():
847 wxMessageBox("There was a problem printing!", "Printing", wxOK)
848 return
849
850 self.preview.SetZoom(60) # initial zoom value
851
852 frame = wxPreviewFrame(self.preview, self.parentFrame, "Print preview")
853
854 frame.Initialize()
855 if self.parentFrame:
856 frame.SetPosition(self.preview_frame_pos)
857 frame.SetSize(self.preview_frame_size)
858 frame.Show(true)
859
860
861 def Print(self):
862 pdd = wxPrintDialogData()
863 pdd.SetPrintData(self.printData)
864 printer = wxPrinter(pdd)
865 printout = SetPrintout(self)
866 if not printer.Print(self.parentFrame, printout):
867 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
868 else:
869 self.printData = printer.GetPrintDialogData().GetPrintData()
870 printout.Destroy()
871
872 def DoDrawing(self, DC):
873 size = DC.GetSizeTuple()
874 DC.BeginDrawing()
875
876 self.text_font = wxFont(self.text_font_size, wxSWISS, self.text_font_attr, wxNORMAL)
877 self.label_font = wxFont(self.label_font_size, wxSWISS, self.label_font_attr, wxNORMAL)
878
879 table = PrintTableDraw(self, DC, size)
880 table.data = self.data
881 table.set_column = self.set_column
882 table.label = self.label
883 table.SetPage(self.page)
884
885 if self.preview is None:
886 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
887 table.SetPTSize(size[0], size[1])
888 table.SetPreview(FALSE)
889 else:
890 if self.preview == 1:
891 table.scale = self.scale
892 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
893 else:
894 table.SetPSize(self.pwidth, self.pheight)
895
896 table.SetPTSize(self.ptwidth, self.ptheight)
897 table.SetPreview(self.preview)
898
899 table.OutCanvas()
900 self.page_total = table.total_pages # total display pages
901
902 DC.EndDrawing()
903
904 self.ymax = DC.MaxY()
905 self.xmax = DC.MaxX()
906
907 self.sizeh = size[0]
908 self.sizew = size[1]
909
910 def GetTotalPages(self):
911 self.page_total = 100
912 return self.page_total
913
914 def HasPage(self, page):
915 if page <= self.page_total:
916 return true
917 else:
918 return false
919
920 def SetPage(self, page):
921 self.page = page
922
923 def SetPageSize(self, width, height):
924 self.pwidth, self.pheight = width, height
925
926 def SetTotalSize(self, width, height):
927 self.ptwidth, self.ptheight = width, height
928
929 def SetPreview(self, preview, scale):
930 self.preview = preview
931 self.scale = scale
932
933 def SetTotalSize(self, width, height):
934 self.ptwidth = width
935 self.ptheight = height
936
937 class PrintGrid:
938 def __init__(self, parent, grid, format = [], total_col = None, total_row = None):
939 if total_row == None:
940 total_row = grid.GetNumberRows()
941 if total_col == None:
942 total_col = grid.GetNumberCols()
943
944 self.total_row = total_row
945 self.total_col = total_col
946 self.grid = grid
947
948 data = []
949 for row in range(total_row):
950 row_val = []
951 value = grid.GetRowLabelValue(row)
952 row_val.append(value)
953
954 for col in range(total_col):
955 value = grid.GetCellValue(row, col)
956 row_val.append(value)
957 data.append(row_val)
958
959 label = [""]
960 for col in range(total_col):
961 value = grid.GetColLabelValue(col)
962 label.append(value)
963
964 self.table = PrintTable(parent)
965 self.table.cell_left_margin = 0.0
966 self.table.cell_right_margin = 0.0
967
968 self.table.label = label
969 self.table.set_column = format
970 self.table.data = data
971
972 def GetTable(self):
973 return self.table
974
975 def SetAttributes(self):
976 for row in range(self.total_row):
977 for col in range(self.total_col):
978 colour = self.grid.GetCellTextColour(row, col-1)
979 self.table.SetCellText(row, col, colour)
980
981 colour = self.grid.GetCellBackgroundColour(row, col-1)
982 self.table.SetCellColour(row, col, colour)
983
984 def Preview(self):
985 self.table.Preview()
986
987 def Print(self):
988 self.table.Print()
989
990
991 class SetPrintout(wxPrintout):
992 def __init__(self, canvas):
993 wxPrintout.__init__(self)
994 self.canvas = canvas
995 self.end_pg = 1000
996
997 def OnBeginDocument(self, start, end):
998 return self.base_OnBeginDocument(start, end)
999
1000 def OnEndDocument(self):
1001 self.base_OnEndDocument()
1002
1003 def HasPage(self, page):
1004 try:
1005 end = self.canvas.HasPage(page)
1006 return end
1007 except:
1008 return true
1009
1010 def GetPageInfo(self):
1011 try:
1012 self.end_pg = self.canvas.GetTotalPages()
1013 except:
1014 pass
1015
1016 end_pg = self.end_pg
1017 str_pg = 1
1018 return (str_pg, end_pg, str_pg, end_pg)
1019
1020 def OnPreparePrinting(self):
1021 self.base_OnPreparePrinting()
1022
1023 def OnBeginPrinting(self):
1024 dc = self.GetDC()
1025
1026 self.preview = self.IsPreview()
1027 if (self.preview):
1028 self.pixelsPerInch = self.GetPPIScreen()
1029 else:
1030 self.pixelsPerInch = self.GetPPIPrinter()
1031
1032 (w, h) = dc.GetSizeTuple()
1033 scaleX = float(w) / 1000
1034 scaleY = float(h) / 1000
1035 self.printUserScale = min(scaleX, scaleY)
1036
1037 self.base_OnBeginPrinting()
1038
1039 def GetSize(self):
1040 self.psizew, self.psizeh = self.GetPPIPrinter()
1041 return self.psizew, self.psizeh
1042
1043 def GetTotalSize(self):
1044 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1045 return self.ptsizew, self.ptsizeh
1046
1047 def OnPrintPage(self, page):
1048 dc = self.GetDC()
1049 (w, h) = dc.GetSizeTuple()
1050 scaleX = float(w) / 1000
1051 scaleY = float(h) / 1000
1052 self.printUserScale = min(scaleX, scaleY)
1053 dc.SetUserScale(self.printUserScale, self.printUserScale)
1054
1055 self.preview = self.IsPreview()
1056
1057 self.canvas.SetPreview(self.preview, self.printUserScale)
1058 self.canvas.SetPage(page)
1059
1060 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1061 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
1062
1063 self.psizew, self.psizeh = self.GetPPIPrinter()
1064 self.canvas.SetPageSize(self.psizew, self.psizeh)
1065
1066 self.canvas.DoDrawing(dc)
1067 return true
1068
1069
1070
1071
1072
1073