1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Common Questions: GTK+ 2 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="GTK+ 2 Reference Manual">
<link rel="up" href="gtk.html" title="Part I. GTK+ Overview">
<link rel="prev" href="gtk-resources.html" title="Mailing lists and bug reports">
<link rel="next" href="chap-drawing-model.html" title="The GTK+ Drawing Model">
<meta name="generator" content="GTK-Doc V1.33.0 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="gtk.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="gtk-resources.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="chap-drawing-model.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="gtk-question-index"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">Common Questions</span></h2>
<p>Common Questions —
Find answers to common questions in the GTK+ manual
</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="id-1.2.11.3"></a><h2>Questions and Answers</h2>
<p>
This is an "index" of the reference manual organized by common "How do
I..." questions. If you aren't sure which documentation to read for
the question you have, this list is a good place to start.
</p>
<div class="qandaset">
<a name="id-1.2.11.3.3"></a><table border="0" style="width: 100%;">
<colgroup>
<col align="left" width="1%">
<col>
</colgroup>
<tbody>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.1"></a>1. General</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.2"></a><a name="id-1.2.11.3.3.1.2.1"></a><p><b>1.1.</b></p>
</td>
<td align="left" valign="top"><p>
How do I get started with GTK+?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
The GTK+ <a class="ulink" href="http://www.gtk.org" target="_top">website</a> offers a
<a class="ulink" href="http://www.gtk.org/tutorial" target="_top">tutorial</a> and a
<a class="ulink" href="http://www.gtk.org/faq" target="_top">FAQ</a>. More documentation ranging
from whitepapers to online books can be found at the
<a class="ulink" href="http://developer.gnome.org/doc" target="_top">GNOME developer's site</a>.
After studying these materials you should be well prepared to come back to
this reference manual for details.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.3"></a><a name="id-1.2.11.3.3.1.3.1"></a><p><b>1.2.</b></p>
</td>
<td align="left" valign="top"><p>
Where can I get help with GTK+, submit a bug report, or make a feature
request?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
See the <a class="link" href="gtk-resources.html" title="Mailing lists and bug reports">documentation on this topic</a>.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.4"></a><a name="id-1.2.11.3.3.1.4.1"></a><p><b>1.3.</b></p>
</td>
<td align="left" valign="top"><p>How do I port from one GTK+
version to another?</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
See the <a class="link" href="gtk-changes-2-0.html" title="Changes from 1.2 to 2.0">list of incompatible changes
from 1.2 to 2.0</a>. Also, the <a class="ulink" href="http://developer.gnome.org/dotplan/porting/" target="_top">GNOME 2.0 porting
guide</a> on <a class="ulink" href="http://developer.gnome.org" target="_top">http://developer.gnome.org</a>
has some more detailed discussion of porting from 1.2 to 2.0.
You may also find useful information in the documentation for
specific widgets and functions.
</p>
<p>
If you have a question not covered in the manual, feel free to
ask on the mailing lists and please <a class="ulink" href="http://bugzilla.gnome.org" target="_top">file a bug report</a> against the
documentation.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.5"></a><a name="id-1.2.11.3.3.1.5.1"></a><p><b>1.4.</b></p>
</td>
<td align="left" valign="top"><p>
How does memory management work in GTK+? Should I free data returned
from functions?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
See the documentation for <span class="type">GObject</span> and <a class="link" href="GtkObject.html" title="GtkObject"><span class="type">GtkObject</span></a>. For <span class="type">GObject</span> note
specifically <code class="function">g_object_ref()</code> and <code class="function">g_object_unref()</code>. <a class="link" href="GtkObject.html" title="GtkObject"><span class="type">GtkObject</span></a> is a subclass
of <span class="type">GObject</span> so the same points apply, except that it has a "floating" state
(explained in its documentation).
</p>
<p>
For strings returned from functions, they will be declared "const"
if they should not be freed. Non-const strings should be
freed with <code class="function">g_free()</code>. Arrays follow the same rule. (If you find an exception
to the rules, please report a bug to <a class="ulink" href="http://bugzilla.gnome.org" target="_top">http://bugzilla.gnome.org</a>.)
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.6"></a><a name="id-1.2.11.3.3.1.6.1"></a><p><b>1.5.</b></p>
</td>
<td align="left" valign="top"><p>
Why does my program leak memory, if I destroy a widget immediately
after creating it ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
If <span class="structname">GtkFoo</span> isn't a toplevel window, then
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">foo</span> <span class="o">=</span> <span class="n">gtk_foo_new</span> <span class="p">();</span>
<span class="n">gtk_widget_destroy</span> <span class="p">(</span><span class="n">foo</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
is a memory leak, because no one assumed the initial floating
reference. If you are using a widget and you aren't immediately
packing it into a container, then you probably want standard
reference counting, not floating reference counting.
</p>
<p>
To to get this, you must acquire a reference to the widget and drop the
floating reference (<span class="quote">“<span class="quote">ref and sink</span>”</span> in GTK+ parlance) after
creating it:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">foo</span> <span class="o">=</span> <span class="n">gtk_foo_new</span> <span class="p">();</span>
<span class="n">g_object_ref</span> <span class="p">(</span><span class="n">foo</span><span class="p">);</span>
<span class="n">gtk_object_sink</span> <span class="p">(</span><span class="n">GTK_OBJECT</span> <span class="p">(</span><span class="n">foo</span><span class="p">));</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
When you want to get rid of the widget, you must call <a class="link" href="GtkWidget.html#gtk-widget-destroy" title="gtk_widget_destroy ()"><code class="function">gtk_widget_destroy()</code></a>
to break any external connections to the widget before dropping your
reference:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">gtk_widget_destroy</span> <span class="p">(</span><span class="n">foo</span><span class="p">);</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">foo</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
When you immediately add a widget to a container, it takes care of
assuming the initial floating reference and you don't have to worry
about reference counting at all ... just call <a class="link" href="GtkWidget.html#gtk-widget-destroy" title="gtk_widget_destroy ()"><code class="function">gtk_widget_destroy()</code></a>
to get rid of the widget.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.7"></a><a name="id-1.2.11.3.3.1.7.1"></a><p><b>1.6.</b></p>
</td>
<td align="left" valign="top"><p>
How do I use GTK+ with threads?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
This is covered in the <GTKDOCLINK HREF="gdk-Threads">GDK threads
documentation</GTKDOCLINK>. See also the GThread
documentation for portable threading primitives.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.8"></a><a name="id-1.2.11.3.3.1.8.1"></a><p><b>1.7.</b></p>
</td>
<td align="left" valign="top"><p>
How do I internationalize a GTK+ program?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
Most people use <a class="ulink" href="http://www.gnu.org/software/gettext/" target="_top">GNU
gettext</a>, already required in order to install GLib. On a UNIX
or Linux system with gettext installed, type <code class="literal">info gettext</code>
to read the documentation.
</p>
<p>
The short checklist on how to use gettext is: call <code class="function">bindtextdomain()</code> so gettext
can find the files containing your translations, call <code class="function">textdomain()</code> to set the
default translation domain, call <code class="function">bind_textdomain_codeset()</code> to request that
all translated strings are returned in UTF-8, then call <code class="function">gettext()</code> to look up
each string to be translated in the default domain.
Conventionally, people define macros as follows for convenience:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="cp">#define _(x) gettext (x)</span>
<span class="cp">#define N_(x) x</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
You use <code class="function">N_()</code> (N stands for no-op) to mark a string for translation in a
context where a function call to <code class="function">gettext()</code> is not allowed, such as in an
array initializer.
You eventually have to call <code class="function">gettext()</code> on the string to actually fetch the
translation. <code class="function">_()</code> both marks the string for translation and actually
translates it.
</p>
<p>
Nowadays, GLib provides the common shorthand macros in the header file
<code class="filename">gi18n.h</code>, so you don't have to define them yourself,
just include that header.
</p>
<p>
Code using these macros ends up looking like this:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="cp">#include</span> <span class="cpf"><gi18n.h></span><span class="cp"></span>
<span class="k">static</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">global_variable</span> <span class="o">=</span> <span class="n">N_</span><span class="p">(</span><span class="s">"Translate this string"</span><span class="p">);</span>
<span class="k">static</span> <span class="kt">void</span>
<span class="nf">make_widgets</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">GtkWidget</span> <span class="o">*</span><span class="n">label1</span><span class="p">;</span>
<span class="n">GtkWidget</span> <span class="o">*</span><span class="n">label2</span><span class="p">;</span>
<span class="n">label1</span> <span class="o">=</span> <span class="n">gtk_label_new</span> <span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">"Another string to translate"</span><span class="p">));</span>
<span class="n">label2</span> <span class="o">=</span> <span class="n">gtk_label_new</span> <span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="n">global_variable</span><span class="p">));</span>
<span class="p">...</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
Libraries using gettext should use <code class="function">dgettext()</code> instead of <code class="function">gettext()</code>, which
allows them to specify the translation domain each time they ask for a
translation. Libraries should also avoid calling <code class="function">textdomain()</code>, since they
will be specifying the domain instead of using the default. For <code class="function">dgettext()</code>
the <code class="function">_()</code> macro can be defined as:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="cp">#define _(x) dgettext ("MyDomain", x)</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
Again, GLib comes with the <code class="filename">gi18n-lib.h</code>, saving you the
trouble of defining the macros by hand. The macros in that header expect the
translation domain to be specified by the <code class="literal">GETTEXT_PACKAGE</code> macro.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.9"></a><a name="id-1.2.11.3.3.1.9.1"></a><p><b>1.8.</b></p>
</td>
<td align="left" valign="top"><p>
How do I use non-ASCII characters in GTK+ programs ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
GTK+ uses <a class="ulink" href="http://www.unicode.org" target="_top">Unicode</a> (more exactly
UTF-8) for all text. UTF-8 encodes each Unicode codepoint as a sequence of
one to six bytes and has a number of nice properties which make it a good
choice for working with Unicode text in C programs:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
ASCII characters are encoded by their familiar ASCII codepoints.
</p></li>
<li class="listitem"><p>
ASCII characters never appear as part of any other character.
</p></li>
<li class="listitem"><p>
The zero byte doesn't occur as part of a character, so that UTF-8 strings
can be manipulated with the usual C library functions for handling
zero-terminated strings.
</p></li>
</ul></div>
<p>
More information about Unicode and UTF-8 can be found in the
<a class="ulink" href="http://www.cl.cam.ac.uk/~mgk25/unicode.html" target="_top">UTF-8 and Unicode i
FAQ for Unix/Linux</a>.
GLib provides functions for converting strings between UTF-8 and other
encodings, see <code class="function">g_locale_to_utf8()</code> and <code class="function">g_convert()</code>.
</p>
<p>
Text coming from external sources (e.g. files or user input), has to be
converted to UTF-8 before being handed over to GTK+. The following example
writes the content of a IS0-8859-1 encoded text file to
<code class="literal">stdout</code>:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">gchar</span> <span class="o">*</span><span class="n">text</span><span class="p">,</span> <span class="o">*</span><span class="n">utf8_text</span><span class="p">;</span>
<span class="n">gsize</span> <span class="n">length</span><span class="p">;</span>
<span class="n">GError</span> <span class="o">*</span><span class="n">error</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="n">g_file_get_contents</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="o">&</span><span class="n">text</span><span class="p">,</span> <span class="o">&</span><span class="n">length</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">))</span>
<span class="p">{</span>
<span class="n">utf8_text</span> <span class="o">=</span> <span class="n">g_convert</span> <span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="s">"UTF-8"</span><span class="p">,</span> <span class="s">"ISO-8859-1"</span><span class="p">,</span>
<span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="o">&</span><span class="n">error</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="n">error</span> <span class="o">!=</span> <span class="nb">NULL</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">fprintf</span> <span class="p">(</span><span class="s">"Couldn't convert file %s to UTF-8</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">filename</span><span class="p">);</span>
<span class="n">g_error_free</span> <span class="p">(</span><span class="n">error</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="n">g_print</span> <span class="p">(</span><span class="n">utf8_text</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span>
<span class="n">fprintf</span> <span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">"Unable to read file %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">filename</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
For string literals in the source code, there are several alternatives for
handling non-ASCII content:
</p>
<div class="variablelist"><table border="0" class="variablelist">
<colgroup>
<col align="left" valign="top">
<col>
</colgroup>
<tbody>
<tr>
<td><p><span class="term">direct UTF-8</span></p></td>
<td><p>
If your editor and compiler are capable of handling UTF-8 encoded sources,
it is very convenient to simply use UTF-8 for string literals, since it allows
you to edit the strings in "wysiwyg". Note that choosing this option may
reduce the portability of your code.
</p></td>
</tr>
<tr>
<td><p><span class="term">escaped UTF-8</span></p></td>
<td><p>
Even if your toolchain can't handle UTF-8 directly, you can still encode string
literals in UTF-8 by using octal or hexadecimal escapes like
<code class="literal">\212</code> or <code class="literal">\xa8</code> to
encode each byte. This is portable, but modifying the escaped strings is not
very convenient. Be careful when mixing hexadecimal escapes with ordinary text;
<code class="literal">"\xa8abcd"</code> is a string of length 1 !
</p></td>
</tr>
<tr>
<td><p><span class="term">runtime conversion</span></p></td>
<td><p>
If the string literals can be represented in an encoding which your toolchain
can handle (e.g. IS0-8859-1), you can write your source files in that encoding
and use <code class="function">g_convert()</code> to convert the strings to UTF-8 at runtime. Note that this
has some runtime overhead, so you may want to move the conversion out of inner
loops.
</p></td>
</tr>
</tbody>
</table></div>
<p>
Here is an example showing the three approaches using the copyright sign
© which has Unicode and ISO-8859-1 codepoint 169 and is represented in
UTF-8 by the two bytes 194, 169:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">g_print</span> <span class="p">(</span><span class="s">"direct UTF-8: ©"</span><span class="p">);</span>
<span class="n">g_print</span> <span class="p">(</span><span class="s">"escaped UTF-8: </span><span class="se">\302\251</span><span class="s">"</span><span class="p">);</span>
<span class="n">text</span> <span class="o">=</span> <span class="n">g_convert</span> <span class="p">(</span><span class="s">"runtime conversion: ©"</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="s">"ISO-8859-1"</span><span class="p">,</span> <span class="s">"UTF-8"</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="n">g_print</span><span class="p">(</span><span class="n">text</span><span class="p">);</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">text</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
<p>
If you are using <code class="function">gettext()</code> to localize your application, you need to
call <code class="function">bind_textdomain_codeset()</code> to ensure that translated strings are
returned in UTF-8 encoding.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.10"></a><a name="id-1.2.11.3.3.1.10.1"></a><p><b>1.9.</b></p>
</td>
<td align="left" valign="top"><p>
How do I use GTK+ with C++?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
There are two ways to approach this. The GTK+ header files use the subset
of C that's also valid C++, so you can simply use the normal GTK+ API
in a C++ program. Alternatively, you can use a "C++ binding"
such as <a class="ulink" href="http://gtkmm.sourceforge.net/" target="_top">gtkmm</a>
which provides a native C++ API.
</p>
<p>
When using GTK+ directly, keep in mind that only functions can be
connected to signals, not methods. So you will need to use global
functions or "static" class functions for signal connections.
</p>
<p>
Another common issue when using GTK+ directly is that
C++ will not implicitly convert an integer to an enumeration.
This comes up when using bitfields; in C you can write the following
code:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">gdk_window_set_events</span> <span class="p">(</span><span class="n">gdk_window</span><span class="p">,</span>
<span class="n">GDK_BUTTON_PRESS_MASK</span> <span class="o">|</span> <span class="n">GDK_BUTTON_RELEASE_MASK</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
while in C++ you must write:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">gdk_window_set_events</span> <span class="p">(</span><span class="n">gdk_window</span><span class="p">,</span>
<span class="p">(</span><span class="n">GdkEventMask</span><span class="p">)</span> <span class="n">GDK_BUTTON_PRESS_MASK</span> <span class="o">|</span> <span class="n">GDK_BUTTON_RELEASE_MASK</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
There are very few functions that require this cast, however.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.11"></a><a name="id-1.2.11.3.3.1.11.1"></a><p><b>1.10.</b></p>
</td>
<td align="left" valign="top"><p>
How do I use GTK+ with other non-C languages?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
See the <a class="ulink" href="http://www.gtk.org/bindings.html" target="_top">list of language
bindings</a> on <a class="ulink" href="http://www.gtk.org" target="_top">http://www.gtk.org</a>.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.12"></a><a name="id-1.2.11.3.3.1.12.1"></a><p><b>1.11.</b></p>
</td>
<td align="left" valign="top"><p>
How do I load an image or animation from a file?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
To load an image file straight into a display widget, use
<a class="link" href="GtkImage.html#gtk-image-new-from-file" title="gtk_image_new_from_file"><code class="function">gtk_image_new_from_file()</code></a> <a href="#ftn.id-1.2.11.3.3.1.12.2.1.2" class="footnote" name="id-1.2.11.3.3.1.12.2.1.2"><sup class="footnote">[1]</sup></a>.
To load an image for another purpose, use <a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-File-Loading.html#gdk-pixbuf-new-from-file"><code class="function">gdk_pixbuf_new_from_file()</code></a>. To i
load an animation, use <a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-Animations.html#gdk-pixbuf-animation-new-from-file"><code class="function">gdk_pixbuf_animation_new_from_file()</code></a>.
<a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-Animations.html#gdk-pixbuf-animation-new-from-file"><code class="function">gdk_pixbuf_animation_new_from_file()</code></a> can also load non-animated images, so
use it in combination with <a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-Animations.html#gdk-pixbuf-animation-is-static-image"><code class="function">gdk_pixbuf_animation_is_static_image()</code></a> to load a
file of unknown type.
</p>
<p>
To load an image or animation file asynchronously (without blocking), use
<a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/GdkPixbufLoader.html#GdkPixbufLoader-struct"><span class="type">GdkPixbufLoader</span></a>.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.13"></a><a name="id-1.2.11.3.3.1.13.1"></a><p><b>1.12.</b></p>
</td>
<td align="left" valign="top"><p>
How do I draw text ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
To draw a piece of text, use a Pango layout and <a href="../html/gdk2-Drawing-Primitives.html#gdk-draw-layout"><code class="function">gdk_draw_layout()</code></a>,
using code like the following:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">layout</span> <span class="o">=</span> <span class="n">gtk_widget_create_pango_layout</span> <span class="p">(</span><span class="n">widget</span><span class="p">,</span> <span class="n">text</span><span class="p">);</span>
<span class="n">fontdesc</span> <span class="o">=</span> <span class="n">pango_font_description_from_string</span> <span class="p">(</span><span class="s">"Luxi Mono 12"</span><span class="p">);</span>
<span class="n">pango_layout_set_font_description</span> <span class="p">(</span><span class="n">layout</span><span class="p">,</span> <span class="n">fontdesc</span><span class="p">);</span>
<span class="n">gdk_draw_layout</span> <span class="p">(...,</span> <span class="n">layout</span><span class="p">);</span>
<span class="n">pango_font_description_free</span> <span class="p">(</span><span class="n">fontdesc</span><span class="p">);</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">layout</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
Do not use the deprecated <a href="../html/gdk2-Fonts.html#GdkFont"><span class="type">GdkFont</span></a> and <a href="../html/gdk2-Drawing-Primitives.html#gdk-draw-text"><code class="function">gdk_draw_text()</code></a>.
</p>
<p>
See also the "Text Handling in GTK 2" section of
<a class="ulink" href="http://developer.gnome.org/dotplan/porting/" target="_top">Porting applications
to the GNOME 2.0 platform</a>.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.14"></a><a name="id-1.2.11.3.3.1.14.1"></a><p><b>1.13.</b></p>
</td>
<td align="left" valign="top"><p>
How do I measure the size of a piece of text ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
To obtain the size of a piece of text, use a Pango layout and
<a href="https://developer.gnome.org/pango/pango-Layout-Objects.html#pango-layout-get-pixel-size"><code class="function">pango_layout_get_pixel_size()</code></a>, using code like the following:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">layout</span> <span class="o">=</span> <span class="n">gtk_widget_create_pango_layout</span> <span class="p">(</span><span class="n">widget</span><span class="p">,</span> <span class="n">text</span><span class="p">);</span>
<span class="n">fontdesc</span> <span class="o">=</span> <span class="n">pango_font_description_from_string</span> <span class="p">(</span><span class="s">"Luxi Mono 12"</span><span class="p">);</span>
<span class="n">pango_layout_set_font_description</span> <span class="p">(</span><span class="n">layout</span><span class="p">,</span> <span class="n">fontdesc</span><span class="p">);</span>
<span class="n">pango_layout_get_pixel_size</span> <span class="p">(</span><span class="n">layout</span><span class="p">,</span> <span class="o">&</span><span class="n">width</span><span class="p">,</span> <span class="o">&</span><span class="n">height</span><span class="p">);</span>
<span class="n">pango_font_description_free</span> <span class="p">(</span><span class="n">fontdesc</span><span class="p">);</span>
<span class="n">g_object_unref</span> <span class="p">(</span><span class="n">layout</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
Do not use the deprecated function <a href="../html/gdk2-Fonts.html#gdk-text-width"><code class="function">gdk_text_width()</code></a>.
</p>
<p>
See also the "Text Handling in GTK 2" section of
<a class="ulink" href="http://developer.gnome.org/dotplan/porting/" target="_top">Porting applications
to the GNOME 2.0 platform</a>.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.15"></a><a name="id-1.2.11.3.3.1.15.1"></a><p><b>1.14.</b></p>
</td>
<td align="left" valign="top"><p>
Why are types not registered if I use their <code class="literal">GTK_TYPE_BLAH</code>
macro ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
The <code class="literal">GTK_TYPE_BLAH</code> macros are defined as calls to
<code class="literal"><code class="function">gtk_blah_get_type()</code></code>, and the <code class="literal"><code class="function">_get_type()</code></code> i
functions are declared as <code class="literal">G_GNUC_CONST</code> which allows the compiler to optimize
the call away if it appears that the value is not being used.
</p>
<p>
A common workaround for this problem is to store the result in a volatile
variable, which keeps the compiler from optimizing the call away.
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="k">volatile</span> <span class="n">GType</span> <span class="n">dummy</span> <span class="o">=</span> <span class="n">GTK_TYPE_BLAH</span><span class="p">;</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.1.16"></a><a name="id-1.2.11.3.3.1.16.1"></a><p><b>1.15.</b></p>
</td>
<td align="left" valign="top"><p>
How do I create a transparent toplevel window ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
To make a window transparent, it needs to use a visual which supports that.
This is done by getting the RGBA colormap of the screen with
<a href="../html/GdkScreen.html#gdk-screen-get-rgba-colormap"><code class="function">gdk_screen_get_rgba_colormap()</code></a> and setting it on the window. Note that
<a href="../html/GdkScreen.html#gdk-screen-get-rgba-colormap"><code class="function">gdk_screen_get_rgba_colormap()</code></a> will return <code class="literal">NULL</code> if transparent windows
are not supported on the screen; also note that this may change from
screen to screen, so it needs to be repeated whenever the window is moved
to a different screen.
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GdkColormap</span> <span class="o">*</span><span class="n">colormap</span><span class="p">;</span>
<span class="n">colormap</span> <span class="o">=</span> <span class="n">gdk_screen_get_rgba_colormap</span> <span class="p">(</span><span class="n">screen</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">colormap</span><span class="p">)</span>
<span class="n">colormap</span> <span class="o">=</span> <span class="n">gdk_screen_get_rgb_colormap</span> <span class="p">(</span><span class="n">screen</span><span class="p">);</span>
<span class="n">gtk_widget_set_colormap</span> <span class="p">(</span><span class="n">widget</span><span class="p">,</span> <span class="n">colormap</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
One possibility to fill the alpha channel on the window is to use
<a href="../html/gdk2-GdkRGB.html#gdk-draw-rgb-32-image"><code class="function">gdk_draw_rgb_32_image()</code></a>.
</p>
<p>
Note that the presence of an RGBA visual is no guarantee that the
window will actually appear transparent on screen. On X11, this
requires a compositing manager to be running. See
<a class="link" href="GtkWidget.html#gtk-widget-is-composited" title="gtk_widget_is_composited ()"><code class="function">gtk_widget_is_composited()</code></a> for a way to find out if the alpha
channel will be respected.
</p>
</td>
</tr>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.2"></a>2. Which widget should I use...</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.2.2"></a><a name="id-1.2.11.3.3.2.2.1"></a><p><b>2.1.</b></p>
</td>
<td align="left" valign="top"><p>
...for lists and trees?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
See <a class="link" href="TreeWidget.html" title="Tree and List Widget Overview">tree widget overview</a> — you
should use the <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> widget. (A list is just a tree with no branches,
so the tree widget is used for lists as well.) Do not use the deprecated
widgets <a class="link" href="GtkTree.html" title="GtkTree"><span class="type">GtkTree</span></a> or <a class="link" href="GtkCList.html" title="GtkCList"><span class="type">GtkCList</span></a>/<a class="link" href="GtkCTree.html" title="GtkCTree"><span class="type">GtkCTree</span></a> in newly-written code, they are
less flexible and result in an inferior user interface.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.2.3"></a><a name="id-1.2.11.3.3.2.3.1"></a><p><b>2.2.</b></p>
</td>
<td align="left" valign="top"><p>
...for multi-line text display or editing?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
See <a class="link" href="TextWidget.html" title="Text Widget Overview">text widget overview</a> — you
should use the <a class="link" href="GtkTextView.html" title="GtkTextView"><span class="type">GtkTextView</span></a> widget. Do not use the deprecated widget <a class="link" href="GtkText.html" title="GtkText"><span class="type">GtkText</span></a>
in newly-written code, it has a number of problems that are best avoided.
</p>
<p>
If you only have a small amount of text, <a class="link" href="GtkLabel.html" title="GtkLabel"><span class="type">GtkLabel</span></a> may also be appropriate
of course. It can be made selectable with <a class="link" href="GtkLabel.html#gtk-label-set-selectable" title="gtk_label_set_selectable ()"><code class="function">gtk_label_set_selectable()</code></a>. For a
single-line text entry, see <a class="link" href="GtkEntry.html" title="GtkEntry"><span class="type">GtkEntry</span></a>.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.2.4"></a><a name="id-1.2.11.3.3.2.4.1"></a><p><b>2.3.</b></p>
</td>
<td align="left" valign="top"><p>
...to display an image or animation?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
<a class="link" href="GtkImage.html" title="GtkImage"><span class="type">GtkImage</span></a> can display images in just about any format GTK+ understands.
You can also use <a class="link" href="GtkDrawingArea.html" title="GtkDrawingArea"><span class="type">GtkDrawingArea</span></a> if you need to do something more complex,
such as draw text or graphics over the top of the image.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.2.5"></a><a name="id-1.2.11.3.3.2.5.1"></a><p><b>2.4.</b></p>
</td>
<td align="left" valign="top"><p>
...for presenting a set of mutually-exclusive choices, where Windows
would use a combo box?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
With GTK+, a <a class="link" href="GtkComboBox.html" title="GtkComboBox"><span class="type">GtkComboBox</span></a> is the recommended widget to use for this use case.
This widget looks like either a combo box or the current option menu, depending
on the current theme. If you need an editable text entry, use <a class="link" href="GtkComboBoxEntry.html" title="GtkComboBoxEntry"><span class="type">GtkComboBoxEntry</span></a>.
</p></td>
</tr>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.3"></a>3. GtkWidget</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.3.2"></a><a name="id-1.2.11.3.3.3.2.1"></a><p><b>3.1.</b></p>
</td>
<td align="left" valign="top"><p>
How do I change the color of a widget?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
See <a class="link" href="GtkWidget.html#gtk-widget-modify-fg" title="gtk_widget_modify_fg ()"><code class="function">gtk_widget_modify_fg()</code></a>, <a class="link" href="GtkWidget.html#gtk-widget-modify-bg" title="gtk_widget_modify_bg ()"><code class="function">gtk_widget_modify_bg()</code></a>, <a class="link" href="GtkWidget.html#gtk-widget-modify-base" title="gtk_widget_modify_base ()"><code class="function">gtk_widget_modify_base()</code></a>,
and <a class="link" href="GtkWidget.html#gtk-widget-modify-text" title="gtk_widget_modify_text ()"><code class="function">gtk_widget_modify_text()</code></a>. See <GTKDOCLINK HREF="gtk-Resource-Files">GTK+
resource files</GTKDOCLINK> for more discussion. You can also change widget color
by installing a resource file and parsing it with <a class="link" href="gtk2-Resource-Files.html#gtk-rc-add-default-file" title="gtk_rc_add_default_file"><code class="function">gtk_rc_add_default_file()</code></a>.
The advantage of a resource file is that users can then override the
color you've chosen.
</p>
<p>To change the background color for widgets such as <a class="link" href="GtkLabel.html" title="GtkLabel"><span class="type">GtkLabel</span></a> that have
no background, place them in a <a class="link" href="GtkEventBox.html" title="GtkEventBox"><span class="type">GtkEventBox</span></a> and set the background of the
event box.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.3.3"></a><a name="id-1.2.11.3.3.3.3.1"></a><p><b>3.2.</b></p>
</td>
<td align="left" valign="top"><p>
How do I change the font of a widget?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
This has several possible answers, depending on what exactly you want to
achieve. One option is <a class="link" href="GtkWidget.html#gtk-widget-modify-font" title="gtk_widget_modify_font ()"><code class="function">gtk_widget_modify_font()</code></a>. Note that this function
can be used to change only the font size, as in the following example:
</p>
<pre class="programlisting">
PangoFontDesc *font_desc = pango_font_description_new ();
pango_font_description_set_size (font_desc, 40);
gtk_widget_modify_font (widget, font);
pango_font_description_free (font_desc);
</pre>
<p>
</p>
<p>
If you want to make the text of a label larger, you can use
<a class="link" href="GtkLabel.html#gtk-label-set-markup" title="gtk_label_set_markup ()"><code class="function">gtk_label_set_markup()</code></a>:
</p>
<pre class="programlisting">
gtk_label_set_markup (label, "<big>big text</big>");
</pre>
<p>
This is preferred for many apps because it's a relative size to the
user's chosen font size. See <code class="function">g_markup_escape_text()</code> if you are
constructing such strings on the fly.
</p>
<p>
You can also change the font of a widget by putting
</p>
<pre class="programlisting">
gtk-font-name = "Sans 30"
</pre>
<p>
in a resource file and parsing it with <a class="link" href="gtk2-Resource-Files.html#gtk-rc-add-default-file" title="gtk_rc_add_default_file"><code class="function">gtk_rc_add_default_file()</code></a>.
The advantage of a resource file is that users can then override the font you
have chosen. See GTK+ resource files
for more discussion.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.3.4"></a><a name="id-1.2.11.3.3.3.4.1"></a><p><b>3.3.</b></p>
</td>
<td align="left" valign="top"><p>
How do I disable/ghost/desensitize a widget?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p> In GTK+ a disabled widget is termed "insensitive." See
<a class="link" href="GtkWidget.html#gtk-widget-set-sensitive" title="gtk_widget_set_sensitive ()"><code class="function">gtk_widget_set_sensitive()</code></a>.
</p></td>
</tr>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.4"></a>4. GtkTextView</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.4.2"></a><a name="id-1.2.11.3.3.4.2.1"></a><p><b>4.1.</b></p>
</td>
<td align="left" valign="top"><p>
How do I get the contents of the entire text widget as a string?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
See <a class="link" href="GtkTextBuffer.html#gtk-text-buffer-get-bounds" title="gtk_text_buffer_get_bounds ()"><code class="function">gtk_text_buffer_get_bounds()</code></a> and <a class="link" href="GtkTextBuffer.html#gtk-text-buffer-get-text" title="gtk_text_buffer_get_text ()"><code class="function">gtk_text_buffer_get_text()</code></a>
or <a class="link" href="GtkTextIter.html#gtk-text-iter-get-text" title="gtk_text_iter_get_text ()"><code class="function">gtk_text_iter_get_text()</code></a>.
</p>
<p>
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="n">GtkTextIter</span> <span class="n">start</span><span class="p">,</span> <span class="n">end</span><span class="p">;</span>
<span class="n">GtkTextBuffer</span> <span class="o">*</span><span class="n">buffer</span><span class="p">;</span>
<span class="kt">char</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
<span class="n">buffer</span> <span class="o">=</span> <span class="n">gtk_text_view_get_buffer</span> <span class="p">(</span><span class="n">GTK_TEXT_VIEW</span> <span class="p">(</span><span class="n">text_view</span><span class="p">));</span>
<span class="n">gtk_text_buffer_get_bounds</span> <span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="o">&</span><span class="n">start</span><span class="p">,</span> <span class="o">&</span><span class="n">end</span><span class="p">);</span>
<span class="n">text</span> <span class="o">=</span> <span class="n">gtk_text_iter_get_text</span> <span class="p">(</span><span class="o">&</span><span class="n">start</span><span class="p">,</span> <span class="o">&</span><span class="n">end</span><span class="p">);</span>
<span class="cm">/* use text */</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">text</span><span class="p">);</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.4.3"></a><a name="id-1.2.11.3.3.4.3.1"></a><p><b>4.2.</b></p>
</td>
<td align="left" valign="top"><p>
How do I make a text widget display its complete contents in a specific font?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
If you use <a class="link" href="GtkTextBuffer.html#gtk-text-buffer-insert-with-tags" title="gtk_text_buffer_insert_with_tags ()"><code class="function">gtk_text_buffer_insert_with_tags()</code></a> 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.
</p>
<p>
To ensure that all text has the desired appearance, use <a class="link" href="GtkWidget.html#gtk-widget-modify-font" title="gtk_widget_modify_font ()"><code class="function">gtk_widget_modify_font()</code></a>
to change the default font for the widget.
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.4.4"></a><a name="id-1.2.11.3.3.4.4.1"></a><p><b>4.3.</b></p>
</td>
<td align="left" valign="top"><p>
How do I make a text view scroll to the end of the buffer automatically ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
A good way to keep a text buffer scrolled to the end is to place a
<a class="link" href="GtkTextMark.html" title="GtkTextMark">mark</a> at the end of the buffer, and
give it right gravity. The gravity has the effect that text inserted
at the mark gets inserted <span class="emphasis"><em>before</em></span>, keeping the mark
at the end.
</p>
<p>
To ensure that the end of the buffer remains visible, use
<a class="link" href="GtkTextView.html#gtk-text-view-scroll-to-mark" title="gtk_text_view_scroll_to_mark ()"><code class="function">gtk_text_view_scroll_to_mark()</code></a> to scroll to the mark after
inserting new text.
</p>
<p>
The gtk-demo application contains an example of this technique.
</p>
</td>
</tr>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.5"></a>5. <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a>
</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.2"></a><a name="id-1.2.11.3.3.5.2.1"></a><p><b>5.1.</b></p>
</td>
<td align="left" valign="top"><p>
How do I associate some data with a row in the tree?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
Remember that the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a> columns don't necessarily have to be displayed.
So you can put non-user-visible data in your model just like any other data,
and retrieve it with <a class="link" href="GtkTreeModel.html#gtk-tree-model-get" title="gtk_tree_model_get ()"><code class="function">gtk_tree_model_get()</code></a>. See the
<a class="link" href="TreeWidget.html" title="Tree and List Widget Overview">tree widget overview</a>.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.3"></a><a name="id-1.2.11.3.3.5.3.1"></a><p><b>5.2.</b></p>
</td>
<td align="left" valign="top"><p>
What's the <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a> equivalent of <a class="link" href="GtkCList.html#gtk-clist-find-row-from-data" title="gtk_clist_find_row_from_data ()"><code class="function">gtk_clist_find_row_from_data()</code></a>?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
As there is no separate data column in the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a>, there's no
built in function to find the iter from data. You can write a custom
searching function to walk the tree and find the data, or use
<a class="link" href="GtkTreeModel.html#gtk-tree-model-foreach" title="gtk_tree_model_foreach ()"><code class="function">gtk_tree_model_foreach()</code></a>.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.4"></a><a name="id-1.2.11.3.3.5.4.1"></a><p><b>5.3.</b></p>
</td>
<td align="left" valign="top"><p>
How do I put an image and some text in the same column?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
You can pack more than one <a class="link" href="GtkCellRenderer.html" title="GtkCellRenderer"><span class="type">GtkCellRenderer</span></a> into a single <a class="link" href="GtkTreeViewColumn.html" title="GtkTreeViewColumn"><span class="type">GtkTreeViewColumn</span></a>
using <a class="link" href="GtkTreeViewColumn.html#gtk-tree-view-column-pack-start" title="gtk_tree_view_column_pack_start ()"><code class="function">gtk_tree_view_column_pack_start()</code></a> or <a class="link" href="GtkTreeViewColumn.html#gtk-tree-view-column-pack-end" title="gtk_tree_view_column_pack_end ()"><code class="function">gtk_tree_view_column_pack_end()</code></a>.
So pack both a <a class="link" href="GtkCellRendererPixbuf.html" title="GtkCellRendererPixbuf"><span class="type">GtkCellRendererPixbuf</span></a> and a <a class="link" href="GtkCellRendererText.html" title="GtkCellRendererText"><span class="type">GtkCellRendererText</span></a> into the
column.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.5"></a><a name="id-1.2.11.3.3.5.5.1"></a><p><b>5.4.</b></p>
</td>
<td align="left" valign="top"><p>
I can set data easily on my <a class="link" href="GtkTreeStore.html" title="GtkTreeStore"><span class="type">GtkTreeStore</span></a>/<a class="link" href="GtkListStore.html" title="GtkListStore"><span class="type">GtkListStore</span></a> models using
<a class="link" href="GtkListStore.html#gtk-list-store-set" title="gtk_list_store_set ()"><code class="function">gtk_list_store_set()</code></a> and <a class="link" href="GtkTreeStore.html#gtk-tree-store-set" title="gtk_tree_store_set ()"><code class="function">gtk_tree_store_set()</code></a>, but can't read it back?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
Both the <a class="link" href="GtkTreeStore.html" title="GtkTreeStore"><span class="type">GtkTreeStore</span></a> and the <a class="link" href="GtkListStore.html" title="GtkListStore"><span class="type">GtkListStore</span></a> implement the <a class="link" href="GtkTreeModel.html" title="GtkTreeModel"><span class="type">GtkTreeModel</span></a>
interface. Consequentially, the can use any function this interface
implements. The easiest way to read a set of data back is to use
<a class="link" href="GtkTreeModel.html#gtk-tree-model-get" title="gtk_tree_model_get ()"><code class="function">gtk_tree_model_get()</code></a>.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.6"></a><a name="id-1.2.11.3.3.5.6.1"></a><p><b>5.5.</b></p>
</td>
<td align="left" valign="top"><p>
How do I change the way that numbers are formatted by <a class="link" href="GtkTreeView.html" title="GtkTreeView"><span class="type">GtkTreeView</span></a>?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
Use <a class="link" href="GtkTreeView.html#gtk-tree-view-insert-column-with-data-func" title="gtk_tree_view_insert_column_with_data_func ()"><code class="function">gtk_tree_view_insert_column_with_data_func()</code></a>
or <a class="link" href="GtkTreeViewColumn.html#gtk-tree-view-column-set-cell-data-func" title="gtk_tree_view_column_set_cell_data_func ()"><code class="function">gtk_tree_view_column_set_cell_data_func()</code></a> and do the conversion from i
number to string yourself (with, say, <code class="function">g_strdup_printf()</code>).
</p>
<p>
The following example demonstrates this:
</p>
<div class="informalexample">
<table class="listing_frame" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="listing_lines" align="right"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64</pre></td>
<td class="listing_code"><pre class="programlisting"><span class="k">enum</span>
<span class="p">{</span>
<span class="n">DOUBLE_COLUMN</span><span class="p">,</span>
<span class="n">N_COLUMNS</span>
<span class="p">};</span>
<span class="n">GtkListStore</span> <span class="o">*</span><span class="n">mycolumns</span><span class="p">;</span>
<span class="n">GtkTreeView</span> <span class="o">*</span><span class="n">treeview</span><span class="p">;</span>
<span class="kt">void</span>
<span class="nf">my_cell_double_to_text</span> <span class="p">(</span><span class="n">GtkTreeViewColumn</span> <span class="o">*</span><span class="n">tree_column</span><span class="p">,</span>
<span class="n">GtkCellRenderer</span> <span class="o">*</span><span class="n">cell</span><span class="p">,</span>
<span class="n">GtkTreeModel</span> <span class="o">*</span><span class="n">tree_model</span><span class="p">,</span>
<span class="n">GtkTreeIter</span> <span class="o">*</span><span class="n">iter</span><span class="p">,</span>
<span class="n">gpointer</span> <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">GtkCellRendererText</span> <span class="o">*</span><span class="n">cell_text</span> <span class="o">=</span> <span class="p">(</span><span class="n">GtkCellRendererText</span> <span class="o">*</span><span class="p">)</span><span class="n">cell</span><span class="p">;</span>
<span class="n">gdouble</span> <span class="n">d</span><span class="p">;</span>
<span class="n">gchar</span> <span class="o">*</span><span class="n">text</span><span class="p">;</span>
<span class="cm">/* Get the double value from the model. */</span>
<span class="n">gtk_tree_model_get</span> <span class="p">(</span><span class="n">tree_model</span><span class="p">,</span> <span class="n">iter</span><span class="p">,</span> <span class="p">(</span><span class="n">gint</span><span class="p">)</span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="n">d</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span>
<span class="cm">/* Now we can format the value ourselves. */</span>
<span class="n">text</span> <span class="o">=</span> <span class="n">g_strdup_printf</span> <span class="p">(</span><span class="s">"%.2f"</span><span class="p">,</span> <span class="n">d</span><span class="p">);</span>
<span class="n">g_object_set</span> <span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="s">"text"</span><span class="p">,</span> <span class="n">text</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="n">g_free</span> <span class="p">(</span><span class="n">text</span><span class="p">);</span>
<span class="p">}</span>
<span class="kt">void</span>
<span class="nf">set_up_new_columns</span> <span class="p">(</span><span class="n">GtkTreeView</span> <span class="o">*</span><span class="n">myview</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">GtkCellRendererText</span> <span class="o">*</span><span class="n">renderer</span><span class="p">;</span>
<span class="n">GtkTreeViewColumn</span> <span class="o">*</span><span class="n">column</span><span class="p">;</span>
<span class="n">GtkListStore</span> <span class="o">*</span><span class="n">mycolumns</span><span class="p">;</span>
<span class="cm">/* Create the data model and associate it with the given TreeView */</span>
<span class="n">mycolumns</span> <span class="o">=</span> <span class="n">gtk_list_store_new</span> <span class="p">(</span><span class="n">N_COLUMNS</span><span class="p">,</span> <span class="n">G_TYPE_DOUBLE</span><span class="p">);</span>
<span class="n">gtk_tree_view_set_model</span> <span class="p">(</span><span class="n">myview</span><span class="p">,</span> <span class="n">GTK_TREE_MODEL</span> <span class="p">(</span><span class="n">mycolumns</span><span class="p">));</span>
<span class="cm">/* Create a GtkCellRendererText */</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_text_new</span> <span class="p">();</span>
<span class="cm">/* Create a new column that has a title ("Example column"),</span>
<span class="cm"> * uses the above created renderer that will render the double</span>
<span class="cm"> * value into text from the associated model's rows. </span>
<span class="cm"> */</span>
<span class="n">column</span> <span class="o">=</span> <span class="n">gtk_tree_view_column_new</span> <span class="p">();</span>
<span class="n">gtk_tree_view_column_set_title</span> <span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="s">"Example column"</span><span class="p">);</span>
<span class="n">renderer</span> <span class="o">=</span> <span class="n">gtk_cell_renderer_text_new</span> <span class="p">();</span>
<span class="n">gtk_tree_view_column_pack_start</span> <span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">renderer</span><span class="p">,</span> <span class="n">TRUE</span><span class="p">);</span>
<span class="cm">/* Append the new column after the GtkTreeView's previous columns. */</span>
<span class="n">gtk_tree_view_append_column</span> <span class="p">(</span><span class="n">GTK_TREE_VIEW</span> <span class="p">(</span><span class="n">myview</span><span class="p">),</span> <span class="n">column</span><span class="p">);</span>
<span class="cm">/* Since we created the column by hand, we can set it up for our</span>
<span class="cm"> * needs, e.g. set its minimum and maximum width, etc.</span>
<span class="cm"> */</span>
<span class="cm">/* Set up a custom function that will be called when the column content</span>
<span class="cm"> * is rendered. We use the func_data pointer as an index into our</span>
<span class="cm"> * model. This is convenient when using multi column lists. </span>
<span class="cm"> */</span>
<span class="n">gtk_tree_view_column_set_cell_data_func</span> <span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">renderer</span><span class="p">,</span>
<span class="n">my_cell_double_to_text</span><span class="p">,</span>
<span class="p">(</span><span class="n">gpointer</span><span class="p">)</span><span class="n">DOUBLE_COLUMN</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">);</span>
<span class="p">}</span></pre></td>
</tr>
</tbody>
</table>
</div>
<p>
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.5.7"></a><a name="id-1.2.11.3.3.5.7.1"></a><p><b>5.6.</b></p>
</td>
<td align="left" valign="top"><p>
How do I hide the expander arrows in my tree view ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
Set the expander-column property of the tree view to a hidden column.
See <a class="link" href="GtkTreeView.html#gtk-tree-view-set-expander-column" title="gtk_tree_view_set_expander_column ()"><code class="function">gtk_tree_view_set_expander_column()</code></a> and <a class="link" href="GtkTreeViewColumn.html#gtk-tree-view-column-set-visible" title="gtk_tree_view_column_set_visible ()"><code class="function">gtk_tree_view_column_set_visible()</code></a>.
</p></td>
</tr>
<tr class="qandadiv"><td align="left" valign="top" colspan="2"><h5 class="title">
<a name="id-1.2.11.3.3.6"></a>6. Using cairo with GTK+</h5></td></tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.6.2"></a><a name="id-1.2.11.3.3.6.2.1"></a><p><b>6.1.</b></p>
</td>
<td align="left" valign="top"><p>
How do I use cairo to draw in GTK+ applications ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
Use <a href="../html/gdk2-Cairo-Interaction.html#gdk-cairo-create"><code class="function">gdk_cairo_create()</code></a> to obtain a cairo context for drawing
on a GDK window or pixmap. See <GTKDOCLINK HREF="gdk-Cairo-Interaction">Cairo
Interaction</GTKDOCLINK> for some more useful functions.
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.6.3"></a><a name="id-1.2.11.3.3.6.3.1"></a><p><b>6.2.</b></p>
</td>
<td align="left" valign="top"><p>
I have created a cairo context with <a href="../html/gdk2-Cairo-Interaction.html#gdk-cairo-create"><code class="function">gdk_cairo_create()</code></a>, but when I
later use it, my drawing does not show up. Why is that ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>
All drawing in GTK+ is normally done in an expose handler, and GTK+
creates a temporary pixmap for double-buffering the drawing. If you
create a cairo context outside the expose handler, it is backed
by the GDK window itself, not the double-buffering pixmap. Consequently,
any drawing you do with that cairo context gets overwritten at the
end of the expose handler, when the double-buffering pixmap is copied
back.
</p>
<p>
Possible solutions to this problem are:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
Turn off double-buffering, with <a class="link" href="GtkWidget.html#gtk-widget-set-double-buffered" title="gtk_widget_set_double_buffered ()"><code class="function">gtk_widget_set_double_buffered()</code></a>.
This is not ideal, since it can cause some flickering.
</p></li>
<li class="listitem"><p>
Create the cairo context inside the expose handler. If you do this,
<code class="function">gdk_create_cairo()</code> arranges for it to be backed by the double-buffering
pixmap. This is the preferred solution, and is used throughout GTK+
itself.
</p></li>
</ul></div>
<p>
</p>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.6.4"></a><a name="id-1.2.11.3.3.6.4.1"></a><p><b>6.3.</b></p>
</td>
<td align="left" valign="top"><p>
Can I improve the performance of my application by using the
Glitz backend of cairo ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
No. The GDK X11 backend uses the cairo X backend (and the other
GDK backends use their respective native cairo backends). The
GTK+ developers believe that the best way to improving the GDK
drawing performance is to optimize the cairo X backend and the
relevant code paths in the X server that is uses (mostly the
Render extension).
</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id-1.2.11.3.3.6.5"></a><a name="id-1.2.11.3.3.6.5.1"></a><p><b>6.4.</b></p>
</td>
<td align="left" valign="top"><p>
Can I use cairo to draw on a <a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-The-GdkPixbuf-Structure.html#GdkPixbuf-struct"><span class="type">GdkPixbuf</span></a> ?
</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top"><p>
No, at least not yet. The cairo image surface does not support the
pixel format used by GdkPixbuf.
</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="footnotes">
<br><hr style="width:100; text-align:left;margin-left: 0">
<div id="ftn.id-1.2.11.3.3.1.12.2.1.2" class="footnote"><p><a href="#id-1.2.11.3.3.1.12.2.1.2" class="para"><sup class="para">[1] </sup></a> If the file load fails,
<a class="link" href="GtkImage.html#gtk-image-new-from-file" title="gtk_image_new_from_file"><code class="function">gtk_image_new_from_file()</code></a> will display no image graphic — to detect
a failed load yourself, use <a href="http://library.gnome.org/devel/gdk-pixbuf/unstable/gdk-pixbuf-File-Loading.html#gdk-pixbuf-new-from-file"><code class="function">gdk_pixbuf_new_from_file()</code></a> directly, then
<a class="link" href="GtkImage.html#gtk-image-new-from-pixbuf" title="gtk_image_new_from_pixbuf ()"><code class="function">gtk_image_new_from_pixbuf()</code></a>.</p></div>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.33.0</div>
</body>
</html>
|