#----------------------------------------------------------------------
# Name:        Companions.py
# Purpose:     Classes defining and implementing the design time
#              behaviour of controls
#
# Author:      Riaan Booysen
#
# Created:     1999
# RCS-ID:      $Id: Companions.py,v 1.26.2.2 2005/03/07 14:19:28 riaan Exp $
# Copyright:   (c) 1999 - 2005 Riaan Booysen
# Licence:     GPL
#----------------------------------------------------------------------

""" Classes defining and implementing the design time behaviour of controls """

from wxPython.wx import *

from wxPython.lib.anchors import LayoutAnchors

from PropEdit import PropertyEditors
from PropEdit.Enumerations import *
from BaseCompanions import HelperDTC
import PaletteStore, RTTI

#---Helpers---------------------------------------------------------------------

class FontDTC(HelperDTC):
    def __init__(self, name, designer, cmpn, obj, ownerPW):
        HelperDTC.__init__(self, name, designer, cmpn, obj, ownerPW)
        self.editors = {'FaceName'  : PropertyEditors.EnumPropEdit,
                        'Family'    : PropertyEditors.EnumPropEdit,
                        'Style'     : PropertyEditors.EnumPropEdit,
                        'Weight'    : PropertyEditors.EnumPropEdit,
                        'Underlined': PropertyEditors.BoolPropEdit,}

        fontEnum = wxFontEnumerator()
        fontEnum.EnumerateFacenames()
        fontNameList = fontEnum.GetFacenames()
        fontFaceName = []
        fontFaceNameNames = {}
        for fnt in fontNameList:
            fontFaceName.append(fnt)
            fontFaceNameNames[fnt] = fnt
        fontFaceName.sort()

        self.options = {'FaceName' : fontFaceName,
                        'Family'   : fontFamily,
                        'Style'    : fontStyle,
                        'Weight'   : fontWeight,}
        self.names = {'FaceName' : fontFaceNameNames,
                      'Family'   : fontFamilyNames,
                      'Style'    : fontStyleNames,
                      'Weight'   : fontWeightNames,}

    def hideDesignTime(self):
        return HelperDTC.hideDesignTime(self) + ['Encoding', 'NativeFontInfo',
               'NativeFontInfoUserDesc', 'NoAntiAliasing']

class ColourDTC(HelperDTC):
    def __init__(self, name, designer, cmpn, obj, ownerPW):
        HelperDTC.__init__(self, name, designer, cmpn, obj, ownerPW)
        self.editors = {'Red'    : PropertyEditors.IntPropEdit,
                        'Green'  : PropertyEditors.IntPropEdit,
                        'Blue'   : PropertyEditors.IntPropEdit,}
    def properties(self):
        return {'Red'  : ('CompnRoute', self.GetRed, self.SetRed),
                'Green': ('CompnRoute', self.GetGreen, self.SetGreen),
                'Blue' : ('CompnRoute', self.GetBlue, self.SetBlue),}

    def GetRed(self, cmpn):
        return self.obj.Red()
    def SetRed(self, value):
        self.obj.Set(max(min(value, 255), 0), self.obj.Green(), self.obj.Blue())

    def GetGreen(self, cmpn):
        return self.obj.Green()
    def SetGreen(self, value):
        self.obj.Set(self.obj.Red(), max(min(value, 255), 0), self.obj.Blue())

    def GetBlue(self, cmpn):
        return self.obj.Blue()
    def SetBlue(self, value):
        self.obj.Set(self.obj.Red(), self.obj.Green(), max(min(value, 255), 0))

class PosDTC(HelperDTC):
    def __init__(self, name, designer, cmpn, obj, ownerPW):
        HelperDTC.__init__(self, name, designer, cmpn, obj, ownerPW)
        self.editors = {'X' : PropertyEditors.IntPropEdit,
                        'Y' : PropertyEditors.IntPropEdit}

    def properties(self):
        return {'X': ('CompnRoute', self.GetX, self.SetX),
                'Y': ('CompnRoute', self.GetY, self.SetY)}

    def GetX(self, comp):
        return self.obj.x
    def SetX(self, value):
        self.obj.Set(value, self.obj.y)

    def GetY(self, comp):
        return self.obj.y
    def SetY(self, value):
        self.obj.Set(self.obj.x, value)

class SizeDTC(HelperDTC): pass

class AnchorsDTC(HelperDTC):
    def __init__(self, name, designer, cmpn, obj, ownerPW):
        HelperDTC.__init__(self, name, designer, cmpn, obj, ownerPW)
        self.editors = {'Left'   : PropertyEditors.BoolPropEdit,
                        'Top'    : PropertyEditors.BoolPropEdit,
                        'Right'  : PropertyEditors.BoolPropEdit,
                        'Bottom' : PropertyEditors.BoolPropEdit}
        self.anchCtrl = cmpn.control
        self.assureAnchors()
        self.GetLeftAnchor('')
        self.GetTopAnchor('')
        self.GetRightAnchor('')
        self.GetBottomAnchor('')

    def properties(self):
        return {'Left'    : ('CompnRoute', self.GetLeftAnchor, self.SetLeftAnchor),
                'Top'     : ('CompnRoute', self.GetTopAnchor, self.SetTopAnchor),
                'Right'   : ('CompnRoute', self.GetRightAnchor, self.SetRightAnchor),
                'Bottom'  : ('CompnRoute', self.GetBottomAnchor, self.SetBottomAnchor),}

##    def getPropList(self):
##        props = self.properties()
##        keys = props.keys()
##        keys.sort();keys.reverse()
##        pl = []
##        for key in keys:
##            rType, getter, setter = props[key]
##            pl.append(RTTI.PropertyWrapper(key, rType, getter, setter))
##        return {'properties': pl}

    def assureAnchors(self):
        if not self.ownerCompn.anchorSettings:
            self.ownerCompn.defaultAnchors()

    def updateAnchors(self):
        self.ownerCompn.anchorSettings = [self.left, self.top, self.right, self.bottom]
        self.obj = LayoutAnchors(self.anchCtrl, self.left, self.top, self.right, self.bottom)

    def GetLeftAnchor(self, name):
        self.assureAnchors()
        self.left = self.ownerCompn.anchorSettings[0]
        return self.left
    def SetLeftAnchor(self, value):
        self.assureAnchors()
        self.left = value
        self.updateAnchors()

    def GetTopAnchor(self, name):
        self.assureAnchors()
        self.top = self.ownerCompn.anchorSettings[1]
        return self.top
    def SetTopAnchor(self, value):
        self.assureAnchors()
        self.top = value
        self.updateAnchors()

    def GetRightAnchor(self, name):
        self.assureAnchors()
        self.right = self.ownerCompn.anchorSettings[2]
        return self.right
    def SetRightAnchor(self, value):
        self.assureAnchors()
        self.right = value
        self.updateAnchors()

    def GetBottomAnchor(self, name):
        self.assureAnchors()
        self.bottom = self.ownerCompn.anchorSettings[3]
        return self.bottom
    def SetBottomAnchor(self, value):
        self.assureAnchors()
        self.bottom = value
        self.updateAnchors()


class BaseFlagsDTC(HelperDTC):
    paramName = 'param'
    propName = 'Prop'
    def __init__(self, name, designer, cmpn, obj, ownerPW):
        HelperDTC.__init__(self, name, designer, cmpn, obj, ownerPW)
        self.editors = {}
        for flag in self.ownerCompn.windowStyles:
            self.editors[flag] = PropertyEditors.BoolPropEdit

    # XXX One of these 2 methods is redundant !

    def properties(self):
        props = {}
        prop = ('NameRoute', self.GetStyle, self.SetStyle)
        for flag in self.ownerCompn.windowStyles:
            props[flag] = prop
        return props

    def getPropList(self):
        propLst = []
        for prop in self.ownerCompn.windowStyles:
            propLst.append(RTTI.PropertyWrapper(prop, 'NameRoute',
                  self.GetStyle, self.SetStyle))
        return {'constructor':[], 'properties': propLst}

    def GetStyle(self, name):
        return name in self.ownerCompn.textConstr.params[self.paramName].split(' | ')

    def SetStyle(self, name, value):
        flags = self.ownerCompn.textConstr.params[self.paramName].split(' | ')
        if value:
            if name not in flags:
                if '0' in flags:
                    flags.remove('0')
                flags.insert(0, name)
        else:
            if name in flags:
                flags.remove(name)
                if not flags:
                    flags.append('0')
        flagsSrc = ' | '.join(flags)
        self.ownerCompn.textConstr.params[self.paramName] = flagsSrc
        self.designer.inspector.constructorUpdate(self.propName)
        flagsVal = self.eval(flagsSrc)
        ctrl = self.ownerCompn.control
        if hasattr(ctrl, 'SetWindowStyleFlag'):
            ctrl.SetWindowStyleFlag(flagsVal)

class WindowStyleDTC(BaseFlagsDTC):
    paramName = 'style'
    propName = 'Style'

class FlagsDTC(BaseFlagsDTC):
    paramName = 'flags'
    propName = 'Flags'


PaletteStore.helperClasses.update({
    'wxFontPtr': FontDTC,
    'wxFont': FontDTC,
    'wxColourPtr': ColourDTC,
    'wxColour': ColourDTC,
    'Anchors': AnchorsDTC
})
