Link to home
Start Free TrialLog in
Avatar of asavard
asavard

asked on

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!
Avatar of asavard
asavard

ASKER

Edited text of question
Avatar of asavard

ASKER

Adjusted points to 70
ASKER CERTIFIED SOLUTION
Avatar of snoegler
snoegler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of asavard

ASKER

>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).


Avatar of asavard

ASKER

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...
i'll check that
>> ... 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 ... :)
Avatar of asavard

ASKER

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.