]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/question_index.sgml
stylecontext: Do invalidation on first resize container
[~andy/gtk] / docs / reference / gtk / question_index.sgml
1 <?xml version="1.0"?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
3                "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
4 ]>
5 <refentry id="gtk-question-index">
6 <refmeta>
7 <refentrytitle>Common Questions</refentrytitle>
8 <manvolnum>3</manvolnum>
9 <refmiscinfo>Common Questions</refmiscinfo>
10 </refmeta>
11
12 <refnamediv>
13 <refname>Common Questions</refname>
14 <refpurpose>
15 Find answers to common questions in the GTK+ manual
16 </refpurpose>
17 </refnamediv>
18
19 <refsect1>
20 <title>Questions and Answers</title>
21
22 <para>
23 This is an "index" of the reference manual organized by common "How do
24 I..." questions. If you aren't sure which documentation to read for
25 the question you have, this list is a good place to start.
26 </para>
27
28 <qandaset>
29
30 <qandadiv><title>General</title>
31
32 <qandaentry>
33 <question><para>
34 How do I get started with GTK+?
35 </para></question>
36
37 <answer><para>
38 The GTK+ <ulink url="http://www.gtk.org">website</ulink> offers some
39 <ulink url="http://www.gtk.org/documentation">tutorials</ulink> and other
40 documentation (most of it about GTK+ 2.x, but mostly still applicable).
41 More documentation ranging from whitepapers to online books can be found at
42 the <ulink url="http://developer.gnome.org">GNOME developer's site</ulink>.
43 After studying these materials you should be well prepared to come back to
44 this reference manual for details.
45 </para></answer>
46 </qandaentry>
47
48 <qandaentry>
49 <question><para>
50 Where can I get help with GTK+, submit a bug report, or make a feature
51 request?
52 </para></question>
53
54 <answer>
55
56 <para>
57 See the <link linkend="gtk-resources">documentation on this topic</link>.
58 </para>
59
60 </answer>
61
62 </qandaentry>
63
64
65 <qandaentry>
66 <question><para>How do I port from one GTK+
67 version to another?</para></question>
68
69 <answer>
70
71 <para>
72 See <xref linkend="gtk-migrating-2-to-3"/>.
73 You may also find useful information in the documentation for
74 specific widgets and functions.
75 </para>
76
77 <para>
78 If you have a question not covered in the manual, feel free to
79 ask on the mailing lists and please <ulink
80 url="http://bugzilla.gnome.org">file a bug report</ulink> against the
81 documentation.
82 </para>
83
84 </answer>
85
86 </qandaentry>
87
88
89 <qandaentry>
90 <question><para>
91 How does memory management work in GTK+? Should I free data returned
92 from functions?
93 </para></question>
94
95 <answer>
96
97 <para>
98 See the documentation for #GObject and #GInitiallyUnowned. For #GObject note
99 specifically g_object_ref() and g_object_unref(). #GInitiallyUnowned is a
100 subclass of #GObject so the same points apply, except that it has a "floating"
101 state (explained in its documentation).
102 </para>
103
104 <para>
105 For strings returned from functions, they will be declared "const"
106 if they should not be freed. Non-const strings should be
107 freed with g_free(). Arrays follow the same rule. If you find an
108 undocumented exception to the rules, please report a bug to <ulink
109 url="http://bugzilla.gnome.org">http://bugzilla.gnome.org</ulink>.
110 </para>
111
112 </answer>
113 </qandaentry>
114
115 <qandaentry>
116 <question>
117 <para>
118 Why does my program leak memory, if I destroy a widget immediately
119 after creating it ?
120 </para>
121 </question>
122
123 <answer>
124 <para>
125 If <structname>GtkFoo</structname> isn't a toplevel window, then
126 <informalexample><programlisting>
127  foo = gtk_foo_new (<!-- -->);
128  gtk_widget_destroy (foo);
129 </programlisting></informalexample>
130 is a memory leak, because no one assumed the initial floating
131 reference. If you are using a widget and you aren't immediately
132 packing it into a container, then you probably want standard
133 reference counting, not floating reference counting.
134 </para>
135
136 <para>
137 To to get this, you must acquire a reference to the widget and drop the
138 floating reference (<quote>ref and sink</quote> in GTK+ parlance) after
139 creating it:
140 <informalexample><programlisting>
141  foo = gtk_foo_new (<!-- -->);
142  g_object_ref_sink (foo);
143 </programlisting></informalexample>
144 When you want to get rid of the widget, you must call gtk_widget_destroy()
145 to break any external connections to the widget before dropping your
146 reference:
147 <informalexample><programlisting>
148  gtk_widget_destroy (foo);
149  g_object_unref (foo);
150 </programlisting></informalexample>
151 When you immediately add a widget to a container, it takes care of
152 assuming the initial floating reference and you don't have to worry
153 about reference counting at all ... just call gtk_widget_destroy()
154 to get rid of the widget.
155 </para>
156 </answer>
157 </qandaentry>
158
159 <qandaentry>
160 <question><para>
161 How do I use GTK+ with threads?
162 </para></question>
163
164 <answer>
165
166 <para>
167 This is covered in the <link linkend="gdk-Threads">GDK threads
168 documentation</link>. See also the <link linkend="glib-Threads">GThread</link>
169 documentation for portable threading primitives.
170 </para>
171
172 </answer>
173
174 </qandaentry>
175
176 <qandaentry>
177 <question><para>
178 How do I internationalize a GTK+ program?
179 </para></question>
180
181 <answer>
182 <para>
183 Most people use <ulink url="http://www.gnu.org/software/gettext/">GNU
184 gettext</ulink>, already required in order to install GLib. On a UNIX
185 or Linux system with gettext installed, type <literal>info gettext</literal>
186 to read the documentation.
187 </para>
188 <para>
189 The short checklist on how to use gettext is: call bindtextdomain() so
190 gettext can find the files containing your translations, call textdomain()
191 to set the default translation domain, call bind_textdomain_codeset() to
192 request that all translated strings are returned in UTF-8, then call
193 gettext() to look up each string to be translated in the default domain.
194 </para>
195 <para>
196 <filename>gi18n.h</filename> provides the following shorthand macros for
197 convenience.
198 Conventionally, people define macros as follows for convenience:
199 <informalexample>
200 <programlisting>
201   #define  _(x)     gettext (x)
202   #define N_(x)     x
203   #define C_(ctx,x) pgettext (ctx, x)
204 </programlisting>
205 </informalexample>
206 You use N_() (N stands for no-op) to mark a string for translation in
207 a location where a function call to gettext() is not allowed, such as
208 in an array initializer.
209 You eventually have to call gettext() on the string to actually fetch
210 the translation. _() both marks the string for translation and actually
211 translates it.
212 The C_() macro (C stands for context) adds an additional context to
213 the string that is marked for translation, which can help to disambiguate
214 short strings that might need different translations in different
215 parts of your program.
216 </para>
217 <para>
218 Code using these macros ends up looking like this:
219 <informalexample>
220 <programlisting>
221  #include &lt;gi18n.h&gt;
222
223  static const char *global_variable = N_("Translate this string");
224
225  static void
226  make_widgets (void)
227  {
228     GtkWidget *label1;
229     GtkWidget *label2;
230
231     label1 = gtk_label_new (_("Another string to translate"));
232     label2 = gtk_label_new (_(global_variable));
233 ...
234 </programlisting>
235 </informalexample>
236 </para>
237 <para>
238 Libraries using gettext should use dgettext() instead of gettext(), which
239 allows them to specify the translation domain each time they ask for a
240 translation. Libraries should also avoid calling textdomain(), since
241 they will be specifying the domain instead of using the default.
242 </para>
243 <para>
244 With the convention that the macro <literal>GETTEXT_PACKAGE</literal> is
245 defined to hold your libraries translation domain,
246 <filename>gi18n-lib.h</filename> can be included to provide
247 the following convenience:
248 <informalexample>
249 <programlisting>
250   #define _(x) dgettext (GETTEXT_PACKAGE, x)
251 </programlisting>
252 </informalexample>
253 </para>
254 </answer>
255 </qandaentry>
256
257 <qandaentry>
258 <question>
259 <para>
260 How do I use non-ASCII characters in GTK+ programs ?
261 </para>
262 </question>
263
264 <answer>
265 <para>
266 GTK+ uses <ulink url="http://www.unicode.org">Unicode</ulink> (more exactly
267 UTF-8) for all text. UTF-8 encodes each Unicode codepoint as a sequence of
268 one to six bytes and has a number of nice properties which make it a good
269 choice for working with Unicode text in C programs:
270 <itemizedlist>
271 <listitem><para>
272 ASCII characters are encoded by their familiar ASCII codepoints.
273 </para></listitem>
274 <listitem><para>
275 ASCII characters never appear as part of any other character.
276 </para></listitem>
277 <listitem><para>
278 The zero byte doesn't occur as part of a character, so that UTF-8 strings
279 can be manipulated with the usual C library functions for handling
280 zero-terminated strings.
281 </para></listitem>
282 </itemizedlist>
283 More information about Unicode and UTF-8 can be found in the
284 <ulink url="http://www.cl.cam.ac.uk/~mgk25/unicode.html">UTF-8 and Unicode
285 FAQ for Unix/Linux</ulink>.
286 GLib provides functions for converting strings between UTF-8 and other
287 encodings, see g_locale_to_utf8() and g_convert().
288 </para>
289 <para>
290 Text coming from external sources (e.g. files or user input), has to be
291 converted to UTF-8 before being handed over to GTK+. The following example
292 writes the content of a IS0-8859-1 encoded text file to
293 <literal>stdout</literal>:
294 <informalexample><programlisting>
295 gchar *text, *utf8_text;
296 gsize length;
297 GError *error = NULL;
298
299 if (g_file_get_contents (filename, &amp;text, &amp;length, NULL))
300   {
301      utf8_text = g_convert (text, length, "UTF-8", "ISO-8859-1",
302                             NULL, NULL, &amp;error);
303      if (error != NULL)
304        {
305          fprintf ("Couldn't convert file &percnt;s to UTF-8\n", filename);
306          g_error_free (error);
307        }
308      else
309        g_print (utf8_text);
310   }
311 else
312   fprintf (stderr, "Unable to read file &percnt;s\n", filename);
313 </programlisting></informalexample>
314 </para>
315 <para>
316 For string literals in the source code, there are several alternatives for
317 handling non-ASCII content:
318 <variablelist>
319 <varlistentry><term>direct UTF-8</term>
320 <listitem><para>
321 If your editor and compiler are capable of handling UTF-8 encoded sources,
322 it is very convenient to simply use UTF-8 for string literals, since it
323 allows you to edit the strings in "wysiwyg". Note that choosing this option
324 may reduce the portability of your code.
325 </para></listitem>
326 </varlistentry>
327
328 <varlistentry><term>escaped UTF-8</term>
329 <listitem><para>
330 Even if your toolchain can't handle UTF-8 directly, you can still encode
331 string literals in UTF-8 by using octal or hexadecimal escapes like
332 <literal>\212</literal> or <literal>\xa8</literal> to encode each byte.
333 This is portable, but modifying the escaped strings is not very convenient.
334 Be careful when mixing hexadecimal escapes with ordinary text;
335 <literal>"\xa8abcd"</literal> is a string of length 1 !
336 </para></listitem>
337 </varlistentry>
338
339 <varlistentry><term>runtime conversion</term>
340 <listitem><para>
341 If the string literals can be represented in an encoding which your
342 toolchain can handle (e.g. IS0-8859-1), you can write your source files
343 in that encoding and use g_convert() to convert the strings to UTF-8 at
344 runtime. Note that this has some runtime overhead, so you may want to move
345 the conversion out of inner loops.
346 </para></listitem>
347 </varlistentry>
348 </variablelist>
349 Here is an example showing the three approaches using the copyright sign
350 &copy; which has Unicode and ISO-8859-1 codepoint 169 and is represented
351 in UTF-8 by the two bytes 194, 169, or <literal>"\302\251"</literal> as
352 a string literal:
353 <informalexample><programlisting>
354 g_print ("direct UTF-8: &copy;");
355 g_print ("escaped UTF-8: \302\251");
356 text = g_convert ("runtime conversion: &copy;", -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL);
357 g_print(text);
358 g_free (text);
359 </programlisting></informalexample>
360 </para>
361 <para>
362 If you are using gettext() to localize your application, you need to
363 call bind_textdomain_codeset() to ensure that translated strings are
364 returned in UTF-8 encoding.
365 </para>
366 </answer>
367 </qandaentry>
368
369 <qandaentry>
370 <question><para>
371 How do I use GTK+ with C++?
372 </para></question>
373
374 <answer>
375 <para>
376 There are two ways to approach this. The GTK+ header files use the subset
377 of C that's also valid C++, so you can simply use the normal GTK+ API
378 in a C++ program. Alternatively, you can use a "C++ binding"
379 such as <ulink url="http://gtkmm.sourceforge.net/">gtkmm</ulink>
380 which provides a native C++ API.
381 </para>
382 <para>
383 When using GTK+ directly, keep in mind that only functions can be
384 connected to signals, not methods. So you will need to use global
385 functions or "static" class functions for signal connections.
386 </para>
387 <para>
388 Another common issue when using GTK+ directly is that
389 C++ will not implicitly convert an integer to an enumeration.
390 This comes up when using bitfields; in C you can write the following
391 code:
392 <informalexample>
393 <programlisting>
394   gdk_window_set_events (gdk_window,
395                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
396 </programlisting>
397 </informalexample>
398 while in C++ you must write:
399 <informalexample>
400 <programlisting>
401   gdk_window_set_events (gdk_window,
402                          (GdkEventMask) GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
403 </programlisting>
404 </informalexample>
405 There are very few functions that require this cast, however.
406 </para>
407 </answer>
408
409 </qandaentry>
410
411 <qandaentry>
412 <question><para>
413 How do I use GTK+ with other non-C languages?
414 </para></question>
415
416 <answer>
417 <para>
418 See the <ulink url="http://www.gtk.org/bindings.html">list of language
419 bindings</ulink> on <ulink
420 url="http://www.gtk.org">http://www.gtk.org</ulink>.
421 </para>
422
423 </answer>
424
425 </qandaentry>
426
427 <qandaentry>
428 <question><para>
429 How do I load an image or animation from a file?
430 </para></question>
431
432 <answer>
433
434 <para>
435 To load an image file straight into a display widget, use
436 gtk_image_new_from_file() <footnote><para> If the file load fails,
437 gtk_image_new_from_file() will display no image graphic &mdash; to detect
438 a failed load yourself, use gdk_pixbuf_new_from_file() directly, then
439 gtk_image_new_from_pixbuf().</para></footnote>.
440 To load an image for another purpose, use gdk_pixbuf_new_from_file(). To i
441 load an animation, use gdk_pixbuf_animation_new_from_file().
442 gdk_pixbuf_animation_new_from_file() can also load non-animated images, so
443 use it in combination with gdk_pixbuf_animation_is_static_image() to load a
444 file of unknown type.
445 </para>
446 <para>
447 To load an image or animation file asynchronously (without blocking), use
448 #GdkPixbufLoader.
449 </para>
450 </answer>
451
452 </qandaentry>
453
454 <qandaentry>
455 <question><para>
456 How do I draw text ?
457 </para></question>
458
459 <answer>
460 <para>
461 To draw a piece of text, use a Pango layout and pango_cairo_show_layout().
462 <informalexample>
463 <programlisting>
464  layout = gtk_widget_create_pango_layout (widget, text);
465  fontdesc = pango_font_description_from_string ("Luxi Mono 12");
466  pango_layout_set_font_description (layout, fontdesc);
467  pango_cairo_show_layout (cr, layout);
468  pango_font_description_free (fontdesc);
469  g_object_unref (layout);
470 </programlisting>
471 </informalexample>
472 </para>
473
474 <para>
475 See also the
476 <ulink url="http://developer.gnome.org/pango/stable/pango-Cairo-Rendering.html">Cairo Rendering</ulink>
477 section of <ulink url="http://developer.gnome.org/pango/stable/">Pango manual</ulink>.
478 </para>
479 </answer>
480
481 </qandaentry>
482
483 <qandaentry>
484 <question>
485 <para>
486 How do I measure the size of a piece of text ?
487 </para>
488 </question>
489
490 <answer>
491 <para>
492 To obtain the size of a piece of text, use a Pango layout and
493 pango_layout_get_pixel_size(), using code like the following:
494 <informalexample>
495 <programlisting>
496  layout = gtk_widget_create_pango_layout (widget, text);
497  fontdesc = pango_font_description_from_string ("Luxi Mono 12");
498  pango_layout_set_font_description (layout, fontdesc);
499  pango_layout_get_pixel_size (layout, &amp;width, &amp;height);
500  pango_font_description_free (fontdesc);
501  g_object_unref (layout);
502 </programlisting>
503 </informalexample>
504 </para>
505
506 <para>
507 See also the
508 <ulink url="http://developer.gnome.org/pango/stable/pango-Layout-Objects.html">Layout Objects</ulink>
509 section of <ulink url="http://developer.gnome.org/pango/stable/">Pango manual</ulink>.
510 </para>
511 </answer>
512 </qandaentry>
513
514 <qandaentry>
515 <question>
516 <para>
517 Why are types not registered if I use their <literal>GTK_TYPE_BLAH</literal>
518 macro ?
519 </para>
520 </question>
521
522 <answer>
523 <para>
524 The <literal>GTK_TYPE_BLAH</literal> macros are defined as calls to
525 <literal>gtk_blah_get_type()</literal>, and the <literal>_get_type()</literal> i
526 functions are declared as %G_GNUC_CONST which allows the compiler to optimize
527 the call away if it appears that the value is not being used.
528 </para>
529
530 <para>
531 A common workaround for this problem is to store the result in a volatile
532 variable, which keeps the compiler from optimizing the call away.
533 <informalexample><programlisting>
534 volatile GType dummy = GTK_TYPE_BLAH;
535 </programlisting></informalexample>
536 </para>
537 </answer>
538 </qandaentry>
539
540 <qandaentry>
541 <question>
542 <para>
543 How do I create a transparent toplevel window ?
544 </para>
545 </question>
546
547 <answer>
548 <para>
549 To make a window transparent, it needs to use a visual which supports that.
550 This is done by getting the RGBA visual of the screen with
551 gdk_screen_get_rgba_visual() and setting it on the window. Note that
552 gdk_screen_get_rgba_visual() will return %NULL if transparent windows
553 are not supported on the screen, you should fall back to
554 gdk_screen_get_system_visual() in that case. Additionally, note that this
555 will change from screen to screen, so it needs to be repeated whenever the
556 window is moved to a different screen.
557 <informalexample><programlisting>
558 GdkVisual *visual;
559
560 visual = gdk_screen_get_rgba_visual (screen);
561 if (visual == NULL)
562   visual = gdk_screen_get_system_visual (screen);
563
564 gtk_widget_set_visual (GTK_WIDGET (window), visual);
565 </programlisting></informalexample>
566 To fill the alpha channel on the window simply use cairos
567 RGBA drawing capabilities.
568 </para>
569 <para>
570 Note that the presence of an RGBA visual is no guarantee that the
571 window will actually appear transparent on screen. On X11, this
572 requires a compositing manager to be running. See
573 gtk_widget_is_composited() for a way to find out if the alpha
574 channel will be respected.
575 </para>
576 </answer>
577 </qandaentry>
578
579 </qandadiv>
580
581 <qandadiv><title>Which widget should I use...</title>
582
583 <qandaentry>
584 <question><para>
585 ...for lists and trees?
586 </para></question>
587
588 <answer>
589 <para>
590 See <link linkend="TreeWidget">tree widget overview</link> &mdash; you
591 should use the #GtkTreeView widget. (A list is just a tree with no branches,
592 so the tree widget is used for lists as well).
593 </para>
594 </answer>
595 </qandaentry>
596
597 <qandaentry>
598 <question><para>
599 ...for multi-line text display or editing?
600 </para></question>
601
602 <answer>
603 <para>
604 See <link linkend="TextWidget">text widget overview</link> &mdash; you
605 should use the #GtkTextView widget.
606 </para>
607 <para>
608 If you only have a small amount of text, #GtkLabel may also be appropriate
609 of course. It can be made selectable with gtk_label_set_selectable(). For a
610 single-line text entry, see #GtkEntry.
611 </para>
612 </answer>
613 </qandaentry>
614
615
616 <qandaentry>
617 <question><para>
618 ...to display an image or animation?
619 </para></question>
620
621 <answer>
622 <para>
623 #GtkImage can display images in just about any format GTK+ understands.
624 You can also use #GtkDrawingArea if you need to do something more complex,
625 such as draw text or graphics over the top of the image.
626 </para>
627 </answer>
628 </qandaentry>
629
630 <qandaentry>
631 <question><para>
632 ...for presenting a set of mutually-exclusive choices, where Windows
633 would use a combo box?
634 </para></question>
635
636 <answer>
637 <para>
638 With GTK+, a #GtkComboBox is the recommended widget to use for this use case.
639 This widget looks like either a combo box or the current option menu, depending
640 on the current theme. If you need an editable text entry, use the
641 #GtkComboBox:has-entry property.
642 </para>
643 </answer>
644 </qandaentry>
645
646 </qandadiv>
647
648 <qandadiv><title>GtkWidget</title>
649
650 <qandaentry>
651 <question><para>
652 How do I change the color of a widget?
653 </para></question>
654
655 <answer><para>
656 See gtk_widget_override_color() and gtk_widget_override_background_color().
657 You can also change the appearance of a widget by installing a
658 custom style provider, see gtk_style_context_add_provider().
659 </para>
660
661 <para>To change the background color for widgets such as #GtkLabel that
662 have no background, place them in a #GtkEventBox and set the background
663 of the event box.
664 </para></answer>
665 </qandaentry>
666
667 <qandaentry>
668 <question><para>
669 How do I change the font of a widget?
670 </para></question>
671
672 <answer><para>
673 This has several possible answers, depending on what exactly you want to
674 achieve. One option is gtk_widget_override_font().
675 <informalexample><programlisting>
676  PangoFontDesc *font_desc = pango_font_description_new (<!-- -->);
677  pango_font_description_set_size (font_desc, 40);
678  gtk_widget_override_font (widget, font);
679  pango_font_description_free (font_desc);
680 </programlisting></informalexample>
681 </para>
682 <para>
683 If you want to make the text of a label larger, you can use
684 gtk_label_set_markup():
685 <informalexample><programlisting>
686 gtk_label_set_markup (label, "&lt;big&gt;big text&lt;/big&gt;");
687 </programlisting></informalexample>
688 This is preferred for many apps because it's a relative size to the
689 user's chosen font size. See g_markup_escape_text() if you are
690 constructing such strings on the fly.
691 </para>
692 <para>
693 You can also change the font of a widget by putting
694 <programlisting>
695  .my-widget-class {
696    font: Sans 30;
697  }
698 </programlisting>
699 in a CSS file, loading it with gtk_css_provider_load_from_file(), and
700 adding the provider with gtk_style_context_add_provider_for_screen().
701 To associate this style information with your widget, set a style class
702 on its #GtkStyleContext using gtk_style_context_add_class().
703 The advantage of this approach is that users can then override the font
704 you have chosen. See the #GtkStyleContext documentation for more discussion.
705 </para>
706 </answer>
707 </qandaentry>
708
709 <qandaentry>
710 <question><para>
711 How do I disable/ghost/desensitize a widget?
712 </para></question>
713
714 <answer><para>
715 In GTK+ a disabled widget is termed "insensitive."
716 See gtk_widget_set_sensitive().
717 </para></answer>
718 </qandaentry>
719
720 </qandadiv>
721
722
723 <qandadiv><title>GtkTextView</title>
724
725 <qandaentry>
726 <question><para>
727 How do I get the contents of the entire text widget as a string?
728 </para></question>
729
730 <answer><para>
731 See gtk_text_buffer_get_bounds() and gtk_text_buffer_get_text()
732 or gtk_text_iter_get_text().
733 </para>
734 <para>
735 <informalexample><programlisting>
736   GtkTextIter start, end;
737   GtkTextBuffer *buffer;
738   char *text;
739
740   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
741   gtk_text_buffer_get_bounds (buffer, &amp;start, &amp;end);
742   text = gtk_text_iter_get_text (&amp;start, &amp;end);
743   /* use text */
744   g_free (text);
745 </programlisting></informalexample>
746 </para></answer>
747 </qandaentry>
748
749 <qandaentry>
750 <question><para>
751 How do I make a text widget display its complete contents in a specific font?
752 </para></question>
753
754 <answer><para>
755 If you use gtk_text_buffer_insert_with_tags() with appropriate tags to
756 select the font, the inserted text will have the desired appearance, but
757 text typed in by the user before or after the tagged block will appear in
758 the default style.
759 </para>
760 <para>
761 To ensure that all text has the desired appearance, use
762 gtk_widget_override_font() to change the default font for the widget.
763 </para></answer>
764 </qandaentry>
765
766 <qandaentry>
767 <question>
768 <para>
769 How do I make a text view scroll to the end of the buffer automatically ?
770 </para>
771 </question>
772
773 <answer>
774 <para>
775 A good way to keep a text buffer scrolled to the end is to place a
776 <link linkend="GtkTextMark">mark</link> at the end of the buffer, and
777 give it right gravity. The gravity has the effect that text inserted
778 at the mark gets inserted <emphasis>before</emphasis>, keeping the mark
779 at the end.
780 </para>
781
782 <para>
783 To ensure that the end of the buffer remains visible, use
784 gtk_text_view_scroll_to_mark() to scroll to the mark after
785 inserting new text.
786 </para>
787
788 <para>
789 The gtk-demo application contains an example of this technique.
790 </para>
791 </answer>
792 </qandaentry>
793 </qandadiv>
794
795
796
797 <qandadiv><title>#GtkTreeView</title>
798
799 <qandaentry>
800 <question><para>
801 How do I associate some data with a row in the tree?
802 </para></question>
803
804 <answer>
805 <para>
806 Remember that the #GtkTreeModel columns don't necessarily have to be
807 displayed. So you can put non-user-visible data in your model just
808 like any other data, and retrieve it with gtk_tree_model_get().
809 See the <link linkend="TreeWidget">tree widget overview</link>.
810 </para>
811 </answer>
812 </qandaentry>
813
814 <qandaentry>
815 <question><para>
816 How do I put an image and some text in the same column?
817 </para></question>
818
819 <answer>
820 <para>
821 You can pack more than one #GtkCellRenderer into a single #GtkTreeViewColumn
822 using gtk_tree_view_column_pack_start() or gtk_tree_view_column_pack_end().
823 So pack both a #GtkCellRendererPixbuf and a #GtkCellRendererText into the
824 column.
825 </para>
826 </answer>
827 </qandaentry>
828
829 <qandaentry>
830 <question><para>
831 I can set data easily on my #GtkTreeStore/#GtkListStore models using
832 gtk_list_store_set() and gtk_tree_store_set(), but can't read it back?
833 </para></question>
834
835 <answer>
836 <para>
837 Both the #GtkTreeStore and the #GtkListStore implement the #GtkTreeModel
838 interface. Consequentially, you can use any function this interface
839 implements. The easiest way to read a set of data back is to use
840 gtk_tree_model_get().
841 </para>
842 </answer>
843 </qandaentry>
844
845 <qandaentry>
846 <question><para>
847 How do I change the way that numbers are formatted by #GtkTreeView?
848 </para></question>
849 <answer><para>
850 Use gtk_tree_view_insert_column_with_data_func()
851 or gtk_tree_view_column_set_cell_data_func() and do the conversion
852 from number to string yourself (with, say, g_strdup_printf()).
853 </para>
854
855 <para>
856 The following example demonstrates this:
857 <informalexample><programlisting>
858 enum
859 {
860   DOUBLE_COLUMN,
861   N_COLUMNS
862 };
863
864 GtkListStore *mycolumns;
865 GtkTreeView *treeview;
866
867 void
868 my_cell_double_to_text (GtkTreeViewColumn *tree_column,
869                         GtkCellRenderer   *cell,
870                         GtkTreeModel      *tree_model,
871                         GtkTreeIter       *iter,
872                         gpointer           data)
873 {
874   GtkCellRendererText *cell_text = (GtkCellRendererText *)cell;
875   gdouble d;
876   gchar *text;
877
878   /* Get the double value from the model. */
879   gtk_tree_model_get (tree_model, iter, (gint)data, &amp;d, -1);
880   /* Now we can format the value ourselves. */
881   text = g_strdup_printf ("&percnt;.2f", d);
882   g_object_set (cell, "text", text, NULL);
883   g_free (text);
884 }
885
886 void
887 set_up_new_columns (GtkTreeView *myview)
888 {
889   GtkCellRendererText *renderer;
890   GtkTreeViewColumn *column;
891   GtkListStore *mycolumns;
892
893   /* Create the data model and associate it with the given TreeView */
894   mycolumns = gtk_list_store_new (N_COLUMNS, G_TYPE_DOUBLE);
895   gtk_tree_view_set_model (myview, GTK_TREE_MODEL (mycolumns));
896
897   /* Create a GtkCellRendererText */
898   renderer = gtk_cell_renderer_text_new (<!-- -->);
899
900   /* Create a new column that has a title ("Example column"),
901    * uses the above created renderer that will render the double
902    * value into text from the associated model's rows.
903    */
904   column = gtk_tree_view_column_new (<!-- -->);
905   gtk_tree_view_column_set_title  (column, "Example column");
906   renderer = gtk_cell_renderer_text_new (<!-- -->);
907   gtk_tree_view_column_pack_start (column, renderer, TRUE);
908
909   /* Append the new column after the GtkTreeView's previous columns. */
910   gtk_tree_view_append_column (GTK_TREE_VIEW (myview), column);
911   /* Since we created the column by hand, we can set it up for our
912    * needs, e.g. set its minimum and maximum width, etc.
913    */
914   /* Set up a custom function that will be called when the column content
915    * is rendered. We use the func_data pointer as an index into our
916    * model. This is convenient when using multi column lists.
917    */
918   gtk_tree_view_column_set_cell_data_func (column, renderer,
919                                            my_cell_double_to_text,
920                                            (gpointer)DOUBLE_COLUMN, NULL);
921 }
922 </programlisting></informalexample>
923 </para></answer>
924 </qandaentry>
925
926 <qandaentry>
927 <question><para>
928 How do I hide the expander arrows in my tree view ?
929 </para></question>
930
931 <answer><para>
932 Set the expander-column property of the tree view to a hidden column.
933 See gtk_tree_view_set_expander_column() and gtk_tree_view_column_set_visible().
934 </para></answer>
935 </qandaentry>
936
937 </qandadiv>
938
939 <qandadiv><title>Using cairo with GTK+</title>
940
941 <qandaentry>
942 <question><para>
943 How do I use cairo to draw in GTK+ applications ?
944 </para></question>
945
946 <answer><para>
947 The #GtkWidget::draw signal gets a ready-to-use cairo context
948 as parameter that you should use.
949 </para>
950 <para>
951 All drawing in GTK+ is normally done in a draw handler, and GTK+
952 creates a temporary pixmap for double-buffering the drawing.
953 It is possible to turn off double-buffering, with
954 gtk_widget_set_double_buffered(), but this is not ideal,
955 since it can cause some flickering.
956 </para>
957 </answer>
958 </qandaentry>
959
960 <qandaentry>
961 <question><para>
962 Can I improve the performance of my application by using the
963 Glitz or GL backend of cairo ?
964 </para></question>
965
966 <answer><para>
967 No. The GDK X11 backend uses the cairo X backend (and the other
968 GDK backends use their respective native cairo backends). The
969 GTK+ developers believe that the best way to improving the GDK
970 drawing performance is to optimize the cairo X backend and the
971 relevant code paths in the X server that is uses (mostly the
972 Render extension).
973 </para></answer>
974 </qandaentry>
975
976 <qandaentry>
977 <question><para>
978 Can I use cairo to draw on a #GdkPixbuf ?
979 </para></question>
980
981 <answer><para>
982 No, at least not yet. The cairo image surface does not support the
983 pixel format used by GdkPixbuf.
984 </para></answer>
985 </qandaentry>
986
987 </qandadiv>
988
989 </qandaset>
990
991 </refsect1>
992
993 </refentry>