#!/usr/bin/env python

# Modified file based upon John Finlay's PyGTK 2.0 Tutorial
# http://www.pygtk.org/pygtk2tutorial/sec-ProgressBars.html

import pygtk
pygtk.require('2.0')
import gtk, gobject

# Update the value of the progress bar so that we get
# some movement
def progress_timeout(pbobj):
    # Calculate the value of the progress bar using the
    # value range set in the adjustment object
    new_val = pbobj.pbar.get_fraction() + 0.10

    if new_val > 1.0:
        return False

    # Set the new value
    pbobj.pbar.set_fraction(new_val)

    # As this is a timeout function, return TRUE so that it
    # continues to get called until we reach 1.0
    return True

class ProgressBar:
    # start the progress bar
    def start_progress(self, widget, data=None):
        # Add a timer callback to update the value of the progress bar
        try:
            gobject.source_remove(self.timer)
        except:
            pass
        self.pbar.set_fraction(0.0)
        self.timer = 0
        self.timer = gobject.timeout_add (500, progress_timeout, self)
 
    # Clean up allocated memory and remove the timer
    def destroy_progress(self, widget, data=None):
        try:
            gobject.source_remove(self.timer)
        except:
            pass
        self.timer = 0
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_resizable(True)

        self.window.connect("destroy", self.destroy_progress)
        self.window.set_title("ProgressBar")
        self.window.set_border_width(0)

        vbox = gtk.VBox(False, 5)
        vbox.set_border_width(10)
        self.window.add(vbox)
        vbox.show()
  
        # Create a centering alignment object
        align = gtk.Alignment(0.5, 0.5, 0, 0)
        vbox.pack_start(align, False, False, 5)
        align.show()

        # Create the ProgressBar
        self.pbar = gtk.ProgressBar()
        self.pbar.set_text("some text")
        self.pbar.set_fraction(0.0)

        align.add(self.pbar)
        self.pbar.show()

        separator = gtk.HSeparator()
        vbox.pack_start(separator, False, False, 0)
        separator.show()

        # Add a button to start the progress button
        button = gtk.Button("start")
        button.connect("clicked", self.start_progress)
        vbox.pack_start(button, False, False, 0)
        button.show()

        # Add a button to exit the program
        button = gtk.Button("close")
        button.connect("clicked", self.destroy_progress)
        vbox.pack_start(button, False, False, 0)
        button.show()

        self.window.show()

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

if __name__ == "__main__":
    ProgressBar()
    main()