GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 7:47 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: A GTK+ Clock Using g_timout_add (C and Libglade)
PostPosted: Tue Oct 30, 2007 6:07 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Almost forgot I had this one laying around.

main.c
Code:
/*
* main.c
* Copyright (C) Micah Carrick 2006 <email@micahcarrick.com>
*
* main.c is free software.
*
* You may redistribute it and/or modify it under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* main.c is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with main.c.  If not, write to:
*    The Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor
*    Boston, MA  02110-1301, USA.
*/

/*
cc -o example -Wall -g main.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
*/

#include <gtk/gtk.h>
#include <glade/glade.h>
#include <time.h>

/* glade interface file is in same path as the executable */
#define GLADE_FILE "gui.glade"
     
/* signal callbacks */
void on_window1_destroy (GtkWidget *widget, gpointer user_data);
void on_quit_activate (GtkMenuItem *item, gpointer user_data);

/* user functions */
gboolean update_clock (gpointer label);
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *main_window; 
        GtkWidget *clock_label;
        GladeXML *gxml;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /*
        create an instance of the GladeXML object and build widgets within
        the window1 root node.
        */
        gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
       
        /* get needed widgets from the glade XML file */
        main_window = glade_xml_get_widget (gxml, "window1");
        clock_label = glade_xml_get_widget (gxml, "label1");
       
        /* connect signals */
        glade_xml_signal_connect (gxml, "on_window1_destroy",
                G_CALLBACK(on_window1_destroy));
       
        glade_xml_signal_connect (gxml, "on_quit_activate",
                G_CALLBACK(on_quit_activate));               
               
        /* show the main window */
        gtk_widget_show (main_window);
       
        /* set timeout */
        g_timeout_add (500, update_clock, (gpointer)clock_label);
       
        /* begin main GTK loop */
        gtk_main ();
       
        return 0;     
}

void
on_window1_destroy (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

void
on_quit_activate (GtkMenuItem *item, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

gboolean
update_clock(gpointer label)
{
        /* the GtkLabel is passed in as user data */
       
        time_t simple_time;
        struct tm *time_info;         
        gchar *str = g_new(gchar, 25);  /* allocate 25 characters for time */
       
        /* get simple time and convert it into a tm structure */
        time (&simple_time);
        time_info = localtime(&simple_time);
       
        /* %X = Preferred time of day representation for current locale. */
        strftime(str, 25, "%X", time_info);     
       
        /* update the label */
        gtk_label_set_text(GTK_LABEL(label), str);
       
        g_free(str);    /* free memory*/

        return TRUE;
}


gui.glade
Code:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">

<glade-interface>

<widget class="GtkWindow" id="window1">
  <property name="visible">True</property>
  <property name="title" translatable="yes">Clock</property>
  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  <property name="window_position">GTK_WIN_POS_NONE</property>
  <property name="modal">False</property>
  <property name="default_width">200</property>
  <property name="default_height">100</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <property name="focus_on_map">True</property>
  <property name="urgency_hint">False</property>
  <signal name="destroy" handler="on_window1_destroy" last_modification_time="Thu, 09 Mar 2006 23:35:37 GMT"/>

  <child>
    <widget class="GtkVBox" id="vbox1">
      <property name="visible">True</property>
      <property name="homogeneous">False</property>
      <property name="spacing">0</property>

      <child>
   <widget class="GtkMenuBar" id="menubar1">
     <property name="visible">True</property>
     <property name="pack_direction">GTK_PACK_DIRECTION_LTR</property>
     <property name="child_pack_direction">GTK_PACK_DIRECTION_LTR</property>

     <child>
       <widget class="GtkMenuItem" id="menuitem1">
         <property name="visible">True</property>
         <property name="label" translatable="yes">_File</property>
         <property name="use_underline">True</property>

         <child>
      <widget class="GtkMenu" id="menuitem1_menu">

        <child>
          <widget class="GtkImageMenuItem" id="quit">
            <property name="visible">True</property>
            <property name="label">gtk-quit</property>
            <property name="use_stock">True</property>
            <signal name="activate" handler="on_quit_activate" last_modification_time="Sun, 09 Jul 2006 20:56:30 GMT"/>
          </widget>
        </child>
      </widget>
         </child>
       </widget>
     </child>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">False</property>
     <property name="fill">False</property>
   </packing>
      </child>

      <child>
   <widget class="GtkLabel" id="label1">
     <property name="visible">True</property>
     <property name="label" translatable="yes">Label1</property>
     <property name="use_underline">False</property>
     <property name="use_markup">False</property>
     <property name="justify">GTK_JUSTIFY_LEFT</property>
     <property name="wrap">False</property>
     <property name="selectable">False</property>
     <property name="xalign">0.5</property>
     <property name="yalign">0.5</property>
     <property name="xpad">0</property>
     <property name="ypad">0</property>
     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
     <property name="width_chars">-1</property>
     <property name="single_line_mode">False</property>
     <property name="angle">0</property>
   </widget>
   <packing>
     <property name="padding">0</property>
     <property name="expand">True</property>
     <property name="fill">True</property>
   </packing>
      </child>
    </widget>
  </child>
</widget>

</glade-interface>


Compile and run
Code:
cc -o example -Wall -g main.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
./example

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Last edited by Micah Carrick on Fri Nov 30, 2007 10:04 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 8:04 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
People should note that Micah uses time() to keep track of the time, not just a timeout with a delay of 1000 ms. The next timeout is not run until 1000 ms after the previous finishes, so the delay from running the timeout adds to the total delay! (Micah implements this correctly!)

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 8:35 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Good point. As a rule, don't use g_timout_xx functions for precise timing. From the API:
Quote:
Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group