[gnome-games] libgames-support: Add a way to inhibit the GamesClock timeout
- From: Christian Persch <chpe src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games] libgames-support: Add a way to inhibit the GamesClock timeout
- Date: Sun, 12 Jul 2009 19:21:17 +0000 (UTC)
commit c2b003ffc93ebafaa17f3b439e18b6340bb8bdd3
Author: Christian Persch <chpe gnome org>
Date: Sun Jul 12 20:46:02 2009 +0200
libgames-support: Add a way to inhibit the GamesClock timeout
This will be used to remove the timeout while the clock is invisible
(since the statusbar it's in is invisible itself).
libgames-support/games-clock.c | 190 +++++++++++++++++++++++++++++-----------
libgames-support/games-clock.h | 14 ++-
2 files changed, 148 insertions(+), 56 deletions(-)
---
diff --git a/libgames-support/games-clock.c b/libgames-support/games-clock.c
index aa1cb14..66e3389 100644
--- a/libgames-support/games-clock.c
+++ b/libgames-support/games-clock.c
@@ -15,120 +15,208 @@
G_DEFINE_TYPE (GamesClock, games_clock, GTK_TYPE_LABEL)
static void
-games_clock_finalize (GObject * object)
+clock_paint (GamesClock *clock_widget)
{
- GamesClock *clock_widget = GAMES_CLOCK (object);
+ char string[32];
+ time_t seconds;
+ int secs;
+ int mins;
+ int hours;
- if (clock_widget->timer_id != 0) {
- g_source_remove (clock_widget->timer_id);
- clock_widget->timer_id = 0;
- }
+ seconds = games_clock_get_seconds (clock_widget);
+ hours = seconds / 3600;
+ secs = seconds - hours * 3600;
+ mins = secs / 60;
+ secs = secs - mins * 60;
- G_OBJECT_CLASS (games_clock_parent_class)->finalize (object);
+ /* FIXMEchpe: i18n! */
+ g_snprintf (string, sizeof (string), "%.2d:%.2d:%.2d", hours, mins, secs);
+
+ gtk_label_set_text (GTK_LABEL (clock_widget), string);
}
-static void
-games_clock_class_init (GamesClockClass * klass)
+static gboolean
+games_clock_update (GamesClock *clock_widget)
{
- GObjectClass *object_class = (GObjectClass *) klass;
+ clock_paint (clock_widget);
- object_class->finalize = games_clock_finalize;
+ return TRUE;
}
static void
-games_clock_init (GamesClock *clock_widget)
+games_clock_start_timer (GamesClock *clock_widget)
{
- clock_widget->timer_id = 0;
- clock_widget->seconds = 0;
+ if (clock_widget->update_timeout_id != 0)
+ return;
- gtk_label_set_text (GTK_LABEL (clock_widget), "00:00:00");
+ clock_widget->update_timeout_id =
+#if GLIB_CHECK_VERSION (2, 14, 0)
+ g_timeout_add_seconds (1, (GSourceFunc) games_clock_update, clock_widget);
+#else
+ g_timeout_add (1000, (GSourceFunc) games_clock_update, clock_widget);
+#endif
}
-GtkWidget *
-games_clock_new (void)
+static void
+games_clock_stop_timer (GamesClock *clock_widget)
{
- return g_object_new (GAMES_TYPE_CLOCK, NULL);
+ if (clock_widget->update_timeout_id != 0) {
+ g_source_remove (clock_widget->update_timeout_id);
+ clock_widget->update_timeout_id = 0;
+ }
}
static void
-clock_paint (GamesClock *clock_widget)
+games_clock_finalize (GObject * object)
{
- char string[32];
- int secs;
- int mins;
- int hours;
+ GamesClock *clock_widget = GAMES_CLOCK (object);
- hours = clock_widget->seconds / 3600;
- secs = clock_widget->seconds - hours * 3600;
- mins = secs / 60;
- secs = secs - mins * 60;
+ games_clock_stop_timer (clock_widget);
- /* FIXMEchpe: i18n! */
- g_snprintf (string, sizeof (string), "%.2d:%.2d:%.2d", hours, mins, secs);
-
- gtk_label_set_text (GTK_LABEL (clock_widget), string);
+ G_OBJECT_CLASS (games_clock_parent_class)->finalize (object);
}
+static void
+games_clock_class_init (GamesClockClass * klass)
+{
+ GObjectClass *object_class = (GObjectClass *) klass;
+
+ object_class->finalize = games_clock_finalize;
+}
-static gboolean
-games_clock_update (GamesClock *clock_widget)
+static void
+games_clock_init (GamesClock *clock_widget)
{
- clock_widget->seconds++;
+ clock_widget->update_timeout_id = 0;
+ clock_widget->start_time = clock_widget->stop_time = 0;
+ clock_widget->started = FALSE;
+ clock_widget->update = TRUE;
- clock_paint (clock_widget);
+ /* FIXMEchpe: call clock_paint() instead */
+ gtk_label_set_text (GTK_LABEL (clock_widget), "00:00:00");
+}
- return TRUE;
+GtkWidget *
+games_clock_new (void)
+{
+ return g_object_new (GAMES_TYPE_CLOCK, NULL);
}
+/**
+ * games_clock_start:
+ * @clock_widget:
+ *
+ * Records the current time as the start time, and starts
+ * updating the clock if updates are enabled (see
+ * games_clock_set_update()).
+ */
void
games_clock_start (GamesClock *clock_widget)
{
g_return_if_fail (GAMES_IS_CLOCK (clock_widget));
- if (clock_widget->timer_id != 0)
- return;
+ clock_widget->started = TRUE;
- clock_widget->timer_id = g_timeout_add (1000,
- (GSourceFunc) games_clock_update, clock_widget);
+ if (clock_widget->update)
+ games_clock_start_timer (clock_widget);
}
+/**
+ * games_clock_stop:
+ * @clock_widget:
+ *
+ * Records the current time as the stop time, and stops
+ * updating the clock.
+ */
void
games_clock_stop (GamesClock *clock_widget)
{
g_return_if_fail (GAMES_IS_CLOCK (clock_widget));
- if (clock_widget->timer_id == 0)
- return;
+ clock_widget->started = FALSE;
+ clock_widget->stop_time = time (NULL);
- g_source_remove (clock_widget->timer_id);
- clock_widget->timer_id = 0;
- clock_widget->stopped = clock_widget->seconds;
+ games_clock_stop_timer (clock_widget);
+ clock_paint (clock_widget);
}
+/**
+ * games_clock_reset:
+ * @clock_widget:
+ *
+ * Resets the time in @clock_widget to 0.
+ */
void
-games_clock_set_seconds (GamesClock *clock_widget,
- time_t seconds)
+games_clock_reset (GamesClock *clock_widget)
{
g_return_if_fail (GAMES_IS_CLOCK (clock_widget));
- clock_widget->seconds = seconds;
+ clock_widget->start_time = clock_widget->stop_time = time (NULL);
+
clock_paint (clock_widget);
}
+/**
+ * games_clock_get_seconds:
+ * @clock_widget:
+ *
+ * Returns: the elapsed time in @clock_widget
+ */
time_t
games_clock_get_seconds (GamesClock *clock_widget)
{
- g_return_val_if_fail ( GAMES_IS_CLOCK (clock_widget), 0);
+ g_return_val_if_fail (GAMES_IS_CLOCK (clock_widget), 0);
- return clock_widget->seconds;
+ if (clock_widget->started)
+ return time (NULL) - clock_widget->start_time;
+ else
+ return clock_widget->stop_time - clock_widget->start_time;
}
+/**
+ * games_clock_add_seconds:
+ * @clock_widget:
+ * @seconds:
+ *
+ * Adds @seconds to the reported elapsed time in @clock_widget.
+ */
void
games_clock_add_seconds (GamesClock *clock_widget,
time_t seconds)
{
g_return_if_fail (GAMES_IS_CLOCK (clock_widget));
- clock_widget->seconds += seconds;
+ if (!clock_widget->started)
+ g_warning ("Clock not started, cannot add seconds!\n");
+
+ clock_widget->start_time -= seconds;
clock_paint (clock_widget);
}
+
+/**
+ * games_clock_set_update:
+ * @clock_widget:
+ *
+ * Sets whether the clock will automatically update itself every second
+ * to display the currently elapsed time.
+ *
+ * Use this e.g. to disable updates while the clock is invisible.
+ */
+void
+games_clock_set_update (GamesClock *clock_widget,
+ gboolean do_update)
+{
+ g_return_if_fail (GAMES_IS_CLOCK (clock_widget));
+
+ do_update = do_update != FALSE;
+ if (do_update == clock_widget->update)
+ return;
+
+ clock_widget->update = do_update;
+ if (do_update) {
+ games_clock_start_timer (clock_widget);
+ clock_paint (clock_widget);
+ } else {
+ games_clock_stop_timer (clock_widget);
+ }
+}
diff --git a/libgames-support/games-clock.h b/libgames-support/games-clock.h
index 58263e1..1191bda 100644
--- a/libgames-support/games-clock.h
+++ b/libgames-support/games-clock.h
@@ -26,10 +26,11 @@ G_BEGIN_DECLS
typedef struct _GamesClock {
GtkLabel label;
- guint timer_id;
-
- time_t seconds;
- time_t stopped;
+ guint update_timeout_id;
+ gboolean update;
+ gboolean started;
+ time_t start_time;
+ time_t stop_time;
} GamesClock;
typedef GtkLabelClass GamesClockClass;
@@ -38,11 +39,14 @@ GType games_clock_get_type (void);
GtkWidget *games_clock_new (void);
void games_clock_start (GamesClock *clock_widget);
void games_clock_stop (GamesClock *clock_widget);
+void games_clock_reset (GamesClock *clock_widget);
+time_t games_clock_get_seconds (GamesClock *clock_widget);
void games_clock_set_seconds (GamesClock *clock_widget,
time_t seconds);
-time_t games_clock_get_seconds (GamesClock *clock_widget);
void games_clock_add_seconds (GamesClock *clock_widget,
time_t seconds);
+void games_clock_set_update (GamesClock *clock_widget,
+ gboolean do_update);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]