gtk-question-index.html

来自「最新gtk中文资料集」· HTML 代码 · 共 1,276 行 · 第 1/4 页

HTML
1,276
字号
    GtkWidget *label2;    label1 = gtk_label_new (_("Another string to translate"));    label2 = gtk_label_new (_(global_variable));...</pre></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 theywill 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"><pre class="programlisting">  #define _(x) dgettext ("MyDomain", x)</pre></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="id3137091"></a><a name="id3137093"></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 type="disc"><li><p>ASCII characters are encoded by their familiar ASCII codepoints.</p></li><li><p>ASCII characters never appear as part of any other character.</p></li><li><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 iFAQ for Unix/Linux</a>.GLib provides functions for converting strings between UTF-8 and otherencodings, see <ahref="/usr/share/gtk-doc/html/glib/glib-Character-Set-Conversion.html#g-locale-to-utf8"><code class="function">g_locale_to_utf8()</code></a> and <ahref="/usr/share/gtk-doc/html/glib/glib-Character-Set-Conversion.html#g-convert"><code class="function">g_convert()</code></a>.</p><p>Text coming from external sources (e.g. files or user input), has to beconverted 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"><pre class="programlisting">gchar *text, *utf8_text;gsize length;GError *error = NULL;if (g_file_get_contents (filename, &amp;text, &amp;length, NULL))   {     utf8_text = g_convert (text, length, "UTF-8", "ISO-8859-1",                             NULL, NULL, &amp;error);     if (error != NULL)       {         fprintf ("Couldn't convert file %s to UTF-8\n", filename);         g_error_free (error);       }     else       g_print (utf8_text);  }else   fprintf (stderr, "Unable to read file %s\n", filename);</pre></div><p></p><p>For string literals in the source code, there are several alternatives forhandling non-ASCII content:</p><div class="variablelist"><table border="0"><col align="left" valign="top"><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 allowsyou 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 stringliterals in UTF-8 by using octal or hexadecimal escapes like <code class="literal">\212</code> or <code class="literal">\xa8</code> toencode each byte. This is portable, but modifying the escaped strings is notvery 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 toolchaincan handle (e.g. IS0-8859-1), you can write your source files in that encodingand use <ahref="/usr/share/gtk-doc/html/glib/glib-Character-Set-Conversion.html#g-convert"><code class="function">g_convert()</code></a> 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 &#169; which has Unicode and ISO-8859-1 codepoint 169 and is represented inUTF-8 by the two bytes 194, 169:</p><div class="informalexample"><pre class="programlisting">g_print ("direct UTF-8: &#169;");g_print ("escaped UTF-8: \302\251");text = g_convert ("runtime conversion: &#169;", -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL);g_print(text);g_free (text);</pre></div><p></p></td></tr><tr class="question"><td align="left" valign="top"><a name="id3137290"></a><a name="id3137292"></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 beconnected to signals, not methods. So you will need to use globalfunctions 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 followingcode:</p><div class="informalexample"><pre class="programlisting">  gdk_window_set_events (gdk_window,                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);</pre></div><p>while in C++ you must write:</p><div class="informalexample"><pre class="programlisting">  gdk_window_set_events (gdk_window,                          (GdkEventMask) GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);</pre></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="id3137352"></a><a name="id3137355"></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 languagebindings</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="id3137381"></a><a name="id3137383"></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"><code class="function">gtk_image_new_from_file()</code></a> <sup>[<a name="id3137402" href="#ftn.id3137402" class="footnote">1</a>]</sup>. To load an image for another purpose, use <ahref="/usr/share/gtk-doc/html/gdk-pixbuf/gdk-pixbuf-file-loading.html#gdk-pixbuf-new-from-file"><code class="function">gdk_pixbuf_new_from_file()</code></a>. To iload an animation, use <ahref="/usr/share/gtk-doc/html/gdk-pixbuf/gdk-pixbuf-animation.html#gdk-pixbuf-animation-new-from-file"><code class="function">gdk_pixbuf_animation_new_from_file()</code></a>.<ahref="/usr/share/gtk-doc/html/gdk-pixbuf/gdk-pixbuf-animation.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 <ahref="/usr/share/gtk-doc/html/gdk-pixbuf/gdk-pixbuf-animation.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 <ahref="/usr/share/gtk-doc/html/gdk-pixbuf/GdkPixbufLoader.html"><span class="type">GdkPixbufLoader</span></a>.</p></td></tr><tr class="question"><td align="left" valign="top"><a name="id3137499"></a><a name="id3137501"></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 <ahref="/usr/share/gtk-doc/html/gdk/gdk-Drawing-Primitives.html#gdk-draw-layout"><code class="function">gdk_draw_layout()</code></a>, using code like the following:</p><div class="informalexample"><pre class="programlisting"> layout = gtk_widget_create_pango_layout (widget, text); fontdesc = pango_font_description_from_string ("Luxi Mono 12"); pango_layout_set_font_description (layout, fontdesc);  gdk_draw_layout (..., layout); pango_font_description_free (fontdesc); g_object_unref (layout);</pre></div><p>Do not use the deprecated <ahref="/usr/share/gtk-doc/html/gdk/gdk-Fonts.html#GdkFont"><span class="type">GdkFont</span></a> and <ahref="/usr/share/gtk-doc/html/gdk/gdk-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="id3137570"></a><a name="id3137573"></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 <ahref="/usr/share/gtk-doc/html/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"><pre class="programlisting"> layout = gtk_widget_create_pango_layout (widget, text); fontdesc = pango_font_description_from_string ("Luxi Mono 12"); pango_layout_set_font_description (layout, fontdesc);  pango_layout_get_pixel_size (layout, &amp;width, &amp;height); pango_font_description_free (fontdesc); g_object_unref (layout);</pre></div><p>Do not use the deprecated function <ahref="/usr/share/gtk-doc/html/gdk/gdk-Fonts.html#gdk-text-width"><code class="function">gdk_text_width()</code></a>.</p><p>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?