CTreeCtrl : How does state and statemaks works?

Hi!

    I want to read and set some states for a  tree item.  Now, how can I test the value of the flags.

For example, if I use the following : pTree->GetItemState(hItem, TVIS_EXPANDEDONCE) and catch the return code, how can I interpret this code?  I want to know if my item was expanded once.

Then, if I want to set the item state, using SetItemState, what the syntax to force expandedonce to be true, for example...

I also need to know how can I know which item is expanding, since GetSelectedItem returns the highlighted item, and the expanding item can be another one, when you push the +/- button on the left.

Thanks!
asavardAsked:
Who is Participating?
 
snoeglerCommented:
>> For example, if I use the following : pTree->GetItemState(hItem, TVIS_EXPANDEDONCE)
>> and catch the return code, how can I interpret this code?  I want to know if my item was
>> expanded once.

The return code of pTree->GetItemState(hItem,TVIS_EXPANDEDONCE) is
TVIS_EXPANDEDONCE if set(TRUE) or 0 if false. This is because the second parameter
(nStateMask) defines the mask which flags should be returned.
I.e. if nStateMask equals TVIS_EXPANDEDONCE|TVIS_DISABLED, the possible return
codes could be either TVIS_EXPANDEDONCE, TVIS_DISABLED, zero or
TVIS_EXPANDEDONCE|TVIS_DISABLED - you know what i mean?

>> Then, if I want to set the item state, using SetItemState, what the syntax to force
>> expandedonce to be true, for example...

If you want to expand a node, just do it like this:
pTree->SetItemState(hItem,TVIS_EXPANDED,TVIS_EXPANDED);
To un-expand a node:
pTree->SetItemState(hItem,0,TVIS_EXPANDED);
This is like in your first question: the last parameter is the mask which flags should be affected,
and the second param defines the values for these flags.
The flag TVIS_EXPANDEDONCE is - as far as i know, i've never used it - just a signal that
a child item of the specified item has been expanded. It has nothing to do with the actual
item - the actual item can be un-expanded and you could get this flag back though.

>> I also need to know how can I know which item is expanding, since GetSelectedItem returns
>> the highlighted item, and the expanding item can be another one, when you push the +/-
>> button on the left.

The point is that many nodes can be in expanded state - take a quick look in the Explorer
and open two or more directories and you'll see what i mean.
So to find out the expanded nodes you have to travel through the tree, finding out which nodes
have the flag 'TVIS_EXPANDED' set, and you have what you need. If you need assistance
or sample code about that, let me know it and i'll post it here.
0
 
asavardAuthor Commented:
Edited text of question
0
 
asavardAuthor Commented:
Adjusted points to 70
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
asavardAuthor Commented:
>The return code of pTree->GetItemState(hItem,TVIS_EXPANDEDONCE) is >TVIS_EXPANDEDONCE if set(TRUE) or 0 if false. This is because the second parameter >(nStateMask) defines the mask which flags should be returned. I.e. if nStateMask equals >TVIS_EXPANDEDONCE|TVIS_DISABLED, the possible return codes could be either >TVIS_EXPANDEDONCE, TVIS_DISABLED, zero or >TVIS_EXPANDEDONCE|TVIS_DISABLED - you know what i mean?
No I don't.  Is there a way to know the UINT value of a mask?

The damn thing returns 2 for the following :
UINT test = pTree->GetItemState(hSelectedItem, TVIS_EXPANDED);
UINT test2 = pTree->GetItemState(hSelectedItem, TVIS_EXPANDEDONCE);
The problem is that the item is neither Expanded or expanded once, so I'd expect a return code of 0.  What can the problem be?  I tried the setImageState the way you told me and called getImageState, and it still returned 2, even if I've set it to 0 on the previous line (and set returned OK).


0
 
asavardAuthor Commented:
For what I've come to understand, the goddamn mask doesn't seem to work.  I don't understand why.  Whatever mask I put on the string, the value is always the same.  What I would expect is that the value changes from the mask that I put, but no.  If the state is set to 98 (which is 2+32+64 as far as I understand how flags work), then every call to get item state returns 98.  But some value should return 0 (like TVIS_BOLD).  I tried 6 mask values and all returned the same value...
0
 
snoeglerCommented:
i'll check that
0
 
snoeglerCommented:
>> ... and it still returned 2, even if I've set it to 0 on the previous line (and set
>> returned OK).

This '2' value is the flag 'TVIS_SELECTED' and i assume that it can't be cleared - it
wouldn't make sense, though. Only 1 item can be selected at a time - see
CTreeItem::GetSelectedItem() and CTreeItem::Select().

>> I tried 6 mask values and all returned the same value...

It is not important if the mask works or not ... i think microsoft knew that and omitted
this point. Consider this:
Say, 'state' contains the current state of 'hItem'. Then a call to
GetItemState(hItem,TVIS_EXPANDED) should result in something like this:
  return (state&mask); // where mask equals TVIS_EXPANDED here
And you should test the return value of this function in the same way:
 mask=TVIS_EXPANDED; // Set here the flags to check
 unsigned okay=pTree->GetItemState(hItem,mask)&mask;
 if(okay==mask)
   // all flags in 'mask' are set
 else
   // at least 1 flag specified in 'mask' isn't set ...

You know what i mean? The (expected) behavior of GetItemState() and its 'mask' param
does not affect the result ... This is a little 'bug' in the GetItemState function, but if you
use the results in the right way, you'll never notice it.

// This are not all TVIS_ constants, but for here they're okay
#define TVIS_FOCUSED 1
#define TVIS_SELECTED 2
#define TVIS_CUT 4
#define TVIS_DROPHILITED 8

// set all flags
unsigned allflags=TVIS_FOCUSED|TVIS_SELECTED|TVIS_CUT|TVIS_DROPHILITED;
pTree->SetItemState(hItem, allflags, allflags);
// now the state of hItem has all the 4 above flags set

// now we want to clear TVIS_CUT
pTree->SetItemState(hItem, 0, TVIS_CUT);
// the mask is TVIS_CUT -> this means, that ONLY the bit 'TVIS_CUT' is affected by
// the value '0' - got it?
pTree->SetItemState(hItem,TVIS_FOCUSED,TVIS_CUT);
// this will *reset* theTVIS_CUT flag also - because TVIS_FOCUSED&TVIS_CUT == 0

Try to imagine that on the 'bit' layer - where a flag which is set equals to 1 and a reset
flag equals to 0.
If you need further explanation just post a comment, or if you like a sample .mdp dealing
with CTreeCtrl let me know it, i'll post it to you (but i'll need your email then).
I hope this helped ... :)
0
 
asavardAuthor Commented:
I had found out the bug myself, but I wanted to be sure I was right. Seems that I was.  This is stupid, though.  I just put zero as my mask in the getstate function and it still returned a value...  Well, thanks for your help, I'll manage to do the rest by myself.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.