#include <gtk/gtk.h>
//在glade中,为button1添加的点击函数
void button1_clicked_cb(GObject *object, gpointer user_data)
{
printf("in button1_clicked_cb\n");
//gtk_main_quit();
//这里仅实现退出程序
}
void button1_toggled_cb(GObject *object, gpointer user_data)
{
printf("in button_toggled_cb\n");
//gtk_main_quit();
//这里仅实现退出程序
}
void button_toggled_cb( GtkToggleButton *toggleBtn, gpointer data )
{
printf("in button_toggled_cb\n");
if( gtk_toggle_button_get_active(toggleBtn) ){
gtk_button_set_label(GTK_BUTTON(toggleBtn), "停駐");
}else{
gtk_button_set_label(GTK_BUTTON(toggleBtn), "未停駐");
}
}
//在自訂 callback 函式 中使用過GtkButton作為範例,
//最基本的GtkButton建立方式是透過gtk_button_new_with_label()來建立一個
//指定 文字的按鈕,
//您也可以使用gtk_button_new_with_mnemonic(),
//這會建立一個具有提示底線及快捷鍵功能的按鈕,
//例如將 自 訂 callback 函式 範例的 gtk_button_new_with_label()該行換成 gtk_button_new_with_mnemonic():
//button = gtk_button_new_with_mnemonic("_Press");
//
//指定的按鈕文字是以底線作為開頭,
//這會在顯示的按鈕文字上以「P ress」的方式呈現,
//並且按下Alt+P時即可觸發按鈕Clicked,執行時的畫面如下:
//若想要製作雙態按鈕(Toggle Button),則可以使用GtkToggleButton,
//您可以gtk_toggle_button_new_with_label()、 gtk_toggle_button_new_with_mnemonic()函式來建立元件,
//雙態按鈕有停駐及非停駐兩種狀態,可以使用 gtk_toggle_button_get_active()取得雙態按鈕的狀態,
//也可以透過 gtk_toggle_button_set_active()直接設定按鈕為停駐或非停駐狀態,
//雙態按鈕所引發的Signal是"toggled",若 是使用gtk_toggle_button_set_active()改變了停駐狀態,
//則會同時引發"clicked"及"toggled"兩個 Signal。
//下面這個程式示範了GtkToggleButton的外觀與Signal處理:
void entry_callback(GtkWidget *widget, gpointer entry)
{
const gchar *entry_text;
//获取文本内容
entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
printf("entry content:%s\n", entry_text);
return;
}
#if 0
int main(int argc, char *argv[])
{
//1.gtk环境初始化
gtk_init(&argc, &argv);
//2.创建一个window
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//设置窗口大小
gtk_widget_set_size_request(window, 200, 50);
g_signal_connect(window,"destroy", G_CALLBACK(gtk_main_quit), NULL);
//3.创建一个行编辑
GtkWidget *entry = gtk_entry_new();
gtk_container_add(GTK_CONTAINER(window), entry);
//4.设置行编辑显示最大字符的长度
gtk_entry_set_max_length(GTK_ENTRY(entry), 100);
//设置行编辑的内容
gtk_entry_set_text(GTK_ENTRY(entry), "hello entry");
//5.当我们在行编辑中敲回车的时候执行的动作
g_signal_connect(entry, "activate", G_CALLBACK(entry_callback), entry);
//6.显示所有窗口
gtk_widget_show_all(window);
//7.主事件循环
gtk_main();
return 0;
}
#endif
int main( int argc, char *argv[] )
{
GtkWidget *btn;
GtkBuilder *builder;
GtkWidget *window;
GtkWidget *toggleBtn;
GtkWidget *entry;
gtk_init(&argc, &argv);
builder = gtk_builder_new ();
gtk_builder_add_from_file(builder, "button.xml", NULL); //构建参数
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); //由构建参数创建窗口
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1")); //获取window1窗口的句柄
toggleBtn = GTK_WIDGET(gtk_builder_get_object (builder, "togglebutton1")); //获取button1控件的句柄
entry = GTK_WIDGET(gtk_builder_get_object (builder, "entry1"));
gtk_builder_connect_signals (builder, NULL); //连接响应事件
g_object_unref (G_OBJECT (builder));
gtk_window_set_title(GTK_WINDOW(window), "GtkToggleButton");
gtk_window_set_default_size(GTK_WINDOW(window), 250, 500);
// toggleBtn = gtk_toggle_button_new_with_label("停駐");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggleBtn), TRUE);
gtk_button_set_label(GTK_BUTTON(toggleBtn),"被选中");
gtk_entry_set_text(GTK_ENTRY(entry), "hello entry");
//5.当我们在行编辑中敲回车的时候执行的动作
g_signal_connect(entry, "activate", G_CALLBACK(entry_callback), entry);
//gtk_container_add(GTK_CONTAINER(window), toggleBtn);
//g_signal_connect(GTK_OBJECT(toggleBtn), "toggled", G_CALLBACK(button_toggled_cb), NULL);
//g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
//gtk_widget_show_all(window);
gtk_widget_show ((GtkWindow*)window);//显示窗体
gtk_main ();//事件响应函数
return 0;
}
///
button.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkSizeGroup" id="sizegroup1"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="button1_clicked_cb" swapped="no"/>
</object>
</child>
<child>
<object class="GtkToggleButton" id="togglebutton1">
<property name="label" translatable="yes">togglebutton</property>
<property name="width_request">96</property>
<property name="height_request">51</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="toggled" handler="button_toggled_cb" swapped="no"/>
</object>
<packing>
<property name="y">486</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry1">
<property name="width_request">388</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin_left">78</property>
<property name="margin_right">148</property>
<property name="shadow_type">etched-out</property>
</object>
<packing>
<property name="x">52</property>
<property name="y">248</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
///
Makefile:
text:helloworld.c
# gtk-builder-convert button.glade button.xml
gcc -Wall -g -o $@ $< `pkg-config --cflags --libs gtk+-3.0` -export-dynamic