/**
 * @file    dropdown.c
 * @brief   Filling a DROPDOWN with items and reading the chosen index.
 *          A reading aid for the tutorial page "widget-types", not a complete
 *          plugin: the functions below would be wired into an IcPlugin
 *          (on_widget_event) or called from init()/a Worker callback.
 *
 * @note    ASCII-only and C89 on purpose (VC6 / CP950 safe, and it must pass
 *          the syntax-only guard). The widget ids match dropdown.nxlay.
 */

#include <string.h>

#include "nexus_plugin_api.h"

/** @brief The HostAPI table, saved by init() (not shown here). */
static const NxHostApi *s_host = (const NxHostApi *)0;

/** @brief widget id of the DROPDOWN ([w.1] in dropdown.nxlay). */
#define WID_MODE 1

/** @brief The items, static because the host copies them during the call only. */
static const char *const s_items[3] = { "Off", "Slow", "Fast" };

/**
 * @brief  Call once from init() (UI Thread).
 * @return ERR_OK or an error code.
 * @note   NX_WP_ITEMS is a BLOB: one NxItemHead per item, followed by the item
 *         text, each record padded to a multiple of 4 bytes. At most 4096 items.
 *         The value of the widget is the item INDEX. The host copies the blob
 *         during the call only.
 */
NxLong dropdown_setup(NxPluginCtx ctx)
{
    unsigned char buf[3 * 24];
    NxPropVal     v;
    NxItemHead    h;
    NxDword       pos = 0;
    NxDword       rec;
    int           i;

    for (i = 0; i < 3; i++)
    {
        h.text_len   = (NxWord)strlen(s_items[i]);
        h.detail_len = 0;
        h.item_id    = (NxDword)i;
        h.flags      = 0;
        rec = ((NxDword)sizeof(h) + h.text_len + 3u) & ~3u;
        memset(buf + pos, 0, rec);
        memcpy(buf + pos, &h, sizeof(h));
        memcpy(buf + pos + sizeof(h), s_items[i], h.text_len);
        pos += rec;
    }
    memset(&v, 0, sizeof(v));
    v.SizeOfStruct = (NxDword)sizeof(v);
    v.kind         = (NxWord)NX_WKIND_BLOB;
    v.u.blob.ptr   = buf;
    v.u.blob.len   = pos;
    return s_host->SetWidgetProp(ctx, 0, WID_MODE, NX_WP_ITEMS, &v);
}

/**
 * @brief  Widget event handler: evt->val.u.i64 is the chosen index (0-based).
 */
void NX_CALL dropdown_on_event(NxPluginCtx ctx, NxByte panel_id, NxWord widget_id,
                       const NxWidgetEvent *evt)
{
    NX_UNUSED_ARG(panel_id);
    if (widget_id == WID_MODE && evt->kind == NX_WEVT_CHANGED &&
        evt->val.kind == NX_WKIND_I64 && evt->val.u.i64 >= 0 && evt->val.u.i64 < 3)
    {
        s_host->LogWrite(ctx, NX_LOG_INFO, s_items[(int)evt->val.u.i64]);
    }
}

/**
 * @brief  Selects an item from code; index must be smaller than the item count.
 */
NxLong dropdown_select(NxPluginCtx ctx, NxDword index)
{
    NxPropVal v;

    memset(&v, 0, sizeof(v));
    v.SizeOfStruct = (NxDword)sizeof(v);
    v.kind         = (NxWord)NX_WKIND_I64;
    v.u.i64        = (NxI64)index;
    return s_host->SetWidgetProp(ctx, 0, WID_MODE, NX_WP_VALUE, &v);
}
