Revision: 8441
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8441&view=rev
Author:   efiring
Date:     2010-06-19 23:46:47 +0000 (Sat, 19 Jun 2010)

Log Message:
-----------
[1530104, 3017380] slider grabs mouse; patch by C. Gohlke and baxissimo

Modified Paths:
--------------
    trunk/matplotlib/lib/matplotlib/backend_bases.py
    trunk/matplotlib/lib/matplotlib/widgets.py

Modified: trunk/matplotlib/lib/matplotlib/backend_bases.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/backend_bases.py    2010-06-17 17:45:38 UTC 
(rev 8440)
+++ trunk/matplotlib/lib/matplotlib/backend_bases.py    2010-06-19 23:46:47 UTC 
(rev 8441)
@@ -1122,7 +1122,10 @@
             return
 
         # Find all axes containing the mouse
-        axes_list = [a for a in self.canvas.figure.get_axes() if 
a.in_axes(self)]
+        if self.canvas.mouse_grabber is None:
+            axes_list = [a for a in self.canvas.figure.get_axes() if 
a.in_axes(self)]
+        else:
+            axes_list = [self.canvas.mouse_grabber]
 
         if len(axes_list) == 0: # None found
             self.inaxes = None
@@ -1332,6 +1335,7 @@
         self._lastx, self._lasty = None, None
         self.button_pick_id = self.mpl_connect('button_press_event',self.pick)
         self.scroll_pick_id = self.mpl_connect('scroll_event',self.pick)
+        self.mouse_grabber = None # the axes currently grabbing mouse
 
         if False:
             ## highlight the artists that are hit
@@ -1610,6 +1614,26 @@
         event = IdleEvent(s, self, guiEvent=guiEvent)
         self.callbacks.process(s, event)
 
+    def grab_mouse(self, ax):
+        """
+        Set the child axes which are currently grabbing the mouse events.
+        Usually called by the widgets themselves.
+        It is an error to call this if the mouse is already grabbed by
+        another axes.
+        """
+        if self.mouse_grabber not in (None, ax):
+            raise RuntimeError('two different attempted to grab mouse input')
+        self.mouse_grabber = ax
+
+    def release_mouse(self, ax):
+        """
+        Release the mouse grab held by the axes, ax.
+        Usually called by the widgets.
+        It is ok to call this even if you ax doesn't have the mouse grab 
currently.
+        """
+        if self.mouse_grabber is ax:
+            self.mouse_grabber = None
+
     def draw(self, *args, **kwargs):
         """
         Render the :class:`~matplotlib.figure.Figure`

Modified: trunk/matplotlib/lib/matplotlib/widgets.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/widgets.py  2010-06-17 17:45:38 UTC (rev 
8440)
+++ trunk/matplotlib/lib/matplotlib/widgets.py  2010-06-19 23:46:47 UTC (rev 
8441)
@@ -106,6 +106,7 @@
 
 
         ax.figure.canvas.mpl_connect('button_press_event', self._click)
+        ax.figure.canvas.mpl_connect('button_release_event', self._release)
         ax.figure.canvas.mpl_connect('motion_notify_event', self._motion)
         ax.set_navigate(False)
         ax.set_axis_bgcolor(color)
@@ -117,8 +118,21 @@
         self._lastcolor = color
 
     def _click(self, event):
-        if event.inaxes != self.ax: return
-        if not self.eventson: return
+        if event.inaxes != self.ax:
+            return
+        if not self.eventson:
+            return
+        if event.canvas.mouse_grabber != self.ax:
+            event.canvas.grab_mouse(self.ax)
+
+    def _release(self, event):
+        if event.canvas.mouse_grabber != self.ax:
+            return
+        event.canvas.release_mouse(self.ax)
+        if not self.eventson:
+            return
+        if event.inaxes != self.ax: 
+            return
         for cid, func in self.observers.items():
             func(event)
 
@@ -209,6 +223,7 @@
         ax.set_navigate(False)
 
         ax.figure.canvas.mpl_connect('button_press_event', self._update)
+        ax.figure.canvas.mpl_connect('button_release_event', self._update)
         if dragging:
             ax.figure.canvas.mpl_connect('motion_notify_event', self._update)
         self.label = ax.text(-0.02, 0.5, label, transform=ax.transAxes,
@@ -227,14 +242,35 @@
         self.closedmax = closedmax
         self.slidermin = slidermin
         self.slidermax = slidermax
+        self.drag_active  = False
 
     def _update(self, event):
         'update the slider position'
-        if event.button !=1: return
-        if event.inaxes != self.ax: return
+        if event.button != 1:
+            return
+
+        if event.name == 'button_press_event' and event.inaxes == self.ax:
+            self.drag_active = True
+            event.canvas.grab_mouse(self.ax)
+
+        if not self.drag_active: 
+            return
+        
+        elif ((event.name == 'button_release_event') 
+             or (event.name == 'button_press_event' and event.inaxes != 
self.ax)):
+            self.drag_active = False
+            event.canvas.release_mouse(self.ax)
+            return
+
         val = event.xdata
-        if not self.closedmin and val<=self.valmin: return
-        if not self.closedmax and val>=self.valmax: return
+        if val <= self.valmin:
+            if not self.closedmin:
+                return
+            val = self.valmin
+        elif val >= self.valmax:
+            if not self.closedmax:
+                return
+            val = self.valmax
 
         if self.slidermin is not None:
             if val<=self.slidermin.val: return


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to