=============
Layout Widget
=============

This z3c.form widget can be used to create a layout. This widget was created
as a part of Deco and is used by plone.app.deco.

  >>> from zope.configuration import xmlconfig
  >>> _ = xmlconfig.string("""\
  ... <configure xmlns="http://namespaces.zope.org/zope" package="plone.testing">
  ...   <include package="plone.z3cform" file="testing.zcml" />
  ...   <include package="plone.app.deco.browser" file="widget.zcml" />
  ... </configure>
  ... """)

As for all widgets, the layout widget must provide the new ``IWidget``
interface:

  >>> from zope.interface.verify import verifyClass
  >>> from z3c.form import interfaces
  >>> from plone.app.deco.browser.layoutwidget import LayoutWidget
  >>> from plone.app.deco.interfaces import ILayoutWidget

  >>> verifyClass(interfaces.IWidget, LayoutWidget)
  True

The widget can be instantiated only using the request:

  >>> from z3c.form.testing import TestRequest
  >>> request = TestRequest()

  >>> widget = LayoutWidget(request)

Before rendering the widget, one has to set the name and id of the widget:

  >>> widget.id = 'id'
  >>> widget.name = 'name'

If we render the widget we get an empty layout widget:

  >>> print widget.render()
  <textarea id="id" name="name" class="layout-widget"></textarea>


Adding some more attributes to the widget will make it display more:

  >>> widget.id = 'id'
  >>> widget.name = 'name'
  >>> widget.value = u'foo\nbar'

  >>> print widget.render()
  <textarea id="id" name="name" class="layout-widget">foo
  bar</textarea>


We can also render the widget in display mode:

  >>> widget.mode = interfaces.DISPLAY_MODE
  >>> print widget.render()
  foo
  bar

Rendering a field widget
------------------------

We can also use this widget for a field. For this to work we need a context and
a data manager.

  >>> from zope import schema
  >>> from zope.interface import implements, Interface
  >>> class IContent(Interface):
  ...     layout_field = schema.Text(title=u"Layout")

  >>> class Content(object):
  ...     implements(IContent)
  ...     def __init__(self, layout):
  ...         self.layout_field = layout
  ...     
  ...     def absolute_url(self):
  ...         return 'http://example.com/content1'

  >>> content = Content(None)

  >>> from z3c.form.datamanager import AttributeField
  >>> from zope.component import provideAdapter
  >>> provideAdapter(AttributeField)

  >>> from plone.app.deco.browser.layoutwidget import LayoutFieldWidget
  >>> layoutWidget = LayoutFieldWidget(IContent['layout_field'], TestRequest())
  >>> layoutWidget.context = content
  >>> layoutWidget.id = 'id'
  >>> layoutWidget.name = 'name'

We didn't set a value for the field yet so we will get an empty widget like
this:

  >>> layoutWidget.render()
  u'<textarea id="id" name="name" class="layout-widget"></textarea>\n'

Now let's set the value of the field.

  >>> layoutWidget.value = u'foo\nbar'

The rendered edit widget will look like this:

  >>> layoutWidget.render()
  u'<textarea id="id" name="name" class="layout-widget">foo\nbar</textarea>\n'

And the view widget will look like this:
  >>> layoutWidget.mode = interfaces.DISPLAY_MODE
  >>> print layoutWidget.render()
  foo
  bar
