#------------------------------------------------------------------------------
# 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.
#------------------------------------------------------------------------------
""" An example of using Include to generate Field widgets on demand.

There are three columns in the example, each showing one of the many
ways that the Include component may be used.

<< autodoc-me >>
"""
import random

from enaml.layout.api import hbox, vbox, align, spacer
from enaml.widgets.api import (
    Window, Container, Label, Field, SpinBox, PushButton, VGroup
)
from enaml.core.api import Include


enamldef Main(Window):
    Container:
        padding = 0
        constraints = [
            hbox(cont1, cont2, cont3),
            align('width', cont1, cont2, cont3)
        ]
        VGroup: cont1:
            trailing_spacer = spacer(0)
            Label:
                text = 'Replace All'
                align = 'center'
            SpinBox: spin1:
                minimum = 0
                maximum = 20
                value = 3
            Include:
                # This generates a completely new list of widgets each
                # time the spin box is changed. All the old widgets
                # will be destroyed. This is the simplest way to use
                # the Include, albeit not the most efficient.
                objects << [Field(text=str(i)) for i in range(spin1.value)]
        VGroup: cont2:
            trailing_spacer = spacer(0)
            Label:
                text = 'Random Shuffle'
                align = 'center'
            PushButton:
                text = 'Shuffle'
                # This randomly shuffles the Fields of the Include. It
                # could also have been done in-place to the same effect,
                # but we do it on a copy to demonstrate that the Include
                # is intelligent enought to deal with assigning it an
                # object or objects which it already owns.
                clicked ::
                    new = list(inc2.objects)
                    random.shuffle(new)
                    inc2.objects = new
                    cont2.request_relayout()
            Include: inc2:
                # The Fields are initialized with the spin box value.
                objects = [Field(text=str(i)) for i in range(10)]
        VGroup: cont3:
            trailing_spacer = spacer(0)
            Label:
                text = 'Modify In-Place'
                align = 'center'
            PushButton:
                text = 'Append'
                # This appends a new Field to the Include in-place.
                clicked ::
                    n = len(inc3.objects)
                    inc3.objects.append(Field(text=str(n)))
            PushButton:
                text = 'Pop'
                # This pops a Field from the Include in-place.
                clicked ::
                    if inc3.objects:
                        inc3.objects.pop()
            Include: inc3:
                objects =  [Field(text=str(i)) for i in range(5)]
