[haiku-3rdparty-dev] Re: Sending my own customized message

  • From: Rene Gollent <anevilyak@xxxxxxxxx>
  • To: haiku-3rdparty-dev@xxxxxxxxxxxxx
  • Date: Tue, 1 Dec 2009 23:27:40 -0600

On Tue, Dec 1, 2009 at 11:17 PM, Yashasvi A.C. <yashasviac@xxxxxxxxx> wrote:
> Hi!
>
> I am trying to create my own message and trigger it when the user interacts
> with a control. I am using:
>
> BMessenger myMessenger;
> BMessage myMessage('YASH');
> myMessenger.SendMessage(&myMessage, NULL);

This won't work as you haven't set a target. To do so you must pass
either a pointer to a valid BHandler in the same team or the signature
of another team. For instance, if you want to send the message to the
window the control belongs to, then, assuming this code is executing
from a function belonging to the control or some other view belonging
to the same window as the control, you would do:
BMessenger myMessenger(Window());

As BWindow is itself a BHandler this is perfectly ok. Also note that
BMessenger has an IsValid() method that can be used to check that it's
in fact pointing at a meaningful target or if the message will be
going nowhere. Also note that reply's default value is already NULL,
so passing that is unnecessary. If all you want to send is a message
code without any more complex content for the message, you can also
simply do:

BMessenger myMessenger(Window());
if (myMessenger.IsValid())
  myMessenger.SendMessage('YASH');

See also http://www.haiku-os.org/legacy-docs/bebook/BMessenger.html
which covers this in quite a bit more detail.

HTH,

Rene

Other related posts: