Re: 2. Re: some newbie g_signal_connect questions



Matthias Mann wrote:

That's new for me. I used this "delete-event" more then one
times for exiting a Gtk+ application. But i never needed the
gboolean return value. Is there any reason to take an action
by asking if return value is TRUE or FALSE? Or is this only
for the event handler that is created with g_signal_connect()?

The gboolean return value of a delete_event-handler is only of use for
GTK+ itself. Remember, event handlers are usually being called by GTK+
only. So the return values are meant to be delivered to GTK+. GTK+ knows
how to act on return values of handlers. Your program is not supposed to
directly act upon returned values of event handler functions, since it's
not supposed to call event handlers directly.

While it's technically possible (from C's point of view) to call any event
handler from anywhere in a program, it usually makes no sense, is
inherently dangerous and can be considered bad coding style. A GTK+ event
handler has to expect that the event, which it should handle, has actually
happened.

There is no GTK+ event if you call the handler directly. And if your
handlers don't expect GTK+ events to have been occurred, then a handler is
likely not the right place for your piece of code. Don't mistake your GTK+
event handlers for libraries, as places to put frequently called functions
in!

For instance, when you click on the "Close"-field of a window the
"delete_event" is triggered (by GTK+) and your handler (if present) will
be called (before the window is closed!). The return value of your handler
will then tell GTK+ whether to allow the window to get closed or not. It's
a common practice to present users a "Do you really want to quit?"-
Yes-No-dialog when the main / last window of an application is to be
closed. Your delete_event-handler is the right place to initiate such a
dialog (if wanted).

Without a handler the window will always be closed immediately. With a
handler you have the chance to prevent the window from being closed, i.e.
because the user clicked "No" on the question above. Now it would make
absolutely no sense to call such a handler by yourself without a window
being about to be closed. The user would just be presented a "Do you
really want to quit?" dialog out of the context, but the window wouldn't
be closed, no matter what the user clicks. The situation is very similar
for most other types of event handlers.

What you'll rather want to do is to trigger the events that force an event
handler to get called by GTK+, instead of calling event handlers directly!

In the case of forcibly closing a window by your program this is a call to
"gtk_widget_destroy ((GtkWidget *) your_window);". The delete_event-
handler will then be triggered by GTK+, possibly presenting the user such
a dialog as described above. If you want such a dialog only if the user
(and not the program) choses to close a window then you can either set a
global flag which is tested by the handler and prevents it from presenting
the dialog or simply disconnect your delete_event-handler before calling
gtk_widget_destroy ().

 And by the way: a "void" function, does it need this
 'return;'? Or could i leave it generally?
    
It doesn't need it as its last statement. It's a matter of
coding style
    
Great! I was very confused. While finding out a solution for
some problems of my source code, i looked at sources of other
programmers and some had a 'return;' at end of callback
functions, others not. So i liked to know why.
    
Now my meaning is that it is better to have always a 'return;'
at end of all functions. I think this makes the source code
best readable and nobody needs to ask why this function has
no 'return;' while other functions have one.

Well, as soon as the the level of novice is passed, no C programmer will
wonder why at the end of some void functions there is a "return;" while on
others there is not. It's a very simple rule of many programming languages
(not only C) that after the last statement of a function the function will
return to its caller. Decisions like whether to use the expression "x = x
+ 1;", "x += 1;", "x++;' or "x += (3 >> 1);" (which all have the same
effect in C) are similar. However, you're still free to put "return;"s at
the end of your void functions.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]