#include <gtk/gtk.h>
#include <stdio.h>
/*微调按钮回调函数*/
void spin_button_callback(GtkWidget *widget,gpointer data)
{
GtkWidget *spinbutton=(GtkWidget *)data;
int val;
/*获取当前微调按钮的值*/
val=gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
g_print("val=%dn",val);
/*修改小数位数*/
gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinbutton),val);
}
/*复选按钮处理函数*/
void check_button_callback(GtkWidget *widget,gpointer data)
{
GtkWidget *spinbutton=(GtkWidget *)data;
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{
/*设置微调按钮只能输入数字*/
gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinbutton),TRUE);
}
else
{
/*设置微调按钮可以输入任意内容*/
gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spinbutton),FALSE);
}
}
/*保存按钮处理函数*/
void save_button_callback(GtkWidget *widget,gpointer data)
{
g_print("保存n");
GtkWidget *window=(GtkWidget *)data;
GtkWidget *dialog;
GtkWidget *label;
dialog=gtk_dialog_new_with_buttons("保存", GTK_WINDOW(window),GTK_DIALOG_MODAL,NULL);
gtk_window_set_default_size(GTK_WINDOW(dialog),100, 100);
label=gtk_label_new("保存成功n");
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),label,TRUE, TRUE,0);
gtk_widget_show(label);
gtk_widget_show(dialog);
}
GtkWidget *dialog;
/*对话框处理回调函数*/
void button_clicked_callback(GtkWidget *widget,gpointer data)
{
char *pada=(gchar *)data;
if(!strcmp(pada,"on"))
{
gtk_widget_destroy(dialog);//关闭对话框
}
if(!strcmp(pada,"ok"))
{
gtk_main_quit();
}
}
/*退出按钮处理函数*/
void quit_button_callback(GtkWidget *widget,gpointer data)
{
g_print("退出n");
GtkWidget *window=(GtkWidget *)data;
GtkWidget *label;
GtkWidget *table;
GtkWidget *button;
dialog=gtk_dialog_new_with_buttons("是否退出", GTK_WINDOW(window),GTK_DIALOG_MODAL,NULL);
gtk_window_set_default_size(GTK_WINDOW(dialog),100, 100);
label=gtk_label_new("是否退出");
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),label,TRUE, TRUE,0);
gtk_widget_show(label);
/*创建组装表*/
table=gtk_table_new(1,0,FALSE);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),table,FALSE, FALSE, 0);
gtk_widget_show(table);
/*创建按钮*/
button=gtk_button_new_from_stock(GTK_STOCK_NO);
gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1);
gtk_widget_show(button);
g_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(button_clicked_callback),"on");
/*创建按钮*/
button=gtk_button_new_from_stock(GTK_STOCK_OK);
gtk_table_attach_defaults(GTK_TABLE(table),button,1,2,0,1);
g_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(button_clicked_callback),"ok");
gtk_widget_show(button);
gtk_widget_show(dialog);
}
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *vbox,*box;
GtkWidget *frame;
GtkAdjustment *adjust;
GtkWidget *table;
GtkWidget *label;
GtkWidget *spin_button,*spin_val_button;
GtkWidget *check_button;
GtkWidget *button;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "微调按钮");
gtk_window_set_default_size(GTK_WINDOW(window),320, 240);
gtk_container_set_border_width(GTK_CONTAINER(window) ,5);
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
/*创建纵向盒*/
vbox=gtk_vbox_new(FALSE,0);
gtk_container_set_border_width(GTK_CONTAINER(vbox) ,2);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_widget_show(vbox);
/*创建框架构件*/
frame=gtk_frame_new("日期设置");
gtk_frame_set_label_align(GTK_FRAME(frame),1.0,0.5);
/*设置框架构件风格*/
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
gtk_box_pack_start(GTK_BOX(vbox),frame, FALSE,TRUE,0);
gtk_widget_show(frame);
/*创建组装表*/
table=gtk_table_new(2,3,FALSE);
gtk_container_add(GTK_CONTAINER(frame),table);
/*设置列之间的间隔*/
gtk_table_set_col_spacings(GTK_TABLE(table),20);
gtk_widget_show(table);
/*创建调整对象*/
adjust=(GtkAdjustment *)gtk_adjustment_new(2022,1970,3000,1,1,0);
/*创建微调按钮*/
spin_button=gtk_spin_button_new(adjust,0.5, 0);
/*限制微调按钮只能输入数值*/
gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(spin_button),TRUE);
/*设置按钮达到最大值后再变为最小值*/
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON(spin_button), TRUE);
gtk_table_attach_defaults (GTK_TABLE(table),spin_button,0,1,1,2);
gtk_widget_show(spin_button);
/*设置标签*/
label=gtk_label_new("年");
gtk_table_attach_defaults(GTK_TABLE(table),label,0,1,0,1);
gtk_widget_show(label);
/*创建微调按钮*/
adjust=(GtkAdjustment *)gtk_adjustment_new(1,1,12,1,1,0);
spin_button=gtk_spin_button_new(adjust,1, 0);
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON(spin_button), TRUE);
gtk_table_attach_defaults (GTK_TABLE(table),spin_button,1,2,1,2);
gtk_widget_show(spin_button);
/*设置标签*/
label=gtk_label_new("月");
gtk_table_attach_defaults(GTK_TABLE(table),label,1,2,0,1);
gtk_widget_show(label);
/*创建微调按钮*/
adjust=(GtkAdjustment *)gtk_adjustment_new(1,1,31,1,1,0);
spin_button=gtk_spin_button_new(adjust,0.5, 0);
gtk_spin_button_set_wrap (GTK_SPIN_BUTTON(spin_button), TRUE);
gtk_table_attach_defaults (GTK_TABLE(table),spin_button,2,3,1,2);
gtk_widget_show(spin_button);
/*设置标签*/
label=gtk_label_new("日");
gtk_table_attach_defaults(GTK_TABLE(table),label,2,3,0,1);
gtk_widget_show(label);
/*创建框架构件*/
frame=gtk_frame_new("数值调整");
gtk_frame_set_label_align(GTK_FRAME(frame),0.5,0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_OUT);
gtk_box_pack_start(GTK_BOX(vbox),frame, FALSE,TRUE,0);
gtk_widget_show(frame);
/*创建纵向向盒*/
box=gtk_vbox_new(FALSE,0);
gtk_container_add(GTK_CONTAINER(frame),box);
gtk_widget_show(box);
/*创建组装表*/
table=gtk_table_new(2,2,FALSE);
gtk_box_pack_start(GTK_BOX(box),table,TRUE,TRUE,0);
/*设置列之间的间隔*/
gtk_table_set_col_spacings(GTK_TABLE(table),10);
gtk_widget_show(table);
/*创建调整对象*/
adjust=(GtkAdjustment *)gtk_adjustment_new(0,-5000,5000,0.5,0.5,0);
spin_val_button=gtk_spin_button_new(adjust,0.5, 2);
gtk_table_attach_defaults (GTK_TABLE(table),spin_val_button,0,1,1,2);
gtk_widget_show(spin_val_button);
/*设置标签*/
label=gtk_label_new("数值");
gtk_table_attach_defaults(GTK_TABLE(table),label,0,1,0,1);
gtk_widget_show(label);
/*创建调整对象*/
adjust=(GtkAdjustment *)gtk_adjustment_new(2,0,6,1,1,0);
spin_button=gtk_spin_button_new(adjust,0.5, 0);
g_signal_connect(G_OBJECT(spin_button),"value_changed",G_CALLBACK(spin_button_callback), spin_val_button);
gtk_table_attach_defaults (GTK_TABLE(table),spin_button,1,2,1,2);
gtk_widget_show(spin_button);
/*设置标签*/
label=gtk_label_new("小数位数");
gtk_table_attach_defaults(GTK_TABLE(table),label,1,2,0,1);
gtk_widget_show(label);
/*创建复选按钮*/
check_button=gtk_check_button_new_with_label("仅输入数字");
gtk_box_pack_start(GTK_BOX(box),check_button,FALSE,FALSE,0);
g_signal_connect(G_OBJECT(check_button),"toggled",G_CALLBACK(check_button_callback), spin_button);
gtk_widget_show(check_button);
/*创建横向盒*/
box=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),box,TRUE,TRUE,0);
gtk_widget_show(box);
/*创建保存按钮*/
button=gtk_button_new_with_label("退出");
gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(quit_button_callback),window);
gtk_widget_show(button);
/*创建保存按钮*/
button=gtk_button_new_with_label("保存");
gtk_box_pack_end(GTK_BOX(box),button,FALSE,FALSE,0);
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(save_button_callback),window);
gtk_widget_show(button);
gtk_widget_show(window);
gtk_main();
}