]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/tmpl/gtkmain.sgml
Add "Since" information.
[~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 </para>
110
111 <note>
112 <para>
113 </para>
114 </note>
115
116 @argc: 
117 @argv: 
118
119
120 <!-- ##### FUNCTION gtk_init_check ##### -->
121 <para>
122 </para>
123
124 @argc: 
125 @argv: 
126 @Returns: 
127
128
129 <!-- ##### FUNCTION gtk_exit ##### -->
130 <para>
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 
133 for GTK+.
134 </para>
135
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 
138 success.
139
140
141 <!-- ##### FUNCTION gtk_events_pending ##### -->
142 <para>
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.
145 </para>
146
147 <example>
148 <title>Updating the GUI during a long computation.</title>
149 <programlisting>
150         /* computation going on */
151 ...
152         while (gtk_events_pending (<!-- -->))
153           gtk_main_iteration (<!-- -->);
154 ...
155         /* computation continued */
156 </programlisting>
157 </example>
158
159 @Returns: %TRUE if any events are pending, %FALSE otherwise.
160
161
162 <!-- ##### FUNCTION gtk_main ##### -->
163 <para>
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.
167 </para>
168
169
170
171 <!-- ##### FUNCTION gtk_main_level ##### -->
172 <para>
173 Asks for the current nesting level of the main loop. This can be useful
174 when calling gtk_quit_add().
175 </para>
176
177 @Returns: the nesting level of the current invocation of the main loop.
178
179
180 <!-- ##### FUNCTION gtk_main_quit ##### -->
181 <para>
182 Makes the innermost invocation of the main loop return when it regains 
183 control.
184 </para>
185
186
187
188 <!-- ##### FUNCTION gtk_main_iteration ##### -->
189 <para>
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.
194 </para>
195
196 @Returns: %TRUE if gtk_main_quit() has been called for the innermost mainloop.
197
198
199 <!-- ##### FUNCTION gtk_main_iteration_do ##### -->
200 <para>
201 Runs a single iteration of the mainloop. If no events are available either
202 return or block dependent on the value of @blocking. 
203 </para>
204
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.
207
208
209 <!-- ##### FUNCTION gtk_main_do_event ##### -->
210 <para>
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.
213 </para>
214 <para>
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 
217 the event:
218 </para>
219
220 <orderedlist>
221 <listitem><para>
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.
226 </para></listitem>
227 <listitem><para>
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().
231 </para></listitem>
232 <listitem><para>
233   Then the event is passed on a stack so you can query the currently handled
234   event with gtk_get_current_event(). 
235 </para></listitem>
236 <listitem><para>
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: 
240
241   <itemizedlist>
242   <listitem><para>
243     Deletion and destruction events are still sent to the event widget for
244     obvious reasons.
245   </para></listitem>
246   <listitem><para>
247     Events which directly relate to the visual representation of the event
248     widget.
249   </para></listitem>
250   <listitem><para>
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.
253   </para></listitem>
254   <listitem><para>
255     Drag events are not redirected because it is unclear what the semantics
256     of that would be.
257   </para></listitem>
258   </itemizedlist>
259
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.
263 </para></listitem>
264 <listitem><para>
265   After finishing the delivery the event is popped from the event stack.
266 </para></listitem>
267 </orderedlist>
268
269 @event: An event to process (normally) passed by GDK.
270
271
272 <!-- ##### USER_FUNCTION GtkModuleInitFunc ##### -->
273 <para>
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.
277 </para>
278
279 @argc: Pointer to the number of arguments remaining after gtk_init().
280 @argv: Points to the argument vector.
281
282
283 <!-- ##### USER_FUNCTION GtkModuleDisplayInitFunc ##### -->
284 <para>
285
286 </para>
287
288 @display: 
289 @Since: 2.2
290
291
292 <!-- ##### FUNCTION gtk_true ##### -->
293 <para>
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...
298 </para>
299
300 <example>
301 <title>A persistent window</title>
302 <programlisting>
303 ##include &lt;gtk/gtk.h&gt;
304
305 int
306 main (int argc, char **argv)
307 {
308   GtkWidget     *win, *but;
309
310   gtk_init( &amp;argc, &amp;argv );
311
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);
317
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);
322
323   gtk_widget_show_all (win);
324   gtk_main (<!-- -->);
325   return 0;
326 }
327 </programlisting>
328 </example>
329
330 @Returns: %TRUE
331
332
333 <!-- ##### FUNCTION gtk_false ##### -->
334 <para>
335 Analogical to gtk_true() this function does nothing 
336 but always returns %FALSE.
337 </para>
338
339 @Returns: %FALSE
340
341
342 <!-- ##### FUNCTION gtk_grab_add ##### -->
343 <para>
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.
347 </para>
348
349 @widget: The widget that grabs keyboard and pointer events.
350
351
352 <!-- ##### FUNCTION gtk_grab_get_current ##### -->
353 <para>
354 Queries the current grab. 
355 </para>
356
357 @Returns: The widget which currently has the grab or %NULL if no grab is active.
358
359
360 <!-- ##### FUNCTION gtk_grab_remove ##### -->
361 <para>
362 Removes the grab from the given widget. You have to pair calls to gtk_grab_add()
363 and gtk_grab_remove().
364 </para>
365
366 @widget: The widget which gives up the grab.
367
368
369 <!-- ##### FUNCTION gtk_init_add ##### -->
370 <para>
371 Registers a function to be called when the mainloop is started.
372 </para>
373
374 @function: Function to invoke when gtk_main() is called next.
375 @data: Data to pass to that function.
376
377
378 <!-- ##### FUNCTION gtk_quit_add_destroy ##### -->
379 <para>
380 Trigger destruction of @object in case the mainloop at level @main_level
381 is quit.
382 </para>
383
384 @main_level: Level of the mainloop which shall trigger the destruction.
385 @object: Object to be destroyed.
386
387
388 <!-- ##### FUNCTION gtk_quit_add ##### -->
389 <para>
390 Registers a function to be called when an instance of the mainloop is left.
391 </para>
392
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
395  mainloop.
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.
401
402
403 <!-- ##### FUNCTION gtk_quit_add_full ##### -->
404 <para>
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.
408 </para>
409 <para>
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.
414 </para>
415
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
418  mainloop.
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 
422  ignored.
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.
427
428
429 <!-- ##### FUNCTION gtk_quit_remove ##### -->
430 <para>
431 Removes a quit handler by it's identifier.
432 </para>
433
434 @quit_handler_id: Identifier for the handler returned when installing it.
435
436
437 <!-- ##### FUNCTION gtk_quit_remove_by_data ##### -->
438 <para>
439 Removes a quit handler identified by it's @data field.
440 </para>
441
442 @data: The pointer passed as @data to gtk_quit_add() or gtk_quit_add_full().
443
444
445 <!-- ##### FUNCTION gtk_timeout_add_full ##### -->
446 <para>
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.
450 </para>
451
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.
459
460
461 <!-- ##### FUNCTION gtk_timeout_add ##### -->
462 <para>
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.
466 </para>
467
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.
473
474
475 <!-- ##### FUNCTION gtk_timeout_remove ##### -->
476 <para>
477 Removes the given timeout destroying all information about it.
478 </para>
479
480 @timeout_handler_id: The identifier returned when installing the timeout.
481
482
483 <!-- ##### FUNCTION gtk_idle_add ##### -->
484 <para>
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.
488 </para>
489
490 @function: The function to call.
491 @data: The information to pass to the function.
492 @Returns: a unique handle for this registration.
493
494
495 <!-- ##### FUNCTION gtk_idle_add_priority ##### -->
496 <para>
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.
500 </para>
501
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.
508
509
510 <!-- ##### FUNCTION gtk_idle_add_full ##### -->
511 <para>
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.
515 </para>
516
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.
525
526
527 <!-- ##### FUNCTION gtk_idle_remove ##### -->
528 <para>
529 Removes the idle function with the given id.
530 </para>
531
532 @idle_handler_id: Identifies the idle function to remove.
533
534
535 <!-- ##### FUNCTION gtk_idle_remove_by_data ##### -->
536 <para>
537 Removes the idle function identified by the user data.
538 </para>
539
540 @data: remove the idle function which was registered with this user data.
541
542
543 <!-- ##### FUNCTION gtk_input_add_full ##### -->
544 <para>
545 Registers a function to be called when a condition becomes true 
546 on a file descriptor.
547 </para>
548
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().
557
558
559 <!-- ##### FUNCTION gtk_input_remove ##### -->
560 <para>
561 Removes the function with the given id.
562 </para>
563
564 @input_handler_id: Identifies the function to remove.
565
566
567 <!-- ##### MACRO GTK_PRIORITY_REDRAW ##### -->
568 <para>
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
572 it again).
573 </para>
574 <note><para>
575 This macro is deprecated. You should use %GDK_PRIORITY_REDRAW instead.
576 </para></note>
577
578
579
580 <!-- ##### MACRO GTK_PRIORITY_RESIZE ##### -->
581 <para>
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.
585 </para>
586
587
588
589 <!-- ##### MACRO GTK_PRIORITY_HIGH ##### -->
590 <para>
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
593 inside the toolkit.
594 <note><para>
595 This macro is deprecated. You should use %G_PRIORITY_HIGH instead.
596 </para></note>
597 </para>
598
599
600
601 <!-- ##### MACRO GTK_PRIORITY_INTERNAL ##### -->
602 <para>
603 This priority is for GTK+ internal stuff. Don't use it in your applications.
604 </para>
605
606
607
608 <!-- ##### MACRO GTK_PRIORITY_DEFAULT ##### -->
609 <para>
610 Default priority for idle functions.
611 <note><para>
612 This macro is deprecated. You should use %G_PRIORITY_DEFAULT_IDLE instead.
613 </para></note>
614 </para>
615
616
617
618 <!-- ##### MACRO GTK_PRIORITY_LOW ##### -->
619 <para>
620 Priority for very unimportant background tasks.
621 <note><para>
622 This macro is deprecated. You should use %G_PRIORITY_LOW instead.
623 </para></note>
624 </para>
625
626
627
628 <!-- ##### FUNCTION gtk_key_snooper_install ##### -->
629 <para>
630 Installs a key snooper function, which will get called on all key events
631 before delivering them normally.
632 </para>
633
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().
637
638
639 <!-- ##### USER_FUNCTION GtkKeySnoopFunc ##### -->
640 <para>
641 Key snooper functions are called before normal event delivery.
642 They can be used to implement custom key event handling.
643 </para>
644
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.
649
650
651 <!-- ##### FUNCTION gtk_key_snooper_remove ##### -->
652 <para>
653 Removes the key snooper function with the given id.
654 </para>
655
656 @snooper_handler_id: Identifies the key snooper to remove.
657
658
659 <!-- ##### FUNCTION gtk_get_current_event ##### -->
660 <para>
661
662 </para>
663
664 @Returns: 
665
666
667 <!-- ##### FUNCTION gtk_get_current_event_time ##### -->
668 <para>
669
670 </para>
671
672 @Returns: 
673
674
675 <!-- ##### FUNCTION gtk_get_current_event_state ##### -->
676 <para>
677
678 </para>
679
680 @state: 
681 @Returns: 
682
683
684 <!-- ##### FUNCTION gtk_get_event_widget ##### -->
685 <para>
686
687 </para>
688
689 @event: 
690 @Returns: 
691
692
693 <!-- ##### FUNCTION gtk_propagate_event ##### -->
694 <para>
695
696 </para>
697
698 @widget: 
699 @event: 
700
701