首先用glade画图形界面:
并且设置gtk元素名称(ID)以及设置事件回调函数。
下面写代码:
#include <gtk/gtk.h>
#include <string.h>
#define UI_PATH "./entry.ui"
#define MAIN_WINDOW "window"
#define CHK_EDITABLE "chk_editable"
#define CHK_VISIBLE "chk_visible"
void editable_toggled(GtkWidget *self,GtkEntry *entry){
//gtk_entry_set_editable(entry,GTK_TOGGLE_BUTTON(self)->active);
printf("%s:enter function\n",__func__);
gtk_editable_set_editable((GtkEditable*)entry,gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON(self)));
}
void visible_toggled(GtkWidget *self,GtkEntry *entry){
//gtk_entry_set_visibility(entry,GTK_TOGGLE_BUTTON(self)->active);
printf("%s:enter function\n",__func__);
gtk_entry_set_visibility(entry,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self)));
}
void entry_changed(GtkWidget *self,gpointer user_data){
gchar *text = (gchar*)gtk_entry_get_text(GTK_ENTRY(self));
printf("%s:text is %s\n",__func__,text);
text = g_ascii_strup(text,strlen(text));
gtk_entry_set_text(GTK_ENTRY(self),text);
}
int main(int argc,char **argv){
GtkWidget *w;
GError *error;
gtk_init(&argc,&argv);
GtkBuilder *builder = gtk_builder_new();
if(!gtk_builder_add_from_file(builder,UI_PATH,&error)){
g_critical("Couldn't load builder file: %s",error->message);
g_error_free(error);
}
w = GTK_WIDGET(gtk_builder_get_object(builder,MAIN_WINDOW));
if(!w){
g_critical("Widget \"%s\" is missing in file %s",MAIN_WINDOW,UI_PATH);
}
gtk_window_set_title(GTK_WINDOW(w),"clist todo list");
gtk_window_set_default_size(GTK_WINDOW(w),300,100);
gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(w),5);
gtk_builder_connect_signals(builder,NULL);
g_object_unref(builder);
gtk_widget_show_all(w);
gtk_main();
return 0;
}
GtkEntry有2个更改事件,分别是activate和changed;前者在文本输入控件内按回车时触发,后者当文本控件内部的文本发生变化时触发。
编译完毕执行效果如下: