📄 gtk_tut-10.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: 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 BGCOLOR="#FFFFFF"><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 NOSHADE><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 ontheir parents. Because of this, they cannot receive events and if theyare incorrectly sized, they don't clip so you can get messyoverwriting etc. If you require more from these widgets, the EventBoxis for you.<P>At first glance, the EventBox widget might appear to be totallyuseless. It draws nothing on the screen and responds to noevents. However, it does serve a function - it provides an X windowfor its child widget. This is important as many GTK widgets do nothave an associated X window. Not having an X window saves memory andimproves performance, but also has some drawbacks. A widget without anX window cannot receive events, and does not perform any clipping onits contents. Although the name <EM>EventBox</EM> emphasizes theevent-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 labelis created that is clipped to a small box, and set up so that amouse-click on the label causes the program to exit. Resizing thewindow 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 ata position and size relative to the size of the Alignment widgetitself. For example, it can be very useful for centering a widgetwithin 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 specifiedparameters. The second function allows the alignment paramters of anexisiting Alignment widget to be altered.<P>All four alignment parameters are floating point numbers which canrange from 0.0 to 1.0. The <CODE>xalign</CODE> and <CODE>yalign</CODE> argumentsaffect the position of the widget placed within the Alignmentwidget. The <CODE>xscale</CODE> and <CODE>yscale</CODE> arguments effect the amount ofspace 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 forthe <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 positionwithin it's window, relative to it's upper left hand corner. Theposition 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 Fixedcontainer.<P><CODE>gtk_fixed_put</CODE> places <CODE>widget</CODE> in the container <CODE>fixed</CODE> atthe 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 newposition.<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 Containter 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 itimplements an infinite (where infinity is less than 2^32) scrollingarea. Xwindows has a limitation where windows can be at most 32767pixels wide or tall. The Layout container gets around this limitationby doing some exotic stuff using window and bit gravities, so that youcan have smooth scrolling even when you have many child widgets inyour 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 thatthe Layout widget will use for it's scrolling.<P>You can add and move widgets in the Layout container using thefollowing 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 widgetset that actively repaint themselves on screen as they are changedusing the above functions (the vast majority of widgets queuerequests 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 thisrepainting 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 formanipulating 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 boxwhich can optionally be labelled. The position of the label and thestyle 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 theframe. A value of NULL for the <CODE>label</CODE> argument will result in nolabel being displayed. The text of the label can be changed using thenext function.<P><BLOCKQUOTE><CODE><PRE>void gtk_frame_set_label( GtkFrame *frame, const gchar *label );</PRE></CODE></BLOCKQUOTE><P>The position of the label can be changed using this function:<P><BLOCKQUOTE><CODE><PRE>void gtk_frame_set_label_align( GtkFrame *frame, gfloat xalign, gfloat yalign );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -