Main loop and Events

Main loop and Events — Library initialization, main event loop, and events

Types and Values


#include <gtk/gtk.h>


Before using GTK+, you need to initialize it; initialization connects to the window system display, and parses some standard command line arguments. The gtk_init() macro initializes GTK+. gtk_init() exits the application if errors occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to recover from a failed GTK+ initialization - you might start up your application in text mode instead.

Like all GUI toolkits, GTK+ uses an event-driven programming model. When the user is doing nothing, GTK+ sits in the “main loop” and waits for input. If the user performs some action - say, a mouse click - then the main loop “wakes up” and delivers an event to GTK+. GTK+ forwards the event to one or more widgets.

When widgets receive an event, they frequently emit one or more “signals”. Signals notify your program that "something interesting happened" by invoking functions you’ve connected to the signal with g_signal_connect(). Functions connected to a signal are often termed “callbacks”.

When your callbacks are invoked, you would typically take some action - for example, when an Open button is clicked you might display a GtkFileChooserDialog. After a callback finishes, GTK+ will return to the main loop and await more user input.

Typical main() function for a GTK+ application

main (int argc, char **argv)
 GtkWidget *mainwin;
  // Initialize i18n support with bindtextdomain(), etc.

  // ...

  // Initialize the widget set
  gtk_init ();

  // Create the main window
  mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  // Set up our GUI elements

  // ...

  // Show the application window
  gtk_widget_show (mainwin);

  // Enter the main event loop, and wait for user interaction
  gtk_main ();

  // The user lost interest
  return 0;

It’s OK to use the GLib main loop directly instead of gtk_main(), though it involves slightly more typing. See GMainLoop in the GLib documentation.


gtk_disable_setlocale ()

gtk_disable_setlocale (void);

Prevents gtk_init(), gtk_init_check() and gtk_parse_args() from automatically calling setlocale (LC_ALL, ""). You would want to use this function if you wanted to set the locale for your program to something other than the user’s locale, or if you wanted to set different values for different locale categories.

Most programs should not need to call this function.

gtk_get_default_language ()

PangoLanguage *
gtk_get_default_language (void);

Returns the PangoLanguage for the default language currently in effect. (Note that this can change over the life of an application.) The default language is derived from the current locale. It determines, for example, whether GTK+ uses the right-to-left or left-to-right text direction.

This function is equivalent to pango_language_get_default(). See that function for details.


the default language as a PangoLanguage, must not be freed.

[transfer none]

gtk_get_locale_direction ()

gtk_get_locale_direction (void);

Get the direction of the current locale. This is the expected reading direction for text and UI.

This function depends on the current locale being set with setlocale() and will default to setting the GTK_TEXT_DIR_LTR direction otherwise. GTK_TEXT_DIR_NONE will never be returned.

GTK+ sets the default text direction according to the locale during gtk_init(), and you should normally use gtk_widget_get_direction() or gtk_widget_get_default_direction() to obtain the current direcion.

This function is only needed rare cases when the locale is changed after GTK+ has already been initialized. In this case, you can use it to update the default text direction as follows:

setlocale (LC_ALL, new_locale);
direction = gtk_get_locale_direction ();
gtk_widget_set_default_direction (direction);


the GtkTextDirection of the current locale

gtk_init ()

gtk_init (void);

Call this function before using any other GTK+ functions in your GUI applications. It will initialize everything needed to operate the toolkit and parses some standard command line options.

If you are using GtkApplication, you don't have to call gtk_init() or gtk_init_check(); the “startup” handler does it for you.

This function will terminate your program if it was unable to initialize the windowing system for some reason. If you want your program to fall back to a textual interface you want to call gtk_init_check() instead.

GTK+ calls signal (SIGPIPE, SIG_IGN) during initialization, to ignore SIGPIPE signals, since these are almost never wanted in graphical applications. If you do need to handle SIGPIPE for some reason, reset the handler after gtk_init(), but notice that other libraries (e.g. libdbus or gvfs) might do similar things.

gtk_init_check ()

gtk_init_check (void);

This function does the same work as gtk_init() with only a single change: It does not terminate the program if the windowing system can’t be initialized. Instead it returns FALSE on failure.

This way the application can fall back to some other means of communication with the user - for example a curses or command line interface.


TRUE if the windowing system has been successfully initialized, FALSE otherwise

gtk_events_pending ()

gtk_events_pending (void);

Checks if any events are pending.

This can be used to update the UI and invoke timeouts etc. while doing some time intensive computation.

Updating the UI during a long computation

 // computation going on...

 while (gtk_events_pending ())
   gtk_main_iteration ();

 // ...computation continued


TRUE if any events are pending, FALSE otherwise

gtk_main ()

gtk_main (void);

Runs the main loop until gtk_main_quit() is called.

You can nest calls to gtk_main(). In that case gtk_main_quit() will make the innermost invocation of the main loop return.

gtk_main_level ()

gtk_main_level (void);

Asks for the current nesting level of the main loop.


the nesting level of the current invocation of the main loop

gtk_main_quit ()

gtk_main_quit (void);

Makes the innermost invocation of the main loop return when it regains control.

gtk_main_iteration ()

gtk_main_iteration (void);

Runs a single iteration of the mainloop.

If no events are waiting to be processed GTK+ will block until the next event is noticed. If you don’t want to block look at gtk_main_iteration_do() or check if any events are pending with gtk_events_pending() first.


TRUE if gtk_main_quit() has been called for the innermost mainloop

gtk_main_iteration_do ()

gtk_main_iteration_do (gboolean blocking);

Runs a single iteration of the mainloop. If no events are available either return or block depending on the value of blocking .



TRUE if you want GTK+ to block if no events are pending



TRUE if gtk_main_quit() has been called for the innermost mainloop

gtk_main_do_event ()

gtk_main_do_event (GdkEvent *event);

Processes a single GDK event.

This is public only to allow filtering of events between GDK and GTK+. You will not usually need to call this function directly.

While you should not call this function directly, you might want to know how exactly events are handled. So here is what this function does with the event:

  1. Compress enter/leave notify events. If the event passed build an enter/leave pair together with the next event (peeked from GDK), both events are thrown away. This is to avoid a backlog of (de-)highlighting widgets crossed by the pointer.

  2. Find the widget which got the event. If the widget can’t be determined the event is thrown away unless it belongs to a INCR transaction.

  3. Then the event is pushed onto a stack so you can query the currently handled event with gtk_get_current_event().

  4. The event is sent to a widget. If a grab is active all events for widgets that are not in the contained in the grab widget are sent to the latter with a few exceptions:

    • Deletion and destruction events are still sent to the event widget for obvious reasons.

    • Events which directly relate to the visual representation of the event widget.

    • Leave events are delivered to the event widget if there was an enter event delivered to it before without the paired leave event.

    • Drag events are not redirected because it is unclear what the semantics of that would be.

  5. After finishing the delivery the event is popped from the event stack.



An event to process (normally passed by GDK)


gtk_grab_add ()

gtk_grab_add (GtkWidget *widget);

Makes widget the current grabbed widget.

This means that interaction with other widgets in the same application is blocked and mouse as well as keyboard events are delivered to this widget.

If widget is not sensitive, it is not set as the current grabbed widget and this function does nothing.




The widget that grabs keyboard and pointer events


gtk_grab_get_current ()

GtkWidget *
gtk_grab_get_current (void);

Queries the current grab of the default window group.


The widget which currently has the grab or NULL if no grab is active.

[transfer none][nullable]

gtk_grab_remove ()

gtk_grab_remove (GtkWidget *widget);

Removes the grab from the given widget.

You have to pair calls to gtk_grab_add() and gtk_grab_remove().

If widget does not have the grab, this function does nothing.




The widget which gives up the grab


gtk_device_grab_add ()

gtk_device_grab_add (GtkWidget *widget,
                     GdkDevice *device,
                     gboolean block_others);

Adds a GTK+ grab on device , so all the events on device and its associated pointer or keyboard (if any) are delivered to widget . If the block_others parameter is TRUE, any other devices will be unable to interact with widget during the grab.



a GtkWidget



a GdkDevice to grab on.



TRUE to prevent other devices to interact with widget .


gtk_device_grab_remove ()

gtk_device_grab_remove (GtkWidget *widget,
                        GdkDevice *device);

Removes a device grab from the given widget.

You have to pair calls to gtk_device_grab_add() and gtk_device_grab_remove().



a GtkWidget



a GdkDevice


gtk_get_current_event ()

GdkEvent *
gtk_get_current_event (void);

Obtains a reference of the event currently being processed by GTK+.

For example, if you are handling a “clicked” signal, the current event will be the GdkEventButton that triggered the ::clicked signal.


a reference of the current event, or NULL if there is no current event. The returned event must be freed with g_object_unref().

[transfer full][nullable]

gtk_get_current_event_time ()

gtk_get_current_event_time (void);

If there is a current event and it has a timestamp, return that timestamp, otherwise return GDK_CURRENT_TIME.


the timestamp from the current event, or GDK_CURRENT_TIME.

gtk_get_current_event_state ()

gtk_get_current_event_state (GdkModifierType *state);

If there is a current event and it has a state field, place that state field in state and return TRUE, otherwise return FALSE.



a location to store the state of the current event.



TRUE if there was a current event and it had a state field

gtk_get_current_event_device ()

GdkDevice *
gtk_get_current_event_device (void);

If there is a current event and it has a device, return that device, otherwise return NULL.


a GdkDevice, or NULL.

[transfer none][nullable]

gtk_get_event_widget ()

GtkWidget *
gtk_get_event_widget (const GdkEvent *event);

If event is NULL or the event was not associated with any widget, returns NULL, otherwise returns the widget that received the event originally.



a GdkEvent



the widget that originally received event , or NULL.

[transfer none][nullable]

gtk_get_event_target ()

GtkWidget *
gtk_get_event_target (const GdkEvent *event);

If event is NULL or the event was not associated with any widget, returns NULL, otherwise returns the widget that is the deepmost receiver of the event.



a GdkEvent



the target widget, or NULL.

[transfer none][nullable]

gtk_get_event_target_with_type ()

GtkWidget *
gtk_get_event_target_with_type (GdkEvent *event,
                                GType type);

If event is NULL or the event was not associated with any widget, returns NULL, otherwise returns first widget found from the event target to the toplevel that matches type .



a GdkEvent



the type to look for



the widget in the target stack with the given type, or NULL.

[transfer none][nullable]

gtk_propagate_event ()

gtk_propagate_event (GtkWidget *widget,
                     GdkEvent *event);

Sends an event to a widget, propagating the event to parent widgets if the event remains unhandled. This function will emit the event through all the hierarchy of widget through all propagation phases.

Events received by GTK+ from GDK normally begin in gtk_main_do_event(). Depending on the type of event, existence of modal dialogs, grabs, etc., the event may be propagated; if so, this function is used.

gtk_propagate_event() calls gtk_widget_event() on each widget it decides to send the event to. So gtk_widget_event() is the lowest-level function; it simply emits the “event” and possibly an event-specific signal on a widget. gtk_propagate_event() is a bit higher-level, and gtk_main_do_event() is the highest level.

All that said, you most likely don’t want to use any of these functions; synthesizing events is rarely needed. There are almost certainly better ways to achieve your goals. For example, use gtk_widget_queue_draw() instead of making up expose events.



a GtkWidget



an event


Types and Values



Use this priority for functionality related to size allocation.

It is used internally by GTK+ to compute the sizes of widgets. This priority is higher than GDK_PRIORITY_REDRAW to avoid resizing a widget which was just redrawn.

See Also

See the GLib manual, especially GMainLoop and signal-related functions such as g_signal_connect()