[Gambas-user] Super Events. Are they posible?

Tobias Boege taboege at ...626...
Thu Feb 13 02:46:07 CET 2014


On Wed, 12 Feb 2014, Jorge Carri?n wrote:
> Oh, Fabien... I don't understand.
> At the begining of the fMain class in a simple proyect with a textbox in
> the fMain form. and a second form, Helpform, that  just contains a label.
> I've declared this
> 
> Static Public Sub Application_Read()
> 
>     Dim f As Helpform
> 
>     If key.code = Key.F1 Then
>         f = New Helpform
>         f.showmodal
>     Endif
> 
> End
> 
> But the Application_Read sub never is called....
> I don't understand.
> The doc on gambasdoc is not very clear. it's suppoused Application_Read
> catch the Standard Input... ?From the Shell? ?From the keyboard? I don't
> understand...
> Is ask too much ask for a little example?
> Thanks in advance.
> 

Application_Read() watches stdin which is not what you need.

Actually, I don't think there is a good solution. Objects are bound to a
single "default" or "main" observer (occasionally called its "parent" but
this is misleading when we talk about control containers which are also
called "parents" but aren't necessarily main observers) which intercepts
events of the observed objects. Normally (when you use the form editor)
controls are bound to the form they are put on. That's why you can write
something like Public Sub btnClose_Click() in your form's class file for a
Button btnClose on the form.

In addition to the main observer, you can create other Observer objects
(Observer is a class in the gb component) which are... additional observers
and duplicate events raised by the observed object. Or you can attach an
object to another main observer at runtime using Object.Attach().

However, if you use the latter method, the event handlers you wrote in your
form code will not work anymore. So we will have to use the Observer stuff
which lets us catch event duplicates behind the scenes.

This technique requires to create an observer for *each* control that you
want to incorporate into your help system... or (!) we can create only *one*
observer per Form instance and use some really black magic called the Grab()
method whose existence is very fortunate here...

Let's consider the second, obviously more efficient, approach. This would
require to add an observer for each of your forms. To automate this task, we
need a way to hook the creation of forms without imposing the need to change
any existing code (which should be our ultimate goal). This is normally done
by overriding a class in the global symbol table. Then we can "hook" the
_new() special method and register the newly created form with our help
system which will create an observer and watch the window.

In the attached POC code, I have deliberately used a Control's Tag property
to store the help text. It's up to you to make it better. Then, in the new
Form class, a new observer for the form is created with the form itself. If
the user presses F1, the form goes into "help mode" which means that the
form will grab any user input (from mouse and keyboard) of its controls so
that, e.g., Buttons don't raise Click events anymore but instead the form
raises a MouseUp event. Help mode is indicated visually by the mouse being
set to Mouse.Pointing. Then we can display the help on the form observer's
MouseUp event handler.

I hope the project helps. At least it satisfies me because as you will see,
you don't need to change any existing code, but only override the Form
class.

And in case you're still curious: the first approach (creating observers for
every control) would *not* work the same way (if at all) because you cannot
override the Control class. This yielded an error I have never seen before
about the inability to override a class which is already inherited or the
like. Likely, my POC code will not work in your project if you have any
classes derived from Form.

Thanks for that interesting question ;-) We'll definitely write an article
about this topic on gambas-buch.de. Do you mind if I mention your name?

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: super-event-poc-1-0.0.1.tar.gz
Type: application/octet-stream
Size: 5988 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20140213/d66dadc3/attachment.obj>


More information about the User mailing list