#!/usr/bin/env python

# example tooltip.py
from sys import argv, exit, stderr
from getopt import getopt, GetoptError
import pygtk
pygtk.require('2.0')
import gobject
import gtk

NEW = True
OLD = False

# Create an Arrow widget with the specified parameters
# and pack it into a button
def create_arrow_button(toolbutton, arrow_type, shadow_type, label_text):
    arrow = gtk.Arrow(arrow_type, shadow_type)
    if toolbutton:
        button = gtk.ToolButton(arrow, label_text)
    else: # create a similar button using the old API
        box = gtk.VBox(False, 0)
        box.set_border_width(2)
        # Create a label for the button
        label = gtk.Label(label_text)
        # Pack the pixmap and label into the box, expand and fill the arrow widget
        box.pack_start(arrow, True, True, 3)
        box.pack_start(label, False, False, 3)
        label.show()
        button = gtk.Button()
        button.add(box)
    button.show()
    arrow.show()
    return button

def set_tip( toolbutton_api, tooltips, button, tip ):
	"""set the tooltip for either the new toolbar API, or use the the old API"""
	if toolbutton_api: # new Toolbar API
		button.set_tooltip(tooltips, tip)
	else: # Old Toolbar API
		tooltips.set_tip(button, tip)

	
def usage():
    tabs = "\t\t"
    print "Usage: porthole [OPTION...]\n"
    print "  -h, --help" + tabs + "Show this help message"
    print "  -b, --button" + tabs + \
            "Run the demo with using the Old gtk.Toolbar API"
    print "  -t, --toolbutton" + tabs + \
            "Run the demo with using the Old gtk.Toolbar API"

class Tooltips:
    def __init__(self, button_type):
        # Create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        if button_type == NEW:
            window.set_title("New Toolbar/Tooltips API")
        else:
            window.set_title("Old Toolbar/Tooltips API")

        # It's a good idea to do this for all windows.
        window.connect("destroy", lambda w: gtk.main_quit())

        # Sets the border width of the window.
        window.set_border_width(10)
        toolbar = gtk.Toolbar()
        toolbar.set_tooltips(True)
        window.add(toolbar)

        # create a tooltips object
        tooltips = gtk.Tooltips()

        button1 = create_arrow_button(button_type, gtk.ARROW_UP, gtk.SHADOW_IN, "Button1")
        toolbar.add(button1) 
        set_tip(button_type, tooltips, button1, "SHADOW_IN")

        button2 = create_arrow_button(button_type, gtk.ARROW_DOWN, gtk.SHADOW_OUT, "Button2")
        toolbar.add(button2) 
        set_tip(button_type, tooltips, button2, "SHADOW_OUT")
  
        button3 = create_arrow_button(button_type, gtk.ARROW_LEFT, gtk.SHADOW_ETCHED_IN, "Button3")
        toolbar.add(button3)
        set_tip(button_type, tooltips, button3, "SHADOW_ETCHED_IN")
  
        button4 = create_arrow_button(button_type, gtk.ARROW_RIGHT, gtk.SHADOW_ETCHED_OUT, "Button4")
        toolbar.add(button4)
        set_tip(button_type, tooltips, button4, "SHADOW_ETCHED_OUT")

        window.show_all() 
        window.resize(600,100)

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    try:
        opts, args = getopt(argv[1:], 'bth', ["button", "toolbutton", "help"])
    except GetoptError, e:
        print >>stderr, e.msg
        exit(1)
    # run thru the options and set the DATA_PATH & imported modules properly
    # bug ==> any porthole script can be used to load both --local and installed modules
    path_set = False
    for opt, arg in opts:
        if opt in ("-b", "--button"):
            new_api = OLD
        if opt in ("-t", "--toolbutton"):
            version = gobject.pygtk_version
            if version[1] >=4:
                new_api = NEW
            else:
                new_api = OLD
                print >>stderr, "pygtk version not new enough to use New Toolbar API"
        if opt in ("-h", "--help"):
            usage()
            exit()
    tt = Tooltips(new_api)
    main()
