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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>
Frequently Asked Questions
</title>
<meta name="GENERATOR" content=
"Modular DocBook HTML Stylesheet Version 1.45">
<link rel="HOME" title="GTK+ / Gnome Application Development"
href="ggad.html">
<link rel="UP" title="Appendices" href="appendices.html">
<link rel="PREVIOUS" title="Table of Header Files" href=
"headers.html">
<link rel="NEXT" title="Online Resources" href="online.html">
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
"#840084" alink="#0000FF">
<div class="NAVHEADER">
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<th colspan="4" align="center">
<font color="#000000" size="2">GTK+ / Gnome Application
Development</font>
</th>
</tr>
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="headers.html"><font color="#0000ff" size="2">
<b><<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="online.html"><font color="#0000ff" size="2">
<b>Next >>></b></font></a>
</td>
</tr>
</table>
</div>
<div class="CHAPTER">
<h1>
<a name="FAQS">Frequently Asked Questions</a>
</h1>
<p>
This chapter contains some commonly-asked questions, and
answers, with references to the rest of the book. See the
table of contents for a summary of the questions.
</p>
<div class="SECT1">
<h1 class="SECT1">
<a name="Z827">Questions, with Answers</a>
</h1>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z828">How do I make my application beep?</a>
</h2>
<p>
Call the <tt class="FUNCTION">gdk_beep()</tt> function.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z829">When do I need to destroy my
widgets?</a>
</h2>
<p>
See <a href="z57.html#WIDGETLIFECYCLE">the section
called <i>Widget Life Cycle</i> in the chapter called
<i>GTK+ Basics</i></a> for the simple answer, and <a
href="sec-finalization.html">the section called <i>
Object Finalization</i> in the chapter called <i>The
GTK+ Object and Type System</i></a> for more details.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z830">When I turn on memory profiling in glib,
my application becomes unstable. What gives?</a>
</h2>
<p>
Normally <tt class="FUNCTION">g_malloc()</tt> and <tt
class="FUNCTION">g_free()</tt> are just wrappers around
<tt class="FUNCTION">malloc()</tt> and <tt class=
"FUNCTION">free()</tt>, with a couple of extra features
described in <a href="cha-glib.html#GLIB-MEMORY">the
section called <i>Memory</i> in the chapter called <i>
glib: Portability and Utility</i></a>. However, when
you turn on memory profiling, they are no longer
interchangeable with <tt class="FUNCTION">malloc()</tt>
and <tt class="FUNCTION">free()</tt>. So anytime you
incorrectly mix the two pairs of functions, your
program will crash.
</p>
<p>
If you're using the GNU C library, which comes with
nearly all Linux distributions, it has a special
feature which can help you debug this. Set the <tt
class="APPLICATION">MALLOC_CHECK_</tt> environment
variable to <tt class="APPLICATION">2</tt> before
running your program, then run the program in <tt
class="APPLICATION">gdb</tt>. As soon as <tt class=
"FUNCTION">free()</tt> gets a pointer not created by
<tt class="FUNCTION">malloc()</tt>, <tt class=
"FUNCTION">abort()</tt> will be called.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z831">To create a custom display, I want to
place widgets in arbitrary locations, or move them
around rapidly, or draw to them directly. How?</a>
</h2>
<p>
You are probably fighting a losing battle. Widgets
really aren't what you want, most likely. Consider
using a <tt class="CLASSNAME">GtkDrawingArea</tt> or
the <tt class="CLASSNAME">GnomeCanvas</tt> to create
your custom display.
</p>
<p>
If you really need interactive widgets, such as a <tt
class="CLASSNAME">GtkEntry</tt> or <tt class=
"CLASSNAME">GtkButton</tt>, you can try to use <tt
class="CLASSNAME">GtkLayout</tt> or <tt class=
"CLASSNAME">GtkFixed</tt>.
</p>
<p>
If you have very specialized needs, you probably need
to write your own widget. <a href="cha-widget.html">the
chapter called <i>Writing a <tt class="CLASSNAME">
GtkWidget</tt></i></a> tells you how to do so.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z832">Why does my memory debugging tool show
memory leaks in glib?</a>
</h2>
<p>
glib does not call <tt class="FUNCTION">malloc()</tt>
every time it needs a new node in a data structure. If
it did, building linked lists (for example) would be
substantially slower. Instead, glib caches pools of
equal-sized "memory chunks" for use in these data
structures. Since the chunks are still available for
recycling when your program exits, they are never <tt
class="FUNCTION">free()</tt>d. (Of course, the
operating system will reclaim the memory, but tools
such as <tt class="APPLICATION">ccmalloc</tt> and <tt
class="APPLICATION">Purify</tt> will report it as a
memory leak.)
</p>
<p>
To get around this, you can plug a new <span class=
"STRUCTNAME">GAllocator</span> into most of the data
structures. A <span class="STRUCTNAME">
GAllocator</span> is a pool of memory as described
above. Just create an allocator manually, so you have a
pointer to it; you can then free the allocator when you
are finished. <a href="faqs.html#FL-GLISTALLOCATOR">
Figure 1</a> summarizes the relevant functions for
<span class="STRUCTNAME">GList</span>. A quick glance
through <tt class="FILENAME">glib.h</tt> will reveal
the corresponding functions for other data structures.
</p>
<p>
The <tt class="APPLICATION">name</tt> argument to <tt
class="FUNCTION">g_allocator_new()</tt> is used in
debugging messages; the <tt class="APPLICATION">
n_preallocs</tt> argument is passed through to <tt
class="FUNCTION">g_mem_chunk_new()</tt>.
</p>
<div class="FIGURE">
<a name="FL-GLISTALLOCATOR"></a>
<div class="FUNCSYNOPSIS">
<a name="FL-GLISTALLOCATOR.SYNOPSIS"></a>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="FUNCSYNOPSISINFO">
#include <glib.h>
</pre>
</td>
</tr>
</table>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
g_list_push_allocator</tt></code>(GAllocator* <tt
class="PARAMETER"><i>allocator</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">
g_list_pop_allocator</tt></code>(void);</code>
</p>
<p>
<code><code class="FUNCDEF">GAllocator* <tt class=
"FUNCTION">g_allocator_new</tt></code>(gchar* <tt
class="PARAMETER"><i>name</i></tt>, guint <tt
class="PARAMETER"><i>n_preallocs</i></tt>);</code>
</p>
<p>
<code><code class="FUNCDEF">void <tt class=
"FUNCTION">g_allocator_free</tt></code>(GAllocator*
<tt class="PARAMETER"><i>
allocator</i></tt>);</code>
</p>
</div>
<p>
<b>Figure 1. Functions for replacing the <span class=
"STRUCTNAME">GList</span> memory allocator</b>
</p>
</div>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z833">I get a bunch of "assertion failed"
warnings from GTK+. What causes these?</a>
</h2>
<p>
These come from the <tt class="FUNCTION">
g_return_if_fail()</tt> checks at the beginning of many
GTK+ functions. (They will only appear if your copy of
GTK+ was compiled with debugging turned on---and
hopefully it was if you are writing an application.)
You will need to look at the exact assertion that
failed to see what causes the warning. A common one: if
you accidentally access a destroyed widget or object,
you will have a pointer to memory garbage. Among other
things, this means the type tag will be invalid; so
GTK+'s runtime type checks will fail.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z834">Why are some things in Gnome rather than
GTK+?</a>
</h2>
<p>
Historical accident, mostly. Sometimes there is a
reason; for example, GTK+ does not include <tt class=
"APPLICATION">gdk_imlib</tt>, so does not include any
widgets that rely on it. In very general terms, GTK+
imposes less "policy" than Gnome; some Gnome widgets
are deliberately inflexible to keep people from
creating an inconsistent user interface. GTK+ does not
take this approach. Finally, some of the Gnome widgets
were considered too "experimental" to go in GTK+ at the
time. However, the core Gnome widgets discussed in this
book are not in this category.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z835">How can I center a window on the
screen?</a>
</h2>
<p>
If the window is a <tt class="CLASSNAME">
GnomeDialog</tt>, this is user-configurable and you
should not do it. In most other cases it would be a bit
strange; but there are exceptions, such as splash
screens. The function you want is <tt class="FUNCTION">
gtk_window_set_position()</tt>; you can leave the
window's position up to the window manager (the
default), ask to have it centered, or ask to have it
appear wherever the mouse pointer is. There is an
enumeration which corresponds to these settings: <span
class="STRUCTNAME">GTK_WIN_POS_NONE</span>, <span
class="STRUCTNAME">GTK_WIN_POS_CENTER</span>, <span
class="STRUCTNAME">GTK_WIN_POS_MOUSE</span>. For
example:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
</pre>
</td>
</tr>
</table>
<p>
You should do this <i class="EMPHASIS">before</i>
calling <tt class="FUNCTION">gtk_widget_show()</tt>,
because the function affects where the window appears
when it is first placed on-screen.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z836">Is there a widget that does
printing?</a>
</h2>
<p>
No. When people ask this question they are usually
looking for an abstract interface that draws either to
the screen or to a printer. There is nothing like that
in GTK+ right now. <tt class="CLASSNAME">
GnomeCanvas</tt> will probably have a feature like this
in a future version.
</p>
<p>
There is a <tt class="APPLICATION">gnome-print</tt>
library available, which handles many unpleasant
low-level details when dealing with fonts and
PostScript. It also comes with a printer-selection
dialog.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z837">When I <tt class="FUNCTION">fork()</tt>,
I get a bunch of warnings and my program crashes.
What's going on?</a>
</h2>
<p>
There are two things to remember:
</p>
<ol type="1">
<li>
<p>
The child process must not try to use the GUI;
since it shares file descriptors with the parent,
including GTK+'s connection to the X server, GTK+
will become very confused.
</p>
</li>
<li>
<p>
The child process must be terminated with <tt
class="FUNCTION">_exit()</tt> rather than <tt
class="FUNCTION">exit()</tt>; calling <tt class=
"FUNCTION">exit()</tt> will shut down GTK+ and
confuse the parent process. (GTK+ registers a
"cleanup" function using <tt class="FUNCTION">
atexit()</tt>.)
</p>
</li>
</ol>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z838">When do I need to call <tt class=
"FUNCTION">gtk_widget_realize()</tt> vs. <tt class=
"FUNCTION">gtk_widget_show()</tt>?</a>
</h2>
<p>
<a href="z57.html#SEC-REALIZINGSHOWING">the section
called <i>Realizing, Mapping, and Showing</i> in the
chapter called <i>GTK+ Basics</i></a> goes into some
detail on this. But here is a brief summary.
</p>
<p>
Showing a widget implies mapping it eventually (to be
precise, it schedules the widget to be mapped when its
parent widgets are mapped). Mapping a widget means
calling <tt class="FUNCTION">gdk_window_show()</tt> to
display the widget's <span class="STRUCTNAME">
GdkWindow</span> on the screen (if it has a <span
class="STRUCTNAME">GdkWindow</span>, some widgets
don't). To map a widget you must first realize it.
Therefore showing a widget implies realizing it.
Therefore if you show a widget you don't need to
explicitly realize it with <tt class="FUNCTION">
gtk_widget_realize()</tt> because it will be realized
eventually anyway.
</p>
<p>
There's one exception, however. To <i class=
"FIRSTTERM">realize</i> a widget means to allocate X
server resources for it, most notably a <span class=
"STRUCTNAME">GdkWindow</span>. Some things you might
want to do require the <span class="STRUCTNAME">
GdkWindow</span> to exist, so you might want to force a
widget to be realized immediately. <tt class=
"FUNCTION">gtk_widget_realize()</tt> does this. Since
parent widgets must be realized before their children,
<tt class="FUNCTION">gtk_widget_realize()</tt> will
immediately realize all of a widget's parents as well.
One of these parents must be a toplevel window, or
realization will not be possible.
</p>
<p>
If you force-realize a widget, you still have to call
<tt class="FUNCTION">gtk_widget_show()</tt> since
realization does not map the widget.
</p>
<p>
A good but not foolproof rule of thumb: if you are
using <span class="STRUCTNAME">
GTK_WIDGET(widget)->window</span>, you will need
<span class="STRUCTNAME">widget</span> to be realized.
</p>
<p>
However, it should be noted that force-realizing a
widget is always a mildly bad idea; it is inefficient
and uncomfortably low-level. In many cases you can work
around the need to do so.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z839">When creating a pixmap, I get the
warning: <tt class="APPLICATION">Creating pixmap from
xpm with NULL window and colormap</tt>. What's
wrong?</a>
</h2>
<p>
Creating a pixmap requires a colormap. <tt class=
"FUNCTION">gdk_pixmap_create_from_xpm_d()</tt> requires
a <span class="STRUCTNAME">GdkWindow</span> argument in
order to extract a colormap. You are probably trying to
use the <span class="STRUCTNAME">window</span> field of
an unrealized widget, which is <span class=
"STRUCTNAME">NULL</span>. You might try the newer
function, <tt class="FUNCTION">
gdk_pixmap_colormap_create_from_xpm_d()</tt> which
accepts a colormap argument; if you pass in a colormap,
its window argument can be <span class="STRUCTNAME">
NULL</span>. However, using Imlib instead is a still
better solution; Imlib's pixmap routines are faster
anyway.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z840">How can I separate the GUI from the rest
of my application?</a>
</h2>
<p>
For a variety of reasons, an application's graphical
interface tends to be an exceptionally volatile and
ever-changing piece of software. It's the focus of most
user requests for change. It is difficult to plan and
execute well the first time around---often you will
discover that some aspect of it is unpleasant to use
only after you have written it. Making things worse,
graphical interfaces are not portable across machines;
Gnome works on X windows, but if your application is
useful, it won't be long before someone wants to run
your application on another system, or have a
command-line version, or have a web-based interface.
You might even want to have two interfaces in the same
version---perhaps the GUI, and a scripting language
such as Guile.
</p>
<p>
In practical terms, this means that any large
application should have a radical separation between
its various <i class="FIRSTTERM">frontends</i>, or
interfaces, and the <i class="FIRSTTERM">backend</i>.
The backend should contain all the ``hard parts'': your
algorithms and data structures, the real work done by
the application. Think of it as an abstract ``model''
being displayed to and manipulated by the user.
</p>
<p>
Each frontend should be a ``view'' and a
``controller.'' As a ``view,'' the frontend must note
any changes in the backend, and change the display
accordingly. As a ``controller,'' the frontend must
allow the user to relay requests for change to the
backend (it defines how manipulations of the frontend
translate into changes in the model).
</p>
<p>
There are many ways to discipline yourself to keep your
application separated. A couple of useful ideas:
</p>
<ul>
<li>
<p>
Write the backend as a library; if this becomes
undesirable for any reason, you can always
statically link.
</p>
</li>
<li>
<p>
Write at least two frontends from the start; one or
both can be ugly prototypes, you just want to get
an idea how to structure the backend. Remember,
frontends should be easy; the backend has the hard
parts.
</p>
</li>
</ul>
<p>
If one of your frontends is Gnome- or GTK+- based, an
excellent choice for the other is an interactive Guile
terminal. Your non-expert end users probably won't use
it, but it's a great debugging tool; you can prototype
and test the backend using easy-to-write Guile
bindings, and add the graphical controls only when
things are working. When you're done, you'll have a
scriptable application almost for free.
</p>
<p>
If your application can potentially be run in batch
mode, command line and web interfaces are also
relatively easy to write, useful for debugging, and
will keep you disciplined.
</p>
<p>
Finally, if your project is large enough to justify the
bother and complexity, consider using a cross-platform
frontend layer to share code between GUI frontends on
different platforms. This approach is taken by Mozilla
(<a href="http://www.mozilla.org" target=
"_top">http://www.mozilla.org</a>), and the AbiSource
office suite (<a href="http://www.abisource.com"
target="_top">http://www.abisource.com</a>). It might
be interesting to have a look at their code.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z841">I don't like the default appearance of
[some widget]. How do I change its appearance?</a>
</h2>
<p>
Don't program your preferences. GTK+ unfortunately has
all sorts of look and feel settings that the programmer
can affect. For example, you can change the appearance
of the ``expanders'' in a <tt class="CLASSNAME">
GtkCTree</tt>---they can be triangles, squares, or
circles. By default they are squares. You change them
by calling <tt class="FUNCTION">
gtk_ctree_set_expander_style()</tt>.
</p>
<p>
There's no good reason to call this function in an
application. Ever. Think about why you would call
it---because you happen to like that expander style
better. It's a purely cosmetic issue. However, if you
do call it, you've just made your application's look
and feel different from that of every other
application. This is <i class="EMPHASIS">harmful</i>,
because it confuses users and even gives them a sense
that your application is ``unprofessional'' or ``not
quite right.''
</p>
<p>
``But I want my favorite expanders!,'' you might whine.
Don't despair. There is a correct way to handle this
situation. Variable aspects of look and feel should be
configurable <i class="EMPHASIS">at runtime</i> by <i
class="EMPHASIS">users</i>. What's more, it should be
configurable <i class="EMPHASIS">globally</i>, for <i
class="EMPHASIS">all applications at once</i>. GTK+
provides themes for precisely this purpose.
</p>
<p>
Unfortunately themes do not yet cover all aspects of
look and feel, and so the temptation remains to
hard-code these in your application. You must resist.
If you are dead-set against the default expander style,
or the default dialog position, or whatever, then do
the work to make it configurable on the library level
and submit that code to the GTK+ or Gnome maintainers.
</p>
<p>
You have to do this on the library level---think about
it. If you provide an application-specific way to
configure look and feel, nothing has really been
gained; if someone does like a particular expander
style, they have to go through each program deciding if
and how the style can be changed. Some programs will
invariably be ``stuck'' with the default, since the
authors of those programs didn't make it configurable.
The resulting mess is very annoying to users.
</p>
<p>
Gnome already has solutions for a number of common
cases. For example, GTK+ lets you pop up a dialog at
the mouse pointer, in the center of the screen, or
wherever the window manager wants; there is no reason
you should pick your favorite and use it in your
application. Thus <tt class="CLASSNAME">
GnomeDialog</tt> loads a user preference for the
dialog's initial position. This preference can be set
from the Gnome control center.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z842">Thanks for the lecture, but I have a
really good reason to change the appearance of a
widget. How do I override the theme?</a>
</h2>
<p>
write this
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z843">Why are signals specified as strings
rather than integers or some sort of macro?</a>
</h2>
<p>
Strings are nicer. They are easier to type and less
headache for <span class="STRUCTNAME">GtkObject</span>
authors to maintain. They don't clutter the C
namespace. Typing a string incorrectly will trigger a
runtime error so macros don't improve error checking.
Finally, strings are internally converted to a numeric
ID so there is no loss in efficiency.
</p>
<p>
Consider the maintenance headache of using enumerations
instead: both enumeration values and their names would
have to be unique across GTK+, Gnome, and third-party
extensions. A nightmare.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z844">Why is GTK+ written in C?</a>
</h2>
<p>
First and foremost: asking this question in any public
forum is <i class="EMPHASIS">strongly discouraged</i>.
Don't do it. Check the archives for several extended
off-topic flamefests if you're interested.
</p>
<p>
Here are some reasons:
</p>
<ul>
<li>
<p>
The original authors wanted to write it in C, and
now many C-only applications are based on it. The
current authors enjoy C.
</p>
</li>
<li>
<p>
GTK+ handles types and objects much more flexibly
than C++; it is runtime-oriented, more like Java or
Objective C than C++ system. This is convenient for
GUI builders and language bindings.
</p>
</li>
<li>
<p>
C is the lingua franca of UNIX development; most
people know how to code in it.
</p>
</li>
<li>
<p>
There are already nice toolkits for languages such
as Java and Objective C. There are C++ wrappers for
GTK+; several, in fact.
</p>
</li>
<li>
<p>
C is more portable than C++; ANSI C++ is not yet
widely implemented, so only an ill-defined subset
of C++ can actually be used.
</p>
</li>
<li>
<p>
When GTK+ development first started, there was no
free, working C++ compiler.
</p>
</li>
</ul>
<p>
Again: do not ask this question on any mailing lists,
because people will not be amused.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z845">My motion event handler is only invoked
once; why is that?</a>
</h2>
<p>
If you specify <span class="STRUCTNAME">
GDK_POINTER_MOTION_HINT_MASK</span>, you must call <tt
class="FUNCTION">gdk_window_get_pointer()</tt> to get
more motion events. One motion event is sent each time
you get the pointer location. See <a href=
"sec-gdkevent.html#SEC-MOVEMENTEVENTS">the section
called <i>Mouse Movement Events</i> in the chapter
called <i>GDK Basics</i></a>.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z846">Can I move the mouse pointer myself?</a>
</h2>
<p>
There is an Xlib routine called <tt class="FUNCTION">
XWarpPointer()</tt> that does this, but GDK does not
wrap it. It is almost certainly a bad idea to use this
feature (in fact it is intended for window managers
only); you might consider writing to one of the GTK+ or
Gnome mailing lists to ask for another way to achieve
whatever you are trying to achieve. However, you can
always use Xlib routines (such as <tt class="FUNCTION">
XWarpPointer()</tt>) by including <tt class="FILENAME">
gdk/gdkx.h</tt> and <tt class="FILENAME">
gdk/gdkprivate.h</tt>, then manipulating the private
parts of the GDK data structures. If that sounds
unsavory, it probably should.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z847">How do I read the pixels out of a <span
class="STRUCTNAME">GdkPixmap</span>?</a>
</h2>
<p>
First and foremost: remember that a pixmap is a
server-side resource, i.e. possibly across a network
and <i class="EMPHASIS">definitely</i> across some kind
of socket. Therefore, you do not want to request its
pixels one by one. Iterating over a pixmap that way
could easily take many seconds.
</p>
<p>
GDK wraps an Xlib object called <span class=
"STRUCTNAME">XImage</span>. The wrapper is called <span
class="STRUCTNAME">GdkImage</span>. A <span class=
"STRUCTNAME">GdkImage</span> is essentially a local
copy of the data in a pixmap. You can copy a region of
a pixmap or window into a <span class="STRUCTNAME">
GdkImage</span> with the <tt class="FUNCTION">
gdk_image_get()</tt> routine, then get and set pixels
with <tt class="FUNCTION">gdk_image_get_pixel()</tt>
and <tt class="FUNCTION">gdk_image_put_pixel()</tt>.
You can also access the image's data structures
directly, but this is quite complicated (due to
visuals, depths, differences between host and network
byte order, and so on). If you modify the image, you
use <tt class="FUNCTION">gdk_draw_image()</tt> to copy
it back to a server-side drawable.
</p>
<p>
Copying a pixmap to a <span class="STRUCTNAME">
GdkImage</span>, or copying a <span class="STRUCTNAME">
GdkImage</span> to a pixmap, still involves moving
quite a bit of data over the network; however, since
it's all in one burst the speed can be tolerable in
many cases. Also, if the client and the server are on
the same machine, and the X shared memory extension is
available, GDK will automatikcally set up a shared
memory segment to copy the data.
</p>
<p>
Most of the time, if you plan to do a lot of image
manipulation, you are better off using RGB buffers as
your primary data structure (see <a href=
"z132.html#SEC-GDKRGB">the section called <i>RGB
Buffers</i> in the chapter called <i>GDK
Basics</i></a>). The functions in <tt class="FILENAME">
gdk/gdkrgb.h</tt> allow you to copy an RGB buffer to a
drawable. These functions use <span class="STRUCTNAME">
GdkImage</span> internally, but they are tuned to be
very fast and handle all the complexities for you.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z848">I'm drawing a lot of points to the
screen with <tt class="FUNCTION">gdk_draw_point()</tt>,
and it's unbelievably slow. What's wrong? How can I
render image data to the screen?</a>
</h2>
<p>
See the previous question. You should probably use the
GDK RGB functions (<a href="z132.html#SEC-GDKRGB">the
section called <i>RGB Buffers</i> in the chapter called
<i>GDK Basics</i></a>).
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z849">I'm trying to set the background of a
<tt class="CLASSNAME">GtkLabel</tt>, and it doesn't
work.</a>
</h2>
<p>
<tt class="CLASSNAME">GtkLabel</tt> is a windowless
widget; it is "transparent" and draws on its parent
container's background. If you want to set the
background, place the label in a <tt class="CLASSNAME">
GtkEventBox</tt>. The same answer applies to other
windowless widgets, such as <tt class="CLASSNAME">
GtkImage</tt>.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z850">In the GTK+ and Gnome source code, many
functions have two variants: one called <tt class=
"FUNCTION">gtk_whatever_foo()</tt>, and another called
<tt class="FUNCTION">gtk_whatever_real_foo()</tt>.
What's the difference?</a>
</h2>
<p>
<tt class="FUNCTION">gtk_whatever_foo()</tt> is
typically a public function which emits the <span
class="SYMBOL">"foo"</span> signal, taking care of any
necessary details before and after emission (remember
that only <span class="STRUCTNAME">
GTK_RUN_ACTION</span> signals can be emitted without
special actions before and after). <tt class=
"FUNCTION">gtk_whatever_real_foo()</tt> will be the
default handler for the signal, installed in the
object's class struct. <a href="cha-widget.html">the
chapter called <i>Writing a <tt class="CLASSNAME">
GtkWidget</tt></i></a> has many examples of this.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z851">How do I "gray out" a widget, so the
user can't select it?</a>
</h2>
<p>
See <a href="z57.html#SEC-SENSITIVITY">the section
called <i>Sensitivity</i> in the chapter called <i>GTK+
Basics</i></a>. Short answer:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
gtk_widget_set_sensitive(widget, FALSE);
</pre>
</td>
</tr>
</table>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z852">I'm connecting to <span class="SYMBOL">
"button_press_event"</span> or some other event signal,
but the callback is never invoked.</a>
</h2>
<p>
There are several possibilities:
</p>
<ul>
<li>
<p>
The widget has no <span class="STRUCTNAME">
GdkWindow</span> (i.e. the <span class=
"STRUCTNAME">GTK_NO_WINDOW</span> flag is set), so
it does not receive events (other than synthesized
expose events).
</p>
</li>
<li>
<p>
The event you're trying to monitor isn't in the
event mask for the widget's <span class=
"STRUCTNAME">GdkWindow</span>. Use <tt class=
"FUNCTION">gtk_widget_add_events()</tt> to add more
events to the mask.
</p>
</li>
<li>
<p>
The widget is a container, and some child widget is
"handling" the event by returning <span class=
"STRUCTNAME">TRUE</span> from the event signal
emission. Only "unhandled" events are propagated
from child to parent.
</p>
</li>
</ul>
<p>
See <a href="sec-gdkevent.html#SEC-GTKEVENTS">the
section called <i>Receiving GDK Events in GTK+</i> in
the chapter called <i>GDK Basics</i></a> for more
details on events and how they are passed to widgets.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z853">I want to use the arrow keys as a
control in my application, but GTK+ keeps stealing the
key press events to move the focus around.</a>
</h2>
<p>
Key press handling is somewhat complex. You might want
to read <a href="sec-gdkevent.html#SEC-GDKKEYFOCUS">the
section called <i>Keyboard Focus</i> in the chapter
called <i>GDK Basics</i></a> and <a href=
"z57.html#SEC-FOCUSWIDGET">the section called <i>
Focus</i> in the chapter called <i>GTK+ Basics</i></a>
for a brief overview. <a href=
"sec-gdkevent.html#SEC-GTKEVENTS">the section called
<i>Receiving GDK Events in GTK+</i> in the chapter
called <i>GDK Basics</i></a> is also relevant.
</p>
<p>
In short, key events are initially received by a
toplevel <tt class="CLASSNAME">GtkWindow</tt>. GTK+'s
key event behavior is more or less defined by default
key press event handler in <tt class="FILENAME">
gtkwindow.c</tt> (looking at this function is
instructive). It works as follows:
</p>
<ul>
<li>
<p>
If there's a focus widget, the key event signal is
emitted on the focus widget. If this emission
returns <span class="STRUCTNAME">TRUE</span>, as
described in <a href=
"sec-gdkevent.html#SEC-GTKEVENTS">the section
called <i>Receiving GDK Events in GTK+</i> in the
chapter called <i>GDK Basics</i></a>, processing
stops.
</p>
</li>
<li>
<p>
If any of the accelerator groups attached to the
window contain an accelerator matching the event,
then processing stops.
</p>
</li>
<li>
<p>
If the key event hasn't been handled yet, there are
some default bindings; the arrow keys move the
focus around, for example.
</p>
</li>
</ul>
<p>
Thus, to override the arrow key behavior, you can
return <span class="STRUCTNAME">TRUE</span> from the
focus widget's signal emission, install an accelerator
for the arrow keys, or connect to <span class="SYMBOL">
"key_press_event"</span> on the toplevel window and use
<tt class="FUNCTION">
gtk_signal_emit_stop_by_name()</tt> to end the signal
emission before the <tt class="CLASSNAME">
GtkWindow</tt> default handler runs.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z854">Does GTK+ have multiple inheritance?</a>
</h2>
<p>
No, but "interfaces" (in Java terms) or "pure virtual
classes" (in C++ terms) are planned for the next
version. See <a href="z144.html#SEC-OVERRIDESIGNALS">
the section called <i>Overridable Signals</i> in the
chapter called <i>Writing a <tt class="CLASSNAME">
GtkWidget</tt></i></a> for a discussion of an ugly
workaround used in <tt class="CLASSNAME">GtkWidget</tt>
to create "activatable" and "scrollable" interfaces.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z855">I'm getting error messages from GDK. How
can I determine the cause of these?</a>
</h2>
<p>
First, run your program with the <tt class=
"APPLICATION">--sync</tt> option. This invokes <tt
class="FUNCTION">XSynchronize()</tt> to turn off event
buffering; it slows down the application, but causes
errors to be reported as soon as they occur.
Alternatively, some Xlib implementations let you turn
on synchronization by setting the global variable <span
class="STRUCTNAME">_Xdebug</span> to <span class=
"STRUCTNAME">TRUE</span> in a debugger.
</p>
<p>
Once errors are being reported synchronously, just run
your app in a debugger and wait for <tt class=
"FUNCTION">abort()</tt> to be called. For warnings, set
a breakpoint at <tt class="FUNCTION">g_logv()</tt>
which is the function called by the <tt class=
"FUNCTION">g_warning()</tt> macro.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z856">How do I update the GUI without
returning control to the main loop?</a>
</h2>
<p>
Just do this:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
while (gtk_events_pending())
gtk_main_iteration();
</pre>
</td>
</tr>
</table>
<p>
This code will handle all pending events, then return
control to you. You can also run nested instances of
<tt class="FUNCTION">gtk_main()</tt>; each call to <tt
class="FUNCTION">gtk_main_quit()</tt> exits one
instance. <tt class="FUNCTION">gnome_dialog_run()</tt>
uses this technique to block waiting for user input.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z857">How should I format code to be included
in GTK+ or Gnome?</a>
</h2>
<p>
The GTK+ coding style is basically the GNU coding style
(<a href="http://www.gnu.org/prep/standards_toc.html"
target=
"_top">http://www.gnu.org/prep/standards_toc.html</a>).
The Gnome libraries are less consistent, but lean
toward the Linux kernel coding style (documented in <tt
class="FILENAME">
/usr/src/linux/Documentation/CodingStyle</tt> on many
Linux systems).
</p>
<p>
The GTK+ style uses two-space indentation, puts all
braces on a new line, and leaves one space between
identifiers and opening parentheses, like this:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
if (whatever)
{
foo (arg1, arg2);
}
</pre>
</td>
</tr>
</table>
<p>
Emacs uses this style by default.
</p>
<p>
The Gnome style uses eight-space indentation and
Kernighan and Ritchie braces, like so:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
if (whatever) {
foo (arg1, arg2);
}
</pre>
</td>
</tr>
</table>
<p>
It also leaves a space between identifiers and opening
parentheses. To make Emacs use the Gnome style, add a
line like this to the top of your source files:
</p>
<table border="0" bgcolor="#E0E0E0" width="100%">
<tr>
<td>
<pre class="PROGRAMLISTING">
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
</pre>
</td>
</tr>
</table>
<p>
When preparing a patch for any piece of free software,
it's polite the style of the preexisting code. It's
customary to include a file called <tt class=
"FILENAME">HACKING</tt> in source code distributions
addressing this and similar issues; read it if it
exists.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z858">Is there a GUI builder for GTK+ and
Gnome?</a>
</h2>
<p>
A very promising GUI builder called Glade is being
developed. Glade can generate source code in several
languages, or an XML description of your widgets. An
add-on module called <tt class="APPLICATION">
libglade</tt> loads these XML descriptions at runtime
and creates the described widgets. The next release of
the Gnome libraries will very likely include or require
<tt class="APPLICATION">libglade</tt>.
</p>
</div>
<div class="SECT2">
<h2 class="SECT2">
<a name="Z859">How well do GTK+ and Gnome support
internationalization?</a>
</h2>
<p>
GTK+ 1.2 supports most European and Asian languages.
GDK contains an API for loading fontsets and rendering
multibyte strings, though this book does not cover it.
The stock GTK+ widgets that handle text use this API
and will deal with multibyte strings correctly. GTK+
also supports input methods for Asian languages. GTK+
1.2 does <i class="EMPHASIS">not</i> support
right-to-left scripts, or scripts that require complex
ligatures and unusual line breaks. However, support for
these languages is a high priority for GTK+ 1.4. For
details on future plans, Owen Taylor's white paper at
<a href=
"http://www.gnome.org/white-papers/i18n/gtki18n/"
target="_top">
http://www.gnome.org/white-papers/i18n/gtki18n/</a> is
an excellent resource.
</p>
<p>
Both GTK+ and Gnome use the <tt class="APPLICATION">
gettext</tt> message catalog system to translate
user-visible strings, so any string the toolkit knows
how to render can be translated into foreign languages.
<a href="sec-i18n.html">the section called <i>
Internationalization</i> in the chapter called <i>Gnome
Application Basics</i></a> covers this topic.
</p>
</div>
</div>
</div>
<div class="NAVFOOTER">
<br>
<br>
<table width="100%" border="0" bgcolor="#ffffff" cellpadding=
"1" cellspacing="0">
<tr>
<td width="25%" bgcolor="#ffffff" align="left">
<a href="headers.html"><font color="#0000ff" size="2">
<b><<< Previous</b></font></a>
</td>
<td width="25%" colspan="2" bgcolor="#ffffff" align=
"center">
<font color="#0000ff" size="2"><b><a href="ggad.html">
<font color="#0000ff" size="2"><b>
Home</b></font></a></b></font>
</td>
<td width="25%" bgcolor="#ffffff" align="right">
<a href="online.html"><font color="#0000ff" size="2">
<b>Next >>></b></font></a>
</td>
</tr>
<tr>
<td colspan="2" align="left">
<font color="#000000" size="2"><b>Table of Header
Files</b></font>
</td>
<td colspan="2" align="right">
<font color="#000000" size="2"><b>Online
Resources</b></font>
</td>
</tr>
</table>
</div>
</body>
</html>
|