''
'' wxWindows-c example, by dumbledore
''

#define DEBUG
#Include Once "inc/wx.bi"

Const FRAME_W = 500
Const FRAME_H = 400

''
'' ids
''
Enum
  ID_FRAME=1
  ID_DIALOG
  ID_PANEL
  ID_TEXTCTRL
  ID_CLICKMEBUTTON
  ID_EXITBUTTON
  ID_DIAG_EXITBUTTON
End Enum

Declare Function app_oninit_cb APICALL () As wxBool
Declare Function app_onexit_cb APICALL () As wxInt

''
'' globals
''
Dim Shared As _wxApp Ptr app
Dim Shared As _wxFrame Ptr frame
Dim Shared As _wxDialog Ptr dialog

'':::::
''
'' main 
''
app = wxApp_ctor()
wxApp_RegisterVirtual(app, @app_oninit_cb, @app_onexit_cb)
wxApp_Run

'':::::
''
'' dialog's exit button callback
''
Sub diag_exitbutton_cb APICALL (event As _wxEvent Ptr)
  dprint("diag_exitbutton_cb")
  wxDialog_EndModal( dialog, 0 )
End Sub

'':::::
''
'' panel's click-me button callback
''
Sub panel_clickmebutton_cb APICALL (event As _wxEvent Ptr)
  dprint("panel_clickmebutton_cb")
  wxDialog_ShowModal( dialog )
end Sub

'':::::
''
'' panel's exit button callback
''
Sub panel_exitbutton_cb APICALL (event As _wxEvent Ptr)
  dprint("panel_exitbutton_cb")
  wxWindow_Close( frame, 0 )
End Sub

'':::::
''
'' on init callback
''
Function app_oninit_cb APICALL () As wxBool
  
  dprint("app_oninit_cb")
  Dim As Integer ypos, size
   
  '' create the main window
  frame = wxFrame_ctor( )
  wxFrame_Create(frame,,ID_FRAME, wxString("Welcome to WX-C"), 200, 200, FRAME_W, FRAME_H, wxFRAME_DEFAULT_STYLE, WX_NULL)

  '' make a dialog
  dialog = wxDialog_ctor( )
  wxDialog_Create(dialog, frame, ID_DIALOG, wxString("Press Exit"), 10, 10, 500, 400, wxFRAME_DEFAULT_STYLE, 0 )
   
  '' add widgets to the dialog
  size = 27: ypos=1
  wxButton_Create(wxButton_ctor(),dialog,,wxString("&Goto")  ,405,ypos,85): ypos += size
  wxButton_Create(wxButton_ctor(),dialog,,wxString("&Select"),405,ypos,85): ypos += size
  wxButton_Create(wxButton_ctor(),dialog,,wxString("E&dit")  ,405,ypos,85): ypos += size
  wxButton_Create(wxButton_ctor(),dialog,,wxString("&Add")   ,405,ypos,85): ypos += size

  Dim As _wxListBox Ptr selector = wxListBox_ctor( )
  wxListBox_Create( selector, dialog,-1, -1,-1,400,350,0, LB_SINGLE Or LB_NEED_SB Or LB_HSCROLL)

  Dim As _wxFont Ptr font = wxFont_ctor( 10, wxMODERN, wxNORMAL, wxNORMAL, 0, 0, 0 )
  wxWindow_SetFont( selector, font )

  Dim As Integer i
  Dim As _wxArrayString Ptr tempString = wxArrayString_ctor( )
  For i = 0 To 15
    wxArrayString_Add( tempString, wxString("list item " + Str( i+1 )) )
    wxListBox_InsertItems( selector, tempString, i )
    wxArrayString_Clear( tempString )
  Next
  
  '' create the main panel
  Dim As _wxPanel Ptr panel = wxPanel_ctor2(frame)

  '' create a text widget
  '' we're not using this thing for any events so it gets an id of -1
  Dim As _wxTextCtrl Ptr text = wxTextCtrl_ctor( )
  wxTextCtrl_Create(text, panel,ID_TEXTCTRL, wxString("Hello from wx-c in fb!"), 8,8,FRAME_W-24,FRAME_H-200, wxVSCROLL)
  wxTextCtrl_SetFont( text, font )
  
   
  Dim As _wxButton Ptr diag_exitbutton = wxButton_ctor( )
  wxButton_Create(diag_exitbutton, dialog, ID_DIAG_EXITBUTTON, wxString("&Exit"), 405, ypos, 85)

  Dim As _wxButton Ptr panel_clickmebutton = wxButton_ctor( )
  wxButton_Create(panel_clickmebutton, panel, ID_CLICKMEBUTTON,wxString("show dialog"), FRAME_W\2 - 100, FRAME_H - 60)

  Dim As _wxButton Ptr panel_exitbutton = wxButton_ctor( )
  wxButton_Create(panel_exitbutton, panel, ID_EXITBUTTON, wxString("Exit"), FRAME_W\2 + 40, FRAME_H - 60)

  '' connect the button handler
  wxEvtHandler_ConnectByType(diag_exitbutton    ,wxEvent_EVT_COMMAND_BUTTON_CLICKED(), @diag_exitbutton_cb)
  wxEvtHandler_ConnectByType(panel_clickmebutton,wxEvent_EVT_COMMAND_BUTTON_CLICKED(), @panel_clickmebutton_cb)
  wxEvtHandler_ConnectByType(panel_exitbutton   ,wxEvent_EVT_COMMAND_BUTTON_CLICKED(), @panel_exitbutton_cb)
 
  '' center it
  wxWindow_CenterOnScreen(frame, wxBOTH )
    
  '' show window
  wxWindow_Show(frame)

  Function = wxApp_OnInit(app)
    
End Function

'':::::
''
'' on exit callback
''
Function app_onexit_cb APICALL () As wxInt
  dprint("app_onexit_cb")
  wxMsgBox(,wxString("Bye Bye..."), wxString("Window Closed!"))
  Function = wxApp_OnExit( app )
End Function
 
