]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/printout.py
wx.FRAME_FLOAT_ON_PARENT notes
[wxWidgets.git] / wxPython / wx / lib / printout.py
index a4e90ceba1f2c9873783b681b9a428aa34c89c02..2c11a6f2c6ba927f27f791261d987477bbbff6fa 100644 (file)
 # add index to data list after parsing total pages for paging
 #----------------------------------------------------------------------------
 # 12/10/2003 - Jeff Grimmett (grimmtooth@softhome.net)
-#
 # o 2.5 compatability update.
-#
-
+#----------------------------------------------------------------------------
+# 11/23/2004 - Vernon Cole (wnvcole@peppermillcas.com)
+# o Generalize for non-2-dimensional sequences and non-text data
+#   (can use as a simple text printer by supplying a list of strings.)
+# o Add a small _main_ for self test
 import  copy
-import  os
-import  sys
-
+import  types
 import  wx
 
 class PrintBase:
@@ -60,18 +61,18 @@ class PrintBase:
                 if self.draw == True and txtdraw == True:
                     test_out = self.TestFull(vout)
                     if self.align == wx.ALIGN_LEFT:
-                        self.DC.DrawText(test_out, (self.indent+self.pcell_left_margin, y))
+                        self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
 
                     elif self.align == wx.ALIGN_CENTRE:
                         diff = self.GetCellDiff(test_out, self.region)
-                        self.DC.DrawText(test_out, (self.indent+diff/2, y))
+                        self.DC.DrawText(test_out, self.indent+diff/2, y)
 
                     elif self.align == wx.ALIGN_RIGHT:
                         diff = self.GetCellDiff(test_out, self.region)
-                        self.DC.DrawText(test_out, (self.indent+diff, y))
+                        self.DC.DrawText(test_out, self.indent+diff, y)
 
                     else:
-                        self.DC.DrawText(test_out, (self.indent+self.pcell_left_margin, y))
+                        self.DC.DrawText(test_out, self.indent+self.pcell_left_margin, y)
                 text = remain
                 y = y + self.space
         return y - self.space + self.pt_space_after
@@ -148,18 +149,18 @@ class PrintBase:
                 if self.draw == True and txtdraw == True:
                     test_out = vout
                     if align == wx.ALIGN_LEFT:
-                        self.DC.DrawText(test_out, (indent, y))
+                        self.DC.DrawText(test_out, indent, y)
 
                     elif align == wx.ALIGN_CENTRE:
                         diff = self.GetCellDiff(test_out, pagew)
-                        self.DC.DrawText(test_out, (indent+diff/2, y))
+                        self.DC.DrawText(test_out, indent+diff/2, y)
 
                     elif align == wx.ALIGN_RIGHT:
                         diff = self.GetCellDiff(test_out, pagew)
-                        self.DC.DrawText(test_out, (indent+diff, y))
+                        self.DC.DrawText(test_out, indent+diff, y)
 
                     else:
-                        self.DC.DrawText(test_out, (indent, y_out))
+                        self.DC.DrawText(test_out, indent, y_out)
                 text = remain
                 y = y + y_line
         return y - y_line
@@ -173,10 +174,9 @@ class PrintBase:
         return date + ' ' + time
 
     def GetNow(self):
-        full = str(wx.DateTime_Now())        # get the current date and time in print format
-        flds = full.split()
-        date = flds[0]
-        time = flds[1]
+        now = wx.DateTime.Now()
+        date = now.FormatDate()
+        time = now.FormatTime()
         return date, time
 
     def SetPreview(self, preview):
@@ -269,10 +269,47 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
         pos_x = self.left_margin * self.pwidth + self.horizontal_offset     # left margin
         self.column.append(pos_x)
 
+        #module logic expects two dimensional data -- fix input if needed
+        if isinstance(self.data,types.StringTypes):
+            self.data = [[copy.copy(self.data)]] # a string becomes a single cell
+        try:
+            rows = len(self.data)
+        except TypeError:
+            self.data = [[str(self.data)]] # a non-iterable becomes a single cell
+            rows = 1
+        first_value = self.data[0]
+
+        if isinstance(first_value, types.StringTypes): # a sequence of strings
+            if self.label == [] and self.set_column == []:
+                data = []
+                for x in self.data:     #becomes one column
+                    data.append([x])
+            else:
+                data = [self.data]      #becames one row
+            self.data = data
+            first_value = data[0]
+        try:
+            column_total = len(first_value)
+        except TypeError:    # a sequence of non-iterables
+            if self.label == [] and self.set_column == []:
+                data = []       #becomes one column
+                for x in self.data:
+                    data.append([str(x)])
+                column_total = 1
+            else:
+                data = [self.data] #becomes one row
+                column_total = len(self.data)
+            self.data = data
+            first_value = data[0]
+
         if self.set_column == []:
             table_width = self.page_width - self.left_margin - self.right_margin
-            width = table_width/(len(self.label))
-            for val in self.label:
+            if self.label == []:
+                temp = first_value
+            else:
+                temp = self.label
+            width = table_width/(len(temp))
+            for val in temp:
                 column_width = width * self.pwidth
                 pos_x = pos_x + column_width
                 self.column.append(pos_x)   # position of each column
@@ -291,13 +328,10 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
                 print "Column Settings Incorrect", "\nColumn Value: " + str(self.column), "\nLabel Value: " + str(self.label)
                 return
 
-        first_value = self.data[0]
-        column_total = len(first_value)
         if column_total != len(self.column) -1:
-            print "Column Settings Incorrect", first_value, self.column
+            print "Cannot fit", first_value, 'in', len(self.column)-1, 'columns.'
             return
 
-        col = 0
         for col in range(column_total):
             try:
                 align = set_column_align[col]       # check if custom column alignment
@@ -315,10 +349,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
                 colour = set_column_txtcolour[col]     # check if custom column text colour
             except:
                 colour = self.GetFontColour(self.parent.text_font)
-
             self.column_txtcolour.append(colour)
 
-            col = col + 1
 
     def SetPointAdjust(self):
         f = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)     # setup using 10 point
@@ -496,8 +528,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
         brush = wx.Brush(colour, wx.SOLID)
         self.DC.SetBrush(brush)
         height = self.label_space + self.label_pt_space_before + self.label_pt_space_after
-        self.DC.DrawRectangle((self.column[0], self.y),
-                              (self.end_x-self.column[0]+1, height))
+        self.DC.DrawRectangle(self.column[0], self.y,
+                              self.end_x-self.column[0]+1, height)
 
     def ColourRowCells(self, height):
         if self.draw == False:
@@ -515,7 +547,7 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
 
             start_x = self.column[col]
             width = self.column[col+1] - start_x + 2
-            self.DC.DrawRectangle((start_x, self.y), (width, height))
+            self.DC.DrawRectangle(start_x, self.y, width, height)
             col = col + 1
 
     def PrintRow(self, row_val, draw = True, align = wx.ALIGN_LEFT):
@@ -527,12 +559,14 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
         self.col = 0
         max_y = 0
         for vtxt in row_val:
+            if not isinstance(vtxt,types.StringTypes):
+                vtxt = str(vtxt)
             self.region = self.column[self.col+1] - self.column[self.col]
             self.indent = self.column[self.col]
             self.align = self.column_align[self.col]
 
             fcolour = self.column_txtcolour[self.col]       # set font colour
-            celltext = self.GetCellText(self.data_cnt, self.col)
+            celltext = self.GetCellTextColour(self.data_cnt, self.col)
             if celltext is not None:
                 fcolour = celltext      # override the column colour
 
@@ -555,7 +589,7 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
         except:
             return None
 
-    def GetCellText(self, row, col):      # check if custom colour defined for the cell text
+    def GetCellTextColour(self, row, col):      # check if custom colour defined for the cell text
         try:
             set = self.cell_text[row]
         except:
@@ -571,12 +605,15 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
         self.DrawColumns()      # draw all vertical lines
 
     def DrawGridLine(self):
-        if self.draw == True:
+        if self.draw == True \
+        and len(self.column) > 2:    #supress grid lines if only one column
             try:
                 size = self.row_line_size[self.data_cnt]
             except:
                 size = self.row_def_line_size
 
+            if size < 1: return
+
             try:
                 colour = self.row_line_colour[self.data_cnt]
             except:
@@ -586,10 +623,11 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
 
             y_out = self.y
 #            y_out = self.y + self.pt_space_before + self.pt_space_after     # adjust for extra spacing
-            self.DC.DrawLine((self.column[0], y_out), (self.end_x, y_out))
+            self.DC.DrawLine(self.column[0], y_out, self.end_x, y_out)
 
     def DrawColumns(self):
-        if self.draw == True:
+        if self.draw == True \
+        and len(self.column) > 2:   #surpress grid line if only one column
             col = 0
             for val in self.column:
                 try:
@@ -597,6 +635,8 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
                 except:
                     size = self.column_def_line_size
 
+                if size < 1: continue
+
                 try:
                     colour = self.column_line_colour[col]
                 except:
@@ -605,7 +645,7 @@ class PrintTableDraw(wx.ScrolledWindow, PrintBase):
                 indent = val
 
                 self.DC.SetPen(wx.Pen(colour, size))
-                self.DC.DrawLine((indent, self.y_start), (indent, self.y))
+                self.DC.DrawLine(indent, self.y_start, indent, self.y)
                 col = col + 1
 
     def DrawText(self):
@@ -721,8 +761,8 @@ class PrintTable:
         self.footer_type = "Pageof"
 
     def SetMargins(self):
-        self.left_margin = 1.0
-        self.right_margin = 1.0     # only used if no column sizes
+        self.left_margin = 0.5
+        self.right_margin = 0.5    # only used if no column sizes
 
         self.top_margin  = 0.8
         self.bottom_margin = 1.0
@@ -870,15 +910,15 @@ class PrintTable:
         self.footer.append(set)
 
     def Preview(self):
+        data = wx.PrintDialogData(self.printData)
         printout = SetPrintout(self)
         printout2 = SetPrintout(self)
-        self.preview = wx.PrintPreview(printout, printout2, self.printData)
+        self.preview = wx.PrintPreview(printout, printout2, data)
         if not self.preview.Ok():
-            wxMessageBox("There was a problem printing!", "Printing", wx.OK)
+            wx.MessageBox("There was a problem printing!", "Printing", wx.OK)
             return
 
         self.preview.SetZoom(60)        # initial zoom value
-
         frame = wx.PreviewFrame(self.preview, self.parentFrame, "Print preview")
 
         frame.Initialize()
@@ -888,14 +928,13 @@ class PrintTable:
         frame.Show(True)
 
     def Print(self):
-        pdd = wx.PrintDialogData()
-        pdd.SetPrintData(self.printData)
+        pdd = wx.PrintDialogData(self.printData)
         printer = wx.Printer(pdd)
         printout = SetPrintout(self)
         if not printer.Print(self.parentFrame, printout):
             wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
         else:
-            self.printData = printer.GetPrintDialogData().GetPrintData()
+            self.printData = wx.PrintData( printer.GetPrintDialogData().GetPrintData() )
         printout.Destroy()
 
     def DoDrawing(self, DC):
@@ -1021,10 +1060,10 @@ class SetPrintout(wx.Printout):
         self.end_pg = 1000
 
     def OnBeginDocument(self, start, end):
-        return self.base_OnBeginDocument(start, end)
+        return super(SetPrintout, self).OnBeginDocument(start, end)
 
     def OnEndDocument(self):
-        self.base_OnEndDocument()
+        super(SetPrintout, self).OnEndDocument()
 
     def HasPage(self, page):
         try:
@@ -1044,7 +1083,7 @@ class SetPrintout(wx.Printout):
         return (str_pg, end_pg, str_pg, end_pg)
 
     def OnPreparePrinting(self):
-        self.base_OnPreparePrinting()
+        super(SetPrintout, self).OnPreparePrinting()
 
     def OnBeginPrinting(self):
         dc = self.GetDC()
@@ -1060,7 +1099,7 @@ class SetPrintout(wx.Printout):
         scaleY = float(h) / 1000
         self.printUserScale = min(scaleX, scaleY)
 
-        self.base_OnBeginPrinting()
+        super(SetPrintout, self).OnBeginPrinting()
 
     def GetSize(self):
         self.psizew, self.psizeh = self.GetPPIPrinter()
@@ -1092,8 +1131,23 @@ class SetPrintout(wx.Printout):
         self.canvas.DoDrawing(dc)
         return True
 
-
-
-
-
-
+if __name__ == '__main__':
+    app = wx.PySimpleApp()
+    frame = wx.Frame(None, -1, "Dummy wx frame for testing printout.py")
+    frame.Show(True)
+    ptbl = PrintTable(frame)
+    ptbl.SetHeader('This is the test HEADER')
+    # a single sequence will print out as a single column with no borders ...
+    ptbl.data = (
+        'This is the first line of text.',
+        'This is the second line\nand the third. The fourth will be the number "4.0".',
+        04.00,
+        'This is the fifth line, but by design it is too long to fit in the width of a standard'\
+         ' page, so it will be forced to wrap around in order to fit without having '\
+         'some of its verbose verbage truncated.',
+        'Here we have the final line.'
+        )
+    #... but, if labels or columns are defined, a single sequence will print out as a single row
+    ##ptbl.label = ('One','Two','Three','Four','5')
+    ptbl.Preview()
+    app.MainLoop()