]> Pileus Git - ~andy/gtk/blob - docs/reference/gtk/question_index.sgml
6dc03c6d3dab6ed03fc38a0885f50e1d5d1f26ae
[~andy/gtk] / docs / reference / gtk / question_index.sgml
1 <refentry id="gtk-question-index" revision="1 Jan 2002">
2 <refmeta>
3 <refentrytitle>Common Questions</refentrytitle>
4 <manvolnum>3</manvolnum>
5 <refmiscinfo>Common Questions</refmiscinfo>
6 </refmeta>
7
8 <refnamediv>
9 <refname>Common Questions</refname>
10 <refpurpose>
11 Find answers to common questions in the GTK+ manual
12 </refpurpose>
13 </refnamediv>
14
15 <refsect1>
16 <title>Questions and Answers</title>
17
18 <para>
19 This is an "index" of the reference manual organized by common "How do
20 I..." questions. If you aren't sure which documentation to read for
21 the question you have, this list is a good place to start.
22 </para>
23
24 <qandaset>
25
26 <qandadiv><title>General</title>
27
28 <qandaentry>
29 <question><para>
30 Where can I get help with GTK+, submit a bug report, or make a feature 
31 request?
32 </para></question>
33
34 <answer>
35
36 <para>
37 See the <link linkend="gtk-resources">documentation on this topic</link>.
38 </para>
39
40 </answer>
41
42 </qandaentry>
43
44
45 <qandaentry>
46 <question><para>How do I port from one GTK+
47 version to another?</para></question>
48
49 <answer>
50
51 <para>
52 See the <link linkend="gtk-changes-2-0">list of incompatible changes
53 from 1.2 to 2.0</link>. Also, the <ulink
54 url="http://developer.gnome.org/dotplan/porting/">GNOME 2.0 porting
55 guide</ulink> on <ulink
56 url="http://developer.gnome.org">http://developer.gnome.org</ulink>
57 has some more detailed discussion of porting from 1.2 to 2.0.
58 You may also find useful information in the documentation for 
59 specific widgets and functions.
60 </para>
61
62 <para>
63 If you have a question not covered in the manual, feel free to
64 ask on the mailing lists and please <ulink
65 url="http://bugzilla.gnome.org">file a bug report</ulink> against the
66 documentation.
67 </para>
68
69 </answer>
70
71 </qandaentry>
72
73
74 <qandaentry>
75 <question><para>
76 How does memory management work in GTK+? Should I free data returned
77 from functions?
78 </para></question>
79
80 <answer>
81
82 <para>
83 See the documentation for <link linkend="GObject">GObject</link> and
84 <link linkend="GtkObject">GtkObject</link>. For <link
85 linkend="GObject">GObject</link> note specifically <link
86 linkend="g-object-ref">g_object_ref()</link> and <link
87 linkend="g-object-unref">g_object_unref()</link>. <link
88 linkend="GtkObject">GtkObject</link> is a subclass of <link
89 linkend="GObject">GObject</link> so the same points apply, except that
90 it has a "floating" state (explained in its documentation).
91 </para>
92
93 <para>
94 For strings returned from functions, they will be declared "const"
95 (using <link linkend="G-CONST-RETURN-CAPS">G_CONST_RETURN</link>) if they
96 should not be freed. Non-const strings should be freed with <link
97 linkend="g-free">g_free()</link>. Arrays follow the same rule.  (If
98 you find an exception to the rules, please report a bug to <ulink
99 url="http://bugzilla.gnome.org">http://bugzilla.gnome.org</ulink>.)
100 </para>
101
102 </answer>
103
104 </qandaentry>
105
106
107 <qandaentry>
108 <question><para>
109 How do I use GTK+ with threads?
110 </para></question>
111
112 <answer>
113
114 <para>
115 This is covered in the 
116 <link linkend="gdk-Threads">GDK threads documentation</link>. 
117 See also the <link linkend="glib-Threads">GThread</link> documentation for portable
118 threading primitives.
119 </para>
120
121 </answer>
122
123 </qandaentry>
124
125 <qandaentry>
126 <question><para>
127 How do I internationalize a GTK+ program?
128 </para></question>
129
130 <answer>
131 <para>
132 Most people use <ulink url="http://www.gnu.org/software/gettext/">GNU
133 gettext</ulink>, already required in order to install GLib. On a UNIX
134 or Linux system with gettext installed, type <literal>info
135 gettext</literal> to read the documentation.
136 </para>
137 <para>
138 The short checklist on how to use gettext is: call
139 <function>bindtextdomain()</function> so gettext can find the files
140 containing your translations, call <function>textdomain()</function>
141 to set the default translation domain, then call
142 <function>gettext()</function> to look up each string to be translated
143 in the default domain. Conventionally, people define macros as
144 follows for convenience:
145 <informalexample>
146 <programlisting>
147   #define  _(x)  gettext (x)
148   #define N_(x)  x
149 </programlisting>
150 </informalexample>
151 You use <function>N_()</function> (N stands for no-op) to mark 
152 a string for translation in a context where a function call 
153 to <function>gettext()</function> is not allowed, such as in 
154 an array initializer. You eventually have to call
155 <function>gettext()</function> on the string to actually fetch the
156 translation.  <function>_()</function> both marks the string for 
157 translation and actually translates it.
158 </para>
159 <para>
160 Code using these macros ends up looking like this:
161 <informalexample>
162 <programlisting>
163  #include &lt;libintl.h&gt;
164
165  #define  _(x)  gettext (x)
166  #define N_(x)  x
167
168  static const char *global_variable = N_("Translate this string");
169
170  static void
171  make_widgets (void)
172  {
173     GtkWidget *label1;
174     GtkWidget *label2;
175
176     label1 = gtk_label_new (_("Another string to translate"));
177     label2 = gtk_label_new (_(global_variable));
178 ...
179 </programlisting>
180 </informalexample>
181 </para>
182 <para>
183 Libraries using gettext should use <function>dgettext()</function>
184 instead of <function>gettext()</function>, which allows
185 them to specify the translation domain each time they 
186 ask for a translation. Libraries should also avoid calling 
187 <function>textdomain()</function>, since they'll be specifying 
188 the domain instead of using the default.
189 For <function>dgettext()</function> the <function>_()</function> macro
190 can be defined as:
191 <informalexample>
192 <programlisting>
193   #define _(x) dgettext ("MyDomain", x)
194 </programlisting>
195 </informalexample>
196 </para>
197 </answer>
198 </qandaentry>
199
200 <qandaentry>
201 <question>
202 <para>
203 How do I use non-ASCII characters in GTK+ programs ?
204 </para>
205 </question>
206
207 <answer>
208 <para>
209 GTK+ uses <ulink url="http://www.unicode.org">Unicode</ulink> (more exactly 
210 UTF-8) for all text. UTF-8 encodes each Unicode codepoint as a
211               sequence of one to six bytes and has a number of nice
212               properties which make it a good choice for working with Unicode
213               text in C programs:
214 <itemizedlist>
215 <listitem><para>
216 ASCII characters are encoded by their familiar ASCII codepoints.
217 </para></listitem>
218 <listitem><para>
219 ASCII characters never appear as part of any other character.
220 </para></listitem>
221 <listitem><para>
222 The zero byte doesn't occur as part of a character, so that UTF-8 strings can
223                     be manipulated with the usual C library functions for
224                     handling zero-terminated strings.
225 </para></listitem>
226 </itemizedlist>
227 More information about Unicode and UTF-8 can be found in the 
228 <ulink url="http://www.cl.cam.ac.uk/~mgk25/unicode.html">UTF-8 and Unicode FAQ for Unix/Linux</ulink>.
229 GLib provides functions for converting strings between UTF-8 and other
230               encodings, see 
231 <link linkend="g-locale-to-utf8">g_locale_to_utf8()</link> and <link
232                 linkend="g-convert">g_convert()</link>.
233 </para>
234 <para>
235 Text coming from external sources (e.g. files or user input), has to be
236               converted to UTF-8 before being handed over to GTK+. The
237               following example writes the content of a IS0-8859-1 encoded text
238               file to <literal>stdout</literal>:
239 <informalexample><programlisting>
240 gchar *text, *utf8_text;
241 gsize length;
242 GError *error = NULL;
243
244 if (g_file_get_contents (filename, &amp;text, &amp;length, NULL)) 
245   {
246      utf8_text = g_convert (text, length, "UTF-8", "ISO-8859-1", 
247                             NULL, NULL, &amp;error);
248      if (error != NULL)
249        {
250          fprintf ("Couldn't convert file &percnt;s to UTF-8\n", filename);
251          g_error_free (error);
252        }
253      else
254        g_print (utf8_text);
255   }
256 else 
257   fprintf (stderr, "Unable to read file &percnt;s\n", filename);
258 </programlisting></informalexample>
259 </para>
260 <para>
261 For string literals in the source code, there are several alternatives for
262               handling non-ASCII content:
263 <variablelist>
264 <varlistentry><term>direct UTF-8</term>
265 <listitem><para>
266 If your editor and compiler are capable of handling UTF-8 encoded sources,
267 it is very convenient to simply use UTF-8 for string literals, since it allows
268 you to edit the strings in "wysiwyg". Note that choosing this option may 
269 reduce the portability of your code.  
270 </para></listitem>
271 </varlistentry>
272
273 <varlistentry><term>escaped UTF-8</term>
274 <listitem><para>
275 Even if your toolchain can't handle UTF-8 directly, you can still encode string
276 literals in UTF-8 by using octal or hexadecimal escapes like 
277 <literal>\212</literal> or <literal>\xa8</literal> to
278 encode each byte. This is portable, but modifying the escaped strings is not
279 very convenient. Be careful when mixing hexadecimal escapes with ordinary text;
280 <literal>"\xa8abcd"</literal> is a string of length 1 !
281 </para></listitem>
282 </varlistentry>
283
284 <varlistentry><term>runtime conversion</term>
285 <listitem><para>
286 If the string literals can be represented in an encoding which your toolchain
287 can handle (e.g. IS0-8859-1), you can write your source files in that encoding
288 and use <link linkend="g-convert">g_convert()</link> to convert the strings to 
289 UTF-8 at runtime. Note that this has some runtime overhead, so you may want to
290 move the conversion out of inner loops.
291 </para></listitem>
292 </varlistentry>
293 </variablelist>
294 Here is an example showing the three approaches using the copyright sign 
295 &copy; which has Unicode and ISO-8859-1 codepoint 169 and is represented in
296 UTF-8 by the two bytes 194, 169:
297 <informalexample><programlisting>
298 g_print ("direct UTF-8: &copy;");
299 g_print ("escaped UTF-8: \302\251");
300 text = g_convert ("runtime conversion: &copy;", -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL);
301 g_print(text);
302 g_free (text);
303 </programlisting></informalexample>
304 </para>
305 </answer>
306 </qandaentry>
307
308 <qandaentry>
309 <question><para>
310 How do I use GTK+ with C++?
311 </para></question>
312
313 <answer>
314 <para>
315 There are two ways to approach this. The GTK+ header files use the subset 
316 of C that's also valid C++, so you can simply use the normal GTK+ API 
317 in a C++ program. Alternatively, you can use a "C++ binding" 
318 such as <ulink url="http://gtkmm.sourceforge.net/">gtkmm</ulink>
319 which provides a C++-native API.
320 </para>
321 <para>
322 When using GTK+ directly, keep in mind that only functions can be
323 connected to signals, not methods. So you will need to use global
324 functions or "static" class functions for signal connections.
325 </para>
326 <para>
327 Another common issue when using GTK+ directly is that 
328 C++ will not implicitly convert an integer to an enumeration. 
329 This comes up when using bitfields; in C you can write the following
330 code:
331 <informalexample>
332 <programlisting>
333   gdk_window_set_events (gdk_window, 
334                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
335 </programlisting>
336 </informalexample>
337 while in C++ you must write:
338 <informalexample>
339 <programlisting>
340   gdk_window_set_events (gdk_window, 
341                          (GdkEventMask) GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
342 </programlisting>
343 </informalexample>
344 There are very few functions that require this cast, however.
345 </para>
346 </answer>
347
348 </qandaentry>
349
350 <qandaentry>
351 <question><para>
352 How do I use GTK+ with other non-C languages?
353 </para></question>
354
355 <answer>
356 <para>
357 See the <ulink url="http://www.gtk.org/bindings.html">list of language
358 bindings</ulink> on <ulink
359                            url="http://www.gtk.org">http://www.gtk.org</ulink>.
360 </para>
361
362 </answer>
363
364 </qandaentry>
365
366 <qandaentry>
367 <question><para>
368 How do I load an image or animation from a file?
369 </para></question>
370
371 <answer>
372
373 <para>
374 To load an image file straight into a display widget, use <link
375 linkend="gtk-image-new-from-file">gtk_image_new_from_file()</link>
376 <footnote><para> If the file load fails, <link
377 linkend="gtk-image-new-from-file">gtk_image_new_from_file()</link>
378 will display a "broken image" graphic &mdash; to detect a failed load
379 yourself, use <link
380 linkend="gdk-pixbuf-new-from-file">gdk_pixbuf_new_from_file()</link>
381 directly then <link
382 linkend="gtk-image-new-from-pixbuf">gtk_image_new_from_pixbuf()</link>.
383 </para></footnote>. To load an image for another purpose, use <link
384 linkend="gdk-pixbuf-new-from-file">gdk_pixbuf_new_from_file()</link>.
385 To load an animation, use <link
386 linkend="gdk-pixbuf-animation-new-from-file">gdk_pixbuf_animation_new_from_file()</link>.
387 <link
388 linkend="gdk-pixbuf-animation-new-from-file">gdk_pixbuf_animation_new_from_file()</link>
389 can also load non-animated images, so use it in combination with 
390 <link
391       linkend="gdk-pixbuf-animation-is-static-image">gdk_pixbuf_animation_is_static_image()</link> to load a file of unknown type. 
392 </para>
393 <para>
394 To load an image or animation file asynchronously (without blocking), use 
395 <link linkend="GdkPixbufLoader">GdkPixbufLoader</link>.
396 </para>
397 </answer>
398
399 </qandaentry>
400
401 <qandaentry>
402 <question><para>
403 How do I draw text ?
404 </para></question>
405
406 <answer>
407 <para>
408 To draw a piece of text, use a Pango layout and 
409 <link linkend="gdk-draw-layout">gdk_draw_layout()</link>, 
410 using code like the following:
411 <informalexample>
412 <programlisting>
413  layout = gtk_widget_create_pango_layout (widget, text);
414  fontdesc = pango_font_description_from_string ("Luxi Mono 12");
415  pango_layout_set_font_description (layout, fontdesc); 
416  gdk_draw_layout (..., layout);
417  pango_font_description_free (fontdesc);
418  g_object_unref (layout);
419 </programlisting>
420 </informalexample>
421 Do not use the deprecated <link linkend="GdkFont">GdkFont</link> and <link linkend="gdk-draw-text">gdk_draw_text()</link>.
422 </para>
423
424 <para>
425 See also the "Text Handling in GTK 2" section of 
426 <ulink url="http://developer.gnome.org/dotplan/porting/">Porting applications 
427 to the GNOME 2.0 platform</ulink>.
428 </para>
429 </answer>
430
431 </qandaentry>
432
433 <qandaentry>
434 <question>
435 <para>
436 How do I measure the size of a piece of text ?
437 </para>
438 </question>
439
440 <answer>
441 <para>
442 To obtain the size of a piece of text, use a Pango layout and 
443 <link
444                 linkend="pango-layout-get-pixel-size">pango_layout_get_pixel_size()</link>, 
445 using code like the following:
446 <informalexample>
447 <programlisting>
448  layout = gtk_widget_create_pango_layout (widget, text);
449  fontdesc = pango_font_description_from_string ("Luxi Mono 12");
450  pango_layout_set_font_description (layout, fontdesc); 
451  pango_layout_get_pixel_size (layout, &amp;width, &amp;height);
452  pango_font_description_free (fontdesc);
453  g_object_unref (layout);
454 </programlisting>
455 </informalexample>
456 Do not use the deprecated function <link linkend="gdk-text-width">gdk_text_width()</link>.
457 </para>
458
459 <para>
460 See also the "Text Handling in GTK 2" section of 
461 <ulink url="http://developer.gnome.org/dotplan/porting/">Porting applications 
462 to the GNOME 2.0 platform</ulink>.
463 </para>
464 </answer>
465 </qandaentry>
466
467 <qandaentry>
468 <question>
469 <para>
470 How do I make a text view scroll to the end of the buffer automatically ?
471 </para>
472 </question>
473
474 <answer>
475 <para>
476 The "insert" <link linkend="GtkTextMark">mark</link> marks the insertion point
477 where <link linkend="gtk-text-buffer-insert">gtk_text_buffer_insert()</link>
478 inserts new text into the buffer. The text is inserted 
479 <emphasis>before</emphasis> the "insert" mark, so that it generally stays 
480 at the end of the buffer. If it gets explicitly moved to some other position, 
481 e.g. when the user selects some text, 
482 use <link linkend="gtk-text-buffer-move-mark">gtk_text_buffer_move_mark()</link>
483 to set it to the desired location before inserting more text. 
484 The "insert" mark of a buffer can be obtained with <link
485 linkend="gtk-text-buffer-get-insert">gtk_text_buffer_get_insert()</link>.
486 </para>
487
488 <para> 
489 To ensure that the end of the buffer remains visible, use
490 <link
491                 linkend="gtk-text-view-scroll-to-mark">gtk_text_view_scroll_to_mark()</link> to scroll to the "insert" mark after inserting new text.
492 </para>
493 </answer>
494 </qandaentry>
495 </qandadiv>
496
497 <qandadiv><title>Which widget should I use...</title>
498
499 <qandaentry>
500 <question><para>
501 ...for lists and trees?
502 </para></question>
503
504 <answer>
505 <para>
506 See <link linkend="TreeWidget">tree widget overview</link> &mdash; you
507 should use the <link linkend="GtkTreeView">GtkTreeView</link> widget.
508 (A list is just a tree with no branches, so the tree widget is used
509 for lists as well.) Do not use the deprecated widgets <link
510 linkend="GtkTree">GtkTree</link> or <link
511 linkend="GtkCList">GtkCList</link>/<link
512 linkend="GtkCTree">GtkCTree</link> in newly-written code, they are
513 less flexible and result in an inferior user interface.
514 </para>
515 </answer>
516 </qandaentry>
517
518 <qandaentry>
519 <question><para>
520 ...for multi-line text display or editing?
521 </para></question>
522
523 <answer>
524 <para>
525 See <link linkend="TextWidget">text widget overview</link> &mdash; you
526 should use the <link linkend="GtkTextView">GtkTextView</link> widget.
527 Do not use the deprecated widget <link
528 linkend="GtkText">GtkText</link> in newly-written code, it has a
529 number of problems that are best avoided.
530 </para>
531 <para>
532 If you only have a small amount of text, <link
533 linkend="GtkLabel">GtkLabel</link> may also be appropriate of course.
534 It can be made selectable with <link linkend="gtk-label-set-selectable">
535 gtk_label_set_selectable()</link>. For a single-line text entry, 
536 see <link linkend="GtkEntry">GtkEntry</link>.
537 </para>
538 </answer>
539 </qandaentry>
540
541
542 <qandaentry>
543 <question><para>
544 ...to display an image or animation?
545 </para></question>
546
547 <answer>
548 <para>
549 <link linkend="GtkImage">GtkImage</link> can display images
550 in just about any format GTK+ understands. You can also 
551 use <link linkend="GtkDrawingArea">GtkDrawingArea</link> if you need 
552 to do something more complex, such as draw text or graphics over the
553 top of the image.
554 </para>
555 </answer>
556 </qandaentry>
557
558 <qandaentry>
559 <question><para>
560 ...for presenting a set of mutually-exclusive choices, where Windows
561 would use a combo box?
562 </para></question>
563
564 <answer>
565 <para>
566 With GTK+, a <link linkend="GtkOptionMenu">GtkOptionMenu</link> is
567 recommended instead of a combo box, if the user is selecting from a
568 fixed set of options. That is, non-editable combo boxes are not
569 encouraged. <link linkend="GtkOptionMenu">GtkOptionMenu</link> is
570 much easier to use than <link linkend="GtkCombo">GtkCombo</link>
571 as well. Use <link linkend="GtkCombo">GtkCombo</link> only when you 
572 need the editable text entry.
573 </para>
574 <para>
575 (As a future enhancement to GTK+, a new widget to replace <link
576 linkend="GtkOptionMenu">GtkOptionMenu</link> and <link
577 linkend="GtkCombo">GtkCombo</link> is planned.  This widget will be
578 themeable to look like either a combo box or the current option menu,
579 and will address some shortcomings in the <link
580 linkend="GtkCombo">GtkCombo</link> API.  <ulink
581 url="http://bugzilla.gnome.org/show_bug.cgi?id=50554">Bug
582 50554</ulink> tracks this issue, if you want to check status or post
583 comments.)
584 </para>
585 </answer>
586 </qandaentry>
587
588 </qandadiv>
589
590 <qandadiv><title><link linkend="GtkWidget">GtkWidget</link></title>
591
592 <qandaentry>
593 <question><para>
594 How do I change the color of a widget?
595 </para></question>
596
597 <answer><para>
598 See <link linkend="gtk-widget-modify-fg">gtk_widget_modify_fg()</link>,
599 <link linkend="gtk-widget-modify-bg">gtk_widget_modify_bg()</link>,
600 <link linkend="gtk-widget-modify-base">gtk_widget_modify_base()</link>,
601 and <link
602 linkend="gtk-widget-modify-text">gtk_widget_modify_text()</link>.  See
603 <link linkend="gtk-Resource-Files">GTK+ resource files</link> for more
604 discussion. You can also change widget color by installing a resource
605 file and parsing it with <link
606 linkend="gtk-rc-add-default-file">gtk_rc_add_default_file()</link>.
607 The advantage of a resource file is that users can then override the
608 color you've chosen.
609 </para>
610
611 <para>To change the background color for widgets such as <link
612 linkend="GtkLabel">GtkLabel</link> that have no background, place them
613 in a <link linkend="GtkEventBox">GtkEventBox</link> and set the
614 background of the event box.  
615 </para></answer>
616 </qandaentry>
617
618 <qandaentry>
619 <question><para>
620 How do I disable/ghost/desensitize a widget?
621 </para></question>
622
623 <answer><para> In GTK+ a disabled widget is termed "insensitive." See
624 <link
625 linkend="gtk-widget-set-sensitive">gtk_widget_set_sensitive()</link>.
626 </para></answer>
627 </qandaentry>
628
629 </qandadiv>
630
631
632 <qandadiv><title><link linkend="GtkTextView">GtkTextView</link></title>
633
634 <qandaentry>
635 <question><para>
636 How do I get the contents of the entire text widget as a string?
637 </para></question>
638
639 <answer><para>
640
641 See <link
642           linkend="gtk-text-buffer-get-bounds">gtk_text_buffer_get_bounds()</link>
643           and <link
644           linkend="gtk-text-buffer-get-text">gtk_text_buffer_get_text()</link>
645 or <link
646           linkend="gtk-text-iter-get-text">gtk_text_iter_get_text()</link>.
647 </para>
648 <para>
649 <informalexample><programlisting>
650   GtkTextIter start, end;
651   GtkTextBuffer *buffer;
652   char *text;
653
654   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
655   gtk_text_buffer_get_bounds (buffer, &amp;start, &amp;end);
656   text = gtk_text_iter_get_text (&amp;start, &amp;end);
657   /* use text */
658   g_free (text);
659 </programlisting></informalexample>
660 </para></answer>
661 </qandaentry>
662
663 <qandaentry>
664 <question><para>
665 How do I make a text widget display its complete contents in a specific font?
666 </para></question>
667
668 <answer><para>
669 If you use <link
670               linkend="gtk-text-buffer-insert-with-tags">gtk_text_buffer_insert_with_tags()</link> with appropriate tags to select the font, the inserted text will have the desired appearance, but text typed in by the user before or after the tagged block will appear in the default style. 
671 </para>
672 <para>
673 To ensure that all text has the desired appearance, use <link
674                 linkend="gtk-widget-modify-font">gtk_widget_modify_font()</link> to change the default font for the widget.
675 </para></answer>
676 </qandaentry>
677 </qandadiv>
678
679
680
681 <qandadiv><title><link linkend="GtkTreeView">GtkTreeView</link></title>
682
683 <qandaentry>
684 <question><para>
685 How do I associate some data with a row in the tree?
686 </para></question>
687
688 <answer>
689 <para>
690 Remember that the <link linkend="GtkTreeModel">GtkTreeModel</link>
691 columns don't necessarily have to be displayed. So you can put
692 non-user-visible data in your model just like any other data, and
693 retrieve it with <link
694 linkend="gtk-tree-model-get">gtk_tree_model_get()</link>.
695 See the <link linkend="TreeWidget">tree widget overview</link>.
696 </para>
697 </answer>
698 </qandaentry>
699
700 <qandaentry>
701 <question><para>
702 What's the <link linkend="GtkTreeView">GtkTreeView</link> equivalent of 
703 <link linkend="gtk-clist-find-row-from-data">gtk_clist_find_row_from_data()</link>?
704 </para></question>
705
706 <answer>
707 <para>
708 As there is no separate data column in the <link linkend="GtkTreeModel">GtkTreeModel</link>, there's no
709 built in function to find the iter from data.  You can write a custom
710 searching function to walk the tree and find the data, or use
711 <link linkend="gtk-tree-model-foreach">gtk_tree_model_foreach()</link>.
712 </para>
713 </answer>
714 </qandaentry>
715
716 <qandaentry>
717 <question><para>
718 How do I put an image and some text in the same column?
719 </para></question>
720
721 <answer>
722 <para>
723 You can pack more than one <link
724 linkend="GtkCellRenderer">GtkCellRenderer</link> into a single
725 <link linkend="GtkTreeViewColumn">GtkTreeViewColumn</link> using
726 <link
727       linkend="gtk-tree-view-column-pack-start">gtk_tree_view_column_pack_start()</link> or <link linkend="gtk-tree-view-column-pack-end">gtk_tree_view_column_pack_end()</link>. So pack both a <link
728 linkend="GtkCellRendererPixbuf">GtkCellRendererPixbuf</link>
729 and a <link
730 linkend="GtkCellRendererText">GtkCellRendererText</link> into the
731 column.
732 </para>
733 </answer>
734 </qandaentry>
735
736 <qandaentry>
737 <question><para>
738 I can set data easily on my <link
739                 linkend="GtkTreeStore">GtkTreeStore</link>/<link
740                 linkend="GtkListStore">GtkListStore</link> models using  <link
741 linkend="gtk-tree-model-get">gtk_list_store_set()</link> and <link
742 linkend="gtk-tree-model-get">gtk_tree_store_set()</link>, but can't read
743               it back?
744 </para></question>
745
746 <answer>
747 <para>
748 Both the <link
749                 linkend="GtkTreeStore">GtkTreeStore</link> and the <link
750                 linkend="GtkListStore">GtkListStore</link> implement the
751               <link linkend="GtkTreeModel">GtkTreeModel</link>
752                 interface.  Consequentially, the can use any function
753                 this interface implements.  The easiest way to read a
754                 set of data back is to use
755  <link
756 linkend="gtk-tree-model-get">gtk_tree_model_get()</link>.
757 </para>
758 </answer>
759 </qandaentry>
760
761 <qandaentry>
762 <question><para>
763 How do I change the way that numbers are formatted by <link linkend="GtkTreeView">GtkTreeView</link>?
764 </para></question>
765 <answer><para>
766 Use <link linkend="gtk-tree-view-insert-column-with-data-func">gtk_tree_view_insert_column_with_data_func()</link> 
767 or <link linkend="gtk-tree-view-column-set-cell-data-func">gtk_tree_view_column_set_cell_data_func()</link>
768 and do the conversion from number to string yourself (with, say, 
769 <link linkend="g-strdup-printf">g_strdup_printf()</link>).
770 </para>
771
772 <para>
773 The following example demonstrates this:
774 <informalexample><programlisting>
775 enum 
776 {
777   DOUBLE_COLUMN,
778   N_COLUMNS
779 };
780
781 GtkListStore *mycolumns;
782 GtkTreeView *treeview;
783
784 void 
785 my_cell_double_to_text (GtkTreeViewColumn *tree_column,
786                         GtkCellRenderer   *cell, 
787                         GtkTreeModel      *tree_model,
788                         GtkTreeIter       *iter, 
789                         gpointer           data)
790 {
791   GtkCellRendererText *cell_text = (GtkCellRendererText *)cell;
792   gdouble d;
793   gchar *text;
794
795   /* Get the double value from the model. */
796   gtk_tree_model_get (tree_model, iter, (gint)data, &amp;d, -1);
797   /* Now we can format the value ourselves. */
798   text = g_strdup_printf ("&percnt;.2f", d);
799   g_object_set (cell, "text", text, NULL);
800   g_free (text);
801 }
802
803 void 
804 set_up_new_columns (GtkTreeView *myview)
805 {
806   GtkCellRendererText *renderer;
807   GtkTreeViewColumn *column;
808   GtkListStore *mycolumns;
809
810   /* Create the data model and associate it with the given TreeView */
811   mycolumns = gtk_list_store_new (N_COLUMNS, G_TYPE_DOUBLE);
812   gtk_tree_view_set_model (myview, GTK_TREE_MODEL (mycolumns));
813
814   /* Create a GtkCellRendererText */
815   renderer = gtk_cell_renderer_text_new ();
816
817   /* Create a new column that has a title ("Example column"),
818    * uses the above created renderer that will render the double
819    * value into text from the associated model's rows. 
820    */
821   column = gtk_tree_view_column_new ();
822   gtk_tree_view_column_set_title  (column, "Example column");
823   renderer = gtk_cell_renderer_text_new ();
824   gtk_tree_view_column_pack_start (column, renderer, TRUE);
825
826   /* Append the new column after the GtkTreeView's previous columns. */
827   gtk_tree_view_append_column (GTK_TREE_VIEW (myview), column);
828   /* Since we created the column by hand, we can set it up for our
829    * needs, e.g. set its minimum and maximum width, etc.
830    */
831   /* Set up a custom function that will be called when the column content
832    * is rendered. We use the func_data pointer as an index into our
833    * model. This is convenient when using multi column lists. 
834    */
835   gtk_tree_view_column_set_cell_data_func (column, renderer,
836                                            my_cell_double_to_text, 
837                                            (gpointer)DOUBLE_COLUMN, NULL);
838 }
839 </programlisting></informalexample>
840 </para></answer>
841 </qandaentry>
842
843 </qandadiv>
844
845
846
847 </qandaset>
848
849 </refsect1>
850
851 </refentry>