Main loop and Events

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

Functions

Types and Values

Includes

#include <gtk/gtk.h>

Description

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int
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
  while (!done)
    g_main_context_iteration (NULL, TRUE);

  // The user lost interest
  return 0;
}

See GMainLoop in the GLib documentation to learn more about main loops and their features.

Functions

gtk_disable_setlocale ()

void
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.

Returns

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

[transfer none]


gtk_get_locale_direction ()

GtkTextDirection
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:

1
2
3
setlocale (LC_ALL, new_locale);
direction = gtk_get_locale_direction ();
gtk_widget_set_default_direction (direction);

Returns

the GtkTextDirection of the current locale


gtk_init ()

void
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 ()

gboolean
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.

Returns

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


gtk_grab_add ()

void
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.

[method]

Parameters

widget

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.

Returns

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

[transfer none][nullable]


gtk_grab_remove ()

void
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.

[method]

Parameters

widget

The widget which gives up the grab

 

gtk_device_grab_add ()

void
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.

Parameters

widget

a GtkWidget

 

device

a GdkDevice to grab on.

 

block_others

TRUE to prevent other devices to interact with widget .

 

gtk_device_grab_remove ()

void
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().

Parameters

widget

a GtkWidget

 

device

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.

Returns

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 ()

guint32
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.

Returns

the timestamp from the current event, or GDK_CURRENT_TIME.


gtk_get_current_event_state ()

gboolean
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.

Parameters

state

a location to store the state of the current event.

[out]

Returns

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.

Returns

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.

Parameters

event

a GdkEvent

 

Returns

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.

Parameters

event

a GdkEvent

 

Returns

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 .

Parameters

event

a GdkEvent

 

type

the type to look for

 

Returns

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

[transfer none][nullable]

See Also

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