#!/usr/bin/python

import gtk
from gtk import gdk
from gettext import gettext as _


ZOOM_DEFAULT=100
ZOOM_MAX=400
ZOOM_MIN=25
ZOOM_STEP=25


def spin_changed_cb(spin):
    # Force a % onto the end of the text buffer for display
    # purposes only. PyGTK 2.22+ only, so check first.
    if spin.get_buffer is not None:
        buf = spin.get_buffer()
        old_txt = buf.get_text()
        new_txt = _("%s%%" % int(spin.get_value()))
        if new_txt != old_txt:
            buf.set_text(new_txt, len(new_txt))
    # Possibly this too. Quite tacky though.
    adj = spin.get_adjustment()
    p = adj.get_value() / adj.get_upper()
    spin.set_progress_fraction(p)
    # Make the reset button's sensitive state meaningful
    spin.set_icon_sensitive(0, spin.get_value() != ZOOM_DEFAULT)


def spin_icon_press_cb(spin, icon_pos, event):
    print "reset"
    spin.set_value(ZOOM_DEFAULT)


def adj_value_changed_cb(adj):
    print adj.get_value()


if __name__ == '__main__':
    import os, sys
    script = os.path.basename(sys.argv[0])
    win = gtk.Window()
    win.set_title(script)
    win.connect("destroy", gtk.main_quit)

    adj = gtk.Adjustment(value=ZOOM_DEFAULT, lower=ZOOM_MIN,
      upper=ZOOM_MAX, step_incr=ZOOM_STEP)
    spin = gtk.SpinButton(adjustment=adj, climb_rate=1.0, digits=0)
    spin.set_icon_from_stock(0, gtk.STOCK_ZOOM_FIT)
    spin.set_icon_tooltip_text(0, "Reset Zoom to default")
    spin.set_icon_activatable(0, True)
    spin.connect("icon-press", spin_icon_press_cb)
    extra_width = 4   # roughly icon size plus spin buttons
    spin.set_width_chars( len(str(ZOOM_MAX)) + extra_width )
    spin.connect("changed", spin_changed_cb)
    adj.connect("value-changed", adj_value_changed_cb)
    spin.set_numeric(True)
    spin.set_editable(False)
    spin.set_can_default(False)
    spin.set_can_focus(False)
    spin.set_text(str(adj.get_value()))

    box = gtk.HBox()
    box.pack_start(spin, True, False)
    box.set_size_request(200, 150)

    win.add(box)

    win.show_all()
    gtk.main()
