Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->Linux技术 ->系统管理 ->正文

GTK v1.2 Tutorial(英文)

来源:Linux-cn.com 作者:Webmaster 时间:2007-05-05 点击: [收藏] [投稿]

  Note that with these calls, the last row and last column do not get any spacing.

4.5 Table Packing Example

  Here we make a window with three buttons in a 2x2 table. The first two buttons will be placed in the upper row. A third, quit button, is placed in the lower row, spanning both columns. Which means it should look something like this:

  Here's the source code:


/* example-start table table.c */
#include <gtk/gtk.h>
/* Our callback.
* The data passed to this function is printed to stdout */
void callback( GtkWidget *widget,gpointer data )
{
g_print ("Hello again - %s was pressed
", (char *) data);
}
/* This callback quits the program */
gint delete_event( GtkWidget *widget,GdkEvent*event,gpointer data )
{
gtk_main_quit ();
return(FALSE);
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *table;
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* Set the window title */
gtk_window_set_title (GTK_WINDOW (window), "Table");
/* Set a handler for delete_event that immediately
 * exits GTK. */
gtk_signal_connect (GTK_OBJECT (window), "delete_event",
GTK_SIGNAL_FUNC (delete_event), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
/* Create a 2x2 table */
table = gtk_table_new (2, 2, TRUE);
/* Put the table in the main window */
gtk_container_add (GTK_CONTAINER (window), table);
/* Create first button */
button = gtk_button_new_with_label ("button 1");
/* When the button is clicked, we call the "callback" function
 * with a pointer to "button 1" as its argument */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
/* Insert button 1 into the upper left quadrant of the table */
gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
gtk_widget_show (button);
/* Create second button */
button = gtk_button_new_with_label ("button 2");
/* When the button is clicked, we call the "callback" function
 * with a pointer to "button 2" as its argument */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
/* Insert button 2 into the upper right quadrant of the table */
gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);
gtk_widget_show (button);
/* Create "Quit" button */
button = gtk_button_new_with_label ("Quit");
/* When the button is clicked, we call the "delete_event" function
 * and the program exits */
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (delete_event), NULL);
/* Insert the quit button into the both 
 * lower quadrants of the table */
gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);
gtk_widget_show (button);
gtk_widget_show (table);
gtk_widget_show (window);
gtk_main ();
return 0;
}
/* example-end */

5. Widget Overview

  The general steps to creating a widget in GTK are:

  gtk_*_new - one of various functions to create a new widget. These are all detailed in this section.

  Connect all signals and events we wish to use to the appropriate handlers.

  Set the attributes of the widget.

  Pack the widget into a container using the appropriate call such as gtk_container_add() or gtk_box_pack_start().

  gtk_widget_show() the widget.

  gtk_widget_show() lets GTK know that we are done setting the attributes of the widget, and it is ready to be displayed. You may also use gtk_widget_hide to make it disappear again. The order in which you show the widgets is not important, but I suggest showing the window last so the whole window pops up at once rather than seeing the individual widgets come up on the screen as they're formed. The children of a widget (a window is a widget too) will not be displayed until the window itself is shown using the gtk_widget_show() function.

5.1 Casting

  You'll notice as you go on that GTK uses a type casting system. This is always done using macros that both test the ability to cast the given item, and perform the cast. Some common ones you will see are:


GTK_WIDGET(widget)
GTK_OBJECT(object)
GTK_SIGNAL_FUNC(function)
GTK_CONTAINER(container)
GTK_WINDOW(window)
GTK_BOX(box)

  These are all used to cast arguments in functions. You'll see them in the examples, and can usually tell when to use them simply by looking at the function's declaration.

  As you can see below in the class hierarchy, all GtkWidgets are derived from the Object base class. This means you can use a widget in any place the function asks for an object - simply use the GTK_OBJECT() macro.

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<

上一页 1 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 下一页


上一篇:GTK入门导引   下一篇:GDK Reference Manual

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
·Motorola微处理器bootloader分析与应用
·Fedora Core5 NFS服务器搭建过程介绍
·Linux系统:让内存不再泄漏的实用技巧
·新手看招 手把手教你安装VMware虚拟机
·“侵权事件” 红帽称微软企图干扰用户
·删除Linux后 如何找回Windows启动菜单
·菜鸟乐园 Linux中常见文件系统格式介绍
·Linux操作系统下IPTables配置方法详解
·实用技巧 Linux系统的经典使用技巧八则
·Linux系统文件优化及磁盘检查方法介绍
推荐文章
·Linux技巧:bz2文件如何解压缩
·用MRTG检测Linux网络性能
·红帽宣布开放JBoss中核心系统管理软
·如何设定与维持准确的电脑时钟
·Linux系统中找出并解决程序错误方法
·在Linux下成功运行最新的QQ和MSN
·如何重新规划分割区
·Webmin---Linux下的用户管理工具
精彩文章
·起步走 二十五步跟我搞定Linux系统
·几个JSP应用模板
·浅析:从xml生成Java类
·Linux下ip隧道研究(1)
·CORBA及MICO简介
·用Linux防火墙构建DMZ
·linux汉化大全(六)
·用Java Mail API查看邮件
·在Linux Shell程序中进行身份验证
·Linux指令大全(2)
·rootkit综述
·在linux7.1上安装jdk+tomcat+orac
·Linux御敌策略谈
·巧用KDE
·Linux 网管 123 --- 第8章. 备份及
·Socket建立连接的过程
·Linux命令Man解释:ftpaccess(5) :ft
·和luster一起学习系统调用(3)
·文件系统安全--EXT2文件系统的属性
·打造完美Linux系统:疑难杂症的解决
·Bean管理持续化实例
·Linux 每次启动时钟日期就多了 8 小
·基于Linux的Web服务器性能测试
·JDK开发工具包(三)
·Linux平台C++类库tinycxx使用说明
·和luster一起学习java编程(4)
Power by linux-cn.com 粤ICP备05006655号