--- /dev/null
+Trivial Tea Timer (c) 2002 Martin Mares <mj@ucw.cz>
+
+
+This program is free software; you can 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.
+
+This program 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.
+
+The official text of the GNU General Public License can be found at
+http://www.gnu.org/.
+
+
+If you find any bugs in this package or if you have any ideas how to
+improve it, I will be glad to hear about them at <mj@ucw.cz>
--- /dev/null
+/*
+ * Trivial Tea Timer
+ *
+ * (c) 2002 Martin Mares <mj@ucw.cz>
+ *
+ * GPL'ed
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+#include <gtk/gtk.h>
+
+#define UNUSED __attribute__((unused))
+
+static guint second_timer;
+static unsigned char old_text[8];
+static GtkWidget *win, *hbox1, *timebox, *togglebutton1;
+static time_t alarm_time;
+
+static void
+it_tolls_for_thee(void)
+{
+ gdk_beep();
+}
+
+static gint
+on_second_timeout(gpointer data UNUSED)
+{
+ char buf[8];
+ time_t now = time(NULL);
+ int delta = alarm_time - now;
+ char *sign = "";
+
+ if (delta < 0)
+ {
+ sign = "-";
+ delta = -delta;
+ }
+ if (delta >= 6000)
+ delta = 5999;
+ sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
+ gtk_entry_set_text(GTK_ENTRY(timebox), buf);
+ if (now >= alarm_time)
+ it_tolls_for_thee();
+ return 1;
+}
+
+static gint
+on_timebox_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
+{
+ if (!strcmp(ev->string, "\r"))
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
+ else if (!strcmp(ev->string, "\033"))
+ gtk_main_quit();
+ return FALSE;
+}
+
+static int
+parse_time(char *c)
+{
+ int t = 0;
+
+ while (*c && *c != ':')
+ {
+ if (*c >= '0' && *c <= '9')
+ t = 10*t + *c++ - '0';
+ else
+ return -1;
+ }
+ if (*c)
+ {
+ int m = 0;
+ c++;
+ while (*c)
+ {
+ if (*c >= '0' && *c <= '9')
+ m = 10*m + *c++ - '0';
+ else
+ return -1;
+ }
+ t = 60*t + m;
+ }
+ if (t >= 6000)
+ return -1;
+ return t;
+}
+
+static void
+on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
+{
+ if (togglebutton->active)
+ {
+ int t;
+ strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
+ t = parse_time(old_text);
+ if (t < 0)
+ {
+ gtk_toggle_button_set_active(togglebutton, 0);
+ return;
+ }
+ alarm_time = time(NULL) + t;
+ gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
+ on_second_timeout(NULL);
+ second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
+ }
+ else
+ {
+ if (second_timer)
+ {
+ gtk_timeout_remove(second_timer);
+ second_timer = 0;
+ }
+ gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
+ gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
+ gtk_widget_grab_focus (timebox);
+ }
+}
+
+static void
+on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
+{
+ gtk_main_quit();
+}
+
+static void
+open_window(void)
+{
+ win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title (GTK_WINDOW (win), "Tea Timer");
+ gtk_window_set_policy (GTK_WINDOW (win), TRUE, TRUE, TRUE);
+
+ hbox1 = gtk_hbox_new (FALSE, 0);
+ gtk_widget_show (hbox1);
+ gtk_container_add (GTK_CONTAINER (win), hbox1);
+
+ timebox = gtk_entry_new_with_max_length (6);
+ gtk_widget_show (timebox);
+ gtk_box_pack_start (GTK_BOX (hbox1), timebox, TRUE, TRUE, 0);
+ gtk_entry_set_text (GTK_ENTRY (timebox), "00:00");
+
+ togglebutton1 = gtk_toggle_button_new_with_label ("Run");
+ gtk_widget_show (togglebutton1);
+ gtk_box_pack_start (GTK_BOX (hbox1), togglebutton1, FALSE, FALSE, 0);
+
+ gtk_signal_connect(GTK_OBJECT (win), "remove", GTK_SIGNAL_FUNC (on_window_remove), NULL);
+ gtk_signal_connect(GTK_OBJECT (timebox), "key_press_event", GTK_SIGNAL_FUNC (on_timebox_key), NULL);
+ gtk_signal_connect(GTK_OBJECT (togglebutton1), "toggled", GTK_SIGNAL_FUNC (on_togglebutton1_toggled), NULL);
+
+ gtk_widget_grab_focus (timebox);
+
+ gtk_widget_show(win);
+}
+
+int
+main(int argc, char **argv)
+{
+ gtk_set_locale();
+ gtk_init(&argc, &argv);
+ open_window();
+ if (argc > 1)
+ {
+ gtk_entry_set_text(GTK_ENTRY(timebox), argv[1]);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);
+ }
+ gtk_main();
+ return 0;
+}