?? gtk_tut-6.html
字號:
<!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: The Button Widget</TITLE>
<LINK HREF="gtk_tut-7.html" REL=next>
<LINK HREF="gtk_tut-5.html" REL=previous>
<LINK HREF="gtk_tut.html#toc6" REL=contents>
</HEAD>
<BODY TEXT="#CCCCCC" BGCOLOR="#000000" LINK="#33cc00" VLINK="#009900" ALINK="#FF0000">
<A HREF="gtk_tut-7.html">Next</A>
<A HREF="gtk_tut-5.html">Previous</A>
<A HREF="gtk_tut.html#toc6">Contents</A>
<HR>
<H2><A NAME="s6">6. The Button Widget</A></H2>
<H2><A NAME="ss6.1">6.1 Normal Buttons</A>
</H2>
<P>We've almost seen all there is to see of the button widget. It's
pretty simple. There are however two ways to create a button. You can
use the gtk_button_new_with_label() to create a button with a label,
or use gtk_button_new() to create a blank button. It's then up to you
to pack a label or pixmap into this new button. To do this, create a
new box, and then pack your objects into this box using the usual
gtk_box_pack_start, and then use gtk_container_add to pack the box
into the button.
<P>Here's an example of using gtk_button_new to create a button with a
picture and a label in it. I've broken up the code to create a box
from the rest so you can use it in your programs. There are further
examples of using pixmaps later in the tutorial.
<P>
<BLOCKQUOTE><CODE>
<PRE>
/* example-start buttons buttons.c */
#include <gtk/gtk.h>
/* Create a new hbox with an image and a label packed into it
* and return the box. */
GtkWidget *xpm_label_box( GtkWidget *parent,
gchar *xpm_filename,
gchar *label_text )
{
GtkWidget *box1;
GtkWidget *label;
GtkWidget *pixmapwid;
GdkPixmap *pixmap;
GdkBitmap *mask;
GtkStyle *style;
/* Create box for xpm and label */
box1 = gtk_hbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (box1), 2);
/* Get the style of the button to get the
* background color. */
style = gtk_widget_get_style(parent);
/* Now on to the xpm stuff */
pixmap = gdk_pixmap_create_from_xpm (parent->window, &mask,
&style->bg[GTK_STATE_NORMAL],
xpm_filename);
pixmapwid = gtk_pixmap_new (pixmap, mask);
/* Create a label for the button */
label = gtk_label_new (label_text);
/* Pack the pixmap and label into the box */
gtk_box_pack_start (GTK_BOX (box1),
pixmapwid, FALSE, FALSE, 3);
gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);
gtk_widget_show(pixmapwid);
gtk_widget_show(label);
return(box1);
}
/* Our usual callback function */
void callback( GtkWidget *widget,
gpointer data )
{
g_print ("Hello again - %s was pressed\n", (char *) data);
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *button;
GtkWidget *box1;
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
/* It's a good idea to do this for all windows. */
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (gtk_exit), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_realize(window);
/* Create a new button */
button = gtk_button_new ();
/* Connect the "clicked" signal of the button to our callback */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");
/* This calls our box creating function */
box1 = xpm_label_box(window, "info.xpm", "cool button");
/* Pack and show all our widgets */
gtk_widget_show(box1);
gtk_container_add (GTK_CONTAINER (button), box1);
gtk_widget_show(button);
gtk_container_add (GTK_CONTAINER (window), button);
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>The xpm_label_box function could be used to pack xpm's and labels into
any widget that can be a container.
<P>Notice in <CODE>xpm_label_box</CODE> how there is a call to
<CODE>gtk_widget_get_style</CODE>. Every widget has a "style", consisting of
foreground and background colors for a variety of situations, font
selection, and other graphics data relevant to a widget. These style
values are defaulted in each widget, and are required by many GDK
function calls, such as <CODE>gdk_pixmap_create_from_xpm</CODE>, which here is
given the "normal" background color. The style data of widgets may
be customized, using
<A HREF="gtk_tut-21.html#sec_gtkrc_files">GTK's rc files</A>.
<P>Also notice the call to <CODE>gtk_widget_realize</CODE> after setting the
window's border width. This function uses GDK to create the X
windows related to the widget. The function is automatically called
when you invoke <CODE>gtk_widget_show</CODE> for a widget, and so has not been
shown in earlier examples. But the call to
<CODE>gdk_pixmap_create_from_xpm</CODE> requires that its <CODE>window</CODE> argument
refer to a real X window, so it is necessary to realize the widget
before this GDK call.
<P>The Button widget has the following signals:
<P>
<UL>
<LI><CODE>pressed</CODE> - emitted when pointer button is pressed within
Button widget</LI>
<LI><CODE>released</CODE> - emitted when pointer button is released within
Button widget</LI>
<LI><CODE>clicked</CODE> - emitted when pointer button is pressed and then
released within Button widget</LI>
<LI><CODE>enter</CODE> - emitted when pointer enters Button widget</LI>
<LI><CODE>leave</CODE> - emitted when pointer leaves Button widget</LI>
</UL>
<P>
<H2><A NAME="ss6.2">6.2 Toggle Buttons</A>
</H2>
<P>Toggle buttons are derived from normal buttons and are very similar,
except they will always be in one of two states, alternated by a
click. They may be depressed, and when you click again, they will pop
back up. Click again, and they will pop back down.
<P>Toggle buttons are the basis for check buttons and radio buttons, as
such, many of the calls used for toggle buttons are inherited by radio
and check buttons. I will point these out when we come to them.
<P>Creating a new toggle button:
<P>
<BLOCKQUOTE><CODE>
<PRE>
GtkWidget *gtk_toggle_button_new( void );
GtkWidget *gtk_toggle_button_new_with_label( gchar *label );
</PRE>
</CODE></BLOCKQUOTE>
<P>As you can imagine, these work identically to the normal button widget
calls. The first creates a blank toggle button, and the second, a
button with a label widget already packed into it.
<P>To retrieve the state of the toggle widget, including radio and check
buttons, we use a construct as shown in our example below. This tests
the state of the toggle, by accessing the <CODE>active</CODE> field of the
toggle widget's structure, after first using the
<CODE>GTK_TOGGLE_BUTTON</CODE> macro to cast the widget pointer into a toggle
widget pointer. The signal of interest to us emitted by toggle
buttons (the toggle button, check button, and radio button widgets) is
the "toggled" signal. To check the state of these buttons, set up a
signal handler to catch the toggled signal, and access the structure
to determine its state. The callback will look something like:
<P>
<BLOCKQUOTE><CODE>
<PRE>
void toggle_button_callback (GtkWidget *widget, gpointer data)
{
if (GTK_TOGGLE_BUTTON (widget)->active)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -