#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
""" Enaml Standard Library - Fields

This is a library of Enaml components deriving from the builtin Field.

"""
from enaml.widgets.api import Field
from enaml.validator import RegexValidator, IntValidator, FloatValidator


enamldef RegexField(Field):
    """ A Field that accepts text validated by a regular expression.

    """
    attr regex : str
    validator << RegexValidator(regex=regex)


_int_converters = {
    2: bin,
    8: oct,
    10: unicode,
    16: hex
}


enamldef IntField(Field):
    """ A field that only accept integer inputs.

    """
    attr minimum = None
    attr maximum = None
    attr base = 10
    attr value : int = 0
    attr converter << _int_converters.get(base, unicode)
    text << converter(value)
    text :: self.value = int(text, base)
    validator << IntValidator(base=base, minimum=minimum, maximum=maximum)


enamldef FloatField(Field):
    """ A Field that only accept floating point values.

    """
    attr minimum = None
    attr maximum = None
    attr allow_exponent : bool = True
    attr value : float = 0.0
    attr converter = unicode
    text << converter(value)
    text :: self.value = float(text)
    validator << FloatValidator(
        minimum=minimum, maximum=maximum, allow_exponent=allow_exponent
    )
