[wxPython-users] Question about drawing program, listing 6.1,
"wxPython in Action"
Robin Dunn
robin at alldunn.com
Mon Feb 4 10:49:16 PST 2008
Chris Carlen wrote:
> Hi:
>
> I have been tinkering with the drawing program (listing 6.1) in the book
> (code quoted below with my comments, modifications, and diagnostics),
> and have grasped about 95% of it.
>
> Now I'm experimenting with one or both of two critical lines which
> affect repainting the Window. I've also made the Window background
> green to distinguish it from the bitmap buffer background (white).
>
> The lines are:
>
> 1. In the SketchWindow class OnIdle method:
> self.Refresh(eraseBackground=False)
>
> 2. In the OnPaint method:
> dc = wx.BufferedPaintDC(self, self.buffer)
>
> I will explain the results of my experiments. Two permutaions I
> understand and a third I don't. The third case is the origin of my
> question. Here are the permutations:
>
> Experiment: OnPaint Refresh()
> 1. not commented commented
> 2. commented commented
> 3. commented not commented
>
> Explanation of experiment 1:
>
> When the window appears, it is green because the bitmap buffer has not
> yet been blitted. That therefore appears to be the main purpose of the
> Refresh() call, to get the initial buffer displayed on the Window. As
> soon as OnMotion gets called by mouse dragging, then the first dropping
> out of scope of the device context in OnMotion causes the buffer to be
> displayed. Thus the background turns white.
>
> This experiment revealed some interesting things. When the Refresh is
> not explicitly called, then I still get Paint events when resizing the
> Window larger, but not smaller. So there is another mechanism which
> creates Paint events besides explicit Refresh calls. Window manager
> activity seems able to cause Paint events to occur.
Paint events are sent to a window by the system anytime some visible
portion of the window is "damaged" which can happen when another window
overlays the window, or a window is shown, or it is resized exposing
areas not seen before, etc. Normally only the damaged portion is
allowed to be drawn in the paint event handler, the rest of the window
shouldn't need painted so it is excluded by a clipping region.
The Refresh() method simply tells the system to think of the window (or
some sub-rectangle of it) as damaged and that it should therefore send a
paint event like it normally would for a normal damage situation. The
purpose of the EVT_PAINT handler is to just (re)draw the current visual
state of the window. In the example you are using a convenience class
to dump a buffer bitmap to the window using a wx.PaintDC since the
buffer already contains the drawing of the doodle lines. In other
situations the paint handler might be more complex and do more actual
drawing, but in the end the purpose is the same.
>
> Explanation of experiment 2:
>
> In this case Paint events are not processed by the line shown above,
> though we get the printed diagnostic telling us when Paint events
> occurred. We find in this situation that everything works as in exp. 1
> until a resize is attempted or another window overlays this one.
>
> Again we start with a green background, then the first drawing activity
> blits the buffer which has the white background. Resizing larger causes
> green Window background to appear where the buffer isn't. The buffer is
> still being resized in this case by InitBuffer, but not getting blitted
> to the Window, until another drawing event.
Without the code in the paint event handler then the only drawing that
gets done is the default erase background handler (simply fills the
window with the background colour) and that which is done in the mouse
motion event handler. There is nothing to fix damaged areas.
>
> Experiment 3:
>
> In this case, once we draw something and get the buffer sent to the
> Window, then any resize whether larger or smaller causes the entire
> Window to turn green. Yet, another window overlaying only causes the
> affected region to turn green.
>
> Why does the entire buffer image get blown away in this case and not in
> experiment 2?
It may depend on platform, as for me 3 works the same as 2.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the wxpython-users
mailing list