/* OCaml-Win32
 * win32_updown.c
 * Copyright (c) 2002-2004 by Harry Chomsky
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "win32.h"

/********************************************************************/
/* User Interface: Windows User Experience: Windows Shell: SDK: Windows Controls: Individual Control Information: Up-Down Controls */

static void udaccel_val(value val, UDACCEL* uda)
{
    uda->nSec = Int_val(Field(val, 0));
    uda->nInc = Int_val(Field(val, 1));
}

static value alloc_udaccel(UDACCEL* uda)
{
    CAMLparam0();
    CAMLlocal1(val);

    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(uda->nSec));
    Store_field(val, 1, Val_int(uda->nInc));
    CAMLreturn(val);
}

CAMLprim value create_up_down_control_native(value style, value position, value size, value parent, value id, value inst, value buddy, value upper, value lower, value pos)
{
    CAMLparam5(style, position, size, parent, id);
    CAMLxparam5(inst, buddy, upper, lower, pos);
    LARGE_INTEGER li;
    POINT p;
    SIZE s;
    HWND hparent, hbuddy;
    HINSTANCE hinst;
    HWND hwnd;
    CAMLlocal1(val);

    li.QuadPart = Int64_val(style);
    p = point_val(position);
    s = size_val(size);
    hparent = Handle_val(parent);
    hinst = Handle_val(inst);
    hbuddy = Handle_val(buddy);
    enter_blocking_section();
    hwnd = CreateUpDownControl(
        li.LowPart, p.x, p.y, s.cx, s.cy, hparent, id, hinst, hbuddy,
        Int_val(upper), Int_val(lower), Int_val(pos));
    leave_blocking_section();
    if (hwnd == NULL)
        raise_win32_error(0);
    val = alloc_handle(hwnd);
    CAMLreturn(val);
}

CAMLprim value create_up_down_control_bytecode(value* argv, int argn)
{
    return create_up_down_control_native(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9]);
}

/* Styles */

static DWORD updown_style_options[] = {
    UDS_ALIGNLEFT,
    UDS_ALIGNRIGHT,
    UDS_ARROWKEYS,
    UDS_AUTOBUDDY,
    UDS_HORZ,
    UDS_HOTTRACK,
    UDS_NOTHOUSANDS,
    UDS_SETBUDDYINT,
    UDS_WRAP
};
Define_static_constants(updown_style_options);

CAMLprim value updown_style_of_option(value option)
{
    return copy_int64((int64)Constant_val(option, updown_style_options));
}

/* Messages */

CAMLprim value unpack_udm_getaccel_count(value result)
{
    return Val_int(Int32_val(result));
}

CAMLprim value issue_udm_getaccel(value sender, value max_count)
{
    CAMLparam2(sender, max_count);
    int max_cnt, cnt, n, i;
    UDACCEL* p;
    CAMLlocal4(contents, result, val, v);

    max_cnt = Int_val(max_count);
    if (max_cnt > 0)
        p = malloc(max_cnt * sizeof(UDACCEL));
    else
        p = NULL;
    contents = pack_message(UDM_GETACCEL, max_cnt, (LPARAM)p);
    result = callback(sender, contents);
    cnt = Int32_val(result);
    n = cnt < max_cnt ? cnt : max_cnt;
    val = Val_nil;
    for (i = n - 1; i >= 0; --i) {
        v = alloc_udaccel(&p[i]);
        val = alloc_cons(v, val);
    }
    if (max_cnt > 0)
        free(p);
    CAMLreturn(val);
}

CAMLprim value unpack_udm_getbase(value result)
{
    return Val_int(Int32_val(result));
}

CAMLprim value unpack_udm_getbuddy(value result)
{
    return alloc_handle((HWND)Int32_val(result));
}

CAMLprim value unpack_udm_getpos(value result)
{
    CAMLparam1(result);
    CAMLlocal1(val);

    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(LOWORD(Int32_val(result))));
    Store_field(val, 1, Val_bool(HIWORD(Int32_val(result)) != 0));
    CAMLreturn(val);
}

CAMLprim value unpack_udm_getrange(value result)
{
    CAMLparam1(result);
    CAMLlocal1(val);

    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(HIWORD(Int32_val(result))));
    Store_field(val, 1, Val_int(LOWORD(Int32_val(result))));
    CAMLreturn(val);
}

CAMLprim value issue_udm_getrange32(value sender)
{
    CAMLparam1(sender);
    INT low, high;
    CAMLlocal2(contents, val);

    contents = pack_message(UDM_GETRANGE32, (WPARAM)&low, (LPARAM)&high);
    callback(sender, contents);
    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(low));
    Store_field(val, 1, Val_int(high));
    CAMLreturn(val);
}

CAMLprim value issue_udm_setaccel(value sender, value accels)
{
    CAMLparam2(sender, accels);
    int cnt, i;
    UDACCEL* uda;
    CAMLlocal2(contents, result);

    cnt = list_length(accels);
    if (cnt > 0)
        uda = malloc(cnt * sizeof(UDACCEL));
    else
        uda = NULL;
    i = 0;
    for (; !Is_nil(accels); accels = Tail(accels))
        udaccel_val(Head(accels), &uda[i++]);
    contents = pack_message(UDM_SETACCEL, cnt, (LPARAM)uda);
    result = callback(sender, contents);
    if (cnt > 0)
        free(uda);
    if (Int32_val(result) == 0)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value pack_udm_setbase(value base)
{
    return pack_message(UDM_SETBASE, Int_val(base), 0);
}

CAMLprim value unpack_udm_setbase(value result)
{
    int base;

    base = Int32_val(result);
    if (base == 0)
        raise_win32_error(0);
    return Val_int(base);
}

CAMLprim value pack_udm_setbuddy(value buddy)
{
    return pack_message(UDM_SETBUDDY, (WPARAM)Handle_val(buddy), 0);
}

CAMLprim value unpack_udm_setbuddy(value result)
{
    return alloc_handle((HWND)Int32_val(result));
}

CAMLprim value pack_udm_setpos(value pos)
{
    return pack_message(UDM_SETPOS, 0, MAKELONG(Int_val(pos), 0));
}

CAMLprim value unpack_udm_setpos(value result)
{
    return Val_int(Int32_val(result));
}

CAMLprim value pack_udm_setrange(value min, value max)
{
    return pack_message(UDM_SETRANGE, 0, MAKELONG(Int_val(max), Int_val(min)));
}

CAMLprim value pack_udm_setrange32(value min, value max)
{
    return pack_message(UDM_SETRANGE32, Int_val(min), Int_val(max));
}

/* Notifications */

CAMLprim value unpack_nmupdown(value msg)
{
    CAMLparam1(msg);
    NMUPDOWN* nmud;
    CAMLlocal1(val);

    nmud = (NMUPDOWN*)Lparam(msg);
    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(nmud->iPos));
    Store_field(val, 1, Val_int(nmud->iDelta));
    CAMLreturn(val);
}
