The absolute beginners guide - Part II - for gnu-smalltalk/gtk (window closing callbacks)

Tagged:  •    •    •    •    •    •  

Let's start with enhancing the small app from the last step.

The problem that we are facing right now is, if we close the app through the window-managers 'close' button, the application (or better the GTK-Main-Loop) is still running and to end it, we have to press Ctrl-D.

Because I start the script from the command-line, I also would like to see, if I close the app, that the command line will come up again.

The solution seems, to create a method, that would be called, if the user presses the close button. We have to clean up the window and to close the app.

This seems implemented in GTK in 2 callbacks, the delete-callback for closing the window stuff and the destroy-callback for exiting the application (or the GTK-Main-Loop).

Therefore, I enhanced the initialize method from the last example with 2 lines of code:

window connectSignal: 'destroy' to: self selector: #destroy:data: userData: nil.
window connectSignal: 'delete_event' to: self selector: #delete:event:data: userData: nil.

There we call the method connectSignal for window with the identfier "destroy" (or delete_event) and say that the current object (self) is responsible for handling this callback. This callback should be routed to the method #destroy (or #delete) and has the parameters : and data:. (Currently, I have no Idea, what the userData is for, I found only a connectSignal method in the GTK.GObject defined and this method has no parameter userData...)

We also have to define the callback-methods, that the callbacks don't run into nirvana:

destroy: object data: data [
     GTK.Gtk mainQuit
]

delete: object event: event data: data [
     window destroy
]

And here you see again, the delete event is for closing the window and the destroy event for leaving the Gtk-endless-loop which was entered with the code-line GTK.Gtk main at the end of the MainWindow.st source-file.

For the main-window of an application, I think only the destroy-callback is neccessary. I would assume, that the delete-callback comes into play, if we work with additional dialogs, but that we will see later on in the future...

Important:
|-
|a lot of the example-code used here, can be found in the source-directory from gnu-smalltalk in the packages/gtk directory in the example_*.st files.
Sourcecode used in this examples:

Joachim.

Hello!

Nice Blog about Smalltalk and GTK binding for it, it is my starting point now to learn GUI programming on this pretty language

As you wrote,
>I have no Idea, what the userData is for

userData came from GTK origins.
As GTK is pure C library, in C GTK callbacks are standalone functions.
usrData pointer is only way to pass there something more that signal can bring itself - pointer to main window, etc.

Best regards,
Dmitry

Hello Dmitry,

thanks! And thanks again for describing that with the userData!

And even if it's not so frequent anymore as in the beginning, my plans are to continue ...

Regards,
Joachim.

I really appreciate your efforts. Easy to read, manageable targets, honest communication.

I'm learning a lot from your experience.

Hi Stefan,

thanks a lot for your comment!

The next few are already on my todo-list. (Yesterday I looked into menus and standard-dialogs...)

Regards,
Joachim.

User login