diff --git a/lib/matplotlib/backends/_backend_gtk.py b/lib/matplotlib/backends/_backend_gtk.py index 2b095c3bc7a9..2652e95d9519 100644 --- a/lib/matplotlib/backends/_backend_gtk.py +++ b/lib/matplotlib/backends/_backend_gtk.py @@ -5,10 +5,9 @@ import logging import matplotlib as mpl -from matplotlib import backend_tools, cbook -from matplotlib.backend_bases import ( - _Backend, NavigationToolbar2, TimerBase, -) +from matplotlib import _api, backend_tools, cbook +from matplotlib.backend_bases import _Backend, NavigationToolbar2, TimerBase +from matplotlib.backend_tools import Cursors # The GTK3/GTK4 backends will have already called `gi.require_version` to set # the desired GTK. @@ -62,6 +61,18 @@ def _create_application(): return _application +def mpl_to_gtk_cursor_name(mpl_cursor): + return _api.check_getitem({ + Cursors.MOVE: "move", + Cursors.HAND: "pointer", + Cursors.POINTER: "default", + Cursors.SELECT_REGION: "crosshair", + Cursors.WAIT: "wait", + Cursors.RESIZE_HORIZONTAL: "ew-resize", + Cursors.RESIZE_VERTICAL: "ns-resize", + }, cursor=mpl_cursor) + + class TimerGTK(TimerBase): """Subclass of `.TimerBase` using GTK timer events.""" diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index 1bd8b7334eca..dbbb4c375e1c 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -8,11 +8,9 @@ from matplotlib import _api, backend_tools, cbook from matplotlib._pylab_helpers import Gcf from matplotlib.backend_bases import ( - _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2, - TimerBase, ToolContainerBase) + FigureCanvasBase, FigureManagerBase, ToolContainerBase) from matplotlib.backend_tools import Cursors from matplotlib.figure import Figure -from matplotlib.widgets import SubplotTool try: import gi @@ -31,7 +29,6 @@ from gi.repository import Gio, GLib, GObject, Gtk, Gdk from . import _backend_gtk from ._backend_gtk import ( - _create_application, _shutdown_application, backend_version, _BackendGTK, _NavigationToolbar2GTK, TimerGTK as TimerGTK3, ) @@ -68,16 +65,9 @@ def cursord(self): @functools.lru_cache() def _mpl_to_gtk_cursor(mpl_cursor): - name = _api.check_getitem({ - Cursors.MOVE: "move", - Cursors.HAND: "pointer", - Cursors.POINTER: "default", - Cursors.SELECT_REGION: "crosshair", - Cursors.WAIT: "wait", - Cursors.RESIZE_HORIZONTAL: "ew-resize", - Cursors.RESIZE_VERTICAL: "ns-resize", - }, cursor=mpl_cursor) - return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name) + return Gdk.Cursor.new_from_name( + Gdk.Display.get_default(), + _backend_gtk.mpl_to_gtk_cursor_name(mpl_cursor)) class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase): @@ -317,10 +307,10 @@ class FigureManagerGTK3(FigureManagerBase): The Gtk.VBox containing the canvas and toolbar window : Gtk.Window The Gtk.Window - """ + def __init__(self, canvas, num): - app = _create_application() + app = _backend_gtk._create_application() self.window = Gtk.Window() app.add_window(self.window) super().__init__(canvas, num) @@ -481,7 +471,7 @@ def __init__(self, canvas, window): self.show_all() - NavigationToolbar2.__init__(self, canvas) + _NavigationToolbar2GTK.__init__(self, canvas) def save_figure(self, *args): dialog = Gtk.FileChooserDialog( @@ -747,7 +737,7 @@ def error_msg_gtk(msg, parent=None): FigureCanvasGTK3, _backend_gtk.RubberbandGTK) -@_Backend.export +@_BackendGTK.export class _BackendGTK3(_BackendGTK): FigureCanvas = FigureCanvasGTK3 FigureManager = FigureManagerGTK3 diff --git a/lib/matplotlib/backends/backend_gtk4.py b/lib/matplotlib/backends/backend_gtk4.py index 74c1317e4eb8..7471bf7fb669 100644 --- a/lib/matplotlib/backends/backend_gtk4.py +++ b/lib/matplotlib/backends/backend_gtk4.py @@ -2,17 +2,12 @@ import io import os from pathlib import Path -import sys import matplotlib as mpl from matplotlib import _api, backend_tools, cbook from matplotlib._pylab_helpers import Gcf from matplotlib.backend_bases import ( - _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2, - TimerBase, ToolContainerBase) -from matplotlib.backend_tools import Cursors -from matplotlib.figure import Figure -from matplotlib.widgets import SubplotTool + FigureCanvasBase, FigureManagerBase, ToolContainerBase) try: import gi @@ -31,24 +26,11 @@ from gi.repository import Gio, GLib, GObject, Gtk, Gdk, GdkPixbuf from . import _backend_gtk from ._backend_gtk import ( - _create_application, _shutdown_application, backend_version, _BackendGTK, _NavigationToolbar2GTK, TimerGTK as TimerGTK4, ) -def _mpl_to_gtk_cursor(mpl_cursor): - return _api.check_getitem({ - Cursors.MOVE: "move", - Cursors.HAND: "pointer", - Cursors.POINTER: "default", - Cursors.SELECT_REGION: "crosshair", - Cursors.WAIT: "wait", - Cursors.RESIZE_HORIZONTAL: "ew-resize", - Cursors.RESIZE_VERTICAL: "ns-resize", - }, cursor=mpl_cursor) - - class FigureCanvasGTK4(Gtk.DrawingArea, FigureCanvasBase): required_interactive_framework = "gtk4" supports_blit = False @@ -108,7 +90,7 @@ def destroy(self): def set_cursor(self, cursor): # docstring inherited - self.set_cursor_from_name(_mpl_to_gtk_cursor(cursor)) + self.set_cursor_from_name(_backend_gtk.mpl_to_gtk_cursor_name(cursor)) def _mouse_event_coords(self, x, y): """ @@ -281,10 +263,10 @@ class FigureManagerGTK4(FigureManagerBase): The Gtk.VBox containing the canvas and toolbar window : Gtk.Window The Gtk.Window - """ + def __init__(self, canvas, num): - app = _create_application() + app = _backend_gtk._create_application() self.window = Gtk.Window() app.add_window(self.window) super().__init__(canvas, num) @@ -422,7 +404,7 @@ def __init__(self, canvas, window): self.message = Gtk.Label() self.append(self.message) - NavigationToolbar2.__init__(self, canvas) + _NavigationToolbar2GTK.__init__(self, canvas) def save_figure(self, *args): dialog = Gtk.FileChooserNative( @@ -691,7 +673,7 @@ def trigger(self, *args, **kwargs): Toolbar = ToolbarGTK4 -@_Backend.export +@_BackendGTK.export class _BackendGTK4(_BackendGTK): FigureCanvas = FigureCanvasGTK4 FigureManager = FigureManagerGTK4