]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtkmain.sgml
move README.linux-fb in here
[~andy/gtk] / docs / reference / gtk / tmpl / gtkmain.sgml
1 <!-- ##### SECTION Title ##### -->
2 Main loop and Events
3
4 <!-- ##### SECTION Short_Description ##### -->
5 Library initialization, main event loop, and events
6
7 <!-- ##### SECTION Long_Description ##### -->
8
9 <para>
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.
16 </para>
17
18 <para>
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
24 more widgets.
25 </para>
26
27 <para>
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>. 
33 </para>
34
35 <para>
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.
40 </para>
41
42 <example>
43 <title>Typical <function>main</function> function for a GTK+ application</title>
44 <programlisting>
45 int 
46 main (int argc, char **argv)
47 {
48   /* Initialize i18n support */
49   gtk_set_locale (<!>);
50
51   /* Initialize the widget set */
52   gtk_init (&amp;argc, &amp;argv);
53
54   /* Create the main window */
55   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
56
57   /* Set up our GUI elements */
58   ...
59
60   /* Show the application window */
61   gtk_widget_show_all (mainwin);
62
63   /* Enter the main event loop, and wait for user interaction */
64   gtk_main (<!>);
65
66   /* The user lost interest */
67   return 0;
68 }
69 </programlisting>
70 </example>
71
72 <para>
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
75 documentation.
76 </para>
77
78 <!-- ##### SECTION See_Also ##### -->
79 <para>
80 See the GLib manual, especially #GMainLoop and signal-related
81 functions such as g_signal_connect().
82 </para>
83
84 <!-- ##### FUNCTION gtk_set_locale ##### -->
85
86 <para>
87 </para>
88
89 @Returns: 
90
91
92 <!-- ##### FUNCTION gtk_disable_setlocale ##### -->
93 <para>
94
95 </para>
96
97
98
99 <!-- ##### FUNCTION gtk_get_default_language ##### -->
100 <para>
101
102 </para>
103
104 @Returns: 
105
106
107 <!-- ##### FUNCTION gtk_init ##### -->
108 <para>
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.
114 </para>
115
116 <note>
117 <para>
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.
121 </para>
122 </note>
123
124 @argc: Address of the <parameter>argc</parameter> parameter of your 
125 <function>main</function> function. Changed if any arguments were 
126 handled.
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.
130
131
132 <!-- ##### FUNCTION gtk_init_check ##### -->
133 <para>
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.
137 </para>
138 <para>
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.
141 </para>
142
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, 
148 %FALSE otherwise.
149
150
151 <!-- ##### FUNCTION gtk_exit ##### -->
152 <para>
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 
155 for GTK+.
156 </para>
157
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 
160 success.
161
162
163 <!-- ##### FUNCTION gtk_events_pending ##### -->
164 <para>
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.
167 </para>
168
169 <example>
170 <title>Updating the GUI during a long computation.</title>
171 <programlisting>
172         /* computation going on */
173 ...
174         while (gtk_events_pending (<!>))
175           gtk_main_iteration (<!>);
176 ...
177         /* computation continued */
178 </programlisting>
179 </example>
180
181 @Returns: %TRUE if any events are pending, %FALSE otherwise.
182
183
184 <!-- ##### FUNCTION gtk_main ##### -->
185 <para>
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.
189 </para>
190
191
192
193 <!-- ##### FUNCTION gtk_main_level ##### -->
194 <para>
195 Asks for the current nesting level of the main loop. This can be useful
196 when calling gtk_quit_add().
197 </para>
198
199 @Returns: the nesting level of the current invocation of the main loop.
200
201
202 <!-- ##### FUNCTION gtk_main_quit ##### -->
203 <para>
204 Makes the innermost invocation of the main loop return when it regains 
205 control.
206 </para>
207
208
209
210 <!-- ##### FUNCTION gtk_main_iteration ##### -->
211 <para>
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.
216 </para>
217
218 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
219
220
221 <!-- ##### FUNCTION gtk_main_iteration_do ##### -->
222 <para>
223 Runs a single iteration of the mainloop. If no events are available either
224 return or block dependent on the value of @blocking. 
225 </para>
226
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.
229
230
231 <!-- ##### FUNCTION gtk_main_do_event ##### -->
232 <para>
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.
235 </para>
236 <para>
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 
239 the event:
240 </para>
241
242 <orderedlist>
243 <listitem><para>
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.
248 </para></listitem>
249 <listitem><para>
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().
253 </para></listitem>
254 <listitem><para>
255   Then the event is passed on a stack so you can query the currently handled
256   event with gtk_get_current_event(). 
257 </para></listitem>
258 <listitem><para>
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: 
262
263   <itemizedlist>
264   <listitem><para>
265     Deletion and destruction events are still sent to the event widget for
266     obvious reasons.
267   </para></listitem>
268   <listitem><para>
269     Events which directly relate to the visual representation of the event
270     widget.
271   </para></listitem>
272   <listitem><para>
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.
275   </para></listitem>
276   <listitem><para>
277     Drag events are not redirected because it is unclear what the semantics
278     of that would be.
279   </para></listitem>
280   </itemizedlist>
281
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.
285 </para></listitem>
286 <listitem><para>
287   After finishing the delivery the event is popped from the event stack.
288 </para></listitem>
289 </orderedlist>
290
291 @event: An event to process (normally) passed by GDK.
292
293
294 <!-- ##### USER_FUNCTION GtkModuleInitFunc ##### -->
295 <para>
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.
299 </para>
300
301 @argc: Pointer to the number of arguments remaining after gtk_init().
302 @argv: Points to the argument vector.
303
304
305 <!-- ##### FUNCTION gtk_true ##### -->
306 <para>
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...
311 </para>
312
313 <example>
314 <title>A persistent window</title>
315 <programlisting>
316 ##include &lt;gtk/gtk.h&gt;
317
318 int
319 main (int argc, char **argv)
320 {
321   GtkWidget     *win, *but;
322
323   gtk_init( &amp;argc, &amp;argv );
324
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);
330
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);
336
337   gtk_widget_show_all (win);
338   gtk_main (<!>);
339   return 0;
340 }
341 </programlisting>
342 </example>
343
344 @Returns: %TRUE
345
346
347 <!-- ##### FUNCTION gtk_false ##### -->
348 <para>
349 Analogical to gtk_true() this function does nothing 
350 but always returns %FALSE.
351 </para>
352
353 @Returns: %FALSE
354
355
356 <!-- ##### FUNCTION gtk_grab_add ##### -->
357 <para>
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.
361 </para>
362
363 @widget: The widget that grabs keyboard and pointer events.
364
365
366 <!-- ##### FUNCTION gtk_grab_get_current ##### -->
367 <para>
368 Queries the current grab. 
369 </para>
370
371 @Returns: The widget which currently has the grab or %NULL if no grab is active.
372
373
374 <!-- ##### FUNCTION gtk_grab_remove ##### -->
375 <para>
376 Removes the grab from the given widget. You have to pair calls to gtk_grab_add()
377 and gtk_grab_remove().
378 </para>
379
380 @widget: The widget which gives up the grab.
381
382
383 <!-- ##### FUNCTION gtk_init_add ##### -->
384 <para>
385 Registers a function to be called when the mainloop is started.
386 </para>
387
388 @function: Function to invoke when gtk_main() is called next.
389 @data: Data to pass to that function.
390
391
392 <!-- ##### FUNCTION gtk_quit_add_destroy ##### -->
393 <para>
394 Trigger destruction of @object in case the mainloop at level @main_level
395 is quit.
396 </para>
397
398 @main_level: Level of the mainloop which shall trigger the destruction.
399 @object: Object to be destroyed.
400
401
402 <!-- ##### FUNCTION gtk_quit_add ##### -->
403 <para>
404 Registers a function to be called when an instance of the mainloop is left.
405 </para>
406
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
409  mainloop.
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.
415
416
417 <!-- ##### FUNCTION gtk_quit_add_full ##### -->
418 <para>
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.
422 </para>
423 <para>
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.
428 </para>
429
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
432  mainloop.
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 
436  ignored.
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.
441
442
443 <!-- ##### FUNCTION gtk_quit_remove ##### -->
444 <para>
445 Removes a quit handler by it's identifier.
446 </para>
447
448 @quit_handler_id: Identifier for the handler returned when installing it.
449
450
451 <!-- ##### FUNCTION gtk_quit_remove_by_data ##### -->
452 <para>
453 Removes a quit handler identified by it's @data field.
454 </para>
455
456 @data: The pointer passed as @data to gtk_quit_add() or gtk_quit_add_full().
457
458
459 <!-- ##### FUNCTION gtk_timeout_add_full ##### -->
460 <para>
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.
464 </para>
465
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.
473
474
475 <!-- ##### FUNCTION gtk_timeout_add ##### -->
476 <para>
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.
480 </para>
481
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.
487
488
489 <!-- ##### FUNCTION gtk_timeout_remove ##### -->
490 <para>
491 Removes the given timeout destroying all information about it.
492 </para>
493
494 @timeout_handler_id: The identifier returned when installing the timeout.
495
496
497 <!-- ##### FUNCTION gtk_idle_add ##### -->
498 <para>
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.
502 </para>
503
504 @function: The function to call.
505 @data: The information to pass to the function.
506 @Returns: a unique handle for this registration.
507
508
509 <!-- ##### FUNCTION gtk_idle_add_priority ##### -->
510 <para>
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.
514 </para>
515
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.
522
523
524 <!-- ##### FUNCTION gtk_idle_add_full ##### -->
525 <para>
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.
529 </para>
530
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.
539
540
541 <!-- ##### FUNCTION gtk_idle_remove ##### -->
542 <para>
543 Removes the idle function with the given id.
544 </para>
545
546 @idle_handler_id: Identifies the idle function to remove.
547
548
549 <!-- ##### FUNCTION gtk_idle_remove_by_data ##### -->
550 <para>
551 Removes the idle function identified by the user data.
552 </para>
553
554 @data: remove the idle function which was registered with this user data.
555
556
557 <!-- ##### FUNCTION gtk_input_add_full ##### -->
558 <para>
559 Registers a function to be called when a condition becomes true 
560 on a file descriptor.
561 </para>
562
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().
571
572
573 <!-- ##### FUNCTION gtk_input_remove ##### -->
574 <para>
575 Removes the function with the given id.
576 </para>
577
578 @input_handler_id: Identifies the function to remove.
579
580
581 <!-- ##### MACRO GTK_PRIORITY_REDRAW ##### -->
582 <para>
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
586 it again).
587 </para>
588 <note><para>
589 This macro is deprecated. You should use %GDK_PRIORITY_REDRAW instead.
590 </para></note>
591
592
593
594 <!-- ##### MACRO GTK_PRIORITY_RESIZE ##### -->
595 <para>
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.
599 </para>
600
601
602
603 <!-- ##### MACRO GTK_PRIORITY_HIGH ##### -->
604 <para>
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
607 inside the toolkit.
608 <note><para>
609 This macro is deprecated. You should use %G_PRIORITY_HIGH instead.
610 </para></note>
611 </para>
612
613
614
615 <!-- ##### MACRO GTK_PRIORITY_INTERNAL ##### -->
616 <para>
617 This priority is for GTK+ internal stuff. Don't use it in your applications.
618 </para>
619
620
621
622 <!-- ##### MACRO GTK_PRIORITY_DEFAULT ##### -->
623 <para>
624 Default priority for idle functions.
625 <note><para>
626 This macro is deprecated. You should use %G_PRIORITY_DEFAULT_IDLE instead.
627 </para></note>
628 </para>
629
630
631
632 <!-- ##### MACRO GTK_PRIORITY_LOW ##### -->
633 <para>
634 Priority for very unimportant background tasks.
635 <note><para>
636 This macro is deprecated. You should use %G_PRIORITY_LOW instead.
637 </para></note>
638 </para>
639
640
641
642 <!-- ##### FUNCTION gtk_key_snooper_install ##### -->
643 <para>
644 Installs a key snooper function, which will get called on all key events
645 before delivering them normally.
646 </para>
647
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().
651
652
653 <!-- ##### USER_FUNCTION GtkKeySnoopFunc ##### -->
654 <para>
655 Key snooper functions are called before normal event delivery.
656 They can be used to implement custom key event handling.
657 </para>
658
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.
663
664
665 <!-- ##### FUNCTION gtk_key_snooper_remove ##### -->
666 <para>
667 Removes the key snooper function with the given id.
668 </para>
669
670 @snooper_handler_id: Identifies the key snooper to remove.
671
672
673 <!-- ##### FUNCTION gtk_get_current_event ##### -->
674 <para>
675
676 </para>
677
678 @Returns: 
679
680
681 <!-- ##### FUNCTION gtk_get_current_event_time ##### -->
682 <para>
683
684 </para>
685
686 @Returns: 
687
688
689 <!-- ##### FUNCTION gtk_get_current_event_state ##### -->
690 <para>
691
692 </para>
693
694 @state: 
695 @Returns: 
696
697
698 <!-- ##### FUNCTION gtk_get_event_widget ##### -->
699 <para>
700
701 </para>
702
703 @event: 
704 @Returns: 
705
706
707 <!-- ##### FUNCTION gtk_propagate_event ##### -->
708 <para>
709
710 </para>
711
712 @widget: 
713 @event: 
714
715