[Gambas-user] A few questions

Benoit Minisini gambas at ...1...
Tue Sep 23 23:20:36 CEST 2008


On mardi 23 septembre 2008, Doriano Blengino wrote:
> Hi,
>
> I am facing a series of little problems (gambas v. 2.0.0).
>

You should use a more recent version of Gambas, otherwise what I will say may 
not work.

> I made a fmPreview which, by raising a "getpage" event, gets data from
> the main program and displays them, and/or actually prints them on a
> printer.
> fmPreview is a form with a few controls in it. fmPreview itself raises a
> "getpage" event, which is to be catched by the main program in order to
> send data to print. fmPreview catches events from its own controls
> (buttons, form events like resize and so on).
>
> The main initiates a preview/print operation by doing this:
>
>   fmPreview.setup("caricopriv", 1350, 2300)		' name of the print and page
> dimensions
>
>   fmPreview.obs = NEW Observer(fmPreview) AS "preview"
>
>   reply = fmPreview.showmodal()				' start operation
>
>
> The observer is needed because, without it, the "instantiating" program
> can not get the event to send page data.
> When fmPreview terminates its duty, in its close() methods, it issues
> these instructions:
>
>   object.Detach(obs)
>   obs = NULL
>
> The previous two instructions should get rid of the observer. This is
> needed because otherwise every time the main program opens up an
> fmPreview, a new observer has to be created.
>
> My questions are:
>
> 1) Is this the correct way to destroy an observer? I did not find
> another way - both instructions are needed, it seems. I hoped an
> "Object.destroy(...)" was present...

You don't decide. Gambas decides.

More precisely, an object is destroyed when nothing references it. The only 
thing you can do in your program is not holding a reference on it, by setting 
NULL to your 'obs' variable for example. 

But it does not mean that a reference is not internally store somewhere else. 
For example, if you do not do the Object.Detach(obs), fmPreview will hold a 
reference on it (any object hold a reference on its observers).

>
> 2) I tried to not to use an observer, but attach/detach seem to catch
> all events involved in fmPreview - both the ones generated by fmpreview
> and the ones generated by fmPreview child controls.

No. An observer catches the events sent by one object only.

>
> 3) I tried to instantiate an observer directly from fmPreview, instead
> of the main program, and then object.attach() it to the main program,
> but it didn't work.

You cannot "Object.Attach() to the main program". A default observer (badly 
named Parent in Gambas) is not a program, but another object.

In the case of a static class (a module), the observer is the class object.

>
> 4) Probably, using "hPreview = NEW fmPreview(...) as "preview" would
> solve, but I would try to understand better how to use an observer,
> attach/detach and so on. My goal would be to instantiate fmPreview with
> only two lines of code (ok, not a so great deal...), or just one line:
> the event name to be raised could be the same as the title of the print.

A form is its own event observer. If you want a form to raise other events to 
other observer, I suggest you think again on how things are encapsulated in 
your application.

>
> 5) Does TableView raise keypress events when in edit mode? I see
> nothing, but I would need it to do certain things. Is there another way
> to do a controlled editing in a tableview?

TableView is made in Gambas, so you just have to look at the sources in 
gb.form to understand how it works. It mainly inherits GridView and uses 
Observers (and the Watcher class) to do its job. 

And, no, you won't see the keyboard events while editing, as they are caught 
by the internal editor widget. In that specific case, we could imagine adding 
a property to get a reference on the editor widget so that you can put your 
own event observer on it to catch the keyboard events.

Regards,

-- 
Benoit Minisini




More information about the User mailing list