Solved

CTreeCtrl : How does state and statemaks works?

Posted on 1998-08-11
8
2,408 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 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