Label in HildonNote must be left aligned, not centered
[hildon] / src / hildon-seekbar.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-seekbar
27  * @short_description: A widget used to identify a place from a content.
28  *
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
34  * a stream.
35  */
36
37 #undef                                          HILDON_DISABLE_DEPRECATED
38
39 #ifdef                                          HAVE_CONFIG_H
40 #include                                        <config.h>
41 #endif
42
43 #include                                        <libintl.h>
44 #include                                        <stdio.h>
45 #include                                        <math.h>
46 #include                                        <gdk/gdkkeysyms.h>
47
48 #include                                        "hildon-seekbar.h"
49 #include                                        "hildon-seekbar-private.h"
50
51 static GtkScaleClass*                           parent_class = NULL;
52
53 static void 
54 hildon_seekbar_class_init                       (HildonSeekbarClass *seekbar_class);
55
56 static void 
57 hildon_seekbar_init                             (HildonSeekbar *seekbar);
58
59 static void
60 hildon_seekbar_set_property                     (GObject *object, 
61                                                  guint prop_id,
62                                                  const GValue *value,
63                                                  GParamSpec *pspec);
64
65 static void 
66 hildon_seekbar_get_property                     (GObject *object, 
67                                                  guint prop_id,
68                                                  GValue *value,
69                                                  GParamSpec *pspec);
70
71 static void
72 hildon_seekbar_size_request                     (GtkWidget *widget,
73                                                  GtkRequisition *event);
74
75 static void 
76 hildon_seekbar_size_allocate                    (GtkWidget *widget,
77                                                  GtkAllocation *allocation);
78
79 static gboolean 
80 hildon_seekbar_button_press_event               (GtkWidget *widget,
81                                                  GdkEventButton *event);
82
83 static gboolean
84 hildon_seekbar_button_release_event             (GtkWidget *widget,
85                                                  GdkEventButton *event);
86
87 static gboolean 
88 hildon_seekbar_keypress                         (GtkWidget *widget,
89                                                  GdkEventKey *event);
90
91 #define                                         MINIMUM_WIDTH 115
92
93 #define                                         DEFAULT_HEIGHT 58
94
95 #define                                         TOOL_MINIMUM_WIDTH 75
96
97 #define                                         TOOL_DEFAULT_HEIGHT 40
98
99 #define                                         DEFAULT_DISPLAYC_BORDER 10
100
101 #define                                         BUFFER_SIZE 32
102
103 #define                                         EXTRA_SIDE_BORDER 20
104
105 #define                                         TOOL_EXTRA_SIDE_BORDER 0
106
107 #define                                         NUM_STEPS 20
108
109 #define                                         SECONDS_PER_MINUTE 60
110
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
118
119 enum 
120 {
121     PROP_0,
122     PROP_TOTAL_TIME,
123     PROP_POSITION,
124     PROP_FRACTION
125 };
126
127 /**
128  * hildon_seekbar_get_type:
129  * 
130  * Initializes, and returns the type of a hildon seekbar.
131  * 
132  * Returns: GType of #HildonSeekbar
133  * 
134  */
135 GType G_GNUC_CONST 
136 hildon_seekbar_get_type                         (void)
137 {
138     static GType seekbar_type = 0;
139
140     if (!seekbar_type) {
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),
149             0,  /* n_preallocs */
150             (GInstanceInitFunc) hildon_seekbar_init,
151         };
152         seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
153                 "HildonSeekbar",
154                 &seekbar_info, 0);
155     }
156
157     return seekbar_type;
158 }
159
160 /**
161  * Initialises the seekbar class.
162  */
163 static void 
164 hildon_seekbar_class_init                       (HildonSeekbarClass *seekbar_class)
165 {
166     GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
167     GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
168
169     parent_class = g_type_class_peek_parent (seekbar_class);
170
171     g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
172
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;
178
179     object_class->set_property          = hildon_seekbar_set_property;
180     object_class->get_property          = hildon_seekbar_get_property;
181
182     /**
183      * HildonSeekbar:total-time:
184      *
185      * Total playing time of this media file.
186      */
187     g_object_class_install_property (object_class, PROP_TOTAL_TIME,
188             g_param_spec_double ("total-time",
189                 "total time",
190                 "Total playing time of this media file",
191                 0,           /* min value */
192                 G_MAXDOUBLE, /* max value */
193                 0,           /* default */
194                 G_PARAM_READWRITE));
195
196     /**
197      * HildonSeekbar:position:
198      *
199      * Current position in this media file.
200      */
201     g_object_class_install_property (object_class, PROP_POSITION,
202             g_param_spec_double ("position",
203                 "position",
204                 "Current position in this media file",
205                 0,           /* min value */
206                 G_MAXDOUBLE, /* max value */
207                 0,           /* default */
208                 G_PARAM_READWRITE));
209
210     /**
211      * HildonSeekbar:fraction:
212      *
213      * Current fraction related to the progress indicator.
214      */
215     g_object_class_install_property (object_class, PROP_FRACTION,
216             g_param_spec_double ("fraction",
217                 "Fraction",
218                 "current fraction related to the"
219                 "progress indicator",
220                 0,           /* min value */
221                 G_MAXDOUBLE, /* max value */
222                 0,           /* default */
223                 G_PARAM_READWRITE));
224 }
225
226
227 static void
228 hildon_seekbar_init                             (HildonSeekbar *seekbar)
229 {
230     GtkRange *range = GTK_RANGE(seekbar);
231
232     /* Initialize range widget */
233     range->orientation = GTK_ORIENTATION_HORIZONTAL;
234     range->flippable = TRUE;
235     range->round_digits = MAX_ROUND_DIGITS;
236
237     gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
238 }
239
240 /*
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.
244  */
245 static gboolean
246 hildon_seekbar_keypress                         (GtkWidget *widget,
247                                                  GdkEventKey *event)
248 {
249     if (event->keyval == GDK_Up || event->keyval == GDK_Down)
250         return FALSE;
251
252     return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
253 }
254
255 static void
256 hildon_seekbar_set_property                     (GObject *object, 
257                                                  guint prop_id,
258                                                  const GValue *value, 
259                                                  GParamSpec *pspec)
260 {
261     HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
262
263     switch (prop_id) {
264
265         case PROP_TOTAL_TIME:
266             hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
267             break;
268
269         case PROP_POSITION:
270             hildon_seekbar_set_position (seekbar, g_value_get_double (value));
271             break;
272
273         case PROP_FRACTION:
274             hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
275             break;
276
277         default:
278             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279             break;
280     }
281 }
282
283 /* handle getting of seekbar properties */
284 static void
285 hildon_seekbar_get_property                     (GObject *object, 
286                                                  guint prop_id,
287                                                  GValue *value, 
288                                                  GParamSpec *pspec)
289 {
290     GtkRange *range = GTK_RANGE (object);
291
292     switch (prop_id) {
293
294         case PROP_TOTAL_TIME:
295             g_value_set_double (value, range->adjustment->upper);
296             break;
297
298         case PROP_POSITION:
299             g_value_set_double (value, range->adjustment->value);
300             break;
301
302         case PROP_FRACTION:
303             g_value_set_double (value, 
304                     hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
305             break;
306
307         default:
308             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309             break;
310     }
311 }
312
313 /**
314  * hildon_seekbar_new:
315  *
316  * Create a new #HildonSeekbar widget.
317  * 
318  * Returns: a #GtkWidget pointer of #HildonSeekbar widget
319  */
320 GtkWidget*
321 hildon_seekbar_new                              (void)
322 {
323     return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
324 }
325
326 /**
327  * hildon_seekbar_get_total_time:
328  * @seekbar: pointer to #HildonSeekbar widget
329  *
330  * Returns: total playing time of media in seconds.
331  */
332 gint
333 hildon_seekbar_get_total_time                   (HildonSeekbar *seekbar)
334 {
335     GtkWidget *widget;
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;
340 }
341
342 /**
343  * hildon_seekbar_set_total_time:
344  * @seekbar: pointer to #HildonSeekbar widget
345  * @time: integer greater than zero
346  *
347  * Set total playing time of media in seconds.
348  */
349 void
350 hildon_seekbar_set_total_time                   (HildonSeekbar *seekbar, 
351                                                  gint time)
352 {
353     GtkAdjustment *adj;
354     GtkWidget *widget;
355     gboolean value_changed = FALSE;
356
357     g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
358     widget = GTK_WIDGET (seekbar);
359
360     if (time <= 0) {
361         return;
362     }
363
364     g_return_if_fail (GTK_RANGE (widget)->adjustment);
365
366     adj = GTK_RANGE (widget)->adjustment;
367     adj->upper = time;
368
369     /* Clamp position to total time */
370     if (adj->value > time) {
371         adj->value = time;
372         value_changed = TRUE;
373     }
374
375     /* Calculate new step value */
376     adj->step_increment = adj->upper / NUM_STEPS;
377     adj->page_increment = adj->step_increment;
378
379     gtk_adjustment_changed (adj);
380
381     /* Update range widget position/fraction */
382     if (value_changed) {
383         gtk_adjustment_value_changed (adj);
384         hildon_seekbar_set_fraction(seekbar,
385                 MIN (hildon_seekbar_get_fraction (seekbar),
386                     time));
387
388         g_object_freeze_notify (G_OBJECT (seekbar));
389
390         hildon_seekbar_set_position (seekbar,
391                 MIN (hildon_seekbar_get_position (seekbar),
392                     time));
393
394         g_object_notify(G_OBJECT (seekbar), "total-time");
395
396         g_object_thaw_notify (G_OBJECT (seekbar));
397     }
398 }
399
400 /**
401  * hildon_seekbar_get_fraction:
402  * @seekbar: pointer to #HildonSeekbar widget
403  *
404  * Get current fraction value of the rage.
405  *
406  * Returns: current fraction
407  */
408 guint 
409 hildon_seekbar_get_fraction                     (HildonSeekbar *seekbar)
410 {
411     g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
412
413 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
414     return gtk_range_get_fill_level (GTK_RANGE (seekbar));
415 #else
416     return 0;
417 #endif
418 }
419
420 /**
421  * hildon_seekbar_set_fraction:
422  * @seekbar: pointer to #HildonSeekbar widget
423  * @fraction: the new position of the progress indicator
424  *
425  * Set current fraction value of the range.
426  * It should be between the minimal and maximal values of the range in seekbar.
427  */
428 void 
429 hildon_seekbar_set_fraction                     (HildonSeekbar *seekbar, 
430                                                  guint fraction)
431 {
432     GtkRange *range = NULL;
433     g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
434
435     range = GTK_RANGE(GTK_WIDGET(seekbar));
436
437     g_return_if_fail (fraction <= range->adjustment->upper &&
438             fraction >= range->adjustment->lower);
439
440     /* Set to show stream indicator. */
441     g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
442
443     fraction = CLAMP (fraction, range->adjustment->lower,
444             range->adjustment->upper);
445
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);
449 #endif
450
451     if (fraction < hildon_seekbar_get_position(seekbar))
452         hildon_seekbar_set_position(seekbar, fraction);
453
454     g_object_notify (G_OBJECT (seekbar), "fraction");
455 }
456
457 /**
458  * hildon_seekbar_get_position:
459  * @seekbar: pointer to #HildonSeekbar widget
460  *
461  * Get current position in stream in seconds.
462  *
463  * Returns: current position in stream in seconds
464  */
465 gint 
466 hildon_seekbar_get_position                     (HildonSeekbar *seekbar)
467 {
468     g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
469     g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
470
471     return GTK_RANGE (seekbar)->adjustment->value;
472 }
473
474 /**
475  * hildon_seekbar_set_position:
476  * @seekbar: pointer to #HildonSeekbar widget
477  * @time: time within range of >= 0 && < G_MAXINT
478  *
479  * Set current position in stream in seconds.
480  */
481 void 
482 hildon_seekbar_set_position                     (HildonSeekbar *seekbar, 
483                                                  gint time)
484 {
485     GtkRange *range;
486     GtkAdjustment *adj;
487     gint value;
488
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);
494
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);
498     if (time != value) {
499         value = (time < adj->upper) ? time : adj->upper;
500
501 #if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
502         if (value <= gtk_range_get_fill_level (range)) {
503 #else
504         if (value) {
505 #endif
506             adj->value = value;
507             gtk_adjustment_value_changed (adj);
508
509             g_object_notify (G_OBJECT (seekbar), "position");
510         }
511     }
512 }
513
514 static void 
515 hildon_seekbar_size_request                     (GtkWidget *widget,
516                                                  GtkRequisition *req)
517 {
518     HildonSeekbar *self = NULL;
519     HildonSeekbarPrivate *priv = NULL;
520     GtkWidget *parent = NULL;
521
522     self = HILDON_SEEKBAR (widget);
523     priv = HILDON_SEEKBAR_GET_PRIVATE (self);
524
525     parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
526
527     priv->is_toolbar = parent ? TRUE : FALSE;
528
529     if (GTK_WIDGET_CLASS (parent_class)->size_request)
530         GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
531
532     /* Request minimum size, depending on whether the widget is in a
533      * toolbar or not */
534     req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
535     req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
536 }
537
538 static void 
539 hildon_seekbar_size_allocate                    (GtkWidget *widget,
540                                                  GtkAllocation *allocation)
541 {
542     HildonSeekbarPrivate *priv;
543
544     priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
545
546     if (priv->is_toolbar == TRUE)
547     {
548         /* Center vertically */
549         if (allocation->height > TOOL_DEFAULT_HEIGHT)
550         {
551             allocation->y +=
552                 (allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
553             allocation->height = TOOL_DEFAULT_HEIGHT;
554         }
555         /* Add space for border */
556         allocation->x += TOOL_EXTRA_SIDE_BORDER;
557         allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
558     }
559     else
560     {
561         /* Center vertically */
562         if (allocation->height > DEFAULT_HEIGHT)
563         {
564             allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
565             allocation->height = DEFAULT_HEIGHT;
566         }
567
568         /* Add space for border */
569         allocation->x += EXTRA_SIDE_BORDER;
570         allocation->width -= 2 * EXTRA_SIDE_BORDER;
571     }
572
573     if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
574         GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
575 }
576
577 /*
578  * Event handler for button press. Changes button1 to button2.
579  */
580 static gboolean
581 hildon_seekbar_button_press_event               (GtkWidget *widget,
582                                                  GdkEventButton *event)
583 {
584     gint result = FALSE;
585
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;
590
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,
594                 event);
595
596     return result;
597 }
598 /*
599  * Event handler for button release. Changes button1 to button2.
600  */
601 static gboolean
602 hildon_seekbar_button_release_event             (GtkWidget *widget,
603                                                  GdkEventButton *event)
604 {
605     gboolean result = FALSE;
606
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;
611
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,
615                 event);
616
617     return result;
618 }