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.gladeCode:
<?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 runCode:
cc -o example -Wall -g main.c `pkg-config --cflags --libs gtk+-2.0 libglade-2.0`
./example