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 */
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 ##### -->
109 Call this function before using any other GTK+ functions in your GUI
110 applications. It will initialize everything needed to operate the toolkit and
111 parses some standard command line options. <parameter>argc</parameter> and
112 <parameter>argv</parameter> are adjusted accordingly so your own code will
113 never see those standard arguments.
118 This function will terminate your program if it was unable to initialize
119 the GUI for some reason. If you want your program to fall back to a
120 textual interface you want to call gtk_init_check() instead.
124 @argc: Address of the <parameter>argc</parameter> parameter of your
125 <function>main</function> function. Changed if any arguments were
127 @argv: Address of the <parameter>argv</parameter> parameter of
128 <function>main()</function>. Any parameters understood by gtk_init()
129 are stripped before return.
132 <!-- ##### FUNCTION gtk_init_check ##### -->
134 This function does the same work as gtk_init() with only
135 a single change: It does not terminate the program if the GUI can't be
136 initialized. Instead it returns %FALSE on failure.
139 This way the application can fall back to some other means of communication
140 with the user - for example a curses or command line interface.
143 @argc: A reference to the <parameter>argc</parameter> of the
144 <function>main()</function> function.
145 @argv: A reference to the <parameter>argv</parameter> of the
146 <function>main()</function> function.
147 @Returns: %TRUE if the GUI has been successfully initialized,
151 <!-- ##### FUNCTION gtk_exit ##### -->
153 Terminates the program and returns the given exit code to the caller.
154 This function will shut down the GUI and free all resources allocated
158 @error_code: Return value to pass to the caller. This is dependend on the
159 target system but at least on Unix systems %0 means
163 <!-- ##### FUNCTION gtk_events_pending ##### -->
165 Checks if any events are pending. This can be used to update the GUI
166 and invoke timeouts etc. while doing some time intensive computation.
170 <title>Updating the GUI during a long computation.</title>
172 /* computation going on */
174 while (gtk_events_pending (<!>))
175 gtk_main_iteration (<!>);
177 /* computation continued */
181 @Returns: %TRUE if any events are pending, %FALSE otherwise.
184 <!-- ##### FUNCTION gtk_main ##### -->
186 Runs the main loop until gtk_main_quit() is called. You can nest calls to
187 gtk_main(). In that case gtk_main_quit() will make the innermost invocation
188 of the main loop return.
193 <!-- ##### FUNCTION gtk_main_level ##### -->
195 Asks for the current nesting level of the main loop. This can be useful
196 when calling gtk_quit_add().
199 @Returns: the nesting level of the current invocation of the main loop.
202 <!-- ##### FUNCTION gtk_main_quit ##### -->
204 Makes the innermost invocation of the main loop return when it regains
210 <!-- ##### FUNCTION gtk_main_iteration ##### -->
212 Runs a single iteration of the mainloop. If no events are waiting to be
213 processed GTK+ will block until the next event is noticed. If you don't
214 want to block look at gtk_main_iteration_do() or check if any events are
215 pending with gtk_events_pending() first.
218 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
221 <!-- ##### FUNCTION gtk_main_iteration_do ##### -->
223 Runs a single iteration of the mainloop. If no events are available either
224 return or block dependent on the value of @blocking.
227 @blocking: %TRUE if you want GTK+ to block if no events are pending.
228 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
231 <!-- ##### FUNCTION gtk_main_do_event ##### -->
233 Processes a single GDK event. This is public only to allow filtering of events
234 between GDK and GTK+. You will not usually need to call this function directly.
237 While you should not call this function directly, you might want to know
238 how exactly events are handled. So here is what this function does with
244 Compress enter/leave notify events. If the event passed build an
245 enter/leave pair together with the next event (peeked from GDK)
246 both events are thrown away. This is to avoid a backlog of (de-)highlighting
247 widgets crossed by the pointer.
250 Find the widget which got the event. If the widget can't be determined
251 the event is thrown away unless it belongs to a INCR transaction. In that
252 case it is passed to gtk_selection_incr_event().
255 Then the event is passed on a stack so you can query the currently handled
256 event with gtk_get_current_event().
259 The event is sent to a widget. If a grab is active all events for
260 widgets that are not in the contained in the grab widget are sent to the
261 latter with a few exceptions:
265 Deletion and destruction events are still sent to the event widget for
269 Events which directly relate to the visual representation of the event
273 Leave events are delivered to the event widget if there was an enter
274 event delivered to it before without the paired leave event.
277 Drag events are not redirected because it is unclear what the semantics
282 Another point of interest might be that all key events are first passed
283 through the key snooper functions if there are any. Read the description
284 of gtk_key_snooper_install() if you need this feature.
287 After finishing the delivery the event is popped from the event stack.
291 @event: An event to process (normally) passed by GDK.
294 <!-- ##### USER_FUNCTION GtkModuleInitFunc ##### -->
296 Each GTK+ module must have a function gtk_module_init() with this prototype.
297 This function is called after loading the module with the @argc and @argv
298 cleaned from any arguments that GTK+ handles itself.
301 @argc: Pointer to the number of arguments remaining after gtk_init().
302 @argv: Points to the argument vector.
305 <!-- ##### FUNCTION gtk_true ##### -->
307 All this function does it to return %TRUE. This can be useful for example
308 if you want to inhibit the deletion of a window. Of course you should
309 not do this as the user expects a reaction from clicking the close
310 icon of the window...
314 <title>A persistent window</title>
316 ##include <gtk/gtk.h>
319 main (int argc, char **argv)
321 GtkWidget *win, *but;
323 gtk_init( &argc, &argv );
325 win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
326 gtk_signal_connect (GTK_OBJECT(win), "delete-event",
327 (GtkSignalFunc) gtk_true, NULL);
328 gtk_signal_connect (GTK_OBJECT(win), "destroy",
329 (GtkSignalFunc) gtk_main_quit, NULL);
331 but = gtk_button_new_with_label ("Close yourself. I mean it!");
332 gtk_signal_connect_object (
333 GTK_OBJECT (but), "clicked",
334 (GtkSignalFunc) gtk_object_destroy, (gpointer) win );
335 gtk_container_add (GTK_CONTAINER (win), but);
337 gtk_widget_show_all (win);
347 <!-- ##### FUNCTION gtk_false ##### -->
349 Analogical to gtk_true() this function does nothing
350 but always returns %FALSE.
356 <!-- ##### FUNCTION gtk_grab_add ##### -->
358 Makes @widget the current grabbed widget. This means that interaction with
359 other widgets in the same application is blocked and mouse as well as
360 keyboard events are delivered to this widget.
363 @widget: The widget that grabs keyboard and pointer events.
366 <!-- ##### FUNCTION gtk_grab_get_current ##### -->
368 Queries the current grab.
371 @Returns: The widget which currently has the grab or %NULL if no grab is active.
374 <!-- ##### FUNCTION gtk_grab_remove ##### -->
376 Removes the grab from the given widget. You have to pair calls to gtk_grab_add()
377 and gtk_grab_remove().
380 @widget: The widget which gives up the grab.
383 <!-- ##### FUNCTION gtk_init_add ##### -->
385 Registers a function to be called when the mainloop is started.
388 @function: Function to invoke when gtk_main() is called next.
389 @data: Data to pass to that function.
392 <!-- ##### FUNCTION gtk_quit_add_destroy ##### -->
394 Trigger destruction of @object in case the mainloop at level @main_level
398 @main_level: Level of the mainloop which shall trigger the destruction.
399 @object: Object to be destroyed.
402 <!-- ##### FUNCTION gtk_quit_add ##### -->
404 Registers a function to be called when an instance of the mainloop is left.
407 @main_level: Level at which termination the function shall be called. You
408 can pass 0 here to have the function run at the termination of the current
410 @function: The function to call. This should return 0 to be removed from the
411 list of quit handlers. Otherwise the function might be called again.
412 @data: Pointer to pass when calling @function.
413 @Returns: A handle for this quit handler (you need this for gtk_quit_remove())
414 or 0 if you passed a %NULL pointer in @function.
417 <!-- ##### FUNCTION gtk_quit_add_full ##### -->
419 Registers a function to be called when an instance of the mainloop is left.
420 In comparison to gtk_quit_add() this function adds the possibility to
421 pass a marshaller and a function to be called when the quit handler is freed.
424 The former can be used to run interpreted code instead of a compiled function
425 while the latter can be used to free the information stored in @data (while
426 you can do this in @function as well)... So this function will mostly be
427 used by GTK+ wrappers for languages other than C.
430 @main_level: Level at which termination the function shall be called. You
431 can pass 0 here to have the function run at the termination of the current
433 @function: The function to call. This should return 0 to be removed from the
434 list of quit handlers. Otherwise the function might be called again.
435 @marshal: The marshaller to be used. If this is non-%NULL, @function is
437 @data: Pointer to pass when calling @function.
438 @destroy: Function to call to destruct @data. Gets @data as argument.
439 @Returns: A handle for this quit handler (you need this for gtk_quit_remove())
440 or 0 if you passed a %NULL pointer in @function.
443 <!-- ##### FUNCTION gtk_quit_remove ##### -->
445 Removes a quit handler by it's identifier.
448 @quit_handler_id: Identifier for the handler returned when installing it.
451 <!-- ##### FUNCTION gtk_quit_remove_by_data ##### -->
453 Removes a quit handler identified by it's @data field.
456 @data: The pointer passed as @data to gtk_quit_add() or gtk_quit_add_full().
459 <!-- ##### FUNCTION gtk_timeout_add_full ##### -->
461 Registers a function to be called periodically. The function will be called
462 repeatedly after @interval milliseconds until it returns %FALSE at which
463 point the timeout is destroyed and will not be called again.
466 @interval: The time between calls to the function, in milliseconds
467 (1/1000ths of a second.)
468 @function: The function to call periodically.
469 @marshal: The marshaller to use instead of the function (if non-%NULL).
470 @data: The data to pass to the function.
471 @destroy: Function to call when the timeout is destroyed or %NULL.
472 @Returns: A unique id for the event source.
475 <!-- ##### FUNCTION gtk_timeout_add ##### -->
477 Registers a function to be called periodically. The function will be called
478 repeatedly after @interval milliseconds until it returns %FALSE at which
479 point the timeout is destroyed and will not be called again.
482 @interval: The time between calls to the function, in milliseconds
483 (1/1000ths of a second.)
484 @function: The function to call periodically.
485 @data: The data to pass to the function.
486 @Returns: A unique id for the event source.
489 <!-- ##### FUNCTION gtk_timeout_remove ##### -->
491 Removes the given timeout destroying all information about it.
494 @timeout_handler_id: The identifier returned when installing the timeout.
497 <!-- ##### FUNCTION gtk_idle_add ##### -->
499 Causes the mainloop to call the given function whenever no events with
500 higher priority are to be processed. The default priority is
501 %GTK_PRIORITY_DEFAULT, which is rather low.
504 @function: The function to call.
505 @data: The information to pass to the function.
506 @Returns: a unique handle for this registration.
509 <!-- ##### FUNCTION gtk_idle_add_priority ##### -->
511 Like gtk_idle_add() this function allows you to have a function called
512 when the event loop is idle. The difference is that you can give a
513 priority different from %GTK_PRIORITY_DEFAULT to the idle function.
516 @priority: The priority which should not be above %G_PRIORITY_HIGH_IDLE.
517 Note that you will interfere with GTK+ if you use a priority above
518 %GTK_PRIORITY_RESIZE.
519 @function: The function to call.
520 @data: Data to pass to that function.
521 @Returns: A unique id for the event source.
524 <!-- ##### FUNCTION gtk_idle_add_full ##### -->
526 Like gtk_idle_add() this function allows you to have a function called
527 when the event loop is idle. The difference is that you can give a
528 priority different from %GTK_PRIORITY_DEFAULT to the idle function.
531 @priority: The priority which should not be above %G_PRIORITY_HIGH_IDLE.
532 Note that you will interfere with GTK+ if you use a priority above
533 %GTK_PRIORITY_RESIZE.
534 @function: The function to call.
535 @marshal: The marshaller to use instead of the function (if non-%NULL).
536 @data: Data to pass to that function.
537 @destroy: Function to call when the timeout is destroyed or %NULL.
538 @Returns: A unique id for the event source.
541 <!-- ##### FUNCTION gtk_idle_remove ##### -->
543 Removes the idle function with the given id.
546 @idle_handler_id: Identifies the idle function to remove.
549 <!-- ##### FUNCTION gtk_idle_remove_by_data ##### -->
551 Removes the idle function identified by the user data.
554 @data: remove the idle function which was registered with this user data.
557 <!-- ##### FUNCTION gtk_input_add_full ##### -->
559 Registers a function to be called when a condition becomes true
560 on a file descriptor.
563 @source: a file descriptor.
564 @condition: the condition.
565 @function: The function to call.
566 @marshal: The marshaller to use instead of the function (if non-%NULL).
567 @data: callback data passed to @function.
568 @destroy: callback function to call with @data when the input
569 handler is removed, or %NULL.
570 @Returns: A unique id for the event source; to be used with gtk_input_remove().
573 <!-- ##### FUNCTION gtk_input_remove ##### -->
575 Removes the function with the given id.
578 @input_handler_id: Identifies the function to remove.
581 <!-- ##### MACRO GTK_PRIORITY_REDRAW ##### -->
583 Use this priority for redrawing related stuff. It is used internally by
584 GTK+ to do pending redraws. This priority is lower than %GTK_PRIORITY_RESIZE
585 to avoid redrawing a widget just before resizing (and therefore redrawing
589 This macro is deprecated. You should use %GDK_PRIORITY_REDRAW instead.
594 <!-- ##### MACRO GTK_PRIORITY_RESIZE ##### -->
596 Use this priority for resizing related stuff. It is used internally by
597 GTK+ to compute the sizes of widgets. This priority is higher than
598 %GTK_PRIORITY_REDRAW to avoid resizing a widget which was just redrawn.
603 <!-- ##### MACRO GTK_PRIORITY_HIGH ##### -->
605 Use this for high priority timeouts. This priority is never used inside
606 GTK+ so everything running at this priority will be running before anything
609 This macro is deprecated. You should use %G_PRIORITY_HIGH instead.
615 <!-- ##### MACRO GTK_PRIORITY_INTERNAL ##### -->
617 This priority is for GTK+ internal stuff. Don't use it in your applications.
622 <!-- ##### MACRO GTK_PRIORITY_DEFAULT ##### -->
624 Default priority for idle functions.
626 This macro is deprecated. You should use %G_PRIORITY_DEFAULT_IDLE instead.
632 <!-- ##### MACRO GTK_PRIORITY_LOW ##### -->
634 Priority for very unimportant background tasks.
636 This macro is deprecated. You should use %G_PRIORITY_LOW instead.
642 <!-- ##### FUNCTION gtk_key_snooper_install ##### -->
644 Installs a key snooper function, which will get called on all key events
645 before delivering them normally.
648 @snooper: a #GtkKeySnoopFunc.
649 @func_data: data to pass to @snooper.
650 @Returns: a unique id for this key snooper for use with gtk_key_snooper_remove().
653 <!-- ##### USER_FUNCTION GtkKeySnoopFunc ##### -->
655 Key snooper functions are called before normal event delivery.
656 They can be used to implement custom key event handling.
659 @grab_widget: the widget to which the event will be delivered.
660 @event: the key event.
661 @func_data: the @func_data supplied to gtk_key_snooper_install().
662 @Returns: %TRUE to stop further processing of @event, %FALSE to continue.
665 <!-- ##### FUNCTION gtk_key_snooper_remove ##### -->
667 Removes the key snooper function with the given id.
670 @snooper_handler_id: Identifies the key snooper to remove.
673 <!-- ##### FUNCTION gtk_get_current_event ##### -->
681 <!-- ##### FUNCTION gtk_get_current_event_time ##### -->
689 <!-- ##### FUNCTION gtk_get_current_event_state ##### -->
698 <!-- ##### FUNCTION gtk_get_event_widget ##### -->
707 <!-- ##### FUNCTION gtk_propagate_event ##### -->