1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 Library initialization, main event loop, and events
7 <!-- ##### SECTION Long_Description ##### -->
10 Before using GTK+, you need to initialize it; initialization connects
11 to the window system display, and parses some standard command line
12 arguments. The gtk_init() function initializes GTK+. gtk_init() exits
13 the application if errors occur; to avoid this, use gtk_init_check().
14 gtk_init_check() allows you to recover from a failed GTK+
15 initialization - you might start up your application in text mode instead.
19 Like all GUI toolkits, GTK+ uses an event-driven programming
20 model. When the user is doing nothing, GTK+ sits in the
21 <firstterm>main loop</firstterm> and waits for input. If the user
22 performs some action - say, a mouse click - then the main loop "wakes
23 up" and delivers an event to GTK+. GTK+ forwards the event to one or
28 When widgets receive an event, they frequently emit one or more
29 <firstterm>signals</firstterm>. Signals notify your program that
30 "something interesting happened" by invoking functions you've
31 connected to the signal with g_signal_connect(). Functions connected
32 to a signal are often termed <firstterm>callbacks</firstterm>.
36 When your callbacks are invoked, you would typically take some action
37 - for example, when an Open button is clicked you might display a
38 #GtkFileSelectionDialog. After a callback finishes, GTK+ will return
39 to the main loop and await more user input.
43 <title>Typical <function>main</function> function for a GTK+ application</title>
46 main (int argc, char **argv)
48 /* Initialize i18n support */
49 gtk_set_locale (<!-- -->);
51 /* Initialize the widget set */
52 gtk_init (&argc, &argv);
54 /* Create the main window */
55 mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
57 /* Set up our GUI elements */
60 /* Show the application window */
61 gtk_widget_show_all (mainwin);
63 /* Enter the main event loop, and wait for user interaction */
66 /* The user lost interest */
73 It's OK to use the GLib main loop directly instead of gtk_main(),
74 though it involves slightly more typing. See #GMainLoop in the GLib
78 <!-- ##### SECTION See_Also ##### -->
80 See the GLib manual, especially #GMainLoop and signal-related
81 functions such as g_signal_connect().
84 <!-- ##### FUNCTION gtk_set_locale ##### -->
92 <!-- ##### FUNCTION gtk_disable_setlocale ##### -->
99 <!-- ##### FUNCTION gtk_get_default_language ##### -->
107 <!-- ##### FUNCTION gtk_init ##### -->
120 <!-- ##### FUNCTION gtk_init_check ##### -->
129 <!-- ##### FUNCTION gtk_exit ##### -->
131 Terminates the program and returns the given exit code to the caller.
132 This function will shut down the GUI and free all resources allocated
136 @error_code: Return value to pass to the caller. This is dependend on the
137 target system but at least on Unix systems %0 means
141 <!-- ##### FUNCTION gtk_events_pending ##### -->
143 Checks if any events are pending. This can be used to update the GUI
144 and invoke timeouts etc. while doing some time intensive computation.
148 <title>Updating the GUI during a long computation.</title>
150 /* computation going on */
152 while (gtk_events_pending (<!-- -->))
153 gtk_main_iteration (<!-- -->);
155 /* computation continued */
159 @Returns: %TRUE if any events are pending, %FALSE otherwise.
162 <!-- ##### FUNCTION gtk_main ##### -->
164 Runs the main loop until gtk_main_quit() is called. You can nest calls to
165 gtk_main(). In that case gtk_main_quit() will make the innermost invocation
166 of the main loop return.
171 <!-- ##### FUNCTION gtk_main_level ##### -->
173 Asks for the current nesting level of the main loop. This can be useful
174 when calling gtk_quit_add().
177 @Returns: the nesting level of the current invocation of the main loop.
180 <!-- ##### FUNCTION gtk_main_quit ##### -->
182 Makes the innermost invocation of the main loop return when it regains
188 <!-- ##### FUNCTION gtk_main_iteration ##### -->
190 Runs a single iteration of the mainloop. If no events are waiting to be
191 processed GTK+ will block until the next event is noticed. If you don't
192 want to block look at gtk_main_iteration_do() or check if any events are
193 pending with gtk_events_pending() first.
196 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
199 <!-- ##### FUNCTION gtk_main_iteration_do ##### -->
201 Runs a single iteration of the mainloop. If no events are available either
202 return or block dependent on the value of @blocking.
205 @blocking: %TRUE if you want GTK+ to block if no events are pending.
206 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
209 <!-- ##### FUNCTION gtk_main_do_event ##### -->
211 Processes a single GDK event. This is public only to allow filtering of events
212 between GDK and GTK+. You will not usually need to call this function directly.
215 While you should not call this function directly, you might want to know
216 how exactly events are handled. So here is what this function does with
222 Compress enter/leave notify events. If the event passed build an
223 enter/leave pair together with the next event (peeked from GDK)
224 both events are thrown away. This is to avoid a backlog of (de-)highlighting
225 widgets crossed by the pointer.
228 Find the widget which got the event. If the widget can't be determined
229 the event is thrown away unless it belongs to a INCR transaction. In that
230 case it is passed to gtk_selection_incr_event().
233 Then the event is passed on a stack so you can query the currently handled
234 event with gtk_get_current_event().
237 The event is sent to a widget. If a grab is active all events for
238 widgets that are not in the contained in the grab widget are sent to the
239 latter with a few exceptions:
243 Deletion and destruction events are still sent to the event widget for
247 Events which directly relate to the visual representation of the event
251 Leave events are delivered to the event widget if there was an enter
252 event delivered to it before without the paired leave event.
255 Drag events are not redirected because it is unclear what the semantics
260 Another point of interest might be that all key events are first passed
261 through the key snooper functions if there are any. Read the description
262 of gtk_key_snooper_install() if you need this feature.
265 After finishing the delivery the event is popped from the event stack.
269 @event: An event to process (normally) passed by GDK.
272 <!-- ##### USER_FUNCTION GtkModuleInitFunc ##### -->
274 Each GTK+ module must have a function gtk_module_init() with this prototype.
275 This function is called after loading the module with the @argc and @argv
276 cleaned from any arguments that GTK+ handles itself.
279 @argc: Pointer to the number of arguments remaining after gtk_init().
280 @argv: Points to the argument vector.
283 <!-- ##### USER_FUNCTION GtkModuleDisplayInitFunc ##### -->
292 <!-- ##### FUNCTION gtk_true ##### -->
294 All this function does it to return %TRUE. This can be useful for example
295 if you want to inhibit the deletion of a window. Of course you should
296 not do this as the user expects a reaction from clicking the close
297 icon of the window...
301 <title>A persistent window</title>
303 ##include <gtk/gtk.h>
306 main (int argc, char **argv)
308 GtkWidget *win, *but;
310 gtk_init( &argc, &argv );
312 win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
313 g_signal_connect (GTK_OBJECT(win), "delete-event",
314 G_CALLBACK (gtk_true), NULL);
315 g_signal_connect (GTK_OBJECT(win), "destroy",
316 G_CALLBACK (gtk_main_quit), NULL);
318 but = gtk_button_new_with_label ("Close yourself. I mean it!");
319 g_signal_connect_swapped (GTK_OBJECT (but), "clicked",
320 G_CALLBACK (gtk_object_destroy), (gpointer) win);
321 gtk_container_add (GTK_CONTAINER (win), but);
323 gtk_widget_show_all (win);
333 <!-- ##### FUNCTION gtk_false ##### -->
335 Analogical to gtk_true() this function does nothing
336 but always returns %FALSE.
342 <!-- ##### FUNCTION gtk_grab_add ##### -->
344 Makes @widget the current grabbed widget. This means that interaction with
345 other widgets in the same application is blocked and mouse as well as
346 keyboard events are delivered to this widget.
349 @widget: The widget that grabs keyboard and pointer events.
352 <!-- ##### FUNCTION gtk_grab_get_current ##### -->
354 Queries the current grab.
357 @Returns: The widget which currently has the grab or %NULL if no grab is active.
360 <!-- ##### FUNCTION gtk_grab_remove ##### -->
362 Removes the grab from the given widget. You have to pair calls to gtk_grab_add()
363 and gtk_grab_remove().
366 @widget: The widget which gives up the grab.
369 <!-- ##### FUNCTION gtk_init_add ##### -->
371 Registers a function to be called when the mainloop is started.
374 @function: Function to invoke when gtk_main() is called next.
375 @data: Data to pass to that function.
378 <!-- ##### FUNCTION gtk_quit_add_destroy ##### -->
380 Trigger destruction of @object in case the mainloop at level @main_level
384 @main_level: Level of the mainloop which shall trigger the destruction.
385 @object: Object to be destroyed.
388 <!-- ##### FUNCTION gtk_quit_add ##### -->
390 Registers a function to be called when an instance of the mainloop is left.
393 @main_level: Level at which termination the function shall be called. You
394 can pass 0 here to have the function run at the termination of the current
396 @function: The function to call. This should return 0 to be removed from the
397 list of quit handlers. Otherwise the function might be called again.
398 @data: Pointer to pass when calling @function.
399 @Returns: A handle for this quit handler (you need this for gtk_quit_remove())
400 or 0 if you passed a %NULL pointer in @function.
403 <!-- ##### FUNCTION gtk_quit_add_full ##### -->
405 Registers a function to be called when an instance of the mainloop is left.
406 In comparison to gtk_quit_add() this function adds the possibility to
407 pass a marshaller and a function to be called when the quit handler is freed.
410 The former can be used to run interpreted code instead of a compiled function
411 while the latter can be used to free the information stored in @data (while
412 you can do this in @function as well)... So this function will mostly be
413 used by GTK+ wrappers for languages other than C.
416 @main_level: Level at which termination the function shall be called. You
417 can pass 0 here to have the function run at the termination of the current
419 @function: The function to call. This should return 0 to be removed from the
420 list of quit handlers. Otherwise the function might be called again.
421 @marshal: The marshaller to be used. If this is non-%NULL, @function is
423 @data: Pointer to pass when calling @function.
424 @destroy: Function to call to destruct @data. Gets @data as argument.
425 @Returns: A handle for this quit handler (you need this for gtk_quit_remove())
426 or 0 if you passed a %NULL pointer in @function.
429 <!-- ##### FUNCTION gtk_quit_remove ##### -->
431 Removes a quit handler by it's identifier.
434 @quit_handler_id: Identifier for the handler returned when installing it.
437 <!-- ##### FUNCTION gtk_quit_remove_by_data ##### -->
439 Removes a quit handler identified by it's @data field.
442 @data: The pointer passed as @data to gtk_quit_add() or gtk_quit_add_full().
445 <!-- ##### FUNCTION gtk_timeout_add_full ##### -->
447 Registers a function to be called periodically. The function will be called
448 repeatedly after @interval milliseconds until it returns %FALSE at which
449 point the timeout is destroyed and will not be called again.
452 @interval: The time between calls to the function, in milliseconds
453 (1/1000ths of a second.)
454 @function: The function to call periodically.
455 @marshal: The marshaller to use instead of the function (if non-%NULL).
456 @data: The data to pass to the function.
457 @destroy: Function to call when the timeout is destroyed or %NULL.
458 @Returns: A unique id for the event source.
461 <!-- ##### FUNCTION gtk_timeout_add ##### -->
463 Registers a function to be called periodically. The function will be called
464 repeatedly after @interval milliseconds until it returns %FALSE at which
465 point the timeout is destroyed and will not be called again.
468 @interval: The time between calls to the function, in milliseconds
469 (1/1000ths of a second.)
470 @function: The function to call periodically.
471 @data: The data to pass to the function.
472 @Returns: A unique id for the event source.
475 <!-- ##### FUNCTION gtk_timeout_remove ##### -->
477 Removes the given timeout destroying all information about it.
480 @timeout_handler_id: The identifier returned when installing the timeout.
483 <!-- ##### FUNCTION gtk_idle_add ##### -->
485 Causes the mainloop to call the given function whenever no events with
486 higher priority are to be processed. The default priority is
487 %GTK_PRIORITY_DEFAULT, which is rather low.
490 @function: The function to call.
491 @data: The information to pass to the function.
492 @Returns: a unique handle for this registration.
495 <!-- ##### FUNCTION gtk_idle_add_priority ##### -->
497 Like gtk_idle_add() this function allows you to have a function called
498 when the event loop is idle. The difference is that you can give a
499 priority different from %GTK_PRIORITY_DEFAULT to the idle function.
502 @priority: The priority which should not be above %G_PRIORITY_HIGH_IDLE.
503 Note that you will interfere with GTK+ if you use a priority above
504 %GTK_PRIORITY_RESIZE.
505 @function: The function to call.
506 @data: Data to pass to that function.
507 @Returns: A unique id for the event source.
510 <!-- ##### FUNCTION gtk_idle_add_full ##### -->
512 Like gtk_idle_add() this function allows you to have a function called
513 when the event loop is idle. The difference is that you can give a
514 priority different from %GTK_PRIORITY_DEFAULT to the idle function.
517 @priority: The priority which should not be above %G_PRIORITY_HIGH_IDLE.
518 Note that you will interfere with GTK+ if you use a priority above
519 %GTK_PRIORITY_RESIZE.
520 @function: The function to call.
521 @marshal: The marshaller to use instead of the function (if non-%NULL).
522 @data: Data to pass to that function.
523 @destroy: Function to call when the timeout is destroyed or %NULL.
524 @Returns: A unique id for the event source.
527 <!-- ##### FUNCTION gtk_idle_remove ##### -->
529 Removes the idle function with the given id.
532 @idle_handler_id: Identifies the idle function to remove.
535 <!-- ##### FUNCTION gtk_idle_remove_by_data ##### -->
537 Removes the idle function identified by the user data.
540 @data: remove the idle function which was registered with this user data.
543 <!-- ##### FUNCTION gtk_input_add_full ##### -->
545 Registers a function to be called when a condition becomes true
546 on a file descriptor.
549 @source: a file descriptor.
550 @condition: the condition.
551 @function: The function to call.
552 @marshal: The marshaller to use instead of the function (if non-%NULL).
553 @data: callback data passed to @function.
554 @destroy: callback function to call with @data when the input
555 handler is removed, or %NULL.
556 @Returns: A unique id for the event source; to be used with gtk_input_remove().
559 <!-- ##### FUNCTION gtk_input_remove ##### -->
561 Removes the function with the given id.
564 @input_handler_id: Identifies the function to remove.
567 <!-- ##### MACRO GTK_PRIORITY_REDRAW ##### -->
569 Use this priority for redrawing related stuff. It is used internally by
570 GTK+ to do pending redraws. This priority is lower than %GTK_PRIORITY_RESIZE
571 to avoid redrawing a widget just before resizing (and therefore redrawing
575 This macro is deprecated. You should use %GDK_PRIORITY_REDRAW instead.
580 <!-- ##### MACRO GTK_PRIORITY_RESIZE ##### -->
582 Use this priority for resizing related stuff. It is used internally by
583 GTK+ to compute the sizes of widgets. This priority is higher than
584 %GTK_PRIORITY_REDRAW to avoid resizing a widget which was just redrawn.
589 <!-- ##### MACRO GTK_PRIORITY_HIGH ##### -->
591 Use this for high priority timeouts. This priority is never used inside
592 GTK+ so everything running at this priority will be running before anything
595 This macro is deprecated. You should use %G_PRIORITY_HIGH instead.
601 <!-- ##### MACRO GTK_PRIORITY_INTERNAL ##### -->
603 This priority is for GTK+ internal stuff. Don't use it in your applications.
608 <!-- ##### MACRO GTK_PRIORITY_DEFAULT ##### -->
610 Default priority for idle functions.
612 This macro is deprecated. You should use %G_PRIORITY_DEFAULT_IDLE instead.
618 <!-- ##### MACRO GTK_PRIORITY_LOW ##### -->
620 Priority for very unimportant background tasks.
622 This macro is deprecated. You should use %G_PRIORITY_LOW instead.
628 <!-- ##### FUNCTION gtk_key_snooper_install ##### -->
630 Installs a key snooper function, which will get called on all key events
631 before delivering them normally.
634 @snooper: a #GtkKeySnoopFunc.
635 @func_data: data to pass to @snooper.
636 @Returns: a unique id for this key snooper for use with gtk_key_snooper_remove().
639 <!-- ##### USER_FUNCTION GtkKeySnoopFunc ##### -->
641 Key snooper functions are called before normal event delivery.
642 They can be used to implement custom key event handling.
645 @grab_widget: the widget to which the event will be delivered.
646 @event: the key event.
647 @func_data: the @func_data supplied to gtk_key_snooper_install().
648 @Returns: %TRUE to stop further processing of @event, %FALSE to continue.
651 <!-- ##### FUNCTION gtk_key_snooper_remove ##### -->
653 Removes the key snooper function with the given id.
656 @snooper_handler_id: Identifies the key snooper to remove.
659 <!-- ##### FUNCTION gtk_get_current_event ##### -->
667 <!-- ##### FUNCTION gtk_get_current_event_time ##### -->
675 <!-- ##### FUNCTION gtk_get_current_event_state ##### -->
684 <!-- ##### FUNCTION gtk_get_event_widget ##### -->
693 <!-- ##### FUNCTION gtk_propagate_event ##### -->