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
|
/*
* 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-volumebar-range
* @short_description: This widget is an "workhorse" for #HildonVolumebar
* widget. It is not designed to be used as a standalone widget.
*
* Purpose of this widget is to act as an "container" for GtkScale
* widget. #HildonVolumebarRange changes some event parameters so
* that #HildonVolumebar can meet its specifications.
*
* Currently #HildonVolumebarRange models range of [0..100].
*
* <note>
* <para>
* #HildonVolumebarRange has been deprecated since Hildon 2.2 and should not
* be used in newly written code. See
* <link linkend="hildon-migrating-volume-bar">Migrating Volume Bars</link>
* section to know how to migrate this deprecated widget.
* </para>
* </note>
*/
#include <gdk/gdkkeysyms.h>
#include "hildon-volumebar-range.h"
#define VOLUMEBAR_RANGE_INITIAL_VALUE 50.0
#define VOLUMEBAR_RANGE_MINIMUM_VALUE 0.0
#define VOLUMEBAR_RANGE_MAXIMUM_VALUE 100.0
#define VOLUMEBAR_RANGE_STEP_INCREMENT_VALUE 5.0
#define VOLUMEBAR_RANGE_PAGE_INCREMENT_VALUE 5.0
#define VOLUMEBAR_RANGE_PAGE_SIZE_VALUE 0.0
#define CHANGE_THRESHOLD 0.001
static GtkScaleClass* parent_class;
static void
hildon_volumebar_range_class_init (HildonVolumebarRangeClass*
volumerange_class);
static void
hildon_volumebar_range_init (HildonVolumebarRange*
volumerange);
static void
hildon_volumebar_range_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void
hildon_volumebar_range_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static gint
hildon_volumebar_range_button_press_event (GtkWidget *widget,
GdkEventButton *event);
static gint
hildon_volumebar_range_button_release_event (GtkWidget *widget,
GdkEventButton *event);
static gboolean
hildon_volumebar_range_keypress (GtkWidget *widget,
GdkEventKey *event);
enum
{
PROP_0,
PROP_LEVEL
};
/**
* hildon_volumebar_range_get_type:
*
* Initializes and returns the type of a hildon volumebar range.
*
* Returns: GType of #HildonVolumebarRange
*/
GType G_GNUC_CONST
hildon_volumebar_range_get_type (void)
{
static GType volumerange_type = 0;
if (!volumerange_type) {
static const GTypeInfo volumerange_info = {
sizeof (HildonVolumebarRangeClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) hildon_volumebar_range_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (HildonVolumebarRange),
0, /* n_preallocs */
(GInstanceInitFunc) hildon_volumebar_range_init,
};
volumerange_type = g_type_register_static (GTK_TYPE_SCALE,
"HildonVolumebarRange",
&volumerange_info, 0);
}
return volumerange_type;
}
static void
hildon_volumebar_range_class_init (HildonVolumebarRangeClass *volumerange_class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (volumerange_class);
GObjectClass *object_class = G_OBJECT_CLASS (volumerange_class);
parent_class = g_type_class_peek_parent (volumerange_class);
widget_class->button_press_event =
hildon_volumebar_range_button_press_event;
widget_class->button_release_event =
hildon_volumebar_range_button_release_event;
widget_class->key_press_event = hildon_volumebar_range_keypress;
object_class->set_property = hildon_volumebar_range_set_property;
object_class->get_property = hildon_volumebar_range_get_property;
/**
* HildonVolumebarRange:level:
*
* Current volume level.
*/
g_object_class_install_property (object_class,
PROP_LEVEL,
g_param_spec_double ("level",
"Level",
"Current volume level",
VOLUMEBAR_RANGE_MINIMUM_VALUE,
VOLUMEBAR_RANGE_MAXIMUM_VALUE,
VOLUMEBAR_RANGE_INITIAL_VALUE,
G_PARAM_READWRITE));
return;
}
static void
hildon_volumebar_range_init (HildonVolumebarRange *volumerange)
{
/* do nothing. */
}
static void
hildon_volumebar_range_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
HildonVolumebarRange *range = HILDON_VOLUMEBAR_RANGE (object);
switch (prop_id) {
case PROP_LEVEL:
hildon_volumebar_range_set_level (range, g_value_get_double (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
hildon_volumebar_range_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
HildonVolumebarRange *range = HILDON_VOLUMEBAR_RANGE (object);
switch (prop_id) {
case PROP_LEVEL:
g_value_set_double (value, hildon_volumebar_range_get_level(range));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static gboolean
hildon_volumebar_range_keypress (GtkWidget *widget,
GdkEventKey *event)
{
/* Accept arrow keys only if they match the orientation of the widget */
if (GTK_RANGE (widget)->orientation == GTK_ORIENTATION_HORIZONTAL)
{
if (event->keyval == GDK_Up || event->keyval == GDK_Down) {
return FALSE;
}
}
else
{
if (event->keyval == GDK_Left || event->keyval == GDK_Right) {
return FALSE;
}
}
return ((GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget,
event));
}
GtkWidget*
hildon_volumebar_range_new (GtkOrientation orientation)
{
GtkAdjustment * adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (VOLUMEBAR_RANGE_INITIAL_VALUE,
VOLUMEBAR_RANGE_MINIMUM_VALUE,
VOLUMEBAR_RANGE_MAXIMUM_VALUE,
VOLUMEBAR_RANGE_STEP_INCREMENT_VALUE,
VOLUMEBAR_RANGE_PAGE_INCREMENT_VALUE,
VOLUMEBAR_RANGE_PAGE_SIZE_VALUE));
HildonVolumebarRange *self =
g_object_new(HILDON_TYPE_VOLUMEBAR_RANGE,
"adjustment", adjustment,
NULL);
GTK_RANGE (self)->orientation = orientation;
/* Default vertical range is upside down for purposes of this widget */
gtk_range_set_inverted (GTK_RANGE (self),
(orientation == GTK_ORIENTATION_VERTICAL));
return GTK_WIDGET(self);
}
gdouble
hildon_volumebar_range_get_level (HildonVolumebarRange *self)
{
g_return_val_if_fail (HILDON_IS_VOLUMEBAR_RANGE(self), -1.0);
return gtk_adjustment_get_value (gtk_range_get_adjustment(GTK_RANGE (self)));
}
void
hildon_volumebar_range_set_level (HildonVolumebarRange * self,
gdouble level)
{
GtkAdjustment *adjustment;
g_return_if_fail (HILDON_IS_VOLUMEBAR_RANGE (self));
adjustment = gtk_range_get_adjustment (GTK_RANGE (self));
/* Check that value has actually changed. Note that it's not safe to
* just compare if floats are equivalent or not */
if (ABS (gtk_adjustment_get_value (adjustment) - level) > CHANGE_THRESHOLD) {
gtk_adjustment_set_value(adjustment, level);
}
}
static gint
hildon_volumebar_range_button_press_event (GtkWidget *widget,
GdkEventButton *event)
{
gboolean result = FALSE;
/* FIXME: By default, clicking left mouse button on GtkRange moves the
slider by one step towards the click location. However, we want stylus
taps to move the slider to the position of the tap, which by default
is the middle button behaviour. To avoid breaking default GtkRange
behaviour, this has been implemented by faking a middle button press. */
event->button = (event->button == 1) ? 2 : event->button;
if (GTK_WIDGET_CLASS (parent_class)->button_press_event) {
result = GTK_WIDGET_CLASS (parent_class)->button_press_event(widget, event);
}
return result;
}
static gint
hildon_volumebar_range_button_release_event (GtkWidget *widget,
GdkEventButton *event)
{
gboolean result = FALSE;
/* FIXME: By default, clicking left mouse button on GtkRange moves the
slider by one step towards the click location. However, we want stylus
taps to move the slider to the position of the tap, which by default
is the middle button behaviour. To avoid breaking default GtkRange
behaviour, this has been implemented by faking a middle button press. */
event->button = event->button == 1 ? 2 : event->button;
if (GTK_WIDGET_CLASS (parent_class)->button_release_event) {
result = GTK_WIDGET_CLASS(parent_class)->button_release_event(widget, event);
}
return result;
}
|