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;
} |