Main

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.

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.

Members

Static functions

deviceGrabAdd
void deviceGrabAdd(Widget widget, Device device, int blockOthers)

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.

deviceGrabRemove
void deviceGrabRemove(Widget widget, Device device)

Removes a device grab from the given widget. You have to pair calls to gtk_device_grab_add() and gtk_device_grab_remove().

disableSetlocale
void disableSetlocale()

Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() 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.

doEvent
void doEvent(Event 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

eventsPending
int eventsPending()

Checks if any events are pending. This can be used to update the UI and invoke timeouts etc. while doing some time intensive computation.

getCurrentEvent
Event getCurrentEvent()

Obtains a copy 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.

getCurrentEventDevice
Device getCurrentEventDevice()

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

getCurrentEventState
int getCurrentEventState(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.

getCurrentEventTime
uint getCurrentEventTime()

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

getDefaultLanguage
PgLanguage getDefaultLanguage()

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.

getEventWidget
Widget getEventWidget(Event 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.

getOptionGroup
OptionGroup getOptionGroup(int openDefaultDisplay)

Returns a GOptionGroup for the commandline arguments recognized by GTK+ and GDK. You should add this group to your GOptionContext with g_option_context_add_group(), if you are using g_option_context_parse() to parse your commandline arguments. Since 2.6

grabAdd
void grabAdd(Widget 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.

grabGetCurrent
Widget grabGetCurrent()

Queries the current grab of the default window group.

grabRemove
void grabRemove(Widget 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.

init
void init(string[] argv)

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. Although you are expected to pass the argc, argv parameters from main() to this function, it is possible to pass NULL if argv is not available or commandline handling is not required. argc and argv are adjusted accordingly so your own code will never see those standard arguments. Since 2.18, 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.

initCheck
int initCheck(string[] argv)

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.

initMultiThread
void initMultiThread(string[] args)

This initiates GtkD to supports multi threaded programs. read full documantation at http://gtk.org/faq/#AEN482 from the FAQ: "There is a single global lock that you must acquire with gdk_threads_enter() before making any GDK calls, and release with gdk_threads_leave() afterwards throughout your code." This is to be used on any call to GDK not executed from the main thread.

initWithArgs
int initWithArgs(string[] argv, string parameterString, GOptionEntry[] entries, string translationDomain)

This function does the same work as gtk_init_check(). Additionally, it allows you to add your own commandline options, and it automatically generates nicely formatted --help output. Note that your program will be terminated after writing out the help output. Since 2.6

iteration
int iteration()

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.

iterationDo
int iterationDo(int blocking)

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

keySnooperInstall
uint keySnooperInstall(GtkKeySnoopFunc snooper, void* funcData)

Warning gtk_key_snooper_install has been deprecated since version 3.4 and should not be used in newly-written code. Key snooping should not be done. Events should be handled by widgets. Installs a key snooper function, which will get called on all key events before delivering them normally.

keySnooperRemove
void keySnooperRemove(uint snooperHandlerId)

Warning gtk_key_snooper_remove has been deprecated since version 3.4 and should not be used in newly-written code. Key snooping should not be done. Events should be handled by widgets. Removes the key snooper function with the given id.

level
uint level()

Asks for the current nesting level of the main loop.

parseArgs
int parseArgs(string[] argv)

Parses command line arguments, and initializes global attributes of GTK+, but does not actually open a connection to a display. (See gdk_display_open(), gdk_get_display_arg_name()) Any arguments used by GTK+ or GDK are removed from the array and argc and argv are updated accordingly. There is no need to call this function explicitely if you are using gtk_init(), or gtk_init_check().

propagateEvent
void propagateEvent(Widget widget, Event event)

Sends an event to a widget, propagating the event to parent widgets if the event remains unhandled. 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 gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead of making up expose events.

quit
void quit()

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

run
void run()

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.

Meta