#!/usr/bin/python

import sys

try:
	import pygtk
	pygtk.require('2.0')
except:
	pass

try:
	import gtk
	import gobject
	import string
except:
	sys.exit(1)



class Test(object):		

	def __init__(self):
		# Allocate a 100x100 window with a clickable
		# label in it.
		self.window = gtk.Window()
		# Shut down when this window is closed.
		self.window.connect(
			'delete-event',
			lambda w, e: gtk.main_quit() )

		
		label = gtk.Label(
			"Clicking here will display a popup menu.\n"
			"\n"
			"- Clicking on 'Normal' should print 'Normal' to "
			"the console.\n"
			"- Clicking on 'Child' *should* print 'Child' to "
			"the console.\n"
			"\n"
			"1.)  Click on 'Normal' to print that string "
			"to the console.  This is the normal case.\n"
			"2.)  Mouse over 'Parent' and click on 'Child'.  "
			"This should print to the console, but doesn't.\n"
			"3.)  Mouse over 'Parent' and mouse over 'Child', "
			"then press ENTER while 'Child' is still "
			"highlighted.  This *will* print 'Child'.\n"
			"4.)\n"
			"   a.)  Click on 'Parent', this will hide the "
			"child menu.\n"
			"   b.)  Click on 'Parent' again, this will "
			"redisplay the child menu.\n"
			"   c.)  Click on 'Child'.  'Child' **will** be "
			"printed to the console!")
		label.set_width_chars(40)
		label.set_property('wrap', True)

		# To make the label clickable, it has to go into
		# an event box to get the events.
		eventbox = gtk.EventBox()
		eventbox.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
		# When a button is release in the event box, call a
		# function to put up a pop-up.
		eventbox.connect_after(
			"button-release-event",
			self.on_button_release_event)
		eventbox.add(label)


		# Add the eventbox to the window and show it.
		self.window.add(eventbox)
		self.window.show_all()


	def on_button_release_event(self, widget, event):
		# Make a popup menu
		popup = gtk.Menu()

		# Add an normal entry to show how things normally work.
		mitem = gtk.MenuItem('_Normal')
		# When it's clicked, just output it's string.
		mitem.connect_after(
			'activate',
			lambda w: sys.stdout.write("Normal\n") )
		popup.append(mitem)

		# Make a submenu accessible via another entry named Parent.
		mitem = gtk.MenuItem('_Parent')
		popup.append(mitem)

		# which means we need a child menu
		childmenu = gtk.Menu()

		# Put a item in the child menu named 'Child'.
		childitem = gtk.MenuItem('_Child')
		# That prints 'Child' when clicked.
		#
		# This is where the failure is.  When you click Child
		# nothing happens.  If you get the mouse over it and
		# press ENTER, it prints like it should.  If you
		# mouse click Parent twice to hide and reshow the
		# child submenu, then clicking on Child works properly.
		childitem.connect_after(
			'activate',
			lambda w: sys.stdout.write("Child\n") )
		childmenu.append(childitem)

		# Set Child as the submenu of Parent
		mitem.set_submenu(childmenu)

		# Show the popup.
		popup.show_all()
		popup.popup(
			parent_menu_shell=None,
			parent_menu_item=None,
			func=None,
			button=event.button,
			activate_time=event.time)


		



# If we're being run, create an instance 
# and run it.
if __name__ == "__main__":
	t = Test()
	gtk.main()
