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
41 #include "hildon-seekbar.h"
45 #include <gtk/gtklabel.h>
46 #include <gtk/gtkframe.h>
47 #include <gtk/gtkalignment.h>
48 #include <gtk/gtkadjustment.h>
49 #include <gtk/gtktoolbar.h>
50 #include <gdk/gdkkeysyms.h>
51 #include "hildon-seekbar-private.h"
53 static GtkScaleClass* parent_class = NULL;
56 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class);
59 hildon_seekbar_init (HildonSeekbar *seekbar);
62 hildon_seekbar_set_property (GObject *object,
68 hildon_seekbar_get_property (GObject *object,
74 hildon_seekbar_size_request (GtkWidget *widget,
75 GtkRequisition *event);
78 hildon_seekbar_size_allocate (GtkWidget *widget,
79 GtkAllocation *allocation);
82 hildon_seekbar_expose (GtkWidget *widget,
83 GdkEventExpose *event);
86 hildon_seekbar_button_press_event (GtkWidget *widget,
87 GdkEventButton *event);
90 hildon_seekbar_button_release_event (GtkWidget *widget,
91 GdkEventButton *event);
94 hildon_seekbar_keypress (GtkWidget *widget,
97 #define MINIMUM_WIDTH 115
99 #define DEFAULT_HEIGHT 58
101 #define TOOL_MINIMUM_WIDTH 75
103 #define TOOL_DEFAULT_HEIGHT 40
105 #define DEFAULT_DISPLAYC_BORDER 10
107 #define BUFFER_SIZE 32
109 #define EXTRA_SIDE_BORDER 20
111 #define TOOL_EXTRA_SIDE_BORDER 0
115 #define SECONDS_PER_MINUTE 60
117 /* the number of digits precision for the internal range.
118 * note, this needs to be enough so that the step size for
119 * small total_times doesn't get rounded off. Currently set to 3
120 * this is because for the smallest total time ( i.e 1 ) and the current
121 * num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
122 * add one for safety */
123 #define MAX_ROUND_DIGITS 3
134 * hildon_seekbar_get_type:
136 * Initializes, and returns the type of a hildon seekbar.
138 * @Returns : GType of #HildonSeekbar
142 hildon_seekbar_get_type (void)
144 static GType seekbar_type = 0;
147 static const GTypeInfo seekbar_info = {
148 sizeof (HildonSeekbarClass),
149 NULL, /* base_init */
150 NULL, /* base_finalize */
151 (GClassInitFunc) hildon_seekbar_class_init,
152 NULL, /* class_finalize */
153 NULL, /* class_data */
154 sizeof (HildonSeekbar),
156 (GInstanceInitFunc) hildon_seekbar_init,
158 seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
167 * Initialises the seekbar class.
170 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
172 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
173 GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
175 parent_class = g_type_class_peek_parent (seekbar_class);
177 g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
179 widget_class->size_request = hildon_seekbar_size_request;
180 widget_class->size_allocate = hildon_seekbar_size_allocate;
181 widget_class->expose_event = hildon_seekbar_expose;
182 widget_class->button_press_event = hildon_seekbar_button_press_event;
183 widget_class->button_release_event = hildon_seekbar_button_release_event;
184 widget_class->key_press_event = hildon_seekbar_keypress;
186 object_class->set_property = hildon_seekbar_set_property;
187 object_class->get_property = hildon_seekbar_get_property;
190 * HildonSeekbar:total-time:
192 * Total playing time of this media file.
194 g_object_class_install_property (object_class, PROP_TOTAL_TIME,
195 g_param_spec_double ("total-time",
197 "Total playing time of this media file",
199 G_MAXDOUBLE, /* max value */
204 * HildonSeekbar:position:
206 * Current position in this media file.
208 g_object_class_install_property (object_class, PROP_POSITION,
209 g_param_spec_double ("position",
211 "Current position in this media file",
213 G_MAXDOUBLE, /* max value */
218 * HildonSeekbar:fraction:
220 * Current fraction related to the progress indicator.
222 g_object_class_install_property (object_class, PROP_FRACTION,
223 g_param_spec_double ("fraction",
225 "current fraction related to the"
226 "progress indicator",
228 G_MAXDOUBLE, /* max value */
235 hildon_seekbar_init (HildonSeekbar *seekbar)
237 HildonSeekbarPrivate *priv;
238 GtkRange *range = GTK_RANGE(seekbar);
240 priv = HILDON_SEEKBAR_GET_PRIVATE (seekbar);
243 /* Initialize range widget */
244 range->orientation = GTK_ORIENTATION_HORIZONTAL;
245 range->flippable = TRUE;
246 range->has_stepper_a = TRUE;
247 range->has_stepper_d = TRUE;
248 range->round_digits = MAX_ROUND_DIGITS;
250 gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
254 * Purpose of this function is to prevent Up and Down keys from
255 * changing the widget's value (like Left and Right). Instead they
256 * are used for changing focus to other widgtes.
259 hildon_seekbar_keypress (GtkWidget *widget,
262 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
265 return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
269 hildon_seekbar_set_property (GObject *object,
274 HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
278 case PROP_TOTAL_TIME:
279 hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
283 hildon_seekbar_set_position (seekbar, g_value_get_double (value));
287 hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 /* handle getting of seekbar properties */
298 hildon_seekbar_get_property (GObject *object,
303 GtkRange *range = GTK_RANGE (object);
307 case PROP_TOTAL_TIME:
308 g_value_set_double (value, range->adjustment->upper);
312 g_value_set_double (value, range->adjustment->value);
316 g_value_set_double (value,
317 hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 * hildon_seekbar_new:
329 * Create a new #HildonSeekbar widget.
331 * Returns: a #GtkWidget pointer of #HildonSeekbar widget
334 hildon_seekbar_new (void)
336 return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
340 * hildon_seekbar_get_total_time:
341 * @seekbar: pointer to #HildonSeekbar widget
343 * Returns: total playing time of media in seconds.
346 hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
349 widget = GTK_WIDGET (seekbar);
350 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
351 g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
352 return GTK_RANGE (widget)->adjustment->upper;
356 * hildon_seekbar_set_total_time:
357 * @seekbar: pointer to #HildonSeekbar widget
358 * @time: integer greater than zero
360 * Set total playing time of media in seconds.
363 hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
368 gboolean value_changed = FALSE;
370 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
371 widget = GTK_WIDGET (seekbar);
377 g_return_if_fail (GTK_RANGE (widget)->adjustment);
379 adj = GTK_RANGE (widget)->adjustment;
382 /* Clamp position to total time */
383 if (adj->value > time) {
385 value_changed = TRUE;
388 /* Calculate new step value */
389 adj->step_increment = adj->upper / NUM_STEPS;
390 adj->page_increment = adj->step_increment;
392 gtk_adjustment_changed (adj);
394 /* Update range widget position/fraction */
396 gtk_adjustment_value_changed (adj);
397 hildon_seekbar_set_fraction(seekbar,
398 MIN (hildon_seekbar_get_fraction (seekbar),
401 g_object_freeze_notify (G_OBJECT (seekbar));
403 hildon_seekbar_set_position (seekbar,
404 MIN (hildon_seekbar_get_position (seekbar),
407 g_object_notify(G_OBJECT (seekbar), "total-time");
409 g_object_thaw_notify (G_OBJECT (seekbar));
414 * hildon_seekbar_get_fraction:
415 * @seekbar: pointer to #HildonSeekbar widget
417 * Get current fraction value of the rage.
419 * Returns: current fraction
422 hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
424 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
427 return gtk_range_get_fill_level (GTK_RANGE (seekbar));
434 * hildon_seekbar_set_fraction:
435 * @seekbar: pointer to #HildonSeekbar widget
436 * @fraction: the new position of the progress indicator
438 * Set current fraction value of the range.
439 * It should be between the minimal and maximal values of the range in seekbar.
442 hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
445 GtkRange *range = NULL;
446 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
448 range = GTK_RANGE(GTK_WIDGET(seekbar));
450 g_return_if_fail (fraction <= range->adjustment->upper &&
451 fraction >= range->adjustment->lower);
453 /* Set to show stream indicator. */
454 g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
456 fraction = CLAMP (fraction, range->adjustment->lower,
457 range->adjustment->upper);
460 /* Update stream position of range widget */
461 gtk_range_set_fill_level (range, fraction);
464 if (fraction < hildon_seekbar_get_position(seekbar))
465 hildon_seekbar_set_position(seekbar, fraction);
467 g_object_notify (G_OBJECT (seekbar), "fraction");
471 * hildon_seekbar_get_position:
472 * @seekbar: pointer to #HildonSeekbar widget
474 * Get current position in stream in seconds.
476 * Returns: current position in stream in seconds
479 hildon_seekbar_get_position (HildonSeekbar *seekbar)
481 g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
482 g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
484 return GTK_RANGE (seekbar)->adjustment->value;
488 * hildon_seekbar_set_position:
489 * @seekbar: pointer to #HildonSeekbar widget
490 * @time: time within range of >= 0 && < G_MAXINT
492 * Set current position in stream in seconds.
495 hildon_seekbar_set_position (HildonSeekbar *seekbar,
502 g_return_if_fail (time >= 0);
503 g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
504 range = GTK_RANGE (seekbar);
505 adj = range->adjustment;
506 g_return_if_fail (adj);
508 /* only change value if it is a different int. this allows us to have
509 smooth scrolls for small total_times */
510 value = floor (adj->value);
512 value = (time < adj->upper) ? time : adj->upper;
515 if (value <= gtk_range_get_fill_level (range)) {
520 gtk_adjustment_value_changed (adj);
522 g_object_notify (G_OBJECT (seekbar), "position");
528 hildon_seekbar_size_request (GtkWidget *widget,
531 HildonSeekbar *self = NULL;
532 HildonSeekbarPrivate *priv = NULL;
533 GtkWidget *parent = NULL;
535 self = HILDON_SEEKBAR (widget);
536 priv = HILDON_SEEKBAR_GET_PRIVATE (self);
539 parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
541 priv->is_toolbar = parent ? TRUE : FALSE;
543 if (GTK_WIDGET_CLASS (parent_class)->size_request)
544 GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
546 /* Request minimum size, depending on whether the widget is in a
548 req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
549 req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
553 hildon_seekbar_size_allocate (GtkWidget *widget,
554 GtkAllocation *allocation)
556 HildonSeekbarPrivate *priv;
558 priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
561 if (priv->is_toolbar == TRUE)
563 /* Center vertically */
564 if (allocation->height > TOOL_DEFAULT_HEIGHT)
567 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
568 allocation->height = TOOL_DEFAULT_HEIGHT;
570 /* Add space for border */
571 allocation->x += TOOL_EXTRA_SIDE_BORDER;
572 allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
576 /* Center vertically */
577 if (allocation->height > DEFAULT_HEIGHT)
579 allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
580 allocation->height = DEFAULT_HEIGHT;
583 /* Add space for border */
584 allocation->x += EXTRA_SIDE_BORDER;
585 allocation->width -= 2 * EXTRA_SIDE_BORDER;
588 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
589 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
593 hildon_seekbar_expose (GtkWidget *widget,
594 GdkEventExpose *event)
596 HildonSeekbarPrivate *priv;
597 gint extra_side_borders = 0;
599 priv = HILDON_SEEKBAR_GET_PRIVATE(widget);
602 extra_side_borders = priv->is_toolbar ? TOOL_EXTRA_SIDE_BORDER :
605 if (GTK_WIDGET_DRAWABLE (widget)) {
607 gtk_paint_box(widget->style, widget->window,
608 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
609 NULL, widget, "seekbar",
610 widget->allocation.x - extra_side_borders,
611 widget->allocation.y,
612 widget->allocation.width + 2 * extra_side_borders,
613 widget->allocation.height);
615 (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
622 * Event handler for button press. Changes button1 to button2.
625 hildon_seekbar_button_press_event (GtkWidget *widget,
626 GdkEventButton *event)
630 /* We change here the button id because we want to use button2
631 * functionality for button1: jump to mouse position
632 * instead of slowly incrementing to it */
633 if (event->button == 1) event->button = 2;
635 /* call the parent handler */
636 if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
637 result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
643 * Event handler for button release. Changes button1 to button2.
646 hildon_seekbar_button_release_event (GtkWidget *widget,
647 GdkEventButton *event)
649 gboolean result = FALSE;
651 /* We change here the button id because we want to use button2
652 * functionality for button1: jump to mouse position
653 * instead of slowly incrementing to it */
654 event->button = event->button == 1 ? 2 : event->button;
656 /* call the parent handler */
657 if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
658 result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,