#include <gtk/gtk.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
//time_t times;
GtkWidget *time_entry,*calendar,*entry_label;
struct tm *t;
char time_buff[20];
gint settime(gpointer data);
void button_event(GtkWidget *widget, gpointer *data);
int main(int argc,char *argv[])
{
GtkWidget *window,*vbox,*hbox,*button,*label,*fixed1,*fixed2;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window,400,380);
gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS );
gtk_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
vbox=gtk_vbox_new(FALSE,10);
hbox=gtk_hbox_new(FALSE,40);
fixed1 = gtk_fixed_new();
fixed2 = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window),vbox);
calendar=gtk_calendar_new();
gtk_box_pack_start(GTK_BOX(vbox),calendar,FALSE,FALSE,5);
time_entry = gtk_entry_new();
gtk_entry_set_width_chars(GTK_ENTRY(time_entry),10);
label = gtk_label_new(g_convert("日期时间:",-1,"UTF-8","GB2312",NULL,NULL,NULL));
entry_label = gtk_entry_new();
gtk_editable_set_editable(GTK_EDITABLE(entry_label),FALSE);
gtk_container_add(GTK_CONTAINER(hbox),label);
gtk_container_add(GTK_CONTAINER(hbox),entry_label);
gtk_container_add(GTK_CONTAINER(hbox),time_entry);
gtk_container_add(GTK_CONTAINER(fixed1),hbox);
gtk_editable_set_editable(GTK_EDITABLE(entry_label),TRUE);
gtk_entry_set_width_chars(GTK_ENTRY(entry_label),20);
gtk_fixed_put(GTK_FIXED(fixed1), hbox, 20, 20);
gtk_box_pack_start(GTK_BOX(vbox),fixed1,FALSE,FALSE,0);
settime(NULL);
//g_timeout_add(100,settime,NULL);
button=gtk_button_new_with_label("OK");
gtk_widget_set_size_request(button,100,30);
gtk_container_add(GTK_CONTAINER(fixed2),button);
gtk_fixed_put(GTK_FIXED(fixed2), button, 140, 20);
gtk_box_pack_start(GTK_BOX(vbox),fixed2,FALSE,FALSE,10);
gtk_signal_connect(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(button_event),NULL);
;
gtk_widget_show_all(window);
gtk_main();
}
void button_event(GtkWidget *widget,gpointer *data)
{
//time_t timep;
// struct tm *t;
//time(&timep);
//t = localtime(&timep);
char data_buff[20];
guint year,month,day;
// times=time(NULL);
gtk_calendar_get_date(GTK_CALENDAR(calendar),&year,&month,&day);
strcpy(time_buff,gtk_entry_get_text(GTK_ENTRY(time_entry)));
sprintf(data_buff,"%d-%d-%d %s",year,month+1,day,time_buff);
gtk_entry_set_text(GTK_ENTRY(entry_label),data_buff);
memset(data_buff,0,sizeof(data_buff));
}
gint settime(gpointer data)
{
time_t timep;
// struct tm *t;
time(&timep);
t = localtime(&timep);
sprintf(time_buff,"%d:%d:%d\n",t->tm_hour,t->tm_min,t->tm_sec);
// times=time(NULL);
gtk_entry_set_text(GTK_ENTRY(time_entry),time_buff);
memset(time_buff,0,sizeof(time_buff));
return TRUE;
}