Easier way to create a ModalCallback for "this"?

Hey guys,

Is there really no easier (out of the box) way than this to set up my component as a listener for an async menu than this?

class MyComp : public Component
{
   static void MyCallback(int modalReturn, MyComp* component)
   {
      component->OnMenuClicked(modalReturn);
   }

   void OnMenuClicked(int modalReturn)
   {
      // do something here!
   }

   void ShowMenu()
   {
        PopupMenu menu;
        menu.addItem(1, "Hello Menu");
        menu.addSeparator();
        menu.addItem(2, "Cancel");
        menu.showMenuAsync(PopupMenu::Options().withTargetComponent(this), ModalCallbackFunction::forComponent(MyCallback, this));
   }
}

This seems very complex for just making a menu non-modal :smiley:

  • bram

C++11 lambdas will be good for this, but until everyone has a C++11 compiler it’s not something I can really use in the library. You could use lambdas yourself though, if you’re only targeting a modern compiler.

Hmm, true, but I suppose I was more thinking along the lines of:

class MenuCallback
{
   void OnMenuModalReturned(int state) = 0;
}

class MyComp : public MenuCallback
{
   void ShowMenu()
   {
        PopupMenu menu;
        menu.addItem(1, "Hello Menu");
        menu.addSeparator();
        menu.addItem(2, "Cancel");
        menu.showMenuAsync(PopupMenu::Options().withTargetComponent(this), SOMETHING(this));
   }

   void OnMenuModalReturned(int state)
   {
       // do something
   }
}

I.e. more just like a regular listener… I suppose you wouldn’t like that?

  • Bram

You could write a little re-usable class yourself that would do that - in my callback code I was just trying to keep things more flexible, I didn’t want to have specialised classes for all the possible things you could use it for.

Okay!

  • bram