2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * SECTION:hildon-seekbar
27 * @short_description: A widget used to identify a place from a content.
29 * HildonSeekbar allows seeking in media with a range widget. It
30 * supports for setting or getting the length (total time) of the media,
31 * the position within it and the fraction (maximum position in a
32 * stream/the amount currently downloaded). The position is clamped
33 * between zero and the total time, or zero and the fraction in case of
37 #undef HILDON_DISABLE_DEPRECATED
46 #include <gdk/gdkkeysyms.h>
48 #include "hildon-seekbar.h"
49 #include "hildon-seekbar-private.h"
51 static GtkScaleClass* parent_class = NULL;
54 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class);
57 hildon_seekbar_init (HildonSeekbar *seekbar);
60 hildon_seekbar_set_property (GObject *object,
66 hildon_seekbar_get_property (GObject *object,
72 hildon_seekbar_size_request (GtkWidget *widget,
73 GtkRequisition *event);
76 hildon_seekbar_size_allocate (GtkWidget *widget,
77 GtkAllocation *allocation);
80 hildon_seekbar_button_press_event (GtkWidget *widget,
81 GdkEventButton *event);
84 hildon_seekbar_button_release_event (GtkWidget *widget,
85 GdkEventButton *event);
88 hildon_seekbar_keypress (GtkWidget *widget,
91 #define MINIMUM_WIDTH 115
93 #define DEFAULT_HEIGHT 58
95 #define TOOL_MINIMUM_WIDTH 75
97 #define TOOL_DEFAULT_HEIGHT 40
99 #define DEFAULT_DISPLAYC_BORDER 10
101 #define BUFFER_SIZE 32
103 #define EXTRA_SIDE_BORDER 20
105 #define TOOL_EXTRA_SIDE_BORDER 0
109 #define SECONDS_PER_MINUTE 60
111 /* the number of digits precision for the internal range.
112 * note, this needs to be enough so that the step size for
113 * small total_times doesn't get rounded off. Currently set to 3
114 * this is because for the smallest total time ( i.e 1 ) and the current
115 * num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
116 * add one for safety */
117 #define MAX_ROUND_DIGITS 3
128 * hildon_seekbar_get_type:
130 * Initializes, and returns the type of a hildon seekbar.
132 * Returns: GType of #HildonSeekbar
136 hildon_seekbar_get_type (void)
138 static GType seekbar_type = 0;
141 static const GTypeInfo seekbar_info = {
142 sizeof (HildonSeekbarClass),
143 NULL, /* base_init */
144 NULL, /* base_finalize */
145 (GClassInitFunc) hildon_seekbar_class_init,
146 NULL, /* class_finalize */
147 NULL, /* class_data */
148 sizeof (HildonSeekbar),
150 (GInstanceInitFunc) hildon_seekbar_init,
152 seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
161 * Initialises the seekbar class.
164 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
166 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
167 GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
169 parent_class = g_type_class_peek_parent (seekbar_class);
171 g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
173 widget_class->size_request = hildon_seekbar_size_request;
174 widget_class->size_allocate = hildon_seekbar_size_allocate;
175 widget_class->button_press_event = hildon_seekbar_button_press_event;
176 widget_class->button_release_event = hildon_seekbar_button_release_event;
177 widget_class->key_press_event = hildon_seekbar_keypress;
179 object_class->set_property = hildon_seekbar_set_property;
180 object_class->get_property = hildon_seekbar_get_property;
183 * HildonSeekbar:total-time:
185 * Total playing time of this media file.
187 g_object_class_install_property (object_class, PROP_TOTAL_TIME,
188 g_param_spec_double ("total-time",
190 "Total playing time of this media file",
192 G_MAXDOUBLE, /* max value */
197 * HildonSeekbar:position:
199 * Current position in this media file.
201 g_object_class_install_property (object_class, PROP_POSITION,
202 g_param_spec_double ("position",
204 "Current position in this media file",
206 G_MAXDOUBLE, /* max value */
211 * HildonSeekbar:fraction:
213 * Current fraction related to the progress indicator.
215 g_object_class_install_property (object_class, PROP_FRACTION,
216 g_param_spec_double ("fraction",
218 "current fraction related to the"
219 "progress indicator",
221 G_MAXDOUBLE, /* max value */
228 hildon_seekbar_init (HildonSeekbar *seekbar)
230 GtkRange *range = GTK_RANGE(seekbar);
232 /* Initialize range widget */
233 range->orientation = GTK_ORIENTATION_HORIZONTAL;
234 range->flippable = TRUE;
235 range->round_digits = MAX_ROUND_DIGITS;
237 gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
241 * Purpose of this function is to prevent Up and Down keys from
242 * changing the widget's value (like Left and Right). Instead they
243 * are used for changing focus to other widgtes.
246 hildon_seekbar_keypress (GtkWidget *widget,
249 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
252 return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
256 hildon_seekbar_set_property (GObject *object,
261 HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
265 case PROP_TOTAL_TIME:
266 hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
270 hildon_seekbar_set_position (seekbar, g_value_get_double (value));
274 hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 /* handle getting of seekbar properties */
285 hildon_seekbar_get_property (GObject *object,
290 GtkRange *range = GTK_RANGE (object);
294 case PROP_TOTAL_TIME:
295 g_value_set_double (value, range->adjustment->upper);
299 g_value_set_double (value, range->adjustment->value);
303 g_value_set_double (value,
304 hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 * hildon_seekbar_new:
316 * Create a new #HildonSeekbar widget.
318 * Returns: a #GtkWidget pointer of #HildonSeekbar widget
321 hildon_seekbar_new (void)
323 return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
327 * hildon_seekbar_get_total_time:
328 * @seekbar: pointer to #HildonSeekbar widget
330 * Returns: total playing time of media in seconds.
333 hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
336 widget = GTK_WIDGET (seekbar);
337 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
338 g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
339 return GTK_RANGE (widget)->adjustment->upper;
343 * hildon_seekbar_set_total_time:
344 * @seekbar: pointer to #HildonSeekbar widget
345 * @time: integer greater than zero
347 * Set total playing time of media in seconds.
350 hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
355 gboolean value_changed = FALSE;
357 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
358 widget = GTK_WIDGET (seekbar);
364 g_return_if_fail (GTK_RANGE (widget)->adjustment);
366 adj = GTK_RANGE (widget)->adjustment;
369 /* Clamp position to total time */
370 if (adj->value > time) {
372 value_changed = TRUE;
375 /* Calculate new step value */
376 adj->step_increment = adj->upper / NUM_STEPS;
377 adj->page_increment = adj->step_increment;
379 gtk_adjustment_changed (adj);
381 /* Update range widget position/fraction */
383 gtk_adjustment_value_changed (adj);
384 hildon_seekbar_set_fraction(seekbar,
385 MIN (hildon_seekbar_get_fraction (seekbar),
388 g_object_freeze_notify (G_OBJECT (seekbar));
390 hildon_seekbar_set_position (seekbar,
391 MIN (hildon_seekbar_get_position (seekbar),
394 g_object_notify(G_OBJECT (seekbar), "total-time");
396 g_object_thaw_notify (G_OBJECT (seekbar));
401 * hildon_seekbar_get_fraction:
402 * @seekbar: pointer to #HildonSeekbar widget
404 * Get current fraction value of the rage.
406 * Returns: current fraction
409 hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
411 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
413 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
414 return gtk_range_get_fill_level (GTK_RANGE (seekbar));
421 * hildon_seekbar_set_fraction:
422 * @seekbar: pointer to #HildonSeekbar widget
423 * @fraction: the new position of the progress indicator
425 * Set current fraction value of the range.
426 * It should be between the minimal and maximal values of the range in seekbar.
429 hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
432 GtkRange *range = NULL;
433 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
435 range = GTK_RANGE(GTK_WIDGET(seekbar));
437 g_return_if_fail (fraction <= range->adjustment->upper &&
438 fraction >= range->adjustment->lower);
440 /* Set to show stream indicator. */
441 g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
443 fraction = CLAMP (fraction, range->adjustment->lower,
444 range->adjustment->upper);
446 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
447 /* Update stream position of range widget */
448 gtk_range_set_fill_level (range, fraction);
451 if (fraction < hildon_seekbar_get_position(seekbar))
452 hildon_seekbar_set_position(seekbar, fraction);
454 g_object_notify (G_OBJECT (seekbar), "fraction");
458 * hildon_seekbar_get_position:
459 * @seekbar: pointer to #HildonSeekbar widget
461 * Get current position in stream in seconds.
463 * Returns: current position in stream in seconds
466 hildon_seekbar_get_position (HildonSeekbar *seekbar)
468 g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
469 g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
471 return GTK_RANGE (seekbar)->adjustment->value;
475 * hildon_seekbar_set_position:
476 * @seekbar: pointer to #HildonSeekbar widget
477 * @time: time within range of >= 0 && < G_MAXINT
479 * Set current position in stream in seconds.
482 hildon_seekbar_set_position (HildonSeekbar *seekbar,
489 g_return_if_fail (time >= 0);
490 g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
491 range = GTK_RANGE (seekbar);
492 adj = range->adjustment;
493 g_return_if_fail (adj);
495 /* only change value if it is a different int. this allows us to have
496 smooth scrolls for small total_times */
497 value = floor (adj->value);
499 value = (time < adj->upper) ? time : adj->upper;
501 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
502 if (value <= gtk_range_get_fill_level (range)) {
507 gtk_adjustment_value_changed (adj);
509 g_object_notify (G_OBJECT (seekbar), "position");
515 hildon_seekbar_size_request (GtkWidget *widget,
518 HildonSeekbar *self = NULL;
519 HildonSeekbarPrivate *priv = NULL;
520 GtkWidget *parent = NULL;
522 self = HILDON_SEEKBAR (widget);
523 priv = HILDON_SEEKBAR_GET_PRIVATE (self);
525 parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
527 priv->is_toolbar = parent ? TRUE : FALSE;
529 if (GTK_WIDGET_CLASS (parent_class)->size_request)
530 GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
532 /* Request minimum size, depending on whether the widget is in a
534 req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
535 req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
539 hildon_seekbar_size_allocate (GtkWidget *widget,
540 GtkAllocation *allocation)
542 HildonSeekbarPrivate *priv;
544 priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
546 if (priv->is_toolbar == TRUE)
548 /* Center vertically */
549 if (allocation->height > TOOL_DEFAULT_HEIGHT)
552 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
553 allocation->height = TOOL_DEFAULT_HEIGHT;
555 /* Add space for border */
556 allocation->x += TOOL_EXTRA_SIDE_BORDER;
557 allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
561 /* Center vertically */
562 if (allocation->height > DEFAULT_HEIGHT)
564 allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
565 allocation->height = DEFAULT_HEIGHT;
568 /* Add space for border */
569 allocation->x += EXTRA_SIDE_BORDER;
570 allocation->width -= 2 * EXTRA_SIDE_BORDER;
573 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
574 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
578 * Event handler for button press. Changes button1 to button2.
581 hildon_seekbar_button_press_event (GtkWidget *widget,
582 GdkEventButton *event)
586 /* We change here the button id because we want to use button2
587 * functionality for button1: jump to mouse position
588 * instead of slowly incrementing to it */
589 if (event->button == 1) event->button = 2;
591 /* call the parent handler */
592 if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
593 result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
599 * Event handler for button release. Changes button1 to button2.
602 hildon_seekbar_button_release_event (GtkWidget *widget,
603 GdkEventButton *event)
605 gboolean result = FALSE;
607 /* We change here the button id because we want to use button2
608 * functionality for button1: jump to mouse position
609 * instead of slowly incrementing to it */
610 event->button = event->button == 1 ? 2 : event->button;
612 /* call the parent handler */
613 if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
614 result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,