(* OCaml-Win32
 * win32_control.ml
 * 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.
 *)

open Win32_core

(********************************************************************)
(* User Interface: Windows User Experience: Windows Shell: SDK: Windows Controls: General Control Information *)

external get_com_ctl_version : unit -> (int * int) = "get_com_ctl_version"

external get_effective_client_rect : wnd:hwnd -> ctrl_ids:int list -> rect = "get_effective_client_rect"

external init_common_controls : unit -> unit = "init_common_controls"

type init_common_control_option =
    ICC_ANIMATE_CLASS
  | ICC_BAR_CLASSES
  | ICC_COOL_CLASSES
  | ICC_DATE_CLASSES
  | ICC_HOTKEY_CLASS
  | ICC_INTERNET_CLASSES
  | ICC_LISTVIEW_CLASSES
  | ICC_PAGESCROLLER_CLASS
  | ICC_PROGRESS_CLASS
  | ICC_TAB_CLASSES
  | ICC_TREEVIEW_CLASSES
  | ICC_UPDOWN_CLASS
  | ICC_USEREX_CLASSES
  | ICC_WIN95_CLASSES
external init_common_controls_ex : options:init_common_control_option list -> unit = "init_common_controls_ex"

external show_hide_menu_ctl :
    wnd:hwnd ->
    item:int ->
    menu:hmenu ->
    item_ctrl_pairs:(int * int) list ->
    unit = "show_hide_menu_ctl"

(* Messages *)

let wm_notifyformat = 0x0055
(* ... *)

let ccm_getunicodeformat = ccm_first + 6
let pack_ccm_getunicodeformat = pack_simple_message ~id:ccm_getunicodeformat
external unpack_ccm_getunicodeformat : message_result -> bool = "unpack_ccm_getunicodeformat"
let issue_ccm_getunicodeformat ~sender = unpack_ccm_getunicodeformat (sender ~msg:pack_ccm_getunicodeformat)

let ccm_setunicodeformat = ccm_first + 5
external pack_ccm_setunicodeformat : unicode:bool -> message_contents = "pack_ccm_setunicodeformat"
external unpack_ccm_setunicodeformat : message_result -> bool = "unpack_ccm_setunicodeformat"
let issue_ccm_setunicodeformat ~sender ~unicode = unpack_ccm_setunicodeformat (sender ~msg:(pack_ccm_setunicodeformat ~unicode))

(* Notifications *)

type nmchar = {
    nmc_char : char;
    nmc_prev_item : int;
    nmc_next_item : int }
external unpack_nmchar : msg:message_contents -> nmchar = "unpack_nmchar"

type nmkey = {
    nmk_key : virtual_key_code;
    nmk_data : key_data }
external unpack_nmkey : msg:message_contents -> nmkey = "unpack_nmkey"

type nmmouse = {
    nmm_item_spec : int;
    nmm_item_data : int32;
    nmm_pt : point }
external unpack_nmmouse : msg:message_contents -> nmmouse = "unpack_nmmouse"

let nm_first = 0
let nm_last = -99

let nm_char = nm_first - 18
let on_nm_char = on_notify_message ~code:nm_char ~unpack:unpack_nmchar ~return:return_simple_message

let nm_click = nm_first - 2
let on_nm_click = on_notify_message ~code:nm_click ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_dblclk = nm_first - 3
let on_nm_dblclk = on_notify_message ~code:nm_dblclk ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_hover = nm_first - 13
let on_nm_hover = on_notify_message ~code:nm_hover ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_keydown = nm_first - 15
let on_nm_keydown = on_notify_message ~code:nm_keydown ~unpack:unpack_nmkey ~return:return_common_notify

let nm_killfocus = nm_first - 8
let on_nm_killfocus = on_notify_message ~code:nm_killfocus ~unpack:unpack_simple_notify ~return:return_simple_message

let nm_nchittest = nm_first - 14
external return_nm_nchittest : result:hit_test_result option -> message_result = "return_nm_nchittest"
let on_nm_nchittest = on_notify_message ~code:nm_nchittest ~unpack:unpack_nmmouse ~return:return_nm_nchittest

let nm_outofmemory = nm_first - 1
let on_nm_outofmemory = on_notify_message ~code:nm_outofmemory ~unpack:unpack_simple_notify ~return:return_simple_message

let nm_rclick = nm_first - 5
let on_nm_rclick = on_notify_message ~code:nm_rclick ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_rdblclk = nm_first - 6
let on_nm_rdblclk = on_notify_message ~code:nm_rdblclk ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_releasedcapture = nm_first - 16
let on_nm_releasedcapture = on_notify_message ~code:nm_releasedcapture ~unpack:unpack_simple_notify ~return:return_simple_message

let nm_return = nm_first - 4
let on_nm_return = on_notify_message ~code:nm_return ~unpack:unpack_simple_notify ~return:return_common_notify

let nm_setcursor = nm_first - 17
external return_nm_setcursor : result:bool -> message_result = "return_nm_setcursor"
let on_nm_setcursor = on_notify_message ~code:nm_setcursor ~unpack:unpack_nmmouse ~return:return_nm_setcursor

let nm_setfocus = nm_first - 7
let on_nm_setfocus = on_notify_message ~code:nm_setfocus ~unpack:unpack_simple_notify ~return:return_simple_message

(* Custom draw *)

type customdraw_draw_stage_type =
    Cddst_global
  | Cddst_item
  | Cddst_subitem
type customdraw_draw_stage =
    CDDS_POSTERASE
  | CDDS_POSTPAINT
  | CDDS_PREERASE
  | CDDS_PREPAINT
type customdraw_item_state =
    CDIS_CHECKED
  | CDIS_DEFAULT
  | CDIS_DISABLED
  | CDIS_FOCUS
  | CDIS_GRAYED
  | CDIS_HOT
  | CDIS_INDETERMINATE
  | CDIS_MARKED
  | CDIS_SELECTED
type nmcustomdraw = {
    nmcd_draw_stage : customdraw_draw_stage_type * customdraw_draw_stage;
    nmcd_dc : hdc;
    nmcd_rect : rect;
    nmcd_item_spec : int32;
    nmcd_item_state : customdraw_item_state list;
    nmcd_item_lparam : int32 }
external unpack_nmcustomdraw : msg:message_contents -> nmcustomdraw = "unpack_nmcustomdraw"

let nm_customdraw = nm_first - 12
type nm_customdraw_result =
    CDRF_NEWFONT
  | CDRF_NOTIFYITEMDRAW
  | CDRF_NOTIFYPOSTERASE
  | CDRF_NOTIFYPOSTPAINT
  | CDRF_NOTIFYSUBITEMDRAW
  | CDRF_SKIPDEFAULT
  | TBCDRF_HILITEHOTTRACK
  | TBCDRF_NOEDGES
  | TBCDRF_NOETCHEDEFFECT
  | TBCDRF_NOMARK
  | TBCDRF_NOOFFSET
  | TVCDRF_NOIMAGES
external return_nm_customdraw : result:nm_customdraw_result list -> message_result = "return_nm_customdraw"
let on_nm_customdraw = on_notify_message ~code:nm_customdraw ~unpack:unpack_nmcustomdraw ~return:return_nm_customdraw
