/* OCaml-Win32
 * win32_flatscrollbar.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: Flat Scroll Bars */

static DWORD flat_sb_modes[] = {
    FSB_ENCARTA_MODE,
    FSB_FLAT_MODE,
    FSB_REGULAR_MODE
};
Define_static_constants(flat_sb_modes);

static DWORD flat_sb_int_props[] = {
    WSB_PROP_CXHSCROLL,
    WSB_PROP_CXHTHUMB,
    WSB_PROP_CXVSCROLL,
    WSB_PROP_CYHSCROLL,
    WSB_PROP_CYVSCROLL,
    WSB_PROP_CYVTHUMB
};
Define_static_constants(flat_sb_int_props);

static DWORD flat_sb_color_props[] = {
    WSB_PROP_HBKGCOLOR,
    WSB_PROP_VBKGCOLOR
};
Define_static_constants(flat_sb_color_props);

static DWORD flat_sb_mode_props[] = {
    WSB_PROP_HSTYLE,
    WSB_PROP_VSTYLE
};
Define_static_constants(flat_sb_mode_props);

CAMLprim value flat_sb_enable_scroll_bar(value wnd, value type, value option)
{
    CAMLparam3(wnd, type, option);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_EnableScrollBar(
        hwnd,
        Constant_val(type, scroll_bar_types),
        Constant_val(option, enable_scroll_bar_options));
    leave_blocking_section();
    CAMLreturn(Val_bool(result));
}

CAMLprim value flat_sb_get_scroll_info(value wnd, value type)
{
    SCROLLINFO si;

    si.cbSize = sizeof(SCROLLINFO);
    si.fMask = SIF_ALL;
    if (!FlatSB_GetScrollInfo(Handle_val(wnd), Constant_val(type, scroll_bar_types), &si))
        raise_last_error();
    return alloc_getscrollinfo(&si);
}

CAMLprim value flat_sb_get_scroll_pos(value wnd, value type)
{
    return Val_int(FlatSB_GetScrollPos(Handle_val(wnd), Constant_val(type, scroll_bar_types)));
}

CAMLprim value flat_sb_get_int_prop(value wnd, value prop)
{
    INT val;

    if (!FlatSB_GetScrollProp(Handle_val(wnd), Constant_val(prop, flat_sb_int_props), &val))
        raise_win32_error(0);
    return Val_int(val);
}

CAMLprim value flat_sb_get_color_prop(value wnd, value prop)
{
    COLORREF val;

    if (!FlatSB_GetScrollProp(Handle_val(wnd), Constant_val(prop, flat_sb_color_props), (LPINT)&val))
        raise_win32_error(0);
    return alloc_color(val);
}

CAMLprim value flat_sb_get_mode_prop(value wnd, value prop)
{
    INT val;

    FlatSB_GetScrollProp(Handle_val(wnd), Constant_val(prop, flat_sb_mode_props), &val);
    return Val_constant(val, flat_sb_modes);
}

CAMLprim value flat_sb_get_palette(value wnd)
{
    HPALETTE val;

    if (!FlatSB_GetScrollProp(Handle_val(wnd), WSB_PROP_PALETTE, (LPINT)&val))
        raise_win32_error(0);
    return alloc_handle(val);
}

CAMLprim value flat_sb_get_winstyle(value wnd)
{
    INT val;

    if (!FlatSB_GetScrollProp(Handle_val(wnd), WSB_PROP_WINSTYLE, &val))
        raise_win32_error(0);
    return copy_int64(val);
}

CAMLprim value flat_sb_get_scroll_range(value wnd, value type)
{
    CAMLparam2(wnd, type);
    int min, max;
    CAMLlocal1(val);

    if (!FlatSB_GetScrollRange(Handle_val(wnd), Constant_val(type, scroll_bar_types), &min, &max))
        raise_last_error();
    val = alloc_tuple(2);
    Store_field(val, 0, Val_int(min));
    Store_field(val, 1, Val_int(max));
    CAMLreturn(val);
}

CAMLprim value flat_sb_set_scroll_info(value wnd, value type, value info, value redraw)
{
    CAMLparam4(wnd, type, info, redraw);
    HWND hwnd;
    SCROLLINFO si;
    int result;

    hwnd = Handle_val(wnd);
    setscrollinfo_val(info, &si);
    enter_blocking_section();
    result = FlatSB_SetScrollInfo(
        hwnd,
        Constant_val(type, scroll_bar_types),
        &si,
        Bool_val(redraw));
    leave_blocking_section();
    CAMLreturn(Val_int(result));
}

CAMLprim value flat_sb_set_scroll_pos(value wnd, value type, value pos, value redraw)
{
    CAMLparam4(wnd, type, pos, redraw);
    HWND hwnd;
    int result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_SetScrollPos(
        hwnd,
        Constant_val(type, scroll_bar_types),
        Int_val(pos),
        Bool_val(redraw));
    leave_blocking_section();
    CAMLreturn(Val_int(result));
}

CAMLprim value flat_sb_set_int_prop(value wnd, value prop, value val, value redraw)
{
    CAMLparam4(wnd, prop, val, redraw);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_SetScrollProp(hwnd, Constant_val(prop, flat_sb_int_props), Int_val(val), Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_set_color_prop(value wnd, value prop, value color, value redraw)
{
    CAMLparam4(wnd, prop, color, redraw);
    HWND hwnd;
    COLORREF c;
    BOOL result;

    hwnd = Handle_val(wnd);
    c = color_val(color);
    enter_blocking_section();
    result = FlatSB_SetScrollProp(hwnd, Constant_val(prop, flat_sb_color_props), (int)c, Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_set_mode_prop(value wnd, value prop, value mode, value redraw)
{
    CAMLparam4(wnd, prop, mode, redraw);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_SetScrollProp(hwnd, Constant_val(prop, flat_sb_mode_props), Constant_val(mode, flat_sb_modes), Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_set_palette(value wnd, value palette, value redraw)
{
    CAMLparam3(wnd, palette, redraw);
    HWND hwnd;
    HPALETTE p;
    BOOL result;

    hwnd = Handle_val(wnd);
    p = Handle_val(palette);
    enter_blocking_section();
    result = FlatSB_SetScrollProp(hwnd, WSB_PROP_PALETTE, (int)p, Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_set_winstyle(value wnd, value style, value redraw)
{
    CAMLparam3(wnd, style, redraw);
    HWND hwnd;
    LARGE_INTEGER li;
    BOOL result;

    hwnd = Handle_val(wnd);
    li.QuadPart = Int64_val(style);
    enter_blocking_section();
    result = FlatSB_SetScrollProp(hwnd, WSB_PROP_WINSTYLE, (int)li.LowPart, Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_set_scroll_range(value wnd, value type, value range, value redraw)
{
    CAMLparam4(wnd, type, range, redraw);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_SetScrollRange(
        hwnd,
        Constant_val(type, scroll_bar_types),
        Int_val(Field(range, 0)),
        Int_val(Field(range, 1)),
        Bool_val(redraw));
    leave_blocking_section();
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}

CAMLprim value flat_sb_show_scroll_bar(value wnd, value type, value show)
{
    CAMLparam3(wnd, type, show);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = FlatSB_ShowScrollBar(hwnd, Constant_val(type, scroll_bar_types), Bool_val(show));
    leave_blocking_section();
    if (!result)
        raise_last_error();
    CAMLreturn(Val_unit);
}

CAMLprim value initialize_flat_sb(value wnd)
{
    CAMLparam1(wnd);
    HWND hwnd;
    BOOL result;

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = InitializeFlatSB(hwnd);
    leave_blocking_section();
    if (!result)
        raise_win32_error(0);
    CAMLreturn(Val_unit);
}

CAMLprim value uninitialize_flat_sb(value wnd)
{
    CAMLparam1(wnd);
    HWND hwnd;
    HRESULT result;
    CAMLlocal1(val);

    hwnd = Handle_val(wnd);
    enter_blocking_section();
    result = UninitializeFlatSB(hwnd);
    leave_blocking_section();
    if (result == S_OK)
        val = Val_int(0);
    else if (result == S_FALSE)
        val = Val_int(1);
    else
        val = Val_int(2);
    CAMLreturn(val);
}
