Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Simple timer

By: a guest on Nov 20th, 2011  |  syntax: C  |  size: 1.65 KB  |  views: 223  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <gtk/gtk.h>
  4.  
  5. static void _quit_cb (GtkWidget *button, gpointer data);
  6. static gboolean _label_update(gpointer data);
  7.  
  8. static void
  9. _quit_cb (GtkWidget *button, gpointer data)
  10. {
  11.     (void)button; (void)data; /*Avoid compiler warnings*/
  12.     gtk_main_quit();
  13.     return;
  14. }
  15.  
  16. static gboolean
  17. _label_update(gpointer data)
  18. {
  19.     GtkLabel *label = (GtkLabel*)data;
  20.     time_t rawtime;
  21.     struct tm * timeinfo;
  22.     time ( &rawtime );
  23.     timeinfo = localtime ( &rawtime );
  24.     gtk_label_set_label(label, asctime (timeinfo) );
  25.     /* Restart the timer */
  26.     return TRUE;
  27.  
  28. }
  29.  
  30. int main(void)
  31. {
  32.     GtkWidget *window;
  33.     GtkWidget *vbox;
  34.     GtkWidget *quit_button;
  35.     GtkWidget *label;
  36.  
  37.     gtk_init(NULL, NULL);
  38.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  39.     gtk_widget_set_size_request(window, 200, 100);
  40.     g_signal_connect (G_OBJECT (window), "destroy",
  41.                     G_CALLBACK (gtk_main_quit),
  42.                     NULL);
  43.     vbox = gtk_vbox_new (FALSE, 2);
  44.     gtk_container_add(GTK_CONTAINER(window), vbox);
  45.  
  46.     quit_button = gtk_button_new_with_label("Quit");
  47.     g_signal_connect(G_OBJECT(quit_button), "clicked", G_CALLBACK(_quit_cb), NULL);
  48.    
  49.     time_t rawtime;
  50.     struct tm * timeinfo;
  51.     time ( &rawtime );
  52.     timeinfo = localtime ( &rawtime );
  53.     label = gtk_label_new(asctime (timeinfo) );
  54.     /* 250ms update */
  55.     g_timeout_add(250, _label_update, label);
  56.  
  57.     gtk_box_pack_start (GTK_BOX(vbox), label, 0, 0, 0);
  58.     gtk_box_pack_start (GTK_BOX (vbox), quit_button, 0, 0, 0);
  59.    
  60.     gtk_widget_show_all(window);
  61.    
  62.     gtk_main();
  63.     return 0;
  64. }
  65.