Skip to content

refactor non wx.ToolBar specific parts of NavigationToolbar2Wx/Agg into NavigationController2Wx/Agg #10145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
unicode_literals)

from six.moves import xrange
import six

import sys
import os
Expand Down Expand Up @@ -1483,9 +1484,13 @@ def __init__(self, targetfig):
tool = SubplotTool(targetfig, toolfig)


class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
class NavigationController2Wx(NavigationToolbar2):
"""
The parts from NavigationToolbar2Wx that are not specific to wx.ToolBar;
so this functionality can be used without adding a toolbar.
"""

def __init__(self, canvas):
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
NavigationToolbar2.__init__(self, canvas)
self.canvas = canvas
self._idle = True
Expand All @@ -1501,33 +1506,8 @@ def get_canvas(self, frame, fig):
return FigureCanvasWx(frame, -1, fig)

def _init_toolbar(self):
DEBUG_MSG("_init_toolbar", 1, self)

self._parent = self.canvas.GetParent()

self.wx_ids = {}
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
self.AddSeparator()
continue
self.wx_ids[text] = wx.NewId()
wxc._AddTool(self, self.wx_ids, text,
_load_bitmap(image_file + '.png'),
tooltip_text)

self.Bind(wx.EVT_TOOL, getattr(self, callback),
id=self.wx_ids[text])

self.Realize()

def zoom(self, *args):
self.ToggleTool(self.wx_ids['Pan'], False)
NavigationToolbar2.zoom(self, *args)

def pan(self, *args):
self.ToggleTool(self.wx_ids['Zoom'], False)
NavigationToolbar2.pan(self, *args)

def configure_subplots(self, evt):
frame = wx.Frame(None, -1, "Configure subplots")

Expand Down Expand Up @@ -1688,6 +1668,40 @@ def set_message(self, s):
if self.statbar is not None:
self.statbar.set_function(s)


class NavigationToolbar2Wx(NavigationController2Wx, wx.ToolBar):
def __init__(self, canvas):
wx.ToolBar.__init__(self, canvas.GetParent(), -1)
NavigationController2Wx.__init__(self, canvas)

def _init_toolbar(self):
DEBUG_MSG("_init_toolbar", 1, self)

self._parent = self.canvas.GetParent()

self.wx_ids = {}
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
self.AddSeparator()
continue
self.wx_ids[text] = wx.NewId()
wxc._AddTool(self, self.wx_ids, text,
_load_bitmap(image_file + '.png'),
tooltip_text)

self.Bind(wx.EVT_TOOL, getattr(self, callback),
id=self.wx_ids[text])

self.Realize()

def zoom(self, *args):
self.ToggleTool(self.wx_ids['Pan'], False)
NavigationToolbar2.zoom(self, *args)

def pan(self, *args):
self.ToggleTool(self.wx_ids['Zoom'], False)
NavigationToolbar2.pan(self, *args)

def set_history_buttons(self):
can_backward = self._nav_stack._pos > 0
can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/backends/backend_wxagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from . import wx_compat as wxc
from . import backend_wx
from .backend_agg import FigureCanvasAgg
from .backend_wx import (
_BackendWx, FigureCanvasWx, FigureFrameWx, NavigationToolbar2Wx, DEBUG_MSG)
from .backend_wx import (_BackendWx, FigureCanvasWx, FigureFrameWx,
NavigationToolbar2Wx, NavigationController2Wx, DEBUG_MSG)


class FigureFrameWxAgg(FigureFrameWx):
Expand Down Expand Up @@ -90,6 +90,11 @@ def print_figure(self, filename, *args, **kwargs):
self.draw()


class NavigationController2WxAgg(NavigationController2Wx):
def get_canvas(self, frame, fig):
return FigureCanvasWxAgg(frame, -1, fig)


class NavigationToolbar2WxAgg(NavigationToolbar2Wx):
def get_canvas(self, frame, fig):
return FigureCanvasWxAgg(frame, -1, fig)
Expand Down