#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#include <GuiListView.au3>
#include <SendMessage.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <EditConstants.au3>
#include <FontConstants.au3>
#include <GUIConstantsEx.au3>
#include <HeaderConstants.au3>
#include <ListViewConstants.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
;================================================================================
;Arbeitsplaner
;================================================================================
Global $Auswahl[2]; Array zum speichern der letzten Listviewauswahl
Global $Year = @YEAR
Global $Zeilen = 0
Global $aLV[1]
Global $hInput, $sTitleFile, $sFile, $hFont, $Input1, $SavePath
Global $IniFileName = @ScriptDir & "\data\data.ini"
Global $Praefix = IniRead($IniFileName, "Infos", "Dateipraefix", @MDAY & @MON & @YEAR & " - ")

Opt("GUIOnEventMode", 1)

Dim $aMonth[12][2] = [[01, "Januar"],[02, "Februar"],[03, "März"],[04, "April"],[05, "Mai"],[06, "Juni"], _
		[07, "Juli"],[08, "August"],[09, "September"],[10, "Oktober"],[11, "November"],[12, "Dezember"]]
$iIndex = _ArraySearch($aMonth, @MON)
$LabelMonth = $aMonth[$iIndex][1]

If Not FileExists(@ScriptDir & "\data") Then DirCreate(@ScriptDir & "\data")
If Not FileExists($IniFileName) Then
	IniWrite($IniFileName, "Infos", "Vormonat", "0")
	IniWrite($IniFileName, "Infos", "Speicherpfad", "")
	IniWrite($IniFileName, "Infos", "Dateipraefix", @MDAY & @MON & @YEAR & " - ")
	IniWrite($IniFileName, "Infos", "Soll", "8.00")
	IniWrite($IniFileName, "Infos", "Pause", "0:45")
EndIf

Global $SavePath = IniRead($IniFileName, "Infos", "Speicherpfad", "")

AdlibRegister("_HasFocus", 10)

#region ### START Koda GUI section ###
$GUI = GUICreate("Arbeitsplaner", 845, 468, 271, 141)
GUISetBkColor(0xFFFFFF)
GUISetOnEvent(-3, "_Exit")
GUISetFont(10, 400, 0, "Arial")
$Datei = GUICtrlCreateMenu("&Datei")
$Neu = GUICtrlCreateMenuItem("&Neu", $Datei)
GUICtrlSetOnEvent(-1, "_Neu")
$Oeffnen = GUICtrlCreateMenuItem("Öffnen", $Datei)
GUICtrlSetOnEvent(-1, "_LoadLV")
$Speichern = GUICtrlCreateMenuItem("&Speichern", $Datei)
GUICtrlSetOnEvent(-1, "_Save")
$Speichernu = GUICtrlCreateMenuItem("S&peichern unter", $Datei)
GUICtrlSetOnEvent(-1, "_SaveLV")
$Drucken = GUICtrlCreateMenuItem("&Drucken", $Datei)
GUICtrlSetOnEvent(-1, "_Print")
$Beenden = GUICtrlCreateMenuItem("Beenden", $Datei)
GUICtrlSetOnEvent(-1, "_Exit")
$Optionen = GUICtrlCreateMenu("&Optionen")
$SubOptionen = GUICtrlCreateMenuItem("&Optionen", $Optionen)
GUICtrlSetOnEvent(-1, "_OptChange")
$Help = GUICtrlCreateMenu("?")
$SubAbout = GUICtrlCreateMenuItem("&About", $Help)
GUICtrlSetOnEvent(-1, "_About")
$Grid = GUICtrlCreateListView("Datum|kommt|geht|Ist|Konto +/-|Bemerkung", 8, 8, 577, 430, $LVS_NOSORTHEADER, BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT))
$hHeader = _GUICtrlListView_GetHeader(GUICtrlGetHandle($Grid))
$hGrid = GUICtrlGetHandle($Grid)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 70)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 85)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 85)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 3, 75)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 4, 75)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 5, 165)
$AZKonto = GUICtrlCreateGroup("", 608, 320, 217, 97)
$Label1 = GUICtrlCreateLabel("AZ-Konto Vormonat:", 614, 336, 122, 20)
$Label2 = GUICtrlCreateLabel("Saldenveränderung:", 614, 356, 117, 20)
$Label3 = GUICtrlCreateLabel("Neuer Saldo AZ-Konto:", 614, 392, 137, 20)
$Vormonat = GUICtrlCreateLabel(IniRead($IniFileName, "Infos", "Vormonat", "0"), 758, 336, 35, 20)
$Veraenderung = GUICtrlCreateLabel("0", 758, 356, 35, 20)
$NeuSaldo = GUICtrlCreateLabel("0", 758, 392, 35, 20)
$Label7 = GUICtrlCreateLabel("h", 805, 336, 11, 20)
$Label8 = GUICtrlCreateLabel("h", 805, 356, 11, 20)
$Label9 = GUICtrlCreateLabel("h", 805, 392, 11, 20)
$Label10 = GUICtrlCreateLabel("______________", 755, 376, 60, 11)
GUICtrlSetFont(-1, 5, 400, 0, "Arial")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group1 = GUICtrlCreateGroup("", 608, 248, 113, 65)
$Label11 = GUICtrlCreateLabel("Soll:", 616, 264, 30, 20)
$Label12 = GUICtrlCreateLabel("Pause:", 616, 288, 45, 20)
$Soll = GUICtrlCreateLabel(IniRead($IniFileName, "Infos", "Soll", "8.00"), 672, 264, 29, 20)
$Pause = GUICtrlCreateLabel(IniRead($IniFileName, "Infos", "Pause", "0:45"), 672, 288, 29, 20)
$Label6 = GUICtrlCreateLabel("h", 704, 288, 11, 20)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$MonthCal1 = GUICtrlCreateMonthCal(@YEAR & "/" & @MON & "/" & @MDAY, 628, 16, 180, 164)
$Label4 = GUICtrlCreateLabel("Zeitauswertung " & $LabelMonth & " " & @YEAR, 608, 224, 195, 23)
GUICtrlSetFont(-1, 13, 400, 0, "Arial")
$Dummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "_Tab")
GUISetState(@SW_SHOW)
Dim $GUI_AccelTable[6][2] = [["^n", $Neu],["^o", $Oeffnen],["^s", $Speichern],["^p", $Drucken],["^a", $SubAbout],["{TAB}", $Dummy]]
GUISetAccelerators($GUI_AccelTable)
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUICtrlSetState($Speichern, $GUI_DISABLE)
#endregion ### START Koda GUI section ###

Global $hInputEx = ListViewEdit($GUI); Input für Listviewedit
GUICtrlSetData($Soll, IniRead($IniFileName, "Infos", "Soll", "8.00"), "8.00")
GUICtrlSetData($Pause, IniRead($IniFileName, "Infos", "Pause", "0:45"), "0:45")

#region ### START Koda GUI section ### Form=
$Options = GUICreate("Optionen", 387, 401, 407, 182)
GUISetBkColor(0xFFFFFF)
GUISetOnEvent(-3, "_Abbrechen")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "_Click")
GUISetFont(10, 400, 0, "Arial")
$Group3 = GUICtrlCreateGroup("Optionen zum Speichern", 32, 48, 289, 137)
$Label14 = GUICtrlCreateLabel("Standard Speicherpfad:", 40, 72, 139, 20)
$Label15 = GUICtrlCreateLabel("Standard Datei-Präfix:", 40, 128, 131, 20)
$Input1 = GUICtrlCreateInput("", 40, 96, 265, 24)
$Input2 = GUICtrlCreateInput("", 40, 152, 265, 24)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group4 = GUICtrlCreateGroup("Soll/Pause - Veränderung", 32, 200, 289, 137)
$Label16 = GUICtrlCreateLabel("Soll:", 40, 224, 30, 20)
$Input3 = GUICtrlCreateInput("", 40, 248, 121, 24)
$Label17 = GUICtrlCreateLabel("Pause:", 40, 280, 45, 20)
$Input4 = GUICtrlCreateInput("", 40, 304, 121, 24)
$Label18 = GUICtrlCreateLabel("Aktuell: " & GUICtrlRead($Soll), 176, 251, 125, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
$Label7 = GUICtrlCreateLabel("Aktuell: " & GUICtrlRead($Pause) & " h", 176, 307, 125, 22)
GUICtrlSetFont(-1, 12, 400, 0, "Arial")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button1 = GUICtrlCreateButton("OK", 80, 360, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_OkBtn")
$Button2 = GUICtrlCreateButton("Abbrechen", 184, 360, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_Abbrechen")
$Button3 = GUICtrlCreateButton("Übernehmen", 288, 360, 89, 25, $WS_GROUP)
GUICtrlSetOnEvent(-1, "_ConfirmBtn")
$Label13 = GUICtrlCreateLabel("Optionen:", 32, 16, 89, 27)
GUICtrlSetFont(-1, 15, 400, 0, "Arial")
GUISetState(@SW_HIDE)
WinSetOnTop($Options, "", 1)
#endregion ### END Koda GUI section ###

Global $aDates = _GetMonthDayInfo(@MON, @YEAR)
_AddDates($hGrid, $aDates)

GUICtrlSetData($Input1, IniRead($IniFileName, "Infos", "Speicherpfad", ""), "")
GUICtrlSetData($Input2, IniRead($IniFileName, "Infos", "Dateipraefix", ""), "")

_LoadStart(IniRead($IniFileName, "Infos", "LastStartUp", ""))

While 1
	Sleep(50)
WEnd

Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
	Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
	$hWndListView = $Grid
	If Not IsHWnd($Grid) Then $hWndListView = GUICtrlGetHandle($Grid)
	$tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
	$hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
	$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
	$iCode = DllStructGetData($tNMHDR, "Code")
	Switch $hWndFrom
		Case $hHeader
			Switch $iCode
				Case -320, -321 ; -320  = $HDN_ITEMCHANGINGW  -321 = $HDN_ITEMCHANGEDW
					Return 1
			EndSwitch
		Case $hWndListView
			Switch $iCode
				Case $NM_CLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
					$tInfo = DllStructCreate($tagNMLISTVIEW, $ilParam); Listviewdaten auslesen
					$Index = DllStructGetData($tInfo, "Item"); Item
					$SubItem = DllStructGetData($tInfo, "SubItem"); Column
					$Auswahl[0] = $Index; Index des Listviews
					$Auswahl[1] = $SubItem;Subitem des Listviews
					Switch $SubItem
						Case 1, 2, 5
							_MoveInput($hGrid, $hInputEx, $Index, $SubItem)
							_WinAPI_SetFocus($hInputEx)
							$Text = _GUICtrlListView_GetItemText($hGrid, $Index, $SubItem);
							_WinAPI_SetWindowText($hInputEx, $Text)
							Return 0
						Case Else
							Return 1
					EndSwitch
				Case $LVN_ENDSCROLL
					_WinAPI_ShowWindow($hInputEx, @SW_HIDE)
					Switch $Auswahl[1]
						Case 1, 2, 5
							_MoveInput($hGrid, $hInputEx, $Auswahl[0], $Auswahl[1]);
							_WinAPI_SetFocus($hInputEx)
					EndSwitch
			EndSwitch
		Case Else
	EndSwitch
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
	#forceref $hWnd, $iMsg
	Local $hWndFrom, $iIDFrom, $iCode, $hWndEdit
	$hWndFrom = $ilParam
	$iIDFrom = _WinAPI_LoWord($iwParam)
	$iCode = _WinAPI_HiWord($iwParam)
	Switch $hWndFrom
		Case $hInputEx
			Switch $iCode
				Case $EN_UPDATE ; Sent when an edit control is about to redraw itself
					$Text = _WinAPI_GetWindowText($hInputEx)
					$aSplitText = StringSplit($Text, ":")
					If Not @error Then
						If StringLen($aSplitText[1]) = 1 Then
							$Text = "0" & $aSplitText[1] & ":" & $aSplitText[2]
						EndIf
					EndIf
					_GUICtrlListView_SetItemText($hGrid, $Auswahl[0], $Text, $Auswahl[1]); Text in das Listview schreiben. $Auswahl[0] = Index $Auswahl[1] = Subitem
			EndSwitch
			_Calculate($Auswahl[0])
			$Saldo = _AddSaldo($hGrid, 4)
			GUICtrlSetData($Veraenderung, $Saldo)
			_NewSaldo()
	EndSwitch
	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _Exit()
	_WinAPI_DeleteObject($hFont)
	AdlibUnRegister("_HasFocus")
	_SaveLV()
	IniWrite($IniFileName, "Infos", "LastStartup", $sFile)
	IniWrite($IniFileName, "Infos", "Actual", _AddSaldo($hGrid, 4))
	If _GetItemTextCount() = $Zeilen Then
		IniWrite($IniFileName, "Infos", "Vormonat", IniRead($IniFileName, "Infos", "Actual", ""))
	EndIf
	Exit
EndFunc   ;==>_Exit

Func _GetMonthDayInfo($iMonth, $iYear)
	If Not StringRegExp($iYear, "\d{2}|\d{4}") Then Return SetError(1, 0, 0)
	If Not StringRegExp($iMonth, "\d{1,2}") Then Return SetError(1, 0, 0)
	If StringLen($iYear) = 2 Then $iYear = "20" & $iYear
	Local $iCnt = _DateDaysInMonth($iYear, $iMonth)
	If @error Then Return SetError(2, 0, 0)
	Local Const $aDayOfWeek[8] = ["", "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"]
	Local $aRet[$iCnt + 1][2] = [[$iCnt]], $iTmp
	For $i = 1 To $iCnt
		$aRet[$i][0] = StringFormat("%.2d.%.2d.%.4d", $i, $iMonth, $iYear)
		$iTmp = _DateToDayOfWeek($iYear, $iMonth, $i)
		If Not @error Then $aRet[$i][1] = $aDayOfWeek[$iTmp]
	Next
	Return $aRet
EndFunc   ;==>_GetMonthDayInfo

Func _Input_Create($hWnd, $iX, $iY, $iWidth = -1, $iHeight = -1, $iStyle = 0, $iExStyle = 0)
	$iStyle = BitOR($iStyle, $WS_CHILD, $WS_VISIBLE)
	Return _WinAPI_CreateWindowEx($iExStyle, "EDIT", "", $iStyle, $iX, $iY, $iWidth, $iHeight, $hWnd)
EndFunc   ;==>_Input_Create

Func ListViewEdit($sHwnd)
	Local $hRet
	If Not IsHWnd($sHwnd) Then $sHwnd = GUICtrlGetHandle($sHwnd)
	Local $iStyle = _WinAPI_GetWindowLong($sHwnd, $GWL_STYLE)
	If BitAND($iStyle, $WS_CLIPCHILDREN) <> $WS_CLIPCHILDREN Then
		_WinAPI_SetWindowLong($sHwnd, $GWL_STYLE, BitOR($iStyle, $WS_CLIPCHILDREN))
	EndIf
	$hRet = _Input_Create($sHwnd, 1, 1, 1, 1)
	$hFont = _WinAPI_CreateFont(16.5, 0, 0, 0, 400, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
	_SendMessage($hRet, $WM_SETFONT, $hFont)
	Return $hRet
EndFunc   ;==>ListViewEdit

Func _MoveInput($hListView, $hProgress, $sItemIndex, $sSubItemIndex)
	Local $aRect
	If $sSubItemIndex = 0 Then
		$aRect = _GUICtrlListView_GetItemRect($hListView, $sItemIndex, 2)
	Else
		$aRect = _GUICtrlListView_GetSubItemRect($hListView, $sItemIndex, $sSubItemIndex)
	EndIf
	If $aRect[1] < 10 Then
		_WinAPI_ShowWindow($hProgress, @SW_HIDE)
	ElseIf $aRect[1] >= 10 Then
		_WinAPI_ShowWindow($hProgress, @SW_SHOW)
	EndIf
	_WinAPI_MoveWindow($hProgress, $aRect[0] + 17, $aRect[1] + 13, $aRect[2] - $aRect[0] - 12, $aRect[3] - $aRect[1] - 3, True)
EndFunc   ;==>_MoveInput

Func _Calculate($iLine)
	$SubItem1 = _GUICtrlListView_GetItemText($hGrid, $iLine, 1)
	$SubItem2 = _GUICtrlListView_GetItemText($hGrid, $iLine, 2)
	If Not $SubItem1 = "" And Not $SubItem2 = "" And StringLen($SubItem1) = 5 And StringLen($SubItem2) = 5 Then
		$ZwischTime = _TimeDiff($Auswahl[0], _GUICtrlListView_GetItemText($hGrid, $iLine, 1), _GUICtrlListView_GetItemText($hGrid, $iLine, 2))
		$iTime = _TimeDiff($Auswahl[0], GUICtrlRead($Pause), $ZwischTime)
		_GUICtrlListView_AddSubItem($hGrid, $iLine, $iTime, 3)
		$iSplitTime = StringSplit($iTime, ":")
		$sKonto = ($iSplitTime[1] + ($iSplitTime[2] / 60)) - GUICtrlRead($Soll)
		_GUICtrlListView_AddSubItem($hGrid, $iLine, $sKonto, 4)
	Else
		_GUICtrlListView_AddSubItem($hGrid, $iLine, "", 3)
		_GUICtrlListView_AddSubItem($hGrid, $iLine, "", 4)
	EndIf
EndFunc   ;==>_Calculate

Func _TimeDiff($iLine, $sStartTime, $sEndTime)
	$iStartEndDiff = _DateDiff("n", @YEAR & "/01/01 " & $sStartTime, @YEAR & "/01/01 " & $sEndTime)
	$iDiffInDez = $iStartEndDiff / 60
	If StringLen($iDiffInDez) < 3 Then
		Return $iDiffInDez & ":00"
	Else
		$aDiffSplit = StringSplit($iDiffInDez, ".")
		$sDezimal = "0" & "." & $aDiffSplit[2]
		$iMinutes = Round($sDezimal * 60)
		If $iMinutes < 10 Then $iMinutes = "0" & $iMinutes
		Return $aDiffSplit[1] & ":" & $iMinutes
	EndIf
EndFunc   ;==>_TimeDiff

Func _Tab()
	Switch $Auswahl[1]
		Case '1'
			_MoveInput($hGrid, $hInputEx, $Auswahl[0], 2)
			$Auswahl[1] = 2
		Case '2'
			_MoveInput($hGrid, $hInputEx, $Auswahl[0], 5)
			$Auswahl[1] = 5
		Case '5'
			$Auswahl[0] += 1
			_MoveInput($hGrid, $hInputEx, $Auswahl[0], 1)
			$Auswahl[1] = 1
	EndSwitch
	_WinAPI_SetWindowText($hInputEx, "")
EndFunc   ;==>_Tab

Func _AddSaldo($hWnd, $iSubItem)
	Local $iValue
	Local $iZeilen = _GUICtrlListView_GetItemCount($hWnd)
	For $i = 0 To $iZeilen
		$iValue += _GUICtrlListView_GetItemText($hGrid, $i, $iSubItem)
	Next
	Return $iValue
EndFunc   ;==>_AddSaldo

Func _NewSaldo()
	GUICtrlSetData($NeuSaldo, (GUICtrlRead($Vormonat) + GUICtrlRead($Veraenderung)))
EndFunc   ;==>_NewSaldo

Func _SaveLV()
	Local $hFileOut, $aItem
	Global $sFile = FileSaveDialog("Datei speichern", $SavePath, "Monatstabellen (*.mtb)", 2 + 16, $Praefix)
	If Not $sFile = "" Then
		If Not FileExists($sFile) Then $sFile &= ".mtb"
		$hFileOut = FileOpen($sFile, 2)
		For $j = 0 To _GUICtrlListView_GetItemCount($hGrid) - 1
			$aItem = _GUICtrlListView_GetItemTextString($hGrid, $j)
			FileWriteLine($hFileOut, $aItem)
		Next
		FileClose($hFileOut)
		GUICtrlSetState($Speichern, $GUI_ENABLE)
	EndIf
EndFunc   ;==>_SaveLV

Func _LoadLV()
	Local $aItems
	Global $sFile = FileOpenDialog("Datei öffnen", @ScriptDir, "Monatstabellen (*.mtb)", 1 + 2)
	If FileExists($sFile) Then
		_FileReadToArray($sFile, $aItems)
		_WinAPI_SetWindowText($hInputEx, "")
		If IsArray($aItems) Then
			For $j = 1 To $aItems[0]
				$aSplit = StringSplit($aItems[$j], "|")
				For $a = 0 To 5
					_GUICtrlListView_AddSubItem($hGrid, $j - 1, $aSplit[$a + 1], $a)
				Next
			Next
		EndIf
		$aPathSplit = StringSplit($sFile, "\")
		Global $sTitleFile = StringTrimRight($aPathSplit[UBound($aPathSplit) - 1], 4)
		WinSetTitle($GUI, "", "Arbeitsplaner - " & $sTitleFile)
		$Saldo = _AddSaldo($hGrid, 4)
		GUICtrlSetData($Veraenderung, $Saldo)
		_NewSaldo()
		GUICtrlSetState($Speichern, $GUI_ENABLE)
	EndIf
EndFunc   ;==>_LoadLV

Func _LoadStart($sFile)
	Local $aItems
	If FileExists($sFile) Then
		_FileReadToArray($sFile, $aItems)
		_WinAPI_SetWindowText($hInputEx, "")
		If IsArray($aItems) Then
			For $j = 1 To $aItems[0]
				$aSplit = StringSplit($aItems[$j], "|")
				For $a = 0 To 5
					_GUICtrlListView_AddSubItem($hGrid, $j - 1, $aSplit[$a + 1], $a)
				Next
			Next
		EndIf
		$aPathSplit = StringSplit($sFile, "\")
		Global $sTitleFile = StringTrimRight($aPathSplit[UBound($aPathSplit) - 1], 4)
		WinSetTitle($GUI, "", "Arbeitsplaner - " & $sTitleFile)
		$Saldo = _AddSaldo($hGrid, 4)
		GUICtrlSetData($Veraenderung, $Saldo)
		_NewSaldo()
		GUICtrlSetState($Speichern, $GUI_ENABLE)
	EndIf
EndFunc   ;==>_LoadStart

Func _AddDates($hWnd, $aDatesArray)
	For $i = 1 To $aDatesArray[0][0]
		$sNew = StringTrimRight($aDatesArray[$i][0], 4)
		_GUICtrlListView_AddItem($hWnd, $sNew & " " & $aDatesArray[$i][1])
		$Zeilen += 1
	Next
EndFunc   ;==>_AddDates

Func _Neu()
	Local $aFile
	If Not FileExists($sFile) Then
		$iMsgBoxAnswer = MsgBox(262179, "Arbeitsplaner", "Möchten Sie die Änderungen in " & $sTitleFile & " speichern?")
		Select
			Case $iMsgBoxAnswer = 6 ;Yes
				_Save()
				_Delete()
			Case $iMsgBoxAnswer = 7 ;No
				_Delete()
				GUICtrlSetState($Speichern, $GUI_DISABLE)
		EndSelect
	EndIf
	_FileReadToArray($sFile, $aFile)
	For $j = 0 To _GUICtrlListView_GetItemCount($hGrid) - 1
		$aItem = _GUICtrlListView_GetItemTextString($hGrid, $j)
		$aListView = _ArrayAdd($aLV, $aItem)
	Next
	If UBound($aFile) <> UBound($aListView) Then
		If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
		$iMsgBoxAnswer = MsgBox(262179, "Arbeitsplaner", "Möchten Sie die Änderungen in " & $sTitleFile & " speichern?")
		Select
			Case $iMsgBoxAnswer = 6 ;Yes
				_Save()
				_Delete()
			Case $iMsgBoxAnswer = 7 ;No
				_Delete()
		EndSelect
	EndIf
EndFunc   ;==>_Neu

Func _Save()
	$hFileOut = FileOpen($sFile, 2)
	For $j = 0 To _GUICtrlListView_GetItemCount($hGrid) - 1
		$aItem = _GUICtrlListView_GetItemTextString($hGrid, $j)
		FileWriteLine($hFileOut, $aItem)
	Next
	FileClose($hFileOut)
EndFunc   ;==>_Save

Func _Delete()
	_GUICtrlListView_DeleteAllItems($hGrid)
	_WinAPI_SetWindowText($hInputEx, "")
	Global $aDates = _GetMonthDayInfo(@MON, @YEAR)
	_AddDates($hGrid, $aDates)
	GUICtrlSetState($Speichern, $GUI_DISABLE)
EndFunc   ;==>_Delete

Func _OptChange()
	GUISetState(@SW_HIDE, $GUI)
	Sleep(500)
	GUISetState(@SW_SHOW, $Options)
	_WinAPI_SetFocus($Options)
EndFunc   ;==>_OptChange

Func _Abbrechen()
	GUISetState(@SW_HIDE, $Options)
	Sleep(500)
	GUICtrlSetData($Input1, IniRead($IniFileName, "Infos", "Speicherpfad", ""))
	GUICtrlSetData($Input2, IniRead($IniFileName, "Infos", "Dateipraefix", ""))
	GUICtrlSetData($Soll, IniRead($IniFileName, "Infos", "Soll", ""))
	GUICtrlSetData($Pause, IniRead($IniFileName, "Infos", "Pause", ""))
	GUISetState(@SW_SHOW, $GUI)
	_WinAPI_SetFocus($GUI)
EndFunc   ;==>_Abbrechen

Func _OkBtn()
	GUISetState(@SW_HIDE, $Options)
	Sleep(500)
	IniWrite($IniFileName, "Infos", "Speicherpfad", GUICtrlRead($Input1))
	IniWrite($IniFileName, "Infos", "Dateipraefix", GUICtrlRead($Input2))
	If StringRegExp(GUICtrlRead($Input3), "(\d{1,2})\.(\d{2})", 1) Then
		IniWrite($IniFileName, "Infos", "Soll", GUICtrlRead($Input3))
	Else
		GUICtrlSetData($Input3, "ERROR")
	EndIf
	If StringRegExp(GUICtrlRead($Input4), "(\d{1})\:(\d{2})", 1) Then
		IniWrite($IniFileName, "Infos", "Pause", GUICtrlRead($Input4))
	Else
		GUICtrlSetData($Input4, "ERROR")
	EndIf
	GUICtrlSetData($Soll, IniRead($IniFileName, "Infos", "Soll", "8.00"))
	GUICtrlSetData($Pause, IniRead($IniFileName, "Infos", "Pause", "0:45"))
	GUISetState(@SW_SHOW, $GUI)
	_WinAPI_SetFocus($GUI)
EndFunc   ;==>_OkBtn

Func _ConfirmBtn()
	IniWrite($IniFileName, "Infos", "Speicherpfad", GUICtrlRead($Input1))
	IniWrite($IniFileName, "Infos", "Dateipraefix", GUICtrlRead($Input2))
	If StringRegExp(GUICtrlRead($Input3), "(\d{1,2})\.(\d{2})", 1) Then
		IniWrite($IniFileName, "Infos", "Soll", GUICtrlRead($Input3))
	Else
		GUICtrlSetData($Input3, "ERROR")
	EndIf
	If StringRegExp(GUICtrlRead($Input4), "(\d{1})\:(\d{2})", 1) Then
		IniWrite($IniFileName, "Infos", "Pause", GUICtrlRead($Input4))
	Else
		GUICtrlSetData($Input4, "ERROR")
	EndIf
	GUICtrlSetData($Soll, IniRead($IniFileName, "Infos", "Soll", "8.00"))
	GUICtrlSetData($Pause, IniRead($IniFileName, "Infos", "Pause", "0:45"))
	GUICtrlSetData($Label18, "Aktuell: " & GUICtrlRead($Soll))
	GUICtrlSetData($Label7, "Aktuell: " & GUICtrlRead($Pause) & " h")
EndFunc   ;==>_ConfirmBtn

Func _Print()
	Local $sFilePath = @ScriptDir & "\print.txt"
	Local $sHeader = "Datum|kommt|geht|Ist|Konto +/-|Bemerkung"
	RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Notepad", "IfFaceName", "REG_SZ", "Arial")
	RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Notepad", "iPointSize", "REG_DWORD", "0x00000082")
	_FileCreate(@ScriptDir & "\Arbeitszeiten" & $LabelMonth & " " & $Year & ".txt")
	$hFile = FileOpen($sFilePath, 2)
	FileWriteLine($hFile, "Arbeitszeiten " & $LabelMonth & " " & $Year)
	FileWriteLine($hFile, @CRLF)
	FileWriteLine($hFile, @CRLF)
	FileWriteLine($hFile, StringReplace($sHeader, "|", @TAB))
	FileWriteLine($hFile, @CRLF)
	For $j = 0 To _GUICtrlListView_GetItemCount($hGrid) - 1
		$aItem = _GUICtrlListView_GetItemTextString($hGrid, $j)
		FileWriteLine($hFile, StringReplace($aItem, "|", @TAB))
	Next
	_FilePrint($sFilePath)
	If Not @error Then
		Sleep(1000)
		MsgBox(262208, "Erfolgreich!", "Der Druckvorgang konnte erfolgreich abgeschlossen werden!")
	Else
		MsgBox(262160, "Fehlgeschlagen!", "Der Druckvorgang ist fehlgeschlagen!")
	EndIf
	FileClose($hFile)
	FileDelete($sFilePath)
	RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Notepad", "IfFaceName", "REG_SZ", "Lucida Console")
	RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Notepad", "iPointSize", "REG_DWORD", "0x00000064")
EndFunc   ;==>_Print

Func _About()
	MsgBox(262208, "About", "Dieses Programm unterliegt einem Copyright von pinguin94 (www.autoit.de)" & @CRLF & _
			"Special Thanks gehen an Raupi (www.autoit.de) für die grandiosen Nachrichtencodes in diesem Programm.")
EndFunc   ;==>_About

Func _HasFocus()
	$hInput = GUICtrlGetHandle($Input1)
	If _WinAPI_GetFocus() = $hInput Then
		_WinAPI_SetFocus(0)
	EndIf
EndFunc   ;==>_HasFocus

Func _PtIsInRect($iXPoint, $iYPoint, $iXRect, $iYRect, $iWidthRect, $iHeightRect)
	If $iXPoint >= $iXRect And $iYPoint >= $iYRect And $iXPoint <= $iXRect + $iWidthRect And $iYPoint <= $iYRect + $iHeightRect Then Return 1
	Return 0
EndFunc   ;==>_PtIsInRect

Func _Click()
	$aMousePos = GUIGetCursorInfo($Options)
	If _PtIsInRect($aMousePos[0], $aMousePos[1], 40, 96, 265, 24) Then
		WinSetOnTop($Options, "", 0)
		$SavePath = FileSelectFolder("Wähle Standard Speicherpfad", "", 1 + 2 + 4)
		GUICtrlSetData($Input1, $SavePath, "")
	EndIf
	WinSetOnTop($Options, "", 1)
EndFunc   ;==>_Click

Func _GetItemTextCount()
	Local $iAnzahl = 0
	For $i = 0 To _GUICtrlListView_GetItemCount($hGrid)
		If _GUICtrlListView_GetItemText($hGrid, $i, 4) <> "" Then
			$iAnzahl += 1
		EndIf
	Next
	Return $iAnzahl
EndFunc   ;==>_GetItemTextCount
