Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 855fc89

Browse files
committed
Refactored nib and backend for cocoa to work with multiple figures/windows.
svn path=/trunk/matplotlib/; revision=1493
1 parent f7fcc3c commit 855fc89

4 files changed

Lines changed: 40 additions & 23 deletions

File tree

lib/matplotlib/backends/Matplotlib.nib/classes.nib

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/matplotlib/backends/Matplotlib.nib/info.nib

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
-10.7 KB
Binary file not shown.

lib/matplotlib/backends/backend_cocoaagg.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929
from backend_agg import FigureCanvasAgg
3030
import pylab
3131

32+
DEBUG = True
33+
3234
mplBundle = NSBundle.bundleWithPath_(matplotlib.get_data_path())
3335

3436
def new_figure_manager(num, *args, **kwargs):
37+
if DEBUG: print >>sys.stderr, 'new_figure_manager'
3538
thisFig = Figure( *args, **kwargs )
3639
canvas = FigureCanvasAgg(thisFig)
3740
return FigureManagerCocoaAgg(canvas, num)
3841

3942
def show():
43+
if DEBUG: print >>sys.stderr, 'show'
4044
# Let the cocoa run loop take over
4145
NSApplication.sharedApplication().run()
4246

@@ -52,35 +56,43 @@ class MatplotlibController(NibClassBuilder.AutoBaseClass):
5256
# PlotView plotView
5357

5458
def awakeFromNib(self):
55-
self.plotWindow.setAcceptsMouseMovedEvents_(True)
56-
self.plotWindow.useOptimizedDrawing_(True)
57-
self.plotWindow.makeKeyAndOrderFront_(self)
58-
self.plotWindow.makeFirstResponder_(self.plotView)
59-
59+
if DEBUG: print 'MPLController awakeFromNib'
6060
# Get a reference to the active canvas
6161
self.canvas = pylab.get_current_fig_manager().canvas
62+
self.plotWindow.plotView = self.plotView
63+
self.plotView.canvas = self.canvas
64+
65+
# Make imageview first responder for key events
66+
self.plotWindow.makeFirstResponder_(self.plotView)
6267

63-
# Issue a update
64-
self.windowDidResize_(self)
68+
# Issue a resize to update plot
69+
self.plotWindow.windowDidResize_(self)
6570

6671
def saveFigure_(self, sender):
6772
pass
6873

6974
def quit_(self, sender):
7075
pass
7176

77+
class PlotWindow(NibClassBuilder.AutoBaseClass):
78+
def awakeFromNib(self):
79+
if DEBUG: print 'PlotWindow awakeFromNib'
80+
self.setAcceptsMouseMovedEvents_(True)
81+
self.useOptimizedDrawing_(True)
82+
self.makeKeyAndOrderFront_(self)
83+
self.setDelegate_(self)
84+
7285
def windowDidResize_(self, sender):
73-
w,h = self.plotView.frame().size
74-
dpi = self.canvas.figure.dpi.get()
75-
self.canvas.figure.set_figsize_inches(w / dpi, h / dpi)
86+
w,h = self.plotView.bounds().size
87+
dpi = self.plotView.canvas.figure.dpi.get()
88+
self.plotView.canvas.figure.set_figsize_inches(w / dpi, h / dpi)
7689
self.plotView.updatePlot()
7790

7891
class PlotView(NibClassBuilder.AutoBaseClass):
7992
def awakeFromNib(self):
93+
if DEBUG: print 'PlotView awakeFromNib'
8094
self.setImageFrameStyle_(NSImageFrameGroove)
8195

82-
self.canvas = pylab.get_current_fig_manager().canvas
83-
8496
def updatePlot(self):
8597
self.canvas.draw() # tell the agg to render
8698

@@ -106,6 +118,7 @@ def updatePlot(self):
106118
def mouseDown_(self, event):
107119
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
108120
type = event.type()
121+
if DEBUG: print >>sys.stderr, 'mouseDown_:', loc, type
109122
if (type == NSLeftMouseDown):
110123
button = 1
111124
else:
@@ -116,12 +129,14 @@ def mouseDown_(self, event):
116129

117130
def mouseDragged_(self, event):
118131
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
132+
if DEBUG: print >>sys.stderr, 'mouseDragged_:', loc
119133
self.canvas.motion_notify_event(loc.x, loc.y)
120134
self.updatePlot()
121135

122136
def mouseUp_(self, event):
123137
loc = self.convertPoint_fromView_(event.locationInWindow(), None)
124138
type = event.type()
139+
if DEBUG: print >>sys.stderr, 'mouseUp_:', loc, type
125140
if (type == NSLeftMouseUp):
126141
button = 1
127142
else:
@@ -131,12 +146,15 @@ def mouseUp_(self, event):
131146
self.updatePlot()
132147

133148
def keyDown_(self, event):
149+
if DEBUG: print >>sys.stderr, 'keyDown_', event.keyCode()
134150
self.canvas.key_press_event(event.keyCode())
135151

136152
def keyUp_(self, event):
153+
if DEBUG: print >>sys.stderr, 'keyUp_', event.keyCode()
137154
self.canvas.key_release_event(event.keyCode())
138155

139156
class MPLBootstrap(NSObject):
157+
# Loads the nib containing the PlotWindow and PlotView
140158
def startWithBundle_(self, bundle):
141159
NSApplicationLoad()
142160
if not bundle.loadNibFile_externalNameTable_withZone_(
@@ -149,10 +167,14 @@ def startWithBundle_(self, bundle):
149167
class FigureManagerCocoaAgg(FigureManagerBase):
150168
def __init__(self, canvas, num):
151169
FigureManagerBase.__init__(self, canvas, num)
152-
if not WMEnable('Matplotlib'):
153-
print >>sys.stderr, 'Unable to hook to native window manager!'
154-
sys.exit()
155170

171+
# If there are multiple figures we only need to enable once
172+
try:
173+
WMEnable('Matplotlib')
174+
except:
175+
pass
176+
177+
# Load a new PlotWindow
156178
MPLBootstrap.alloc().init().performSelectorOnMainThread_withObject_waitUntilDone_(
157179
'startWithBundle:',
158180
mplBundle,
@@ -204,7 +226,7 @@ def WMEnable(name='Python'):
204226
return False
205227
err = d['CPSEnableForegroundOperation'](psn)
206228
if err:
207-
print >>sys.stderr, 'CPSEnableForegroundOperation', (err, psn)
229+
#print >>sys.stderr, 'CPSEnableForegroundOperation', (err, psn)
208230
return False
209231
err = d['SetFrontProcess'](psn)
210232
if err:

0 commit comments

Comments
 (0)