''Helper functions

#Include Once "inc/Visual_WX-C_Designer.bi"

''holds the temp icons for bitmapbuttons
Dim Shared As wxBitmapButton Ptr bitbuttons( )
Dim Shared As wxBitmapButton Ptr currentButton

''
'' Replaces 'search' in 'subject' with 'replacement'
''
Function str_replace(search As String, replacement As String, subject As String) As String
	Dim result As String
	Dim As Integer i
	
	For i = 0 To Len(subject) - 1
		If subject[i] = search[0] Then
			If Mid(subject, i + 1, Len(search)) = search Then
				result += replacement
				i += Len(search) - 1
			Else
				result += Chr(subject[i])
			EndIf
		Else
			result += Chr(subject[i])
		EndIf
	Next
	
	Return result
End Function

''
'' Counts 'search' in 'subject'
''
Function str_count(search As String, subject As String) As Integer
	Dim As Integer result = 0, i
	
	For i = 1 To Len(subject) - Len(search)
		If Mid(subject, i, Len(search)) = search Then
			result += 1
		EndIf
	Next
	
	Return result
End Function

''
'' 
''
Function str_getline(subject As String, num As Integer) As String
	Dim As String result
	Dim As Integer cur = 0, i
	
	For i = 0 To Len(subject) - 1
		If subject[i] = Asc(!"\r") Then
			cur += 1
		ElseIf cur = num Then
			result += Chr(subject[i])
		EndIf
		If cur > num Then
			Exit For
		EndIf
	Next
	
	Return result
End Function

''
'' 
''
'Function labeldiag( ByVal label As String ) As wxTextCtrl Ptr
'	Dim As wxButton Ptr closebutton
'	Dim As wxTextCtrl Ptr textctrl
'	Dim As wxDialog Ptr dialog
'	
'	dialog = wxDialog( )
'	wxDialog_Create( dialog, 0, -1, label, wxSize(0, 0), wxSize(160, 190), wxDEFAULT_DIALOG_STYLE, 0 )
'	wxWindow_CenterOnScreen( dialog, wxBOTH )
'	
'	textctrl = wxTextCtrl( )
'	wxTextCtrl_Create( textctrl, dialog, -1, "", wxSize(5, 5), wxSize(140, 120), wxTE_MULTILINE Or wxHSCROLL, 0, 0 )
'	
'	closebutton = wxButton( )
'	wxButton_Create( closebutton, dialog, 1, "Done", wxSize(30, 130), wxSize(-1, -1), 0, 0, 0 )
'	wxEvtHandler_proxy( dialog, Cast(EventListener, @closewin) )
'	wxEvtHandler_Connect( dialog, wxEvent_EVT_COMMAND_BUTTON_CLICKED(), 1, -1, 0 )
'	wxDialog_ShowModal( dialog )
'	
'	Return textctrl
'	
'End Function

''
'' Translate wxString to FBString
''
Function wxString_to_fbstring( ByVal wxstr As wxString Ptr ) As String
	
	Dim As String ret
	Dim As Integer i
	
	For i = 0 To wxString_Length( wxstr )
		If wxString_CharAt( wxstr, i ) <> 0 Then
			ret += Chr( wxString_CharAt( wxstr, i ) )
		EndIf
	Next
	
	Return ret
	
End Function

''
'' Translate wxString to ZString Ptr
''
Function wxString_to_zstring( ByVal wxstr As wxString Ptr ) As ZString Ptr
	
	Dim As ZString Ptr ret
	Dim As Integer i
	
	ret = Callocate( wxString_Length( wxstr ), SizeOf(wxString Ptr) ) + 1
	For i = 0 To wxString_Length( wxstr )
		ret[i] = wxString_CharAt( wxstr, i )
	Next
	
	Return ret
	
End Function

''
'' Update frame properties
''
Sub updateframelist( )
	
	wxWindow_Enable( mylist, TRUE )
	wxListCtrl_DeleteAllItems( mylist )
	
	wxListCtrl_InsertTextItem(mylist, 0, StrPtr("wx_frame/wx_panel"))
	wxListCtrl_SetItem_By_Row_Col(mylist, 0, 1, "Name", 0)
	wxListCtrl_InsertTextItem(mylist, 1, StrPtr(app.title))
	wxListCtrl_SetItem_By_Row_Col(mylist, 1, 1, "Windowtitle", 0)
	
	
	
	Dim As wxSize Ptr p_s = wxSize( -1, -1 )
	wxWindow_GetPosition( proj, p_s )
	wxListCtrl_InsertTextItem(mylist, 2, Str(wxSize_GetWidth( p_s )))
	wxListCtrl_SetItem_By_Row_Col(mylist, 2, 1, "X-Pos", 0)
	wxListCtrl_InsertTextItem(mylist, 3, Str(wxSize_GetHeight( p_s )))
	wxListCtrl_SetItem_By_Row_Col(mylist, 3, 1, "Y-Pos", 0)
	
	Dim As wxSize Ptr s_s = wxSize( -1, -1 )
	wxWindow_GetSize( proj, s_s )
	wxListCtrl_InsertTextItem(mylist, 4, Str(wxSize_GetWidth( s_s )))
	wxListCtrl_SetItem_By_Row_Col(mylist, 4, 1, "Width", 0)
	wxListCtrl_InsertTextItem(mylist, 5, Str(wxSize_GetHeight( s_s )))
	wxListCtrl_SetItem_By_Row_Col(mylist, 5, 1, "Height", 0)
	
	wxListCtrl_InsertTextItem(mylist, 6, StrPtr(app.savePath))
	wxListCtrl_SetItem_By_Row_Col(mylist, 6, 1, "Savepath", 0)
	wxListCtrl_InsertTextItem(mylist, 7, StrPtr(app.icon))
	wxListCtrl_SetItem_By_Row_Col(mylist, 7, 1, "Icon", 0)
	
	'wxListCtrl_InsertTextItem(mylist, 8, StrPtr(app.relativePath))
	'wxListCtrl_SetItem_By_Row_Col(mylist, 8, 1, "Relative Path", 0)End Sub
	
End Sub

''
'' Update widget properties
''
Sub updatelist( ByVal widget As VWX_Widget Ptr )
	Dim As Integer i
	
	wxWindow_Enable( mylist, TRUE )
	wxWindow_Enable( updbut, TRUE )
	wxWindow_Enable( delbut, TRUE )
	wxWindow_Enable( evntbut, TRUE )
	
	wxListCtrl_DeleteAllItems( mylist )
	
	wxListCtrl_InsertTextItem(mylist, 0, widget->Name)
	wxListCtrl_SetItem_By_Row_Col(mylist, 0, 1, "Name", 0)
	wxListCtrl_InsertTextItem(mylist, 1, widget->label)
	wxListCtrl_SetItem_By_Row_Col(mylist, 1, 1, "Label", 0)
	wxListCtrl_InsertTextItem(mylist, 2, Str( wxSize_GetWidth( widget->position ) ))
	wxListCtrl_SetItem_By_Row_Col(mylist, 2, 1, "X-Pos", 0)
	wxListCtrl_InsertTextItem(mylist, 3, Str( wxSize_GetHeight( widget->position ) ))
	wxListCtrl_SetItem_By_Row_Col(mylist, 3, 1, "Y-Pos", 0)
	wxListCtrl_InsertTextItem(mylist, 4, Str( wxSize_GetWidth( widget->size ) ))
	wxListCtrl_SetItem_By_Row_Col(mylist, 4, 1, "Width", 0)
	wxListCtrl_InsertTextItem(mylist, 5, Str( wxSize_GetHeight( widget->size ) ))
	wxListCtrl_SetItem_By_Row_Col(mylist, 5, 1, "Height", 0)
	
	Select Case widget->kind
		Case VWX_WIDGET_choice
			wxListCtrl_SetItemText( mylist, 1, "n/a (see below)" )
			For i = 0 To wxChoice_GetCount( widget->self )
				wxListCtrl_InsertTextItem( mylist, i + 6, str_getline( widget->label, i ) )
				wxListCtrl_SetItem_By_Row_Col( mylist, i + 6, 1, "Choice " + Str( i + 1 ), 0 )
			Next
			
		Case VWX_WIDGET_combo
			wxListCtrl_SetItemText( mylist, 1, str_getline( widget->label, 0 ) )
			For i = 0 To wxChoice_GetCount( widget->self ) - 1
				wxListCtrl_InsertTextItem( mylist, i + 6, str_getline( widget->label, i + 1 ) )
				wxListCtrl_SetItem_By_Row_Col( mylist, i + 6, 1, "Choice " + Str( i + 1 ), 0 )
			Next
			
		Case VWX_WIDGET_gauge
			wxListCtrl_InsertTextItem( mylist, 6, Str( wxGauge_GetRange( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 6, 1, "Max Value", 0 )
			wxListCtrl_InsertTextItem( mylist, 7, Str( wxGauge_GetValue( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 7, 1, "Initial Value", 0 )
			
		Case VWX_WIDGET_radio
			wxListCtrl_SetItemText( mylist, 1, str_getline( widget->label, 0 ) )
			For i = 0 To wxRadioBox_GetCount( widget->self )
				wxListCtrl_InsertTextItem( mylist, i + 6, str_getline( widget->label, i + 1 ) )
				wxListCtrl_SetItem_By_Row_Col( mylist, i + 6, 1, "Choice " + Str( i + 1 ), 0 )
			Next
			
		Case VWX_WIDGET_list
			wxListCtrl_SetItemText( mylist, 1, "n/a (see below)" )
			For i = 0 To wxListBox_GetCount( widget->self )
				wxListCtrl_InsertTextItem( mylist, i + 6, str_getline( widget->label, i ) )
				wxListCtrl_SetItem_By_Row_Col( mylist, i + 6, 1, "Choice " + Str( i + 1 ), 0 )
			Next
			
		Case VWX_WIDGET_spinctrl
			wxListCtrl_InsertTextItem( mylist, 6, Str( wxSpinCtrl_GetMax( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 6, 1, "Max Value", 0 )
			wxListCtrl_InsertTextItem( mylist, 7, Str( wxSpinCtrl_GetMin( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 7, 1, "Min Value", 0 )
			wxListCtrl_InsertTextItem( mylist, 8, Str( wxSpinCtrl_GetValue( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 8, 1, "Initial Value", 0 )
			
		Case VWX_WIDGET_togglebutton
			wxListCtrl_InsertTextItem( mylist, 6, Str( wxToggleButton_GetValue( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 6, 1, "State", 0 )
			
		Case VWX_WIDGET_checkbox
			wxListCtrl_InsertTextItem( mylist, 6, Str( wxCheckBox_GetValue( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 6, 1, "State", 0 )
			
		Case VWX_WIDGET_slider
			wxListCtrl_InsertTextItem( mylist, 6, Str( wxSlider_GetMax( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 6, 1, "Max Value", 0 )
			wxListCtrl_InsertTextItem( mylist, 7, Str( wxSlider_GetMin( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 7, 1, "Min Value", 0 )
			wxListCtrl_InsertTextItem( mylist, 8, Str( wxSlider_GetValue( widget->self ) ) )
			wxListCtrl_SetItem_By_Row_Col( mylist, 8, 1, "Initial Value", 0 )
			
		Case VWX_WIDGET_checklistbox
			wxListCtrl_SetItemText( mylist, 1, "n/a (see below)" )
			For i = 0 To wxListBox_GetCount( widget->self )
				wxListCtrl_InsertTextItem( mylist, i + 6, str_getline( widget->label, i ) )
				wxListCtrl_SetItem_By_Row_Col( mylist, i + 6, 1, "Choice " + Str( i + 1 ), 0 )
			Next
			
	End Select
	
End Sub

''
'' 
''
Sub bitmapbutton_helper(ByVal event As wxEvent Ptr, ByVal iListener As Long)
	
	'Motionevent
	If wxEvent_GetEventType(event) = wxEvent_EVT_ENTER_WINDOW() Then
		currentButton = wxEvent_GetEventObject(event)
		wxEvent_Skip( event, TRUE )
	EndIf
	
End Sub

''
'' Creates the toolbuttons
''
Function create_bmpbutton( ByVal id As Integer, _
						   ByVal filename As String, _
						   ByVal parent As Any Ptr, _
						   ByVal x As Integer, _
						   ByVal y As Integer, _
						   ByVal w As Integer, _
						   ByVal h As Integer, _
						   ByVal style As Integer) As wxBitmapButton Ptr

	Dim As wxBitmap Ptr pic
	Dim As _bitmapbutton Ptr bitbutton
	Static As Integer numbuttons = 0

	pic = wxBitmap( )
	wxBitmap_LoadFile( pic, "images/" + filename, wxBITMAP_TYPE_BMP )

	bitbutton = wxBitmapButton( )
	'numbuttons += 1
	
	
	wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb1) )
	ReDim Preserve bitbuttons( numbuttons + 1 )
	
	
	
	'Select Case numbuttons
	'	Case 1
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb1) )
	'	Case 2
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb2) )
	'	Case 3
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb3) )
	'	Case 4
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb4) )
	'	Case 5
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb5) )
	'	Case 6
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb6) )
	'	Case 7
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb7) )
	'	Case 8
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb8) )
	'	Case 9
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb9) )
	'	Case 10
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb10) )
	'	Case 11
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb11) )
	'	Case 12
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb12) )
	'	Case 13
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb13) )
	'	Case 14
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb14) )
	'	Case 15
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb15) )
	'	Case 16
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb16) )
	'	Case 17
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb17) )
	'	Case 18
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb18) )
	'	Case 19
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb19) )
	'	Case 20
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb20) )
	'	Case 21
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb21) )
	'	Case 22
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb22) )
	'	Case 23
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb23) )
	'	Case 24
	'		wxBitmapButton_RegisterVirtual( bitbutton, Cast(Virtual_OnSetBitmap, @bitmapbutton_cb24) )
	'		
	'End Select
	bitbuttons( numbuttons ) = bitbutton
	numbuttons += 1 'this was above
	
	wxBitmapButton_Create( bitbutton, parent, id, pic, wxSize( x, y ), wxSize( w, h ), style, 0, 0 )
	wxEvtHandler_proxy( bitbutton, Cast(EventListener, @bitmapbutton_helper) )
	wxEvtHandler_Connect( bitbutton, wxEvent_EVT_ENTER_WINDOW(), id, -1, 0 )
	
	wxWindow_SetBackgroundColour( bitbutton, wxColour_ctorByParts( 255, 255, 255 ) )
	
	Dim As String toolTipText = Left(filename, Len(filename) - 4)
	If toolTipText <> "box" Then
		wxWindow_SetToolTip(bitbutton, StrPtr(toolTipText))
	EndIf
	
	
	Return bitbutton
	
End Function

''
'' bmpbutton count getter/setter
''
Function bmpbuttoncounter( ByVal kind As Integer ) As Integer
	'the user can change the widgets name so we can't read the number of bmpbuttons out of its name
	'we need an additional counter here to have correct emitted code
	
	Static As Integer bmpbuttoncount = 0
	
	Select Case kind
		Case -1	'deleted button
			bmpbuttoncount -= 1
			Return TRUE
		Case 1	'added button
			bmpbuttoncount += 1
			Return TRUE
		Case 0	'get current state
			Return bmpbuttoncount
		Case Else
			'this should never happen
			Return FALSE
	End Select
	
End Function

''
'' Save dialog
''
Function get_filename( extension As String ) As String
	Static As wxFileDialog Ptr dlg1 = NULL
	
	check_filename( )
	
	If extension = ".bas" Then
		If ( dlg1 = NULL ) Then
			dlg1 = wxFileDialog( wx_frame, "Save code as", 0, _
								app.fileName, "FreeBASIC Files (*.bas)|*.bas|Any file(*)|*.*", _
								wxSAVE Or wxOVERWRITE_PROMPT, _
								wxSize( 0, 0 ) )
	    EndIf
	ElseIf extension = ".xml" Then
		If ( dlg1 = NULL ) Then
			dlg1 = wxFileDialog( wx_frame, "Save project as", 0, _
								Left(app.fileName, Len(app.fileName) - 4 ) & ".xml", "XML Files (*.xml)|*.xml|Any file(*)|*.*", _
								wxSAVE Or wxOVERWRITE_PROMPT, _
								wxSize( 0, 0 ) )
		EndIf
	EndIf
	
	
	'' Cancelled
	If ( wxFileDialog_ShowModal( dlg1 ) <> wxID_OK ) Then
		Return ""
	EndIf
	
	Return wxString_to_fbstring( wxFileDialog_GetPath( dlg1 ) )
	
End Function

''
'' Checks filename for spaces and extension
''
Sub check_filename( )
	
	If app.fileName = "" Then
		For i As Integer = 0 To Len(app.title) - 1
			If app.title[i] <> 32 Then 'not space
				app.fileName &= Chr(app.title[i])
			Else
				app.fileName &= "_"
			EndIf
		Next
	EndIf
	If Right(app.fileName, 4) <> ".bas" Then
		app.fileName &= ".bas"
	EndIf
	
End Sub

''
'' XML chooser
''
Function get_xml( ) As String
	Static As wxFileDialog Ptr dlg1 = NULL
	
	If ( dlg1 = NULL ) Then
		dlg1 = wxFileDialog( wx_frame, "Open project", 0, _
							"", "Any file(*)|*.*|XML|*.xml", _
							wxOPEN Or wxHIDE_READONLY Or wxFILE_MUST_EXIST, _
							wxSize( 0, 0 ) )
	EndIf

	'' Cancelled
	If ( wxFileDialog_ShowModal( dlg1 ) <> wxID_OK ) Then
		Return ""
	EndIf
	
	Return wxString_to_fbstring( wxFileDialog_GetPath( dlg1 ) )
	
End Function

''
'' Picture chooser
''
Function get_picture( ) As String
	Static As wxFileDialog Ptr dlg1 = NULL
	
	If ( dlg1 = NULL ) Then
		dlg1 = wxFileDialog( wx_frame, "Load picture", 0, _
							"", "Any file(*)|*.*|Bitmap|*.bmp|GIF|(*.gif)|JPEG|(*.JPEG)|PNG|(*.PNG)|Icon|(*.ICO)|Cursor|(*.CUR)", _
							wxOPEN Or wxHIDE_READONLY Or wxFILE_MUST_EXIST, _
							wxSize( 0, 0 ) )
	EndIf

	'' Cancelled
	If ( wxFileDialog_ShowModal( dlg1 ) <> wxID_OK ) Then
		Return ""
	EndIf
	
	Return wxString_to_fbstring( wxFileDialog_GetPath( dlg1 ) )
	
End Function

''
'' Picture Type getter
''
Function get_pictureType ( picture As String ) As Integer
	
	Dim As Integer foundAt = InStrRev(picture, ".")
	
	#Ifdef __FB_LINUX__
	If foundAt = 0 Then
		Return wxBITMAP_TYPE_ANY
	EndIf
	#EndIf
	
	Select Case UCase(Mid(picture, foundAt + 1, Len(picture) - foundAt))
		Case "BMP"
			Return wxBITMAP_TYPE_BMP
		Case "GIF"
			Return wxBITMAP_TYPE_GIF
		Case "JPEG", "JPG"
			Return wxBITMAP_TYPE_JPEG
		Case "PNG"
			Return wxBITMAP_TYPE_PNG
		Case "ICO"
			Return wxBITMAP_TYPE_ICO
		Case "CUR"
			Return wxBITMAP_TYPE_CUR
		Case Else
			Return wxBITMAP_TYPE_ANY
	End Select
	
End Function

''
'' returns the type as string
''
Function get_pictureTypeString ( ByVal pictureType As Integer ) As String
	
	Select Case pictureType
		Case wxBITMAP_TYPE_BMP
			Return "wxBITMAP_TYPE_BMP"
		Case wxBITMAP_TYPE_GIF
			Return "wxBITMAP_TYPE_GIF"
		Case wxBITMAP_TYPE_JPEG
			Return "wxBITMAP_TYPE_JPEG"
		Case wxBITMAP_TYPE_PNG
			Return "wxBITMAP_TYPE_PNG"
		Case wxBITMAP_TYPE_ICO
			Return "wxBITMAP_TYPE_ICO"
		Case wxBITMAP_TYPE_CUR
			Return "wxBITMAP_TYPE_CUR"
		Case Else
			Return "wxBITMAP_TYPE_ANY"
	End Select
	
End Function

''
'' Closes the window which calls this sub
''
Sub closewin( ByVal event As wxevent Ptr, ByVal iListener As Long )
	
	wxWindow_Close( wxWindow_GetParent( wxEvent_GetEventObject( event ) ), 0 )
	
End Sub

''
'' returns the string value of a list-item
''
Function get_strfromlist( ByVal list As wxListCtrl Ptr, _
						  ByVal item As Integer ) As String'ZString Ptr
	
	'Return wxString_mb_str( wxListCtrl_GetItemText( list, item ) )
	'Dim As ZString Ptr tmpz
	Dim As String tmp
	tmp = wxString_to_fbstring( wxListCtrl_GetItemText( list, item ) )
	'tmpz = Allocate( Len( tmp ) + 1)
	
	'Dim As Integer i
	'For i = 0 To Len( tmp ) - 1
	'	tmpz[ i ] = tmp[ i ]
	'Next
	
	
	Return tmp
	
End Function

''
'' returns the integer value of a list-item
''
Function get_intfromlist( ByVal list As wxListCtrl Ptr, _
							  ByVal item As Integer ) As Integer
	
	Return Val( wxString_to_fbstring( wxListCtrl_GetItemText( list, item ) ) )
	
End Function

''
'' 
''
Sub widget_redraw( ByVal widget As VWX_Widget Ptr )
	
	'Dim As wxSize Ptr mysize
	'mysize = wxSize( -1, -1 )
	'wxWindow_GetBestSize( widget->self, mysize )
	'If ( wxSize_GetWidth( widget->size ) ) > -1 Then
	'	wxSize_SetWidth( mysize, wxSize_GetWidth( widget->size ) )
	'EndIf
	'If ( wxSize_GetHeight( widget->size ) ) > -1 Then
	'	wxSize_SetHeight( mysize, wxSize_GetHeight( widget->size ) )
	'EndIf
	'wxWindow_Refresh( widget->self, 1, _
	'				  wxRect( 0, 0, wxSize_GetWidth( mysize ), _
	'				  wxSize_GetHeight( mysize ) ) )
	
	wxWindow_Refresh( widget->self, -1, 0 )

End Sub

''
'' 
''
Sub widget_tofront( ByVal widget As VWX_Widget Ptr )
	
	wxWindow_Raise( widget->self )
	
	boxes_tofront( )
	
End Sub

''
'' Deletes a userdefined widget
''
Sub widget_delete( ByVal widget As VWX_Widget Ptr )
	Select Case widget->kind
		Case VWX_WIDGET_bmpbutton
			bmpbuttoncounter( -1 )
			'check if bmpbutton first then delete
			
		Case VWX_WIDGET_stattext, VWX_WIDGET_choice, VWX_WIDGET_combo, VWX_WIDGET_spinctrl, VWX_WIDGET_gauge, _
			VWX_WIDGET_slider, VWX_WIDGET_radio, VWX_WIDGET_calendar, VWX_WIDGET_image, VWX_WIDGET_staticline
			Dim As Any Ptr parent = wxWindow_GetParent( widget->self )
			wxObject_Dtor( widget->self )
			wxObject_Dtor( parent )
			
		Case Else
			wxObject_Dtor( widget->self )
			
	End Select
	
	listDelNode( @widgetlist, Cast(TLISTNODE Ptr, widget) )
	selected = 0
	boxes_hide( )
	wxListCtrl_DeleteAllItems( mylist )
	wxWindow_Enable( mylist, FALSE )
	wxWindow_Enable( updbut, FALSE )
	wxWindow_Enable( delbut, FALSE )
	wxWindow_Enable( evntbut, FALSE )
End Sub

''
'' Delete-Button clicked event
''
Sub delbut_clicked( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	widget_delete( selected )
	
End Sub

''
'' Event-Button clicked event (to create event-code for widgets)
''
Sub evntbut_clicked ( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	Dim As wxListbox Ptr chk
	Dim As wxDialog Ptr tmp
	Dim As wxButton Ptr teh_but
	Dim As Integer i
	
	tmp = wxDialog( )
	wxDialog_Create( tmp, proj, -1, "Select events:", wxSize( 0, 0 ), wxSize( 300, 200 ), wxSYSTEM_MENU Or wxDEFAULT_DIALOG_STYLE Or wxCLOSE_BOX, 0 )
	chk = wxCheckListBox_ctor2( tmp, -1, wxSize( 5, 5 ), wxSize( 320, 200 ), 198, @events( 0 ), 0, 0, 0 )
	
	For i = 0 To 197
		If selected->events( i ) = 1 Then
			wxCheckListBox_Check( chk, i, TRUE )
		EndIf
	Next
	
	teh_but = wxButton( )
	wxButton_Create( teh_but, tmp, -1, "OK", wxSize( 120, 220 ), wxSize( -1, -1 ), 0, 0, 0 )
	wxEvtHandler_proxy( teh_but, Cast(EventListener, @closewin) )
	wxEvtHandler_Connect( teh_but, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
	
	wxWindow_Fit( tmp )
	wxWindow_CenterOnScreen( tmp, wxBOTH )
	wxDialog_ShowModal( tmp )
	
	selected->events( UBound( selected->events ) ) = 0
	
	For i = 0 To 197
		selected->events( i ) = wxCheckListBox_IsChecked( chk, i )
		
		If wxCheckListBox_IsChecked( chk, i ) = 1 Then
			selected->events( UBound( selected->events ) ) = 1
		EndIf
	Next
End Sub

''
'' 
''
Sub redrawgrid( ByVal redraw As Byte = FALSE )
	
	'wxWindow_PrepareDC( projpn, -1 )
	Static bigbmp As wxBitmap Ptr
	
	If gridon = FALSE Then
		wxDC_Clear( wxPaintDC( ) )
		Exit Sub
	EndIf
	
	If redraw Then
		
		Dim As wxSize Ptr temp
		temp = wxSize( -1, -1 )
		wxWindow_GetSize( proj, temp )
		
		Dim As Integer smw, smh
		smw = wxSize_GetWidth( temp )
		smh = wxSize_GetHeight( temp )
		
		Dim As wxImage Ptr big
		big = wxImage_ctorintintbool( smw, smh, TRUE )
		
		Dim As Integer x, y, gx, gy
		gx = gridsize: gy = gridsize
		
		For y = 0 To smh Step gy
			For x = 0 To smw Step gx
					
				wxImage_Paste( big, gridparts( -( ( x / gx ) Mod 10 = 0 ) + -( ( y / gy ) Mod 10 = 0 ) * 2 ), x, y )
					
			Next
		Next
		
		#If Defined(__FB_WIN32__)
			bigbmp = wxBitmap_ctorByImage( big, 24 )
		#ElseIf Defined(__FB_LINUX__)
			bigbmp = wxBitmap_ctorByImage( big, -1 )
		#EndIf
		'Exit Sub
		
	EndIf
	
	'wxDC_DrawBitmap( wxPaintDC( ), bigbmp, 0, 0, FALSE )
	wxDC_DrawBitmap( wxWindowDC_ctor2( projpn ), bigbmp, 0, 0, FALSE )
	
End Sub

''
'' 
''
Sub redrawwidgets( )
	
	Dim As VWX_Widget Ptr widget
	widget = Cast(VWX_Widget Ptr, listGetFirstNode( @widgetlist ))
	While widget <> NULL
		
		'widget_redraw( widget->self )
		widget_redraw( widget )
		
		widget = Cast(VWX_Widget Ptr, listGetNextNode( Cast(TLISTNODE Ptr, widget) ))
		
	Wend
	
End Sub

''
'' 
''
Sub bitmapbutton_cb1(  )
	
	Static As Integer cnt = 0
	Dim As wxBitmapButton Ptr bmpbutton
	
	bmpbutton = wxBitmapButton( )
	If currentButton = NULL Then
		bmpbutton = bitbuttons( cnt )
		cnt += 1
	Else
		bmpbutton = bitbuttons( wxWindow_GetId(currentButton) - tool_notool )
	EndIf
	
	wxBitmapButton_OnSetBitmap( bmpbutton )
	
End Sub

'''
''' Change properties
'''
'Sub properties_changed ( ByVal event As wxEvent Ptr, ByVal iListener As Long )
'	
'	'windowtitle
'	app.title = wxString_to_fbstring(wxTextCtrl_GetValue(app.titleCtrl))
'	wxWindow_SetTitle(proj, StrPtr(app.title))
'	
'	'Filename
'	app.fileName = wxString_to_fbstring(wxTextCtrl_GetValue(app.fileNameCtrl))
'	check_filename( )
'	
'	'Savepath
'	app.savePath = wxString_to_fbstring(wxTextCtrl_GetValue(app.savePathCtrl))
'	
'	'Iconpath
'	app.icon = wxString_to_fbstring(wxTextCtrl_GetValue(app.iconCtrl))
'	'Dim As String temp = wxString_to_fbstring(wxTextCtrl_GetValue(app.iconCtrl))
'	'If temp <> app.icon Then
'	'	app.icon = temp
'	'	Dim As wxIcon Ptr frameIcon = wxIcon( )
'	'	Dim As wxBitmap Ptr iconImage = wxBitmap( )
'	'	wxBitmap_LoadFile( iconImage, StrPtr(app.icon), get_pictureType( app.icon ) )
'	'	wxIcon_CopyFromBitmap( frameIcon, iconImage )
'	'	wxFrame_SetIcon( proj, frameIcon )
'	'EndIf
'	
'	
'	'Relative paths
'	app.relativePathInt = wxCheckBox_GetValue(app.relativePathCtrl)
'	app.relativePath = wxString_to_fbstring(wxTextCtrl_GetValue(app.relativePathTextCtrl))
'	
'	'Additional output
'	app.additionalOutput = wxCheckBox_GetValue(app.additionalOutputCtrl)
'	
'	
'	closewin(event, iListener)
'	
'End Sub

''
'' Search Path callback 1
''
Sub pathSearch ( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	Dim As wxDirDialog Ptr dirChooser
	If app.savePath = "" Then
		dirChooser = wxDirDialog( wxEvent_GetEventObject(event), "Choose your Dir", 0, wxDD_NEW_DIR_BUTTON, wxSize( -1, -1 ), wxSize( -1, -1 ), 0 )
	Else
		dirChooser = wxDirDialog( wxEvent_GetEventObject(event), "Choose your Dir", StrPtr(app.savePath), wxDD_NEW_DIR_BUTTON, wxSize( -1, -1 ), wxSize( -1, -1 ), 0 )
	EndIf
	
	wxDirDialog_ShowModal( dirChooser )
	app.savePath = wxString_to_fbstring(wxDirDialog_GetPath( dirChooser )) & SEPARATOR
	If app.savePath = SEPARATOR Then
		app.savePath = ""
	EndIf
	wxTextCtrl_SetValue(app.savePathCtrl, StrPtr(app.savePath))
	
End Sub

''
'' Search Path callback 2
''
Sub relPathSearch ( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	Dim As wxDirDialog Ptr dirChooser
	If app.relativePath = "" Then
		dirChooser = wxDirDialog( wxEvent_GetEventObject(event), "Choose your Dir", 0, wxDD_NEW_DIR_BUTTON, wxSize( -1, -1 ), wxSize( -1, -1 ), 0 )
	Else
		dirChooser = wxDirDialog( wxEvent_GetEventObject(event), "Choose your Dir", StrPtr(app.relativePath), wxDD_NEW_DIR_BUTTON, wxSize( -1, -1 ), wxSize( -1, -1 ), 0 )
	EndIf
	
	wxDirDialog_ShowModal( dirChooser )
	
	app.relativePath = wxString_to_fbstring(wxDirDialog_GetPath( dirChooser )) & SEPARATOR
	If app.relativePath = SEPARATOR Then
		app.relativePath = ""
	EndIf
	wxTextCtrl_SetValue(app.relativePathTextCtrl, StrPtr(app.relativePath))
	wxCheckBox_SetValue(app.relativePathCtrl, TRUE)
	
End Sub

''
'' Icon search callback
''
Sub iconSearch ( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	app.icon = get_picture()
	wxTextCtrl_SetValue(app.iconCtrl, StrPtr(app.icon))
	
End Sub

''
'' Returns the relative path of a given path
''
Function getRelPath ( path As String ) As String
	
	If app.relativePath <> "" And app.relativePathInt <> FALSE And Left(path, Len(app.relativePath)) = app.relativePath Then
		Return Right(path, Len(path) - Len(app.relativePath))
	Else
		If path[0] = Asc(SEPARATOR) Then
			Return Right(path, Len(path) - 1)
		Else
			Return path
		EndIf
	EndIf
	
End Function

