/* OCaml-Win32
 * win32_datetimepicker.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: Date and Time Picker Controls */

/* Styles */

static DWORD datetimepick_style_options[] = {
    DTS_APPCANPARSE,
    DTS_LONGDATEFORMAT,
    DTS_RIGHTALIGN,
    DTS_SHOWNONE,
    DTS_SHORTDATEFORMAT,
    DTS_TIMEFORMAT,
    DTS_UPDOWN
};
Define_static_constants(datetimepick_style_options);

CAMLprim value datetimepick_style_of_option(value option)
{
    return copy_int64((int64)Constant_val(option, datetimepick_style_options));
}

/* Messages */

CAMLprim value pack_dtm_getmccolor(value which)
{
    return pack_message(DTM_GETMCCOLOR, Constant_val(which, calendar_colors), 0);
}

CAMLprim value unpack_dtm_getmccolor(value result)
{
    return alloc_color((COLORREF)Int32_val(result));
}

CAMLprim value unpack_dtm_getmcfont(value result)
{
    return alloc_handle((HFONT)Int32_val(result));
}

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

CAMLprim value issue_dtm_getrange(value sender)
{
    CAMLparam1(sender);
    SYSTEMTIME st[2];
    CAMLlocal4(contents, result, val, v);

    contents = pack_message(DTM_GETRANGE, 0, (LPARAM)st);
    result = callback(sender, contents);
    val = alloc_tuple(2);
    if (Int32_val(result) & GDTR_MIN)
        v = alloc_some(alloc_systemtime(&st[0]));
    else
        v = Val_none;
    Store_field(val, 0, v);
    if (Int32_val(result) & GDTR_MAX)
        v = alloc_some(alloc_systemtime(&st[1]));
    else
        v = Val_none;
    Store_field(val, 1, v);
    CAMLreturn(val);
}

CAMLprim value issue_dtm_getsystemtime(value sender)
{
    CAMLparam1(sender);
    SYSTEMTIME st;
    CAMLlocal3(contents, result, val);

    contents = pack_message(DTM_GETSYSTEMTIME, 0, (LPARAM)&st);
    result = callback(sender, contents);
    if (Int32_val(result) == GDT_ERROR)
        raise_win32_error(0);
    if (Int32_val(result) == GDT_VALID)
        val = alloc_some(alloc_systemtime(&st));
    else
        val = Val_none;
    CAMLreturn(val);
}

CAMLprim value issue_dtm_setformat(value sender, value format)
{
    CAMLparam2(sender, format);
    char* s;
    CAMLlocal2(contents, result);

    s = new_stringoption(format);
    contents = pack_message(DTM_SETFORMAT, 0, (LPARAM)s);
    result = callback(sender, contents);
    free_stringoption(s);
    if (Int32_val(result) == 0)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value pack_dtm_setmccolor(value which, value color)
{
    return pack_message(DTM_SETMCCOLOR, Constant_val(which, calendar_colors), (LPARAM)color_val(color));
}

CAMLprim value unpack_dtm_setmccolor(value result)
{
    if (Int32_val(result) == -1)
        raise_win32_error(0);
    return alloc_color((COLORREF)Int32_val(result));
}

CAMLprim value pack_dtm_setmcfont(value font, value redraw)
{
    return pack_message(DTM_SETMCFONT, (WPARAM)Handle_val(font), Bool_val(redraw));
}

CAMLprim value issue_dtm_setrange(value sender, value min, value max)
{
    CAMLparam3(sender, min, max);
    DWORD flags;
    SYSTEMTIME st[2];
    CAMLlocal2(contents, result);

    flags = 0;
    if (Is_some(min)) {
        flags |= GDTR_MIN;
        systemtime_val(Value_of(min), &st[0]);
    }
    if (Is_some(max)) {
        flags |= GDTR_MAX;
        systemtime_val(Value_of(max), &st[1]);
    }
    contents = pack_message(DTM_SETRANGE, flags, (LPARAM)st);
    result = callback(sender, contents);
    if (Int32_val(result) == 0)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value issue_dtm_setsystemtime(value sender, value time)
{
    CAMLparam2(sender, time);
    DWORD flag;
    SYSTEMTIME st;
    CAMLlocal2(contents, result);

    if (Is_some(time)) {
        flag = GDT_VALID;
        systemtime_val(Value_of(time), &st);
    } else
        flag = GDT_NONE;
    contents = pack_message(DTM_SETSYSTEMTIME, flag, (LPARAM)&st);
    result = callback(sender, contents);
    if (Int32_val(result) == 0)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

/* Notifications */

CAMLprim value unpack_nmdatetimechange(value msg)
{
    CAMLparam1(msg);
    NMDATETIMECHANGE* nmdtc;
    CAMLlocal2(val, v);

    nmdtc = (NMDATETIMECHANGE*)Lparam(msg);
    val = alloc_tuple(1);
    if (nmdtc->dwFlags == GDT_VALID)
        v = alloc_some(alloc_systemtime(&nmdtc->st));
    else
        v = Val_none;
    Store_field(val, 0, v);
    CAMLreturn(val);
}

CAMLprim value on_dtn_format(value handler, value wnd, value msg)
{
    CAMLparam3(handler, wnd, msg);
    NMHDR* nmh;
    NMDATETIMEFORMAT* nmdtf;
    CAMLlocalN(args, 6);
    CAMLlocal2(result, ret);

    ret = Val_none;
    if (Int_val(Field(msg, 0)) == WM_NOTIFY) {
        nmh = (NMHDR*)Lparam(msg);
        if (nmh->code == DTN_FORMAT) {
            nmdtf = (NMDATETIMEFORMAT*)nmh;
            args[0] = wnd;
            args[1] = msg;
            args[2] = Val_int(Wparam(msg));
            args[3] = alloc_nmhdr(nmh);
            args[4] = copy_string(nmdtf->pszFormat);
            args[5] = alloc_systemtime(&nmdtf->st);
            result = callbackN(handler, 6, args);
            if (Is_block(result)) {
                if (Tag_val(result) == 0)
                    ret = alloc_some(Field(result, 0));
                else {
                    strncpy(nmdtf->szDisplay, String_val(Field(result, 0)),
                        sizeof(nmdtf->szDisplay) - 1);
                    nmdtf->szDisplay[sizeof(nmdtf->szDisplay)-1] = '\0';
                    ret = alloc_some(copy_int32(0));
                }
            }
        }
    }
    CAMLreturn(ret);
}

CAMLprim value on_dtn_formatquery(value handler, value wnd, value msg)
{
    CAMLparam3(handler, wnd, msg);
    NMHDR* nmh;
    NMDATETIMEFORMATQUERY* nmdtfq;
    CAMLlocalN(args, 5);
    CAMLlocal2(result, ret);

    ret = Val_none;
    if (Int_val(Field(msg, 0)) == WM_NOTIFY) {
        nmh = (NMHDR*)Lparam(msg);
        if (nmh->code == DTN_FORMATQUERY) {
            nmdtfq = (NMDATETIMEFORMATQUERY*)nmh;
            args[0] = wnd;
            args[1] = msg;
            args[2] = Val_int(Wparam(msg));
            args[3] = alloc_nmhdr(nmh);
            args[4] = copy_string(nmdtfq->pszFormat);
            result = callbackN(handler, 5, args);
            if (Is_block(result)) {
                if (Tag_val(result) == 0)
                    ret = alloc_some(Field(result, 0));
                else {
                    nmdtfq->szMax = size_val(Field(result, 0));
                    ret = alloc_some(copy_int32(0));
                }
            }
        }
    }
    CAMLreturn(ret);
}

CAMLprim value on_dtn_userstring(value handler, value wnd, value msg)
{
    CAMLparam3(handler, wnd, msg);
    NMHDR* nmh;
    NMDATETIMESTRING* nmdts;
    CAMLlocalN(args, 5);
    CAMLlocal3(result, st, ret);

    ret = Val_none;
    if (Int_val(Field(msg, 0)) == WM_NOTIFY) {
        nmh = (NMHDR*)Lparam(msg);
        if (nmh->code == DTN_USERSTRING) {
            nmdts = (NMDATETIMESTRING*)nmh;
            args[0] = wnd;
            args[1] = msg;
            args[2] = Val_int(Wparam(msg));
            args[3] = alloc_nmhdr(nmh);
            args[4] = copy_string(nmdts->pszUserString);
            result = callbackN(handler, 5, args);
            if (Is_block(result)) {
                if (Tag_val(result) == 0)
                    ret = alloc_some(Field(result, 0));
                else {
                    st = Field(result, 0);
                    if (Is_some(st)) {
                        systemtime_val(Value_of(st), &nmdts->st);
                        nmdts->dwFlags = GDT_VALID;
                    } else
                        nmdts->dwFlags = GDT_NONE;
                    ret = alloc_some(copy_int32(0));
                }
            }
        }
    }
    CAMLreturn(ret);
}

CAMLprim value on_dtn_wmkeydown(value handler, value wnd, value msg)
{
    CAMLparam3(handler, wnd, msg);
    NMHDR* nmh;
    NMDATETIMEWMKEYDOWN* nmdtkd;
    CAMLlocalN(args, 7);
    CAMLlocal2(result, ret);

    ret = Val_none;
    if (Int_val(Field(msg, 0)) == WM_NOTIFY) {
        nmh = (NMHDR*)Lparam(msg);
        if (nmh->code == DTN_WMKEYDOWN) {
            nmdtkd = (NMDATETIMEWMKEYDOWN*)nmh;
            args[0] = wnd;
            args[1] = msg;
            args[2] = Val_int(Wparam(msg));
            args[3] = alloc_nmhdr(nmh);
            args[4] = alloc_virtualkeycode((BYTE)nmdtkd->nVirtKey);
            args[5] = copy_string(nmdtkd->pszFormat);
            args[6] = alloc_systemtime(&nmdtkd->st);
            result = callbackN(handler, 7, args);
            if (Is_block(result)) {
                if (Tag_val(result) == 0)
                    ret = alloc_some(Field(result, 0));
                else {
                    systemtime_val(Field(result, 0), &nmdtkd->st);
                    ret = alloc_some(copy_int32(0));
                }
            }
        }
    }
    CAMLreturn(ret);
}
