ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
part 9


8 - Exclusive Code

Now we've given each object an awareness of all others, how does this provide exclusivity? For that, we need to intercept button presses with our MsgHandler() function:
  S32 cpaRadioButton::MsgHandler(MSG_TYPE type,CViewable*from,S32 data)
  {
    switch (type)
    {
      case MSG_PEN_DOWN:
      case MSG_PEN_DOUBLECLICK:
        SetDownStatus(true);
        // send message to parent
        GetParent()->MsgHandler(MSG_BUTTON_SELECT,this,true);
        break;
      case MSG_PEN_UP:
      case MSG_PEN_TRACK:
        // ignore these
        break;
      default: 
        // all others messages processed
        // by CRadioButton class as usual
        return CRadioButton::MsgHandler(type,from,data);
    }
    return 1;
  }
Our code is very straightforward in that we really only need to monitor for button down events (our PEN messages). We then call SetDownStatus() and send a message to our parent so it behaves like a normal button. The parent message has available to it the state (always on), as well as the button object pointer, which gives it access to the object's tag. The tag value is a carryover from my programming on other systems, where I've found a tag value a handy place to associated numbers with objects. For instance, the tag could contain a value associated with each choice, making relating the button choice to the end result that much easier (using Tag(value) to set it in initialization).

If we happen to want to access the radio buttons outside of the MsgHandler() call, there is a static function cpaRadioButton::GetGroupDownButton(int group), which returns a pointer to the current 'down' button in the specified group.

Tying all this together is the SetDownStatus() call, which walks the linked list looking for group id matches (if positive - negative groups are not allowed), and turning off all in the group except the current item:
  void cpaRadioButton::SetDownStatus(BOOL status)
  {
    if (false==status) // trying to 'unselect'? Don't allow that
      return;
    if ( m_groupId>=0 ) // valid group id?
    {
      // walk list, turning off all others with identical ids
      cpaRadioButton*ptr=s_firstObject;
      while ( NULL!=ptr )
      {
        // check if from our group and down - if so, set up
        if ( ptr->m_groupId==m_groupId && ptr->GetDownStatus() )
          ptr->CRadioButton::SetDownStatus(false);
        ptr=ptr->m_nextObject;
      }
    }
    CRadioButton::SetDownStatus(status);
  }
As the final part of the example program's display, we have the bottom showing three columns of three radio buttons, with each column allowing a single 'on' button.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice