'Script Copyright  2012 Ryan Marshall (Skarn)
'All Rights Reserved

'common drag globals
Dim dragX
Dim dragY
Dim drag_xDown
Dim drag_yDown
Dim drag_lastXOffset
Dim drag_lastYOffset

Dim allowDrag				'dxEdit controls wide - allows normal object repositioning
Dim allowGrid
Dim allowRing
Dim allowFree
Dim allowCustom

'common script components
Dim dxEdit
Dim dxs

'external references
Dim xmlFile
Dim xmlShortcuts

'control specific
Dim imageSize
Dim strRootName
Dim dragging
Dim intLayoutType			'0 - grid, 1 - rings, 2 - theme, 3 - free
Dim intRenderMode			'0 - developer, 1 - user

'internally calculated
Dim shortcutCount
Dim columnMax
Dim shortcutsHeight

Sub Object_OnScriptEnter()
	On Error Resume Next
	Call dxInit()
End Sub

Sub dxInit()	
	dragging = False
	strRootName = Object.Name

	Set dxs = DesktopX.ScriptObject("dxScriptor")
	Set dxEdit = DesktopX.ScriptObject("dxEdit_script")
	Set xmlFile = dxEdit.dxShortcutsXML
	
	Set xmlShortcuts = xmlFile.selectSingleNode("layers/layer[@name = """ & strRootName & """]")
	If xmlShortcuts Is Nothing Then
		Set newLayer = xmlFile.selectSingleNode("layers/templates/layer").cloneNode(true)
		newLayer.selectSingleNode("@name").text = strRootName
		Call xmlFile.selectSingleNode("layers").appendChild(newLayer)
		Set xmlShortcuts = xmlFile.selectSingleNode("layers/layer[@name = """ & strRootName & """]")
	End If
	If DesktopX.IsObject(strRootName & "_scrollArea") Then
		DesktopX.Object(strRootName & "_scrollArea").Move 0,0 
		DesktopX.Object(strRootName & "_scrollArea").Width = Object.Width
		DesktopX.Object(strRootName & "_scrollArea").Height = 10000
	End If
	
	allowRing = xmlShortcuts.selectSingleNode("@allowRing").text
	allowGrid = xmlShortcuts.selectSingleNode("@allowGrid").text
	allowFree = xmlShortcuts.selectSingleNode("@allowFree").text
	allowCustom = xmlShortcuts.selectSingleNode("@allowCustom").text
	
	Call refreshShortcuts()
End Sub

'object events
Sub Object_OnDropFilesEx(obj,files)
	arrFiles = split(files,"|")
	strFilePath = arrFiles(0)
	Call shortcutAdd(strFilePath)
End Sub

Function Object_OnLButtonDownEx(obj,x,y)
	drag_xDown = x
	drag_yDown = y
	drag_lastXOffset = 0
	drag_lastYOffset = 0
	'Object_OnLButtonDownEx = True
End Function

Function Object_OnLButtonUpEx(obj,x,y,dragged)
	If dragged = False Then
		If obj.Group = "shortcut" Then
			strToggleObject = xmlShortcuts.selectSingleNode("@toggleObject").text
			Select Case strToggleObject
				Case "* none"
					
				Case "* parent"
					DesktopX.Object(Object.ParentName).Visible = False
				
				Case "* layer"
					DesktopX.Object(obj.ParentName).Visible = False
					
				Case Else
					DesktopX.Object(strToggleObject).Visible = False
			
			End Select
		End If
	End If
	'Object_OnLButtonUpEx = True
End Function

Function Object_OnRButtonUpEx(obj,x,y,dragged)
	Select Case obj.Group
		Case "shortcut"		'display shortcut menu
			Object_OnRButtonUpEx = shortcutMenu(obj)
			
	End Select
	
	Select Case obj.Name	'display base menu
		Case strRootName & "_scrollArea",strRootName
			Object_OnRButtonUpEx = baseMenu(obj)
			
	End Select
	Object_OnRButtonUpEx = True
End Function

Sub Object_OnDragEx(obj,x,y,newX,newY)
	Select Case obj.Group
		Case "shortcut"
			If intLayoutType <> 3 Then
				obj.Left = obj.Left
				obj.Top = obj.Top
				If dragging = False then
					obj.Clone "dragImage",newX,newY
					DesktopX.Object("dragImage").Opacity = 50
					DesktopX.Object("dragImage").Group = ""
					DesktopX.Object("dragImage").Cursor = 12
					DesktopX.Object("dragImage").Visible = True
					dragging = True
				Else
					dragX = newX + drag_xDown
					dragY = newY + drag_yDown
					DesktopX.Object("dragImage").Move newX,newY
				End If
			Else
				obj.Group = "shortcut_temp"
			End If
			
			Exit Sub
		
		Case "shortcut_temp"
			Exit Sub
			
	End Select
	
	Select Case obj.Name
		Case strRootName & "_magicScroller"
			If dxEdit.dxEdit_allowDrag = False Then
				obj.Left = obj.Left
				obj.Top = obj.Top
				drag_xOffset = newX - obj.Left
				drag_yOffset = newY - obj.Top
				drag_deltaX = drag_lastXOffset - drag_xOffset
				drag_deltaY = 2*(drag_lastYOffset - drag_yOffset)
				scrollAreaYPos = DesktopX.Object(strRootName & "_scrollArea").Top
				newScrollAreaYPos = scrollAreaYPos + drag_deltaY
				If newScrollAreaYPos > 0 then
					newScrollAreaYPos = 0
				End If
				
				If newScrollAreaYPos < -shortcutsHeight Then
					newScrollAreaYPos = -shortcutsHeight
				End If
				
				DesktopX.Object(strRootName & "_scrollArea").Top = newScrollAreaYPos
				drag_lastXOffset = drag_xOffset
				drag_lastYOffset = drag_yOffset
			End If
			
		Case strRootName, strRootName & "_magicScroll"
			If dxEdit.dxEdit_allowDrag = False Then
				obj.Left = obj.Left
				obj.Top = obj.Top
			End If
			
		Case else
			obj.Left = obj.Left
			obj.Top = obj.Top
			
	End Select
			
End Sub

Sub Object_OnDragFinishEx(obj)
	Select Case obj.Group
		Case "shortcut"
			dragging = False
			If intLayoutType <> 3 Then
				If DesktopX.IsObject("dragImage") Then
					Call dxs.dxDestroy("dragImage")
				End If
				
				shortcutNameBeingMoved = Replace(Left(obj.Name,Len(obj.Name)-1),strRootName & "_","")
				
				If DesktopX.IsObject(strRootName & "_scrollArea") Then
					strDropObject = dxs.dxObjectAtPos(dragX,dragY,strRootName & "_scrollArea")
				Else
					strDropObject = dxs.dxObjectAtPos(dragX,dragY,strRootName)
				End If
				
				If strDropObject = False then
					Call shortcutInsert(shortcutNameBeingMoved,Null)
				Else
					shortcutNameBeingShoved = Replace(Left(strDropObject,Len(strDropObject)-1),strRootName & "_","")
					Call shortcutInsert(shortcutNameBeingMoved,shortcutNameBeingShoved)
				End If
			End If
		
		Case "shortcut_temp"
			dragging = False
			shortcutNameBeingMoved = Replace(Left(obj.Name,Len(obj.Name)-1),strRootName & "_","")
			Set nodeShortcut = xmlShortcuts.selectSingleNode("shortcuts/shortcut[shortcutName = """ & shortcutNameBeingMoved & """]")
			nodeShortcut.selectSingleNode("@x").text = obj.Left
			nodeShortcut.selectSingleNode("@y").text = obj.Top
			obj.Group = "shortcut"
			Call xmlFile.Save(Object.Directory & "shortcutLayers.xml")
			
	End Select
	
	dragX = 0
	dragY = 0
	drag_xDown = 0
	drag_yDown = 0
	drag_lastXOffset = 0
	drag_lastYOffset = 0
End Sub

'shortcut functions
Sub refreshShortcuts()	'regenerates all shortcuts for layer
	If DesktopX.IsObject(strRootName & "_scrollArea") Then
		Set objBase = DesktopX.Object(strRootName & "_scrollArea")
	Else
		Set objBase = DesktopX.Object(strRootName)
	End If
	
	'clear all previous shortcut objects/icons
	For each objChild in objBase.Children
		If objChild.Group = "shortcut" Then
			Call dxs.dxDestroy(objChild.Name)
		End If
	Next
	centerXPos = objBase.Width/2
	centerYPos = objBase.Height/2
	
	intLayoutType = Cint(xmlShortcuts.selectSingleNode("@layoutType").text)
	imageSize = Cint(xmlShortcuts.selectSingleNode("@imageSize").text)
	Set nodesShortcuts = xmlShortcuts.selectNodes("shortcuts/shortcut")
	shortcutsCount = nodesShortcuts.length
	shortcutCount = 0
	shortcutTallyInRings = 0
	rowCount = 0
	ringCount = 1
	For each nodeShortcut in nodesShortcuts
		strShortcutCaption = ""
		If intLayoutType = 1 Then
			Set nodeRing = xmlShortcuts.selectSingleNode("rings/ring[" & ringCount & "]")
			If nodeRing Is Nothing Then
				Exit For
			End If
			ring_distribute = nodeRing.selectSingleNode("@distribute").text
			ring_radius = nodeRing.selectSingleNode("@radius").text
			ring_maxCount = nodeRing.selectSingleNode("@maxCount").text
			If ring_maxCount = 0 Then ring_maxCount = 1
			ring_xOffset = nodeRing.selectSingleNode("@xOffset").text
			ring_yOffset = nodeRing.selectSingleNode("@yOffset").text
			ring_angleOffset = nodeRing.selectSingleNode("@angleOffset").text
			ring_angleRange  = nodeRing.selectSingleNode("@angleRange").text
			pi=4*Atn(1)
			If ring_distribute = 1 Then
				If ring_maxCount > (shortcutsCount - shortcutTallyInRings) Then
					angleIncrement = ring_angleRange * pi / (shortcutsCount - shortcutTallyInRings)
				End If
			Else
				angleIncrement = ring_angleRange * pi / ring_maxCount
			End If
			currAngle = ring_angleOffset + shortcutCount * angleIncrement
			xPos = ring_xOffset + centerXPos + ring_radius * Cos(currAngle) - imageSize/2
			yPos = ring_yOffset + centerYPos + ring_radius * Sin(currAngle) - imageSize/2
		ElseIf intLayoutType = 0 Then
			yPos = (imageSize + 2) * rowCount
			xPos = (imageSize + 2) * shortcutCount
			shortcutsHeight = yPos
			If yPos + imageSize > objBase.Height Then
				Exit For
			End If
		ElseIf intLayoutType = 3 Then
			xPos = CInt(nodeShortcut.selectSingleNode("@x").text)
			yPos = CInt(nodeShortcut.selectSingleNode("@y").text)
		ElseIf intLayoutType = 2 Then
			Set nodeX = xmlShortcuts.selectSingleNode("customs/pos[" & shortcutCount & "]/@x")
			If nodeX Is Nothing Then
				xPos = -100
				yPos = -100
			Else
				xPos = CInt(xmlShortcuts.selectSingleNode("customs/pos[" & shortcutCount & "]/@x").text)
				yPos = CInt(xmlShortcuts.selectSingleNode("customs/pos[" & shortcutCount & "]/@y").text)
			End If
		End If
		
		'get the shortcut details
		strNodeImage = nodeShortcut.selectSingleNode("image").text
		strShortcutPath = nodeShortcut.selectSingleNode("shortcutPath").text
		strShortcutName = nodeShortcut.selectSingleNode("shortcutName").text
		strShortcutCaption = nodeShortcut.selectSingleNode("shortcutCaption").text
		
		'create shortcut object
		DesktopX.Object("dxEdit_iconMate").Clone strRootName & "_" & strShortcutName & "*",-1000,-1000
		DesktopX.Object(strRootName & "_" & strShortcutName & "*").Parent = objBase
		Set objShortcut = DesktopX.Object(strRootName & "_" & strShortcutName & "*")
		
		'check if image file exists and set to default if not found
		If dxs.filFileExists(Object.Directory & strNodeImage) = False then
			strNodeImage = "<default>"
		End If

		'set the shortcut object properties
		objShortcut.TooltipText = CStr(strShortcutCaption)
		objShortcut.Picture = strNodeImage
		objShortcut.Resize imageSize,imageSize
		objShortcut.Command = strShortcutPath
		objShortcut.Move xPos,yPos
			
		objShortcut.Visible = True
		objShortcut.Group = "shortcut"
		
		shortcutCount = shortcutCount + 1		
		If intLayoutType = 1 Then
			If shortcutCount > ring_maxCount - 1 Then
				shortcutTallyInRings = shortcutTallyInRings + ring_maxCount
				shortcutCount = 0
				ringCount = ringCount + 1
			End If
		Else
			columnMax = Int(Object.Width/(imageSize+2)) - 1
			If shortcutCount > columnMax Then
				shortcutCount = 0
				rowCount = rowCount + 1
			End If
		End If
	Next
End Sub

Sub shortcutUpdate(strShortcutName,strShortcutPath,strImageFile)	'updates an existing shortcut details in the back-end shortcuts xml
	Set nodeParent = xmlShortcuts.selectSingleNode("shortcuts")
	Set nodeShortcut = nodeParent.selectSingleNode("shortcut[shortcutName = """ & strShortcutName & """]")
	If nodeShortcut Is Nothing Then
		MsgBox("Shortcut is no longer valid.")
		Exit Sub
	End If
	
	'if a new shortcut path has been specified, then update it
	If IsNull(strShortcutPath) = False Then
		If strShortcutPath <> "" Then
			'check if new path is a file and calculate new shortcut name
			If dxs.filFileExists(strShortcutPath) Then
				strNewShortcutName = Trim(dxs.filGetBaseName(strShortcutPath))
			'check if new path is a folder and calculate new shortcut name
			ElseIf dxs.filFolderExists(strShortcutPath) Then
				strNewShortcutName = Trim(dxs.filGetBaseName(strShortcutPath))
				strNewShortcutName = Left(strShortcutPath,1) & "_" & strNewShortcutName
			Else
				MsgBox("Invalid shortcut path to update with.")
				Exit Sub
			End If
			'update shortcut name and shortcut path
			nodeShortcut.selectSingleNode("shortcutName").text = strNewShortcutName
			nodeShortcut.selectSingleNode("shortcutPath").text = strShortcutPath
		End If
	End If
	
	'if a new shortcut image has been specified, then update it
	'the shortcutUpdate function is called internally and strImageFile can only ever be an image file name that have been copied to the Object.Directory
	If IsNull(strImageFile) = False Then
		If strImageFile <> "" Then
			nodeShortcut.selectSingleNode("image").text = strImageFile
		End If
	End If
	
	Call xmlFile.Save(Object.Directory & "shortcutLayers.xml")
	Call refreshShortcuts()
End Sub

Sub shortcutAdd(strShortcutPath)	'create a new shortcut in xml file
	'check if new path is a file ** and calculate new shortcut name
	If dxs.filFileExists(strShortcutPath) Then
		strNewShortcutBaseName = Trim(dxs.filGetBaseName(strShortcutPath))
		strNewShortcutCaption = strNewShortcutBaseName
		strNewShortcutName = strNewShortcutBaseName
		
		'extract the icons from the file
		extractIcons = dxs.extractIconsFromFile(strShortcutPath,Object.Directory & "tempIcons")
		If extractIcons <> False then
			'read the files extracted to the tempIcons directory
			strImageFilePath = System.FileOpenDialog("Select Image:", "", Object.Directory  & "tempIcons\", "All files|*.*", &H8)'dxEdit.conCreateImageSelector("selectImage",arrImages,imageSize)
			If strImageFilePath = "" Then	
				strImageFilePath = ""
			End If
			
			strImageFileExtension = dxs.filGetExtensionName(strImageFilePath)
			If Instr(1,"png,jpg,bmp,ico,gif,cur",strNewImageExtension,1) = 0 Then
				strImageFilePath = ""
			End If
			
			If strImageFilePath <> "" Then
				'copy image to Object.Directory
				strNewImageFileName = strNewShortcutBaseName & "." & strImageFileExtension
				Call dxs.filFileCopy(strImageFilePath,Object.Directory,strNewImageFileName)
				strNewShortcutImageName = strNewImageFileName
			Else
				strNewShortcutImageName = ""
			End If
		Else
			strNewShortcutImageName = ""
		End If
		
	'check if new path is a folder ** and calculate new shortcut name
	ElseIf dxs.filFolderExists(strShortcutPath) Then
		strNewShortcutBaseName = Trim(Replace(dxs.filGetBaseName(strShortcutPath),":",""))
		strNewShortcutCaption = strNewShortcutBaseName
		strNewShortcutName = Left(strShortcutPath,1) & "_" & strNewShortcutName
		strNewShortcutImageName = DesktopX.Object("dxEdit_images").States("folderImage").Picture
	Else
		MsgBox("Invalid shortcut path to update with.")
		Exit Sub
	End If
	
	Set nodeParent = xmlShortcuts.selectSingleNode("shortcuts")
	Set nodeShortcut = nodeParent.selectSingleNode("shortcut[shortcutName = """ & strNewShortcutName & """]")
	
	'check if shortcut nodes already exists and create it if it doesn't.
	If nodeShortcut Is Nothing Then
		Set nodeShortcut = xmlFile.selectSingleNode("layers/templates/shortcut").cloneNode(true)
		nodeShortcut.selectSingleNode("image").text = strNewShortcutImageName
		nodeShortcut.selectSingleNode("shortcutPath").text = strShortcutPath
		nodeShortcut.selectSingleNode("shortcutName").text = strNewShortcutName
		nodeShortcut.selectSingleNode("shortcutCaption").text = strNewShortcutCaption
		nodeShortcut.selectSingleNode("@x").text = "0"
		nodeShortcut.selectSingleNode("@y").text = "0"
		nodeParent.appendChild(nodeShortcut)
	Else
		MsgBox("A shortcut for that file/folder already exists.")
		Exit Sub
	End If
	
	Call xmlFile.Save(Object.Directory & "shortcutLayers.xml")
	Call refreshShortcuts()
End Sub

Sub shortcutDelete(strShortCutName)	'deletes a shortcut node from the xml
	Set nodeParent = xmlShortcuts.selectSingleNode("shortcuts")
	Set nodeToDelete = xmlShortcuts.selectSingleNode("shortcuts/shortcut[shortcutName =""" & strShortCutName & """]")
	If Not nodeToDelete Is Nothing Then
		Call nodeParent.removeChild(nodeToDelete)
		Call xmlFile.Save(Object.Directory & "shortcutLayers.xml")
		Call refreshShortcuts()
	End If
End Sub

Sub shortcutBrowse() 'lets user select new shortcut from a file select window.
	strShortcutPath = System.FolderDialog ("Please select a folder or file:", "", &H4000)
	If strShortcutPath = "" Then
		Exit Sub
	End If
	
	Call shortcutAdd(strShortcutPath)
End Sub

Sub shortcutInsert(shortcutNameToMove,shortcutNameToInsertBefore)	'relocates a shortcut node in front of another shortcut node in the xml
	Set nodeParent = xmlShortcuts.selectSingleNode("shortcuts")
	Set nodeToReposition = nodeParent.selectSingleNode("shortcut[shortcutName =""" & shortcutNameToMove & """]")
	If IsNull(shortcutNameToInsertBefore) Then
		Call nodeParent.insertBefore(nodeToReposition,Null)
	Else
		Set nodeToInsertBefore = nodeParent.selectSingleNode("shortcut[shortcutName =""" & shortcutNameToInsertBefore & """]")
		Call nodeParent.insertBefore(nodeToReposition,nodeToInsertBefore)
	End If
	
	Call xmlFile.Save(Object.Directory & "shortcutLayers.xml")
	Call refreshShortcuts()
End Sub

'menus
Function baseMenu(obj)	'if the scroll area or base layer is right clicked
	Dim menu
	Set menu = DesktopX.CreatePopupMenu
	menu.AppendMenu 0, 1, "Add shortcut/link"
	menu.AppendMenu &H00000800, -1, "" 
	menu.AppendMenu 0, 2, "Change icon size"
	menu.AppendMenu &H00000800, -1, "" 
	
	intLayoutType = Cint(xmlShortcuts.selectSingleNode("@layoutType").text)
	
	If allowGrid = "1" Then
		If intLayoutType = 0 Then
			menu.AppendMenu &H00000008, 3, "Grid layout"
		Else
			menu.AppendMenu 0, 3, "Grid layout"
		End If
	End If
	If allowRing = "1" Then
		If intLayoutType = 1 Then
			menu.AppendMenu &H00000008, 4, "Ring layout"
		Else
			menu.AppendMenu 0, 4, "Ring layout"
		End If
	End If
	If allowCustom = "1" Then
		If intLayoutType = 2 Then
			menu.AppendMenu &H00000008, 5, "Theme layout"
		Else
			menu.AppendMenu 0, 5, "Theme layout"
		End If
	End If
	If allowFree = "1" Then
		If intLayoutType = 3 Then
			menu.AppendMenu &H00000008, 6, "Free layout"
		Else
			menu.AppendMenu 0, 6, "Free layout"
		End If
	End If
	menu.AppendMenu &H00000800, -1, "" 
	menu.AppendMenu 0, 99, "Cancel"
	
	Dim result
	result = menu.TrackPopupMenu(0, System.CursorX, System.CursorY)
	baseMenu = True
	Select Case result
		Case 1
			'call browse for shortcut function
			Call shortcutBrowse()
			
		Case 2
			'change image size value
			strCurrentSize = xmlShortcuts.selectSingleNode("@imageSize").text
			strNewIconSize = InputBox("Enter new icon size:","Icon options",strCurrentSize)
			If strNewIconSize <> "" and IsNumeric(strNewIconSize) Then
				minSize = dxs.Min(Array(Object.Width,257))
				If Int(strNewIconSize) > minSize Then
					MsgBox("Too large. Please enter a value less than " & minSize)
				Else
					imageSize = Int(strNewIconSize)
					xmlShortcuts.selectSingleNode("@imageSize").text = strNewIconSize
					Call refreshShortcuts()
				End If
			Else
				MsgBox("Please enter a number.")
			End If
		
		Case 3
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "0"
			Call refreshShortcuts()
			
		Case 4
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "1"
			Call refreshShortcuts()
		
		Case 5
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "2"
			Call refreshShortcuts()
			
		Case 6
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "3"
			Call refreshShortcuts()
			
		Case 99
			Exit Function
			
	End Select
End Function

Function shortcutMenu(obj)	'if a shortcut object is right click
	lenRootName = Len(strRootName)+2
	shortcutName = Mid(obj.Name,lenRootName)
	shortcutName = Left(shortcutName,Len(shortcutName)-1)
	
	Dim menu
	Set menu = DesktopX.CreatePopupMenu
	menu.AppendMenu 0, 1000, "Change Icon (from linked file)"
	menu.AppendMenu 0, 2000, "Browse for Icon (image file)"
		Dim packagerMenu
		Set packagerMenu = DesktopX.CreatePopupMenu
		Set objShell = CreateObject("Shell.Application")
		strPublicDocumentsPath = objShell.Namespace(&h2e).Self.Path
		strPackagesPath = strPublicDocumentsPath & "\Stardock\IconPackager"
		If dxs.filFolderExists(strPackagesPath) Then
			arrPackages = Split(dxs.filFolderList(strPackagesPath,False),"|")
			packageCount = 3001
			For each strPackage in arrPackages
				packagerMenu.AppendMenu 0,packageCount,strPackage
				packageCount = packageCount + 1
			Next
			menu.AppendMenu &H00000010, packagerMenu.MenuID, "Browse for Icon (Iconpackager)"
		End If
		
	menu.AppendMenu 0, 4000, "Change shortcut/link"
	menu.AppendMenu 0, 98000, "Delete (shortcut)"
	menu.AppendMenu &H00000800, -1, "" 
	menu.AppendMenu 0, 5000, "Add shortcut/link"
	menu.AppendMenu 0, 6000, "Change icon(s) size"
	
	If allowGrid = "1" Then
		If intLayoutType = 0 Then
			menu.AppendMenu &H00000008, 7000, "Grid layout"
		Else
			menu.AppendMenu 0, 7000, "Grid layout"
		End If
	End If
	If allowRing = "1" Then
		If intLayoutType = 1 Then
			menu.AppendMenu &H00000008, 8000, "Ring layout"
		Else
			menu.AppendMenu 0, 8000, "Ring layout"
		End If
	End If
	If allowCustom = "1" Then
		If intLayoutType = 2 Then
			menu.AppendMenu &H00000008, 9000, "Theme layout"
		Else
			menu.AppendMenu 0, 9000, "Theme layout"
		End If
	End If
	If allowFree = "1" Then
		If intLayoutType = 3 Then
			menu.AppendMenu &H00000008, 10000, "Free layout"
		Else
			menu.AppendMenu 0, 10000, "Free layout"
		End If
	End If
	
	menu.AppendMenu 0, 99000, "Cancel"
	
	Dim result
	result = menu.TrackPopupMenu(0, System.CursorX, System.CursorY)
	shortcutMenu = True
	Select Case Int(result/1000)
		Case 1
			Set nodeShortcut = xmlShortcuts.selectSingleNode("shortcuts/shortcut[shortcutName = """ & shortcutName & """]")
			If nodeShortcut Is Nothing Then
				MsgBox("Shortcut/Link is no longer valid.")
				Exit Function
			End If
			
			strShortcutPath = nodeShortcut.selectSingleNode("shortcutPath").text
			If dxs.filFolderExists(strShortcutPath) Then
				MsgBox("Change folder shortcut icons with the other options.")
				
			ElseIf dxs.filFileExists(strShortcutPath) Then
				extractIcons = dxs.extractIconsFromFile(strShortcutPath,Object.Directory & "tempIcons")
				If extractIcons = False then
					MsgBox("Unable to extract icons from file.")
					Exit Function
				Else
					'read the files extracted to the tempIcons directory
					strImageFilePath = System.FileOpenDialog("Select Image:", "", Object.Directory & "tempIcons\", "All files|*.*", &H8)'dxEdit.conCreateImageSelector("selectImage",arrImages,imageSize)
					If strImageFilePath = "" Then
						Exit Function
					End If

					strImageFileExtension = dxs.filGetExtensionName(strImageFilePath)
					If Instr(1,"png,jpg,bmp,ico,gif,cur",strImageFileExtension,1) = 0 Then
						MsgBox("Please select an image file.")
						Exit Function
					End If

					'copy image to Object.Directory
					strNewImageFileName = shortcutName & "." & strImageFileExtension
					Call dxs.filFileCopy(strImageFilePath,Object.Directory,strNewImageFileName)
					strNewShortcutImageName = strNewImageFileName
				End If
				
				Call shortcutUpdate(shortcutName,"",strNewShortcutImageName)
			Else
				MsgBox("Linked file/folder can no longer be found.")
			End If
		
		Case 2
			Set nodeShortcut = xmlShortcuts.selectSingleNode("shortcuts/shortcut[shortcutName = """ & shortcutName & """]")
			If nodeShortcut Is Nothing Then
				MsgBox("Shortcut/Link is no longer valid.")
				Exit Function
			End If
			
			Set objShell = CreateObject("Shell.Application")
			strMyPicturesPath = objShell.Namespace(&h27).Self.Path
			strNewImageFilePath = System.FileOpenDialog ("Select an image file:", "", strMyPicturesPath, "All files|*.*", &H100000)
			If strNewImageFilePath = "" Then
				Exit Function
			End If
			
			strNewImageExtension = dxs.filGetExtensionName(strNewImageFilePath)
			If Instr(1,"png,jpg,bmp,ico,gif,cur",strNewImageExtension,1) = 0 Then
				MsgBox("Please select an image file.")
				Exit Function
			End If
			
			strNewImageFileName = shortcutName & "." & strNewImageExtension
			Call dxs.filFileCopy(strNewImageFilePath,Object.Directory,strNewImageFileName)
			Call shortcutUpdate(shortcutName,"",strNewImageFileName)
					
		Case 3
			subValue = result - 3001
			strPackageName = arrPackages(subValue)
			strNewImageFilePath = System.FileOpenDialog("Select Image:", "", strPackagesPath & "\" & strPackageName, "All files|*.*", &H8)'dxEdit.conCreateImageSelector("selectImage",arrImagePaths,imageSize)
			If strNewImageFilePath = "" Then
				Exit Function
			End If
			
			strNewImageExtension = dxs.filGetExtensionName(strNewImageFilePath)
			If Instr(1,"png,jpg,bmp,ico,gif,cur",strNewImageExtension,1) = 0 Then
				MsgBox("Please select an image file.")
				Exit Function
			End If
			
			strNewImageFileName = shortcutName & "." & strNewImageExtension
			Call dxs.filFileCopy(strNewImageFilePath,Object.Directory,strNewImageFileName)
			Call shortcutUpdate(shortcutName,"",strNewImageFileName)
			
		Case 4
			strNewShortcutPath = System.FolderDialog ("Please select a folder or file:", "", &H4000)
			If strNewShortcutPath = "" Then
				Exit Function
			End If
			Call shortcutUpdate(shortcutName,strNewShortcutPath,"")
		
		Case 5
			'call browse for shortcut function
			Call shortcutBrowse()
			
		Case 6
			'change image size value
			strCurrentSize = xmlShortcuts.selectSingleNode("shortcuts/@imageSize").text
			strNewIconSize = InputBox("Enter new icon size:","Icon options",strCurrentSize)
			If strNewIconSize <> "" and IsNumeric(strNewIconSize) Then
				minSize = dxs.Min(Array(Object.Width,257))
				If Int(strNewIconSize) > minSize Then
					MsgBox("Too large. Please enter a value less than " & minSize)
				Else
					imageSize = Int(strNewIconSize)
					xmlShortcuts.selectSingleNode("shortcuts/@imageSize").text = strNewIconSize
					Call refreshShortcuts()
				End If
			Else
				MsgBox("Please enter a number.")
			End If
		
		Case 7
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "0"
			Call refreshShortcuts()
			
		Case 8
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "1"
			Call refreshShortcuts()
		
		Case 9
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "2"
			Call refreshShortcuts()
			
		Case 10
			'change the layout type
			xmlShortcuts.selectSingleNode("@layoutType").text = "3"
			Call refreshShortcuts()
		
		Case 98
			Call shortcutDelete(shortcutName)
			
		Case 99
			Exit Function
			
	End Select
End Function




