Get Treeview Node Icon?

BlitzMax Forums/MaxGUI Module/Get Treeview Node Icon?

Chalky(Posted 2008) [#1]
I am writing a programme which catalogues DVD/CD data disks so that the user can search for archived files and know which DVD/CD they are on. To do this I read the directory structure and store it in a Treeview in traditional Windows Explorer style - setting the icon value for each node according to whether it's a drive, directory or file.

I would like to be able to read the icon value of a Treeview node in order to:

1) know when to toggle an icon between an open-folder and closed-folder using ModifyTreeViewNode when the user double clicks a folder

2) save each node's icon value when writing the Treeview node structure to the database [I currently do this by keeping track of each node's icon in a separate array, but this is not very elegant as the info I want is already stored within the Treeview structure - and I am therefore duplicating data]

I have been unable to find a way to retrieve the icon value of a Treeview node. Can anyone please tell me whether this is possible?


Blueapples(Posted 2008) [#2]
For windows. I'm not sure how much of this header you'll need, don't have a lot of time to filter it out for you, but the two functions below should do what you want.



The functions:

Function TreeViewGetNodeImage:Int(TreeView:TGadget, Node:TGadget)
	Local Item:TVITEM = New TVITEM, pItem:Byte Ptr = Item
	Local hwndView:Int, hwndNode:Int

	hwndView = QueryGadget(TreeView, QUERY_HWND)
'	hwndNode = QueryGadget(Node, 1)
	hwndNode = SendMessage(hwndView, TVM_GETNEXTITEM, TVGN_CARET, 0)

	Print "hwndView = " + String(hwndView) + " hwndNode = " + String(hwndNode)
	Item.hItem = hwndNode
	Item.Mask = TVIF_IMAGE
	
	SendMessage(hwndView, TVM_GETITEM, 0, Int(pItem))
	Return Item.iImage
EndFunction

Function TreeViewSetNodeImage:Int(TreeView:TGadget, Node:TGadget, Image:Int)
	Local Item:TVITEM = New TVITEM, pItem:Byte Ptr = Item
	Local hwndView:Int, hwndNode:Int
	
	hwndView = QueryGadget(TreeView, QUERY_HWND)
'	hwndNode = QueryGadget(Node, 1)
	hwndNode = SendMessage(hwndView, TVM_GETNEXTITEM, TVGN_CARET, 0)


	Print "hwndView = " + String(hwndView) + " hwndNode = " + String(hwndNode)
	Item.hItem = hwndNode
	Item.Mask = TVIF_IMAGE
	Item.iImage = Image

	SendMessage(hwndView, TVM_SETITEM, 0, Int(pItem))
	Print "Set icon to " + Item.iImage
	Return Item.iImage
EndFunction


I might of gotten these somewhere on this forum - might not have. I don't have a note of it but it is in a pile of Win32 API code that I have gotten from various places and written myself. Apologies if this is verbatim copied without attribution!

You'll want to remove the Print lines from these in production code.


Chalky(Posted 2008) [#3]
Many thanks Blueapples - that will work fine for changing an icon image when the user selects a node (in which case the Node parm in the function definition is not required).

Since TVGN_CARET is used to return a handle to the currently selected node, I cannot use the same code to retrieve icon values for all nodes in the treeview. What I need is a similar function to which I can pass a node - but I couldn't seem to get the supplied one to work using the Node parm as hwnd=QueryGadget(Node,QUERY_HWND) always returns zero.
I will keep trying...


Chalky(Posted 2008) [#4]
Well I managed to get it to work using SelectTreeViewNode but this auto-expanded all the nodes in the treeview. This wouldn't matter if I could turn off the refresh so that this was not visible as I could re-collapse them all once I had finished processing them. I seem to remember finding an API during my VB days which disabled visual control-updates - can anyone confirm whether such an API (or SendMessage parm) exists?

[edit]
Found it - LockWindowUpdate. All sorted now :o)


Blueapples(Posted 2008) [#5]
I hadn't noticed that since I always used these functions to work with the currently selected node.

Does this work instead? I don't have any code using this currently so I can't really try it...

	
	Local Item:TVITEM = New TVITEM, pItem:Byte Ptr = Item
	Local hwndView:Int, hwndNode:Int
	
	hwndView = QueryGadget(TreeView, QUERY_HWND)
	hwndNode = QueryGadget(Node, QUERY_HWND)

	'or maybe uncomment this - not sure why it was commented out, since it appears
	'to try to do the same thing
	'hwndNode = QueryGadget(Node, 1)



Chalky(Posted 2008) [#6]
I did try that, but hwndNode=QueryGadget(Node, QUERY_HWND) returns zero every time. There probably is a way to do it but I don't have enough knowledge on treeview structures to work out how. It would be extremely useful to be able to access any treeview node's data without having to select it first.