####################################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015, Alexander I. Mykyta
# All rights reserved.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
####################################################################################################

import tkinter as tk
from tkinter import ttk

import threading

####################################################################################################
class ProgressBox(tk.Toplevel):
    """
    Progress bar dialog box
    """
    def __init__(self, job_func, job_data = {}, parent = None, title = None):
        if(parent):
            # Has a defined parent.
            self.parent = parent
            self.foster_parent = None
            tk.Toplevel.__init__(self, self.parent)
            self.transient(self.parent)
        else:
            # Headless Dialog. Create foster parent
            self.foster_parent = tk.Tk()
            self.parent = None
            self.foster_parent.withdraw()
            tk.Toplevel.__init__(self, self.foster_parent)
        
        #--------------------------------------------------------
        # Create Widgets
        
        if title:
            self.title(title)
        else:
            self.title("Working...")
        
        body = ttk.Frame(
            self,
            padding = 5
        )
        self.create_body(body)
        body.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        
        buttonbox = ttk.Frame(
            self,
            padding = 5
        )
        self.create_buttonbox(buttonbox)
        buttonbox.pack(side=tk.BOTTOM, fill=tk.X)
        
        #--------------------------------------------------------
        
        self.resizable(False, False)
        
        if(self.parent):
            # Place dialog on top of parent window
            self.grab_set()
            self.geometry("+%d+%d" % (self.parent.winfo_rootx()+50,
                                      self.parent.winfo_rooty()+50))
        
        # Do Cancel if closed
        self.protocol("WM_DELETE_WINDOW", self.dlg_pbCancel)
        
        self.dlg_if = self.dialog_interface()
        
        worker = self.worker_thread(self.dlg_if, job_func, job_data)
        worker.start()
        
        # Loop here until the thread finishes
        while(worker.is_alive()):
            
            # Process any updates from the worker
            x = self.dlg_if.get_progress()