''

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

Dim Shared As wxMenuBar Ptr menub
Dim Shared As Integer nummenus = 0
Dim Shared As Integer submenucnt = 0
Const initnumitems = 4
Const id_div = 7

#Define VWX_MENU_main 1
#Define VWX_MENU_submenu 2

''
'' create menubar and menus
''
Sub menu_makemenu( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	Dim As wxMenu Ptr submenu = wxMenu( 0, 0 )
	Dim As VWX_Menu Ptr menu
	wxMenuBase_Append( submenu, 0 + ( nummenus * id_div ), "&Normal item", "Insert normal item", 0 )
	wxMenuBase_Append( submenu, 1 + ( nummenus * id_div ), "&Check item", "Insert check item", 0 )
	wxMenuBase_Append( submenu, 2 + ( nummenus * id_div ), "&Radio item", "Insert radio item", 0 )
	wxMenuBase_Append( submenu, 3 + ( nummenus * id_div ), "&Separator", "Insert separator", 0 )
	wxMenuBase_Append( submenu, 4 + ( nummenus * id_div ), "Sub &menu", "Insert sub menu", 0 )
	
	If menub = NULL Then menub = wxMenuBar( )
	
	menu = Cast(VWX_Menu Ptr, listNewNode( @menulist ))
	menu->self = wxMenu( 0, 0 )
	menu->kind = VWX_MENU_main
	menu->position = nummenus - submenucnt
	listNew( @menu->items, VWX_WIDGETLIST_INITNODES, Len( VWX_Menuitem ) )
	wxMenuBase_AppendSeparator( menu->self )
	wxMenuBase_AppendSubMenu( menu->self, nummenus, "&Add item", submenu, "Add an item to this menu" )
	wxMenuBase_Append( menu->self, 5 + ( nummenus * id_div ), "&Remove menu", "Delete the whole menu", 0 )
	wxMenuBase_Append( menu->self, 6 + ( nummenus * id_div ), "&Properties", "View menu properties", 0 )
	
	If *Cast(Integer Ptr, event) <> 0 Then
		menu->Name = "&Menu " + Str( nummenus )
	Else
		menu->Name = *Cast(ZString Ptr, Cast(Integer Ptr, event)[1])
	EndIf
	wxMenuBar_Append( menub, menu->self, menu->Name )
	wxFrame_SetMenuBar( proj, menub )
	
	nummenus += 1
	
End Sub

''
'' add menuitems and delete/change/reorder them
''
Sub menu_change( ByVal event As wxEvent Ptr, ByVal iListener As Long )
	
	Static As VWX_Menu Ptr tempmenu
	Static As VWX_Menuitem Ptr cursel
	Dim As wxMenu Ptr parent_menu
	Dim As VWX_Menuitem Ptr item
	Dim As Integer evntid, i, tmpindex
	Static As Integer index
	Static As Integer radiocnt = 0
	
	Static As wxButton Ptr diagacceptbutton
	Static As wxButton Ptr diagdelbutton
	Static As wxButton Ptr moveupbutton
	Static As wxButton Ptr movedownbutton
	Static As wxChoice Ptr menuchoice
	Static As wxTextCtrl Ptr widgetname
	
	If wxEvent_GetEventType( event ) = wxEvent_EVT_MOVE() Then
		If selected = 0 Then
			updateframelist( )
		EndIf
	EndIf
	
	If wxEvent_GetEventType( event ) <> wxEvent_EVT_COMMAND_MENU_SELECTED() And _
	   wxEvent_GetEventObject( event ) <> diagacceptbutton And _
	   wxEvent_GetEventObject( event ) <> diagdelbutton And _
	   wxEvent_GetEventObject( event ) <> moveupbutton And _
	   wxEvent_GetEventObject( event ) <> movedownbutton And _
	   wxEvent_GetEventType( event ) <> wxEvent_EVT_COMMAND_CHOICE_SELECTED( ) Then _
		event_projframe( event, iListener ): Exit Sub
		'Print "called: ", wxEvent_GetId( event )
	
	Dim As Integer oldchoice
	Dim As VWX_Menuitem Ptr curitem
	If wxEvent_GetEventObject( event ) = diagacceptbutton And diagacceptbutton <> NULL Then
		If cursel <> NULL Then
			''item
			wxMenuItem_SetText( cursel->self, wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) ) )
			Print 1
			If cursel->submenu <> NULL Then
				Print 2
				cursel->submenu->Name = wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) )
			EndIf
			cursel = NULL
		Else
			''menu, have to access the menubar :/
			''(unless it's a submenu :/)
			If tempmenu->kind = VWX_MENU_main Then
				Print 3
				wxMenuBar_SetLabelTop( menub, tempmenu->position, wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) ) )
				tempmenu->Name = wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) )
			ElseIf tempmenu->kind = VWX_MENU_submenu Then
				Print 4
				wxMenuItem_SetText( tempmenu->self, wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) ) )
				wxMenuItem_SetText( Cast(VWX_Menu Ptr, tempmenu->parent)->self, wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) ) )
				tempmenu->Name = wxString_to_fbstring( wxTextCtrl_GetValue( widgetname ) )
				cursel = NULL
			EndIf
		EndIf
		
		''regen choices
		oldchoice = wxChoice_GetSelection( menuchoice )
		wxChoice_Clear( menuchoice )
		wxChoice_Append( menuchoice, tempmenu->Name )
		curitem = Cast(VWX_MenuItem Ptr, listGetFirstNode( Cast(TLIST Ptr, @tempmenu->items) ))
		While curitem <> NULL
			wxChoice_Append( menuchoice, "     " + wxString_to_fbstring( wxMenuItem_GetText( curitem->self ) ) )
			curitem = Cast(VWX_MenuItem Ptr, listGetNextNode( Cast(TLISTNODE Ptr, curitem )))
		Wend
		wxChoice_SetSelection( menuchoice, oldchoice )
		
		'cursel = listGetFirstNode( @tempmenu->items )
		'For i = 2 To oldchoice
		'	cursel = listGetNextNode( cursel )
		'Next
		'If wxChoice_GetSelection( menuchoice ) = 0 then
		'	cursel = NULL
		'	wxWindow_Enable( diagdelbutton, FALSE )
		'	wxTextCtrl_SetValue( widgetname, tempmenu->name )
		'Else
		'	wxWindow_Enable( diagdelbutton, TRUE )
		'	wxTextCtrl_SetValue( widgetname, wxString_to_fbstring( wxMenuItem_GetText( cursel->self ) ) )
		'EndIf
		
		Exit Sub
    ElseIf wxEvent_GetEventObject( event ) = diagdelbutton And diagdelbutton <> NULL Then
		If cursel->submenu <> NULL Then
			'' it's a submenu, i already have somthing that kills 'em, and they're special ;)
			Dim As VWX_Menu Ptr lastitem
			lastitem = Cast( VWX_Menu Ptr, listGetPrevNode( Cast( TLISTNODE Ptr, tempmenu ) ) )
			
			delsubmenu( cursel )
			''now we could somehow determine which index this menu is and only regen the ones
			''we need to, maybe, why not give it a shot.... er.....
			''...
			''hmh, didn't have a var for that, crap
			''oh wait, i can make index static, yay, no searching crap anymore!!
			lastitem = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, lastitem ) ) )
			tmpindex = index
			While lastitem <> NULL
				regenmenu( lastitem, tmpindex + 1 )
				tmpindex += 1
			Wend
			listDelNode( @tempmenu->items, Cast( TLISTNODE Ptr, cursel ) )
		Else
			listDelNode( @tempmenu->items, Cast( TLISTNODE Ptr, cursel ) )
		EndIf
		regenmenu( tempmenu, index )
		
		''regen choices
		'Dim As Integer oldchoice_
		oldchoice = wxChoice_GetSelection( menuchoice )
		wxChoice_Clear( menuchoice )
		wxChoice_Append( menuchoice, tempmenu->Name )
		curitem = Cast( VWX_Menuitem Ptr, listGetFirstNode( @tempmenu->items ) )
		While curitem <> NULL
			wxChoice_Append( menuchoice, "     " + wxString_to_fbstring( wxMenuItem_GetText( curitem->self ) ) )
			curitem = Cast( VWX_Menuitem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, curitem ) ) )
		Wend
		wxChoice_SetSelection( menuchoice, oldchoice + ( oldchoice > 0 ) )
		
		cursel = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
		For i = 2 To oldchoice + ( oldchoice > 0 )
			cursel = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, cursel ) ) )
		Next
		If wxChoice_GetSelection( menuchoice ) = 0 Then
			cursel = NULL
			wxWindow_Enable( diagdelbutton, FALSE )
			wxTextCtrl_SetValue( widgetname, tempmenu->Name )
		Else
			wxWindow_Enable( diagdelbutton, TRUE )
			wxTextCtrl_SetValue( widgetname, wxString_to_fbstring( wxMenuItem_GetText( cursel->self ) ) )
		EndIf
		
		Exit Sub
    ElseIf wxEvent_GetEventType( event ) = wxEvent_EVT_COMMAND_CHOICE_SELECTED( ) Then
		cursel = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
		For i = 2 To wxChoice_GetSelection( menuchoice )
			cursel = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, cursel ) ) )
		Next
		If wxChoice_GetSelection( menuchoice ) = 0 Then
			cursel = NULL
			wxWindow_Enable( diagdelbutton, FALSE )
			wxWindow_Enable( moveupbutton, FALSE )
			wxWindow_Enable( movedownbutton, FALSE )
			wxTextCtrl_SetValue( widgetname, tempmenu->Name )
		Else
			wxWindow_Enable( diagdelbutton, TRUE )
			If wxChoice_GetSelection( menuchoice ) > 1 Then 
				wxWindow_Enable( moveupbutton, TRUE )
			Else
				wxWindow_Enable( moveupbutton, FALSE )
			EndIf
			If wxChoice_GetSelection( menuchoice ) < wxChoice_GetCount( menuchoice ) - 1 Then
				wxWindow_Enable( movedownbutton, TRUE )
			Else
				wxWindow_Enable( movedownbutton, FALSE )
			EndIf
			wxTextCtrl_SetValue( widgetname, wxString_to_fbstring( wxMenuItem_GetText( cursel->self ) ) )
		EndIf
		Exit Sub
    ElseIf wxEvent_GetEventObject( event ) = moveupbutton And moveupbutton <> NULL Then
		If wxChoice_GetSelection( menuchoice ) > 1 Then ''can't move up the menu itself or the first item
			Dim As VWX_Menuitem Ptr tmpprev
			tmpprev = Cast( VWX_Menuitem Ptr, listGetPrevNode( Cast( TLISTNODE Ptr, cursel ) ) )
			cursel->ll_prev = tmpprev->ll_prev
			tmpprev->ll_next = cursel->ll_next
			cursel->ll_next = Cast( VWX_Widget Ptr, tmpprev )
			tmpprev->ll_prev = Cast( VWX_Widget Ptr, cursel )
			If cursel->ll_prev Then
				cursel->ll_prev->ll_next = Cast( VWX_Widget Ptr, cursel ) 'fix circular problems
			Else
				tempmenu->items.head = Cast( TLISTNODE Ptr, cursel ) 'it's the new first node
			EndIf
			If tmpprev->ll_next Then
				tmpprev->ll_next->ll_prev = Cast( VWX_Widget Ptr, tmpprev ) 'fix circular problems
			Else
				tempmenu->items.tail = Cast( TLISTNODE Ptr, tmpprev ) 'it's the new last node
			EndIf
			
			regenmenu( tempmenu, index )
			
			''regen choices
			'Dim As Integer oldchoice__
			oldchoice = wxChoice_GetSelection( menuchoice )
			wxChoice_Clear( menuchoice )
			'Dim As VWX_Menuitem Ptr curitem__
			wxChoice_Append( menuchoice, tempmenu->Name )
			curitem = Cast( VWX_Menuitem Ptr, listGetFirstNode( @tempmenu->items ) )
			While curitem <> NULL
				wxChoice_Append( menuchoice, "     " + wxString_to_fbstring( wxMenuItem_GetText( curitem->self ) ) )
				curitem = Cast( VWX_Menuitem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, curitem ) ) )
			Wend
			wxChoice_SetSelection( menuchoice, oldchoice - 1 )
			
			cursel = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
			For i = 2 To oldchoice - 1
				cursel = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, cursel ) ) )
			Next
			wxWindow_Enable( diagdelbutton, TRUE )
			If wxChoice_GetSelection( menuchoice ) > 1 Then 
				wxWindow_Enable( moveupbutton, TRUE )
			Else
				wxWindow_Enable( moveupbutton, FALSE )
			EndIf
			If wxChoice_GetSelection( menuchoice ) < wxChoice_GetCount( menuchoice ) - 1 Then
				wxWindow_Enable( movedownbutton, TRUE )
			Else
				wxWindow_Enable( movedownbutton, FALSE )
			EndIf
			
			Exit Sub
		EndIf
    ElseIf wxEvent_GetEventObject( event ) = movedownbutton And movedownbutton <> NULL Then
		If wxChoice_GetSelection( menuchoice ) < wxChoice_GetCount( menuchoice ) - 1 And _
		   wxChoice_GetSelection( menuchoice ) > 0 Then ''can't move down the menu itself or the last item
			Dim As VWX_Menuitem Ptr tmpnext
			tmpnext = Cast( VWX_Menuitem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, cursel ) ) )
			cursel->ll_next = tmpnext->ll_next
			tmpnext->ll_prev = cursel->ll_prev
			cursel->ll_prev = Cast( VWX_Widget Ptr, tmpnext )
			tmpnext->ll_next = Cast( VWX_Widget Ptr, cursel )
			If cursel->ll_next Then
				cursel->ll_next->ll_prev = Cast( VWX_Widget Ptr, cursel ) 'fix circular problems
			Else
				tempmenu->items.tail = Cast( TLISTNODE Ptr, cursel ) 'it's the new first node
			EndIf
			If tmpnext->ll_prev Then
				tmpnext->ll_prev->ll_next = Cast( VWX_Widget Ptr, tmpnext ) 'fix circular problems
			Else
				tempmenu->items.head = Cast( TLISTNODE Ptr, tmpnext ) 'it's the new last node
			EndIf
			
			regenmenu( tempmenu, index )
			
			''regen choices
			
			oldchoice = wxChoice_GetSelection( menuchoice )
			wxChoice_Clear( menuchoice )
			'dim as VWX_Menuitem ptr curitem__
			wxChoice_Append( menuchoice, tempmenu->Name )
			curitem = Cast( VWX_Menuitem Ptr, listGetFirstNode( @tempmenu->items ) )
			While curitem <> NULL
				wxChoice_Append( menuchoice, "     " + wxString_to_fbstring( wxMenuItem_GetText( curitem->self ) ) )
				curitem = Cast( VWX_Menuitem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, curitem ) ) )
			Wend
			wxChoice_SetSelection( menuchoice, oldchoice + 1 )
			
			cursel = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
			For i = 2 To oldchoice + 1
				cursel = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, cursel ) ) )
			Next
			wxWindow_Enable( diagdelbutton, TRUE )
			If wxChoice_GetSelection( menuchoice ) > 1 Then 
				wxWindow_Enable( moveupbutton, TRUE )
			Else
				wxWindow_Enable( moveupbutton, FALSE )
			EndIf
			If wxChoice_GetSelection( menuchoice ) < wxChoice_GetCount( menuchoice ) - 1 Then
				wxWindow_Enable( movedownbutton, TRUE )
			Else
				wxWindow_Enable( movedownbutton, FALSE )
			EndIf
			
			Exit Sub
		EndIf
	EndIf
	
	If Int( wxEvent_GetId( event ) / id_div ) >  nummenus - 1 Then Exit Sub
	index = Int( wxEvent_GetId( event ) / id_div )
	
	tempmenu = Cast( VWX_Menu Ptr, listGetFirstNode( @menulist ) )
	For i = 1 To Int( wxEvent_GetId( event ) / id_div )
		
		tempmenu = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, tempmenu ) ) )
		
	Next
	parent_menu = tempmenu->self
	
	evntid = wxEvent_GetId( event ) Mod id_div
	'Print evntid, wxEvent_GetId( event ), id_div
	
	If evntid = 0 Then 'add normal item
		
		item = Cast( VWX_MenuItem Ptr, listNewNode( @tempmenu->items ) )
		If *Cast(Integer Ptr, event) <> 0 Then
			item->self = wxMenuBase_Insert2( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems, -1, _
										"item" + Str( wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems ), 0, 0 )
		Else
			item->self = wxMenuBase_Insert2( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems, -1, _
										*Cast(ZString Ptr, Cast(Integer Ptr, event)[1]), 0, 0 )
		EndIf
	ElseIf evntid = 1 Then 'add checkitem
		
		item = Cast( VWX_MenuItem Ptr, listNewNode( @tempmenu->items ) )
		If *Cast(Integer Ptr, event) <> 0 Then
			item->self = wxMenuBase_InsertCheckItem( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems, -1, _
										"checkitem" + Str( wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems ), 0 )
		Else
			item->self = wxMenuBase_InsertCheckItem( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems, -1, _
										*Cast(ZString Ptr, Cast(Integer Ptr, event)[1]), 0 )
		EndIf
		wxMenuItem_Check( item->self, TRUE )
		
	ElseIf evntid = 2 Then 'add radioitem
		
		''argh, insertradioitem doesn't work, do this the hard way
		''(or the easy way ;) )
		
		Dim As wxMenuItem Ptr tempitem
		item = Cast( VWX_MenuItem Ptr, listNewNode( @tempmenu->items ) )
		If *Cast(Integer Ptr, event) <> 0 Then
			item->self = wxMenuBase_AppendRadioItem( parent_menu, -1, _
										"radioitem" + Str( wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems ), 0 )
		Else
			item->self = wxMenuBase_AppendRadioItem( parent_menu, -1, _
										*Cast(ZString Ptr, Cast(Integer Ptr, event)[1]), 0 )
		EndIf
		'For i = 0 To initnumitems - 1
		'	tempitem = wxMenuBase_FindItemByPosition( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems - 1 )
		'	wxMenuBase_Remove( parent_menu, wxMenuItem_GetId( tempitem ) )
		'	wxMenuBase_AppendItem( parent_menu, tempitem )
		'Next
		regenmenu( tempmenu, index )
		
		radiocnt+=1
		
	ElseIf evntid = 3 Then 'add separator
		
		item = Cast( VWX_MenuItem Ptr, listNewNode( @tempmenu->items ) )
		item->self = wxMenuBase_InsertSeparator( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems )
		
	ElseIf evntid = 4 Then 'add submenu
		
		item = Cast( VWX_MenuItem Ptr, listNewNode( @tempmenu->items ) )
		tempmenu = Cast( VWX_Menu Ptr, listNewNode( @menulist ) )
		tempmenu->self = wxMenu( 0, 0 )
		tempmenu->kind = VWX_MENU_submenu
		tempmenu->position = -1
		listNew( @tempmenu->items, VWX_WIDGETLIST_INITNODES, Len( VWX_Menuitem ) )
		Dim As wxMenu Ptr submenu = wxMenu( 0, 0 )
		wxMenuBase_Append( submenu, 0 + ( nummenus * id_div ), "&Normal item", "Insert normal item", 0 )
		wxMenuBase_Append( submenu, 1 + ( nummenus * id_div ), "&Check item", "Insert check item", 0 )
		wxMenuBase_Append( submenu, 2 + ( nummenus * id_div ), "&Radio item", "Insert radio item", 0 )
		wxMenuBase_Append( submenu, 3 + ( nummenus * id_div ), "&Separator", "Insert separator", 0 )
		wxMenuBase_Append( submenu, 4 + ( nummenus * id_div ), "Sub &menu", "Insert sub menu", 0 )
		wxMenuBase_AppendSeparator( tempmenu->self )
		wxMenuBase_AppendSubMenu( tempmenu->self, nummenus, "&Add item", submenu, "Add an item to this menu" )
		wxMenuBase_Append( tempmenu->self, 5 + ( nummenus * id_div ), "&Remove menu", "Delete the whole menu", 0 )
		wxMenuBase_Append( tempmenu->self, 6 + ( nummenus * id_div ), "&Properties", "View menu properties", 0 )
		
		If *Cast(Integer Ptr, event) <> 0 Then
			tempmenu->Name = "submenuitem" + Str( wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems )
		Else
			tempmenu->Name = *Cast(ZString Ptr, Cast(Integer Ptr, event)[1])
		EndIf
		item->self = wxMenuBase_InsertSubMenu( parent_menu, wxMenuBase_GetMenuItemCount( parent_menu ) - initnumitems, -1, _
								tempmenu->Name, _
								tempmenu->self, 0 )
		
		item->submenu = tempmenu
		tempmenu->parent = item
		nummenus += 1
		submenucnt += 1
		
	ElseIf evntid = 5 Then 'remove menu
		
		If tempmenu->kind = VWX_MENU_main Then
			
			Dim As VWX_Menu Ptr tempmenu2
			tempmenu2 = Cast( VWX_Menu Ptr, listGetPrevNode( Cast( TLISTNODE Ptr, tempmenu ) ) )
			wxMenuBar_Remove( menub, tempmenu->position )
			
			Dim As VWX_Menuitem Ptr item2
			item2 = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
			While item2 <> NULL
				If item2->submenu <> NULL Then
					delsubmenu( item2 )
				EndIf
				item2 = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, item2 ) ) )
			Wend
			
			listDel( @tempmenu->items )
			listDelNode( @menulist, Cast( TLISTNODE Ptr, tempmenu ) )
			If tempmenu2 <> NULL Then
				tempmenu2 = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, tempmenu2 ) ) )
			Else
				tempmenu2 = Cast( VWX_Menu Ptr, listGetFirstNode( @menulist ) )
			EndIf
			While tempmenu2 <> NULL
				If tempmenu2->kind = VWX_MENU_main Then
					tempmenu2->position -= 1
				EndIf
				regenmenu( tempmenu2, index )
				tempmenu2 = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, tempmenu2 ) ) )
				index += 1
			Wend
			
			nummenus -= 1
			
		ElseIf tempmenu->kind = VWX_MENU_submenu Then
			
			Dim As Any Ptr temp_parent_menu
			Dim As VWX_Menu Ptr menuholder ''holds the menus we need to change... :/
			Dim As VWX_Menuitem Ptr temp_item
			temp_item = tempmenu->parent
			menuholder = Cast( VWX_Menu Ptr, listGetPrevNode( Cast( TLISTNODE Ptr, tempmenu ) ) )
			temp_parent_menu = wxMenuItem_GetMenu( temp_item->self )
			delsubmenu( tempmenu->parent )
			'' now we have to search for the thing's parent, ew...
			'' if it's not in the list the whole program'll probably crash
			'' so noone mess with the list kthx
			Dim As VWX_Menu Ptr searchmenu
			searchmenu = Cast( VWX_Menu Ptr, listGetFirstNode( @menulist ) )
			While searchmenu->self <> temp_parent_menu
				searchmenu = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, searchmenu ) ) )
			Wend
			'' k good, i'm going to assume we now have a valid menu ptr
			'' which is why the prog will crash if it didn't find one :P
			wxMenuBase_Remove2( temp_parent_menu, temp_item->self )
			listDelNode( @searchmenu->items, Cast( TLISTNODE Ptr, temp_item ) )
			'' okies, now we change the ids of all the other menus so
			'' they don't blow up in our faces.
			If menuholder <> NULL Then
				menuholder = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, menuholder ) ) )
			Else
				menuholder = Cast( VWX_Menu Ptr, listGetFirstNode( @menulist ) )
			EndIf
			While menuholder <> NULL
				regenmenu( menuholder, index )
				menuholder = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, menuholder ) ) )
				index += 1
			Wend
			
		EndIf
		
	ElseIf evntid = 6 Then 'open propertywindow for menu
		
		'' teh uber-l337 properties dialog box0rz
		Dim As wxDialog Ptr myDiag
		myDiag = wxDialog( )
		wxDialog_Create( myDiag, proj, -1, "Menu Properties", _
						 wxSize( -1, -1 ), wxSize( 195, 205 ), _
						 wxDEFAULT_FRAME_STYLE Or wxCLOSE_BOX Xor wxRESIZE_BORDER Xor wxMAXIMIZE_BOX, _
						 0 )
		wxWindow_SetAutoLayout( myDiag, TRUE )
		
		menuchoice = wxChoice( )
		ReDim textchoices( 1 ) As ZString Ptr
		ReDim textchoicestmp( 1 ) As String
		'Dim As VWX_Menuitem Ptr curitem
		Dim As Integer numitems
		numitems = 1
		textchoices( 0 ) = StrPtr( tempmenu->Name )
		curitem = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
		While curitem <> NULL
			textchoicestmp( numitems ) = "     " + wxString_to_fbstring( wxMenuItem_GetText( curitem->self ) )
			textchoices( numitems ) = StrPtr( textchoicestmp( numitems ) )
			numitems += 1
			ReDim Preserve textchoices( numitems )
			ReDim Preserve textchoicestmp( numitems )
			curitem = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, curitem ) ) )
		Wend
		wxChoice_Create( menuchoice, myDiag, -1, wxSize( 5, 5 ), wxSize( -1, -1 ), numitems, @textchoices( 0 ), 0, 0, 0 )
		wxChoice_SetSelection( menuchoice, 0 )
		wxEvtHandler_proxy( menuchoice, Cast(EventListener, @menu_change) )
		wxEvtHandler_Connect( menuchoice, wxEvent_EVT_COMMAND_CHOICE_SELECTED( ), -1, -1, 0 )
		
		Dim As wxStaticText Ptr diagtext
		diagtext = wxStaticText( )
		wxStaticText_Create( diagtext, myDiag, -1, "Label:", wxSize( 5, 40 ), wxSize( -1, -1 ), 0, 0 )
		
		widgetname = wxTextCtrl( )
		wxTextCtrl_Create( widgetname, myDiag, -1, tempmenu->Name, wxSize( 5, 55 ), wxSize( 150, -1 ), 0, 0, 0 )
		
		diagacceptbutton = wxButton( )
		wxButton_Create( diagacceptbutton, myDiag, -1, "Change", wxSize( 5, 80 ), wxSize( -1, -1 ), 0, 0, 0 )
		wxEvtHandler_proxy( diagacceptbutton, Cast(EventListener, @menu_change) ) ''connect it to itself
										'' this way, i can make use of static vars for the diag
										'' instead of having even more globals ;)
		wxEvtHandler_Connect( diagacceptbutton, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
		
		diagdelbutton = wxButton( )
		wxButton_Create( diagdelbutton, myDiag, -1, "Delete item", wxSize( 105, 80 ), wxSize( -1, -1 ), 0, 0, 0 )
		wxEvtHandler_proxy( diagdelbutton, Cast(EventListener, @menu_change) ) ''connect it to itself
										'' this way, i can make use of static vars for the diag
										'' instead of having even more globals ;)
		wxEvtHandler_Connect( diagdelbutton, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
		wxWindow_Enable( diagdelbutton, FALSE ) '' no deleting the menu from here!!!!!!1
		
		moveupbutton = wxButton( )
		wxButton_Create( moveupbutton, myDiag, -1, "Move up", wxSize( 5, 110 ), wxSize( -1, -1 ), 0, 0, 0 )
		wxEvtHandler_proxy( moveupbutton, Cast(EventListener, @menu_change) )
		wxEvtHandler_Connect( moveupbutton, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
		wxWindow_Enable( moveupbutton, FALSE )
		
		movedownbutton = wxButton( )
		wxButton_Create( movedownbutton, myDiag, -1, "Move down", wxSize( 105, 110 ), wxSize( -1, -1 ), 0, 0, 0 )
		wxEvtHandler_proxy( movedownbutton, Cast(EventListener, @menu_change) )
		wxEvtHandler_Connect( movedownbutton, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
		wxWindow_Enable( movedownbutton, FALSE )
		
		Dim As wxButton Ptr diagclosebutton
		diagclosebutton = wxButton( )
		wxButton_Create( diagclosebutton, myDiag, -1, "Close", wxSize( 55, 140 ), wxSize( -1, -1 ), 0, 0, 0 )
		wxEvtHandler_proxy( diagclosebutton, Cast(EventListener, @closewin) )
		wxEvtHandler_Connect( diagclosebutton, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
		
		wxDialog_ShowModal( myDiag )
		
	EndIf
	
End Sub

''
'' 
''
Sub regenmenu( ByVal menu As VWX_Menu Ptr, ByVal index As Integer )
	
	Dim As Integer i
	
	For i = 0 To wxMenuBase_GetMenuItemCount( menu->self ) - 1
		wxMenuBase_Remove( menu->self, wxMenuItem_GetId( wxMenuBase_FindItemByPosition( menu->self, 0 ) ) )
	Next
	
	Dim As VWX_Menuitem Ptr item
	item = Cast( VWX_MenuItem Ptr, listGetFirstNode( @menu->items ) )
	
	While item <> NULL
		wxMenuBase_AppendItem( menu->self, item->self )
		item = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, item ) ) )
	Wend
	
	Dim As wxMenu Ptr submenu = wxMenu( 0, 0 )
	wxMenuBase_Append( submenu, 0 + ( index * id_div ), "&Normal item", "Insert normal item", 0 )
	wxMenuBase_Append( submenu, 1 + ( index * id_div ), "&Check item", "Insert check item", 0 )
	wxMenuBase_Append( submenu, 2 + ( index * id_div ), "&Radio item", "Insert radio item", 0 )
	wxMenuBase_Append( submenu, 3 + ( index * id_div ), "&Separator", "Insert separator", 0 )
	wxMenuBase_Append( submenu, 4 + ( index * id_div ), "Sub &menu", "Insert sub menu", 0 )
	wxMenuBase_AppendSeparator( menu->self )
	wxMenuBase_AppendSubMenu( menu->self, index, "&Add item", submenu, "Add an item to this menu" )
	wxMenuBase_Append( menu->self, 5 + ( index * id_div ), "&Remove menu", "Delete the whole menu", 0 )
	wxMenuBase_Append( menu->self, 6 + ( index * id_div ), "&Properties", "View menu properties", 0 )
	
	wxMenuBase_UpdateUI( menu->self, NULL )
	
End Sub

''
'' 
''
Sub delsubmenu( ByVal submenu As VWX_Menuitem Ptr )
	
	Dim As VWX_Menu Ptr tempmenu
	Dim As VWX_MenuItem Ptr tempitem
	Dim As Integer i
	tempmenu = submenu->submenu
	
	tempitem = Cast( VWX_MenuItem Ptr, listGetFirstNode( @tempmenu->items ) )
	While tempitem <> NULL
		If tempitem->submenu <> NULL Then delsubmenu( tempitem )  '' recursive is fun
		wxMenuBase_Remove2( tempmenu->self, tempitem->self )
		tempitem = Cast( VWX_MenuItem Ptr, listGetNextNode( Cast( TLISTNODE Ptr, tempitem ) ) )
	Wend
	
	For i = 0 To wxMenuBase_GetMenuItemCount( tempmenu->self ) - 1
		wxMenuBase_Remove( tempmenu->self, wxMenuItem_GetId( wxMenuBase_FindItemByPosition( tempmenu->self, 0 ) ) )
	Next
	
	wxMenuBase_UpdateUI( tempmenu->self, NULL )
	
	listDelNode( @menulist, Cast( TLISTNODE Ptr, tempmenu ) )
	nummenus -= 1
	submenucnt -= 1
	
End Sub

''
'' 
''
Function menunode( ByVal menu As wxMenu Ptr ) As VWX_Menu Ptr
	
	Dim As VWX_Menu Ptr searchmenu
	searchmenu = Cast( VWX_Menu Ptr, listGetFirstNode( @menulist ) )
	While searchmenu->self <> menu
		searchmenu = Cast( VWX_Menu Ptr, listGetNextNode( Cast( TLISTNODE Ptr, searchmenu ) ) )
		If searchmenu = NULL Then Return NULL
	Wend
	
	Function = searchmenu
	
End Function

