#
###
# Application: Pycadia
# File:        glade2.py
# Description: pygtk GUI class
# 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.
#
import sys
import os
import new
import types
#import pygtk and gtk
import pygtk
#do stuff for windows that lets py2exe install work correctly
if sys.platform != 'win32':
    pygtk.require('2.0')
else:
    os.environ['PATH'] += ";lib;"
import gtk
assert gtk.pygtk_version >= (1,99,16), 'pygtk should be >= 1.99.16'
import gtk.glade

class LoadGlade2:
    """
    Superclass for glade2 based applications
    """
    def __init__(self, sFilename, sWindowName):
        #set some properties
        self.sFilename = sFilename
        self.sWindowName = sWindowName
        #set glade file name
        self.sGladeFile = os.path.join('glade', self.sFilename)
        #load glade file
        self.xml = gtk.glade.XML(self.sGladeFile, self.sWindowName)
        #find and store methods as bound callbacks
        callbacks = {}
        class_methods = self.__class__.__dict__
        for method_name in class_methods.keys():
            method = class_methods[method_name]
            if type(method) == types.FunctionType:
                callbacks[method_name] = new.instancemethod(method, self, self.__class__)
        #autoconnect signals
        self.xml.signal_autoconnect(callbacks)

    def __getattr__(self, key):
        #print "LoadGlade.__getattr__(", key, ")"
        w = self.xml.get_widget(key)
        if w is None:
            raise AttributeError("Widget " + key + " not found")
        self.__dict__[key] = w
        return w

    #set this
    __getitem__ = __getattr__
    
