SHARE
TWEET

GladeTime

alljumbledup Sep 12th, 2017 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <gtk/gtk.h>
  2.  
  3. typedef struct {
  4.     GtkWidget *w_lbl_time;
  5. } app_widgets;
  6.  
  7. gboolean timer_handler(app_widgets *widgets);
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     GtkBuilder      *builder;
  12.     GtkWidget       *window;
  13.     app_widgets        *widgets = g_slice_new(app_widgets);
  14.  
  15.     gtk_init(&argc, &argv);
  16.  
  17.     builder = gtk_builder_new();
  18.     gtk_builder_add_from_file (builder, "glade/window_main.glade", NULL);
  19.  
  20.     window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
  21.     widgets->w_lbl_time  = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_time"));
  22.     gtk_builder_connect_signals(builder, widgets);
  23.     g_object_unref(builder);
  24.    
  25.     // start 1 second timer
  26.     g_timeout_add_seconds(1, (GSourceFunc)timer_handler, widgets);
  27.  
  28.     gtk_widget_show(window);                
  29.     gtk_main();
  30.     g_slice_free(app_widgets, widgets);
  31.  
  32.     return 0;
  33. }
  34.  
  35. // handler for the 1 second timer tick
  36. gboolean timer_handler(app_widgets *widgets)
  37. {
  38.     GDateTime *date_time;
  39.     gchar *dt_format;
  40.  
  41.     date_time = g_date_time_new_now_local();                        // get local time
  42.     dt_format = g_date_time_format(date_time, "%H:%M:%S");            // 24hr time format
  43.     gtk_label_set_text(GTK_LABEL(widgets->w_lbl_time), dt_format);    // update label
  44. printf("%s\n", dt_format);
  45.     g_free (dt_format);
  46.    
  47.     return TRUE;
  48. }
  49.  
  50. // called when window is closed
  51. void on_window_main_destroy()
  52. {
  53.     gtk_main_quit();
  54. }
  55.  
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <!-- Generated with glade 3.18.3 -->
  58. <interface>
  59.   <requires lib="gtk+" version="3.12"/>
  60.   <object class="GtkWindow" id="window_main">
  61.     <property name="can_focus">False</property>
  62.     <property name="title" translatable="yes">Time</property>
  63.     <signal name="destroy" handler="on_window_main_destroy" swapped="no"/>
  64.     <child>
  65.       <object class="GtkBox" id="box1">
  66.         <property name="visible">True</property>
  67.         <property name="can_focus">False</property>
  68.         <property name="margin_left">10</property>
  69.         <property name="margin_right">10</property>
  70.         <property name="margin_top">10</property>
  71.         <property name="margin_bottom">10</property>
  72.         <property name="spacing">7</property>
  73.         <property name="homogeneous">True</property>
  74.         <child>
  75.           <object class="GtkLabel" id="label1">
  76.             <property name="visible">True</property>
  77.             <property name="can_focus">False</property>
  78.             <property name="label" translatable="yes">Current Time:</property>
  79.             <attributes>
  80.               <attribute name="weight" value="bold"/>
  81.             </attributes>
  82.           </object>
  83.           <packing>
  84.             <property name="expand">False</property>
  85.             <property name="fill">True</property>
  86.             <property name="position">0</property>
  87.           </packing>
  88.         </child>
  89.         <child>
  90.           <object class="GtkLabel" id="lbl_time">
  91.             <property name="visible">True</property>
  92.             <property name="can_focus">False</property>
  93.             <property name="label" translatable="yes">Time</property>
  94.             <attributes>
  95.               <attribute name="weight" value="bold"/>
  96.             </attributes>
  97.           </object>
  98.           <packing>
  99.             <property name="expand">False</property>
  100.             <property name="fill">True</property>
  101.             <property name="position">1</property>
  102.           </packing>
  103.         </child>
  104.       </object>
  105.     </child>
  106.   </object>
  107. </interface>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top