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
36 * #HildonSeekbar has been deprecated since hildon 2.2. Use #GtkScale
37 * instead. See hildon_gtk_hscale_new() and hildon_gtk_vscale_new().
40 #undef HILDON_DISABLE_DEPRECATED
49 #include <gdk/gdkkeysyms.h>
51 #include "hildon-seekbar.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_button_press_event (GtkWidget *widget,
84 GdkEventButton *event);
87 hildon_seekbar_button_release_event (GtkWidget *widget,
88 GdkEventButton *event);
91 hildon_seekbar_keypress (GtkWidget *widget,
94 #define MINIMUM_WIDTH 115
96 #define DEFAULT_HEIGHT 58
98 #define TOOL_MINIMUM_WIDTH 75
100 #define TOOL_DEFAULT_HEIGHT 40
102 #define DEFAULT_DISPLAYC_BORDER 10
104 #define BUFFER_SIZE 32
106 #define EXTRA_SIDE_BORDER 20
108 #define TOOL_EXTRA_SIDE_BORDER 0
112 #define SECONDS_PER_MINUTE 60
114 /* the number of digits precision for the internal range.
115 * note, this needs to be enough so that the step size for
116 * small total_times doesn't get rounded off. Currently set to 3
117 * this is because for the smallest total time ( i.e 1 ) and the current
118 * num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
119 * add one for safety */
120 #define MAX_ROUND_DIGITS 3
131 * hildon_seekbar_get_type:
133 * Initializes, and returns the type of a hildon seekbar.
135 * Returns: GType of #HildonSeekbar
139 hildon_seekbar_get_type (void)
141 static GType seekbar_type = 0;
144 static const GTypeInfo seekbar_info = {
145 sizeof (HildonSeekbarClass),
146 NULL, /* base_init */
147 NULL, /* base_finalize */
148 (GClassInitFunc) hildon_seekbar_class_init,
149 NULL, /* class_finalize */
150 NULL, /* class_data */
151 sizeof (HildonSeekbar),
153 (GInstanceInitFunc) hildon_seekbar_init,
155 seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
164 * Initialises the seekbar class.
167 hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
169 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
170 GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
172 parent_class = g_type_class_peek_parent (seekbar_class);
174 g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
176 widget_class->size_request = hildon_seekbar_size_request;
177 widget_class->size_allocate = hildon_seekbar_size_allocate;
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 GtkRange *range = GTK_RANGE(seekbar);
235 /* Initialize range widget */
236 range->orientation = GTK_ORIENTATION_HORIZONTAL;
237 range->flippable = TRUE;
238 range->round_digits = MAX_ROUND_DIGITS;
240 gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
244 * Purpose of this function is to prevent Up and Down keys from
245 * changing the widget's value (like Left and Right). Instead they
246 * are used for changing focus to other widgtes.
249 hildon_seekbar_keypress (GtkWidget *widget,
252 if (event->keyval == GDK_Up || event->keyval == GDK_Down)
255 return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
259 hildon_seekbar_set_property (GObject *object,
264 HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
268 case PROP_TOTAL_TIME:
269 hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
273 hildon_seekbar_set_position (seekbar, g_value_get_double (value));
277 hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286 /* handle getting of seekbar properties */
288 hildon_seekbar_get_property (GObject *object,
293 GtkRange *range = GTK_RANGE (object);
297 case PROP_TOTAL_TIME:
298 g_value_set_double (value, range->adjustment->upper);
302 g_value_set_double (value, range->adjustment->value);
306 g_value_set_double (value,
307 hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 * hildon_seekbar_new:
319 * Create a new #HildonSeekbar widget.
321 * Returns: a #GtkWidget pointer of #HildonSeekbar widget
324 hildon_seekbar_new (void)
326 return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
330 * hildon_seekbar_get_total_time:
331 * @seekbar: pointer to #HildonSeekbar widget
333 * Returns: total playing time of media in seconds.
336 hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
339 widget = GTK_WIDGET (seekbar);
340 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
341 g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
342 return GTK_RANGE (widget)->adjustment->upper;
346 * hildon_seekbar_set_total_time:
347 * @seekbar: pointer to #HildonSeekbar widget
348 * @time: integer greater than zero
350 * Set total playing time of media in seconds.
353 hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
358 gboolean value_changed = FALSE;
360 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
361 widget = GTK_WIDGET (seekbar);
367 g_return_if_fail (GTK_RANGE (widget)->adjustment);
369 adj = GTK_RANGE (widget)->adjustment;
372 /* Clamp position to total time */
373 if (adj->value > time) {
375 value_changed = TRUE;
378 /* Calculate new step value */
379 adj->step_increment = adj->upper / NUM_STEPS;
380 adj->page_increment = adj->step_increment;
382 gtk_adjustment_changed (adj);
384 /* Update range widget position/fraction */
386 gtk_adjustment_value_changed (adj);
387 hildon_seekbar_set_fraction(seekbar,
388 MIN (hildon_seekbar_get_fraction (seekbar),
391 g_object_freeze_notify (G_OBJECT (seekbar));
393 hildon_seekbar_set_position (seekbar,
394 MIN (hildon_seekbar_get_position (seekbar),
397 g_object_notify(G_OBJECT (seekbar), "total-time");
399 g_object_thaw_notify (G_OBJECT (seekbar));
404 * hildon_seekbar_get_fraction:
405 * @seekbar: pointer to #HildonSeekbar widget
407 * Get current fraction value of the rage.
409 * Returns: current fraction
412 hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
414 g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
416 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
417 return gtk_range_get_fill_level (GTK_RANGE (seekbar));
424 * hildon_seekbar_set_fraction:
425 * @seekbar: pointer to #HildonSeekbar widget
426 * @fraction: the new position of the progress indicator
428 * Set current fraction value of the range.
429 * It should be between the minimal and maximal values of the range in seekbar.
432 hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
435 GtkRange *range = NULL;
436 g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
438 range = GTK_RANGE(GTK_WIDGET(seekbar));
440 g_return_if_fail (fraction <= range->adjustment->upper &&
441 fraction >= range->adjustment->lower);
443 /* Set to show stream indicator. */
444 g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
446 fraction = CLAMP (fraction, range->adjustment->lower,
447 range->adjustment->upper);
449 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
450 /* Update stream position of range widget */
451 gtk_range_set_fill_level (range, fraction);
454 if (fraction < hildon_seekbar_get_position(seekbar))
455 hildon_seekbar_set_position(seekbar, fraction);
457 g_object_notify (G_OBJECT (seekbar), "fraction");
461 * hildon_seekbar_get_position:
462 * @seekbar: pointer to #HildonSeekbar widget
464 * Get current position in stream in seconds.
466 * Returns: current position in stream in seconds
469 hildon_seekbar_get_position (HildonSeekbar *seekbar)
471 g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
472 g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
474 return GTK_RANGE (seekbar)->adjustment->value;
478 * hildon_seekbar_set_position:
479 * @seekbar: pointer to #HildonSeekbar widget
480 * @time: time within range of >= 0 && < G_MAXINT
482 * Set current position in stream in seconds.
485 hildon_seekbar_set_position (HildonSeekbar *seekbar,
492 g_return_if_fail (time >= 0);
493 g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
494 range = GTK_RANGE (seekbar);
495 adj = range->adjustment;
496 g_return_if_fail (adj);
498 /* only change value if it is a different int. this allows us to have
499 smooth scrolls for small total_times */
500 value = floor (adj->value);
502 value = (time < adj->upper) ? time : adj->upper;
504 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
505 if (value <= gtk_range_get_fill_level (range)) {
510 gtk_adjustment_value_changed (adj);
512 g_object_notify (G_OBJECT (seekbar), "position");
518 hildon_seekbar_size_request (GtkWidget *widget,
521 HildonSeekbar *self = NULL;
522 HildonSeekbarPrivate *priv = NULL;
523 GtkWidget *parent = NULL;
525 self = HILDON_SEEKBAR (widget);
526 priv = HILDON_SEEKBAR_GET_PRIVATE (self);
528 parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
530 priv->is_toolbar = parent ? TRUE : FALSE;
532 if (GTK_WIDGET_CLASS (parent_class)->size_request)
533 GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
535 /* Request minimum size, depending on whether the widget is in a
537 req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
538 req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
542 hildon_seekbar_size_allocate (GtkWidget *widget,
543 GtkAllocation *allocation)
545 HildonSeekbarPrivate *priv;
547 priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
549 if (priv->is_toolbar == TRUE)
551 /* Center vertically */
552 if (allocation->height > TOOL_DEFAULT_HEIGHT)
555 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
556 allocation->height = TOOL_DEFAULT_HEIGHT;
558 /* Add space for border */
559 allocation->x += TOOL_EXTRA_SIDE_BORDER;
560 allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
564 /* Center vertically */
565 if (allocation->height > DEFAULT_HEIGHT)
567 allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
568 allocation->height = DEFAULT_HEIGHT;
571 /* Add space for border */
572 allocation->x += EXTRA_SIDE_BORDER;
573 allocation->width -= 2 * EXTRA_SIDE_BORDER;
576 if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
577 GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
581 * Event handler for button press. Changes button1 to button2.
584 hildon_seekbar_button_press_event (GtkWidget *widget,
585 GdkEventButton *event)
589 /* We change here the button id because we want to use button2
590 * functionality for button1: jump to mouse position
591 * instead of slowly incrementing to it */
592 if (event->button == 1) event->button = 2;
594 /* call the parent handler */
595 if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
596 result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
602 * Event handler for button release. Changes button1 to button2.
605 hildon_seekbar_button_release_event (GtkWidget *widget,
606 GdkEventButton *event)
608 gboolean result = FALSE;
610 /* We change here the button id because we want to use button2
611 * functionality for button1: jump to mouse position
612 * instead of slowly incrementing to it */
613 event->button = event->button == 1 ? 2 : event->button;
615 /* call the parent handler */
616 if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
617 result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,