2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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
44 #include <gdk/gdkkeysyms.h>
46 #include "hildon-seekbar.h"
47 #include "hildon-seekbar-private.h"
49 static GtkScaleClass* parent_class = NULL;
52 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class);
55 hildon_seekbar_init (HildonSeekbar *seekbar);
58 hildon_seekbar_set_property (GObject *object,
64 hildon_seekbar_get_property (GObject *object,
70 hildon_seekbar_size_request (GtkWidget *widget,
71 GtkRequisition *event);
74 hildon_seekbar_size_allocate (GtkWidget *widget,
75 GtkAllocation *allocation);
78 hildon_seekbar_expose (GtkWidget *widget,
79 GdkEventExpose *event);
82 hildon_seekbar_button_press_event (GtkWidget *widget,
83 GdkEventButton *event);
86 hildon_seekbar_button_release_event (GtkWidget *widget,
87 GdkEventButton *event);
90 hildon_seekbar_keypress (GtkWidget *widget,
93 #define MINIMUM_WIDTH 115
95 #define DEFAULT_HEIGHT 58
97 #define TOOL_MINIMUM_WIDTH 75
99 #define TOOL_DEFAULT_HEIGHT 40
101 #define DEFAULT_DISPLAYC_BORDER 10
103 #define BUFFER_SIZE 32
105 #define EXTRA_SIDE_BORDER 20
107 #define TOOL_EXTRA_SIDE_BORDER 0
111 #define SECONDS_PER_MINUTE 60
113 /* the number of digits precision for the internal range.
114 * note, this needs to be enough so that the step size for
115 * small total_times doesn't get rounded off. Currently set to 3
116 * this is because for the smallest total time ( i.e 1 ) and the current
117 * num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
118 * add one for safety */
119 #define MAX_ROUND_DIGITS 3
130 * hildon_seekbar_get_type:
132 * Initializes, and returns the type of a hildon seekbar.
134 * @Returns : GType of #HildonSeekbar
138 hildon_seekbar_get_type (void)
140 static GType seekbar_type = 0;
143 static const GTypeInfo seekbar_info = {
144 sizeof (HildonSeekbarClass),
145 NULL, /* base_init */
146 NULL, /* base_finalize */
147 (GClassInitFunc) hildon_seekbar_class_init,
148 NULL, /* class_finalize */
149 NULL, /* class_data */
150 sizeof (HildonSeekbar),
152 (GInstanceInitFunc) hildon_seekbar_init,
154 seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
163 * Initialises the seekbar class.
166 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
168 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
169 GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
171 parent_class = g_type_class_peek_parent (seekbar_class);
173 g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
175 widget_class->size_request = hildon_seekbar_size_request;
176 widget_class->size_allocate = hildon_seekbar_size_allocate;
177 widget_class->expose_event = hildon_seekbar_expose;
178 widget_class->button_press_event = hildon_seekbar_button_press_event;
179 widget_class->button_release_event = hildon_seekbar_button_release_event;
180 widget_class->key_press_event = hildon_seekbar_keypress;
182 object_class->set_property = hildon_seekbar_set_property;
183 object_class->get_property = hildon_seekbar_get_property;
186 * HildonSeekbar:total-time:
188 * Total playing time of this media file.
190 g_object_class_install_property (object_class, PROP_TOTAL_TIME,
191 g_param_spec_double ("total-time",
193 "Total playing time of this media file",
195 G_MAXDOUBLE, /* max value */
200 * HildonSeekbar:position:
202 * Current position in this media file.
204 g_object_class_install_property (object_class, PROP_POSITION,
205 g_param_spec_double ("position",
207 "Current position in this media file",
209 G_MAXDOUBLE, /* max value */
214 * HildonSeekbar:fraction:
216 * Current fraction related to the progress indicator.
218 g_object_class_install_property (object_class, PROP_FRACTION,
219 g_param_spec_double ("fraction",
221 "current fraction related to the"
222 "progress indicator",
224 G_MAXDOUBLE, /* max value */
231 hildon_seekbar_init (HildonSeekbar *seekbar)
233 HildonSeekbarPrivate *priv;
234 GtkRange *range = GTK_RANGE(seekbar);
236 priv = HILDON_SEEKBAR_GET_PRIVATE (seekbar);
239 /* Initialize range widget */
240 range->orientation = GTK_ORIENTATION_HORIZONTAL;
241 range->flippable = TRUE;
242 range->has_stepper_a = TRUE;
243 range->has_stepper_d = TRUE;
244 range->round_digits = MAX_ROUND_DIGITS;
246 gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
250 * Purpose of this function is to prevent Up and Down keys from
251 * changing the widget's value (like Left and Right). Instead they
252 * are used for changing focus to other widgtes.
255 hildon_seekbar_keypress (GtkWidget *widget,
258 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
261 return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
265 hildon_seekbar_set_property (GObject *object,
270 HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
274 case PROP_TOTAL_TIME:
275 hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
279 hildon_seekbar_set_position (seekbar, g_value_get_double (value));
283 hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 /* handle getting of seekbar properties */
294 hildon_seekbar_get_property (GObject *object,
299 GtkRange *range = GTK_RANGE (object);
303 case PROP_TOTAL_TIME:
304 g_value_set_double (value, range->adjustment->upper);
308 g_value_set_double (value, range->adjustment->value);
312 g_value_set_double (value,
313 hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323 * hildon_seekbar_new:
325 * Create a new #HildonSeekbar widget.
327 * Returns: a #GtkWidget pointer of #HildonSeekbar widget
330 hildon_seekbar_new (void)
332 return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
336 * hildon_seekbar_get_total_time:
337 * @seekbar: pointer to #HildonSeekbar widget
339 * Returns: total playing time of media in seconds.
342 hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
345 widget = GTK_WIDGET (seekbar);
346 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
347 g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
348 return GTK_RANGE (widget)->adjustment->upper;
352 * hildon_seekbar_set_total_time:
353 * @seekbar: pointer to #HildonSeekbar widget
354 * @time: integer greater than zero
356 * Set total playing time of media in seconds.
359 hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
364 gboolean value_changed = FALSE;
366 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
367 widget = GTK_WIDGET (seekbar);
373 g_return_if_fail (GTK_RANGE (widget)->adjustment);
375 adj = GTK_RANGE (widget)->adjustment;
378 /* Clamp position to total time */
379 if (adj->value > time) {
381 value_changed = TRUE;
384 /* Calculate new step value */
385 adj->step_increment = adj->upper / NUM_STEPS;
386 adj->page_increment = adj->step_increment;
388 gtk_adjustment_changed (adj);
390 /* Update range widget position/fraction */
392 gtk_adjustment_value_changed (adj);
393 hildon_seekbar_set_fraction(seekbar,
394 MIN (hildon_seekbar_get_fraction (seekbar),
397 g_object_freeze_notify (G_OBJECT (seekbar));
399 hildon_seekbar_set_position (seekbar,
400 MIN (hildon_seekbar_get_position (seekbar),
403 g_object_notify(G_OBJECT (seekbar), "total-time");
405 g_object_thaw_notify (G_OBJECT (seekbar));
410 * hildon_seekbar_get_fraction:
411 * @seekbar: pointer to #HildonSeekbar widget
413 * Get current fraction value of the rage.
415 * Returns: current fraction
418 hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
420 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
422 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
423 return gtk_range_get_fill_level (GTK_RANGE (seekbar));
430 * hildon_seekbar_set_fraction:
431 * @seekbar: pointer to #HildonSeekbar widget
432 * @fraction: the new position of the progress indicator
434 * Set current fraction value of the range.
435 * It should be between the minimal and maximal values of the range in seekbar.
438 hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
441 GtkRange *range = NULL;
442 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
444 range = GTK_RANGE(GTK_WIDGET(seekbar));
446 g_return_if_fail (fraction <= range->adjustment->upper &&
447 fraction >= range->adjustment->lower);
449 /* Set to show stream indicator. */
450 g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
452 fraction = CLAMP (fraction, range->adjustment->lower,
453 range->adjustment->upper);
455 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
456 /* Update stream position of range widget */
457 gtk_range_set_fill_level (range, fraction);
460 if (fraction < hildon_seekbar_get_position(seekbar))
461 hildon_seekbar_set_position(seekbar, fraction);
463 g_object_notify (G_OBJECT (seekbar), "fraction");
467 * hildon_seekbar_get_position:
468 * @seekbar: pointer to #HildonSeekbar widget
470 * Get current position in stream in seconds.
472 * Returns: current position in stream in seconds
475 hildon_seekbar_get_position (HildonSeekbar *seekbar)
477 g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
478 g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
480 return GTK_RANGE (seekbar)->adjustment->value;
484 * hildon_seekbar_set_position:
485 * @seekbar: pointer to #HildonSeekbar widget
486 * @time: time within range of >= 0 && < G_MAXINT
488 * Set current position in stream in seconds.
491 hildon_seekbar_set_position (HildonSeekbar *seekbar,
498 g_return_if_fail (time >= 0);
499 g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
500 range = GTK_RANGE (seekbar);
501 adj = range->adjustment;
502 g_return_if_fail (adj);
504 /* only change value if it is a different int. this allows us to have
505 smooth scrolls for small total_times */
506 value = floor (adj->value);
508 value = (time < adj->upper) ? time : adj->upper;
510 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
511 if (value <= gtk_range_get_fill_level (range)) {
516 gtk_adjustment_value_changed (adj);
518 g_object_notify (G_OBJECT (seekbar), "position");
524 hildon_seekbar_size_request (GtkWidget *widget,
527 HildonSeekbar *self = NULL;
528 HildonSeekbarPrivate *priv = NULL;
529 GtkWidget *parent = NULL;
531 self = HILDON_SEEKBAR (widget);
532 priv = HILDON_SEEKBAR_GET_PRIVATE (self);
535 parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
537 priv->is_toolbar = parent ? TRUE : FALSE;
539 if (GTK_WIDGET_CLASS (parent_class)->size_request)
540 GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
542 /* Request minimum size, depending on whether the widget is in a
544 req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
545 req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
549 hildon_seekbar_size_allocate (GtkWidget *widget,
550 GtkAllocation *allocation)
552 HildonSeekbarPrivate *priv;
554 priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
557 if (priv->is_toolbar == TRUE)
559 /* Center vertically */
560 if (allocation->height > TOOL_DEFAULT_HEIGHT)
563 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
564 allocation->height = TOOL_DEFAULT_HEIGHT;
566 /* Add space for border */
567 allocation->x += TOOL_EXTRA_SIDE_BORDER;
568 allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
572 /* Center vertically */
573 if (allocation->height > DEFAULT_HEIGHT)
575 allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
576 allocation->height = DEFAULT_HEIGHT;
579 /* Add space for border */
580 allocation->x += EXTRA_SIDE_BORDER;
581 allocation->width -= 2 * EXTRA_SIDE_BORDER;
584 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
585 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
589 hildon_seekbar_expose (GtkWidget *widget,
590 GdkEventExpose *event)
592 HildonSeekbarPrivate *priv;
593 gint extra_side_borders = 0;
595 priv = HILDON_SEEKBAR_GET_PRIVATE(widget);
598 extra_side_borders = priv->is_toolbar ? TOOL_EXTRA_SIDE_BORDER :
601 if (GTK_WIDGET_DRAWABLE (widget)) {
603 gtk_paint_box(widget->style, widget->window,
604 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
605 NULL, widget, "seekbar",
606 widget->allocation.x - extra_side_borders,
607 widget->allocation.y,
608 widget->allocation.width + 2 * extra_side_borders,
609 widget->allocation.height);
611 (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
618 * Event handler for button press. Changes button1 to button2.
621 hildon_seekbar_button_press_event (GtkWidget *widget,
622 GdkEventButton *event)
626 /* We change here the button id because we want to use button2
627 * functionality for button1: jump to mouse position
628 * instead of slowly incrementing to it */
629 if (event->button == 1) event->button = 2;
631 /* call the parent handler */
632 if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
633 result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
639 * Event handler for button release. Changes button1 to button2.
642 hildon_seekbar_button_release_event (GtkWidget *widget,
643 GdkEventButton *event)
645 gboolean result = FALSE;
647 /* We change here the button id because we want to use button2
648 * functionality for button1: jump to mouse position
649 * instead of slowly incrementing to it */
650 event->button = event->button == 1 ? 2 : event->button;
652 /* call the parent handler */
653 if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
654 result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,