GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

A GTK+ Clock Using g_timout_add (C and Libglade)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 407
Location: Portland, OR USA

PostPosted: Tue Oct 30, 2007 6:07 pm    Post subject: A GTK+ Clock Using g_timout_add (C and Libglade) Reply with quote

Almost forgot I had this one laying around.

main.c
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * 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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?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: (Plaintext)
1
2
3
cc -o example -Wall -g main.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
./example


Last edited by Micah Carrick on Fri Nov 30, 2007 10:04 pm; edited 2 times in total
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 372
Location: State College, Pennsylvania

PostPosted: Tue Oct 30, 2007 8:04 pm    Post subject: Reply with quote

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!)
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 407
Location: Portland, OR USA

PostPosted: Tue Oct 30, 2007 8:35 pm    Post subject: Reply with quote

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).
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP