GTK v1.2 Tutorial(英文)
来源:Linux-cn.com
作者:Webmaster
时间:2007-05-05
点击:
[ 收藏] [ 投稿]
GdkPixmap *gdk_pixmap_create_from_xpm( GdkWindow *window,
GdkBitmap**mask,GdkColor*transparent_color,const gchar *filename );
|
XPM format is a readable pixmap representation for the X Window System. It is widely used and many different utilities are available for creating image files in this format. The file specified by filename must contain an image in that format and it is loaded into the pixmap structure. The mask specifies which bits of the pixmap are opaque. All other bits are colored using the color specified by transparent_color. An example using this follows below.
GdkPixmap *gdk_pixmap_create_from_xpm_d( GdkWindow*window,
GdkBitmap **mask,GdkColor *transparent_color,gchar **data );
|
Small images can be incorporated into a program as data in the XPM format. A pixmap is created using this data, instead of reading it from a file. An example of such data is
/* XPM */
static const char * xpm_data[] = {
"16 16 3 1",
" c None",
".c #000000000000",
"Xc #FFFFFFFFFFFF",
"",
" ...... ",
" .XXX.X.",
" .XXX.XX. ",
" .XXX.XXX.",
" .XXX.....",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .........",
"",
""};
|
When we're done using a pixmap and not likely to reuse it again soon, it is a good idea to release the resource using gdk_pixmap_unref(). Pixmaps should be considered a precious resource, because they take up memory in the end-user's X server process. Even though the X client you write may run on a powerful "server" computer, the user may be running the X server on a small personal computer.
Once we've created a pixmap, we can display it as a GTK widget. We must create a GTK pixmap widget to contain the GDK pixmap. This is done using
GtkWidget *gtk_pixmap_new( GdkPixmap *pixmap,GdkBitmap *mask );
|
The other pixmap widget calls are
guint gtk_pixmap_get_type( void );
voidgtk_pixmap_set( GtkPixmap*pixmap,GdkPixmap*val,GdkBitmap*mask );
voidgtk_pixmap_get( GtkPixmap*pixmap,
GdkPixmap **val,GdkBitmap **mask);
|
gtk_pixmap_set is used to change the pixmap that the widget is currently managing. Val is the pixmap created using GDK.
The following is an example of using a pixmap in a button.
/* example-start pixmap pixmap.c */
#include <gtk/gtk.h>
/* XPM data of Open-File icon */
static const char * xpm_data[] = {
"16 16 3 1",
" c None",
".c #000000000000",
"Xc #FFFFFFFFFFFF",
"",
" ...... ",
" .XXX.X.",
" .XXX.XX. ",
" .XXX.XXX.",
" .XXX.....",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .XXXXXXX.",
" .........",
"",
""};
/* when invoked (via signal delete_event), terminates the application.*/
gint close_application( GtkWidget *widget,GdkEvent*event,gpointer data )
{
gtk_main_quit();
return(FALSE);
}
/* is invoked when the button is clicked.It just prints a message.*/
void button_clicked( GtkWidget *widget,gpointer data )
{
g_print( "button clicked
" );
}
int main( int argc,
char *argv[] )
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window, *pixmapwid, *button;
GdkPixmap *pixmap;
GdkBitmap *mask;
GtkStyle *style;
/* create the main window, and attach delete_event signal to terminating
the application */
gtk_init( &argc, &argv );
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_signal_connect( GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (close_application), NULL );
gtk_container_set_border_width( GTK_CONTAINER (window), 10 );
gtk_widget_show( window );
/* now for the pixmap from gdk */
style = gtk_widget_get_style( window );
pixmap = gdk_pixmap_create_from_xpm_d( window->window,&mask,
&style->bg[GTK_STATE_NORMAL],
(gchar **)xpm_data );
/* a pixmap widget to contain the pixmap */
pixmapwid = gtk_pixmap_new( pixmap, mask );
gtk_widget_show( pixmapwid );
/* a button to contain the pixmap widget */
button = gtk_button_new();
gtk_container_add( GTK_CONTAINER(button), pixmapwid );
gtk_container_add( GTK_CONTAINER(window), button );
gtk_widget_show( button );
gtk_signal_connect( GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(button_clicked), NULL );
/* show the window */
gtk_main ();
return 0;
}
/* example-end */
如果您对本文有任何疑问或者建议,请到讨论区发表您的意见:
>>
论坛入口 <<
上一篇:GTK入门导引
下一篇:GDK Reference Manual
【文章评论】
【收藏本文】
【推荐好友】
【打印本文】
【我要投稿】 【论坛讨论】
更多相关文章
|
|
|