1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/*
* This file is a part of hildon
*
* Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
*
* Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/**
* SECTION:hildon-seekbar
* @short_description: A widget used to identify a place from a content.
*
* HildonSeekbar allows seeking in media with a range widget. It
* supports for setting or getting the length (total time) of the media,
* the position within it and the fraction (maximum position in a
* stream/the amount currently downloaded). The position is clamped
* between zero and the total time, or zero and the fraction in case of
* a stream.
*/
#undef HILDON_DISABLE_DEPRECATED
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <libintl.h>
#include <stdio.h>
#include <math.h>
#include <gdk/gdkkeysyms.h>
#include "hildon-seekbar.h"
#include "hildon-seekbar-private.h"
static GtkScaleClass* parent_class = NULL;
static void
hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class);
static void
hildon_seekbar_init (HildonSeekbar *seekbar);
static void
hildon_seekbar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void
hildon_seekbar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void
hildon_seekbar_size_request (GtkWidget *widget,
GtkRequisition *event);
static void
hildon_seekbar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean
hildon_seekbar_button_press_event (GtkWidget *widget,
GdkEventButton *event);
static gboolean
hildon_seekbar_button_release_event (GtkWidget *widget,
GdkEventButton *event);
static gboolean
hildon_seekbar_keypress (GtkWidget *widget,
GdkEventKey *event);
#define MINIMUM_WIDTH 115
#define DEFAULT_HEIGHT 58
#define TOOL_MINIMUM_WIDTH 75
#define TOOL_DEFAULT_HEIGHT 40
#define DEFAULT_DISPLAYC_BORDER 10
#define BUFFER_SIZE 32
#define EXTRA_SIDE_BORDER 20
#define TOOL_EXTRA_SIDE_BORDER 0
#define NUM_STEPS 20
#define SECONDS_PER_MINUTE 60
/* the number of digits precision for the internal range.
* note, this needs to be enough so that the step size for
* small total_times doesn't get rounded off. Currently set to 3
* this is because for the smallest total time ( i.e 1 ) and the current
* num steps ( 20 ) is: 1/20 = 0.05. 0.05 is 2 digits, and we
* add one for safety */
#define MAX_ROUND_DIGITS 3
enum
{
PROP_0,
PROP_TOTAL_TIME,
PROP_POSITION,
PROP_FRACTION
};
/**
* hildon_seekbar_get_type:
*
* Initializes, and returns the type of a hildon seekbar.
*
* Returns: GType of #HildonSeekbar
*
*/
GType G_GNUC_CONST
hildon_seekbar_get_type (void)
{
static GType seekbar_type = 0;
if (!seekbar_type) {
static const GTypeInfo seekbar_info = {
sizeof (HildonSeekbarClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) hildon_seekbar_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (HildonSeekbar),
0, /* n_preallocs */
(GInstanceInitFunc) hildon_seekbar_init,
};
seekbar_type = g_type_register_static(GTK_TYPE_SCALE,
"HildonSeekbar",
&seekbar_info, 0);
}
return seekbar_type;
}
/**
* Initialises the seekbar class.
*/
static void
hildon_seekbar_class_init (HildonSeekbarClass *seekbar_class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (seekbar_class);
GObjectClass *object_class = G_OBJECT_CLASS (seekbar_class);
parent_class = g_type_class_peek_parent (seekbar_class);
g_type_class_add_private (seekbar_class, sizeof (HildonSeekbarPrivate));
widget_class->size_request = hildon_seekbar_size_request;
widget_class->size_allocate = hildon_seekbar_size_allocate;
widget_class->button_press_event = hildon_seekbar_button_press_event;
widget_class->button_release_event = hildon_seekbar_button_release_event;
widget_class->key_press_event = hildon_seekbar_keypress;
object_class->set_property = hildon_seekbar_set_property;
object_class->get_property = hildon_seekbar_get_property;
/**
* HildonSeekbar:total-time:
*
* Total playing time of this media file.
*/
g_object_class_install_property (object_class, PROP_TOTAL_TIME,
g_param_spec_double ("total-time",
"total time",
"Total playing time of this media file",
0, /* min value */
G_MAXDOUBLE, /* max value */
0, /* default */
G_PARAM_READWRITE));
/**
* HildonSeekbar:position:
*
* Current position in this media file.
*/
g_object_class_install_property (object_class, PROP_POSITION,
g_param_spec_double ("position",
"position",
"Current position in this media file",
0, /* min value */
G_MAXDOUBLE, /* max value */
0, /* default */
G_PARAM_READWRITE));
/**
* HildonSeekbar:fraction:
*
* Current fraction related to the progress indicator.
*/
g_object_class_install_property (object_class, PROP_FRACTION,
g_param_spec_double ("fraction",
"Fraction",
"current fraction related to the"
"progress indicator",
0, /* min value */
G_MAXDOUBLE, /* max value */
0, /* default */
G_PARAM_READWRITE));
}
static void
hildon_seekbar_init (HildonSeekbar *seekbar)
{
GtkRange *range = GTK_RANGE(seekbar);
/* Initialize range widget */
range->orientation = GTK_ORIENTATION_HORIZONTAL;
range->flippable = TRUE;
range->round_digits = MAX_ROUND_DIGITS;
gtk_scale_set_draw_value (GTK_SCALE (seekbar), FALSE);
}
/*
* Purpose of this function is to prevent Up and Down keys from
* changing the widget's value (like Left and Right). Instead they
* are used for changing focus to other widgtes.
*/
static gboolean
hildon_seekbar_keypress (GtkWidget *widget,
GdkEventKey *event)
{
if (event->keyval == GDK_Up || event->keyval == GDK_Down)
return FALSE;
return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event));
}
static void
hildon_seekbar_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
HildonSeekbar *seekbar = HILDON_SEEKBAR (object);
switch (prop_id) {
case PROP_TOTAL_TIME:
hildon_seekbar_set_total_time (seekbar, g_value_get_double (value));
break;
case PROP_POSITION:
hildon_seekbar_set_position (seekbar, g_value_get_double (value));
break;
case PROP_FRACTION:
hildon_seekbar_set_fraction (seekbar, g_value_get_double (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/* handle getting of seekbar properties */
static void
hildon_seekbar_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkRange *range = GTK_RANGE (object);
switch (prop_id) {
case PROP_TOTAL_TIME:
g_value_set_double (value, range->adjustment->upper);
break;
case PROP_POSITION:
g_value_set_double (value, range->adjustment->value);
break;
case PROP_FRACTION:
g_value_set_double (value,
hildon_seekbar_get_fraction (HILDON_SEEKBAR(object)));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* hildon_seekbar_new:
*
* Create a new #HildonSeekbar widget.
*
* Returns: a #GtkWidget pointer of #HildonSeekbar widget
*/
GtkWidget*
hildon_seekbar_new (void)
{
return g_object_new (HILDON_TYPE_SEEKBAR, NULL);
}
/**
* hildon_seekbar_get_total_time:
* @seekbar: pointer to #HildonSeekbar widget
*
* Returns: total playing time of media in seconds.
*/
gint
hildon_seekbar_get_total_time (HildonSeekbar *seekbar)
{
GtkWidget *widget;
widget = GTK_WIDGET (seekbar);
g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
g_return_val_if_fail (GTK_RANGE (widget)->adjustment, 0);
return GTK_RANGE (widget)->adjustment->upper;
}
/**
* hildon_seekbar_set_total_time:
* @seekbar: pointer to #HildonSeekbar widget
* @time: integer greater than zero
*
* Set total playing time of media in seconds.
*/
void
hildon_seekbar_set_total_time (HildonSeekbar *seekbar,
gint time)
{
GtkAdjustment *adj;
GtkWidget *widget;
gboolean value_changed = FALSE;
g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
widget = GTK_WIDGET (seekbar);
if (time <= 0) {
return;
}
g_return_if_fail (GTK_RANGE (widget)->adjustment);
adj = GTK_RANGE (widget)->adjustment;
adj->upper = time;
/* Clamp position to total time */
if (adj->value > time) {
adj->value = time;
value_changed = TRUE;
}
/* Calculate new step value */
adj->step_increment = adj->upper / NUM_STEPS;
adj->page_increment = adj->step_increment;
gtk_adjustment_changed (adj);
/* Update range widget position/fraction */
if (value_changed) {
gtk_adjustment_value_changed (adj);
hildon_seekbar_set_fraction(seekbar,
MIN (hildon_seekbar_get_fraction (seekbar),
time));
g_object_freeze_notify (G_OBJECT (seekbar));
hildon_seekbar_set_position (seekbar,
MIN (hildon_seekbar_get_position (seekbar),
time));
g_object_notify(G_OBJECT (seekbar), "total-time");
g_object_thaw_notify (G_OBJECT (seekbar));
}
}
/**
* hildon_seekbar_get_fraction:
* @seekbar: pointer to #HildonSeekbar widget
*
* Get current fraction value of the rage.
*
* Returns: current fraction
*/
guint
hildon_seekbar_get_fraction (HildonSeekbar *seekbar)
{
g_return_val_if_fail (HILDON_IS_SEEKBAR (seekbar), 0);
#if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
return gtk_range_get_fill_level (GTK_RANGE (seekbar));
#else
return 0;
#endif
}
/**
* hildon_seekbar_set_fraction:
* @seekbar: pointer to #HildonSeekbar widget
* @fraction: the new position of the progress indicator
*
* Set current fraction value of the range.
* It should be between the minimal and maximal values of the range in seekbar.
*/
void
hildon_seekbar_set_fraction (HildonSeekbar *seekbar,
guint fraction)
{
GtkRange *range = NULL;
g_return_if_fail (HILDON_IS_SEEKBAR (seekbar));
range = GTK_RANGE(GTK_WIDGET(seekbar));
g_return_if_fail (fraction <= range->adjustment->upper &&
fraction >= range->adjustment->lower);
/* Set to show stream indicator. */
g_object_set (G_OBJECT (seekbar), "show-fill-level", TRUE, NULL);
fraction = CLAMP (fraction, range->adjustment->lower,
range->adjustment->upper);
#if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
/* Update stream position of range widget */
gtk_range_set_fill_level (range, fraction);
#endif
if (fraction < hildon_seekbar_get_position(seekbar))
hildon_seekbar_set_position(seekbar, fraction);
g_object_notify (G_OBJECT (seekbar), "fraction");
}
/**
* hildon_seekbar_get_position:
* @seekbar: pointer to #HildonSeekbar widget
*
* Get current position in stream in seconds.
*
* Returns: current position in stream in seconds
*/
gint
hildon_seekbar_get_position (HildonSeekbar *seekbar)
{
g_return_val_if_fail (HILDON_IS_SEEKBAR(seekbar), 0);
g_return_val_if_fail (GTK_RANGE(seekbar)->adjustment, 0);
return GTK_RANGE (seekbar)->adjustment->value;
}
/**
* hildon_seekbar_set_position:
* @seekbar: pointer to #HildonSeekbar widget
* @time: time within range of >= 0 && < G_MAXINT
*
* Set current position in stream in seconds.
*/
void
hildon_seekbar_set_position (HildonSeekbar *seekbar,
gint time)
{
GtkRange *range;
GtkAdjustment *adj;
gint value;
g_return_if_fail (time >= 0);
g_return_if_fail (HILDON_IS_SEEKBAR(seekbar));
range = GTK_RANGE (seekbar);
adj = range->adjustment;
g_return_if_fail (adj);
/* only change value if it is a different int. this allows us to have
smooth scrolls for small total_times */
value = floor (adj->value);
if (time != value) {
value = (time < adj->upper) ? time : adj->upper;
#if defined(MAEMO_GTK) || GTK_CHECK_VERSION(2,11,0)
if (value <= gtk_range_get_fill_level (range)) {
#else
if (value) {
#endif
adj->value = value;
gtk_adjustment_value_changed (adj);
g_object_notify (G_OBJECT (seekbar), "position");
}
}
}
static void
hildon_seekbar_size_request (GtkWidget *widget,
GtkRequisition *req)
{
HildonSeekbar *self = NULL;
HildonSeekbarPrivate *priv = NULL;
GtkWidget *parent = NULL;
self = HILDON_SEEKBAR (widget);
priv = HILDON_SEEKBAR_GET_PRIVATE (self);
parent = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_TOOLBAR);
priv->is_toolbar = parent ? TRUE : FALSE;
if (GTK_WIDGET_CLASS (parent_class)->size_request)
GTK_WIDGET_CLASS (parent_class)->size_request (widget, req);
/* Request minimum size, depending on whether the widget is in a
* toolbar or not */
req->width = priv->is_toolbar ? TOOL_MINIMUM_WIDTH : MINIMUM_WIDTH;
req->height = priv->is_toolbar ? TOOL_DEFAULT_HEIGHT : DEFAULT_HEIGHT;
}
static void
hildon_seekbar_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
HildonSeekbarPrivate *priv;
priv = HILDON_SEEKBAR_GET_PRIVATE (widget);
if (priv->is_toolbar == TRUE)
{
/* Center vertically */
if (allocation->height > TOOL_DEFAULT_HEIGHT)
{
allocation->y +=
(allocation->height - TOOL_DEFAULT_HEIGHT) / 2;
allocation->height = TOOL_DEFAULT_HEIGHT;
}
/* Add space for border */
allocation->x += TOOL_EXTRA_SIDE_BORDER;
allocation->width -= 2 * TOOL_EXTRA_SIDE_BORDER;
}
else
{
/* Center vertically */
if (allocation->height > DEFAULT_HEIGHT)
{
allocation->y += (allocation->height - DEFAULT_HEIGHT) / 2;
allocation->height = DEFAULT_HEIGHT;
}
/* Add space for border */
allocation->x += EXTRA_SIDE_BORDER;
allocation->width -= 2 * EXTRA_SIDE_BORDER;
}
if (GTK_WIDGET_CLASS (parent_class)->size_allocate)
GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
}
/*
* Event handler for button press. Changes button1 to button2.
*/
static gboolean
hildon_seekbar_button_press_event (GtkWidget *widget,
GdkEventButton *event)
{
gint result = FALSE;
/* We change here the button id because we want to use button2
* functionality for button1: jump to mouse position
* instead of slowly incrementing to it */
if (event->button == 1) event->button = 2;
/* call the parent handler */
if (GTK_WIDGET_CLASS (parent_class)->button_press_event)
result = GTK_WIDGET_CLASS (parent_class)->button_press_event (widget,
event);
return result;
}
/*
* Event handler for button release. Changes button1 to button2.
*/
static gboolean
hildon_seekbar_button_release_event (GtkWidget *widget,
GdkEventButton *event)
{
gboolean result = FALSE;
/* We change here the button id because we want to use button2
* functionality for button1: jump to mouse position
* instead of slowly incrementing to it */
event->button = event->button == 1 ? 2 : event->button;
/* call the parent handler */
if (GTK_WIDGET_CLASS (parent_class)->button_release_event)
result = GTK_WIDGET_CLASS (parent_class)->button_release_event (widget,
event);
return result;
}
|