''
'' Eventhandler for all designer widgets (menu & buttons)
''

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


''
'' Handles all events
''
Sub event_mainframe(ByVal event As wxEvent Ptr, ByVal iListener As Long)
	
	'The close button for the designer
	If wxEvent_GetEventType(event) = wxEvent_EVT_CLOSE_WINDOW() Then
		wxApp_OnExit( wx_app )'wxWindow_Close(wx_frame, 0)
		End
	EndIf
	
	'The eventhandler for the menu
	If wxEvent_GetEventType(event) = wxEvent_EVT_COMMAND_MENU_SELECTED() Then
		
		Select Case wxEvent_GetId(event)
			Case menu_save
				If app.savePath = "" Then
					Dim As String pathAndName
					pathAndName = get_filename( ".bas" )
					If ( pathAndName = "" ) Then
						Exit Sub
					EndIf
					app.savePath = Left(pathAndName, InStrRev(pathAndName, SEPARATOR))
					app.fileName = Right(pathAndName, Len(pathAndName) - Len(app.savePath))
				Else
					check_filename( )
				EndIf
				
				emitcode( app.savePath & app.fileName )
				wxMsgBox(wx_frame, "Code emitted!", "Done.", wxOK, wxSize(150, 50))
			
			Case menu_export
				Static As String pathAndName
				If app.savePath = "" Then
					pathAndName = get_filename( ".xml" )
					If ( pathAndName = "" ) Then
						Exit Sub
					EndIf
					app.savePath = Left(pathAndName, InStrRev(pathAndName, SEPARATOR))
					app.fileName = Right(pathAndName, Len(pathAndName) - Len(app.savePath))
					app.fileName = Mid(pathAndName, Len(app.savePath) + 1, Len(pathAndName) - Len(app.savePath) - 4) & ".bas"
					pathAndName = Right(pathAndName, Len(pathAndName) - Len(app.savePath))
				Else
					check_filename( )
				EndIf
				
				If pathAndName = "" Then
					pathAndName = Left(app.fileName, Len(app.fileName) - 4) & ".xml"
				EndIf
				
				xmlexport( app.savePath & pathAndName )
				wxMsgBox(wx_frame, "Form exported!", "Done.", wxOK, wxSize(150, 50))
			
			Case menu_import
				If Cast(VWX_Widget Ptr, listGetFirstNode( @widgetlist )) <> 0 Then
					wxMsgBox(wx_frame, "Delete all widgets first!", "Panel not empty.", wxOK, wxSize(150, 50))
					Exit Sub
				EndIf
				Dim As String xmlFile
				xmlFile = get_xml( )
				If xmlFile <> "" Then
					xmlimport( xmlFile )
				EndIf
			
			Case menu_exit
				wxWindow_Close(wx_frame, 0)
			
			Case menu_grid1x1
				gridsize = 1
				If wxMenuBase_IsChecked( gridmenu, menu_gridoff ) Then
					gridon = TRUE
				EndIf
				'gridon = FALSE
				'redrawgrid( )
				redrawgrid( TRUE ) 
				wxWindow_RefreshRect( projpn, NULL )
			
			Case menu_grid5x5
				gridsize = 5
				If wxMenuBase_IsChecked( gridmenu, menu_gridoff ) Then
					gridon = TRUE
				EndIf
				redrawgrid( TRUE )			
				wxWindow_RefreshRect( projpn, NULL )
			
			Case menu_grid10x10
				gridsize = 10
				If wxMenuBase_IsChecked( gridmenu, menu_gridoff ) Then
					gridon = TRUE
				EndIf
				redrawgrid( TRUE )		   
				wxWindow_RefreshRect( projpn, NULL )
			
			Case menu_gridoff
				gridon = wxMenuBase_IsChecked( gridmenu, menu_gridoff )
				redrawgrid( TRUE )
				redrawwidgets( )
				wxWindow_RefreshRect( projpn, NULL )
				'redrawwidgets( )
				
			Case menu_create
				menu_makemenu( event, iListener )
				
			Case menu_toolbar
				wxMenuBase_Enable( menu2, menu_addtotoolbar, IIf( wxMenuBase_IsEnabled( menu2, menu_addtotoolbar ), FALSE, TRUE ) )
				wxMenuBase_Enable( menu2, menu_septoolbar, IIf( wxMenuBase_IsEnabled( menu2, menu_septoolbar ), FALSE, TRUE ) )
				make_toolbar( proj )
				
			Case menu_addtotoolbar
				add_to_toolbar( proj )
				
			Case menu_septoolbar
				add_separator( proj )
				
			Case propertyDialog
				  Dim As wxDialog Ptr myDiag
				  myDiag = wxDialog( )
				  wxDialog_Create( myDiag, proj, -1, "Properties", wxSize( -1, -1 ), wxSize( 277, 342 ), _
									wxDEFAULT_FRAME_STYLE Or wxCLOSE_BOX Xor wxRESIZE_BORDER Xor wxMAXIMIZE_BOX, 0 )
				  wxWindow_SetAutoLayout( myDiag, TRUE )
					
					Dim As wxStaticText Ptr statAppName, statFileName, statSavePath, statIcon
					Dim As wxTextCtrl Ptr titleCtrl, fileNameCtrl, additionalOutputCtrl
					Dim As wxButton Ptr propertyDialogOK, searchPath0, searchIcon, relPathBttn
					Dim As wxStaticLine Ptr staticline0, staticline1, staticline2
					
					statAppName = wxStaticText( )
					statFileName = wxStaticText( )
					statSavePath = wxStaticText( )
					statIcon = wxStaticText( )
					wxStaticText_Create( statAppName, myDiag, -1, "Windowtitle:", wxSize( 10, 10 ), wxSize( 95, 15 ), 0, 0 )
					wxStaticText_Create( statFileName, myDiag, -1, "Filename:", wxSize( 160, 10 ), wxSize( 70, 15 ), 0, 0 )
					wxStaticText_Create( statSavePath, myDiag, -1, "Savepath:", wxSize( 10, 80 ), wxSize( 70, 15 ), 0, 0 )
					wxStaticText_Create( statIcon, myDiag, -1, "Icon:", wxSize( 160, 80 ), wxSize( 60, 15 ), 0, 0 )
					
					propertyDialogOK = wxButton( )
					wxButton_Create( propertyDialogOK, myDiag, -1, "OK", wxSize( 100, 275 ), wxSize( 80, 25 ), 0, 0, 0 )
					wxEvtHandler_proxy( propertyDialogOK, Cast(EventListener, @closewin) )
					wxEvtHandler_Connect( propertyDialogOK, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
					
					searchPath0 = wxButton( )
					wxButton_Create( searchPath0, myDiag, -1, "Search Path", wxSize( 20, 135 ), wxSize( 80, 25 ), 0, 0, 0 )
					wxEvtHandler_proxy( searchPath0, Cast(EventListener, @pathSearch) )
					wxEvtHandler_Connect( searchPath0, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
					
					searchIcon = wxButton( )
					wxButton_Create( searchIcon, myDiag, -1, "Search Icon", wxSize( 170, 135 ), wxSize( 80, 25 ), 0, 0, 0 )
					wxEvtHandler_proxy( searchIcon, Cast(EventListener, @iconSearch) )
					wxEvtHandler_Connect( searchIcon, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
					
					
					app.relativePathCtrl = wxCheckBox( )
					additionalOutputCtrl = wxCheckBox( )
					wxCheckBox_Create( app.relativePathCtrl, myDiag, -1, "Relative paths:", wxSize( 10, 180 ), wxSize( 90, 15 ), 0, 0, 0 )
					wxWindow_SetToolTip( app.relativePathCtrl, "All given paths in widgets properties will be relative to the given relative path" )
					wxCheckBox_SetValue( app.relativePathCtrl, app.relativePathInt )
					#If Defined(__FB_LINUX__)
						wxCheckBox_Create( additionalOutputCtrl, myDiag, -1, "Get Shellscript", wxSize( 10, 180 + 50 ), wxSize( 130, 15 ), 0, 0, 0 )
						wxWindow_SetToolTip( additionalOutputCtrl, "Creates a shellscript to include wx-c libs" )
					#ElseIf Defined(__FB_WIN32__)
						wxCheckBox_Create( additionalOutputCtrl, myDiag, -1, "Get Manifest", wxSize( 10, 180 + 50 ), wxSize( 130, 15 ), 0, 0, 0 )
						wxWindow_SetToolTip( additionalOutputCtrl, "Creates a resource with manifest" )
					#EndIf
					wxCheckBox_SetValue( additionalOutputCtrl, app.additionalOutput )
					
					app.relativePathTextCtrl = wxTextCtrl( )
					wxTextCtrl_Create( app.relativePathTextCtrl, myDiag, -1, app.relativePath, wxSize( 105, 178 ), wxSize( 120, 21 ), 0, 0, 0 )
					relPathBttn = wxButton( )
					wxButton_Create( relPathBttn, myDiag, -1, "...", wxSize( 235, 175 ), wxSize( 25, 25 ), 0, 0, 0 )
					wxEvtHandler_proxy( relPathBttn, Cast(EventListener, @relPathSearch) )
					wxEvtHandler_Connect( relPathBttn, wxEvent_EVT_COMMAND_BUTTON_CLICKED( ), -1, -1, 0 )
					
					titleCtrl = wxTextCtrl( )
					wxTextCtrl_Create( titleCtrl, myDiag, -1, app.title, wxSize( 10, 35 ), wxSize( -1, -1 ), 0, 0, 0 )
					fileNameCtrl = wxTextCtrl( )
					wxTextCtrl_Create( fileNameCtrl, myDiag, -1, app.fileName, wxSize( 160, 35 ), wxSize( -1, -1 ), 0, 0, 0 )
					app.savePathCtrl = wxTextCtrl( )
					wxTextCtrl_Create( app.savePathCtrl, myDiag, -1, app.savePath, wxSize( 10, 105 ), wxSize( -1, -1 ), 0, 0, 0 )
					app.iconCtrl = wxTextCtrl( )
					wxTextCtrl_Create( app.iconCtrl, myDiag, -1, app.icon, wxSize( 160, 105 ), wxSize( -1, -1 ), 0, 0, 0 )
					
					staticline0 = wxStaticLine( )
					staticline1 = wxStaticLine( )
					staticline2 = wxStaticLine( )
					wxStaticLine_Create( staticline0, myDiag, -1, wxSize( 10, 70 ), wxSize( 250, 1 ), 0, 0 )
					wxStaticLine_Create( staticline1, myDiag, -1, wxSize( 10, 210 ), wxSize( 250, 1 ), 0, 0 )
					wxStaticLine_Create( staticline2, myDiag, -1, wxSize( 10, 260 ), wxSize( 250, 1 ), 0, 0 )
					
					wxDialog_ShowModal( myDiag )
					
					
					'Save the changes
					
					'windowtitle
					app.title = wxString_to_fbstring( wxTextCtrl_GetValue( titleCtrl ) )
					wxWindow_SetTitle( proj, StrPtr(app.title) )
					
					'Filename
					app.fileName = wxString_to_fbstring( wxTextCtrl_GetValue( fileNameCtrl ) )
					check_filename( )
					
					'Savepath
					app.savePath = wxString_to_fbstring( wxTextCtrl_GetValue( app.savePathCtrl ) )
					
					'Icon
					'app.icon = wxString_to_fbstring( wxTextCtrl_GetValue( app.iconCtrl ) )
					Dim As String temp = wxString_to_fbstring( wxTextCtrl_GetValue( app.iconCtrl ) )
					If temp <> "" 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( additionalOutputCtrl )
					
					updateframelist( )
					
					
		End Select
	EndIf
	
	'The button-eventhandler
	If wxEvent_GetEventType(event) = wxEvent_EVT_COMMAND_BUTTON_CLICKED() Then
		Dim As String tool
		Select Case wxEvent_GetId(event)
			Case tool_notool
				curtool = tool_notool
				tool = "No tool selected"
			Case tool_stattext
				curtool = tool_stattext
				tool = "StatText"
			Case tool_button
				curtool = tool_button
				tool = "Button"
			Case tool_togglebutton
				curtool = tool_togglebutton
				tool = "ToggleButton"
			Case tool_bmpbutton
				curtool = tool_bmpbutton
				tool = "BitmapButton"
			Case tool_image
				curtool = tool_image
				tool = "Image"
			Case tool_checkbox
				curtool = tool_checkbox
				tool = "CheckBox"
			Case tool_choice
				curtool = tool_choice
				tool = "Choice"
			Case tool_combo
				curtool = tool_combo
				tool = "Combo"
			Case tool_textctrl
				curtool = tool_textctrl
				tool = "TextCtrl"
			Case tool_spinctrl
				curtool = tool_spinctrl
				tool = "SpinCtrl"
			Case tool_richtext
				curtool = tool_richtext
				tool = "RichText"
			Case tool_gauge
				curtool = tool_gauge
				tool = "Gauge"
			Case tool_radio
				curtool = tool_radio
				tool = "Radio"
			Case tool_list
				curtool = tool_list
				tool = "List"
			Case tool_staticbox
				curtool = tool_staticbox
				tool = "StaticBox"
			Case tool_slider
				curtool = tool_slider
				tool = "Slider"
			Case tool_staticline
				curtool = tool_staticline
				tool = "StaticLine"
			Case tool_spinbutton
				curtool = tool_spinbutton
				tool = "SpinButton"
			Case tool_calendar
				curtool = tool_calendar
				tool = "Calendar"
			Case tool_checklistbox
				curtool = tool_checklistbox
				tool = "CheckListBox"
		End Select
		
		wxTextCtrl_SetValue(toolText, StrPtr(tool))
		
    EndIf
	
End Sub

''
'' Eventhandler for property list
''
Sub listevent(ByVal event As wxEvent Ptr, ByVal iListener As Long)
	Static As String temp
	
	'Rightclick on listitem
	If wxEvent_GetEventType( event ) = wxEvent_EVT_COMMAND_LIST_ITEM_RIGHT_CLICK( ) Then
		'save last item if new item is empty
		temp = wxString_to_fbstring( wxListCtrl_GetItemText( wxEvent_GetEventObject( event ),  wxListEvent_GetIndex( event ) ) )
		wxListCtrl_EditLabel( wxEvent_GetEventObject( event ), wxListEvent_GetIndex( event ) )
	EndIf
	
	'after editing a listitem
	If wxEvent_GetEventType( event ) = wxEvent_EVT_COMMAND_LIST_END_LABEL_EDIT( ) Then
		'check if empty
		If wxString_to_fbstring( wxListEvent_GetText( event ) ) = "" Then
			wxListCtrl_SetItemText( wxEvent_GetEventObject( event ), wxListEvent_GetIndex( event ), StrPtr( temp ) )
		Else
			wxListCtrl_SetItemText( wxEvent_GetEventObject( event ), wxListEvent_GetIndex( event ), wxString_to_zstring( wxListEvent_GetText( event ) ) )
		EndIf
		'call update function
		propevent( 0, 0 )
	EndIf
	
End Sub
