#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Tue Jun 17 16:59:56 2008

import wx, os, sys

# begin wxGlade: extracode
# end wxGlade

class PreferenceHandler:
    # prefOwner is object from which '.pref_*' attributes will be written to / read from
    def __init__(self, prefOwner, fileName):
        if prefOwner:
            self.owner = prefOwner
        else:
            self.owner = self
        self.fileName = fileName

    # Save preferences to file
    def save(self):
        fileText = ""
        for a in dir(self.owner):
            if a[ :5 ] == "pref_":
                #print "Preference", a, "=", getattr(self.owner, a)
                attr = getattr(self.owner, a)													# Get attribute (object)
                fileText += a + '\t' + str( attr ) + '\t' + str( type(attr) )[ 7:-2 ] + '\n'	# Output line format: [Pref name]  [Pref Value]  [Pref Type]

        f = open(self.fileName, 'w')
        f.write(fileText)
        f.close()

    # Load preferences from file
    def load(self):
        f = open(self.fileName, 'r')
        lines = f.read().split('\n')
        for r in lines:
            if len(r) > 0 and not r[ :1 ] == "#":			# Check line is not blank or commented out
                pref, val, dType = tuple( r.split('\t') )
                if dType == "int":
                    val = int(val)
                elif dType == "float":
                    val = float(val)
                elif dType == "long":
                    val = long(val)
                elif dType == "bool":
                    if val == "True":
                        val = True
                    else:
                        val = False
                setattr(self.owner, pref, val)


class PreferencesPanel(wx.Panel):
    def __init__(self, *args, **kwds):
        # begin wxGlade: PreferencesPanel.__init__
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.checkbox_offset = wx.CheckBox(self, -1, "Ignore offset")

        self.__set_properties()
        self.__do_layout()
        # end wxGlade
        
        if os.system=="win32":
            homeDir = os.environ.get("USERPROFILE")
        else:
            homeDir = os.environ.get("HOME")
        self.prefHandler = PreferenceHandler( self,  os.path.join(homeDir, ".pyRepRap/output_smil.conf"))
        
        #self.prefHandler.save() #temp
        self.prefHandler.load()
        self.setPrefValues()

    def __set_properties(self):
        # begin wxGlade: PreferencesPanel.__set_properties
        self.checkbox_offset.SetValue(1)
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: PreferencesPanel.__do_layout
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.checkbox_offset, 0, wx.ADJUST_MINSIZE, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        # end wxGlade
    
    
            # Set values of frame control
    def setPrefValues(self):
        pass

    def savePrefValues(self):
        self.prefHandler.save()

# end of class PreferencesPanel


class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.panel_1 = PreferencesPanel(self, -1)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyDialog.__set_properties
        self.SetTitle("dialog_2")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyDialog.__do_layout
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.panel_1, 1, wx.ALL|wx.EXPAND, 10)
        self.SetSizer(sizer_2)
        sizer_2.Fit(self)
        self.Layout()
        # end wxGlade

# end of class MyDialog

