@@ -237,6 +237,75 @@ def resize(self, event):
237
237
self .resize_event ()
238
238
self .show ()
239
239
240
+ # a resizing will in general move the pointer position
241
+ # relative to the canvas, so process it as a motion notify
242
+ # event. An intended side effect of this call is to allow
243
+ # window raises (which trigger a resize) to get the cursor
244
+ # position to the mpl event framework so key presses which are
245
+ # over the axes will work w/o clicks or explicit motion
246
+ self ._update_pointer_position (event )
247
+
248
+ def _update_pointer_position (self , guiEvent = None ):
249
+ """
250
+ Figure out if we are inside the canvas or not and update the
251
+ canvas enter/leave events
252
+ """
253
+ # if the pointer if over the canvas, set the lastx and lasty
254
+ # attrs of the canvas so it can process event w/o mouse click
255
+ # or move
256
+
257
+ # the window's upper, left coords in screen coords
258
+ xw = self ._tkcanvas .winfo_rootx ()
259
+ yw = self ._tkcanvas .winfo_rooty ()
260
+ # the pointer's location in screen coords
261
+ xp , yp = self ._tkcanvas .winfo_pointerxy ()
262
+
263
+ # not figure out the canvas coordinates of the pointer
264
+ xc = xp - xw
265
+ yc = yp - yw
266
+
267
+ # flip top/bottom
268
+ yc = self .figure .bbox .height - yc
269
+
270
+ # JDH: this method was written originally to get the pointer
271
+ # location to the backend lastx and lasty attrs so that events
272
+ # like KeyEvent can be handled without mouse events. Eg, if
273
+ # the cursor is already above the axes, then key presses like
274
+ # 'g' should toggle the grid. In order for this to work in
275
+ # backend_bases, the canvas needs to know _lastx and _lasty.
276
+ # There are three ways to get this info the canvas:
277
+ #
278
+ # 1) set it explicity
279
+ #
280
+ # 2) call enter/leave events explicity. The downside of this
281
+ # in the impl below is that enter could be repeatedly
282
+ # triggered if thes mouse is over the axes and one is
283
+ # resizing with the keyboard. This is not entirely bad,
284
+ # because the mouse position relative to the canvas is
285
+ # changing, but it may be surprising to get repeated entries
286
+ # without leaves
287
+ #
288
+ # 3) process it as a motion notify event. This also has pros
289
+ # and cons. The mouse is moving relative to the window, but
290
+ # this may surpise an event handler writer who is getting
291
+ # motion_notify_events even if the mouse has not moved
292
+
293
+ # here are the three scenarios
294
+ if 1 :
295
+ # just manually set it
296
+ self ._lastx , self ._lasty = xc , yc
297
+ elif 0 :
298
+ # alternate implementation: process it as a motion
299
+ FigureCanvasBase .motion_notify_event (self , xc , yc , guiEvent )
300
+ elif 0 :
301
+ # alternate implementation -- process enter/leave events
302
+ # instead of motion/notify
303
+ if self .figure .bbox .contains (xc , yc ):
304
+ self .enter_notify_event (guiEvent , xy = (xc ,yc ))
305
+ else :
306
+ self .leave_notify_event (guiEvent )
307
+
308
+
240
309
def draw (self ):
241
310
FigureCanvasAgg .draw (self )
242
311
tkagg .blit (self ._tkphoto , self .renderer ._renderer , colormode = 2 )
0 commit comments