Lets take another look at the signal_connect prucedure. After calling it you can inspect feature: last_signal_id: INTEGER; defined in the class VEGTK_CALLBACK_HANDLER.
This is a tag that identifies your last callback procedure installed. As stated above, you may have as many callbacks per signal and per object as you need, and each will be executed in turn, in the order they were attached.
This tag allows you to remove this callback from the list by using:
signal_disconnect(widget : GTK_OBJECT;sig_id : INTEGER)
So, by passing in the widget you wish to remove the handler from, and the tag taken after one of the calls to signal_connect procedure, you can disconnect a signal handler.
Another procedure defined in GTK_OBJECT and thus available in all widgets is:
remove_all_signal_handlers;
This call is fairly self explanatory. It simply removes all the current signal handlers from the object .
Let's take a look at a slightly improved helloworld with better examples of callbacks. This will also introduce us to our next topic, packing widgets.
-- example-start helloworld2 helloworld2.e
class HELLOWORLD2
inherit
-- All constants we need are defined in the class below.
-- eg gtk_window_toplevel etc.
GTK_CONSTANTS
-- This class contains common features like vegtk_init and so
VEGTK_MAIN
-- If we need to handle callbacks we must inherit from the below
-- class
VEGTK_CALLBACK_HANDLER
creation
make
feature -- Callback handlers
callback (data : ANY) is
-- Our new improved callback. The data passed to this function
-- is printed
do
print("Hello again - ");
print(data);
print(" was pressed%N");
end
delete_event is
-- another callback
do
gtk_main_quit;
end
feature -- Creation
make is
local
window : GTK_WINDOW;
button : GTK_BUTTON;
box1 : GTK_HBOX;
do
-- This is called in all GTK applications. Arguments are parsed
-- from the command line and are returned to the application.
vegtk_init;
-- Create a new window
!!window.make(Gtk_window_dialog)
-- This is a new call, this just sets the title of our
-- new window to "Hello Buttons!" */
window.set_title ("Hello Buttons!");
-- Here we just set a handler for delete_event that immediately
-- exits GTK. */
signal_connect (window, "delete_event",$delete_event);
-- Sets the border width of the window.
window.set_border_width (10);
-- We create a box to pack widgets into. This is described in detail
-- in the "packing" section. The box is not really visible, it
-- is just used as a tool to arrange widgets.
!!box1.make(False, 0);
-- Put the box into the main window.
window.add (box1);
-- Creates a new button with the label "Button 1".
!!button.make_with_label ("Button 1");
-- Now when the button is clicked, we call the "callback" function
-- with a string "button 1" as its data argument
signal_connect_with_data (button, "clicked",$callback,"Button 1");
-- Instead of adding to window, we pack this button into the invisible
-- box, which has been packed into the window. */
box1.pack_start(button, True, True, 0);
-- Always remember this step, this tells GTK that our preparation for
-- this button is complete, and it can now be displayed.
button.show;
-- Do these same steps again to create a second button
!!button.make_with_label ("Button 2");
-- Call the same callback function with a different argument,
-- passing the string "button 2" instead.
signal_connect_with_data (button, "clicked",$callback, "button 2");
box1.pack_start(button, True, True, 0);
-- The order in which we show the buttons is not really important, but I
-- recommend showing the window last, so it all pops up at once.
button.show;
box1.show;
window.show;
-- and the window
window.show;
-- Rest in gtk_main and wait for the fun to begin!
gtk_main
print ("End%N");
end
end -- class HELLOWORLD2
-- example-end
You'll notice this time there is no easy way to exit the program, you have to use your window manager or command line to kill it. A good exercise for the reader would be to insert a third "Quit" button that will exit the program. You may also wish to play with the options to pack_start() while reading the next section. Try resizing the window, and observe the behavior.
Just as a side note, there is another useful define for window.make() - Gtk_window_dialog. This interacts with the window manager a little differently and should be used for transient windows.