?? gtk_tut-10.html
字號(hào):
<!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: Container Widgets</TITLE>
<LINK HREF="gtk_tut-11.html" REL=next>
<LINK HREF="gtk_tut-9.html" REL=previous>
<LINK HREF="gtk_tut.html#toc10" REL=contents>
</HEAD>
<BODY TEXT="#CCCCCC" BGCOLOR="#000000" LINK="#33cc00" VLINK="#009900" ALINK="#FF0000">
<A HREF="gtk_tut-11.html">Next</A>
<A HREF="gtk_tut-9.html">Previous</A>
<A HREF="gtk_tut.html#toc10">Contents</A>
<HR>
<H2><A NAME="s10">10. Container Widgets</A> </H2>
<H2><A NAME="sec_EventBox"></A> <A NAME="ss10.1">10.1 The EventBox </A>
</H2>
<P>
Some GTK widgets don't have associated X windows, so they just draw on
their parents. Because of this, they cannot receive events and if they
are incorrectly sized, they don't clip so you can get messy
overwriting, etc. If you require more from these widgets, the EventBox
is for you.
<P>At first glance, the EventBox widget might appear to be totally
useless. It draws nothing on the screen and responds to no
events. However, it does serve a function - it provides an X window
for its child widget. This is important as many GTK widgets do not
have an associated X window. Not having an X window saves memory and
improves performance, but also has some drawbacks. A widget without an
X window cannot receive events, and does not perform any clipping on
its contents. Although the name <EM>EventBox</EM> emphasizes the
event-handling function, the widget can also be used for clipping.
(and more, see the example below).
<P>To create a new EventBox widget, use:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_event_box_new( void );
</PRE>
</CODE></BLOCKQUOTE>
<P>A child widget can then be added to this EventBox:
<P>
<BLOCKQUOTE><CODE>
<PRE>
gtk_container_add( GTK_CONTAINER(event_box), child_widget );
</PRE>
</CODE></BLOCKQUOTE>
<P>The following example demonstrates both uses of an EventBox - a label
is created that is clipped to a small box, and set up so that a
mouse-click on the label causes the program to exit. Resizing the
window reveals varying amounts of the label.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start eventbox eventbox.c */
#include <gtk/gtk.h>
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *event_box;
GtkWidget *label;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Event Box");
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
/* Create an EventBox and add it to our toplevel window */
event_box = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(window), event_box);
gtk_widget_show (event_box);
/* Create a long label */
label = gtk_label_new ("Click here to quit, quit, quit, quit, quit");
gtk_container_add (GTK_CONTAINER (event_box), label);
gtk_widget_show (label);
/* Clip it short. */
gtk_widget_set_usize (label, 110, 20);
/* And bind an action to it */
gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
gtk_signal_connect (GTK_OBJECT(event_box), "button_press_event",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
/* Yet one more thing you need an X window for ... */
gtk_widget_realize (event_box);
gdk_window_set_cursor (event_box->window, gdk_cursor_new (GDK_HAND1));
gtk_widget_show (window);
gtk_main ();
return(0);
}
/* example-end */
</PRE>
</CODE></BLOCKQUOTE>
<P>
<H2><A NAME="sec_Alignment"></A> <A NAME="ss10.2">10.2 The Alignment widget </A>
</H2>
<P>The alignment widget allows you to place a widget within its window at
a position and size relative to the size of the Alignment widget
itself. For example, it can be very useful for centering a widget
within the window.
<P>There are only two functions associated with the Alignment widget:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget* gtk_alignment_new( gfloat xalign,
gfloat yalign,
gfloat xscale,
gfloat yscale );
void gtk_alignment_set( GtkAlignment *alignment,
gfloat xalign,
gfloat yalign,
gfloat xscale,
gfloat yscale );
</PRE>
</CODE></BLOCKQUOTE>
<P>The first function creates a new Alignment widget with the specified
parameters. The second function allows the alignment paramters of an
exisiting Alignment widget to be altered.
<P>All four alignment parameters are floating point numbers which can
range from 0.0 to 1.0. The <CODE>xalign</CODE> and <CODE>yalign</CODE> arguments
affect the position of the widget placed within the Alignment
widget. The <CODE>xscale</CODE> and <CODE>yscale</CODE> arguments effect the amount of
space allocated to the widget.
<P>A child widget can be added to this Alignment widget using:
<P>
<BLOCKQUOTE><CODE>
<PRE>
gtk_container_add( GTK_CONTAINER(alignment), child_widget );
</PRE>
</CODE></BLOCKQUOTE>
<P>For an example of using an Alignment widget, refer to the example for
the
<A HREF="gtk_tut-9.html#sec_ProgressBar">Progress Bar</A> widget.
<P>
<H2><A NAME="ss10.3">10.3 Fixed Container</A>
</H2>
<P>The Fixed container allows you to place widgets at a fixed position
within it's window, relative to it's upper left hand corner. The
position of the widgets can be changed dynamically.
<P>There are only three functions associated with the fixed widget:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget* gtk_fixed_new( void );
void gtk_fixed_put( GtkFixed *fixed,
GtkWidget *widget,
gint16 x,
gint16 y );
void gtk_fixed_move( GtkFixed *fixed,
GtkWidget *widget,
gint16 x,
gint16 y );
</PRE>
</CODE></BLOCKQUOTE>
<P>The function <CODE>gtk_fixed_new</CODE> allows you to create a new Fixed
container.
<P><CODE>gtk_fixed_put</CODE> places <CODE>widget</CODE> in the container <CODE>fixed</CODE> at
the position specified by <CODE>x</CODE> and <CODE>y</CODE>.
<P><CODE>gtk_fixed_move</CODE> allows the specified widget to be moved to a new
position.
<P>The following example illustrates how to use the Fixed Container.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start fixed fixed.c */
#include <gtk/gtk.h>
/* I'm going to be lazy and use some global variables to
* store the position of the widget within the fixed
* container */
gint x=50;
gint y=50;
/* This callback function moves the button to a new position
* in the Fixed container. */
void move_button( GtkWidget *widget,
GtkWidget *fixed )
{
x = (x+30)%300;
y = (y+50)%300;
gtk_fixed_move( GTK_FIXED(fixed), widget, x, y);
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *fixed;
GtkWidget *button;
gint i;
/* Initialise GTK */
gtk_init(&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Fixed Container");
/* Here we connect the "destroy" event to a signal handler */
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 Fixed Container */
fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), fixed);
gtk_widget_show(fixed);
for (i = 1 ; i <= 3 ; i++) {
/* Creates a new button with the label "Press me" */
button = gtk_button_new_with_label ("Press me");
/* When the button receives the "clicked" signal, it will call the
* function move_button() passing it the Fixed Container as its
* argument. */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (move_button), fixed);
/* This packs the button into the fixed containers window. */
gtk_fixed_put (GTK_FIXED (fixed), button, i*50, i*50);
/* The final step is to display this newly created widget. */
gtk_widget_show (button);
}
/* Display the window */
gtk_widget_show (window);
/* Enter the event loop */
gtk_main ();
return(0);
}
/* example-end */
</PRE>
</CODE></BLOCKQUOTE>
<P>
<H2><A NAME="ss10.4">10.4 Layout Container</A>
</H2>
<P>The Layout container is similar to the Fixed container except that it
implements an infinite (where infinity is less than 2^32) scrolling
area. The X window system has a limitation where windows can be at
most 32767 pixels wide or tall. The Layout container gets around this
limitation by doing some exotic stuff using window and bit gravities,
so that you can have smooth scrolling even when you have many child
widgets in your scrolling area.
<P>A Layout container is created using:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_layout_new( GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment );
</PRE>
</CODE></BLOCKQUOTE>
<P>As you can see, you can optionally specify the Adjustment objects that
the Layout widget will use for its scrolling.
<P>You can add and move widgets in the Layout container using the
following two functions:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_layout_put( GtkLayout *layout,
GtkWidget *widget,
gint x,
gint y );
void gtk_layout_move( GtkLayout *layout,
GtkWidget *widget,
gint x,
gint y );
</PRE>
</CODE></BLOCKQUOTE>
<P>The size of the Layout container can be set using the next function:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_layout_set_size( GtkLayout *layout,
guint width,
guint height );
</PRE>
</CODE></BLOCKQUOTE>
<P>Layout containers are one of the very few widgets in the GTK widget
set that actively repaint themselves on screen as they are changed
using the above functions (the vast majority of widgets queue
requests which are then processed when control returns to the
<CODE>gtk_main()</CODE> function).
<P>When you want to make a large number of changes to a Layout container,
you can use the following two functions to disable and re-enable this
repainting functionality:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_layout_freeze( GtkLayout *layout );
void gtk_layout_thaw( GtkLayout *layout );
</PRE>
</CODE></BLOCKQUOTE>
<P>The final four functions for use with Layout widgets are for
manipulating the horizontal and vertical adjustment widgets:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkAdjustment* gtk_layout_get_hadjustment( GtkLayout *layout );
GtkAdjustment* gtk_layout_get_vadjustment( GtkLayout *layout );
void gtk_layout_set_hadjustment( GtkLayout *layout,
GtkAdjustment *adjustment );
void gtk_layout_set_vadjustment( GtkLayout *layout,
GtkAdjustment *adjustment);
</PRE>
</CODE></BLOCKQUOTE>
<P>
<H2><A NAME="sec_Frames"></A> <A NAME="ss10.5">10.5 Frames </A>
</H2>
<P>Frames can be used to enclose one or a group of widgets with a box
which can optionally be labelled. The position of the label and the
style of the box can be altered to suit.
<P>A Frame can be created with the following function:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_frame_new( const gchar *label );
</PRE>
</CODE></BLOCKQUOTE>
<P>The label is by default placed in the upper left hand corner of the
frame. A value of NULL for the <CODE>label</CODE> argument will result in no
label being displayed. The text of the label can be changed using the
next function.
<P>
<BLOCKQUOTE><CODE>
<PRE>
void gtk_frame_set_label( GtkFrame *frame,
const gchar *label );
</PRE>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -