#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=steam.ico
#AutoIt3Wrapper_Outfile=Steam_Quicklauncher.exe
#AutoIt3Wrapper_Res_Comment=Steam - Quickstart your Games!
#AutoIt3Wrapper_Res_Description=Now with a ContextMenu and TrayMenü
#AutoIt3Wrapper_Res_Fileversion=1.0.1.14
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=This Application is not affilated with Steam or Valve
#AutoIt3Wrapper_Run_Tidy=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#cs ----------------------------------------------------------------------------
	
	AutoIt Version: 3.3.8.1
	Author:         myName
	
	Script Function:
	Template AutoIt script.
	
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here


#include <Array.au3>
#include <StructureConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScrollBarConstants.au3>
#include <GuiScrollBars.au3>
#include <Misc.au3>
#include "_UskinLibrary.au3"
#include "array.au3"
#include "axis.au3"


If _Singleton("Steam_Quicklauncher", 1) = 0 Then
	MsgBox($MB_SYSTEMMODAL, "Achtung", "Steam_Quicklauncher lauft bereits!")
	Exit
EndIf

Local $debug = "false"
Local $name[2560]
Local $appid[2560]
Local $imgpath[2560]
Local $image[2560]
Local $pic[2560]
Local $context[2560], $cmnews[2560], $cmhide[2560]
Local $skin
Local $x = 5
Local $a = 5
Local $Trayexit, $Trayshow
Global $aReturn, $sXMLData
Opt("TrayMenuMode", 3)





DebugLine("################INFO#################", "")

GUIRegisterMsg($WM_VSCROLL, "WM_VSCROLL")
GUIRegisterMsg($WM_SIZE, "WM_SIZE")


If DirGetSize(@ScriptDir & "\images") = -1 Then
	DirCreate(@ScriptDir & "\images\")

EndIf

If FileExists("games.xml") Then FileDelete("games.xml")



If Not FileExists("config.ini") Then
	$var = MsgBox($MB_YESNOCANCEL, "Steam ID ?", "Bitte gib an ob du deine Steam ID weisst..")

	If $var = "6" Then Nick()

	If $var = "7" Then ShellExecute("steam://url/SteamIDEditPage")
	If $var = "2" Then XExit("Cancel|SteamID")
EndIf


$GuiMultiply = IniRead("config.ini", "config", "GuiMultiply", "4.5")
$skin = IniRead("config.ini", "config", "Skin", "embedded")
$debug = IniRead("config.ini", "config", "DEBUG", "")
$nick = IniRead("config.ini", "config", "steamAcc", "")
If $nick = "" Then Nick()
$d = InetGet("http://steamcommunity.com/id/" & $nick & "/games?tab=all&xml=1", "games.xml")


$handle = FileOpen("games.xml")
$data = FileRead($handle)
$aReturn = _GetXML($data, "game")



If $skin = "embedded" Then
	;Integriert Axis Skin
	$skin = _Axis(True, @TempDir)

EndIf


Func DebugLine($Section, $string)
	If Not $debug = "" Then FileWriteLine("debug.log", $Section & ": " & $string & @CRLF)
EndFunc   ;==>DebugLine

Func XExit($error)
	DebugLine("Error", $error)
	Exit
EndFunc   ;==>XExit

Func Gui($step) ;Main Gui 3  Steps

	Switch $step
		Case "0"
			#Region ### START Koda GUI section ### Form=
			Global $Form1 = GUICreate("Steam Quicklaunch", 207, 500, 192, 124, BitXOR($GUI_SS_DEFAULT_GUI, $WS_MINIMIZEBOX))
			GUISetIcon(@AutoItExe, 1)
			GUISetIcon(@AutoItExe, 1)
			GUISetBkColor(0x666666)
			#EndRegion ### END Koda GUI section ###
			_Uskin_LoadDLL()
;~ _USkin_Init(@ScriptDir & "\"&$skin); <-- Put here your skin...


			_USkin_Init($skin)

;~ _ArrayDisplay($aReturn)
		Case "1"
			For $i = 1 To $aReturn[0]

				$appidx = _GetXML($aReturn[$i], "appID")
				$nametmp = _GetXML($aReturn[$i], "name")
				$namex = StringReplace($nametmp[1], "<![CDATA[", "")
				$name[$i] = StringReplace($namex, "]]>", "")
				$appid[$i] = $appidx[1]
				If IniRead("config.ini", "Hide", $appid[$i], "") = $name[$i] Then

				Else
					$nametmp = _GetXML($aReturn[$i], "name")
					$imagetmp = _GetXML($aReturn[$i], "logo")

					$imagex = StringReplace($imagetmp[1], "<![CDATA[", "")
					$image = StringReplace($imagex, "]]>", "")

					$imgpath[$i] = @ScriptDir & "\images\" & $appid[$i] & ".jpg"
					DebugLine("Image", $imgpath[$i])
					InetGet($image, $imgpath[$i])
;~ MsgBox(default,$imgpath[$i],$imagetmp[1])
					$Pic[$i] = GUICtrlCreatePic($imgpath[$i], 5, $x, 184, 69)
					$context[$i] = GUICtrlCreateContextMenu($Pic[$i])
					GUICtrlCreateMenuItem($name[$i], $context[$i])
					GUICtrlCreateMenuItem("", $context[$i])
					$cmnews[$i] = GUICtrlCreateMenuItem("News anzeigen", $context[$i])
					$cmhide[$i] = GUICtrlCreateMenuItem("Verstecken", $context[$i])

;~ GUICtrlSetImage(-1,$imgpath[$i])
					$x = 69 + $x + $a
				EndIf




			Next
		Case "2"

			GUISetState(@SW_SHOW)

			_GUIScrollBars_Init($Form1, "", $GuiMultiply * $aReturn[0])

			_GUIScrollBars_EnableScrollBar($Form1, $SB_VERT)
	EndSwitch
EndFunc   ;==>Gui

Func TrayMenu() ;SysTray Menü
	TraySetIcon(@AutoItExe)


	$Trayshow = TrayCreateItem("Anzeigen")
	TrayCreateItem("")
	$Trayexit = TrayCreateItem("Beenden")
	; Create a separator line.
	TraySetState(1)
EndFunc   ;==>TrayMenu


Func Loop() ;Main Loop
	While 1
		$nMsg = GUIGetMsg()
		$TMsg = TrayGetMsg()
		Switch $TMsg
			Case $Trayexit
				Exit
			Case $Trayshow
				GUISetState(@SW_SHOW, $Form1)
		EndSwitch



		Switch $nMsg
			Case $GUI_EVENT_CLOSE
				Exit

			Case Else
				For $i = 0 To $aReturn[0]
					If $nMsg = "" Then
					Else

						If $nMsg = $pic[$i] Then

							GUISetState(@SW_HIDE, $Form1)
							ShellExecute("steam://rungameid/" & $appid[$i], "", "open")

						EndIf
						$cmnews
						If $nMsg = $cmnews[$i] Then


							ShellExecute("steam://appnews/" & $appid[$i], "", "open")

						EndIf

						If $nMsg = $cmhide[$i] Then
							IniWrite("config.ini", "Hide", $appid[$i], $name[$i])
							MsgBox(Default, "Info", "Das Programm muss neu gestartet werden um die Änderungen zu übernehmen!")
						EndIf
					EndIf
				Next
		EndSwitch
	WEnd
EndFunc   ;==>Loop


Func Nick() ;Steam ID abfrage

	$namea = InputBox("Steam Quick Launcher", "Bitte gib den Namen deiner Steam ID ein(Du kannst deine Steam ID in deinen Steam Profil Einstellungen festlegen)")
	If @error = "1" Then XExit("Cancel|SteamID")
	If $namea = "" Then Nick()
	IniWrite("config.ini", "config", "steamAcc", $namea)
	IniWrite("config.ini", "config", "GuiMultiply", "4.5")

	IniWrite("config.ini", "config", "Skin", "embedded")


EndFunc   ;==>Nick


Func _GetXML($sString, $sData) ;XML Parser
	Local $aError[2] = [1, $sString], $aReturn
	$aReturn = StringRegExp('<' & $sData & '></' & $sData & '>' & $sString, '(?s)(?i)<' & $sData & '>(.*?)</' & $sData & '>', 3)
	If @error Then
		Return SetError(1, 0, $aError)
	EndIf
	$aReturn[0] = UBound($aReturn, 1) - 1
	Return SetError(0, 0, $aReturn)
EndFunc   ;==>_GetXML


Func WM_SIZE($hWnd, $Msg, $wParam, $lParam)
	#forceref $Msg, $wParam
	Local $index = -1, $yChar, $xChar, $xClientMax, $xClient, $yClient, $ivMax
	For $x = 0 To UBound($aSB_WindowInfo) - 1
		If $aSB_WindowInfo[$x][0] = $hWnd Then
			$index = $x
			$xClientMax = $aSB_WindowInfo[$index][1]
			$xChar = $aSB_WindowInfo[$index][2]
			$yChar = $aSB_WindowInfo[$index][3]
			$ivMax = $aSB_WindowInfo[$index][7]
			ExitLoop
		EndIf
	Next
	If $index = -1 Then Return 0

	Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)

	; Retrieve the dimensions of the client area.
	$xClient = BitAND($lParam, 0x0000FFFF)
	$yClient = BitShift($lParam, 16)
	$aSB_WindowInfo[$index][4] = $xClient
	$aSB_WindowInfo[$index][5] = $yClient

	; Set the vertical scrolling range and page size
	DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
	DllStructSetData($tSCROLLINFO, "nMin", 0)
	DllStructSetData($tSCROLLINFO, "nMax", $ivMax)
	DllStructSetData($tSCROLLINFO, "nPage", $yClient / $yChar)
	_GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)

	; Set the horizontal scrolling range and page size
	DllStructSetData($tSCROLLINFO, "fMask", BitOR($SIF_RANGE, $SIF_PAGE))
	DllStructSetData($tSCROLLINFO, "nMin", 0)
	DllStructSetData($tSCROLLINFO, "nMax", 2 + $xClientMax / $xChar)
	DllStructSetData($tSCROLLINFO, "nPage", $xClient / $xChar)
	_GUIScrollBars_SetScrollInfo($hWnd, $SB_HORZ, $tSCROLLINFO)

	Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SIZE


Func WM_VSCROLL($hWnd, $Msg, $wParam, $lParam)
	#forceref $Msg, $wParam, $lParam
	Local $nScrollCode = BitAND($wParam, 0x0000FFFF)
	Local $index = -1, $yChar, $yPos
	Local $Min, $Max, $Page, $Pos, $TrackPos

	For $x = 0 To UBound($aSB_WindowInfo) - 1
		If $aSB_WindowInfo[$x][0] = $hWnd Then
			$index = $x
			$yChar = $aSB_WindowInfo[$index][3]
			ExitLoop
		EndIf
	Next
	If $index = -1 Then Return 0


	; Get all the vertial scroll bar information
	Local $tSCROLLINFO = _GUIScrollBars_GetScrollInfoEx($hWnd, $SB_VERT)
	$Min = DllStructGetData($tSCROLLINFO, "nMin")
	$Max = DllStructGetData($tSCROLLINFO, "nMax")
	$Page = DllStructGetData($tSCROLLINFO, "nPage")
	; Save the position for comparison later on
	$yPos = DllStructGetData($tSCROLLINFO, "nPos")
	$Pos = $yPos
	$TrackPos = DllStructGetData($tSCROLLINFO, "nTrackPos")

	Switch $nScrollCode
		Case $SB_TOP ; user clicked the HOME keyboard key
			DllStructSetData($tSCROLLINFO, "nPos", $Min)

		Case $SB_BOTTOM ; user clicked the END keyboard key
			DllStructSetData($tSCROLLINFO, "nPos", $Max)

		Case $SB_LINEUP ; user clicked the top arrow
			DllStructSetData($tSCROLLINFO, "nPos", $Pos - 1)

		Case $SB_LINEDOWN ; user clicked the bottom arrow
			DllStructSetData($tSCROLLINFO, "nPos", $Pos + 1)

		Case $SB_PAGEUP ; user clicked the scroll bar shaft above the scroll box
			DllStructSetData($tSCROLLINFO, "nPos", $Pos - $Page)

		Case $SB_PAGEDOWN ; user clicked the scroll bar shaft below the scroll box
			DllStructSetData($tSCROLLINFO, "nPos", $Pos + $Page)

		Case $SB_THUMBTRACK ; user dragged the scroll box
			DllStructSetData($tSCROLLINFO, "nPos", $TrackPos)
	EndSwitch

;~    // Set the position and then retrieve it.  Due to adjustments
;~    //   by Windows it may not be the same as the value set.

	DllStructSetData($tSCROLLINFO, "fMask", $SIF_POS)
	_GUIScrollBars_SetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
	_GUIScrollBars_GetScrollInfo($hWnd, $SB_VERT, $tSCROLLINFO)
	;// If the position has changed, scroll the window and update it
	$Pos = DllStructGetData($tSCROLLINFO, "nPos")

	If ($Pos <> $yPos) Then
		_GUIScrollBars_ScrollWindow($hWnd, 0, $yChar * ($yPos - $Pos))
		$yPos = $Pos
	EndIf

	Return $GUI_RUNDEFMSG

EndFunc   ;==>WM_VSCROLL





TrayMenu()
Gui(0)
Gui(1)
Gui(2)


Loop()
