Adding Advanced Checkbox

WEBINAR:On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

In the previous topic we covered how to implement a simple checkbox for each item in the tree control. In this topic we cover a slightly different implementation. The checkbox for each item will indicate whether the item is checked or not and it will also indicate whether any of its descendant is checked or not. The main reason for visually indicating whether a descendant is checked is that it is easier for the user to determine if there are checked items in the outline and if there is a checked item, it is easy to navigate to it.

Step 1: Create bitmap with checkbox images

Create a bitmap that will provide the images needed for the checkbox. The images in the bitmap below are 13x13 pixels. The first image is blank since this bitmap is going to be used for the state image list. The next image indicates an unchecked item. The third image indicates a checked item. The fourth image indicates an unchecked item but conveys that at least one of the descendant item is checked. The fifth image indicates a checked item and also indicates that at least one of the descendant item is checked.

Step 2: Initialize the state image list

Setting up state images has been covered in a previous topic. If the tree control class has a member variable m_imageState then here's how to set the image list.

Step 3: Insert items with the checkbox as a state image

If you are using TV_INSERTSTRUCT to insert an item into the tree control, then specify the state and the stateMask in the TV_ITEM member. The index of the state image is specified by using the macro INDEXTOSTATEIMAGEMASK. You can also use the SetItemState() function.

SetItemState( hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK );

Step 4: Add enumeration for check operation and state

Since we are dealing with four different check states, it makes the code more readable if we define an enumeration for the different states. We also define an enumeration to list the different operations on the check state.

Step 5: Define SetCheck() function

When we check an item, the parent items are also updated to indicate that a descendant is checked. Similarly, when we uncheck an item, the parents item have to be updated again. We may also need to update the parents state if an item is moved.

Depending on the value of nCheck, the function determines what the new state of the item should be. If nCheck is REFRESH, only the immediate children items are scanned to determine if the checkbox should be red ( to indicate that a child is checked ). The function then updates the parent items.

Step 7: Add code to OnKeyDown to toggle checkbox

This step provides the keyboard interface for checking and unchecking an item. The key used is usually the space key, so we will use the space key. The code is nearly the same as in the previous step. Instead of using HitTest() to determine the item handle, we use the function GetSelectedItem().

Step 8: Define helper functions

Define helper functions to determine whether an item is checked or not and to iterate through the checked items. The purpose of the functions is very clear from the function names. The GetFirstCheckedItem() and the GetNextCheckedItem() are optimized. They look at the item state before deciding whether it should search the children items. The GetPrevCheckedItem() function searches sequentially through all items. I leave it as an exercise to optimize this function.

Give it a go

By looking that many of you request the source code and yet it is (the source code) not posted, we should realize that this guy is not giving out one. i just feel sorry for your guy :-) he does not even reply or give comment to any the request. So why bother. I dont see it is that hard to implement that feature. Give it a go and you may learn more than what he says.

Top White Papers and Webcasts

Whether a mandate to secure all web and mobile apps comes from a newly enlightened CIO or in response to a major security breach, beginning even a small application security program can be a daunting task. How will you know how many digital assets you have, let alone their risk profile?
In this webinar we explore how, using a cloud solution like Fortify on Demand, even the largest organizations can begin to scan apps immediately and rapidly scale an application security program. Identify and risk rank assets, …

The software-defined data center (SDDC) and new trends in cloud and virtualization bring increased agility, automation, and intelligent services and management to all areas of the data center. Businesses can now more easily manage the entire lifecycle of their applications and services via the SDDC. This Aberdeen analyst report examines how a strong foundation in both the cloud and internal data centers is empowering organizations to fully leverage their IT infrastructure and is also preparing them to be able …