Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tree Control with Bitmap Checkboxes Supported

0.00/5 (No votes)
24 Oct 2002 2  
Adding a checkbox along with tree items

Sample Image - new_float.jpg

Introduction

I posted an article for float control several months ago. Somebody gave me a suggestion that I should add a checkbox in my control. I spade for an hour, and I found it is easy to do that. Here is my result. I got up today, "__Stephane Rodriguez__" (fakedwar@hotmail.com) sent me a mail. He suggested to change the title of this article to make it more accurate. Thank you. Anthony_Yio sent me a mail suggesting full selection of children and full un-checking of the children in the tree control as Office 2000 features. I have added the function to my tree control too.  

Bug Fixed

Stephane Rodriguez mentioned a bug in the click and expand of the tree. It is fixed in this version.

Implementation of Custom Tree Control

In the property page of tree control, there is a style named CheckBox. You shall implement your tree control from this point.

Firstly, you shall check CheckBox style in your resource where you want to use checkboxed tree control. Add a new bitmap resource in your project named IDB_STATE which includes 3 images: blank image, unchecked and checked box.

And then, we derive a new class from CTreeControl. Because this sample starts from my previous article, I used the same class as in the previous article. You shall first load the image for state using the following statement:

m_imageState.Create( IDB_STATE, 16, 1, RGB(255,255,255) );
SetImageList( &m_imageState, TVSIL_STATE );

The image will be used together with images for meanings. We shall respond to the Windows' message WM_LBUTTONDOWN to deal with check or uncheck of an item, as follows:

void CXMLTree::OnLButtonDown(UINT nFlags, CPoint point) 
{
   UINT uFlags=0;
   HTREEITEM hti = HitTest(point,&uFlags);

   if( uFlags & TVHT_ONITEMSTATEICON )
   {
      ToggleItemState(hti);

//  int iImage = GetItemState( hti, TVIS_STATEIMAGEMASK )>>12;
//  SetItemState( hti, INDEXTOSTATEIMAGEMASK(iImage == 1 ? 2 : 1), 
//               TVIS_STATEIMAGEMASK );
   return;
   }
   //The following line is needed for our program to run smoothly.
   CTreeCtrl::OnLButtonDown(nFlags, point);
}

In the standard Windows implementation of a control, you shall use space key to toggle the state of check box. So, we need to respond to WM_KEYDOWN for space key.

   if( nChar == VK_SPACE )
   {
      HTREEITEM hti = GetSelectedItem();
      ToggleItemState(hti);
//      int iImage = GetItemState( hti, TVIS_STATEIMAGEMASK )>>12;
//      SetItemState( hti, INDEXTOSTATEIMAGEMASK(iImage == 1 ? 2 : 1), 
//               TVIS_STATEIMAGEMASK );
      return;
   }
   CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);

We add a new function to support full selection of child items. The pros functions are changed to support it. Following is the function added:

void CXMLTree::ToggleItemState(HTREEITEM hti)
{
   int iImage = GetItemState( hti, TVIS_STATEIMAGEMASK )>>12;
   SetItemState( hti, INDEXTOSTATEIMAGEMASK(iImage == 1 ? 2 : 1), 
            TVIS_STATEIMAGEMASK );
   if ( ItemHasChildren(hti))
   {
      HTREEITEM htiChild = this->GetChildItem (hti);
      if (htiChild)
         ToggleItemState(htiChild);
      else
         return ;
      HTREEITEM htiSibling = GetNextSiblingItem (htiChild);
      while (htiSibling )
      {
         ToggleItemState(htiSibling);
         htiSibling = GetNextSiblingItem(htiSibling);
      }
   }
}

We will implement several helper functions to get or set the state of checkbox in the tree control attached to the tree items. If it is check box, it means multiple selection, we shall iterate all items checked. We also provide the function for iteration. But these functions have not been thoroughly tested.

BOOL CXMLTree::IsItemChecked(HTREEITEM hItem)
{
   return GetItemState( hItem, TVIS_STATEIMAGEMASK )>>12 == 2;
}

HTREEITEM CXMLTree::GetFirstCheckedItem()
{
   for ( HTREEITEM hItem = GetRootItem(); 
     hItem!=NULL; hItem = GetNextItem( hItem ) )
      if ( IsItemChecked(hItem) )
         return hItem;

   return NULL;
}

HTREEITEM CXMLTree::GetNextCheckedItem(HTREEITEM hItem)
{
   for ( hItem = GetNextItem( hItem ); 
     hItem!=NULL; hItem = GetNextItem( hItem ) )
      if ( IsItemChecked(hItem) )
         return hItem;

   return NULL;
}

HTREEITEM CXMLTree::GetPrevCheckedItem(HTREEITEM hItem)
{
   for ( hItem = GetPrevItem( hItem ); 
     hItem!=NULL; hItem = GetPrevItem( hItem ) )
      if ( IsItemChecked(hItem) )
         return hItem;

   return NULL;
}

void CXMLTree::SetChecked(HTREEITEM hItem)
{
   SetItemState( hItem, INDEXTOSTATEIMAGEMASK(1), 
                              TVIS_STATEIMAGEMASK );
}
// GetNextItem   - Get next item as if outline was completely expanded
// Returns      - The item immediately below the reference item
// hItem      - The reference item

HTREEITEM CXMLTree::GetNextItem(HTREEITEM hItem)
{
   HTREEITEM   hti;

   if( ItemHasChildren( hItem ) )
      return GetChildItem( hItem ); // return first child
   else{
      // return next sibling item
      // Go up the tree to find a parent's sibling if needed.
      while( (hti = GetNextSiblingItem( hItem )) == NULL ){
         if( (hItem = GetParentItem( hItem ) ) == NULL )
            return NULL;
      }
   }
   return hti;
}

HTREEITEM CXMLTree::GetPrevItem(HTREEITEM hItem)
{
   HTREEITEM hti;

   // Return a previous sibling item if it exists
   if( hti = GetPrevSiblingItem(hItem) )
      return hti;
   else
      // No sibling, return the parent 
      return GetParentItem(hItem);
}

A new function is added to tree control. If you select the item, all of its children are selected. So it is done for uncheck actions. See insertItem function.

Using this new control is very simple. Add a tree control in your dialog box and set the checkbox style. The other operations are same as other controls. Enjoy it!

Acknowledgement

Thanks to Anthony_Yio and Stephane Rodriguez. They gave me good suggestions and bug reports. Thanks to Prettybabe Prettybabe for raising the idea to write such a control.

History

  • October 25th, 2002: Version 1.0
    • Fixed bugs in SetChecked - the state is error
    • Fixed bugs for mouse down to populate nodes
    • Added new functions for full selection and deselection
    • Added new function: SetUnChecked
  • October 24th, 2002: Version 0.9. initial

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here