📄 gtk_tut-9.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>GTK v1.2 Tutorial: Miscellaneous Widgets</TITLE> <LINK HREF="gtk_tut-10.html" REL=next> <LINK HREF="gtk_tut-8.html" REL=previous> <LINK HREF="gtk_tut.html#toc9" REL=contents></HEAD><BODY BGCOLOR="#FFFFFF"><A HREF="gtk_tut-10.html">Next</A><A HREF="gtk_tut-8.html">Previous</A><A HREF="gtk_tut.html#toc9">Contents</A><HR NOSHADE><H2><A NAME="s9">9. Miscellaneous Widgets</A></H2><H2><A NAME="ss9.1">9.1 Labels</A></H2><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, use the <A HREF="gtk_tut-10.html#sec_EventBox">EventBox</A> widget.<P>To create a new label, use:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_label_new( char *str );</PRE></CODE></BLOCKQUOTE><P>Where the sole argument is the string you wish the label to display.<P>To change the label's text after creation, use the function:<P><BLOCKQUOTE><CODE><PRE>void gtk_label_set_text( GtkLabel *label, char *str );</PRE></CODE></BLOCKQUOTE><P>Where the first argument is the label you created previously (castusing the GTK_LABEL() macro), and the second is the new string.<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>To retrieve the current string, use:<P><BLOCKQUOTE><CODE><PRE>void gtk_label_get( GtkLabel *label, char **str );</PRE></CODE></BLOCKQUOTE><P>Where 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>The label text can be justified using:<P><BLOCKQUOTE><CODE><PRE>void gtk_label_set_justify( GtkLabel *label, GtkJustification jtype );</PRE></CODE></BLOCKQUOTE><P>Values for <CODE>jtype</CODE> are:<UL><LI> GTK_JUSTIFY_LEFT</LI><LI> GTK_JUSTIFY_RIGHT</LI><LI> GTK_JUSTIFY_CENTER (the default)</LI><LI> GTK_JUSTIFY_FILL</LI></UL><P>The label widget is also capable of line wrapping the textautomatically. This can be activated using:<P><BLOCKQUOTE><CODE><PRE>void gtk_label_set_line_wrap (GtkLabel *label, gboolean wrap);</PRE></CODE></BLOCKQUOTE><P>The <//wrap/ argument takes a TRUE or FALSE value.<P>If you want your label underlined, then you can set a pattern on thelabel:<P><BLOCKQUOTE><CODE><PRE>void gtk_label_set_pattern (GtkLabel *label, const gchar *pattern);</PRE></CODE></BLOCKQUOTE><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 <PRE>"__ __"</PRE> would underline thefirst two characters and eigth and ninth characters.<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 <A HREF="gtk_tut-10.html#sec_Frames">Frame</A> widget is explained later on.<P><BLOCKQUOTE><CODE><PRE>/* 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 seneance 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></CODE></BLOCKQUOTE><P><H2><A NAME="ss9.2">9.2 Arrows</A></H2><P>The Arrow widget draws an arrowhead, facing in a number of possibledirections and having a number of possible styles. It can be veryuseful when placed on a button in many applications.<P>There are only two functions for manipulating an Arrow widget:<P><BLOCKQUOTE><CODE><PRE>GtkWidget *gtk_arrow_new( GtkArrowType arrow_type, GtkShadowType shadow_type );void gtk_arrow_set( GtkArrow *arrow, GtkArrowType arrow_type, GtkShadowType shadow_type );</PRE></CODE></BLOCKQUOTE><P>The first creates a new arrow widget with the indicated type andappearance. The second allows these values to be alteredretrospectively. The <CODE>arrow_type</CODE> argument may take one of thefollowing values:<P><UL><LI> GTK_ARROW_UP</LI><LI> GTK_ARROW_DOWN</LI><LI> GTK_ARROW_LEFT</LI><LI> GTK_ARROW_RIGHT</LI></UL><P>These values obviously indicate the direction in which the arrow willpoint. The <CODE>shadow_type</CODE> argument may take one of these values:<P><UL><LI> GTK_SHADOW_IN</LI><LI> GTK_SHADOW_OUT (the default)</LI><LI> GTK_SHADOW_ETCHED_IN</LI><LI> GTK_SHADOW_ETCHED_OUT</LI></UL><P>Here's a brief example to illustrate their use.<P><BLOCKQUOTE><CODE><PRE>/* example-start arrow arrow.c */#include <gtk/gtk.h>/* Create an Arrow widget with the specified parameters * and pack it into a button */GtkWidget *create_arrow_button( GtkArrowType arrow_type, GtkShadowType shadow_type ){ GtkWidget *button; GtkWidget *arrow; button = gtk_button_new(); arrow = gtk_arrow_new (arrow_type, shadow_type); gtk_container_add (GTK_CONTAINER (button), arrow); gtk_widget_show(button); gtk_widget_show(arrow); return(button);}int main( int argc, char *argv[] ){ /* GtkWidget is the storage type for widgets */ GtkWidget *window; GtkWidget *button; GtkWidget *box; /* Initialize the toolkit */ gtk_init (&argc, &argv); /* Create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "Arrow Buttons"); /* It's a good idea to do this for all windows. */ gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (gtk_main_quit), NULL); /* Sets the border width of the window. */ gtk_container_set_border_width (GTK_CONTAINER (window), 10); /* Create a box to hold the arrows/buttons */ box = gtk_hbox_new (FALSE, 0); gtk_container_set_border_width (GTK_CONTAINER (box), 2); gtk_container_add (GTK_CONTAINER (window), box); /* Pack and show all our widgets */ gtk_widget_show(box); button = create_arrow_button(GTK_ARROW_UP, GTK_SHADOW_IN); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 3); button = create_arrow_button(GTK_ARROW_DOWN, GTK_SHADOW_OUT); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 3); button = create_arrow_button(GTK_ARROW_LEFT, GTK_SHADOW_ETCHED_IN); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 3); button = create_arrow_button(GTK_ARROW_RIGHT, GTK_SHADOW_ETCHED_OUT); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 3); gtk_widget_show (window); /* Rest in gtk_main and wait for the fun to begin! */ gtk_main (); return(0);}/* example-end */</PRE></CODE></BLOCKQUOTE><P><H2><A NAME="ss9.3">9.3 The Tooltips Widget</A></H2><P>These are the little text strings that pop up when you leave yourpointer over a button or other widget for a few seconds. They are easyto use, so I will just explain them without giving an example. If youwant to see some code, take a look at the testgtk.c programdistributed with GTK.<P>Widgets that do not receieve events (widgets that do not have theirown window) will not work with tooltips.<P>The first call you will use creates a new tooltip. You only need to dothis once for a set of tooltips as the <CODE>GtkTooltip</CODE> object thisfunction returns can be used to create multiple tooltips.<P><BLOCKQUOTE><CODE><PRE>GtkTooltips *gtk_tooltips_new( void );</PRE></CODE></BLOCKQUOTE><P>Once you have created a new tooltip, and the widget you wish to use iton, simply use this call to set it:<P><BLOCKQUOTE><CODE><PRE>void gtk_tooltips_set_tip( GtkTooltips *tooltips, GtkWidget *widget, const gchar *tip_text, const gchar *tip_private );</PRE></CODE></BLOCKQUOTE><P>The first argument is the tooltip you've already created, followed bythe widget you wish to have this tooltip pop up for, and the text youwish it to say. The last argument is a text string that can be used asan identifier when using GtkTipsQuery to implement context sensitivehelp. For now, you can set it to NULL.<P><P>Here's a short example:<P><BLOCKQUOTE><CODE><PRE>GtkTooltips *tooltips;GtkWidget *button;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -