[CONNECT C++] DialogItemHookHandler Lifetime

I found the DialogItemHookHandler and DialogHookHandler classes tucked away in a corner of the MicroStationAPI help doc.  They simplify the task of writing a dialog or dialog item hook (a.k.a. callback function).  The classes provides virtual callback methods that elegantly replace the switch statements we otherwise have to write in a hook function.

What's not clear is the lifetime management of a class that inherits from DialogItemHookHandler or DialogHookHandler.  The class is instantiated in the help examples by a static factory method...

static void HookResolve (DialogItemMessage *dimP)
{
  if (DITEM_MESSAGE_HOOKRESOLVE == dimP->messageType)
  {
    dimP->u.hookResolve.hookHandlerP = new MyItemHookHandler (dimP->db, dimP->dialogItemP);
    dimP->msgUnderstood = true;
  }
}

What is the lifetime of the Hook Handler created by that factory method?  Presumably it's destroyed when the dialog or item terminates.  But the destruction mechanism is not, as far as I can see, published.  Could I use an object that's instantiated elsewhere?  That is,

dimP->u.hookResolve.hookHandlerP = MyExistingHookHandler;
MyExistingHookHandler.SetPointers (dimP->db, dimP->dialogItemP);

But, if I do that, then the Hook Handler destruction mechanism will attempt to destroy something that it doesn't own.

Why do I ask this question?  Elegant though the new classes may be, then don't eliminate the distributed nature of dialog item functionality.  Some functionality is built-in to MDL dialog items.  Other functionality we add using these hook classes.  But, the classes remain distinct from one another.  For example, I have a dialog box that contains several dialog items.  I want to control the visibility of some items depending on the state of a ComboBox.  I have a DialogItemHookHandler class for the ComboBox and a DialogHookHandler for the dialog box.  On the ComboBox QueueCommand event, I tell the dialog to hide some items and reveal others.  That all works fine, but I'm having to use mdlDialog_xxx functions to send a message from the ComboBox handler to the DialogHookHandler.  That works, but I have two distinct classes filled with comments about who does what and to whom.

It would be useful to have, say, a class hierarchy that enabled me to say that the ComboBox handler class is subordinate or contained within the dialog handler class.  I don't see a way to do that right now.

Parents Reply Children
  • Unknown said:
    The associated DialogItemHook, which is owned by the Dialog Manager

    Is there any way to obtain a pointer to a hook class from the Dialog Manager?  That would facilitate passing data between the various widgets in a dialog.

    For example, if my ListBox is part of the dialog, its hook class might hold data that is useful to other classes handling other user items.  Currently, data has to be passed via global variables external to the hook classes.

    It would be tidier to be able to retrieve a hook class pointer from the Dialog Manager, in much the same way as, say, a ListBox can get its ListModel pointer using mdlDialog_listBoxGetListModelP().  If that were so, then one could write something like …

    //  Hook Handler class for LISTBOXID_MyListBox
    struct MyListBoxHandler : DialogItemHandler
    {
      MyData data_;
      MyDataCR GetData () const { return data;  }
      ...
      
    };
    

    struct MyWidgetHandler : DialogItemHandler
    {
      ...
      void DoSomething ()
      {
         DialogItemP  listBox = mdlDialog_itemFindByTypeAndId (GetDialog (), 
                                   RTYPE_ListBox, LISTBOXID_MyListBox, 0);
         //  The following function is a product of my imagination
         DialogItemHandlerCP listBoxHandler = 
              dynamic_cast<MyListBoxHandler*>(mdlDialog_itemGetHookHandler (GetDialog (), listBox));
         if (listBoxHandler)
         {
           MyDataCR data = listBoxHandler->GetData ();
         }
      }
      ...
    };
    

    The above requires a new public function having this signature …

    
    DialogItemHandler const* mdlDialog_itemGetHookHandler (MSDialogP db, DialogItemP item);
    

     
    Regards, Jon Summers
    LA Solutions