#!/usr/local/bin/python2.6
"""chared.py

   A Python Tk editor for character files

   Adventures on Planet Zephulor by Aaron Hachez. 

   All code, artwork, and sound copyright 2003, Aaron Hachez.

   ----------------------------------------------------------------------------------------

   This file is part of "Adventures on Planet Zephulor".

   A copy of the GNU General Public License should be included with any
   redistribution of this program in a file named "COPYING.txt".

   "Adventures on Planet Zephulor" 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.

   "Adventures on Planet Zephulor" 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 "Adventures on Planet Zephulor"; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   ----------------------------------------------------------------------------------------

   "Adventures on Planet Zephulor" uses the following libraries:

   PyGame
   SDL

   Both are at the time of this writing, licensed under the GNU LGPL. Please see
   the included file LGPL for details 
"""

from Tkinter import *
import cPickle, sys

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        #self.TEXT = Text(self)
        #self.TEXT.pack({"side": "left"})
        self.TEXT = Entry(self)
        self.TEXT.pack({"side": "left"})
        self.OKAY = Button(self)
        self.OKAY['text'] = "Okay"
        self.OKAY['command'] = self.gettext()
        self.OKAY.pack({"side": "left"})

    def gettext(self):
        print self.TEXT.get()
        print "Done"

class App(Frame):

    charsize = 10

    def __init__(self, master=None, filename=None):
        Frame.__init__(self, master)
        self.pack()
        
        #self.entrythingy = Entry()
        #self.entrythingy.pack()

        # Create a place to enter the filename
        self.filenentry = Entry()
        self.filenentry.pack(expand=1, fill='x')

        self.filename = StringVar()
        self.filename.set('Enter filename to load')

        self.filenentry['textvariable'] = self.filename

        self.filenentry.bind('<Key-Return>', self.loadfile)
        
        # Create the actual data holders
        self.entrythingy = []
        self.data = []

        for i in range(self.charsize):
            self.data.append(StringVar())
            self.data[i].set(`i`)

            self.entrythingy.append(Entry())
            self.entrythingy[i].pack(expand=1, fill='x')
            self.entrythingy[i]['textvariable'] = self.data[i]
 
        # and here we get a callback when the user hits return.
        # we will have the program print out the value of the
        # application variable when the user hits return
        self.entrythingy[self.charsize - 1].bind('<Key-Return>',
                              self.save_contents)

      
        # here is the application variable
#        self.one = StringVar()
#        self.two = StringVar()
#        self.three = StringVar()
#        self.four = StringVar()
#        self.five = StringVar()
#        self.six = StringVar()
#        self.seven = StringVar()

        # set it to some value
#        self.contents.set("this is a variable")
        # tell the entry widget to watch this variable
#        self.entrythingy["textvariable"] = self.contents
       
    def save_contents(self, event):
        
        filename = self.filename.get()

        print "Saving file...", filename

        f = file(filename, 'w')

        for i in range(self.charsize):
            tmpvar = self.data[i].get()

            if i == 1 or i == 2:
                cPickle.dump(tmpvar, f)
            else:
                writevar = eval(tmpvar)
                cPickle.dump(writevar, f)

        f.close()

        print "Done."

#        self.initial_focus = None
#        self.destroy()

    def loadfile(self, event):
        filename = self.filename.get()

        print "Loading file...", filename

        f = file(filename,'r')
        
        for i in range(self.charsize):
            tmpvar = cPickle.load(f)
            self.data[i].set(tmpvar)

        f.close()
        
        print "Done."

    def load(self, filename):

        print "Loading file...", filename

        f = file(filename,'r')
        
        for i in range(self.charsize):
            tmpvar = cPickle.load(f)
            self.data[i].set(tmpvar)

        f.close()
        
        self.filename.set(filename)
        print "Done."


myapp = App()

if sys.argv[1]:
    myapp.load(sys.argv[1])

myapp.mainloop()
