GTK gossip: GTK event handling function

Source: Internet
Author: User
Tags gtk signal handler

For
Finally, an event signal and callback function are formed. The g_signal_connect () function is used, but the callback of the event signal is handled.
The callback function of the function is different from that of the GTK signal of zookeeper. The following is the callback function for processing the event signal:


Gboolean callback_func (

Gtkwidget * widget, gdkevent * event, gpointer callback_data );


The callback function has an additional gdkevent * number, while the callback value is partial,
It can be used to control whether an event is going to be broadcast next,
If this parameter is set to true, the event has been handled until the end of the event. If this parameter is set to false, the event does not need to be broadcast.

The event signal processing function is handled before the GTK signal processing function. The following is an example:

Press export and press --> Generate gdk_button_press --> gdk Handler

--> Generate the button_press_event signal --> GTK handler configuration Handler

--> Generate a clicked signal --> GTK Handler


You can set the callback function of the event signal to intercept the button_press_event. When the callback is returned to true
Does not parse the GTK handler set by the primary handler, so the signal of the clicked will not be generated. Only when the primary key is set to false will the signal of the clicked be generated.
The GTK signal handler will be deleted.

The following is an example:

  • Event_demo.c

 

# Include <GTK/GTK. h>

// Signal handler
Void button_clicked_callback (gtkwidget * button, gpointer data ){
G_print ("clicked handler/N ");
}

// Event handling function
Gboolean button_press_callback (
Gtkwidget * button, gdkevent * event, gpointer data ){
Gdkeventtype type = event-> type;

If (type = gdk_button_press ){
G_print ("button_press_event handling function (% d, % d)/n ",
(Gint) Event-> button. X, (Gint) Event-> button. y );
}

Return false;
}

Int main (INT argc, char * argv []) {
Gtkwidget * window;
Gtkwidget * button;

Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "haha! GTK +! ");

Button = gtk_button_new_with_label ("press me ");
Gtk_container_add (gtk_container (window), button );

G_signal_connect (gtk_object (button), "Event ",
G_callback (button_press_callback), null );
G_signal_connect (gtk_object (button), "clicked ",
G_callback (button_clicked_callback), null );
G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );

Gtk_widget_show (window );
Gtk_widget_show (button );

Gtk_main ();

Return 0;
}


In the above program, the event is handled using the button_clicked_callback () function, because
Set g_signal_connect () to "Event", indicating that all events will be handled by button_press_callback.
If the cursor gdkeventtype is determined, the cursor is displayed only when the mouse is pressed, and the last cursor is returned to false. The result is as follows:

Button_press_event handling function (58, 44)

Clicked Handler

Button_press_event handling function (134,108)

Clicked Handler

Button_press_event handling function (66,149)

Clicked Handler

Button_press_event handling function (146, 44)

Clicked Handler

If
When the button_clicked_callback () function returns true, the button_clicked_callback () function will not begin, and the "clicked processing function" text will not be displayed.



If you know that you want to handle a specific event type, you can declare a specific event type in the callback function and specify a specific event signal in g_signal_connect, for example, the preceding example can be changed to the following, and the results of the preceding example are the same:

  • Event_demo.c

 

# Include <GTK/GTK. h>

// Callback function
Void button_clicked_callback (gtkwidget * button, gpointer data ){
G_print ("clicked handler/N ");
}

Gboolean button_press_callback (
Gtkwidget * button, gdkeventbutton * event, gpointer data ){
G_print ("button_press_event handling function (% d, % d)/n ",
(Gint) Event-> X, (Gint) Event-> Y );

Return false;
}

Int main (INT argc, char * argv []) {
Gtkwidget * window;
Gtkwidget * button;

Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "haha! GTK +! ");

Button = gtk_button_new_with_label ("press me ");
Gtk_container_add (gtk_container (window), button );

G_signal_connect (gtk_object (button), "button_press_event ",
G_callback (button_press_callback), null );
G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );
G_signal_connect (gtk_object (button), "clicked ",
G_callback (button_clicked_callback), null );

Gtk_widget_show (window );
Gtk_widget_show (button );

Gtk_main ();

Return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.