## $Id$
##
## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""An improved sequence widget.

We provide a sequence widget that better suits our needs layout-wise
than the default implementation from zope.formlib.
"""
import grok
from zope.browserpage import ViewPageTemplateFile
from zope.formlib.interfaces import IInputWidget, IDisplayWidget
from zope.formlib.widgets import (
    ListSequenceWidget, SequenceDisplayWidget)
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.schema.interfaces import IField, IList

class SIRPSequenceWidget(ListSequenceWidget):
    """A sequence widget for lists.

    This is basically a plain copy from zope.formlib. We have,
    however, the possibility to tweak the attached template.
    """
    template = ViewPageTemplateFile('sequencewidget.pt')

class KofaSequenceDisplayWidget(SequenceDisplayWidget):
    """A sequence widget for lists.

    This is basically a plain copy from zope.formlib. We have,
    however, the possibility to tweak html tags.
    """

    tag = None
    itemTag = 'div'

# Register our sequence widgets as default for lists.
@grok.adapter(IList, IField, IBrowserRequest)
@grok.implementer(IInputWidget)
def seq_input_widget(obj, field, req, *args, **kw):
    return SIRPSequenceWidget(obj, field, req, *args, **kw)

@grok.adapter(IList, IField, IBrowserRequest)
@grok.implementer(IDisplayWidget)
def seq_display_widget(obj, field, req, *args, **kw):
    return KofaSequenceDisplayWidget(obj, field, req, *args, **kw)
