gtk电子时钟

#include<gtk/gtk.h>
#include<time.h>
#include<sys/time.h>
#include<string.h>
static guint flag=0;
static button_flag=0;
guint flag_s=0;
guint f_flag=0;
static guint timer = 0;
guint time_flag=0;
struct labeltime{
GtkWidget * s_label_hour;
GtkWidget * s_label_min;
GtkWidget * s_label_second;
int s_cancel_hour;
int s_cancel_min;
int s_cancel_sec;
int s_set_hour;
int s_set_min;
int s_set_sec;
}s_label;
GdkColor set_color(unsigned int r, unsigned int g, unsigned int b)
{
GdkColor color;
color.red = 65535*r/256;
color.green = 65535*g/256;
color.blue = 65535*b/256;

return color;
}
gboolean on_timer(gpointer data)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test============");
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);

if(time_flag==1)
{
p->tm_hour=s_label.s_set_hour;
p->tm_min=s_label.s_set_min;
p->tm_sec=s_label.s_set_sec;

time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
}
time_flag=0;
sprintf(buff,"%d",p->tm_hour);
g_print("%s\n",buff);
g_print("on_time_hour=%d\n",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
g_print("%s\n",buff);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
sprintf(buff,"%d",p->tm_sec);
g_print("%s\n",buff);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);

return TRUE;
}
static void chang_background(GtkWidget *widget, int w, int h, const gchar *path)
{
gtk_widget_set_app_paintable(widget, TRUE); //允许窗口可以绘图
    gtk_widget_realize(widget);

/* 更改背景图时,图片会重叠
* 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。
*/
gtk_widget_queue_draw(widget);

    GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(path, NULL); // 创建图片资源对象
    // w, h是指定图片的宽度和高度
    GdkPixbuf *dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h, GDK_INTERP_BILINEAR);


GdkPixmap *pixmap = NULL;

/* 创建pixmap图像; 
* NULL:不需要蒙版; 
* 123: 0~255,透明到不透明
*/
    gdk_pixbuf_render_pixmap_and_mask(dst_pixbuf, &pixmap, NULL, 128);
    // 通过pixmap给widget设置一张背景图,最后一个参数必须为: FASLE
gdk_window_set_back_pixmap(widget->window, pixmap, FALSE);
    
// 释放资源
g_object_unref(src_pixbuf);
    g_object_unref(dst_pixbuf);
    g_object_unref(pixmap);
}
static void set_widget_font_size(GtkWidget *widget, int size, gboolean is_button)
{
GtkWidget *labelChild;  
PangoFontDescription *font;  
gint fontSize = size;  
  
font = pango_font_description_from_string("Sans");          //"Sans"字体名   
pango_font_description_set_size(font, fontSize*PANGO_SCALE);//设置字体大小   

if(is_button){
labelChild = gtk_bin_get_child(GTK_BIN(widget));//取出GtkButton里的label  
}else{
labelChild = widget;
}

//设置label的字体,这样这个GtkButton上面显示的字体就变了
gtk_widget_modify_font(GTK_WIDGET(labelChild), font);
pango_font_description_free(font);
}
void on_click_button_up(GtkWidget *widget, gpointer window)
{
struct tm *p;
struct timeval time_tv;

gchar buff[20];
g_print("=============test  up============\n");
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
g_print("sec0=%d\n",p->tm_sec);
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
g_source_remove(timer);
if(f_flag==0)
s_label.s_set_sec=p->tm_sec;
if(button_flag==1)
{
if(p->tm_hour==23)
p->tm_hour=0;
p->tm_hour++;
}
else if(button_flag==2)
{
if(p->tm_min==59)
p->tm_min=0;
p->tm_min++;
}
else if(button_flag==0)
{

if(p->tm_sec==59)
p->tm_sec=0;
p->tm_sec++;
s_label.s_set_sec=p->tm_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);
}

g_print("sec2=%d\n",p->tm_sec);
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
g_print("sec3=%d\n",p->tm_sec);
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);

s_label.s_set_hour=p->tm_hour;
s_label.s_set_min=p->tm_min;

f_flag=1;


}
}
void on_click_button_down(GtkWidget *widget, gpointer window)
{

struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test  down %d============\n",p->tm_hour);
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);

if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{

g_source_remove(timer);
if(f_flag==0)
s_label.s_set_sec=p->tm_sec;
if(button_flag==1)
{
if(p->tm_hour==0)
p->tm_hour=23;
p->tm_hour--;
}
else if(button_flag==2)
{
if(p->tm_min==0)
p->tm_min=59;
p->tm_min--;
}
else if(button_flag==0)
{
g_source_remove(timer);
if(p->tm_sec==0)
p->tm_sec=59;
p->tm_sec--;
s_label.s_set_sec=p->tm_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);

}
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
s_label.s_set_hour=p->tm_hour;
s_label.s_set_min=p->tm_min;
f_flag=1;

}
}
void on_click_button_sure(GtkWidget *widget, gpointer window)
{


GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);

flag=0;
button_flag=0;
time_flag=1;
f_flag=0;
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
}
}
void on_click_button_cancel(GtkWidget *widget, gpointer window)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
p->tm_hour=s_label.s_cancel_hour;
p->tm_min=s_label.s_cancel_min;
p->tm_sec=s_label.s_cancel_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");

gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);
flag=0;
button_flag=0;
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
}
}
void on_click_button_set(GtkButton *button, gpointer data)
{

if(flag==0)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test  down %d============\n",p->tm_hour);
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
s_label.s_cancel_hour=p->tm_hour;
s_label.s_cancel_min=p->tm_min;
s_label.s_cancel_sec=p->tm_sec;
g_print("hour=%d\n",p->tm_hour);
g_print("min=%d\n",s_label.s_cancel_min);
g_print("sec=%d\n",s_label.s_cancel_sec);
}
flag=1;
GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(button_flag==0)//hour
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_white);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
}
else if(button_flag==1)//min
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_white);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
}
else if(button_flag==2)//second
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_white);
button_flag=-1;
}
button_flag++;
g_print("on_click_button=%d\n",flag);
}


gboolean deal_mouse_press(GtkWidget *widget, GdkEventButton *event, gpointer window)
{
char *path_name[6]={"./image/1.jpg","./image/2.jpg","./image/3.jpg","./image/4.jpg","./image/5.jpg","./image/6.jpg"};
static int i=1;
chang_background(window, 800, 480, path_name[i]);
i++;
if(i==6)
i=0;
return TRUE;
}


int main(int argc,char *argv[])
{



gtk_init(&argc,&argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget* table = gtk_table_new(3,5,TRUE);


GtkWidget* label1 =gtk_label_new(":");
GtkWidget* label2 =gtk_label_new(":");
GtkWidget* label_hour =gtk_label_new("00");
GtkWidget* label_min =gtk_label_new("00");
GtkWidget* label_second =gtk_label_new("00");

GtkWidget *button_set= gtk_button_new(); // 先创建空按钮
GtkWidget *image1 = gtk_image_new_from_file("./image/button_set.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_set), image1);

GtkWidget* button_up=gtk_button_new();
GtkWidget *image2 = gtk_image_new_from_file("./image/button_add.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_up), image2);

GtkWidget* button_down=gtk_button_new();
GtkWidget *image3 = gtk_image_new_from_file("./image/button_sub.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_down), image3);

GtkWidget* button_sure=gtk_button_new();
GtkWidget *image4 = gtk_image_new_from_file("./image/button_ok.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_sure), image4);

GtkWidget* button_cancel=gtk_button_new();
GtkWidget *image5 = gtk_image_new_from_file("./image/button_cancel.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_cancel), image5);





gtk_widget_set_size_request(window,800,480);


set_widget_font_size(label1, 40, FALSE);
set_widget_font_size(label2, 40, FALSE);
set_widget_font_size(label_hour, 80, FALSE);
set_widget_font_size(label_min, 80, FALSE);
set_widget_font_size(label_second, 80, FALSE);


chang_background(window, 800, 480, "./image/1.jpg");
gtk_container_add(GTK_CONTAINER(window),table);







gtk_table_attach_defaults(GTK_TABLE(table),label_hour,0,1,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label_min,2,3,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label_second,4,5,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label1,1,2,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label2,3,4,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button_set,0,1,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_up,1,2,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_down,2,3,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_sure,3,4,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_cancel,4,5,2,3);
gtk_button_set_relief(GTK_BUTTON(button_set),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_up),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_down),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_sure),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_cancel),GTK_RELIEF_NONE);


//鼠标双击事件
gtk_widget_add_events(window, GDK_2BUTTON_PRESS);
g_signal_connect(window, "button_press_event", G_CALLBACK(deal_mouse_press),(gpointer)window);


g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_print("=============test2============");
s_label.s_label_hour=label_hour;
s_label.s_label_min=label_min;
s_label.s_label_second=label_second;
g_print("=============test1============");
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
g_signal_connect(button_set, "clicked", G_CALLBACK(on_click_button_set), &timer);
g_signal_connect(button_up, "clicked", G_CALLBACK(on_click_button_up), (gpointer)window);
g_signal_connect(button_down, "clicked", G_CALLBACK(on_click_button_down), (gpointer)window);
g_signal_connect(button_sure, "clicked", G_CALLBACK(on_click_button_sure), (gpointer)window);
g_signal_connect(button_cancel, "clicked", G_CALLBACK(on_click_button_cancel), (gpointer)window);


gtk_widget_show_all(window);
gtk_main();
return 0;
}#include<gtk/gtk.h>
#include<time.h>
#include<sys/time.h>
#include<string.h>
static guint flag=0;
static button_flag=0;
guint flag_s=0;
guint f_flag=0;
static guint timer = 0;
guint time_flag=0;
struct labeltime{
GtkWidget * s_label_hour;
GtkWidget * s_label_min;
GtkWidget * s_label_second;
int s_cancel_hour;
int s_cancel_min;
int s_cancel_sec;
int s_set_hour;
int s_set_min;
int s_set_sec;
}s_label;
GdkColor set_color(unsigned int r, unsigned int g, unsigned int b)
{
GdkColor color;
color.red = 65535*r/256;
color.green = 65535*g/256;
color.blue = 65535*b/256;

return color;
}
gboolean on_timer(gpointer data)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test============");
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);

if(time_flag==1)
{
p->tm_hour=s_label.s_set_hour;
p->tm_min=s_label.s_set_min;
p->tm_sec=s_label.s_set_sec;

time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
}
time_flag=0;
sprintf(buff,"%d",p->tm_hour);
g_print("%s\n",buff);
g_print("on_time_hour=%d\n",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
g_print("%s\n",buff);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
sprintf(buff,"%d",p->tm_sec);
g_print("%s\n",buff);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);

return TRUE;
}
static void chang_background(GtkWidget *widget, int w, int h, const gchar *path)
{
gtk_widget_set_app_paintable(widget, TRUE); //允许窗口可以绘图
    gtk_widget_realize(widget);

/* 更改背景图时,图片会重叠
* 这时要手动调用下面的函数,让窗口绘图区域失效,产生窗口重绘制事件(即 expose 事件)。
*/
gtk_widget_queue_draw(widget);

    GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(path, NULL); // 创建图片资源对象
    // w, h是指定图片的宽度和高度
    GdkPixbuf *dst_pixbuf = gdk_pixbuf_scale_simple(src_pixbuf, w, h, GDK_INTERP_BILINEAR);


GdkPixmap *pixmap = NULL;

/* 创建pixmap图像; 
* NULL:不需要蒙版; 
* 123: 0~255,透明到不透明
*/
    gdk_pixbuf_render_pixmap_and_mask(dst_pixbuf, &pixmap, NULL, 128);
    // 通过pixmap给widget设置一张背景图,最后一个参数必须为: FASLE
gdk_window_set_back_pixmap(widget->window, pixmap, FALSE);
    
// 释放资源
g_object_unref(src_pixbuf);
    g_object_unref(dst_pixbuf);
    g_object_unref(pixmap);
}
static void set_widget_font_size(GtkWidget *widget, int size, gboolean is_button)
{
GtkWidget *labelChild;  
PangoFontDescription *font;  
gint fontSize = size;  
  
font = pango_font_description_from_string("Sans");          //"Sans"字体名   
pango_font_description_set_size(font, fontSize*PANGO_SCALE);//设置字体大小   

if(is_button){
labelChild = gtk_bin_get_child(GTK_BIN(widget));//取出GtkButton里的label  
}else{
labelChild = widget;
}

//设置label的字体,这样这个GtkButton上面显示的字体就变了
gtk_widget_modify_font(GTK_WIDGET(labelChild), font);
pango_font_description_free(font);
}
void on_click_button_up(GtkWidget *widget, gpointer window)
{
struct tm *p;
struct timeval time_tv;

gchar buff[20];
g_print("=============test  up============\n");
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
g_print("sec0=%d\n",p->tm_sec);
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
g_source_remove(timer);
if(f_flag==0)
s_label.s_set_sec=p->tm_sec;
if(button_flag==1)
{
if(p->tm_hour==23)
p->tm_hour=0;
p->tm_hour++;
}
else if(button_flag==2)
{
if(p->tm_min==59)
p->tm_min=0;
p->tm_min++;
}
else if(button_flag==0)
{

if(p->tm_sec==59)
p->tm_sec=0;
p->tm_sec++;
s_label.s_set_sec=p->tm_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);
}

g_print("sec2=%d\n",p->tm_sec);
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
g_print("sec3=%d\n",p->tm_sec);
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);

s_label.s_set_hour=p->tm_hour;
s_label.s_set_min=p->tm_min;

f_flag=1;


}
}
void on_click_button_down(GtkWidget *widget, gpointer window)
{

struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test  down %d============\n",p->tm_hour);
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);

if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{

g_source_remove(timer);
if(f_flag==0)
s_label.s_set_sec=p->tm_sec;
if(button_flag==1)
{
if(p->tm_hour==0)
p->tm_hour=23;
p->tm_hour--;
}
else if(button_flag==2)
{
if(p->tm_min==0)
p->tm_min=59;
p->tm_min--;
}
else if(button_flag==0)
{
g_source_remove(timer);
if(p->tm_sec==0)
p->tm_sec=59;
p->tm_sec--;
s_label.s_set_sec=p->tm_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);

}
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
s_label.s_set_hour=p->tm_hour;
s_label.s_set_min=p->tm_min;
f_flag=1;

}
}
void on_click_button_sure(GtkWidget *widget, gpointer window)
{


GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);

flag=0;
button_flag=0;
time_flag=1;
f_flag=0;
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
}
}
void on_click_button_cancel(GtkWidget *widget, gpointer window)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(flag==0)
{
GtkWidget *dialog = gtk_message_dialog_new(window,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING,
GTK_BUTTONS_OK,
"请先进行设置!");
gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
gtk_dialog_run( GTK_DIALOG(dialog) );
gtk_widget_destroy(dialog);
}
else
{
p->tm_hour=s_label.s_cancel_hour;
p->tm_min=s_label.s_cancel_min;
p->tm_sec=s_label.s_cancel_sec;
time_set=mktime(p);
time_tv.tv_sec=time_set;
time_tv.tv_usec=0;
if(0!=(settimeofday(&time_tv,NULL)))
perror("set");

gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
sprintf(buff,"%d",p->tm_hour);
gtk_label_set_text(GTK_LABEL(s_label.s_label_hour),buff);
sprintf(buff,"%d",p->tm_min);
gtk_label_set_text(GTK_LABEL(s_label.s_label_min),buff);
sprintf(buff,"%d",p->tm_sec);
gtk_label_set_text(GTK_LABEL(s_label.s_label_second),buff);
flag=0;
button_flag=0;
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
}
}
void on_click_button_set(GtkButton *button, gpointer data)
{

if(flag==0)
{
struct tm *p;
struct timeval time_tv;
gchar buff[20];
g_print("=============test  down %d============\n",p->tm_hour);
time_t time_s1=0,time_set=0;
time(&time_s1);
p=localtime(&time_s1);
s_label.s_cancel_hour=p->tm_hour;
s_label.s_cancel_min=p->tm_min;
s_label.s_cancel_sec=p->tm_sec;
g_print("hour=%d\n",p->tm_hour);
g_print("min=%d\n",s_label.s_cancel_min);
g_print("sec=%d\n",s_label.s_cancel_sec);
}
flag=1;
GdkColor color_white = set_color(255, 255, 255);// 创建一个颜色,RGB, 白色
GdkColor color_black = set_color(0, 0, 0);// 创建一个颜色,RGB, 白色
if(button_flag==0)//hour
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_white);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
}
else if(button_flag==1)//min
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_white);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_black);
}
else if(button_flag==2)//second
{
gtk_widget_modify_fg(s_label.s_label_hour, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_min, GTK_STATE_NORMAL, &color_black);
gtk_widget_modify_fg(s_label.s_label_second, GTK_STATE_NORMAL, &color_white);
button_flag=-1;
}
button_flag++;
g_print("on_click_button=%d\n",flag);
}


gboolean deal_mouse_press(GtkWidget *widget, GdkEventButton *event, gpointer window)
{
char *path_name[6]={"./image/1.jpg","./image/2.jpg","./image/3.jpg","./image/4.jpg","./image/5.jpg","./image/6.jpg"};
static int i=1;
chang_background(window, 800, 480, path_name[i]);
i++;
if(i==6)
i=0;
return TRUE;
}


int main(int argc,char *argv[])
{



gtk_init(&argc,&argv);
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget* table = gtk_table_new(3,5,TRUE);


GtkWidget* label1 =gtk_label_new(":");
GtkWidget* label2 =gtk_label_new(":");
GtkWidget* label_hour =gtk_label_new("00");
GtkWidget* label_min =gtk_label_new("00");
GtkWidget* label_second =gtk_label_new("00");

GtkWidget *button_set= gtk_button_new(); // 先创建空按钮
GtkWidget *image1 = gtk_image_new_from_file("./image/button_set.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_set), image1);

GtkWidget* button_up=gtk_button_new();
GtkWidget *image2 = gtk_image_new_from_file("./image/button_add.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_up), image2);

GtkWidget* button_down=gtk_button_new();
GtkWidget *image3 = gtk_image_new_from_file("./image/button_sub.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_down), image3);

GtkWidget* button_sure=gtk_button_new();
GtkWidget *image4 = gtk_image_new_from_file("./image/button_ok.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_sure), image4);

GtkWidget* button_cancel=gtk_button_new();
GtkWidget *image5 = gtk_image_new_from_file("./image/button_cancel.png"); // 图像控件
gtk_button_set_image(GTK_BUTTON(button_cancel), image5);





gtk_widget_set_size_request(window,800,480);


set_widget_font_size(label1, 40, FALSE);
set_widget_font_size(label2, 40, FALSE);
set_widget_font_size(label_hour, 80, FALSE);
set_widget_font_size(label_min, 80, FALSE);
set_widget_font_size(label_second, 80, FALSE);


chang_background(window, 800, 480, "./image/1.jpg");
gtk_container_add(GTK_CONTAINER(window),table);







gtk_table_attach_defaults(GTK_TABLE(table),label_hour,0,1,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label_min,2,3,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label_second,4,5,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label1,1,2,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),label2,3,4,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button_set,0,1,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_up,1,2,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_down,2,3,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_sure,3,4,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button_cancel,4,5,2,3);
gtk_button_set_relief(GTK_BUTTON(button_set),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_up),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_down),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_sure),GTK_RELIEF_NONE);
gtk_button_set_relief(GTK_BUTTON(button_cancel),GTK_RELIEF_NONE);


//鼠标双击事件
gtk_widget_add_events(window, GDK_2BUTTON_PRESS);
g_signal_connect(window, "button_press_event", G_CALLBACK(deal_mouse_press),(gpointer)window);


g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
g_print("=============test2============");
s_label.s_label_hour=label_hour;
s_label.s_label_min=label_min;
s_label.s_label_second=label_second;
g_print("=============test1============");
timer = g_timeout_add(1000,(GSourceFunc)on_timer,NULL);
g_signal_connect(button_set, "clicked", G_CALLBACK(on_click_button_set), &timer);
g_signal_connect(button_up, "clicked", G_CALLBACK(on_click_button_up), (gpointer)window);
g_signal_connect(button_down, "clicked", G_CALLBACK(on_click_button_down), (gpointer)window);
g_signal_connect(button_sure, "clicked", G_CALLBACK(on_click_button_sure), (gpointer)window);
g_signal_connect(button_cancel, "clicked", G_CALLBACK(on_click_button_cancel), (gpointer)window);


gtk_widget_show_all(window);
gtk_main();
return 0;
}

猜你喜欢

转载自blog.csdn.net/wangmeng0804/article/details/52873462