Skip to content
Merged
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
19 changes: 15 additions & 4 deletions lib/matplotlib/backends/_backend_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""

Expand Down
26 changes: 8 additions & 18 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
30 changes: 6 additions & 24 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -691,7 +673,7 @@ def trigger(self, *args, **kwargs):
Toolbar = ToolbarGTK4


@_Backend.export
@_BackendGTK.export
class _BackendGTK4(_BackendGTK):
FigureCanvas = FigureCanvasGTK4
FigureManager = FigureManagerGTK4