📄 ch-miscwidgets.html
字号:
<HTML><HEAD><TITLE>Miscellaneous Widgets</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="PREVIOUS"TITLE="Example"HREF="sec-rangewidgetsexample.html"><LINKREL="NEXT"TITLE="Arrows"HREF="sec-arrows.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-rangewidgetsexample.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-arrows.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH-MISCWIDGETS">Chapter 9. Miscellaneous Widgets</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT>9.1. <AHREF="ch-miscwidgets.html#SEC-LABELS">Labels</A></DT><DT>9.2. <AHREF="sec-arrows.html">Arrows</A></DT><DT>9.3. <AHREF="sec-thetooltipsobject.html">The Tooltips Object</A></DT><DT>9.4. <AHREF="sec-progressbars.html">Progress Bars</A></DT><DT>9.5. <AHREF="sec-dialogs.html">Dialogs</A></DT><DT>9.6. <AHREF="sec-pixmaps.html">Pixmaps</A></DT><DT>9.7. <AHREF="sec-rulers.html">Rulers</A></DT><DT>9.8. <AHREF="sec-statusbars.html">Statusbars</A></DT><DT>9.9. <AHREF="sec-textentries.html">Text Entries</A></DT><DT>9.10. <AHREF="sec-spinbuttons.html">Spin Buttons</A></DT><DT>9.11. <AHREF="sec-combobox.html">Combo Box</A></DT><DT>9.12. <AHREF="sec-calendar.html">Calendar</A></DT><DT>9.13. <AHREF="sec-colorselection.html">Color Selection</A></DT><DT>9.14. <AHREF="sec-fileselections.html">File Selections</A></DT></DL></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-LABELS">9.1. Labels</A></H1><P>Labels are used a lot in GTK, and are relatively simple. Labels emitno signals as they do not have an associated X window. If you need tocatch signals, or do clipping, place it inside a <AHREF="ch-containerwidgets.html#SEC-EVENTBOX">EventBox</A> widget or a Button widget.</P><P>To create a new label, use:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkWidget *gtk_label_new( char *str );</PRE></TD></TR></TABLE><P>The sole argument is the string you wish the label to display.</P><P>To change the label's text after creation, use the function:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_label_set_text( GtkLabel *label, char *str );</PRE></TD></TR></TABLE><P>The first argument is the label you created previously (castusing the <TTCLASS="LITERAL">GTK_LABEL()</TT> macro), and the second is the new string.</P><P>The space needed for the new string will be automatically adjusted ifneeded. You can produce multi-line labels by putting line breaks inthe label string.</P><P>To retrieve the current string, use:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_label_get( GtkLabel *label, char **str );</PRE></TD></TR></TABLE><P>The first argument is the label you've created, and the second,the return for the string. Do not free the return string, as it isused internally by GTK.</P><P>The label text can be justified using:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_label_set_justify( GtkLabel *label, GtkJustification jtype );</PRE></TD></TR></TABLE><P>Values for <TTCLASS="LITERAL">jtype</TT> are:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> GTK_JUSTIFY_LEFT GTK_JUSTIFY_RIGHT GTK_JUSTIFY_CENTER (the default) GTK_JUSTIFY_FILL</PRE></TD></TR></TABLE><P>The label widget is also capable of line wrapping the textautomatically. This can be activated using:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_label_set_line_wrap (GtkLabel *label, gboolean wrap);</PRE></TD></TR></TABLE><P>The <TTCLASS="LITERAL">wrap</TT> argument takes a TRUE or FALSE value.</P><P>If you want your label underlined, then you can set a pattern on thelabel:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_label_set_pattern (GtkLabel *label, const gchar *pattern);</PRE></TD></TR></TABLE><P>The pattern argument indicates how the underlining should look. Itconsists of a string of underscore and space characters. An underscoreindicates that the corresponding character in the label should beunderlined. For example, the string <TTCLASS="LITERAL">"__ __"</TT> would underline thefirst two characters and eight and ninth characters.</P><P>Below is a short example to illustrate these functions. This examplemakes use of the Frame widget to better demonstrate the labelstyles. You can ignore this for now as the <AHREF="sec-frames.html">Frame</A> widget is explained later on.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start label label.c */#include <gtk/gtk.h>int main( int argc, char *argv[] ){ static GtkWidget *window = NULL; GtkWidget *hbox; GtkWidget *vbox; GtkWidget *frame; GtkWidget *label; /* Initialise GTK */ gtk_init(&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC(gtk_main_quit), NULL); gtk_window_set_title (GTK_WINDOW (window), "Label"); vbox = gtk_vbox_new (FALSE, 5); hbox = gtk_hbox_new (FALSE, 5); gtk_container_add (GTK_CONTAINER (window), hbox); gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0); gtk_container_set_border_width (GTK_CONTAINER (window), 5); frame = gtk_frame_new ("Normal Label"); label = gtk_label_new ("This is a Normal label"); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); frame = gtk_frame_new ("Multi-line Label"); label = gtk_label_new ("This is a Multi-line label.\nSecond line\n" \ "Third line"); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); frame = gtk_frame_new ("Left Justified Label"); label = gtk_label_new ("This is a Left-Justified\n" \ "Multi-line label.\nThird line"); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); frame = gtk_frame_new ("Right Justified Label"); label = gtk_label_new ("This is a Right-Justified\nMulti-line label.\n" \ "Fourth line, (j/k)"); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); vbox = gtk_vbox_new (FALSE, 5); gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0); frame = gtk_frame_new ("Line wrapped label"); label = gtk_label_new ("This is an example of a line-wrapped label. It " \ "should not be taking up the entire " /* big space to test spacing */\ "width allocated to it, but automatically " \ "wraps the words to fit. " \ "The time has come, for all good men, to come to " \ "the aid of their party. " \ "The sixth sheik's six sheep's sick.\n" \ " It supports multiple paragraphs correctly, " \ "and correctly adds "\ "many extra spaces. "); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); frame = gtk_frame_new ("Filled, wrapped label"); label = gtk_label_new ("This is an example of a line-wrapped, filled label. " \ "It should be taking "\ "up the entire width allocated to it. " \ "Here is a sentence to prove "\ "my point. Here is another sentence. "\ "Here comes the sun, do de do de do.\n"\ " This is a new paragraph.\n"\ " This is another newer, longer, better " \ "paragraph. It is coming to an end, "\ "unfortunately."); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); frame = gtk_frame_new ("Underlined label"); label = gtk_label_new ("This label is underlined!\n" "This one is underlined in quite a funky fashion"); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT); gtk_label_set_pattern (GTK_LABEL (label), "_________________________ _ _________ _ ______ __ _______ ___"); gtk_container_add (GTK_CONTAINER (frame), label); gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0); gtk_widget_show_all (window); gtk_main (); return(0);}/* example-end */</PRE></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-rangewidgetsexample.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-arrows.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Example</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Arrows</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -