Thomas Mills Hinkle wrote:

Can anyone point me to a faq/post that explains/hints at how to do
reassignable keybindings for menu items (where a user can set a key
binding for a menu item by typing it when the menu item is highlighted).
I've noticed this feature in a number of GNOME programs and would like
to implement it, but haven't been able to find the proper resource.



You have to set the "gtk-can-change-accels" property of the Settings object to True after you create a Menu:

http://www.pygtk.org/pygtk2reference/class-gtksettings.html

You have to specify the accelerators using accel paths gtk.accel_map_change_entry() and associate accel paths with your menu items using gtk.MenuItem.set_accel_path() or gtk.Menu.set_accel_path():
http://www.pygtk.org/pygtk2reference/class-gtkaccelgroup.html#function-gtk--accel-map-change-entry
http://www.pygtk.org/pygtk2reference/class-gtkmenuitem.html#method-gtkmenuitem--set-accel-path
http://www.pygtk.org/pygtk2reference/class-gtkmenu.html#method-gtkmenu--set-accel-path


Also, I'd like to find the right place to report a UI bug with this
feature -- I've noticed that e.g. in epiphany, you can rebind a regular
old key quite by accident with no warning, leading to mistifying
behavior (such as opening new windows when you're trying to type a new
message). I filed a bug with epiphany but they told me it was a problem
elsewhere in the toolkit. Obviously this isn't a pygtk issue, but I
thought I'd mention it, since if you can answer my first question, you
can probably answer this one too :)


The accel_map entries can be prevented from changing by locking them individually using gtk.accel_map_lock_path():

http://www.pygtk.org/pygtk2reference/class-gtkaccelgroup.html#function-gtk--accel-map-lock-path

I've attached a simple example illustrating that the accelerator on the "Test" menu item can be changed to say "Control-G" but not to "Control-E" which is used by the "Other" menu item which is locked.

Given your description is sounds like your bug should be filed against epiphany.

John


import gtk

settings = gtk.settings_get_default()
w = gtk.Window()
mb = gtk.MenuBar()
w.add(mb)
ag = gtk.AccelGroup()
mi = gtk.MenuItem('Top')
mb.append(mi)
m = gtk.Menu()
settings.set_property('gtk-can-change-accels', True)
mi.set_submenu(m)
mi1 = gtk.MenuItem('Test')
m.append(mi1)
mi2 = gtk.MenuItem('Other')
m.append(mi2)
m.set_accel_group(ag)
w.add_accel_group(ag)
m.set_accel_path('<test>/Top')
gtk.accel_map_change_entry('<test>/Top/Test', ord('d'), gtk.gdk.CONTROL_MASK,
                           True)
gtk.accel_map_change_entry('<test>/Top/Other', ord('e'), gtk.gdk.CONTROL_MASK,
                           True)
gtk.accel_map_lock_path('<test>/Top/Other')
def cb(*args):
    print args

mi1.connect('activate', cb)
mi2.connect('activate', cb)

w.show_all()

gtk.main()
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to