#!/usr/bin/env python
#
###
# Application: Pycadia
# File:        pycadia.py
# Description: Pycadia main GUI dialog code
# Copyright (c) 2002-2003   Andy Balcombe <http://www.anti-particle.com>
###
#
# 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 Library 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.
#
"""pycadia gui"""

#standard python modules
import sys
import getopt
import os
import os.path

#project imports
from glade2 import *
import pycadia_prefs
import config
import spacewarpy
import vektoroids

#add gtk path for windows  
if sys.platform == 'win32':
    os.environ['PATH'] += ';gtk/lib;gtk/bin'

class appMain(LoadGlade2):
    """
    The main pycadia gui class
    """
    def __init__(self, sFilename, sWindowName):
        """setup the gui"""
        LoadGlade2.__init__(self, sFilename, sWindowName)
        #get config
        sCfgFile, cfg = config.PycadiaConfig()
        #set default game
        if cfg.get('pycadia', 'last_game') == 'spacewarpy':
            #spacewarpy
            self.optGame.set_history(0)
            self.on_spacewarpy_activate()
        else:
            #vektoroids 
            self.optGame.set_history(1)
            self.on_vektoroids_activate()
        #show window
        self.winMain.show()
        
    def on_btnStartGame_clicked(self, *args):
        """Start the specified game"""
        #os.system(os.path.abspath('./%s &' % (self.sGame)))
        if self.sGame == 'spacewarpy':
            spacewarpy.main()
        if self.sGame == 'vektoroids':
            vektoroids.main()
 
    def on_btnPrefs_clicked(self, *args):
        """Display the preferences dialog"""
        prefs = pycadia_prefs.Prefs(config._sGladeFile, 'winPrefs', self.winMain)

    def on_btnClose_clicked(self, *args):
        """Close Pycadia"""
        #get config
        sCfgFile, cfg = config.PycadiaConfig()
        if cfg.get('pycadia', 'last_game') != self.sGame:
            #set the last game played 
            cfg.set('pycadia', 'last_game', self.sGame)
            #write config details back to file
            f = open(sCfgFile, 'w')
            cfg.write(f)
            f.close()
        #close window
        self.on_winMain_destroy()

    def on_winMain_destroy(self, *args):
        """done, quit the application"""
        gtk.main_quit()
        
    def on_spacewarpy_activate(self, *args):
        """set game to spacewarpy"""
        self.imgLogo.set_from_file('pixmaps/spacewarpy_msg.png')
        self.sGame = 'spacewarpy'
        
    def on_vektoroids_activate(self, *args):
        """set game to vektoroids"""
        self.imgLogo.set_from_file('pixmaps/vektoroids_msg.png')
        self.sGame = 'vektoroids'
    
    def process_gtk_events(self):
        """process gtk events"""
        while gtk.events_pending():
            gtk.mainiteration()

            
if __name__ == "__main__":
    #get command line options
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hv", ["help", "version"])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    for o, a in opts:
        #help
        if o in ("-h", "--help"):
            print 'Usage: %s [options]' % (sys.argv[0],)
            print 
            print '    --version       display version'
            print '    --help          display help'
            sys.exit()
        #show version
        if o in ("-v", "--version"):
            print '%s %s' % (sys.argv[0], config._version)
            print 
            print 'Copyright (C) 2002-2003 Andy Balcombe'
            print 'Released under GNU General Public License (GPL) v2'
            print 'See COPYING or go to http://www.gnu.org/copyleft/gpl.html'
            print
            print 'This is free software; see the source for copying conditions.  There is NO'
            print 'warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.'
            sys.exit()

    #instantiate main GUI window class
    app = appMain(config._sGladeFile, 'winMain') 
    #and... go...
    gtk.main()

