⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk_tut-9.html

📁 GTK development guide
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.7">
 <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 TEXT="#CCCCCC" BGCOLOR="#000000" LINK="#33cc00" VLINK="#009900" ALINK="#FF0000">
<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>
<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 emit
no signals as they do not have an associated X window. If you need to
catch signals, or do clipping, place it inside a 
<A HREF="gtk_tut-10.html#sec_EventBox">EventBox</A> widget or a Button widget.
<P>To create a new label, use:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_label_new( char *str );
</PRE>
</CODE></BLOCKQUOTE>
<P>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>The first argument is the label you created previously (cast
using the <CODE>GTK_LABEL()</CODE> macro), and the second is the new string.
<P>The space needed for the new string will be automatically adjusted if
needed. You can produce multi-line labels by putting line breaks in
the 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>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 is
used 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:
<BLOCKQUOTE><CODE>
<PRE>
  GTK_JUSTIFY_LEFT
  GTK_JUSTIFY_RIGHT
  GTK_JUSTIFY_CENTER (the default)
  GTK_JUSTIFY_FILL
</PRE>
</CODE></BLOCKQUOTE>
<P>The label widget is also capable of line wrapping the text
automatically. This can be activated using:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_label_set_line_wrap (GtkLabel *label,
                              gboolean  wrap);
</PRE>
</CODE></BLOCKQUOTE>
<P>The <CODE>wrap</CODE> argument takes a TRUE or FALSE value.
<P>If you want your label underlined, then you can set a pattern on the
label:
<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. It
consists of a string of underscore and space characters. An underscore
indicates that the corresponding character in the label should be
underlined. For example, the string 
<PRE>
"__     __"
</PRE>
 would underline the
first two characters and eight and ninth characters.
<P>Below is a short example to illustrate these functions. This example
makes use of the Frame widget to better demonstrate the label
styles. 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 &lt;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(&amp;argc, &amp;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>
</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 possible
directions and having a number of possible styles. It can be very
useful when placed on a button in many applications. Like the Label
widget, it emits no signals.
<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 and
appearance. The second allows these values to be altered
retrospectively. The <CODE>arrow_type</CODE> argument may take one of the
following values:
<P>
<BLOCKQUOTE><CODE>
<PRE>
  GTK_ARROW_UP
  GTK_ARROW_DOWN
  GTK_ARROW_LEFT
  GTK_ARROW_RIGHT
</PRE>
</CODE></BLOCKQUOTE>
<P>These values obviously indicate the direction in which the arrow will
point. The <CODE>shadow_type</CODE> argument may take one of these values:
<P>
<BLOCKQUOTE><CODE>
<PRE>
  GTK_SHADOW_IN
  GTK_SHADOW_OUT (the default)
  GTK_SHADOW_ETCHED_IN
  GTK_SHADOW_ETCHED_OUT
</PRE>
</CODE></BLOCKQUOTE>
<P>Here's a brief example to illustrate their use.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start arrow arrow.c */

#include &lt;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 (&amp;argc, &amp;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 Object</A>
</H2>

<P>These are the little text strings that pop up when you leave your
pointer over a button or other widget for a few seconds. They are easy
to use, so I will just explain them without giving an example. If you
want to see some code, take a look at the testgtk.c program
distributed with GTK.
<P>Widgets that do not receive events (widgets that do not have their
own window) will not work with tooltips.
<P>The first call you will use creates a new tooltip. You only need to do
this once for a set of tooltips as the <CODE>GtkTooltips</CODE> object this
function 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 it
on, 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 );

⌨️ 快捷键说明

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