'
' test for WID
'

' NOTE: a parent IUP control must be mapped before you can use it's HWND handel.

#ifndef __FB_WIN32__
#error this is a windows only test (a linux version is under construction)
#endif

#include once "windows.bi"
#include once "win/commctrl.bi"

#include once "inc/iup.bi"

' register extra controls
sub _init_ constructor
  dim as INITCOMMONCONTROLSEX IccEx
  with IccEx
  .dwSize = sizeof(INITCOMMONCONTROLSEX)
  .dwICC  = &HFFFF ' all
  end with
  InitCommonControlsEx @IccEx
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub

' create the native windows control
sub CreateMonthCalender(hParent as HWND)
  ' first create a new user IUP class
  dim as IHandle ptr MonthCalender = IupUser()
  ' create the native control
  dim as HWND hWid = CreateWindowEx(0, MONTHCAL_CLASS, NULL, _
                                    WS_CHILD or WS_VISIBLE or MCS_DAYSTATE, _
                                    40,40,200,200, _
                                    hParent, _
                                    NULL,NULL,NULL)
  ' set our attributes (we can use any attribute names here) 
  IupSetAttribute(MonthCalender,"PARENTWID",cptr(zstring ptr,hParent))
  IupSetAttribute(MonthCalender,"WID",cptr(zstring ptr,hWid))
  ' optinal make our new control global (usefull inside callbacks)
  IupSetGlobal("MONTHCALENDER",cptr(zstring ptr,MonthCalender))
end sub

' the right time to create a child control
function frame_map_cb cdecl (frame as IHandle ptr) as integer
  dim as HWND hParent = cptr(HWND,IupGetAttribute(frame,"WID"))
  CreateMonthCalender(hParent)
  return IUP_DEFAULT
end function

' the right time to destoy a child control from parent
function frame_unmap_cb cdecl (frame as IHandle ptr) as integer
  ' get our user IUP class from global attribute hashtable
  dim as IHandle ptr MonthCalender = cptr(IHandle ptr,IupGetGlobal("MONTHCALENDER"))
  ' get HWND from MonthCalender
  dim as HWND hWin = cptr(HWND,IupGetAttribute(MonthCalender,"WID"))
  ' destroy the native control
  if hWin then DestroyWindow(hWin) 
  ' free all stored attrributes "WID" and "PARENTWID" etc.
  IupSetAttribute(MonthCalender,"CLEARATTRIBUTES",NULL) 
  ' remove our IUP class from global hastable
  IupSetGlobal("MONTHCALENDER",NULL)
  return IUP_DEFAULT
end function

function button_action_cb cdecl (button as IHandle ptr) as integer
  dim as SYSTEMTIME st
  ' get our IUP class
  dim as IHandle ptr MonthCalender = cptr(IHandle ptr,IupGetGlobal("MONTHCALENDER"))
  ' get the native window id
  dim as HWND hWin = cptr(HWND,IupGetAttribute(MonthCalender,"WID"))
  ' get current selected values from control
  SendMessage(hWin, MCM_GETCURSEL,0 ,cast(LPARAM,@st))
  print
  with st
    print "Year        : " & .wYear
    print "Month       : " & .wMonth
    print "DayOfWeek   : " & .wDayOfWeek
    print "Day         : " & .wDay
  end with   
  print
  return IUP_DEFAULT
end function

'
' main
'
dim as IHandle ptr frame = IupFrame(NULL)
IupSetAttribute(frame,"TITLE"  ,"IupFrame as parent")
IupSetCallback(frame,"MAP_CB"  ,@frame_map_cb)
IupSetCallback(frame,"UNMAP_CB",@frame_unmap_cb)

dim as IHandle ptr button = IupButton("Print values from MonthControl",NULL)
IupSetCallback(button,"ACTION",@button_action_cb)


dim as IHandle ptr vbox = IupVBox(frame, _
                                  IupFill(), _
                                  IupHBox(IupFill(),button,IupFill(),NULL), _
                                  IupFill(), _
                                  NULL) 


dim as IHandle ptr dlg = IupDialog(vbox)
IupSetAttribute(dlg, "TITLE", "Windows API")
IupSetAttribute(dlg, "SIZE", "200x200")
IupShowXY(dlg, IUP_CENTER, IUP_CENTER)

IupMainLoop()

