1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
| import sys
import os
import wx
BUFFERED = 1
#-------------------------------------------------------------------------------
class My_Canvas(wx.ScrolledWindow):
def __init__(self, parent, txt, id=-1, size=wx.DefaultSize):
wx.ScrolledWindow.__init__(self, parent, id,
pos=(0, 0),
size=size,
style=wx.SUNKEN_BORDER)
#------------
self.parent = parent
#------------
self.maxWidth = 1000
self.maxHeight = 1000
self.x = self.y = 0
self.drawing = False
self.lines = txt
#------------
self.SetBackgroundColour("WHITE")
#------------
self.SetVirtualSize((self.maxWidth, self.maxHeight))
self.SetScrollRate(30, 30)
#------------
if BUFFERED:
# Initialize the buffer bitmap. No real DC is needed at this point
#self.buffer = wx.EmptyBitmap(self.maxWidth, self.maxHeight)
self.buffer = wx.Bitmap(self.maxWidth, self.maxHeight)
dc = wx.BufferedDC(None, self.buffer)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
self.DoDrawing(dc)
#------------
# Bind a paint event to an events handler
self.Bind(wx.EVT_PAINT, self.OnPaint)
#---------------------------------------------------------------------------
def GetWidth(self):
return self.maxWidth
def GetHeight(self):
return self.maxHeight
def OnPaint(self, event):
if BUFFERED:
# Create a buffered paint DC. It will create the real
# wx.PaintDC and then blit the bitmap to it when dc is
# deleted. Since we don't need to draw anything else
# here that's all there is to it.
dc = wx.BufferedPaintDC(self, self.buffer, wx.BUFFER_VIRTUAL_AREA)
else:
dc = wx.PaintDC(self)
self.PrepareDC(dc)
# Since we're not buffering in this case, we have to
# (re)paint the all the contents of the window, which can
# be potentially time consuming and flickery depending on
# what is being drawn and how much of it there is.
self.DoDrawing(dc)
def DoDrawing(self, dc, printing=False):
"""
"""
pen = wx.Pen('BLACK', 4, wx.SOLID)
dc.SetTextForeground("BLACK")
dc.SetFont(wx.Font(48, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
dc.DrawText(self.lines, 6, 57)
#-------------------------------------------------------------------------------
class My_Printout(wx.Printout):
"""
"""
def __init__(self, canvas, title):
wx.Printout.__init__(self, title)
#------------
self.canvas = canvas
#---------------------------------------------------------------------------
def OnBeginDocument(self, start, end):
"""
"""
return super(My_Printout, self).OnBeginDocument(start, end)
def OnEndDocument(self):
"""
"""
super(My_Printout, self).OnEndDocument()
def OnBeginPrinting(self):
"""
"""
super(My_Printout, self).OnBeginPrinting()
def OnEndPrinting(self):
"""
"""
super(My_Printout, self).OnEndPrinting()
def OnPreparePrinting(self):
"""
"""
super(My_Printout, self).OnPreparePrinting()
def HasPage(self, page):
"""
"""
if page <= 2:
return True
else:
return False
def GetPageInfo(self):
"""
"""
return (1, 2, 1, 2)
def OnPrintPage(self, page):
"""
"""
dc = self.GetDC()
#------------
# One possible method of setting scaling factors...
maxX = self.canvas.GetWidth()
maxY = self.canvas.GetHeight()
#------------
# Let's have at least 50 device units margin.
marginX = 180
marginY = 180
#------------
# Add the margin to the graphic size.
maxX = maxX + (2 * marginX)
maxY = maxY + (2 * marginY)
#------------
# Get the size of the DC in pixels.
#(w, h) = dc.GetSizeTuple()
(w, h) = dc.GetSize()
# Calculate a suitable scaling factor.
scaleX = float(w) / maxX
scaleY = float(h) / maxY
# Use x or y scaling factor, whichever fits on the DC.
actualScale = min(scaleX, scaleY)
# Calculate the position on the DC for centering the graphic.
posX = (w - (self.canvas.GetWidth() * actualScale)) / 2.0
posY = (h - (self.canvas.GetHeight() * actualScale)) / 2.0
# Set the scale and origin.
dc.SetUserScale(actualScale, actualScale)
dc.SetDeviceOrigin(int(posX), int(posY))
#------------
self.canvas.DoDrawing(dc, True)
return True
#-------------------------------------------------------------------------------
class My_Frame(wx.Frame):
"""
"""
def __init__(self, parent, id, title=""):
wx.Frame.__init__(self,
parent,
id,
title,
size=(600, 367),
style=wx.DEFAULT_FRAME_STYLE)
#------------
# Simplified init method.
self.CreateCtrls()
self.CreatePrintData()
self.BindEvents()
self.DoLayout()
#------------
self.CenterOnScreen()
#---------------------------------------------------------------------------
def CreateCtrls(self):
"""
Make widgets for my app.
"""
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetWeight(wx.BOLD)
font.SetPointSize(10)
#------------
# First create the controls.
self.panel = wx.Panel(self,
id=-1,
style=wx.BORDER_THEME|
wx.TAB_TRAVERSAL)
self.tc = wx.TextCtrl(self.panel,
id=-1,
size=(200, -1),
value="Hello, World ! A sample text.")
text = self.tc.GetValue()
self.canvas = My_Canvas(self.panel, text, size=(0, 0))
self.btnSetup = wx.Button(self.panel,
id=wx.ID_PAGE_SETUP,
label="Page set&up")
self.btnPreview = wx.Button(self.panel,
id=wx.ID_PREVIEW,
label="Print Pre&view")
self.btnPrint = wx.Button(self.panel,
id=wx.ID_PRINT,
label="&Print")
self.btnClose = wx.Button(self.panel,
id=wx.ID_CLOSE,
label="E&xit")
def CreatePrintData(self):
"""
Create printing data.
"""
self.printdata = wx.PrintData()
self.printdata.SetPrinterName('')
self.printdata.SetOrientation(wx.PORTRAIT)
self.printdata.SetPaperId(wx.PAPER_A4)
self.printdata.SetQuality(wx.PRINT_QUALITY_DRAFT)
# Black and white printing if (False) :
self.printdata.SetColour(True)
self.printdata.SetNoCopies(1)
self.printdata.SetCollate(True)
# self.printData.SetPrintMode(wx.PRINT_MODE_PRINTER)
def BindEvents(self):
"""
Bind all the events related to my application.
"""
# Bind some menu events to an events handler.
self.Bind(wx.EVT_MENU, self.OnBtnPageSetup, id=wx.ID_PAGE_SETUP)
self.Bind(wx.EVT_MENU, self.OnBtnPreview, id=wx.ID_PREVIEW)
self.Bind(wx.EVT_MENU, self.OnBtnPrint, id=wx.ID_PRINT)
self.Bind(wx.EVT_MENU, self.OnBtnClose, id=wx.ID_EXIT)
# Bind the close event to an event handler.
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
# Bind some buttons events to an events handler.
self.Bind(wx.EVT_BUTTON, self.OnBtnPageSetup, self.btnSetup)
self.Bind(wx.EVT_BUTTON, self.OnBtnPreview, self.btnPreview)
self.Bind(wx.EVT_BUTTON, self.OnBtnPrint, self.btnPrint)
self.Bind(wx.EVT_BUTTON, self.OnBtnClose, self.btnClose)
def DoLayout(self):
"""
"""
mainSizer = wx.BoxSizer(wx.VERTICAL)
#------------
hBox2 = wx.BoxSizer(wx.HORIZONTAL)
hBox2.Add(self.btnSetup, 0, wx.ALL, 10)
hBox2.Add(self.btnPreview, 0, wx.ALL, 10)
hBox2.Add(self.btnPrint, 0, wx.ALL, 10)
hBox2.Add(self.btnClose, 0, wx.ALL, 10)
#------------
mainSizer.Add(wx.StaticLine(self.panel),
0, wx.EXPAND|wx.TOP|wx.BOTTOM, 5)
mainSizer.Add(self.tc, 0, wx.ALL, 15)
mainSizer.Add(hBox2, 0, wx.ALL, 5)
#------------
self.panel.SetSizer(mainSizer)
def OnBtnPageSetup(self, event):
"""
"""
psdd = wx.PageSetupDialogData(self.printdata)
psdd.EnablePrinter(True)
#------------
dlg = wx.PageSetupDialog(self, psdd)
dlg.ShowModal()
#------------
# This makes a copy of the wx.PrintData instead of just saving
# a reference to the one inside the PrintDialogData that will
# be destroyed when the dialog is destroyed
self.printdata = wx.PrintData(dlg.GetPageSetupData().GetPrintData())
dlg.Destroy()
def OnBtnPreview(self, event):
"""
"""
data = wx.PrintDialogData(self.printdata)
printout1 = My_Printout(self.canvas, "- Mon document")
printout2 = My_Printout(self.canvas, "- Mon document")
printPreview = wx.PrintPreview(printout1, printout2, data)
# Initial zoom value.
if "__WXMAC__" in wx.PlatformInfo:
printPreview.SetZoom(50)
else:
printPreview.SetZoom(35)
if not printPreview.IsOk():
wx.MessageBox(("Il y a un problème d'impression.\nPeut-être "\
"que votre imprimante n'est \npas "\
"réglée correctement ?"),
("Impression"),
wx.OK)
return
else:
previewFrame = wx.PreviewFrame(printPreview, None, "Prévisualisation")
previewFrame.Initialize()
previewFrame.SetPosition(self.GetPosition())
previewFrame.SetSize(self.GetSize())
previewFrame.Show(True)
previewFrame.Layout()
def OnBtnPrint(self, event):
"""
"""
pdd = wx.PrintDialogData(self.printdata)
pdd.SetPrintData(self.printdata)
pdd.SetMinPage(1)
pdd.SetMaxPage(1)
pdd.SetFromPage(1)
pdd.SetToPage(1)
pdd.SetPrintToFile(False)
#------------
printer = wx.Printer(pdd)
myPrintout = My_Printout(self.canvas, "- Mon document")
if not printer.Print(self, myPrintout, True):
wx.MessageBox(("Il y a un problème d'impression.\nPeut-être "\
"que votre imprimante n'est \npas "\
"réglée correctement ?"),
("Impression"),
wx.OK)
return
else:
self.printData = wx.PrintData(printer.GetPrintDialogData().GetPrintData())
myPrintout.Destroy()
def OnBtnClose(self, event):
"""
"""
self.Close(True)
def OnCloseWindow(self, event):
"""
"""
self.Destroy()
#-------------------------------------------------------------------------------
class My_App(wx.App):
"""
"""
def OnInit(self):
self.locale = wx.Locale(wx.LANGUAGE_FRENCH)
#------------
frame = My_Frame(None, id=-1, title="test")
self.SetTopWindow(frame)
frame.Show(True)
return True
#-------------------------------------------------------------------------------
def main():
app = My_App(False)
app.MainLoop()
#-------------------------------------------------------------------------------
if __name__ == "__main__" :
main() |
Partager