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

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

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


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: str,
    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, str)
    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 = str
    text << converter(value)
    text :: self.value = float(text)
    validator << FloatValidator(
        minimum=minimum, maximum=maximum, allow_exponent=allow_exponent
    )
