#! /usr/bin/env python
""" Consult the magic 8 ball for all your decisions

This code is partially based on Robert Vollmert's wmdock.py
             (See description below)
"""
##   wmdock -- a python module to create WindowMaker dockapps
##   Copyright (C) 1999  Robert Vollmert <rvollmert@gmx.net>
##  
##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation; either version 2 of the License, or
##   (at your option) any later version.
##  
##   This program is distributed in the hope that it will be useful,
##   but WITHOUT ANY WARRANTY; without even the implied warranty of
##   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##   GNU General Public License for more details.
##  
##   You should have received a copy of the GNU General Public License
##   along with this program; if not, write to the Free Software
##   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""a simple module to program WindowMaker dock applications

This module contains one class, DockWindow. DockWindow is an extension
of Toplevel. It can be used to create a dock window WindowMaker (see
http://www.windowmaker.org/).
"""

VERSION = 0.6

import Tkinter, time, whrandom

# delay between map event of toplevel window and grouping
DELAY = 0.1
# used to centre icon window
ICONWIDTH = ICONHEIGHT = 64
# default maximum width/height
DEFWIDTH = DEFHEIGHT = 56

class DockWindow(Tkinter.Toplevel):
    """a window for the WindowMaker dock.

    This class implements a simple window for the WindowMaker dock. It
    does this by extending the basic Tkinter.Toplevel class. The
    Toplevel window becomes its own icon window. For it to be
    displayed, a toplevel window is created offscreen which,
    when mapped, adds itself to the icon windows group. It can then
    (after the icon window is mapped) safely be withdrawn and
    destroyed.

    methods:
    __init__ -- initialize instance
    setsize -- set the size of the iconwindow
    center -- center the icon window
    top_handle_map -- handle map event of the toplevel window
    dock_handle_map -- create appicon and destroy toplevel window
    """
        
    def __init__(self, master=None, width=None, height=None,
                 *args, **kw):
        """initialize dockapp instance.

        keyword arguments:
        width, height -- initial width and height of window (equivalent
            to calling setsize(width, height)
        """
        apply(Tkinter.Toplevel.__init__, (self, master)+args, kw)
        self.iconwindow(self)
        
        self.center_when_mapped = 1
        if width is not None and height is not None:
            self.setsize(width, height)
        else:
            self.geometry("+%d+0" % ICONWIDTH)

        self.toplevel = Tkinter.Toplevel(self)
        self.toplevel.geometry('+%d+0' % self.winfo_screenwidth())
        self.toplevel.bind('<Map>', self.top_handle_map)
        self.dock_mapped = 0
        self.bind('<Map>', self.dock_handle_map)

    def setsize(self, width, height):
        """set window size and center
        """
        self.geometry("%dx%d+%d+%d" % (
            width, height, (ICONWIDTH-width)/2, (ICONHEIGHT-height)/2))
        self.center_when_mapped = 0

    def center(self):
        """center window (use only after map)
        """
        self.setsize(self.winfo_width(), self.winfo_height())

    def top_handle_map(self, event):
        """handle the map event of the toplevel window.
        """
        time.sleep(DELAY)
        self.toplevel.group(self)

    def dock_handle_map(self, event):
        """handle the first map event of the dock window
        """
        if not self.dock_mapped:
            self.dock_mapped = 1
            if self.center_when_mapped:
                self.center()
            self.toplevel.withdraw()
            self.toplevel.destroy()
            del self.toplevel

EIGHTBALLIMAGE = """
R0lGODlhPABAAPUmAAAAAAAAMwAzAAAzMzMAADMAMzMzADMzMzMzZjNmMzNmZmYzM2YzZmZm
M2ZmZmZmmWaZZmaZmZlmM5lmZplmmZmZZpnMZsyZZszMZpmZmZmZzJnMmZnMzMyZmf+ZmcyZ
zMzMmf//mcDAwMzMzP/MzP//zP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BABkACcALAAA
AAA8AEAAAAb+QJFIJBKJRCKRSCQSiUQikUgkEolEIpFIJBKJRCKRSCQSiUQikUgkEolEIpFI
JBKJRCKRSCQSiYAikUgkEolEIpFIJBKJRCKRSCQSiUQikUgkEolEIpFIJBKJRCKRSCQSiUQi
kUgkEolEIpFIJBIBRSKRSCQSiUQikUgkEolEIpGoU3EsDofDYuGYVEAikUgkEolEIpFIJBKJ
RCKRSCQSiUQikUgkAopEIpFIJBKJRCKRSFQ5HA6AwwFwAAAOhMOBcDgc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IJBKJRCKRCCgSiUQikUgkEolEIpFIJBKJ
RCKRSCQSiUQikUgkEolEIpFIJBKJRCKRSCQSiUQikUgkEgUBADs=

"""

def answer():
    # in, out, in, out, shake it all about
    for n in range(3):
        button8.config(relief=Tkinter.SUNKEN)
	root.update_idletasks()
	time.sleep(0.05)
        button8.config(relief=Tkinter.RAISED)
	root.update_idletasks()
	time.sleep(0.05)
    buttonanswer.config(text=whrandom.choice(answers))
    button8.lower()
    
def ball():
    buttonframe.lower()

def main():
    global root
    root = Tkinter.Tk()
    root.withdraw()
    dock = DockWindow(name='8ball', class_='8ball')
    global img
    img = Tkinter.PhotoImage(data=EIGHTBALLIMAGE) #file="8ball.gif")
    global button8
    button8 = Tkinter.Button(dock, image=img, command=answer)
    button8.grid(row=0,column=0)
    global buttonanswer
    global buttonframe
    buttonframe = Tkinter.Frame(dock, height=60, width=60)
    buttonframe.pack_propagate(0) # don't shrink
    buttonanswer = Tkinter.Button(buttonframe, text="init", command=ball, wraplength=60)
    buttonanswer.pack(fill=Tkinter.BOTH, expand=1)
    buttonframe.grid(row=0,column=0,sticky=Tkinter.W+Tkinter.E+Tkinter.S+Tkinter.N)
    buttonframe.lower()

    global answers
    answers = [
    'Signs point to yes.',
    'Yes.',
    'Reply hazy, try again.',
    'Without a doubt.',
    'My sources say no.',
    'As I see it, yes.',
    'You may rely on it.',
    'Outlook not so good.',
    'It is decidedly so.',
    'Better not tell you now.',
    'Very doubtful.',
    'Yes - definitely.',
    'It is certain.',
    'Cannot predict now.',
    'Most likely.',
    'Ask again later.',
    'My reply is no.',
    'Outlook good.',
    'Don\'t count on it.']

    root.mainloop()

if __name__ == '__main__':
    main()