summaryrefslogtreecommitdiff
path: root/src/plugins/skipto/totem-time-entry.c
blob: bd632b4fce1624c84bbde0fbe28d7e80b709b49a (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * Copyright (C) 2008 Philip Withnall <philip@tecnocode.co.uk>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 * The Totem project hereby grant permission for non-gpl compatible GStreamer
 * plugins to be used and distributed together with GStreamer and Totem. This
 * permission are above and beyond the permissions granted by the GPL license
 * Totem is covered by.
 *
 * Monday 7th February 2005: Christian Schaller: Add excemption clause.
 * See license_change file for details.
 *
 * Author: Philip Withnall <philip@tecnocode.co.uk>
 */

#include "config.h"
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>

#include "totem-time-helpers.h"
#include "totem-time-entry.h"

static void dispose (GObject *object);
static gboolean output_cb (GtkSpinButton *self, gpointer user_data);
static gint input_cb (GtkSpinButton *self, gdouble *new_value, gpointer user_data);
static void notify_adjustment_cb (TotemTimeEntry *self, GParamSpec *pspec, gpointer user_data);
static void changed_cb (GtkAdjustment *adjustment, TotemTimeEntry *self);

struct _TotemTimeEntry {
	GtkSpinButton parent;
	GtkAdjustment *adjustment;
	gulong adjustment_changed_signal;
};

G_DEFINE_TYPE (TotemTimeEntry, totem_time_entry, GTK_TYPE_SPIN_BUTTON)

static gint64
totem_string_to_time (const char *time_string)
{
	int sec, min, hour, args;

	args = sscanf (time_string, C_("long time format", "%d:%02d:%02d"), &hour, &min, &sec);

	if (args == 3) {
		/* Parsed all three arguments successfully */
		return (hour * (60 * 60) + min * 60 + sec) * 1000;
	} else if (args == 2) {
		/* Only parsed the first two arguments; treat hour and min as min and sec, respectively */
		return (hour * 60 + min) * 1000;
	} else if (args == 1) {
		/* Only parsed the first argument; treat hour as sec */
		return hour * 1000;
	} else {
		/* Error! */
		return -1;
	}
}

static void
totem_time_entry_class_init (TotemTimeEntryClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = dispose;
}

static void
totem_time_entry_init (TotemTimeEntry *self)
{
	/* Connect to signals */
	g_signal_connect (self, "output", G_CALLBACK (output_cb), NULL);
	g_signal_connect (self, "input", G_CALLBACK (input_cb), NULL);
	g_signal_connect (self, "notify::adjustment", G_CALLBACK (notify_adjustment_cb), NULL);
}

static void
dispose (GObject *object)
{
	TotemTimeEntry *entry = TOTEM_TIME_ENTRY (object);

	if (entry->adjustment != NULL) {
		g_signal_handler_disconnect (entry->adjustment, entry->adjustment_changed_signal);
		g_object_unref (entry->adjustment);
	}
	entry->adjustment = NULL;

	G_OBJECT_CLASS (totem_time_entry_parent_class)->dispose (object);
}

GtkWidget *
totem_time_entry_new (GtkAdjustment *adjustment, gdouble climb_rate)
{
	return g_object_new (TOTEM_TYPE_TIME_ENTRY,
			     "adjustment", adjustment,
			     "climb-rate", climb_rate,
			     "digits", 0,
			     "numeric", FALSE,
			     NULL);
}

static gboolean
output_cb (GtkSpinButton *self, gpointer user_data)
{
	gchar *text;

	text = totem_time_to_string ((gint64) gtk_spin_button_get_value (self) * 1000, TOTEM_TIME_FLAG_NONE);
	gtk_entry_set_text (GTK_ENTRY (self), text);
	g_free (text);

	return TRUE;
}

static gint
input_cb (GtkSpinButton *self, gdouble *new_value, gpointer user_data)
{
	gint64 val;

	val = totem_string_to_time (gtk_entry_get_text (GTK_ENTRY (self)));
	if (val == -1)
		return GTK_INPUT_ERROR;

	*new_value = val / 1000;
	return TRUE;
}

static void
notify_adjustment_cb (TotemTimeEntry *self, GParamSpec *pspec, gpointer user_data)
{
	if (self->adjustment != NULL) {
		g_signal_handler_disconnect (self->adjustment, self->adjustment_changed_signal);
		g_object_unref (self->adjustment);
	}

	self->adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (self));
	self->adjustment_changed_signal = 0;

	if (self->adjustment != NULL) {
		g_object_ref (self->adjustment);
		self->adjustment_changed_signal = g_signal_connect (self->adjustment, "changed", G_CALLBACK (changed_cb), self);
	}
}

static void
changed_cb (GtkAdjustment *adjustment, TotemTimeEntry *self)
{
	gchar *time_string;
	guint upper, width;

	/* Set the width of the entry according to the length of the longest string it'll now accept */
	upper = (guint) gtk_adjustment_get_upper (adjustment); /* in seconds */

	time_string = totem_time_to_string (((gint64) upper) * 1000, TOTEM_TIME_FLAG_NONE);
	width = strlen (time_string);
	g_free (time_string);

	gtk_entry_set_width_chars (GTK_ENTRY (self), width);
}