printrun-src/printrun/gui/bufferedcanvas.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rw-r--r--

updated and added new files for printrun

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 BufferedCanvas -- flicker-free canvas widget
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 Copyright (C) 2005, 2006 Daniel Keep, 2011 Duane Johnson
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 To use this widget, just override or replace the draw method.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 This will be called whenever the widget size changes, or when
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 the update method is explicitly called.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 Please submit any improvements/bugfixes/ideas to the following
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 url:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 http://wiki.wxpython.org/index.cgi/BufferedCanvas
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 2006-04-29: Added bugfix for a crash on Mac provided by Marc Jans.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 # Hint: try removing '.sp4msux0rz'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 __author__ = 'Daniel Keep <daniel.keep.sp4msux0rz@gmail.com>'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 __license__ = """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 __all__ = ['BufferedCanvas']
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 class BufferedCanvas(wx.Panel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 Implements a flicker-free canvas widget.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 Standard usage is to subclass this class, and override the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 draw method. The draw method is passed a device context, which
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 should be used to do your drawing.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 If you want to force a redraw (for whatever reason), you should
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 call the update method. This is because the draw method is never
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 called as a result of an EVT_PAINT event.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 # These are our two buffers. Just be aware that when the buffers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 # are flipped, the REFERENCES are swapped. So I wouldn't want to
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 # try holding onto explicit references to one or the other ;)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 buffer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 backbuffer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 def __init__(self,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 parent,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 ID=-1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 pos = wx.DefaultPosition,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 size = wx.DefaultSize,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 style = wx.NO_FULL_REPAINT_ON_RESIZE | wx.WANTS_CHARS):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 wx.Panel.__init__(self, parent, ID, pos, size, style)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 # Bind events
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 self.Bind(wx.EVT_PAINT, self.onPaint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 # Disable background erasing (flicker-licious)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 def disable_event(*pargs, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 pass # the sauce, please
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.Bind(wx.EVT_ERASE_BACKGROUND, disable_event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 # General methods
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 def draw(self, dc, w, h):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 Stub: called when the canvas needs to be re-drawn.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 def update(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 Causes the canvas to be updated.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 self.Refresh()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 def getWidthHeight(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
93 width, height = self.GetClientSize()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 if width == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 width = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 if height == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 height = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 return (width, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 # Event handlers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 def onPaint(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 # Blit the front buffer to the screen
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
106 w, h = self.GetClientSize()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 if not w or not h:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 dc = wx.BufferedPaintDC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.draw(dc, w, h)

mercurial