diff --git a/lib/matplotlib/backend_tools.py b/lib/matplotlib/backend_tools.py index 803ce1c02c26..cb0349cee8c1 100644 --- a/lib/matplotlib/backend_tools.py +++ b/lib/matplotlib/backend_tools.py @@ -1039,25 +1039,25 @@ def _format_tool_keymap(self, name): keymaps = self.toolmanager.get_tool_keymap(name) return ", ".join(self.format_shortcut(keymap) for keymap in keymaps) - def _get_help_text(self): + def _get_help_entries(self): entries = [] for name, tool in sorted(self.toolmanager.tools.items()): if not tool.description: continue - entries.append( - "{}: {}\n\t{}".format( - name, self._format_tool_keymap(name), tool.description)) + entries.append((name, self._format_tool_keymap(name), + tool.description)) + return entries + + def _get_help_text(self): + entries = self._get_help_entries() + entries = ["{}: {}\n\t{}".format(*entry) for entry in entries] return "\n".join(entries) def _get_help_html(self): fmt = "{}{}{}" rows = [fmt.format( "Action", "Shortcuts", "Description")] - for name, tool in sorted(self.toolmanager.tools.items()): - if not tool.description: - continue - rows.append(fmt.format( - name, self._format_tool_keymap(name), tool.description)) + rows += [fmt.format(*row) for row in self._get_help_entries()] return ("" "" + rows[0] + "" "".join(rows[1:]) + "
") diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 7c684e76cdd9..652151106ad6 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -1824,6 +1824,57 @@ def remove_rubberband(self, dc=None): self._rect = None +class _HelpDialog(wx.Dialog): + _instance = None # a reference to an open dialog singleton + headers = [("Action", "Shortcuts", "Description")] + widths = [100, 140, 300] + + def __init__(self, parent, help_entries): + wx.Dialog.__init__(self, parent, title="Help", + style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER) + + sizer = wx.BoxSizer(wx.VERTICAL) + grid_sizer = wx.FlexGridSizer(0, 3, 8, 6) + # create and add the entries + bold = self.GetFont().MakeBold() + for r, row in enumerate(self.headers + help_entries): + for (col, width) in zip(row, self.widths): + label = wx.StaticText(self, label=col) + if r == 0: + label.SetFont(bold) + label.Wrap(width) + grid_sizer.Add(label, 0, 0, 0) + # finalize layout, create button + sizer.Add(grid_sizer, 0, wx.ALL, 6) + OK = wx.Button(self, wx.ID_OK) + sizer.Add(OK, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 8) + self.SetSizer(sizer) + sizer.Fit(self) + self.Layout() + self.Bind(wx.EVT_CLOSE, self.OnClose) + OK.Bind(wx.EVT_BUTTON, self.OnClose) + + def OnClose(self, evt): + _HelpDialog._instance = None # remove global reference + self.DestroyLater() + evt.Skip() + + @classmethod + def show(cls, parent, help_entries): + # if no dialog is shown, create one; otherwise just re-raise it + if cls._instance: + cls._instance.Raise() + return + cls._instance = cls(parent, help_entries) + cls._instance.Show() + + +class HelpWx(backend_tools.ToolHelpBase): + def trigger(self, *args): + _HelpDialog.show(self.figure.canvas.GetTopLevelParent(), + self._get_help_entries()) + + class ToolCopyToClipboardWx(backend_tools.ToolCopyToClipboardBase): def trigger(self, *args, **kwargs): if not self.canvas._isDrawn: @@ -1839,6 +1890,7 @@ def trigger(self, *args, **kwargs): backend_tools.ToolSaveFigure = SaveFigureWx backend_tools.ToolSetCursor = SetCursorWx backend_tools.ToolRubberband = RubberbandWx +backend_tools.ToolHelp = HelpWx backend_tools.ToolCopyToClipboard = ToolCopyToClipboardWx