Solved

CTreeCtrl : How does state and statemaks works?

Posted on 1998-08-11
8
2,457 Views
Last Modified: 2009-09-22
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!
0
Comment
Question by:asavard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:asavard
ID: 1169999
Edited text of question
0
 

Author Comment

by:asavard
ID: 1170000
Adjusted points to 70
0
 
LVL 6

Accepted Solution

by:
snoegler earned 70 total points
ID: 1170001
>> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:asavard
ID: 1170002
>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
 

Author Comment

by:asavard
ID: 1170003
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1170004
i'll check that
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1170005
>> ... 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
 

Author Comment

by:asavard
ID: 1170006
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question