Ubuntu – Putting an arbitrary gtk.Widget into an appindicator.Indicator

gtkindicatorprogrammingpygtkpython

Putting any gtk.Widget inside a gtk.Menu works, somewhat, by putting an empty gtk.MenuItem in first. It doesn't seem to be reliable – but it basically works. With an appindicator.Indicator, it doesn't work. The entry just stays empty. Even if you set the child[border_size] to 32, for example.

The only widget that works is a gtk.Label (plus the MenuItem, ImageMenuItem and so on of course).

Now the Me Menu has got a gtk.Entry in it, so it must surely be possible to do this Properly. How do I do it?

Here's some example code to explain what I'm on about:

    self.item = gtk.MenuItem()
    self.item.add(gtk.Label("hello world!"))
    self.menu.append(self.item)

The above code works, this however doesn't:

    self.item = gtk.MenuItem()
    self.item.add(gtk.Entry())
    self.menu.append(self.item)

Where self.menu is an appindicator.Indicator, if it were a gtk.Menu, the Entry would be at least displayed.

Here's some working code:

import gtk
import appindicator

class AppIndicator (object):

    def __init__(self):
        self.ind = appindicator.Indicator("hello world client",
            "distributor-logo", appindicator.CATEGORY_APPLICATION_STATUS)
        self.ind.set_status (appindicator.STATUS_ACTIVE)
        self.menu = gtk.Menu()
        item = gtk.MenuItem()

        item.add(gtk.Label("hello world"))
        # item.add(gtk.Entry())

        self.menu.append(item)
        self.menu.show_all()
        self.ind.set_menu(self.menu)


indicator = AppIndicator()
gtk.main()

Best Answer

The Application Indicator menu support is based on D-Bus menus, which are limited in what they support - they only support basic menu functionality, not more exotic things such as arbitrary widgets.

One significant roadblock to them ever supporting such things is the fact that the application indicator menu is rendered by a different process, the application indicator process, so your program doesn't have access to directly draw anything on it. Overcoming this would require either supporting something like X-Embed in D-Bus menus or allowing all of GTK to work over D-Bus.