Skip to content

Commit 90e40a1

Browse files
committed
gtk3&4
1 parent 2fea63e commit 90e40a1

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,20 @@ class FigureManagerGTK3(_FigureManagerGTK):
591591
_toolbar2_class = NavigationToolbar2GTK3
592592
_toolmanager_toolbar_class = ToolbarGTK3
593593

594+
def context_menu(self, event, labels=None, actions=None):
595+
if labels is None or actions is None:
596+
return
597+
menu = Gtk.Menu()
598+
for label, action in zip(labels, actions):
599+
item = Gtk.MenuItem(label=label)
600+
menu.append(item)
601+
def draw_lambda(_w, a=action):
602+
a()
603+
self.canvas.draw()
604+
item.connect('activate', draw_lambda)
605+
item.show()
606+
menu.popup_at_pointer(event.guiEvent)
607+
594608

595609
@_BackendGTK.export
596610
class _BackendGTK3(_BackendGTK):

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,34 @@ class FigureManagerGTK4(_FigureManagerGTK):
637637
_toolbar2_class = NavigationToolbar2GTK4
638638
_toolmanager_toolbar_class = ToolbarGTK4
639639

640+
def context_menu(self, event, labels=None, actions=None):
641+
if labels is None or actions is None:
642+
return
643+
popover = Gtk.Popover()
644+
popover.set_parent(self.window)
645+
popover.set_has_arrow(False)
646+
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
647+
popover.set_child(box)
648+
for label, action in zip(labels, actions):
649+
btn = Gtk.Button(label=label)
650+
def draw_lambda(_btn, a=action, p=popover):
651+
a()
652+
self.canvas.draw()
653+
p.popdown()
654+
btn.connect('clicked', draw_lambda)
655+
box.append(btn)
656+
scale = self.canvas.get_scale_factor()
657+
x_canvas = (event.x / scale)
658+
y_canvas = ((self.canvas.get_height() * scale - event.y) / scale)
659+
x_win,y_win = self.canvas.translate_coordinates(self.window, x_canvas, y_canvas)
660+
rect = Gdk.Rectangle()
661+
rect.x = x_win
662+
rect.y = y_win
663+
rect.width = 1
664+
rect.height = 1
665+
popover.set_pointing_to(rect)
666+
popover.popup()
667+
640668

641669
@_BackendGTK.export
642670
class _BackendGTK4(_BackendGTK):

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def __init__(
188188
pseudo_bbox = self.transLimits.inverted().transform([(0, 0), (1, 1)])
189189
self._pseudo_w, self._pseudo_h = pseudo_bbox[1] - pseudo_bbox[0]
190190

191-
self._right_click_moved = False
191+
self._mouse_moved = False
192192

193193
# mplot3d currently manages its own spines and needs these turned off
194194
# for bounding box calculations
@@ -1360,7 +1360,7 @@ def clear(self):
13601360

13611361
def _button_press(self, event):
13621362
if event.inaxes == self:
1363-
self._right_click_moved = False
1363+
self._mouse_moved = False
13641364
self.button_pressed = event.button
13651365
self._sx, self._sy = event.xdata, event.ydata
13661366
toolbar = self.get_figure(root=True).canvas.toolbar
@@ -1373,7 +1373,7 @@ def _button_release(self, event):
13731373
self.button_pressed = None
13741374

13751375
if event.button in self._zoom_btn and event.inaxes == self \
1376-
and not self._right_click_moved:
1376+
and not self._mouse_moved:
13771377
canvas = self.get_figure(root=True).canvas
13781378
canvas.manager.context_menu(
13791379
event,
@@ -1638,7 +1638,7 @@ def _on_move(self, event):
16381638
# Zoom
16391639
elif self.button_pressed in self._zoom_btn:
16401640
# zoom view (dragging down zooms in)
1641-
self._right_click_moved = True
1641+
self._mouse_moved = True
16421642
scale = h/(h - dy)
16431643
self._scale_axis_limits(scale, scale, scale)
16441644

0 commit comments

Comments
 (0)