Hello,
I am trying to get a menu to popup at the coordinates of mouse pointer with a set of actions.
This is part of the work to convert the D-rats application from PyGtk to at least Gtk 3 with close to the same functionality. I am not the original author.
I am getting the menu popup to appear on the right click, but none of the actions can be selected.
# map/mappopupmenu.py
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gio
class MapPopupModel(Gio.Menu):
def __init__(self, position):
Gio.Menu.__init__(self)
menu_popup = Gio.Menu()
menu_popup.append(_("Center here"), 'win.center')
@staticmethod
def add_actions(window):
action_center = Gio.SimpleAction(name='center',
parameter_type=None,
enabled=True)
action_center.connect('activate', window.popup_center_handler)
window.add_action(action_center)
#mapwidget.py
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gdk
class MapWidget(Gtk.DrawingArea):
def __init__(self, width, height, tilesize=256, window=None):
Gtk.DrawingArea.__init__(self)
self.tilesize = tilesize
self.set_size_request(self.tilesize * self.width,
self.tilesize * self.height)
#mapwindow.py
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
from . import mappopmodel.MapPopupModel
from . import mapwidget.MapWidget
class MapWindow(Gtk.ApplicationWindow):
def __init__(self, application, config):
Gtk.ApplicationWindow.__init__(self, application=application)
self.map_widget = MapWidget(width=9, height=9,
window=self)
self.map_widget.show()
self.scrollw = Gtk.ScrolledWindow()
self.scrollw.add(self.map_widget)
self.scrollw.show()
self.map_widget.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
self.map_widget.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.map_widget.connect("motion-notify-event", self._mouse_move_event)
self.scrollw.connect("button-press-event", self._mouse_click_event)
self.set_default_size(800, 600)
MapPopupModel.add_actions(self)
def _mouse_click_event(self, widget, event):
position="10, 100" # Actually where mouse is on map
popup_model = Map.PopupModel(position)
popup_menu = Gtk.Menu.new_from_model(popup_model)
popup_menu.popup_at_pointer()
def popup_center_handler(self, _action, _value):
print("action", type(_action))
print("value", type(_value))