ubuntn下用Gtk做的时钟

#include <gtk/gtk.h>
#include <time.h>
#include <string.h>
#include <linux/rtc.h>
char buf[100];
int x,y;
time_t timep;
struct tm *p;
int date_c[3],time_c[3],i;
int count = 0, hh = 0;
GtkWidget *da;
void change_pix();
static void destroy( GtkWidget *widget, gpointer data )
{
    gtk_main_quit ();
}
static gboolean my_expose( GtkWidget *da1, GdkEventExpose *event, gpointer data )
{
    GdkGC *gc1, *gc2, *gc;  /*gc1为矩形,gc2为文字*/
    GdkColor color;
    gc1 = gdk_gc_new( da1->window );
    color.red = 30000;
    color.green = 0;
    color.blue = 30000;
    gdk_gc_set_rgb_fg_color(gc1, &color );
    gc2 = gdk_gc_new( da1->window );
    color.red = 65535;
    color.green = 0;
    color.blue = 65535;
    gdk_gc_set_rgb_fg_color( gc2, &color );
    gc = gc1;
    if(count > 0)
  gdk_draw_rectangle( da1->window, gc, TRUE, x, y, 20, 20 ); 
 PangoLayout *layout =
        gtk_widget_create_pango_layout( da1, buf);
    PangoFontDescription *fontdesc =
        pango_font_description_from_string( "Luxi Mono 12" );
    pango_layout_set_font_description( layout, fontdesc );
    gdk_draw_layout( da1->window, gc2, 50, 55, layout );
    pango_font_description_free( fontdesc );
    g_object_unref( layout );
    g_object_unref( G_OBJECT(gc1) );
    g_object_unref( G_OBJECT(gc2) );
  
    return TRUE;
}
void fresh_time()
{
    GdkGC *gc2;
 GdkColor color;
 time(&timep);
 p = localtime(&timep);
 time_c[0]=p->tm_hour;
 time_c[1]=p->tm_min;
 time_c[2]=p->tm_sec;
    sprintf(buf,"%d : %d : %d",time_c[0],time_c[1],time_c[2]);
 gdk_window_invalidate_rect(da->window,NULL,FALSE);
}

void change_pix()
{
 //gtk_timeout_remove(hh);
 if(count < 3){
  if((time_c[0] > 9) && (time_c[1] > 9))
   x = x + 50;
  else if((time_c[0] > 9) ||(time_c[1] > 9)){
   if(count == 1)
    x = x + 40;
   else if(count == 2)
    x = x + 50;
   else
    x = x + 50;
  }else{
   if(count == 1)
    x = x + 40;
   else if(count == 2)
    x = x + 40;
   else
    x = x + 50;
  }
 }
 count = count + 1;
 if(count == 4){
  x = 0;
  count = 0;
  if(hh == 0)
   hh =gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL);
  printf("change_pix hh = %d/n",hh);
 }
 gdk_window_invalidate_rect(da->window,NULL,FALSE);
 printf("count = %d/n",count);
}
void add_value()
{
 if(count == 0)return ;
 //gtk_timeout_remove(gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL));
 if(hh != 0){
  gtk_timeout_remove(hh);
  hh = 0;
 }
 printf("add_value hh = %d/n",hh);
 if(count == 1){
  if(time_c[0] < 23)time_c[0] = time_c[0] + 1;
  else time_c[0] = 0;
 }else if(count == 2){
  if(time_c[1] < 59)time_c[1] = time_c[1] + 1;
  else{
   time_c[1] = 0;
   time_c[0]++;
  }
 }else if(count == 3){
  if(time_c[2] < 59)time_c[2] = time_c[2] + 1;
  else{
   time_c[2] = 0;
   time_c[1]++;
  }
 }
    sprintf(buf,"%d : %d : %d",time_c[0],time_c[1],time_c[2]);
    gdk_window_invalidate_rect(da->window,NULL,FALSE);
    printf("time_c[0] = %d, time_c[1] = %d/n",time_c[0],time_c[1]);
}
void sub_value()
{
 if(count == 0)return ;
 //gtk_timeout_remove(gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL));
 if(hh != 0){
  gtk_timeout_remove(hh);
  hh = 0;
 }
 printf("sub_value hh = %d/n",hh);
 if(count == 1){
  if(time_c[0] > 0)time_c[0] = time_c[0] - 1;
  else time_c[0] = 23;
 }else if(count == 2){
  if(time_c[1] > 0)time_c[1] = time_c[1] - 1;
  else{
   time_c[1] = 59;
   time_c[0]--;
  }
 }else if(count == 3){
  if(time_c[2] > 0)time_c[2] = time_c[2] - 1;
  else{
   time_c[2] = 59;
   time_c[1]--;
  }
 }
    sprintf(buf,"%d : %d : %d",time_c[0],time_c[1],time_c[2]);
    gdk_window_invalidate_rect(da->window,NULL,FALSE);
    printf("time_c[0] = %d, time_c[1] = %d/n",time_c[0],time_c[1]);
}
void date_time()
{
 struct rtc_time rtc_tm;
 char temp[32];
 rtc_tm.tm_hour = time_c[0];
 rtc_tm.tm_min = time_c[1];
 rtc_tm.tm_sec = time_c[2];
 rtc_tm.tm_mday = date_c[2];
 rtc_tm.tm_year = date_c[0] - 1900;
 rtc_tm.tm_mon = date_c[1] - 1;
 rtc_tm.tm_wday = 0;
 rtc_tm.tm_isdst = 0;
 sprintf(temp,"date %02d%02d%02d%02d%04d",date_c[1],date_c[2],time_c[0],time_c[1],date_c[0]);
 system(temp);
 x = 0;
 count = 0;
    if(hh == 0)
  hh =gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL);
 printf("date_time hh = %d/n",hh);
    gdk_window_invalidate_rect(da->window,NULL,FALSE);
 
}
void cancel_time()
{
 x = 0;
 count = 0;
    if(hh == 0)
  hh =gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL);
 printf("cancel_time hh = %d/n",hh);
 time(&timep);
 p = localtime(&timep);
 time_c[0]=p->tm_hour;
 time_c[1]=p->tm_min;
 time_c[2]=p->tm_sec;
 date_c[0]=1900+p->tm_year;
 date_c[1]=1+p->tm_mon;
 date_c[2]=p->tm_mday;
    sprintf(buf,"%d : %d : %d",time_c[0],time_c[1],time_c[2]);
    gdk_window_invalidate_rect(da->window,NULL,FALSE);
}
int main( int  argc, char *argv[] )
{
    GtkWidget *window;
 GtkWidget *button;
 GtkWidget *button1;
 GtkWidget *button2;
 GtkWidget *button3;
 GtkWidget *button4;
 GtkWidget *vbox;
    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect( G_OBJECT (window), "destroy",
                G_CALLBACK (destroy), NULL );
    x = 0;
 y = 55;
 vbox = gtk_vbox_new(FALSE,0);
 gtk_container_add(GTK_CONTAINER(window),vbox);
 memset(buf, 0, sizeof(buf));
 time(&timep);
 p = localtime(&timep);
 time_c[0]=p->tm_hour;
 time_c[1]=p->tm_min;
 time_c[2]=p->tm_sec;
 date_c[0]=1900+p->tm_year;
 date_c[1]=1+p->tm_mon;
 date_c[2]=p->tm_mday;
    sprintf(buf,"%d : %d : %d",time_c[0],time_c[1],time_c[2]);
 da = gtk_drawing_area_new ();
    /* set a minimum size */
    gtk_widget_set_size_request( da, 300, 300 );
    gtk_container_add( GTK_CONTAINER(vbox), da );

 button = gtk_button_new_with_label("Set");
 gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,TRUE,0);
 button1 = gtk_button_new_with_label("Add");
 gtk_box_pack_start(GTK_BOX(vbox),button1,FALSE,TRUE,0);
 button2 = gtk_button_new_with_label("Sub");
 gtk_box_pack_start(GTK_BOX(vbox),button2,FALSE,TRUE,0);
 button3 = gtk_button_new_with_label("Ok");
 gtk_box_pack_start(GTK_BOX(vbox),button3,FALSE,TRUE,0);
 button4 = gtk_button_new_with_label("Cancel");
 gtk_box_pack_start(GTK_BOX(vbox),button4,FALSE,TRUE,0);
    g_signal_connect( da, "expose_event",
             G_CALLBACK(my_expose), NULL );
    g_signal_connect( G_OBJECT(button), "clicked",
             G_CALLBACK(change_pix), NULL );
    g_signal_connect( G_OBJECT(button1), "clicked",
             G_CALLBACK(add_value), NULL );
    g_signal_connect( G_OBJECT(button2), "clicked",
             G_CALLBACK(sub_value), NULL );
    g_signal_connect( G_OBJECT(button3), "clicked",
             G_CALLBACK(date_time), NULL );
    g_signal_connect( G_OBJECT(button4), "clicked",
             G_CALLBACK(cancel_time), NULL );
 hh =gtk_timeout_add(1000,(GtkFunction)fresh_time,NULL);
 printf("main hh = %d/n",hh);
 gtk_widget_show_all( window ); //simply show all widgets
    gtk_main();
   
    return 0;
}

你可能感兴趣的:(ubuntn下用Gtk做的时钟)