What the usage of tryHandle and handle

View: New views
6 Messages — Rating Filter:   Alert me  

What the usage of tryHandle and handle

by Feng Feng :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hello all,

What the usage of tryHandle and handle is?

And, when I should use it?

// Somebody wants our the selection
long FXWindow::onSelectionRequest(FXObject*,FXSelector,void* ptr){
  return target && target->tryHandle(this,FXSEL(SEL_SELECTION_REQUEST,message),ptr);
  }

  // Update Alpha text fields
long FXColorSelector::onUpdAlphaText(FXObject* sender,FXSelector,void*){
  FXString value;
  if(isOpaqueOnly()){
    sender->handle(this,FXSEL(SEL_COMMAND,ID_HIDE),NULL);
    }
  else{
    ...
    }
  return 1;
  }


Thank you
-Feng


------------------------------------------------------------------------------

_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users

Re: What the usage of tryHandle and handle

by Jeroen van der Zijp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 23 June 2009, Feng Feng wrote:
> Hello all,
>
> What the usage of tryHandle and handle is?

All the widgets call the handlers through tryHandle(), not handle().

The reason is that we don't know what the handler code does; it MAY throw
an exception.

If it does throw an exception, tryHandle() catches the exception and then
returns normally.

Why is this done?  Because the logic of many widgets requires that code surrounding
the target->tryHandle() gets executed, in order to leave the widget in a known
state.

If we were to let an exception unroll without executing the surrounding code, we'd
risk leaving the widget in an intermediate state, and this would typically cause
problems later on.

At any rate, the idea is that prior to calling the handler, everything was still
OK, and thus trapping off the exception would be simply picking things up at the
last known-good state anyway.

Putting it in other words: if there is something you'd want to do when an exception
is thrown, you'd better do it prior to returning from the handler, or FOX will do
the exception processing for you.

The exception [pun intended] is we're only doing this with exceptions derived from
FXException.  Other types of exceptions are not caught by FOX, and would typically
cause your application to simply bomb.


                - Jeroen


------------------------------------------------------------------------------
_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users

Re: What the usage of tryHandle and handle

by Jeroen van der Zijp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 23 June 2009, Feng Feng wrote:

> Hello all,
>
> What the usage of tryHandle and handle is?
>
> And, when I should use it?
>
> // Somebody wants our the selection
> long FXWindow::onSelectionRequest(FXObject*,FXSelector,void* ptr){
>   return target && target->tryHandle(this,FXSEL(SEL_SELECTION_REQUEST,message),ptr);
>   }
>
>   // Update Alpha text fields
> long FXColorSelector::onUpdAlphaText(FXObject* sender,FXSelector,void*){
>   FXString value;
>   if(isOpaqueOnly()){
>     sender->handle(this,FXSEL(SEL_COMMAND,ID_HIDE),NULL);
>     }
>   else{
>     ...
>     }
>   return 1;
>   }

To elaborate slightly on the exception issue:

When FOX widgets call handlers in YOUR code, we do so with tryHandle().

When YOUR code invokes a update handler in FOX, you can use handle() since
FOX won't throw an exception anyway.  It is totally OK to call tryHandle()
if it pleases you, but it is not necessary...


                - Jeroen

------------------------------------------------------------------------------
_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users

Parent Message unknown Re: What the usage of tryHandle and handle

by Feng Feng :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
Hello Lyle,

Thank you for your excellent explanation for what the difference is.

Now, I have one more question related to the usage of the function,

For the code,
sender->handle(this,FXSEL(SEL_COMMAND,ID_HIDE),NULL);

Does it mean the following?

Use sender's handle to deliver the message SEL_COMMAND to object which is created with ID_HIDE.

If
FXMAPFUNC(SEL_COMMAND, XXX::ID_HIDE, XXX::onHide),

Then the function onHide will be called.

Is this correct?

Thank you
Feng













From: Lyle Johnson <lyle@...>
To: Feng Feng <askfoxtoolkit@...>
Sent: Tuesday, June 23, 2009 1:53:07 PM
Subject: Re: [Foxgui-users] What the usage of tryHandle and handle


On Jun 23, 2009, at 1:39 PM, Feng Feng wrote:

What the usage of tryHandle and handle is?

You call either handle() or tryHandle() to send a message to an object. Both methods expect the same arguments. The difference between them has to do with how they deal with exceptions.

The FOX library will raise runtime exceptions under certain conditions when a resource is unavailable. For example, if you're trying to scale an image and FOX isn't able to dynamically allocate the memory it needs to carry out that operation, it will throw an FXMemoryException. All of these resource-related exceptions are subclasses of the FXResourceException base class.

So if you're sening a message to an object using handle(), and if that results in an FXResourceException being thrown, you will need to deal with the exception in your code, e.g.

try {
obj->handle(this, FXSEL(SEL_xxx, message), ptr);
} catch (FXResourceException& ex) {
// handle it!
}

If you instead send the message using tryHandle(), FOX will catch any thrown exceptions for you, and return zero if an exception was thrown:

long result = obj->tryHandle(this, FXSEL(SEL_xxx, message), ptr);
if (result == 0) {
// the message was not handled, or possibly an exception was thrown
}

Hope this helps,

Lyle


------------------------------------------------------------------------------

_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users

Re: What the usage of tryHandle and handle

by Lyle Johnson-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Jun 23, 2009, at 3:46 PM, Feng Feng wrote:

Thank you for your excellent explanation for what the difference is.

Now, I have one more question related to the usage of the function,

For the code,
sender->handle(this,FXSEL(SEL_COMMAND,ID_HIDE),NULL);

Does it mean the following?

Use sender's handle to deliver the message SEL_COMMAND to object which is created with ID_HIDE.

You've basically got it right. I would have said it means, "ask 'sender' to handle a message from me (this), with message type SEL_COMMAND, message identifier ID_HIDE, and NULL message data".

If
FXMAPFUNC(SEL_COMMAND, XXX::ID_HIDE, XXX::onHide),

Then the function onHide will be called.

Yes, right.

------------------------------------------------------------------------------

_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users

Re: What the usage of tryHandle and handle

by Jeroen van der Zijp :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 23 June 2009, Feng Feng wrote:

> Hello Lyle,
>
> Thank you for your excellent explanation for what the difference is.
>
> Now, I have one more question related to the usage of the function,
>
> For the code,
> sender->handle(this,FXSEL(SEL_COMMAND,ID_HIDE),NULL);
>
> Does it mean the following?
>
> Use sender's handle to deliver the message SEL_COMMAND to object which is created with ID_HIDE.
>
> If
> FXMAPFUNC(SEL_COMMAND, XXX::ID_HIDE, XXX::onHide),
>
> Then the function onHide will be called.
>
> Is this correct?

Yes, that's correct.

All widgets support a few standard messages, like ID_HIDE, ID_SHOW, ID_ENABLE, ID_DISABLE.

These messages can be sent back from SEL_UPDATE handlers.

Valuator controls like FXSlider also understand messages like ID_INTVALUE, etc. which
allows setting value from SEL_UPDATE handlers and FXDataTarget.

This is a neat feature because the update handler code doesn't know what kind of widget
invoked the handler.  Thus, different widget types may get updated by the same handler
code.

This is basically how FXDataTarget works.



                        - Jeroen

------------------------------------------------------------------------------
_______________________________________________
Foxgui-users mailing list
Foxgui-users@...
https://lists.sourceforge.net/lists/listinfo/foxgui-users