]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/printout.py
944da991b66382ab5b1507af9121f08cf610e1eb
1 #----------------------------------------------------------------------------
3 # Purpose: preview and printing class -> table/grid printing
5 # Author: Lorne White (email: lorne.white@telusplanet.net)
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------------
13 import os
, sys
, string
, copy
15 from wxPython
.wx
import *
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
:
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
)
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
)
35 elif self
.align
== wxALIGN_RIGHT
:
36 diff
= self
.GetCellDiff(test_out
, self
.region
)
37 self
.DC
.DrawText(test_out
, self
.indent
+diff
, y
)
40 self
.DC
.DrawText(test_out
, self
.indent
+self
.pcell_left_margin
, y
)
43 return y
- self
.space
+ self
.pt_space_after
45 def GetCellDiff(self
, text
, width
): # get the remaining cell size for adjustment
46 w
, h
= self
.DC
.GetTextExtent(text
)
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
59 def SetFlow(self
, ln_text
, width
):
60 width
= width
- self
.pcell_right_margin
62 split
= string
.split(ln_text
)
68 bword
= " " + word
# blank + word
71 w
, h
= self
.DC
.GetTextExtent(text
+ bword
)
76 remain
= string
.joinfields(split
[cnt
:],' ')
77 text
= string
.strip(text
)
80 remain
= string
.joinfields(split
[cnt
:],' ')
81 vout
= string
.strip(text
)
84 def SetChar(self
, ln_text
, width
): # truncate string to fit into width
85 width
= width
- self
.pcell_right_margin
- self
.pcell_left_margin
88 w
, h
= self
.DC
.GetTextExtent(text
+ val
)
91 return text
# fitted text value
95 def OutTextPageWidth(self
, textout
, y_out
, align
, indent
, txtdraw
= TRUE
):
96 textlines
= string
.splitfields(textout
, '\n')
99 pagew
= self
.parent
.page_width
* self
.pwidth
# full page width
100 w
, h
= self
.DC
.GetTextExtent(textout
)
103 for text
in textlines
:
106 vout
, remain
= self
.SetFlow(text
, pagew
)
107 if self
.draw
== TRUE
and txtdraw
== TRUE
:
109 if align
== wxALIGN_LEFT
:
110 self
.DC
.DrawText(test_out
, indent
, y
)
112 elif align
== wxALIGN_CENTRE
:
113 diff
= self
.GetCellDiff(test_out
, pagew
)
114 self
.DC
.DrawText(test_out
, indent
+diff
/2, y
)
116 elif align
== wxALIGN_RIGHT
:
117 diff
= self
.GetCellDiff(test_out
, pagew
)
118 self
.DC
.DrawText(test_out
, indent
+diff
, y
)
121 self
.DC
.DrawText(test_out
, indent
, y_out
)
126 def SetPreview(self
, preview
):
127 self
.preview
= preview
129 def SetPSize(self
, width
, height
):
130 self
.pwidth
= width
/self
.scale
131 self
.pheight
= height
/self
.scale
133 def SetScale(self
, scale
):
136 def SetPTSize(self
, width
, height
):
138 self
.ptheight
= height
147 class PrintTableDraw(wxScrolledWindow
, PrintBase
):
148 def __init__(self
, parent
, DC
, size
):
151 self
.scale
= parent
.scale
153 self
.height
= size
[1]
156 def SetDefaults(self
):
158 self
.total_pages
= None
160 self
.page_width
= self
.parent
.page_width
161 self
.page_height
= self
.parent
.page_height
163 self
.left_margin
= self
.parent
.left_margin
164 self
.right_margin
= self
.parent
.right_margin
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
171 self
.label_colour
= self
.parent
.label_colour
173 self
.row_line_colour
= self
.parent
.row_line_colour
174 self
.row_line_size
= self
.parent
.row_line_size
176 self
.row_def_line_colour
= self
.parent
.row_def_line_colour
177 self
.row_def_line_size
= self
.parent
.row_def_line_size
179 self
.column_line_colour
= self
.parent
.column_line_colour
180 self
.column_line_size
= self
.parent
.column_line_size
182 self
.column_def_line_size
= self
.parent
.column_def_line_size
183 self
.column_def_line_colour
= self
.parent
.column_def_line_colour
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
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
193 def AdjustValues(self
):
194 self
.vertical_offset
= self
.pheight
* self
.parent
.vertical_offset
195 self
.horizontal_offset
= self
.pheight
* self
.parent
.horizontal_offset
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
202 self
.pheader_margin
= self
.pheight
* self
.parent
.header_margin
203 self
.pfooter_margin
= self
.pheight
* self
.parent
.footer_margin
205 self
.cell_colour
= self
.parent
.set_cell_colour
206 self
.cell_text
= self
.parent
.set_cell_text
209 self
.column_align
= []
210 self
.column_bgcolour
= []
211 self
.column_txtcolour
= []
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
217 pos_x
= self
.left_margin
* self
.pwidth
+ self
.horizontal_offset
# left margin
218 self
.column
.append(pos_x
)
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
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
233 if pos_x
> self
.page_width
* self
.pwidth
: # check if it fits in page
234 print "Warning, Too Wide for Page"
238 if len(self
.column
) -1 != len(self
.label
):
239 print "Column Settings Incorrect", "\nColumn Value: " + str(self
.column
), "\nLabel Value: " + str(self
.label
)
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
249 for col
in range(column_total
):
251 align
= set_column_align
[col
] # check if custom column alignment
254 self
.column_align
.append(align
)
257 colour
= set_column_bgcolour
[col
] # check if custom column background colour
259 colour
= self
.parent
.column_colour
260 self
.column_bgcolour
.append(colour
)
263 colour
= set_column_txtcolour
[col
] # check if custom column text colour
265 colour
= self
.parent
.text_font_colour
266 self
.column_txtcolour
.append(colour
)
270 def SetPointAdjust(self
):
271 f
= wxFont(10, wxSWISS
, wxNORMAL
, wxNORMAL
) # setup using 10 point
273 f
.SetFaceName(self
.text_font_name
)
274 x
, y
= self
.DC
.GetTextExtent("W")
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
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
282 def SetPage(self
, page
):
285 def SetColumns(self
, col
):
290 self
.SetPointAdjust()
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
295 self
.text_font
.SetFaceName(self
.label_font_name
)
296 self
.DC
.SetFont(self
.label_font
)
297 x
, y
= self
.DC
.GetTextExtent("W")
300 self
.text_font
.SetFaceName(self
.text_font_name
)
301 self
.DC
.SetFont(self
.text_font
)
302 x
, y
= self
.DC
.GetTextExtent("W")
305 if self
.total_pages
== None:
306 self
.GetTotalPages() # total pages for display/printing
310 cnt
= 1 # get selected page
316 while cnt
< self
.page
:
326 def GetTotalPages(self
):
332 test
= self
.OutPage()
337 self
.total_pages
= cnt
+ 1
340 self
.y
= self
.y_start
341 self
.end_x
= self
.column
[-1]
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
349 for val
in self
.data
:
351 row_val
= self
.data
[self
.data_cnt
]
356 max_y
= self
.PrintRow(row_val
, FALSE
) # test to see if row will fit in remaining space
357 test
= max_y
+ self
.space
359 if test
> self
.y_end
:
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
367 if self
.y
> self
.y_end
:
370 self
.data_cnt
= self
.data_cnt
+ 1
374 if self
.data_cnt
== len(self
.data
): # last value in list
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
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
)
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
]
395 self
.align
= wxALIGN_LEFT
397 max_out
= self
.OutTextRegion(vtxt
, TRUE
)
400 self
.col
= self
.col
+ 1
402 self
.DrawGridLine() # top line of label
403 self
.y
= max_y
+ self
.label_space
405 def PrintHeader(self
): # print the header array
406 if self
.draw
== FALSE
:
409 for val
in self
.parent
.header
:
410 f
= wxFont(val
["Size"], wxSWISS
, wxNORMAL
, val
["Attr"])
412 fontname
= val
["Name"]
414 f
.SetFaceName(fontname
)
415 self
.DC
.SetTextForeground(val
["Colour"])
417 header_indent
= val
["Indent"] * self
.pwidth
418 self
.OutTextPageWidth(val
["Text"], self
.pheader_margin
, val
["Align"], header_indent
, TRUE
)
420 def PrintFooter(self
): # print the header array
421 if self
.draw
== FALSE
:
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"])
428 fontname
= val
["Name"]
430 f
.SetFaceName(fontname
)
431 self
.DC
.SetTextForeground(val
["Colour"])
433 footer_indent
= val
["Indent"] * self
.pwidth
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
)
440 text
= str(self
.page
)
444 self
.OutTextPageWidth(text
, footer_pos
, val
["Align"], footer_indent
, TRUE
)
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
)
452 def ColourRowCells(self
, height
):
453 if self
.draw
== FALSE
:
457 for colour
in self
.column_bgcolour
:
458 cellcolour
= self
.GetCellColour(self
.data_cnt
, col
)
459 if cellcolour
!= None:
462 brush
= wxBrush(colour
, wxSOLID
)
463 self
.DC
.SetBrush(brush
)
464 self
.DC
.SetPen(wxPen(wxNamedColour('WHITE'), 0))
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
)
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
)
475 self
.pt_space_before
= self
.text_pt_space_before
# set the point spacing
476 self
.pt_space_after
= self
.text_pt_space_after
480 self
.DC
.SetTextForeground(self
.text_font_colour
)
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
]
486 fcolour
= self
.column_txtcolour
[self
.col
] # set font colour
487 celltext
= self
.GetCellText(self
.data_cnt
, self
.col
)
489 fcolour
= celltext
# override the column colour
491 self
.DC
.SetTextForeground(fcolour
)
493 max_out
= self
.OutTextRegion(vtxt
, draw
)
496 self
.col
= self
.col
+ 1
499 def GetCellColour(self
, row
, col
): # check if custom colour defined for the cell background
501 set = self
.cell_colour
[row
]
510 def GetCellText(self
, row
, col
): # check if custom colour defined for the cell text
512 set = self
.cell_text
[row
]
521 def FinishDraw(self
):
522 self
.DrawGridLine() # draw last row line
523 self
.DrawColumns() # draw all vertical lines
525 def DrawGridLine(self
):
526 if self
.draw
== TRUE
:
528 size
= self
.row_line_size
[self
.data_cnt
]
530 size
= self
.row_def_line_size
533 colour
= self
.row_line_colour
[self
.data_cnt
]
535 colour
= self
.row_def_line_colour
537 self
.DC
.SetPen(wxPen(colour
, size
))
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
)
543 def DrawColumns(self
):
544 if self
.draw
== TRUE
:
546 for val
in self
.column
:
548 size
= self
.column_line_size
[col
]
550 size
= self
.column_def_line_size
553 colour
= self
.column_line_colour
[col
]
555 colour
= self
.column_def_line_colour
559 self
.DC
.SetPen(wxPen(colour
, size
))
560 self
.DC
.DrawLine(indent
, self
.y_start
, indent
, self
.y
)
566 def DoDrawing(self
, DC
):
567 size
= DC
.GetSizeTuple()
574 self
.sizew
= DC
.MaxY()
575 self
.sizeh
= DC
.MaxX()
579 def __init__(self
, parentFrame
=None):
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
= {}
596 self
.parentFrame
= parentFrame
597 self
.SetPreviewSize()
599 self
.printData
= wxPrintData()
607 self
.SetPrinterOffset()
608 self
.SetHeaderValue()
609 self
.SetFooterValue()
613 def SetPreviewSize(self
, position
= wxPoint(0, 0), 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
)
619 self
.preview_frame_size
= size
620 self
.preview_frame_pos
= position
622 def SetPaperId(self
, paper
):
623 self
.printData
.SetPaperId(paper
)
625 def SetOrientation(self
, orient
):
626 self
.printData
.SetOrientation(orient
)
629 self
.row_def_line_colour
= wxNamedColour('BLACK')
630 self
.row_def_line_size
= 1
632 self
.column_def_line_colour
= wxNamedColour('BLACK')
633 self
.column_def_line_size
= 1
634 self
.column_colour
= wxNamedColour('WHITE')
636 self
.label_colour
= wxNamedColour('LIGHT GREY')
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')
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')
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
653 self
.text_pt_adj_before
= 0 # point adjustment before and after the row text
654 self
.text_pt_adj_after
= 0
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
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
664 def SetPrinterOffset(self
): # offset to adjust for printer
665 self
.vertical_offset
= -0.1
666 self
.horizontal_offset
= -0.1
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
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"
688 def SetMargins(self
):
689 self
.left_margin
= 1.0
690 self
.right_margin
= 1.0 # only used if no column sizes
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
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
703 def SetLandscape(self
):
704 self
.printData
.SetOrientation(wxLANDSCAPE
)
705 self
.page_width
= 11.0
706 self
.page_height
= 8.5
717 def SetColAlignment(self
, col
, align
=wxALIGN_LEFT
):
718 self
.set_column_align
[col
] = align
720 def SetColBackgroundColour(self
, col
, colour
):
721 self
.set_column_bgcolour
[col
] = colour
723 def SetColTextColour(self
, col
, colour
):
724 self
.set_column_txtcolour
[col
] = colour
726 def SetCellColour(self
, row
, col
, colour
): # cell background colour
728 set = self
.set_cell_colour
[row
] # test if row already exists
730 set[col
] = colour
# test if column already exists
732 set = { col: colour }
# create the column value
734 set = { col: colour }
# create the column value
736 self
.set_cell_colour
[row
] = set # create dictionary item for colour settings
738 def SetCellText(self
, row
, col
, colour
): # font colour for custom cells
740 set = self
.set_cell_text
[row
] # test if row already exists
742 set[col
] = colour
# test if column already exists
744 set = { col: colour }
# create the column value
746 set = { col: colour }
# create the column value
748 self
.set_cell_text
[row
] = set # create dictionary item for colour settings
750 def SetColumnLineSize(self
, col
, size
): # column line size
751 self
.column_line_size
[col
] = size
# create dictionary item for column line settings
753 def SetColumnLineColour(self
, col
, colour
):
754 self
.column_line_colour
[col
] = colour
756 def SetRowLineSize(self
, row
, size
):
757 self
.row_line_size
[row
] = size
759 def SetRowLineColour(self
, row
, colour
):
760 self
.row_line_colour
[row
] = colour
762 def SetHeader(self
, text
= "", type = None, name
=None, size
=None, colour
= None, align
= None, indent
= None, attr
=None):
763 set = { "Text": text }
766 set["Name"] = self
.header_font_name
771 set["Size"] = self
.header_font_size
776 set["Colour"] = self
.header_font_colour
778 set["Colour"] = colour
781 set["Align"] = self
.header_align
786 set["Indent"] = self
.header_indent
788 set["Indent"] = indent
791 set["Attr"] = self
.header_font_attr
796 set["Type"] = self
.header_type
800 self
.header
.append(set)
802 def SetFooter(self
, text
= "", type = None, name
=None, size
=None, colour
= None, align
= None, indent
= None, attr
=None):
803 set = { "Text": text }
806 set["Name"] = self
.footer_font_name
811 set["Size"] = self
.footer_font_size
816 set["Colour"] = self
.footer_font_colour
818 set["Colour"] = colour
821 set["Align"] = self
.footer_align
826 set["Indent"] = self
.footer_indent
828 set["Indent"] = indent
831 set["Type"] = self
.footer_type
836 set["Attr"] = self
.footer_font_attr
840 self
.footer
.append(set)
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
)
850 self
.preview
.SetZoom(60) # initial zoom value
852 frame
= wxPreviewFrame(self
.preview
, self
.parentFrame
, "Print preview")
856 frame
.SetPosition(self
.preview_frame_pos
)
857 frame
.SetSize(self
.preview_frame_size
)
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
)
869 self
.printData
= printer
.GetPrintDialogData().GetPrintData()
872 def DoDrawing(self
, DC
):
873 size
= DC
.GetSizeTuple()
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
)
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
)
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
)
890 if self
.preview
== 1:
891 table
.scale
= self
.scale
892 table
.SetPSize(size
[0]/self
.page_width
, size
[1]/self
.page_height
)
894 table
.SetPSize(self
.pwidth
, self
.pheight
)
896 table
.SetPTSize(self
.ptwidth
, self
.ptheight
)
897 table
.SetPreview(self
.preview
)
900 self
.page_total
= table
.total_pages
# total display pages
904 self
.ymax
= DC
.MaxY()
905 self
.xmax
= DC
.MaxX()
910 def GetTotalPages(self
):
911 self
.page_total
= 100
912 return self
.page_total
914 def HasPage(self
, page
):
915 if page
<= self
.page_total
:
920 def SetPage(self
, page
):
923 def SetPageSize(self
, width
, height
):
924 self
.pwidth
, self
.pheight
= width
, height
926 def SetTotalSize(self
, width
, height
):
927 self
.ptwidth
, self
.ptheight
= width
, height
929 def SetPreview(self
, preview
, scale
):
930 self
.preview
= preview
933 def SetTotalSize(self
, width
, height
):
935 self
.ptheight
= height
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()
944 self
.total_row
= total_row
945 self
.total_col
= total_col
949 for row
in range(total_row
):
951 value
= grid
.GetRowLabelValue(row
)
952 row_val
.append(value
)
954 for col
in range(total_col
):
955 value
= grid
.GetCellValue(row
, col
)
956 row_val
.append(value
)
960 for col
in range(total_col
):
961 value
= grid
.GetColLabelValue(col
)
964 self
.table
= PrintTable(parent
)
965 self
.table
.cell_left_margin
= 0.0
966 self
.table
.cell_right_margin
= 0.0
968 self
.table
.label
= label
969 self
.table
.set_column
= format
970 self
.table
.data
= data
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
)
981 colour
= self
.grid
.GetCellBackgroundColour(row
, col
-1)
982 self
.table
.SetCellColour(row
, col
, colour
)
991 class SetPrintout(wxPrintout
):
992 def __init__(self
, canvas
):
993 wxPrintout
.__init
__(self
)
997 def OnBeginDocument(self
, start
, end
):
998 return self
.base_OnBeginDocument(start
, end
)
1000 def OnEndDocument(self
):
1001 self
.base_OnEndDocument()
1003 def HasPage(self
, page
):
1005 end
= self
.canvas
.HasPage(page
)
1010 def GetPageInfo(self
):
1012 self
.end_pg
= self
.canvas
.GetTotalPages()
1016 end_pg
= self
.end_pg
1018 return (str_pg
, end_pg
, str_pg
, end_pg
)
1020 def OnPreparePrinting(self
):
1021 self
.base_OnPreparePrinting()
1023 def OnBeginPrinting(self
):
1026 self
.preview
= self
.IsPreview()
1028 self
.pixelsPerInch
= self
.GetPPIScreen()
1030 self
.pixelsPerInch
= self
.GetPPIPrinter()
1032 (w
, h
) = dc
.GetSizeTuple()
1033 scaleX
= float(w
) / 1000
1034 scaleY
= float(h
) / 1000
1035 self
.printUserScale
= min(scaleX
, scaleY
)
1037 self
.base_OnBeginPrinting()
1040 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
1041 return self
.psizew
, self
.psizeh
1043 def GetTotalSize(self
):
1044 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
1045 return self
.ptsizew
, self
.ptsizeh
1047 def OnPrintPage(self
, page
):
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
)
1055 self
.preview
= self
.IsPreview()
1057 self
.canvas
.SetPreview(self
.preview
, self
.printUserScale
)
1058 self
.canvas
.SetPage(page
)
1060 self
.ptsizew
, self
.ptsizeh
= self
.GetPageSizePixels()
1061 self
.canvas
.SetTotalSize(self
.ptsizew
, self
.ptsizeh
)
1063 self
.psizew
, self
.psizeh
= self
.GetPPIPrinter()
1064 self
.canvas
.SetPageSize(self
.psizew
, self
.psizeh
)
1066 self
.canvas
.DoDrawing(dc
)