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 <gtk/gtkversion.h>
51 #include <gdk/gdkkeysyms.h>
52 #include "hildon-seekbar-private.h"
54 static GtkScaleClass* parent_class = NULL;
57 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class);
60 hildon_seekbar_init (HildonSeekbar *seekbar);
63 hildon_seekbar_set_property (GObject *object,
69 hildon_seekbar_get_property (GObject *object,
75 hildon_seekbar_size_request (GtkWidget *widget,
76 GtkRequisition *event);
79 hildon_seekbar_size_allocate (GtkWidget *widget,
80 GtkAllocation *allocation);
83 hildon_seekbar_expose (GtkWidget *widget,
84 GdkEventExpose *event);
87 hildon_seekbar_button_press_event (GtkWidget *widget,
88 GdkEventButton *event);
91 hildon_seekbar_button_release_event (GtkWidget *widget,
92 GdkEventButton *event);
95 hildon_seekbar_keypress (GtkWidget *widget,
98 #define MINIMUM_WIDTH 115
100 #define DEFAULT_HEIGHT 58
102 #define TOOL_MINIMUM_WIDTH 75
104 #define TOOL_DEFAULT_HEIGHT 40
106 #define DEFAULT_DISPLAYC_BORDER 10
108 #define BUFFER_SIZE 32
110 #define EXTRA_SIDE_BORDER 20
112 #define TOOL_EXTRA_SIDE_BORDER 0
116 #define SECONDS_PER_MINUTE 60
118 /* the number of digits precision for the internal range.
119 * note, this needs to be enough so that the step size for
120 * small total_times doesn't get rounded off. Currently set to 3
121 * this is because for the smallest total time ( i.e 1 ) and the current
122 * num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
123 * add one for safety */
124 #define MAX_ROUND_DIGITS 3
135 * hildon_seekbar_get_type:
137 * Initializes, and returns the type of a hildon seekbar.
139 * @Returns : GType of #HildonSeekbar
143 hildon_seekbar_get_type (void)
145 static GType seekbar_type = 0;
148 static const GTypeInfo seekbar_info = {
149 sizeof (HildonSeekbarClass),
150 NULL, /* base_init */
151 NULL, /* base_finalize */
152 (GClassInitFunc) hildon_seekbar_class_init,
153 NULL, /* class_finalize */
154 NULL, /* class_data */
155 sizeof (HildonSeekbar),
157 (GInstanceInitFunc) hildon_seekbar_init,
159 seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
168 * Initialises the seekbar class.
171 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
173 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
174 GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
176 parent_class = g_type_class_peek_parent (seekbar_class);
178 g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
180 widget_class->size_request = hildon_seekbar_size_request;
181 widget_class->size_allocate = hildon_seekbar_size_allocate;
182 widget_class->expose_event = hildon_seekbar_expose;
183 widget_class->button_press_event = hildon_seekbar_button_press_event;
184 widget_class->button_release_event = hildon_seekbar_button_release_event;
185 widget_class->key_press_event = hildon_seekbar_keypress;
187 object_class->set_property = hildon_seekbar_set_property;
188 object_class->get_property = hildon_seekbar_get_property;
191 * HildonSeekbar:total-time:
193 * Total playing time of this media file.
195 g_object_class_install_property (object_class, PROP_TOTAL_TIME,
196 g_param_spec_double ("total-time",
198 "Total playing time of this media file",
200 G_MAXDOUBLE, /* max value */
205 * HildonSeekbar:position:
207 * Current position in this media file.
209 g_object_class_install_property (object_class, PROP_POSITION,
210 g_param_spec_double ("position",
212 "Current position in this media file",
214 G_MAXDOUBLE, /* max value */
219 * HildonSeekbar:fraction:
221 * Current fraction related to the progress indicator.
223 g_object_class_install_property (object_class, PROP_FRACTION,
224 g_param_spec_double ("fraction",
226 "current fraction related to the"
227 "progress indicator",
229 G_MAXDOUBLE, /* max value */
236 hildon_seekbar_init (HildonSeekbar *seekbar)
238 HildonSeekbarPrivate *priv;
239 GtkRange *range = GTK_RANGE(seekbar);
241 priv = HILDON_SEEKBAR_GET_PRIVATE (seekbar);
244 /* Initialize range widget */
245 range->orientation = GTK_ORIENTATION_HORIZONTAL;
246 range->flippable = TRUE;
247 range->has_stepper_a = TRUE;
248 range->has_stepper_d = TRUE;
249 range->round_digits = MAX_ROUND_DIGITS;
251 gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
255 * Purpose of this function is to prevent Up and Down keys from
256 * changing the widget's value (like Left and Right). Instead they
257 * are used for changing focus to other widgtes.
260 hildon_seekbar_keypress (GtkWidget *widget,
263 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
266 return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
270 hildon_seekbar_set_property (GObject *object,
275 HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
279 case PROP_TOTAL_TIME:
280 hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
284 hildon_seekbar_set_position (seekbar, g_value_get_double (value));
288 hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
297 /* handle getting of seekbar properties */
299 hildon_seekbar_get_property (GObject *object,
304 GtkRange *range = GTK_RANGE (object);
308 case PROP_TOTAL_TIME:
309 g_value_set_double (value, range->adjustment->upper);
313 g_value_set_double (value, range->adjustment->value);
317 g_value_set_double (value,
318 hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 * hildon_seekbar_new:
330 * Create a new #HildonSeekbar widget.
332 * Returns: a #GtkWidget pointer of #HildonSeekbar widget
335 hildon_seekbar_new (void)
337 return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
341 * hildon_seekbar_get_total_time:
342 * @seekbar: pointer to #HildonSeekbar widget
344 * Returns: total playing time of media in seconds.
347 hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
350 widget = GTK_WIDGET (seekbar);
351 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
352 g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
353 return GTK_RANGE (widget)->adjustment->upper;
357 * hildon_seekbar_set_total_time:
358 * @seekbar: pointer to #HildonSeekbar widget
359 * @time: integer greater than zero
361 * Set total playing time of media in seconds.
364 hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
369 gboolean value_changed = FALSE;
371 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
372 widget = GTK_WIDGET (seekbar);
378 g_return_if_fail (GTK_RANGE (widget)->adjustment);
380 adj = GTK_RANGE (widget)->adjustment;
383 /* Clamp position to total time */
384 if (adj->value > time) {
386 value_changed = TRUE;
389 /* Calculate new step value */
390 adj->step_increment = adj->upper / NUM_STEPS;
391 adj->page_increment = adj->step_increment;
393 gtk_adjustment_changed (adj);
395 /* Update range widget position/fraction */
397 gtk_adjustment_value_changed (adj);
398 hildon_seekbar_set_fraction(seekbar,
399 MIN (hildon_seekbar_get_fraction (seekbar),
402 g_object_freeze_notify (G_OBJECT (seekbar));
404 hildon_seekbar_set_position (seekbar,
405 MIN (hildon_seekbar_get_position (seekbar),
408 g_object_notify(G_OBJECT (seekbar), "total-time");
410 g_object_thaw_notify (G_OBJECT (seekbar));
415 * hildon_seekbar_get_fraction:
416 * @seekbar: pointer to #HildonSeekbar widget
418 * Get current fraction value of the rage.
420 * Returns: current fraction
423 hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
425 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
427 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
428 return gtk_range_get_fill_level (GTK_RANGE (seekbar));
435 * hildon_seekbar_set_fraction:
436 * @seekbar: pointer to #HildonSeekbar widget
437 * @fraction: the new position of the progress indicator
439 * Set current fraction value of the range.
440 * It should be between the minimal and maximal values of the range in seekbar.
443 hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
446 GtkRange *range = NULL;
447 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
449 range = GTK_RANGE(GTK_WIDGET(seekbar));
451 g_return_if_fail (fraction <= range->adjustment->upper &&
452 fraction >= range->adjustment->lower);
454 /* Set to show stream indicator. */
455 g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
457 fraction = CLAMP (fraction, range->adjustment->lower,
458 range->adjustment->upper);
460 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
461 /* Update stream position of range widget */
462 gtk_range_set_fill_level (range, fraction);
465 if (fraction < hildon_seekbar_get_position(seekbar))
466 hildon_seekbar_set_position(seekbar, fraction);
468 g_object_notify (G_OBJECT (seekbar), "fraction");
472 * hildon_seekbar_get_position:
473 * @seekbar: pointer to #HildonSeekbar widget
475 * Get current position in stream in seconds.
477 * Returns: current position in stream in seconds
480 hildon_seekbar_get_position (HildonSeekbar *seekbar)
482 g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
483 g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
485 return GTK_RANGE (seekbar)->adjustment->value;
489 * hildon_seekbar_set_position:
490 * @seekbar: pointer to #HildonSeekbar widget
491 * @time: time within range of >= 0 && < G_MAXINT
493 * Set current position in stream in seconds.
496 hildon_seekbar_set_position (HildonSeekbar *seekbar,
503 g_return_if_fail (time >= 0);
504 g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
505 range = GTK_RANGE (seekbar);
506 adj = range->adjustment;
507 g_return_if_fail (adj);
509 /* only change value if it is a different int. this allows us to have
510 smooth scrolls for small total_times */
511 value = floor (adj->value);
513 value = (time < adj->upper) ? time : adj->upper;
515 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
516 if (value <= gtk_range_get_fill_level (range)) {
521 gtk_adjustment_value_changed (adj);
523 g_object_notify (G_OBJECT (seekbar), "position");
529 hildon_seekbar_size_request (GtkWidget *widget,
532 HildonSeekbar *self = NULL;
533 HildonSeekbarPrivate *priv = NULL;
534 GtkWidget *parent = NULL;
536 self = HILDON_SEEKBAR (widget);
537 priv = HILDON_SEEKBAR_GET_PRIVATE (self);
540 parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
542 priv->is_toolbar = parent ? TRUE : FALSE;
544 if (GTK_WIDGET_CLASS (parent_class)->size_request)
545 GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
547 /* Request minimum size, depending on whether the widget is in a
549 req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
550 req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
554 hildon_seekbar_size_allocate (GtkWidget *widget,
555 GtkAllocation *allocation)
557 HildonSeekbarPrivate *priv;
559 priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
562 if (priv->is_toolbar == TRUE)
564 /* Center vertically */
565 if (allocation->height > TOOL_DEFAULT_HEIGHT)
568 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
569 allocation->height = TOOL_DEFAULT_HEIGHT;
571 /* Add space for border */
572 allocation->x += TOOL_EXTRA_SIDE_BORDER;
573 allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
577 /* Center vertically */
578 if (allocation->height > DEFAULT_HEIGHT)
580 allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
581 allocation->height = DEFAULT_HEIGHT;
584 /* Add space for border */
585 allocation->x += EXTRA_SIDE_BORDER;
586 allocation->width -= 2 * EXTRA_SIDE_BORDER;
589 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
590 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
594 hildon_seekbar_expose (GtkWidget *widget,
595 GdkEventExpose *event)
597 HildonSeekbarPrivate *priv;
598 gint extra_side_borders = 0;
600 priv = HILDON_SEEKBAR_GET_PRIVATE(widget);
603 extra_side_borders = priv->is_toolbar ? TOOL_EXTRA_SIDE_BORDER :
606 if (GTK_WIDGET_DRAWABLE (widget)) {
608 gtk_paint_box(widget->style, widget->window,
609 GTK_WIDGET_STATE(widget), GTK_SHADOW_OUT,
610 NULL, widget, "seekbar",
611 widget->allocation.x - extra_side_borders,
612 widget->allocation.y,
613 widget->allocation.width + 2 * extra_side_borders,
614 widget->allocation.height);
616 (*GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
623 * Event handler for button press. Changes button1 to button2.
626 hildon_seekbar_button_press_event (GtkWidget *widget,
627 GdkEventButton *event)
631 /* We change here the button id because we want to use button2
632 * functionality for button1: jump to mouse position
633 * instead of slowly incrementing to it */
634 if (event->button == 1) event->button = 2;
636 /* call the parent handler */
637 if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
638 result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
644 * Event handler for button release. Changes button1 to button2.
647 hildon_seekbar_button_release_event (GtkWidget *widget,
648 GdkEventButton *event)
650 gboolean result = FALSE;
652 /* We change here the button id because we want to use button2
653 * functionality for button1: jump to mouse position
654 * instead of slowly incrementing to it */
655 event->button = event->button == 1 ? 2 : event->button;
657 /* call the parent handler */
658 if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
659 result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,