Finding an item (matching any member of TV_ITEM)

FindNextItem traverses a tree searching for an item matching all the item attributes set in a TV_ITEM structure. In the TV_ITEM structure, the mask member specifies which attributes make up the search criteria. If a match is found, the function returns the handle otherwise NULL. This function uses the function GetNextItem, seen previously. The function only searches in one direction (down) and if hItem is NULL starts from the root of the tree.


HTREEITEM CTreeCtrlEx::FindNextItem(TV_ITEM* pItem, HTREEITEM hItem)
{
   ASSERT(::IsWindow(m_hWnd));
   
   TV_ITEM hNextItem;

   //Clear Item data
   ZeroMemory(&hNextItem, sizeof(hNextItem));
      
   //The mask is used to retrieve the data to compare
   hNextItem.mask = pItem->mask;
   hNextItem.hItem = (hItem) ? GetNextItem(hItem) : GetRootItem();
   
   //Prepare to compare pszText
   //Testing pItem->pszText protects the code from a client setting the
   //TVIF_TEXT bit but passing in a NULL pointer.
   if((pItem->mask & TVIF_TEXT) && pItem->pszText)
   {
      hNextItem.cchTextMax = strlen(pItem->pszText);

      if(hNextItem.cchTextMax)
         hNextItem.pszText = new char[++hNextItem.cchTextMax];
   }

   while(hNextItem.hItem)
   {
      if(Compare(pItem, hNextItem))
      {         
         //Copy all the information into pItem and return
         memcpy(pItem, &hNextItem, sizeof(TV_ITEM));

         //Free resources
         if(hNextItem.pszText)
            delete hNextItem.pszText;
         
         return pItem->hItem;
      }

      //The mask is used to retrieve the data to compare and must be
      //reset before calling Compare
      hNextItem.mask = pItem->mask;
      hNextItem.hItem = GetNextItem(hNextItem.hItem);
   }   
   
   //Set hItem in pItem
   pItem->hItem = NULL;

   //Free resources
   if(hNextItem.pszText)
      delete hNextItem.pszText;
   
   return NULL;
}

BOOL CTreeCtrlEx::Compare(TV_ITEM* pItem, TV_ITEM& tvTempItem)
{
   //This call uses the .mask setting to just retrieve the values
   //that the client wants to compare.
   //Get all the data passed in by pItem
   GetItem(&tvTempItem);

   //Reset the mask so I can keep track of the matching attributes
   tvTempItem.mask = 0;

   if((pItem->mask & TVIF_STATE) && 
      (pItem->state == tvTempItem.state))
      tvTempItem.mask |= TVIF_STATE;

   if((pItem->mask & TVIF_IMAGE) && 
      (pItem->iImage == tvTempItem.iImage))
      tvTempItem.mask |= TVIF_IMAGE;

   if((pItem->mask & TVIF_PARAM) && 
      (pItem->lParam == tvTempItem.lParam))
      tvTempItem.mask |= TVIF_PARAM;

   if((pItem->mask & TVIF_TEXT) &&
      pItem->pszText && tvTempItem.pszText && //Don't compare if either is NULL
      !strcmp(pItem->pszText, tvTempItem.pszText))
      tvTempItem.mask |= TVIF_TEXT;

   if((pItem->mask & TVIF_CHILDREN) && 
      (pItem->cChildren == tvTempItem.cChildren))
      tvTempItem.mask |= TVIF_CHILDREN;

   if((pItem->mask & TVIF_SELECTEDIMAGE) && 
      (pItem->iSelectedImage == tvTempItem.iSelectedImage))
      tvTempItem.mask |= TVIF_SELECTEDIMAGE;
   
   //If by this point these two values are the same.
   //tvTempItem.hItem is the desired item
   return (pItem->mask == tvTempItem.mask);
}


Date Last Updated: April 24, 1999



Comments

  • Doesn't traverse

    Posted by Legacy on 11/04/2002 12:00am

    Originally posted by: Raymond Parsons

    This works well, although it doesn't provide any support for traversing into child items.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds