'#define DEBUG ' eanble dprint(string) macro
#include once "inc/wx.bi"

' sound flags are
'  wxSOUND_SYNC
'  wxSOUND_ASYNC
'  wxSOUND_LOOP

enum ID
  frmMain = 1
  btnPlay
  btnStop
  chbLoops
end enum

dim shared as _wxApp      ptr App
dim shared as _wxSound    ptr Sound
dim shared as _wxCheckBox ptr CheckBoxLoops
  
sub EventListener APICALL (event as _wxEvent ptr, iListener as wxInt)
  select case wxEvent_GetID(event)
  case btnPlay : dim as wxUint flag = wxSOUND_ASYNC
                 if wxCheckBox_IsChecked(CheckBoxLoops) then
                   flag or= wxSOUND_LOOP
                 end if  
                 wxSound_Play(Sound,flag)
  case btnStop : wxSound_Stop(Sound)
  end select
end sub

function OnInit APICALL as wxBool
  ' create a wxFrame
  dim as _wxFrame ptr Frame = wxFrame_ctor()
  wxFrame_Create(frame,WX_NULL,frmMain, wxString("test of wxSound"),,,,,wxFRAME_DEFAULT_STYLE)
  ' create a wxPanel
  dim as _wxPanel ptr Panel = wxPanel_ctor2(Frame)
  
  ' create a wxButton play
  dim as _wxButton ptr ButtonPlay = wxButton_ctor()
  wxButton_Create(ButtonPlay, Panel,btnPlay, wxString("play"),10,10)
  wxEvtHandler_proxy(ButtonPlay, @EventListener)
  wxEvtHandler_Connect(ButtonPlay, wxEvent_EVT_COMMAND_BUTTON_CLICKED())
  
  ' create a wxButton stop
  dim as _wxButton ptr ButtonStop = wxButton_ctor()
  wxButton_Create(ButtonStop,Panel,btnStop, wxString("stop"),10,40)
    wxEvtHandler_proxy(ButtonStop, @EventListener)
  wxEvtHandler_Connect(ButtonStop, wxEvent_EVT_COMMAND_BUTTON_CLICKED())

  ' create a wxCheckBox loop
  CheckBoxLoops = wxCheckBox_ctor()
  wxCheckbox_Create(CheckBoxLoops,Panel,chbLoops, wxString("loop"),10,70)
  
  ' create wxSound
  Sound = wxSound_ctor()
  wxSound_Create(Sound,wxString("./media/rnb_loop.wav") )
  
  if wxSound_IsOK(Sound)=WX_FALSE then
    wxMsgBox(,wxString("sound object isn't ok:"),wxString("error:"))
    return WX_FALSE
  end if
    
  wxWindow_Show(Frame)
  
  return wxApp_OnInit(App)
end function  

function OnExit APICALL as wxInt
  return wxApp_OnExit(App)
end function

' set the current path for sound loading
ChDir ExePath

' create our wxApplication
App = wxApp_ctor()
wxApp_RegisterVirtual(App,@OnInit,@OnExit)
' let it run
wxApp_Run()
