]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/printout.py
b158215ff1cf90ced93ad40b8b2cf7fb5b960a78
[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.7
9 # Date: August 18, 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
598 self.printData = wxPrintData()
599 self.scale = 1.0
600
601 self.SetParms()
602 self.SetColors()
603 self.SetFonts()
604 self.TextSpacing()
605
606 self.SetPrinterOffset()
607 self.SetHeaderValue()
608 self.SetFooterValue()
609 self.SetMargins()
610 self.SetPortrait()
611
612 def SetPaperId(self, paper):
613 self.printData.SetPaperId(paper)
614
615 def SetOrientation(self, orient):
616 self.printData.SetOrientation(orient)
617
618 def SetColors(self):
619 self.row_def_line_colour = wxNamedColour('BLACK')
620 self.row_def_line_size = 1
621
622 self.column_def_line_colour = wxNamedColour('BLACK')
623 self.column_def_line_size = 1
624 self.column_colour = wxNamedColour('WHITE')
625
626 self.label_colour = wxNamedColour('LIGHT GREY')
627
628 def SetFonts(self):
629 self.label_font_size = 12
630 self.label_font_attr = wxNORMAL
631 self.label_font_name = "Arial"
632 self.label_font_colour = wxNamedColour('BLACK')
633
634 self.text_font_size = 10
635 self.text_font_attr = wxNORMAL
636 self.text_font_name = "Arial"
637 self.text_font_colour = wxNamedColour('BLACK')
638
639 def TextSpacing(self):
640 self.label_pt_adj_before = 0 # point adjustment before and after the label text
641 self.label_pt_adj_after = 0
642
643 self.text_pt_adj_before = 0 # point adjustment before and after the row text
644 self.text_pt_adj_after = 0
645
646 def SetLabelSpacing(self, before, after): # method to set the label space adjustment
647 self.label_pt_adj_before = before
648 self.label_pt_adj_after = after
649
650 def SetRowSpacing(self, before, after): # method to set the row space adjustment
651 self.text_pt_adj_before = before
652 self.text_pt_adj_after = after
653
654 def SetPrinterOffset(self): # offset to adjust for printer
655 self.vertical_offset = -0.1
656 self.horizontal_offset = -0.1
657
658 def SetHeaderValue(self):
659 self.header_margin = 0.25
660 self.header_font_size = 12
661 self.header_font_colour = wxNamedColour('BLACK')
662 self.header_font_attr = wxBOLD
663 self.header_font_name = self.text_font_name
664 self.header_align = wxALIGN_CENTRE
665 self.header_indent = 0
666 self.header_type = None
667
668 def SetFooterValue(self):
669 self.footer_margin = 0.7
670 self.footer_font_size = 10
671 self.footer_font_colour = wxNamedColour('BLACK')
672 self.footer_font_attr = wxNORMAL
673 self.footer_font_name = self.text_font_name
674 self.footer_align = wxALIGN_CENTRE
675 self.footer_indent = 0
676 self.footer_type = "Pageof"
677
678 def SetMargins(self):
679 self.left_margin = 1.0
680 self.right_margin = 1.0 # only used if no column sizes
681
682 self.top_margin = 0.8
683 self.bottom_margin = 1.0
684 self.cell_left_margin = 0.1
685 self.cell_right_margin = 0.1
686
687 def SetPortrait(self):
688 self.printData.SetPaperId(wxPAPER_LETTER)
689 self.printData.SetOrientation(wxPORTRAIT)
690 self.page_width = 8.5
691 self.page_height = 11.0
692
693 def SetLandscape(self):
694 self.printData.SetOrientation(wxLANDSCAPE)
695 self.page_width = 11.0
696 self.page_height = 8.5
697
698 def SetParms(self):
699 self.ymax = 1
700 self.xmax = 1
701 self.page = 1
702 self.total_pg = 100
703
704 self.preview = None
705 self.page = 0
706
707 def SetColAlignment(self, col, align=wxALIGN_LEFT):
708 self.set_column_align[col] = align
709
710 def SetColBackgroundColour(self, col, colour):
711 self.set_column_bgcolour[col] = colour
712
713 def SetColTextColour(self, col, colour):
714 self.set_column_txtcolour[col] = colour
715
716 def SetCellColour(self, row, col, colour): # cell background colour
717 try:
718 set = self.set_cell_colour[row] # test if row already exists
719 try:
720 set[col] = colour # test if column already exists
721 except:
722 set = { col: colour } # create the column value
723 except:
724 set = { col: colour } # create the column value
725
726 self.set_cell_colour[row] = set # create dictionary item for colour settings
727
728 def SetCellText(self, row, col, colour): # font colour for custom cells
729 try:
730 set = self.set_cell_text[row] # test if row already exists
731 try:
732 set[col] = colour # test if column already exists
733 except:
734 set = { col: colour } # create the column value
735 except:
736 set = { col: colour } # create the column value
737
738 self.set_cell_text[row] = set # create dictionary item for colour settings
739
740 def SetColumnLineSize(self, col, size): # column line size
741 self.column_line_size[col] = size # create dictionary item for column line settings
742
743 def SetColumnLineColour(self, col, colour):
744 self.column_line_colour[col] = colour
745
746 def SetRowLineSize(self, row, size):
747 self.row_line_size[row] = size
748
749 def SetRowLineColour(self, row, colour):
750 self.row_line_colour[row] = colour
751
752 def SetHeader(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
753 set = { "Text": text }
754
755 if name == None:
756 set["Name"] = self.header_font_name
757 else:
758 set["Name"] = name
759
760 if size == None:
761 set["Size"] = self.header_font_size
762 else:
763 set["Size"] = size
764
765 if colour == None:
766 set["Colour"] = self.header_font_colour
767 else:
768 set["Colour"] = colour
769
770 if align == None:
771 set["Align"] = self.header_align
772 else:
773 set["Align"] = align
774
775 if indent == None:
776 set["Indent"] = self.header_indent
777 else:
778 set["Indent"] = indent
779
780 if attr == None:
781 set["Attr"] = self.header_font_attr
782 else:
783 set["Attr"] = attr
784
785 if type == None:
786 set["Type"] = self.header_type
787 else:
788 set["Type"] = type
789
790 self.header.append(set)
791
792 def SetFooter(self, text = "", type = None, name=None, size=None, colour = None, align = None, indent = None, attr=None):
793 set = { "Text": text }
794
795 if name == None:
796 set["Name"] = self.footer_font_name
797 else:
798 set["Name"] = name
799
800 if size == None:
801 set["Size"] = self.footer_font_size
802 else:
803 set["Size"] = size
804
805 if colour == None:
806 set["Colour"] = self.footer_font_colour
807 else:
808 set["Colour"] = colour
809
810 if align == None:
811 set["Align"] = self.footer_align
812 else:
813 set["Align"] = align
814
815 if indent == None:
816 set["Indent"] = self.footer_indent
817 else:
818 set["Indent"] = indent
819
820 if type == None:
821 set["Type"] = self.footer_type
822 else:
823 set["Type"] = type
824
825 if attr == None:
826 set["Attr"] = self.footer_font_attr
827 else:
828 set["Attr"] = attr
829
830 self.footer.append(set)
831
832 def Preview(self):
833 printout = SetPrintout(self)
834 printout2 = SetPrintout(self)
835 self.preview = wxPrintPreview(printout, printout2, self.printData)
836 if not self.preview.Ok():
837 wxMessageBox("There was a problem printing!", "Printing", wxOK)
838 return
839
840 self.preview.SetZoom(60) # initial zoom value
841
842 frame = wxPreviewFrame(self.preview, self.parentFrame, "Print preview")
843
844 frame.Initialize()
845 if self.parentFrame:
846 frame.SetPosition(self.parentFrame.GetPosition())
847 frame.SetSize(self.parentFrame.GetSize())
848 frame.Show(true)
849
850
851 def Print(self):
852 pdd = wxPrintDialogData()
853 pdd.SetPrintData(self.printData)
854 printer = wxPrinter(pdd)
855 printout = SetPrintout(self)
856 if not printer.Print(self.parentFrame, printout):
857 wxMessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wxOK)
858 else:
859 self.printData = printer.GetPrintDialogData().GetPrintData()
860 printout.Destroy()
861
862 def DoDrawing(self, DC):
863 size = DC.GetSizeTuple()
864 DC.BeginDrawing()
865
866 self.text_font = wxFont(self.text_font_size, wxSWISS, self.text_font_attr, wxNORMAL)
867 self.label_font = wxFont(self.label_font_size, wxSWISS, self.label_font_attr, wxNORMAL)
868
869 table = PrintTableDraw(self, DC, size)
870 table.data = self.data
871 table.set_column = self.set_column
872 table.label = self.label
873 table.SetPage(self.page)
874
875 if self.preview is None:
876 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
877 table.SetPTSize(size[0], size[1])
878 table.SetPreview(FALSE)
879 else:
880 if self.preview == 1:
881 table.scale = self.scale
882 table.SetPSize(size[0]/self.page_width, size[1]/self.page_height)
883 else:
884 table.SetPSize(self.pwidth, self.pheight)
885
886 table.SetPTSize(self.ptwidth, self.ptheight)
887 table.SetPreview(self.preview)
888
889 table.OutCanvas()
890 self.page_total = table.total_pages # total display pages
891
892 DC.EndDrawing()
893
894 self.ymax = DC.MaxY()
895 self.xmax = DC.MaxX()
896
897 self.sizeh = size[0]
898 self.sizew = size[1]
899
900 def GetTotalPages(self):
901 self.page_total = 100
902 return self.page_total
903
904 def HasPage(self, page):
905 if page <= self.page_total:
906 return true
907 else:
908 return false
909
910 def SetPage(self, page):
911 self.page = page
912
913 def SetPageSize(self, width, height):
914 self.pwidth, self.pheight = width, height
915
916 def SetTotalSize(self, width, height):
917 self.ptwidth, self.ptheight = width, height
918
919 def SetPreview(self, preview, scale):
920 self.preview = preview
921 self.scale = scale
922
923 def SetTotalSize(self, width, height):
924 self.ptwidth = width
925 self.ptheight = height
926
927 class PrintGrid:
928 def __init__(self, parent, grid, format = [], total_col = None, total_row = None):
929 if total_row == None:
930 total_row = grid.GetNumberRows()
931 if total_col == None:
932 total_col = grid.GetNumberCols()
933
934 self.total_row = total_row
935 self.total_col = total_col
936 self.grid = grid
937
938 data = []
939 for row in range(total_row):
940 row_val = []
941 value = grid.GetRowLabelValue(row)
942 row_val.append(value)
943
944 for col in range(total_col):
945 value = grid.GetCellValue(row, col)
946 row_val.append(value)
947 data.append(row_val)
948
949 label = [""]
950 for col in range(total_col):
951 value = grid.GetColLabelValue(col)
952 label.append(value)
953
954 self.table = PrintTable(parent)
955 self.table.cell_left_margin = 0.0
956 self.table.cell_right_margin = 0.0
957
958 self.table.label = label
959 self.table.set_column = format
960 self.table.data = data
961
962 def GetTable(self):
963 return self.table
964
965 def SetAttributes(self):
966 for row in range(self.total_row):
967 for col in range(self.total_col):
968 colour = self.grid.GetCellTextColour(row, col-1)
969 self.table.SetCellText(row, col, colour)
970
971 colour = self.grid.GetCellBackgroundColour(row, col-1)
972 self.table.SetCellColour(row, col, colour)
973
974 def Preview(self):
975 self.table.Preview()
976
977 def Print(self):
978 self.table.Print()
979
980
981 class SetPrintout(wxPrintout):
982 def __init__(self, canvas):
983 wxPrintout.__init__(self)
984 self.canvas = canvas
985 self.end_pg = 1000
986
987 def OnBeginDocument(self, start, end):
988 return self.base_OnBeginDocument(start, end)
989
990 def OnEndDocument(self):
991 self.base_OnEndDocument()
992
993 def HasPage(self, page):
994 try:
995 end = self.canvas.HasPage(page)
996 return end
997 except:
998 return true
999
1000 def GetPageInfo(self):
1001 try:
1002 self.end_pg = self.canvas.GetTotalPages()
1003 except:
1004 pass
1005
1006 end_pg = self.end_pg
1007 str_pg = 1
1008 return (str_pg, end_pg, str_pg, end_pg)
1009
1010 def OnPreparePrinting(self):
1011 self.base_OnPreparePrinting()
1012
1013 def OnBeginPrinting(self):
1014 dc = self.GetDC()
1015
1016 self.preview = self.IsPreview()
1017 if (self.preview):
1018 self.pixelsPerInch = self.GetPPIScreen()
1019 else:
1020 self.pixelsPerInch = self.GetPPIPrinter()
1021
1022 (w, h) = dc.GetSizeTuple()
1023 scaleX = float(w) / 1000
1024 scaleY = float(h) / 1000
1025 self.printUserScale = min(scaleX, scaleY)
1026
1027 self.base_OnBeginPrinting()
1028
1029 def GetSize(self):
1030 self.psizew, self.psizeh = self.GetPPIPrinter()
1031 return self.psizew, self.psizeh
1032
1033 def GetTotalSize(self):
1034 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1035 return self.ptsizew, self.ptsizeh
1036
1037 def OnPrintPage(self, page):
1038 dc = self.GetDC()
1039 (w, h) = dc.GetSizeTuple()
1040 scaleX = float(w) / 1000
1041 scaleY = float(h) / 1000
1042 self.printUserScale = min(scaleX, scaleY)
1043 dc.SetUserScale(self.printUserScale, self.printUserScale)
1044
1045 self.preview = self.IsPreview()
1046
1047 self.canvas.SetPreview(self.preview, self.printUserScale)
1048 self.canvas.SetPage(page)
1049
1050 self.ptsizew, self.ptsizeh = self.GetPageSizePixels()
1051 self.canvas.SetTotalSize(self.ptsizew, self.ptsizeh)
1052
1053 self.psizew, self.psizeh = self.GetPPIPrinter()
1054 self.canvas.SetPageSize(self.psizew, self.psizeh)
1055
1056 self.canvas.DoDrawing(dc)
1057 return true
1058
1059
1060
1061
1062
1063