#include <ButtonConstants.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <Array.au3>
#include <EditConstants.au3>
#include <file.au3>

Global $fontsize=9.5
Global $fontwidh=500
Global $button[6]
Global $id[6]
Global $location[6]
Global $save
Global $name
Global $new
Global $aktuell
Global $progname[6]
Global $readpath
; Tray einstellungen
Opt("TrayMenuMode",1)
TraySetState (2)

_maingui()
Func _maingui()
For $i=0 to 5
$button[$i]=IniRead(@ScriptDir&"\data.ini","progs",$i+1,"(leer)")
$location[$i]=IniRead(@ScriptDir&"\data.ini","location",$i+1,"0")
$progname[$i]=IniRead(@ScriptDir&"\data.ini","dataname",$i+1,"0")
Next

;Buttons erstellen
GUICreate ("Programmauswahl",230,160)
GUICtrlCreateLabel("Choose Program to Start:",10,10)
GUICtrlSetFont(-1,-1,500,4)
$id[0]=GUICtrlCreateButton($button[0],10,30,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$id[1]=GUICtrlCreateButton($button[1],120,30,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$id[2]=GUICtrlCreateButton($button[2],10,70,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$id[3]=GUICtrlCreateButton($button[3],120,70,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$id[4]=GUICtrlCreateButton($button[4],10,110,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$id[5]=GUICtrlCreateButton($button[5],120,110,100,30)
GUICtrlSetFont(-1,$fontsize,$fontwidh)
$delete=GUICtrlCreateButton("Delete Buttons",10,143,70,15)
GUICtrlSetFont(-1,7)
GUICtrlCreateLabel("(c) Blacky911",155,143)
GUICtrlSetFont(-1,$fontsize-0.5,$fontwidh)

GUISetState(@SW_SHOW) ;gui anzeigen

;abfrage der Buttons
While 1
	$msg = GUIGetMsg()
	Select
		Case $msg = $GUI_EVENT_CLOSE
			ExitLoop
		Case $msg = $id[0]
			IF GUICtrlRead($id[0])="(leer)" Then
				GUIDelete()
				_buttonname(0)
			Else
				ShellExecute($progname[0],"",$location[0])
			EndIf
		Case $msg = $id[1]
			IF GUICtrlRead($id[1])="(leer)" Then
				GUIDelete()
				_buttonname(1)
			Else
				ShellExecute($progname[1],"",$location[1])
			EndIf
		Case $msg = $id[2]
			IF GUICtrlRead($id[2])="(leer)" Then
				GUIDelete()
				_buttonname(2)
			Else
				ShellExecute($progname[2],"",$location[2])
			EndIf
		Case $msg = $id[3]
			IF GUICtrlRead($id[3])="(leer)" Then
				GUIDelete()
				_buttonname(3)
			Else
				ShellExecute($progname[3],"",$location[3])
			EndIf
		Case $msg = $id[4]
			IF GUICtrlRead($id[4])="(leer)" Then
				GUIDelete()
				_buttonname(4)
			Else
				ShellExecute($progname[4],"",$location[4])
			EndIf
		Case $msg = $id[5]
			IF GUICtrlRead($id[5])="(leer)" Then
				GUIDelete()
				_buttonname(5)
			Else
				ShellExecute($progname[5],"",$location[5])
			EndIf
		Case $msg = $delete
			$ask=MsgBox(4,"Delete Button","Möchtest du wirklich die Belegung der Kompletten Buttons löschen?")
			If $ask = 6 Then
				FileDelete(@Scriptdir & "\data.ini")
				GUIDelete()
				_maingui()
			EndIf
	EndSelect
WEnd
EndFunc

Func _buttonname($aktuell)
	;Pfad auswählen der Datei
	$var=FileOpenDialog("Bitte Programm zum ausführen auswählen:","C:\","Ausführbares (*.exe)",1)
	;$var = StringReplace($var, "|", @CRLF) ;nur für mehrfachauswahl nutzbar
	Dim $szDrive, $szDir, $szFName, $szExt
	$readpath = _PathSplit($var, $szDrive, $szDir, $szFName, $szExt)
	IniWrite(@ScriptDir&"\data.ini","location",$aktuell+1,$readpath[1]&$readpath[2])
	IniWrite(@ScriptDir&"\data.ini","dataname",$aktuell+1,$readpath[3]&$readpath[4])
	
	;Gui für den Buttonnamen erstellen
	GUICreate ("Buttonname",170,90)
	GUICtrlCreateLabel("Bitte den Buttonname angeben:",10,10)
	GUICtrlSetFont(-1,-1,500,4)
	$name=GUICtrlCreateEdit("Buttonname",10,30,150,20,$WS_TABSTOP)
	$save=GUICtrlCreateButton("Benutzen",60,55)
	
	GUISetState(@SW_SHOW) ;Gui Anzeigen
	
	;Abfrage der Buttons und eintragen des namens in die data.ini
	While 1
		$msg = GUIGetMsg()
			Select
				Case $msg = -3
					ExitLoop
				Case $msg = $save
					$new=GUICtrlRead($name)
					Iniwrite(@ScriptDir&"\data.ini","progs",$aktuell+1,$new)
					ExitLoop
			EndSelect
	WEnd
	GUIDelete()
	
	;erneutes ausführen der maingui()
	_maingui()
EndFunc