<tal:comment replace="nothing">
  This macro shows the content of a layouted object, like a page or widget.
  It requires:
    contextObj         The Zope object on which we are working
    layoutType         The kind of layout: "view"? "edit"? "cell"?
    layout             The layout object that will dictate how object content
                       will be rendered.
  Options:
    contextMacro       The base folder containing the macros to call for
                       rendering the elements within the layout.
                       Defaults to app.ui
    tagId              The name and id of the main tag for this layout (used
                       a.o. for master/slave relationships).
    tagCss             Some additional CSS class for the main tag
                       (ie, the CSS class for a slave).
</tal:comment>
<metal:show define-macro="layout"
            tal:define="contextMacro contextMacro| python: app.ui;
                        tagId        tagId|python:'';
                        tagCss       tagCss|python:'';
                        layoutCss    layout/css_class;">
 <table tal:attributes="cellpadding  layout/cellpadding;
                        cellspacing  layout/cellspacing;
                        width        layout/width;
                        align        python: tool.flipLanguageDirection(layout['align'], dir);
                        class        python: tagCss and ('%s %s' % (tagCss, layoutCss)).strip() or layoutCss;
                        style        layout/style;
                        id           tagId;
                        name         tagId;">
   <tal:comment replace="nothing">The table header row</tal:comment>
   <tr tal:condition="layout/headerRow" tal:attributes="valign layout/headerRow/valign">
     <th tal:repeat="cell layout/headerRow/cells"
         tal:attributes="align python: tool.flipLanguageDirection(cell['align'], dir);
                         width cell/width;">
     </th>
   </tr>
   <tal:comment replace="nothing">The table content</tal:comment>
   <tr tal:repeat="row layout/rows" tal:attributes="valign row/valign">
     <td tal:repeat="cell row/cells"
         tal:attributes="align python: tool.flipLanguageDirection(cell['align'], dir);
                         colspan cell/colspan;
                         class python: test(repeat['cell'].end, '', 'cellGap')"><tal:content repeat="elem cell/content"><tal:field condition="python: elem == '?'"><metal:call use-macro="python: getattr(contextMacro, widget['type'].lower()).macros[layoutType]"/></tal:field><tal:other condition="python: elem != '?'"><metal:call use-macro="python: getattr(contextMacro, elem[0]).macros[elem[1]]"/></tal:other><img tal:condition="not: repeat/elem/end" tal:attributes="src string: $appUrl/ui/space.gif"/></tal:content>
     </td>
   </tr>
 </table>
</metal:show>

<tal:comment replace="nothing">
  This macro displays the widget corresponding to a given field. It requires:
    contextObj         The Zope object for which this widget must be rendered
    page               The page where the widget lies
    layoutType         "edit"? "view"? "cell?"
    widget             The widget to render

  Optionally:
    widgetName         If the field to show is within a List, we cheat and
                       include, within the widgetName, the row index.
</tal:comment>
<metal:field define-macro="field"
  tal:define="contextMacro python: app.ui.widgets;
              layout       python: widget['layouts'][layoutType];
              name         widgetName| widget/name;
              sync         python: widget['sync'][layoutType];
              outerValue   value|nothing;
              rawValue     python: contextObj.getFieldValue(name,onlyIfSync=True,layoutType=layoutType,outerValue=outerValue);
              value        python: contextObj.getFormattedFieldValue(name, rawValue);
              requestValue python: contextObj.getRequestFieldValue(name);
              inRequest    python: request.has_key(name);
              errors       errors | python: ();
              inError      python: name in errors;
              isMultiple   python: (widget['multiplicity'][1] == None) or (widget['multiplicity'][1] &gt; 1);
              masterCss    python: widget['slaves'] and ('master_%s' % name) or '';
              slaveCss     python: widget['master'] and ('slave_%s_%s' % (widget['masterName'], '_'.join(widget['masterValue']))) or '';
              tagCss       tagCss | python:'';
              tagCss       python: ('%s %s' % (slaveCss, tagCss)).strip();
              tagId        python: widget['master'] and 'slave' or ''">
  <metal:layout use-macro="context/ui/widgets/show/macros/layout"/>
</metal:field>

<tal:comment replace="nothing">
  This macro displays the widget corresponding to a group of widgets.
  It requires:
    contextObj         The Zope object for which this widget must be rendered
    page               The page where the widget lies
    layoutType         "edit"? "view"? "cell?"
    widget             The widget to render
</tal:comment>
<metal:group define-macro="group"
   tal:define="tagCss python: widget['master'] and ('slave_%s_%s' % (widget['masterName'], '_'.join(widget['masterValue']))) or '';
               widgetCss widget/css_class;
               groupCss python: tagCss and ('%s %s' % (tagCss, widgetCss)) or widgetCss;
               tagId python: widget['master'] and 'slave' or ''">
  <fieldset tal:condition="python: widget['style'] == 'fieldset'">
    <legend tal:condition="widget/hasLabel">
      <i tal:content="structure python: contextObj.translate(widget['labelId'])"></i>
      <tal:help condition="widget/hasHelp">
        <metal:call use-macro="app/ui/widgets/show/macros/help"/>
      </tal:help>
    </legend>
    <div tal:condition="widget/hasDescr" class="discreet" 
         tal:content="structure python: contextObj.translate(widget['descrId'])"></div>
    <metal:content use-macro="app/ui/widgets/show/macros/groupContent"/>
  </fieldset>
  <tal:asSection condition="python: widget['style'] not in ('fieldset', 'tabs')">
    <metal:content use-macro="app/ui/widgets/show/macros/groupContent"/>
  </tal:asSection>
  <tal:asTabs condition="python: widget['style'] == 'tabs'">
  <table tal:attributes="width widget/wide;
                         class groupCss;
                         id tagId; name tagId">
    <tal:comment replace="nothing">First row: the tabs.</tal:comment>
    <tr valign="middle"><td style="border-bottom: 1px solid #ff8040">
      <table style="position:relative; bottom:-2px;" cellpadding="0" cellspacing="0">
        <tr valign="bottom">
          <tal:tab repeat="widgetRow widget/widgets">
          <tal:id define="tabId python:'tab_%s_%d_%d' % (widget['name'], repeat['widgetRow'].number(), len(widget['widgets']))">
          <td><img tal:attributes="src string: $appUrl/ui/tabLeft.png;
                                   id python: '%s_left' % tabId"/><td>
          <td tal:attributes="style python:'background-image: url(%s/ui/tabBg.png)' % appUrl;
                              id tabId">
            <a style="cursor:pointer"
               tal:content="python: _('%s_col%d' % (widget['labelId'], repeat['widgetRow'].number()))"
               tal:attributes="onClick python: 'showTab(\'%s_%d_%d\')' % (widget['name'], repeat['widgetRow'].number(), len(widget['widgets']))"></a>
          </td>
          <td><img tal:attributes="src string: $appUrl/ui/tabRight.png;
                                   id python: '%s_right' % tabId"/><td>
          </tal:id>
          </tal:tab>
        </tr>
      </table>
    </td></tr>
    <tal:comment replace="nothing">Other rows: the widgets.</tal:comment>
    <tr tal:repeat="widgetRow widget/widgets"
        tal:attributes="id python: 'tabcontent_%s_%d_%d' % (widget['name'], repeat['widgetRow'].number(), len(widget['widgets']));
                        style python: test(repeat['widgetRow'].number()==1, 'display:table-row', 'display:none')">
      <td tal:define="widget python: widgetRow[0]">
        <tal:group condition="python: widget['type'] == 'group'">
          <metal:call use-macro="app/ui/widgets/show/macros/group"/>
        </tal:group>
        <tal:field condition="python: widget['type'] != 'group'">
          <metal:call use-macro="app/ui/widgets/show/macros/field"/>
        </tal:field>
      </td>
    </tr>
  </table>
  <script language="javascript"
          tal:content="python: 'initTab(\'tab_%s\', \'%s_1_%d\')' % (widget['name'], widget['name'], len(widget['widgets']))"></script>
  </tal:asTabs>
</metal:group>

<tal:comment replace="nothing">
  This macro displays the content of a group of widgets.
  It is exclusively called by macro "group" above.
</tal:comment>
<table metal:define-macro="groupContent"
       tal:define="cellgap widget/cellgap"
       tal:attributes="width widget/wide;
                       align python: tool.flipLanguageDirection(widget['align'], dir);
                       class groupCss;
                       cellspacing widget/cellspacing;
                       cellpadding widget/cellpadding;
                       id tagId; name tagId">
  <tal:comment replace="nothing">Display the title of the group if it is not rendered a fieldset.</tal:comment>
  <tr tal:condition="python: (widget['style'] != 'fieldset') and widget['hasLabel']">
    <td tal:attributes="colspan python: len(widget['columnsWidths']);
                        class widget/style;
                        align dleft">
      <span tal:replace="structure python: contextObj.translate(widget['labelId'])"/>
      <tal:help condition="widget/hasHelp">
        <metal:call use-macro="app/ui/widgets/show/macros/help"/>
      </tal:help>
    </td>
  </tr>
  <tr tal:condition="python: (widget['style'] != 'fieldset') and widget['hasDescr']">
    <td tal:attributes="colspan python: len(widget['columnsWidths'])" class="discreet"
        tal:content="structure python: contextObj.translate(widget['descrId'])">
    </td>
  </tr>
  <tr> <tal:comment replace="nothing">The column headers</tal:comment>
    <th tal:repeat="colNb python:range(len(widget['columnsWidths']))"
        tal:attributes="width python:widget['columnsWidths'][colNb];
                        align python: tool.flipLanguageDirection(widget['columnsAligns'][colNb], dir)"
        tal:content="structure python: widget['hasHeaders'] and contextObj.translate('%s_col%d' % (widget['labelId'], colNb+1)) or ''">
    </th>
  </tr>
  <tal:comment replace="nothing">The rows of widgets</tal:comment>
  <tr tal:attributes="valign widget/valign" tal:repeat="widgetRow widget/widgets">
    <td tal:repeat="widget widgetRow"
        tal:attributes="colspan widget/colspan|python:1;
                        style python: test(repeat['widget'].number() != len(widgetRow), 'padding-right: %s'%cellgap, '')">
      <tal:showWidget condition="widget">
        <tal:group condition="python: widget['type'] == 'group'">
          <metal:call use-macro="app/ui/widgets/show/macros/group"/>
        </tal:group>
        <tal:field condition="python: widget['type'] != 'group'">
          <metal:call use-macro="app/ui/widgets/show/macros/field"/>
        </tal:field>
      </tal:showWidget>
    </td>
  </tr>
</table>

<tal:comment replace="nothing">Displays a field label.</tal:comment>
<tal:label metal:define-macro="label" condition="widget/hasLabel"><label tal:attributes="for widget/name" tal:condition="python: (widget['type'] != 'Action')" tal:content="structure python: contextObj.translate('label', field=widget)"></label></tal:label>

<tal:comment replace="nothing">Displays a field description.</tal:comment>
<tal:description metal:define-macro="description" condition="widget/hasDescr"><span class="discreet" tal:content="structure python: contextObj.translate('descr', field=widget)"></span>
</tal:description>

<tal:comment replace="nothing">Displays a field help.</tal:comment>
<tal:help metal:define-macro="help"><acronym tal:attributes="title python: contextObj.translate('help', field=widget)"><img tal:attributes="src string: $appUrl/ui/help.png"/></acronym></tal:help>

<tal:comment replace="nothing">Displays validation-error-related info about a field.</tal:comment>
<tal:validation metal:define-macro="validation"><acronym tal:condition="inError" tal:attributes="title python: errors[name]"><img tal:attributes="src string: $appUrl/ui/warning.png"/></acronym><img tal:condition="not: inError" tal:attributes="src string: $appUrl/ui/warning_no.gif"/></tal:validation>

<tal:comment replace="nothing">Displays the fact that a field is required.</tal:comment>
<tal:required metal:define-macro="required"><img tal:attributes="src string: $appUrl/ui/required.gif"/></tal:required>
