Debian Codegraph @

src/gtkdial.c

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2021 Stiliyan Varbanov <https://www.fiverr.com/stilvar>
2 // SPDX-FileCopyrightText: 2022-2025 Geoffrey D. Bennett <[email protected]>
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 /*
6 * A Dial widget for GTK-4 similar to GtkScale.
7 * 2021 Stiliyan Varbanov www.fiverr.com/stilvar
8 */
9
10 #include <stdlib.h>
11 #include <glib-2.0/glib.h>
12 #include <glib-object.h>
13 #include <cairo/cairo.h>
14 #include <stdio.h>
15 #include <graphene-1.0/graphene.h>
16
17 #include <math.h>
18
19 #include "gtkdial.h"
20 #include "glow.h"
21 #include "db.h"
22
23 #define DIAL_MIN_WIDTH 50
24 #define DIAL_MAX_WIDTH 70
25
26 #define HISTORY_COUNT 50
27
28 static int set_value(GtkDial *dial, double newval);
29 static int set_level(GtkDial *dial, double newlevel);
30
31 static void gtk_dial_set_property(
32 GObject *object,
33 guint prop_id,
34 const GValue *value,
35 GParamSpec *pspec
36 );
37
38 static void gtk_dial_get_property(
39 GObject *object,
40 guint prop_id,
41 GValue *value,
42 GParamSpec *pspec
43 );
44
45 static void gtk_dial_move_slider(GtkDial *dial, GtkScrollType scroll);
46
47 static void gtk_dial_drag_gesture_begin(
48 GtkGestureDrag *gesture,
49 double offset_x,
50 double offset_y,
51 GtkDial *dial
52 );
53
54 static void gtk_dial_drag_gesture_update(
55 GtkGestureDrag *gesture,
56 double offset_x,
57 double offset_y,
58 GtkDial *dial
59 );
60
61 static void gtk_dial_drag_gesture_end(
62 GtkGestureDrag *gesture,
63 double offset_x,
64 double offset_y,
65 GtkDial *dial
66 );
67
68 static void gtk_dial_click_gesture_pressed(
69 GtkGestureClick *gesture,
70 int n_press,
71 double x,
72 double y,
73 GtkDial *dial
74 );
75
76 static gboolean gtk_dial_scroll_controller_scroll(
77 GtkEventControllerScroll *scroll,
78 double dx,
79 double dy,
80 GtkDial *dial
81 );
82
83 static void gtk_dial_dispose(GObject *o);
84
85 typedef enum {
86 GRAB_NONE,
87 GRAB_SLIDER
88 } e_grab;
89
90 enum {
91 PROP_0,
92 PROP_ADJUSTMENT,
93 PROP_ROUND_DIGITS,
94 PROP_ZERO_DB,
95 PROP_OFF_DB,
96 PROP_IS_LINEAR,
97 PROP_TAPER,
98 PROP_CAN_CONTROL,
99 PROP_PEAK_HOLD,
100 PROP_LEVEL,
101 PROP_SHOW_LEVEL,
102 PROP_SHOW_VALUE,
103 LAST_PROP
104 };
105
106 enum {
107 SIGNAL_0,
108 VALUE_CHANGED,
109 MOVE_SLIDER,
110 LAST_SIGNAL
111 };
112
113 static guint signals[LAST_SIGNAL];
114 static GParamSpec *properties[LAST_PROP];
115
116 struct _GtkDial {
117 GtkWidget parent_instance;
118 GtkAdjustment *adj; // for value (gain setting)
119 GtkAdjustment *level_adj; // for level metering (dB scale)
120
121 GtkGesture *drag_gesture, *click_gesture;
122 GtkEventController *scroll_controller;
123 e_grab grab;
124 double dvalp;
125
126 int round_digits;
127 double zero_db;
128 double off_db;
129 gboolean is_linear;
130 int taper;
131 gboolean can_control;
132 int peak_hold;
133
134 int properties_updated;
135
136 // linear taper breakpoints array
137 double *taper_breakpoints;
138 double *taper_outputs;
139 int taper_breakpoints_count;
140
141 // level meter colour breakpoints array
142 const int *level_breakpoints;
143 const double *level_colours;
144 int level_breakpoints_count;
145
146 // cached static layer (background, value arc, knob, focus ring)
147 cairo_surface_t *static_cache;
148 int static_cache_valid;
149
150 // variables derived from the widget's dynamic properties (size and
151 // configuration, excluding the value)
152 int dim;
153 double w;
154 double h;
155 double radius;
156 double slider_thickness;
157 double knob_radius;
158 double slider_radius;
159 double background_radius;
160 double cx;
161 double cy;
162 double zero_db_x;
163 double zero_db_y;
164 double *level_breakpoint_angles;
165
166 // cairo patterns dependent on the above
167 cairo_pattern_t *fill_pattern[2][2];
168 cairo_pattern_t *outline_pattern[2];
169
170 // pango resources for displaying the peak value
171 PangoLayout *peak_layout;
172 PangoFontDescription *peak_font_desc;
173
174 // variables derived from the dial value (gain setting)
175 double valp;
176 double angle;
177 double slider_cx;
178 double slider_cy;
179
180 // level metering (separate from value)
181 double level; // current signal level in dB
182 double level_valp; // normalised level position (0-1)
183 double level_angle; // angle for level arc
184 gboolean show_level; // whether to show level meter
185 gboolean show_value; // whether to show value arc (defaults TRUE)
186
187 // peak tracking (applies to level, not value)
188 double peak_angle;
189
190 // value history for displaying peak
191 double hist_values[HISTORY_COUNT];
192 long long hist_time[HISTORY_COUNT];
193 double current_peak;
194 int hist_head, hist_tail, hist_count;
195 };
196
197 G_DEFINE_TYPE(GtkDial, gtk_dial, GTK_TYPE_WIDGET)
198
199 static void dial_snapshot(GtkWidget *widget, GtkSnapshot *snapshot);
200 static void dial_measure(
201 GtkWidget *widget,
202 GtkOrientation orientation,
203 int for_size,
204 int *minimum,
205 int *natural,
206 int *minimum_baseline,
207 int *natural_baseline
208 );
209
210 #define add_slider_binding(w_class, binding_set, keyval, mask, scroll) \
211 gtk_widget_class_add_binding_signal(w_class, \
212 keyval, mask, \
213 "move-slider", \
214 "(i)", scroll)
215
216 long long current_time = 0;
217
218 void gtk_dial_peak_tick(void) {
219 struct timespec ts;
220
221 if (clock_gettime(CLOCK_BOOTTIME, &ts) < 0)
222 return;
223
224 current_time = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
225 }
226
227 // BEGIN SECTION HELPERS
228
229 #define TOTAL_ROTATION_DEGREES 290
230 #define TOTAL_ROTATION (2 * M_PI * TOTAL_ROTATION_DEGREES / 360)
231 #define ANGLE_START (-M_PI / 2 - TOTAL_ROTATION / 2)
232 #define ANGLE_END (-M_PI / 2 + TOTAL_ROTATION / 2)
233
234 #define DRAG_FACTOR 0.5
235
236 // convert val from mn..mx to 0..1 with clamp
237 static double calc_valp(double val, double mn, double mx) {
238 if (val <= mn)
239 return 0.0;
240 if (val >= mx)
241 return 1.0;
242
243 return (val - mn) / (mx - mn);
244 }
245
246 static double taper_linear(double val, double *bp, double *out, int count) {
247 if (count < 3)
248 return val;
249
250 if (val <= bp[0])
251 return out[0];
252
253 for (int i = 0; i < count - 1; i++) {
254 if (val > bp[i + 1])
255 continue;
256
257 double scale = (out[i + 1] - out[i]) / (bp[i + 1] - bp[i]);
258 return out[i] + scale * (val - bp[i]);
259 }
260
261 return out[count - 1];
262 }
263
264 static double taper_log(double val) {
265
266 // 10^(val - 1) converts it to 0.1..1 with a nice curve
267 val = pow(10, val - 1);
268
269 // convert to 0..1 again
270 return (val - 0.1) / 0.9;
271 }
272
273 // calc_taper_adj: calculate taper using a specific adjustment's range
274 // use_linear_conversion: apply is_linear conversion (only for value, not level)
275 static double calc_taper_adj(
276 GtkDial *dial,
277 GtkAdjustment *adj,
278 double val,
279 gboolean use_linear_conversion
280 ) {
281 double mn = gtk_adjustment_get_lower(adj);
282 double mx = gtk_adjustment_get_upper(adj);
283 double off_db = gtk_dial_get_off_db(dial);
284
285 if (use_linear_conversion && gtk_dial_get_is_linear(dial)) {
286 val = linear_value_to_cdb(val, mn, mx, -8000, 1200) / 100.0;
287 mn = -60;
288 mx = 12;
289 }
290
291 // if off_db is set, then values below it are considered as
292 // almost-silence, so we clamp them to 0.01
293 // only apply this for level display (not for value taper)
294 if (!use_linear_conversion && off_db > mn) {
295 if (val <= mn)
296 val = 0;
297 else if (val <= off_db)
298 val = 0.01;
299 else
300 val = calc_valp(val, off_db, mx) * 0.99 + 0.01;
301 } else {
302 val = calc_valp(val, mn, mx);
303 }
304
305 if (dial->taper == GTK_DIAL_TAPER_LINEAR)
306 return taper_linear(
307 val,
308 dial->taper_breakpoints,
309 dial->taper_outputs,
310 dial->taper_breakpoints_count
311 );
312
313 if (dial->taper == GTK_DIAL_TAPER_LOG)
314 return taper_log(val);
315
316 g_warning("Invalid taper value: %d", dial->taper);
317
318 return val;
319 }
320
321 // calc_taper: calculate taper for value using dial->adj
322 static double calc_taper(GtkDial *dial, double val) {
323 return calc_taper_adj(dial, dial->adj, val, TRUE);
324 }
325
326 static double calc_val(double valp, double mn, double mx) {
327 return (mx - mn) * valp + mn;
328 }
329
330 static int calculate_dial_height(int width) {
331 double radius = width / 2;
332 double angle = (360 - TOTAL_ROTATION_DEGREES) / 2 * M_PI / 180;
333 double height = radius + radius * cos(angle);
334
335 return ceil(height);
336 }
337
338 static double calculate_dial_radius_from_height(int height) {
339 double angle = (360 - TOTAL_ROTATION_DEGREES) / 2.0 * M_PI / 180.0;
340 return height / (1 + cos(angle));
341 }
342
343 // internal replacement for cairo_pattern_add_color_stop_rgb() that
344 // dims the color if the widget is insensitive and brightens it by
345 // focus_mult
346 static void cairo_add_stop_rgb_dim(
347 cairo_pattern_t *pat,
348 double offset,
349 double r,
350 double g,
351 double b,
352 int dim,
353 double focus_mult
354 ) {
355 double x = dim ? 0.5 : 1.0;
356 x *= focus_mult;
357
358 cairo_pattern_add_color_stop_rgb(pat, offset, r * x, g * x, b * x);
359 }
360
361 static int update_dial_properties(GtkDial *dial) {
362
363 // always update
364 dial->dim = !gtk_widget_is_sensitive(GTK_WIDGET(dial)) && dial->can_control;
365
366 // the rest of the values only depend on the widget size and
367 // configuration
368 double width = gtk_widget_get_width(GTK_WIDGET(dial));
369 double height = gtk_widget_get_height(GTK_WIDGET(dial));
370
371 if (dial->w == width && dial->h == height && !dial->properties_updated)
372 return 0;
373
374 dial->w = width;
375 dial->h = height;
376 dial->properties_updated = 0;
377
378 // calculate size of dial to fit within the given space
379 if (width > DIAL_MAX_WIDTH)
380 width = DIAL_MAX_WIDTH;
381
382 double max_height = calculate_dial_height(DIAL_MAX_WIDTH);
383 if (height > max_height)
384 height = max_height;
385
386 // calculate dial radius
387 double radius_from_width = width / 2;
388 double radius_from_height = calculate_dial_radius_from_height(height);
389
390 dial->radius = radius_from_width < radius_from_height ?
391 radius_from_width : radius_from_height;
392 dial->radius -= 0.5;
393
394 // calculate center of dial
395 double angle = (360 - TOTAL_ROTATION_DEGREES) / 2.0 * M_PI / 180.0;
396 double y_offset = dial->radius * cos(angle);
397
398 dial->cx = dial->w / 2;
399 dial->cy = (dial->h / 2.0) + (dial->radius - y_offset) / 2.0 - 0.5;
400
401 dial->slider_thickness = dial->radius / 2.2;
402 dial->knob_radius = dial->radius - dial->slider_thickness;
403 dial->slider_radius = dial->radius - dial->slider_thickness / 2;
404 dial->background_radius = dial->slider_radius + dial->slider_thickness / 4;
405
406 // calculate zero_db marker position
407 double zero_db = gtk_dial_get_zero_db(dial);
408
409 if (zero_db != -G_MAXDOUBLE) {
410 double zero_db_valp = calc_taper(dial, zero_db);
411 double zero_db_angle = calc_val(zero_db_valp, ANGLE_START, ANGLE_END);
412
413 dial->zero_db_x = cos(zero_db_angle) * dial->slider_radius + dial->cx;
414 dial->zero_db_y = sin(zero_db_angle) * dial->slider_radius + dial->cy;
415 }
416
417 // generate cairo fill patterns
418 for (int focus = 0; focus <= 1; focus++) {
419 for (int dim = 0; dim <= 1; dim++) {
420 if (dial->fill_pattern[focus][dim])
421 cairo_pattern_destroy(dial->fill_pattern[focus][dim]);
422
423 cairo_pattern_t *pat = cairo_pattern_create_radial(
424 dial->cx + 5, dial->cy + 5, 0, dial->cx, dial->cy, dial->radius
425 );
426 cairo_add_stop_rgb_dim(pat, 0.0, 0.18, 0.18, 0.20, dim, focus ? 1.65 : 1);
427 cairo_add_stop_rgb_dim(pat, 0.4, 0.18, 0.18, 0.20, dim, focus ? 1.65 : 1);
428 cairo_add_stop_rgb_dim(pat, 1.0, 0.40, 0.40, 0.42, dim, focus ? 1.25 : 1);
429
430 dial->fill_pattern[focus][dim] = pat;
431 }
432 }
433
434 // generate cairo outline pattern
435 for (int dim = 0; dim <= 1; dim++) {
436 if (dial->outline_pattern[dim])
437 cairo_pattern_destroy(dial->outline_pattern[dim]);
438
439 cairo_pattern_t *pat = cairo_pattern_create_linear(
440 dial->cx - dial->radius / 2,
441 dial->cy - dial->radius / 2,
442 dial->cx + dial->radius / 2,
443 dial->cy + dial->radius / 2
444 );
445 cairo_add_stop_rgb_dim(pat, 0, 0.6, 0.6, 0.6, dim, 1);
446 cairo_add_stop_rgb_dim(pat, 1, 0.2, 0.2, 0.2, dim, 1);
447
448 dial->outline_pattern[dim] = pat;
449 }
450
451 // init pango layout for peak value
452 if (dial->peak_layout)
453 g_object_unref(dial->peak_layout);
454 if (dial->peak_font_desc)
455 pango_font_description_free(dial->peak_font_desc);
456
457 PangoContext *context = gtk_widget_create_pango_context(GTK_WIDGET(dial));
458 dial->peak_layout = pango_layout_new(context);
459 dial->peak_font_desc = pango_context_get_font_description(context);
460 int size = pango_font_description_get_size(dial->peak_font_desc) * 0.6;
461 dial->peak_font_desc = pango_font_description_copy(dial->peak_font_desc);
462 pango_font_description_set_size(dial->peak_font_desc, size);
463 pango_layout_set_font_description(dial->peak_layout, dial->peak_font_desc);
464 g_object_unref(context);
465
466 // calculate level meter breakpoint angles using level_adj (dB range)
467 if (dial->level_breakpoint_angles)
468 free(dial->level_breakpoint_angles);
469
470 if (dial->level_breakpoints_count) {
471 dial->level_breakpoint_angles = malloc(
472 dial->level_breakpoints_count * sizeof(double)
473 );
474 for (int i = 0; i < dial->level_breakpoints_count; i++) {
475 double valp = calc_taper_adj(
476 dial, dial->level_adj, dial->level_breakpoints[i], FALSE
477 );
478 dial->level_breakpoint_angles[i] =
479 calc_val(valp, ANGLE_START, ANGLE_END);
480 }
481 }
482
483 return 1;
484 }
485
486 static void update_dial_values(GtkDial *dial) {
487 dial->valp = calc_taper(dial, gtk_adjustment_get_value(dial->adj));
488 dial->angle = calc_val(dial->valp, ANGLE_START, ANGLE_END);
489 dial->slider_cx = cos(dial->angle) * dial->slider_radius + dial->cx;
490 dial->slider_cy = sin(dial->angle) * dial->slider_radius + dial->cy;
491 }
492
493 static void update_dial_level_values(GtkDial *dial) {
494 // update level display values using level_adj (dB range)
495 dial->level_valp = calc_taper_adj(dial, dial->level_adj, dial->level, FALSE);
496 dial->level_angle = calc_val(dial->level_valp, ANGLE_START, ANGLE_END);
497
498 if (!dial->peak_hold)
499 return;
500
501 double peak_valp = calc_taper_adj(dial, dial->level_adj, dial->current_peak, FALSE);
502 dial->peak_angle = calc_val(peak_valp, ANGLE_START, ANGLE_END);
503 }
504
505 static double pdist2(double x1, double y1, double x2, double y2) {
506 double dx = x2 - x1;
507 double dy = y2 - y1;
508
509 return dx * dx + dy * dy;
510 }
511
512 static gboolean circle_contains_point(
513 double cx,
514 double cy,
515 double r,
516 double px,
517 double py
518 ) {
519 return pdist2(cx, cy, px, py) <= r * r;
520 }
521
522 // END SECTION HELPERS
523
524 static void gtk_dial_state_flags_changed(
525 GtkWidget *widget,
526 GtkStateFlags previous_flags
527 ) {
528 GTK_DIAL(widget)->static_cache_valid = 0;
529 gtk_widget_queue_draw(widget);
530 }
531
532 static void gtk_dial_class_init(GtkDialClass *klass) {
533 GtkWidgetClass *w_class = GTK_WIDGET_CLASS(klass);
534 GObjectClass *g_class = G_OBJECT_CLASS(klass);
535 GtkWidgetClass *p_class = GTK_WIDGET_CLASS(gtk_dial_parent_class);
536
537 g_class->set_property = &gtk_dial_set_property;
538 g_class->get_property = &gtk_dial_get_property;
539 g_class->dispose = &gtk_dial_dispose;
540
541 w_class->size_allocate = p_class->size_allocate;
542 w_class->measure = &dial_measure;
543 w_class->snapshot = &dial_snapshot;
544 w_class->grab_focus = p_class->grab_focus;
545 w_class->focus = p_class->focus;
546 w_class->state_flags_changed = &gtk_dial_state_flags_changed;
547
548 klass->move_slider = &gtk_dial_move_slider;
549 klass->value_changed = NULL;
550
551 gtk_widget_class_set_css_name(w_class, "dial");
552
553 /**
554 * GtkDial:adjustment: (attributes org.gtk.Method.get=gtk_dial_get_adjustment org.gtk.Method.set=gtk_dial_set_adjustment)
555 *
556 * The GtkAdjustment that contains the current value of this dial object.
557 */
558 properties[PROP_ADJUSTMENT] = g_param_spec_object(
559 "adjustment",
560 "Adjustment",
561 "The GtkAdjustment that contains the current value of this dial object",
562 GTK_TYPE_ADJUSTMENT,
563 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
564 );
565
566 /**
567 * GtkDial:round_digits: (attributes org.gtk.Method.get=gtk_dial_get_round_digits org.gtk.Method.set=gtk_dial_set_round_digits)
568 *
569 * Limits the number of decimal points this GtkDial will store (default 0: integers).
570 */
571 properties[PROP_ROUND_DIGITS] = g_param_spec_int(
572 "round_digits",
573 "RoundDigits",
574 "Limits the number of decimal points this GtkDial will store",
575 -1, 1000,
576 -1,
577 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
578 );
579
580 /**
581 * GtkDial:zero_db: (attributes org.gtk.Method.get=gtk_dial_get_zero_db org.gtk.Method.set=gtk_dial_set_zero_db)
582 *
583 * The zero-dB value of the dial.
584 */
585 properties[PROP_ZERO_DB] = g_param_spec_double(
586 "zero_db",
587 "ZerodB",
588 "The zero-dB value of the dial",
589 -G_MAXDOUBLE, G_MAXDOUBLE,
590 -G_MAXDOUBLE,
591 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
592 );
593
594 /**
595 * GtkDial:off_db: (attributes org.gtk.Method.get=gtk_dial_get_off_db org.gtk.Method.set=gtk_dial_set_off_db)
596 *
597 * Values above the lower value of the adjustment up to this value
598 * will be considered as the minimum value + 1 (so will display as
599 * just-above-zero).
600 */
601 properties[PROP_OFF_DB] = g_param_spec_double(
602 "off_db",
603 "OffdB",
604 "Values up to this value will be considered as almost-silence",
605 -G_MAXDOUBLE, G_MAXDOUBLE,
606 -G_MAXDOUBLE,
607 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
608 );
609
610 /**
611 * GtkDial:is_linear: (attributes org.gtk.Method.get=gtk_dial_get_is_linear org.gtk.Method.set=gtk_dial_set_is_linear)
612 *
613 * Whether the dial values are linear or dB.
614 */
615 properties[PROP_IS_LINEAR] = g_param_spec_boolean(
616 "is_linear",
617 "IsLinear",
618 "Whether the dial values are linear or dB",
619 FALSE,
620 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
621 );
622
623 /**
624 * GtkDial:taper: (attributes org.gtk.Method.get=gtk_dial_get_taper org.gtk.Method.set=gtk_dial_set_taper)
625 *
626 * The taper of the dial.
627 */
628 properties[PROP_TAPER] = g_param_spec_int(
629 "taper",
630 "Taper",
631 "The taper of the dial",
632 GTK_DIAL_TAPER_LINEAR, GTK_DIAL_TAPER_LOG,
633 GTK_DIAL_TAPER_LINEAR,
634 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
635 );
636
637 /**
638 * GtkDial:can-control: (attributes org.gtk.Method.get=gtk_dial_get_can_control org.gtk.Method.set=gtk_dial_set_can_control)
639 *
640 * Whether the dial can be controlled by the user (even though it
641 * might sometimes be insensitive).
642 */
643 properties[PROP_CAN_CONTROL] = g_param_spec_boolean(
644 "can-control",
645 "CanControl",
646 "Whether the dial can be controlled by the user",
647 TRUE,
648 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
649 );
650
651 /**
652 * GtkDial:peak-hold: (attributes org.gtk.Method.get=gtk_dial_get_peak_hold org.gtk.Method.set=gtk_dial_set_peak_hold)
653 *
654 * The number of milliseconds to hold the peak value.
655 */
656 properties[PROP_PEAK_HOLD] = g_param_spec_int(
657 "peak-hold",
658 "PeakHold",
659 "The number of milliseconds to hold the peak value",
660 0, 1000,
661 0,
662 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
663 );
664
665 /**
666 * GtkDial:level: (attributes org.gtk.Method.get=gtk_dial_get_level org.gtk.Method.set=gtk_dial_set_level)
667 *
668 * The signal level for metering (in dB).
669 */
670 properties[PROP_LEVEL] = g_param_spec_double(
671 "level",
672 "Level",
673 "The signal level for metering",
674 -G_MAXDOUBLE, G_MAXDOUBLE,
675 -80.0,
676 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
677 );
678
679 /**
680 * GtkDial:show-level: (attributes org.gtk.Method.get=gtk_dial_get_show_level org.gtk.Method.set=gtk_dial_set_show_level)
681 *
682 * Whether to show the level meter.
683 */
684 properties[PROP_SHOW_LEVEL] = g_param_spec_boolean(
685 "show-level",
686 "ShowLevel",
687 "Whether to show the level meter",
688 FALSE,
689 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
690 );
691
692 /**
693 * GtkDial:show-value: (attributes org.gtk.Method.get=gtk_dial_get_show_value org.gtk.Method.set=gtk_dial_set_show_value)
694 *
695 * Whether to show the value arc (defaults TRUE).
696 */
697 properties[PROP_SHOW_VALUE] = g_param_spec_boolean(
698 "show-value",
699 "ShowValue",
700 "Whether to show the value arc",
701 TRUE,
702 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
703 );
704
705 g_object_class_install_properties(g_class, LAST_PROP, properties);
706
707 /**
708 * GtkDial::value-changed:
709 * @dial: the `GtkDial` that received the signal
710 *
711 * Emitted when the dial value changes.
712 */
713 signals[VALUE_CHANGED] = g_signal_new(
714 "value-changed",
715 G_TYPE_FROM_CLASS(g_class),
716 G_SIGNAL_RUN_LAST,
717 G_STRUCT_OFFSET(GtkDialClass, value_changed),
718 NULL, NULL,
719 NULL,
720 G_TYPE_NONE, 0
721 );
722
723 /**
724 * GtkDial::move-slider:
725 * @Dial: the `GtkDial` that received the signal
726 * @step: how to move the slider
727 *
728 * Virtual function that moves the slider.
729 *
730 * Used for keybindings.
731 */
732 signals[MOVE_SLIDER] = g_signal_new(
733 "move-slider",
734 G_TYPE_FROM_CLASS(g_class),
735 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
736 G_STRUCT_OFFSET(GtkDialClass, move_slider),
737 NULL, NULL,
738 NULL,
739 G_TYPE_NONE, 1,
740 GTK_TYPE_SCROLL_TYPE
741 );
742
743 add_slider_binding(w_class, binding_set, GDK_KEY_Left, 0, GTK_SCROLL_STEP_LEFT);
744 add_slider_binding(w_class, binding_set, GDK_KEY_Down, 0, GTK_SCROLL_STEP_LEFT);
745 add_slider_binding(w_class, binding_set, GDK_KEY_Right, 0, GTK_SCROLL_STEP_RIGHT);
746 add_slider_binding(w_class, binding_set, GDK_KEY_Up, 0, GTK_SCROLL_STEP_RIGHT);
747 add_slider_binding(w_class, binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_RIGHT);
748 add_slider_binding(w_class, binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_LEFT);
749 add_slider_binding(w_class, binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START);
750 add_slider_binding(w_class, binding_set, GDK_KEY_End, 0, GTK_SCROLL_END);
751 }
752
753 static void gtk_dial_focus_change_cb(
754 GtkEventControllerFocus *controller, GtkDial *dial
755 ) {
756 dial->static_cache_valid = 0;
757 gtk_widget_queue_draw(GTK_WIDGET(dial));
758 }
759
760 static void gtk_dial_init(GtkDial *dial) {
761 gtk_widget_set_focusable(GTK_WIDGET(dial), TRUE);
762
763 dial->adj = NULL;
764
765 dial->grab = GRAB_NONE;
766 dial->drag_gesture = gtk_gesture_drag_new();
767 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(dial->drag_gesture), 0);
768 g_signal_connect(
769 dial->drag_gesture, "drag-begin",
770 G_CALLBACK(gtk_dial_drag_gesture_begin), dial
771 );
772 g_signal_connect(
773 dial->drag_gesture, "drag-update",
774 G_CALLBACK(gtk_dial_drag_gesture_update), dial
775 );
776 g_signal_connect(
777 dial->drag_gesture, "drag-end",
778 G_CALLBACK(gtk_dial_drag_gesture_end), dial
779 );
780 gtk_widget_add_controller(
781 GTK_WIDGET(dial), GTK_EVENT_CONTROLLER(dial->drag_gesture)
782 );
783
784 dial->click_gesture = gtk_gesture_click_new();
785 gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(dial->click_gesture), 0);
786 g_signal_connect(
787 dial->click_gesture, "pressed",
788 G_CALLBACK(gtk_dial_click_gesture_pressed), dial
789 );
790 gtk_widget_add_controller(
791 GTK_WIDGET(dial), GTK_EVENT_CONTROLLER(dial->click_gesture)
792 );
793 gtk_gesture_group(dial->click_gesture, dial->drag_gesture);
794
795 dial->scroll_controller = gtk_event_controller_scroll_new(
796 GTK_EVENT_CONTROLLER_SCROLL_BOTH_AXES
797 );
798 g_signal_connect(
799 dial->scroll_controller, "scroll",
800 G_CALLBACK(gtk_dial_scroll_controller_scroll), dial
801 );
802 gtk_widget_add_controller(GTK_WIDGET(dial), dial->scroll_controller);
803
804 GtkEventController *controller = gtk_event_controller_focus_new();
805 g_signal_connect(
806 controller, "enter", G_CALLBACK(gtk_dial_focus_change_cb), dial
807 );
808 g_signal_connect(
809 controller, "leave", G_CALLBACK(gtk_dial_focus_change_cb), dial
810 );
811 gtk_widget_add_controller(GTK_WIDGET(dial), controller);
812
813 dial->current_peak = -INFINITY;
814 dial->hist_head = 0;
815 dial->hist_tail = 0;
816 dial->hist_count = 0;
817
818 dial->level = -80.0;
819 dial->show_level = FALSE;
820
821 // create level adjustment with dB range
822 dial->level_adj = gtk_adjustment_new(-80.0, -80.0, 0.0, 1.0, 6.0, 0.0);
823 g_object_ref_sink(dial->level_adj);
824 }
825
826 static void dial_measure(
827 GtkWidget *widget,
828 GtkOrientation orientation,
829 int for_size,
830 int *minimum,
831 int *natural,
832 int *minimum_baseline,
833 int *natural_baseline
834 ) {
835 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
836 *minimum = DIAL_MIN_WIDTH;
837 *natural = DIAL_MAX_WIDTH;
838 } else {
839 *minimum = calculate_dial_height(DIAL_MIN_WIDTH);
840 *natural = calculate_dial_height(DIAL_MAX_WIDTH);
841 }
842 *minimum_baseline = -1;
843 *natural_baseline = -1;
844 }
845
846 // internal replacement for cairo_set_source_rgba() that dims the
847 // color if the widget is insensitive
848 static void cairo_set_source_rgba_dim(
849 cairo_t *cr,
850 double r,
851 double g,
852 double b,
853 double a,
854 int dim
855 ) {
856 if (dim)
857 cairo_set_source_rgba(cr, r * 0.5, g * 0.5, b * 0.5, a);
858 else
859 cairo_set_source_rgba(cr, r, g, b, a);
860 }
861
862 static void draw_peak(GtkDial *dial, cairo_t *cr, double radius) {
863
864 // don't draw if peak is at or below minimum
865 if (dial->peak_angle <= ANGLE_START)
866 return;
867
868 double angle_start = dial->peak_angle - M_PI / 180;
869 if (angle_start < ANGLE_START)
870 angle_start = ANGLE_START;
871
872 // determine the colour of the peak
873 int count = dial->level_breakpoints_count;
874
875 // if there are no colours, don't draw the peak
876 if (!count)
877 return;
878
879 int i;
880
881 for (i = 0; i < count - 1; i++)
882 if (dial->current_peak < dial->level_breakpoints[i + 1])
883 break;
884
885 const double *colours = &dial->level_colours[i * 3];
886
887 cairo_set_source_rgba_dim(
888 cr, colours[0], colours[1], colours[2], 0.5, 0
889 );
890 cairo_set_line_width(cr, 2);
891 cairo_arc(cr, dial->cx, dial->cy, radius, ANGLE_START, dial->peak_angle);
892 cairo_stroke(cr);
893
894 cairo_set_source_rgba_dim(
895 cr, colours[0], colours[1], colours[2], 1, 0
896 );
897 cairo_set_line_width(cr, 4);
898 cairo_arc(cr, dial->cx, dial->cy, radius, angle_start, dial->peak_angle);
899 cairo_stroke(cr);
900 }
901
902 static void show_peak_value(GtkDial *dial, cairo_t *cr) {
903 double value = round(dial->current_peak);
904
905 if (value <= gtk_adjustment_get_lower(dial->level_adj))
906 return;
907
908 char s[20];
909 char *p = s;
910 if (value < 0)
911 p += sprintf(p, "−");
912 snprintf(p, 10, "%.0f", fabs(value));
913
914 pango_layout_set_text(dial->peak_layout, s, -1);
915
916 int width, height;
917 pango_layout_get_pixel_size(dial->peak_layout, &width, &height);
918
919 cairo_set_source_rgba_dim(cr, 1, 1, 1, 0.5, 0);
920
921 cairo_move_to(
922 cr,
923 dial->cx - width / 2 - 1,
924 dial->cy - height / 2
925 );
926
927 pango_cairo_show_layout(cr, dial->peak_layout);
928 }
929
930 // draw the level arc (coloured, uses level_angle)
931 static void draw_level_arc(
932 GtkDial *dial,
933 cairo_t *cr,
934 double radius,
935 double thickness,
936 double alpha
937 ) {
938 cairo_set_line_width(cr, thickness);
939
940 int count = dial->level_breakpoints_count;
941
942 if (!count) {
943 cairo_arc(cr, dial->cx, dial->cy, radius, ANGLE_START, dial->level_angle);
944 cairo_set_source_rgba_dim(cr, 1, 1, 1, alpha, 0);
945 cairo_stroke(cr);
946 return;
947 }
948
949 // if the last breakpoint is at the upper limit, then the maximum
950 // value is displayed with the whole arc in that colour
951 if (dial->level_breakpoint_angles[count - 1] == ANGLE_END &&
952 dial->level_angle == ANGLE_END) {
953 const double *colours = &dial->level_colours[(count - 1) * 3];
954
955 cairo_set_source_rgba_dim(
956 cr,
957 colours[0], colours[1], colours[2],
958 alpha,
959 0
960 );
961
962 cairo_arc(cr, dial->cx, dial->cy, radius, ANGLE_START, ANGLE_END);
963 cairo_stroke(cr);
964 return;
965 }
966
967 for (int i = 0; i < count; i++) {
968 const double *colours = &dial->level_colours[i * 3];
969
970 cairo_set_source_rgba_dim(
971 cr,
972 colours[0], colours[1], colours[2],
973 alpha,
974 0
975 );
976
977 double angle_start = dial->level_breakpoint_angles[i];
978 double angle_end =
979 i == count - 1
980 ? ANGLE_END
981 : dial->level_breakpoint_angles[i + 1];
982
983 if (dial->level_angle < angle_end) {
984 cairo_arc(cr, dial->cx, dial->cy, radius, angle_start, dial->level_angle);
985 cairo_stroke(cr);
986 return;
987 }
988
989 cairo_arc(cr, dial->cx, dial->cy, radius, angle_start, angle_end);
990 cairo_stroke(cr);
991 }
992 }
993
994 // draw the value arc (white, for gain setting display)
995 static void draw_value_arc(
996 GtkDial *dial,
997 cairo_t *cr,
998 double radius,
999 double thickness,
1000 double alpha
1001 ) {
1002 if (dial->valp <= 0.0)
1003 return;
1004
1005 cairo_set_line_width(cr, thickness);
1006 cairo_arc(cr, dial->cx, dial->cy, radius, ANGLE_START, dial->angle);
1007 cairo_set_source_rgba_dim(cr, 1, 1, 1, alpha, dial->dim);
1008 cairo_stroke(cr);
1009 }
1010
1011 // draw level indicator with blur/glow at specified radius
1012 static void draw_level_indicator(
1013 GtkDial *dial,
1014 cairo_t *cr,
1015 double radius,
1016 double shadow_radius
1017 ) {
1018 if (dial->level_valp <= 0.0)
1019 return;
1020
1021 // outside level shadow
1022 draw_level_arc(dial, cr, shadow_radius, dial->slider_thickness / 2, 0.1);
1023 // level blur 2
1024 draw_level_arc(dial, cr, radius, 6, 0.3);
1025 // level blur 1
1026 draw_level_arc(dial, cr, radius, 4, 0.5);
1027 // level arc (keep bright)
1028 draw_level_arc(dial, cr, radius, 2, 1);
1029 }
1030
1031 // draw a tick mark symmetrical about the arc path
1032 static void draw_arc_tick(
1033 GtkDial *dial,
1034 cairo_t *cr,
1035 double dx, // unit direction x (from centre)
1036 double dy, // unit direction y (from centre)
1037 double alpha,
1038 double offset // extent either side of arc
1039 ) {
1040 double inner_r = dial->slider_radius - offset;
1041 double outer_r = dial->slider_radius + offset;
1042 cairo_move_to(cr, dial->cx + dx * inner_r, dial->cy + dy * inner_r);
1043 cairo_line_to(cr, dial->cx + dx * outer_r, dial->cy + dy * outer_r);
1044 cairo_set_line_width(cr, 2);
1045 cairo_set_source_rgba_dim(cr, 1, 1, 1, alpha, dial->dim);
1046 cairo_stroke(cr);
1047 }
1048
1049 // draw the static parts of the dial to the cache surface
1050 static void dial_draw_static(GtkDial *dial) {
1051 int w = (int)dial->w;
1052 int h = (int)dial->h;
1053
1054 if (dial->static_cache)
1055 cairo_surface_destroy(dial->static_cache);
1056
1057 dial->static_cache = cairo_image_surface_create(
1058 CAIRO_FORMAT_ARGB32, w, h
1059 );
1060
1061 cairo_t *cr = cairo_create(dial->static_cache);
1062 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
1063 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1064
1065 // 1. background line (thin grey arc showing full range)
1066 cairo_arc(
1067 cr, dial->cx, dial->cy,
1068 dial->slider_radius, ANGLE_START, ANGLE_END
1069 );
1070 cairo_set_line_width(cr, 2);
1071 cairo_set_source_rgba_dim(cr, 1, 1, 1, 0.17, dial->dim);
1072 cairo_stroke(cr);
1073
1074 // 2. dim tick at zero dB
1075 double zero_db = gtk_dial_get_zero_db(dial);
1076 double tick_large = dial->slider_thickness / 4;
1077 double tick_small = dial->slider_thickness / 6;
1078 if (zero_db != -G_MAXDOUBLE) {
1079 draw_arc_tick(
1080 dial, cr,
1081 (dial->zero_db_x - dial->cx) / dial->slider_radius,
1082 (dial->zero_db_y - dial->cy) / dial->slider_radius,
1083 0.17, tick_large
1084 );
1085 }
1086
1087 // 3. value arc (for dials with show_value)
1088 if (dial->show_value) {
1089 double value = gtk_dial_get_value(dial);
1090 int at_min = value == gtk_adjustment_get_lower(dial->adj);
1091 if (at_min ||
1092 value == gtk_adjustment_get_upper(dial->adj) ||
1093 (zero_db != -G_MAXDOUBLE && value == zero_db)) {
1094 draw_arc_tick(
1095 dial, cr,
1096 (dial->slider_cx - dial->cx) / dial->slider_radius,
1097 (dial->slider_cy - dial->cy) / dial->slider_radius,
1098 0.5, at_min ? tick_small : tick_large
1099 );
1100 }
1101
1102 draw_value_arc(dial, cr, dial->slider_radius, 4, 0.5);
1103 draw_value_arc(dial, cr, dial->slider_radius, 2, 1);
1104 }
1105
1106 // 4. fill the knob circle
1107 int has_focus = gtk_widget_has_focus(GTK_WIDGET(dial));
1108 cairo_set_source(
1109 cr, dial->fill_pattern[has_focus][dial->dim]
1110 );
1111 cairo_arc(
1112 cr, dial->cx, dial->cy, dial->knob_radius, 0, 2 * M_PI
1113 );
1114 cairo_fill(cr);
1115
1116 // 5. draw the knob outline
1117 cairo_set_source(cr, dial->outline_pattern[dial->dim]);
1118 cairo_arc(
1119 cr, dial->cx, dial->cy, dial->knob_radius, 0, 2 * M_PI
1120 );
1121 cairo_set_line_width(cr, 2);
1122 cairo_stroke(cr);
1123
1124 // 6. focus ring
1125 if (has_focus) {
1126 cairo_new_path(cr);
1127 cairo_set_source_rgba(cr, 1, 0.125, 0.125, 0.5);
1128 cairo_set_line_width(cr, 2);
1129 cairo_arc(
1130 cr, dial->cx, dial->cy,
1131 dial->knob_radius + 2, 0, 2 * M_PI
1132 );
1133 cairo_stroke(cr);
1134 }
1135
1136 cairo_destroy(cr);
1137 dial->static_cache_valid = 1;
1138 }
1139
1140 static void dial_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) {
1141 GtkDial *dial = GTK_DIAL(widget);
1142
1143 if (update_dial_properties(dial)) {
1144 dial->static_cache_valid = 0;
1145 update_dial_values(dial);
1146 update_dial_level_values(dial);
1147 }
1148
1149 if (!dial->static_cache_valid)
1150 dial_draw_static(dial);
1151
1152 cairo_t *cr = gtk_snapshot_append_cairo(
1153 snapshot,
1154 &GRAPHENE_RECT_INIT(0, 0, dial->w, dial->h)
1155 );
1156
1157 // paint cached static layer
1158 cairo_set_source_surface(cr, dial->static_cache, 0, 0);
1159 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1160 cairo_paint(cr);
1161
1162 // 7. peak hold indicator on outer arc (level-only dials)
1163 cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
1164 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1165
1166 if (dial->peak_hold && dial->show_level && !dial->show_value)
1167 draw_peak(dial, cr, dial->slider_radius);
1168
1169 // 8. level arc on outer ring (level-only dials)
1170 if (dial->show_level && !dial->show_value)
1171 draw_level_indicator(
1172 dial, cr,
1173 dial->slider_radius, dial->background_radius
1174 );
1175
1176 // 9. level arc inside knob (dual-purpose dials)
1177 if (dial->show_level && dial->show_value) {
1178 if (dial->peak_hold)
1179 draw_peak(dial, cr, dial->knob_radius);
1180
1181 draw_level_indicator(
1182 dial, cr, dial->knob_radius, dial->knob_radius
1183 );
1184 }
1185
1186 // 10. peak value text
1187 if (dial->peak_hold && dial->show_level)
1188 show_peak_value(dial, cr);
1189
1190 cairo_destroy(cr);
1191 }
1192
1193 GtkWidget *gtk_dial_new(GtkAdjustment *adjustment) {
1194 g_return_val_if_fail(
1195 adjustment == NULL || GTK_IS_ADJUSTMENT(adjustment),
1196 NULL
1197 );
1198
1199 return g_object_new(
1200 GTK_TYPE_DIAL,
1201 "adjustment", adjustment,
1202 NULL
1203 );
1204 }
1205
1206 GtkWidget *gtk_dial_new_with_range(
1207 double min,
1208 double max,
1209 double step,
1210 double page
1211 ) {
1212 GtkAdjustment *adj;
1213 int digits;
1214
1215 g_return_val_if_fail(min < max, NULL);
1216
1217 adj = gtk_adjustment_new(min, min, max, step, page, 0);
1218
1219 if (step == 0.0) {
1220 digits = -1;
1221 } else if (fabs(step) >= 1.0) {
1222 digits = 0;
1223 } else {
1224 digits = abs((int)floor(log10(fabs(step))));
1225 if (digits > 5)
1226 digits = 5;
1227 }
1228
1229 return g_object_new(
1230 GTK_TYPE_DIAL,
1231 "adjustment", adj,
1232 "round_digits", digits,
1233 NULL
1234 );
1235 }
1236
1237 static void gtk_dial_set_property(
1238 GObject *object,
1239 guint prop_id,
1240 const GValue *value,
1241 GParamSpec *pspec
1242 ) {
1243 GtkDial *dial = GTK_DIAL(object);
1244
1245 switch (prop_id) {
1246 case PROP_ADJUSTMENT:
1247 gtk_dial_set_adjustment(dial, g_value_get_object(value));
1248 break;
1249 case PROP_ROUND_DIGITS:
1250 gtk_dial_set_round_digits(dial, g_value_get_int(value));
1251 break;
1252 case PROP_ZERO_DB:
1253 gtk_dial_set_zero_db(dial, g_value_get_double(value));
1254 break;
1255 case PROP_OFF_DB:
1256 gtk_dial_set_off_db(dial, g_value_get_double(value));
1257 break;
1258 case PROP_IS_LINEAR:
1259 gtk_dial_set_is_linear(dial, g_value_get_boolean(value));
1260 break;
1261 case PROP_TAPER:
1262 gtk_dial_set_taper(dial, g_value_get_int(value));
1263 break;
1264 case PROP_CAN_CONTROL:
1265 gtk_dial_set_can_control(dial, g_value_get_boolean(value));
1266 break;
1267 case PROP_PEAK_HOLD:
1268 gtk_dial_set_peak_hold(dial, g_value_get_int(value));
1269 break;
1270 case PROP_LEVEL:
1271 gtk_dial_set_level(dial, g_value_get_double(value));
1272 break;
1273 case PROP_SHOW_LEVEL:
1274 gtk_dial_set_show_level(dial, g_value_get_boolean(value));
1275 break;
1276 case PROP_SHOW_VALUE:
1277 gtk_dial_set_show_value(dial, g_value_get_boolean(value));
1278 break;
1279 default:
1280 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1281 break;
1282 }
1283 }
1284
1285 static void gtk_dial_get_property(
1286 GObject *object,
1287 guint prop_id,
1288 GValue *value,
1289 GParamSpec *pspec
1290 ) {
1291 GtkDial *dial = GTK_DIAL(object);
1292
1293 switch (prop_id) {
1294 case PROP_ADJUSTMENT:
1295 g_value_set_object(value, dial->adj);
1296 break;
1297 case PROP_ROUND_DIGITS:
1298 g_value_set_int(value, dial->round_digits);
1299 break;
1300 case PROP_ZERO_DB:
1301 g_value_set_double(value, dial->zero_db);
1302 break;
1303 case PROP_OFF_DB:
1304 g_value_set_double(value, dial->off_db);
1305 break;
1306 case PROP_IS_LINEAR:
1307 g_value_set_boolean(value, dial->is_linear);
1308 break;
1309 case PROP_TAPER:
1310 g_value_set_int(value, dial->taper);
1311 break;
1312 case PROP_CAN_CONTROL:
1313 g_value_set_boolean(value, dial->can_control);
1314 break;
1315 case PROP_PEAK_HOLD:
1316 g_value_set_int(value, dial->peak_hold);
1317 break;
1318 case PROP_LEVEL:
1319 g_value_set_double(value, dial->level);
1320 break;
1321 case PROP_SHOW_LEVEL:
1322 g_value_set_boolean(value, dial->show_level);
1323 break;
1324 case PROP_SHOW_VALUE:
1325 g_value_set_boolean(value, dial->show_value);
1326 break;
1327 default:
1328 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
1329 break;
1330 }
1331 }
1332
1333 double gtk_dial_get_value(GtkDial *dial) {
1334 return gtk_adjustment_get_value(dial->adj);
1335 }
1336
1337 void gtk_dial_set_value(GtkDial *dial, double value) {
1338 if (set_value(dial, value))
1339 gtk_widget_queue_draw(GTK_WIDGET(dial));
1340 }
1341
1342 void gtk_dial_set_round_digits(GtkDial *dial, int round_digits) {
1343 dial->round_digits = round_digits;
1344 gtk_dial_set_value(dial, gtk_dial_get_value(dial));
1345 }
1346
1347 int gtk_dial_get_round_digits(GtkDial *dial) {
1348 return dial->round_digits;
1349 }
1350
1351 void gtk_dial_set_zero_db(GtkDial *dial, double zero_db) {
1352 dial->zero_db = zero_db;
1353 dial->properties_updated = 1;
1354 dial->static_cache_valid = 0;
1355 }
1356
1357 double gtk_dial_get_zero_db(GtkDial *dial) {
1358 return dial->zero_db;
1359 }
1360
1361 void gtk_dial_set_off_db(GtkDial *dial, double off_db) {
1362 dial->off_db = off_db;
1363 dial->properties_updated = 1;
1364 dial->static_cache_valid = 0;
1365 }
1366
1367 double gtk_dial_get_off_db(GtkDial *dial) {
1368 return dial->off_db;
1369 }
1370
1371 void gtk_dial_set_is_linear(GtkDial *dial, gboolean is_linear) {
1372 dial->is_linear = is_linear;
1373 dial->properties_updated = 1;
1374 dial->static_cache_valid = 0;
1375 }
1376
1377 gboolean gtk_dial_get_is_linear(GtkDial *dial) {
1378 return dial->is_linear;
1379 }
1380
1381 void gtk_dial_set_taper(GtkDial *dial, int taper) {
1382 dial->taper = taper;
1383 dial->properties_updated = 1;
1384 dial->static_cache_valid = 0;
1385 }
1386
1387 int gtk_dial_get_taper(GtkDial *dial) {
1388 return dial->taper;
1389 }
1390
1391 void gtk_dial_set_taper_linear_breakpoints(
1392 GtkDial *dial,
1393 const double *breakpoints,
1394 const double *outputs,
1395 int count
1396 ) {
1397 dial->properties_updated = 1;
1398 dial->static_cache_valid = 0;
1399
1400 free(dial->taper_breakpoints);
1401 free(dial->taper_outputs);
1402 dial->taper_breakpoints = NULL;
1403 dial->taper_outputs = NULL;
1404 dial->taper_breakpoints_count = 0;
1405
1406 if (count < 1)
1407 return;
1408
1409 int total_count = count + 2;
1410
1411 dial->taper_breakpoints = malloc(total_count * sizeof(double));
1412 dial->taper_outputs = malloc(total_count * sizeof(double));
1413
1414 dial->taper_breakpoints[0] = 0;
1415 dial->taper_outputs[0] = 0;
1416
1417 for (int i = 0; i < count; i++) {
1418 dial->taper_breakpoints[i + 1] = breakpoints[i];
1419 dial->taper_outputs[i + 1] = outputs[i];
1420 }
1421
1422 dial->taper_breakpoints[total_count - 1] = 1;
1423 dial->taper_outputs[total_count - 1] = 1;
1424
1425 dial->taper_breakpoints_count = total_count;
1426 }
1427
1428 void gtk_dial_set_can_control(GtkDial *dial, gboolean can_control) {
1429 dial->can_control = can_control;
1430 dial->properties_updated = 1;
1431 dial->static_cache_valid = 0;
1432 }
1433
1434 gboolean gtk_dial_get_can_control(GtkDial *dial) {
1435 return dial->can_control;
1436 }
1437
1438 void gtk_dial_set_level_meter_colours(
1439 GtkDial *dial,
1440 const int *breakpoints,
1441 const double *colours,
1442 int count
1443 ) {
1444 dial->level_breakpoints = breakpoints;
1445 dial->level_colours = colours;
1446 dial->level_breakpoints_count = count;
1447 dial->properties_updated = 1;
1448 dial->static_cache_valid = 0;
1449 }
1450
1451 void gtk_dial_set_peak_hold(GtkDial *dial, int peak_hold) {
1452 dial->peak_hold = peak_hold;
1453 }
1454
1455 int gtk_dial_get_peak_hold(GtkDial *dial) {
1456 return dial->peak_hold;
1457 }
1458
1459 void gtk_dial_set_level(GtkDial *dial, double level) {
1460 if (set_level(dial, level))
1461 gtk_widget_queue_draw(GTK_WIDGET(dial));
1462 }
1463
1464 double gtk_dial_get_level(GtkDial *dial) {
1465 return dial->level;
1466 }
1467
1468 void gtk_dial_set_show_level(GtkDial *dial, gboolean show_level) {
1469 dial->show_level = show_level;
1470 dial->properties_updated = 1;
1471 dial->static_cache_valid = 0;
1472 gtk_widget_queue_draw(GTK_WIDGET(dial));
1473 }
1474
1475 gboolean gtk_dial_get_show_level(GtkDial *dial) {
1476 return dial->show_level;
1477 }
1478
1479 void gtk_dial_set_show_value(GtkDial *dial, gboolean show_value) {
1480 dial->show_value = show_value;
1481 dial->properties_updated = 1;
1482 dial->static_cache_valid = 0;
1483 gtk_widget_queue_draw(GTK_WIDGET(dial));
1484 }
1485
1486 gboolean gtk_dial_get_show_value(GtkDial *dial) {
1487 return dial->show_value;
1488 }
1489
1490 void gtk_dial_set_adjustment(GtkDial *dial, GtkAdjustment *adj) {
1491 if (!(adj == NULL || GTK_IS_ADJUSTMENT(adj)))
1492 return;
1493 if (dial->adj)
1494 g_object_unref(dial->adj);
1495 dial->adj = adj;
1496 g_object_ref_sink(dial->adj);
1497 g_signal_emit(dial, signals[VALUE_CHANGED], 0);
1498 dial->static_cache_valid = 0;
1499 gtk_widget_queue_draw(GTK_WIDGET(dial));
1500 }
1501
1502 GtkAdjustment *gtk_dial_get_adjustment(GtkDial *dial) {
1503 return dial->adj;
1504 }
1505
1506 static void gtk_dial_add_hist_value(GtkDial *dial, double value) {
1507
1508 int need_peak_update = 0;
1509
1510 // remove the oldest value(s) if they are too old or if the history
1511 // is full
1512 while (dial->hist_count > 0 &&
1513 (dial->hist_time[dial->hist_head] < current_time - dial->peak_hold ||
1514 dial->hist_count == HISTORY_COUNT)) {
1515
1516 // check if the value removed is the current peak
1517 if (dial->hist_values[dial->hist_head] >= dial->current_peak)
1518 need_peak_update = 1;
1519
1520 // move the head forward
1521 dial->hist_head = (dial->hist_head + 1) % HISTORY_COUNT;
1522 dial->hist_count--;
1523 }
1524
1525 // recalculate the peak if needed
1526 if (need_peak_update) {
1527 dial->current_peak = -INFINITY;
1528 for (int i = dial->hist_head;
1529 i != dial->hist_tail;
1530 i = (i + 1) % HISTORY_COUNT)
1531 if (dial->hist_values[i] > dial->current_peak)
1532 dial->current_peak = dial->hist_values[i];
1533 }
1534
1535 // add the new value
1536 dial->hist_values[dial->hist_tail] = value;
1537 dial->hist_time[dial->hist_tail] = current_time;
1538 dial->hist_tail = (dial->hist_tail + 1) % HISTORY_COUNT;
1539 dial->hist_count++;
1540
1541 // update the peak if needed
1542 if (value > dial->current_peak)
1543 dial->current_peak = value;
1544 }
1545
1546 static int set_value(GtkDial *dial, double newval) {
1547 if (dial->round_digits >= 0) {
1548 double power;
1549 int i;
1550
1551 i = dial->round_digits;
1552 power = 1;
1553 while (i--)
1554 power *= 10;
1555
1556 newval = floor((newval * power) + 0.5) / power;
1557 }
1558
1559 if (newval < gtk_adjustment_get_lower(dial->adj))
1560 newval = gtk_adjustment_get_lower(dial->adj);
1561 else if (newval > gtk_adjustment_get_upper(dial->adj))
1562 newval = gtk_adjustment_get_upper(dial->adj);
1563
1564 double oldval = gtk_adjustment_get_value(dial->adj);
1565
1566 if (oldval == newval)
1567 return 0;
1568
1569 gtk_adjustment_set_value(dial->adj, newval);
1570 g_signal_emit(dial, signals[VALUE_CHANGED], 0);
1571
1572 double old_valp = dial->valp;
1573 update_dial_values(dial);
1574
1575 if (old_valp != dial->valp) {
1576 dial->static_cache_valid = 0;
1577 return 1;
1578 }
1579
1580 return 0;
1581 }
1582
1583 // set the level value for metering (this tracks peaks)
1584 // level is always in dB, using level_adj range
1585 static int set_level(GtkDial *dial, double newlevel) {
1586 double old_level = dial->level;
1587 double old_peak = dial->current_peak;
1588
1589 // track peaks from level
1590 gtk_dial_add_hist_value(dial, newlevel);
1591
1592 dial->level = newlevel;
1593
1594 if (old_level == newlevel && old_peak == dial->current_peak)
1595 return 0;
1596
1597 // update level display values using level_adj (dB range)
1598 dial->level_valp = calc_taper_adj(dial, dial->level_adj, newlevel, FALSE);
1599 dial->level_angle = calc_val(dial->level_valp, ANGLE_START, ANGLE_END);
1600
1601 // update peak angle
1602 double peak_valp = calc_taper_adj(dial, dial->level_adj, dial->current_peak, FALSE);
1603 dial->peak_angle = calc_val(peak_valp, ANGLE_START, ANGLE_END);
1604
1605 return 1;
1606 }
1607
1608 static double do_step(GtkDial *dial, double step_amount) {
1609 double mn = gtk_adjustment_get_lower(dial->adj);
1610 double mx = gtk_adjustment_get_upper(dial->adj);
1611 double newval = gtk_adjustment_get_value(dial->adj);
1612 double step = gtk_adjustment_get_step_increment(dial->adj);
1613
1614 if (gtk_dial_get_is_linear(dial)) {
1615 double db_val = linear_value_to_cdb(newval, mn, mx, -8000, 1200) / 100.0;
1616 db_val = round(db_val / step) * step + step_amount;
1617
1618 newval = cdb_to_linear_value(db_val * 100.0, mn, mx, -8000, 1200);
1619 if (newval == gtk_adjustment_get_value(dial->adj)) {
1620 newval = CLAMP(newval + (step_amount > 0 ? 1 : -1), mn, mx);
1621 }
1622 } else {
1623 newval += step_amount;
1624 }
1625
1626 return newval;
1627 }
1628
1629 static void step_back(GtkDial *dial) {
1630 set_value(dial, do_step(dial, -gtk_adjustment_get_step_increment(dial->adj)));
1631 }
1632
1633 static void step_forward(GtkDial *dial) {
1634 set_value(dial, do_step(dial, gtk_adjustment_get_step_increment(dial->adj)));
1635 }
1636
1637 static void page_back(GtkDial *dial) {
1638 set_value(dial, do_step(dial, -gtk_adjustment_get_page_increment(dial->adj)));
1639 }
1640
1641 static void page_forward(GtkDial *dial) {
1642 set_value(dial, do_step(dial, gtk_adjustment_get_page_increment(dial->adj)));
1643 }
1644
1645 static void scroll_begin(GtkDial *dial) {
1646 double newval = gtk_adjustment_get_lower(dial->adj);
1647
1648 set_value(dial, newval);
1649 }
1650
1651 static void scroll_end(GtkDial *dial) {
1652 double newval = gtk_adjustment_get_upper(dial->adj) - gtk_adjustment_get_page_size(dial->adj);
1653
1654 set_value(dial, newval);
1655 }
1656
1657 static gboolean should_invert_move(GtkDial *dial, GtkOrientation o) {
1658 return FALSE;
1659 }
1660
1661 static void gtk_dial_move_slider(GtkDial *dial, GtkScrollType scroll) {
1662 switch (scroll) {
1663 case GTK_SCROLL_STEP_LEFT:
1664 if (should_invert_move(dial, GTK_ORIENTATION_HORIZONTAL))
1665 step_forward(dial);
1666 else
1667 step_back(dial);
1668 break;
1669
1670 case GTK_SCROLL_STEP_UP:
1671 if (should_invert_move(dial, GTK_ORIENTATION_VERTICAL))
1672 step_forward(dial);
1673 else
1674 step_back(dial);
1675 break;
1676
1677 case GTK_SCROLL_STEP_RIGHT:
1678 if (should_invert_move(dial, GTK_ORIENTATION_HORIZONTAL))
1679 step_back(dial);
1680 else
1681 step_forward(dial);
1682 break;
1683
1684 case GTK_SCROLL_STEP_DOWN:
1685 if (should_invert_move(dial, GTK_ORIENTATION_VERTICAL))
1686 step_back(dial);
1687 else
1688 step_forward(dial);
1689 break;
1690
1691 case GTK_SCROLL_STEP_BACKWARD:
1692 step_back(dial);
1693 break;
1694
1695 case GTK_SCROLL_STEP_FORWARD:
1696 step_forward(dial);
1697 break;
1698
1699 case GTK_SCROLL_PAGE_LEFT:
1700 if (should_invert_move(dial, GTK_ORIENTATION_HORIZONTAL))
1701 page_forward(dial);
1702 else
1703 page_back(dial);
1704 break;
1705
1706 case GTK_SCROLL_PAGE_UP:
1707 if (should_invert_move(dial, GTK_ORIENTATION_VERTICAL))
1708 page_forward(dial);
1709 else
1710 page_back(dial);
1711 break;
1712
1713 case GTK_SCROLL_PAGE_RIGHT:
1714 if (should_invert_move(dial, GTK_ORIENTATION_HORIZONTAL))
1715 page_back(dial);
1716 else
1717 page_forward(dial);
1718 break;
1719
1720 case GTK_SCROLL_PAGE_DOWN:
1721 if (should_invert_move(dial, GTK_ORIENTATION_VERTICAL))
1722 page_back(dial);
1723 else
1724 page_forward(dial);
1725 break;
1726
1727 case GTK_SCROLL_PAGE_BACKWARD:
1728 page_back(dial);
1729 break;
1730
1731 case GTK_SCROLL_PAGE_FORWARD:
1732 page_forward(dial);
1733 break;
1734
1735 case GTK_SCROLL_START:
1736 scroll_begin(dial);
1737 break;
1738
1739 case GTK_SCROLL_END:
1740 scroll_end(dial);
1741 break;
1742
1743 case GTK_SCROLL_JUMP:
1744 case GTK_SCROLL_NONE:
1745 default:
1746 break;
1747 }
1748
1749 gtk_widget_queue_draw(GTK_WIDGET(dial));
1750 }
1751
1752 static void gtk_dial_drag_gesture_begin(
1753 GtkGestureDrag *gesture,
1754 double offset_x,
1755 double offset_y,
1756 GtkDial *dial
1757 ) {
1758 dial->dvalp = calc_valp(
1759 gtk_dial_get_value(dial),
1760 gtk_adjustment_get_lower(dial->adj),
1761 gtk_adjustment_get_upper(dial->adj)
1762 );
1763 gtk_gesture_set_state(dial->drag_gesture, GTK_EVENT_SEQUENCE_CLAIMED);
1764 }
1765
1766 static void gtk_dial_drag_gesture_update(
1767 GtkGestureDrag *gesture,
1768 double offset_x,
1769 double offset_y,
1770 GtkDial *dial
1771 ) {
1772 double mn = gtk_adjustment_get_lower(dial->adj);
1773 double mx = gtk_adjustment_get_upper(dial->adj);
1774 gboolean is_linear = gtk_dial_get_is_linear(dial);
1775
1776 double valp;
1777
1778 // add a 1px deadband to prevent double-click with zero mouse
1779 // movement from changing the value from the toggled -inf/0dB value
1780 // (sometimes we see an offset_y value that rounds to +/- 1 which
1781 // causes the value to change after the double-click has set the
1782 // value)
1783 if (offset_y < -1) {
1784 offset_y += 1;
1785 } else if (offset_y < 1) {
1786 offset_y = 0;
1787 } else {
1788 offset_y -= 1;
1789 }
1790
1791 if (is_linear) {
1792 double step = gtk_adjustment_get_step_increment(dial->adj);
1793
1794 // Convert initial value from linear to dB space
1795 double db_val = linear_value_to_cdb(
1796 calc_val(dial->dvalp, mn, mx),
1797 mn, mx,
1798 -8000, 1200
1799 ) / 100.0;
1800
1801 // Adjust in dB space
1802 db_val -= 30.0 * DRAG_FACTOR * (offset_y / dial->h);
1803
1804 // Round
1805 db_val = round(db_val / step) * step;
1806
1807 // Convert back to linear space and normalise
1808 double val = cdb_to_linear_value(
1809 db_val * 100.0,
1810 mn, mx,
1811 -8000, 1200
1812 );
1813
1814 valp = calc_valp(val, mn, mx);
1815 } else {
1816 valp = dial->dvalp - DRAG_FACTOR * (offset_y / dial->h);
1817 valp = CLAMP(valp, 0.0, 1.0);
1818 }
1819
1820 set_value(dial, calc_val(valp, mn, mx));
1821
1822 gtk_widget_queue_draw(GTK_WIDGET(dial));
1823 }
1824
1825 static void gtk_dial_drag_gesture_end(
1826 GtkGestureDrag *gesture,
1827 double offset_x,
1828 double offset_y,
1829 GtkDial *dial
1830 ) {
1831 dial->grab = GRAB_NONE;
1832 gtk_widget_queue_draw(GTK_WIDGET(dial));
1833 }
1834
1835 static void gtk_dial_click_gesture_pressed(
1836 GtkGestureClick *gesture,
1837 int n_press,
1838 double x,
1839 double y,
1840 GtkDial *dial
1841 ) {
1842
1843 // on double (or more) click, toggle between lower and zero_db value
1844 if (n_press >= 2) {
1845 double lower = gtk_adjustment_get_lower(dial->adj);
1846
1847 if (gtk_dial_get_value(dial) != lower)
1848 set_value(dial, lower);
1849 else
1850 set_value(dial, dial->zero_db);
1851
1852 return;
1853 }
1854
1855 if (gtk_widget_get_focus_on_click(GTK_WIDGET(dial)) &&
1856 !gtk_widget_has_focus(GTK_WIDGET(dial)))
1857 gtk_widget_grab_focus(GTK_WIDGET(dial));
1858
1859 if (circle_contains_point(
1860 dial->slider_cx, dial->slider_cy, dial->radius, x, y
1861 ))
1862 dial->grab = GRAB_SLIDER;
1863 else
1864 dial->grab = GRAB_NONE;
1865
1866 gtk_widget_queue_draw(GTK_WIDGET(dial));
1867 gtk_gesture_set_state(GTK_GESTURE(gesture), GTK_EVENT_SEQUENCE_CLAIMED);
1868 }
1869
1870 static gboolean gtk_dial_scroll_controller_scroll(
1871 GtkEventControllerScroll *scroll,
1872 double dx,
1873 double dy,
1874 GtkDial *dial
1875 ) {
1876 double delta = dx ? dx : dy;
1877 double absolute_delta = fabs(delta);
1878
1879 if (absolute_delta > 1)
1880 delta *= absolute_delta;
1881
1882 double step = -gtk_adjustment_get_step_increment(dial->adj) * delta;
1883
1884 set_value(dial, do_step(dial, step));
1885 gtk_widget_queue_draw(GTK_WIDGET(dial));
1886
1887 return GDK_EVENT_STOP;
1888 }
1889
1890 void gtk_dial_dispose(GObject *o) {
1891 GtkDial *dial = GTK_DIAL(o);
1892
1893 free(dial->taper_breakpoints);
1894 dial->taper_breakpoints = NULL;
1895 free(dial->taper_outputs);
1896 dial->taper_outputs = NULL;
1897 dial->taper_breakpoints_count = 0;
1898 free(dial->level_breakpoint_angles);
1899 dial->level_breakpoint_angles = NULL;
1900
1901 if (dial->static_cache)
1902 cairo_surface_destroy(dial->static_cache);
1903 dial->static_cache = NULL;
1904
1905 for (int focus = 0; focus <= 1; focus++)
1906 for (int dim = 0; dim <= 1; dim++)
1907 if (dial->fill_pattern[focus][dim])
1908 cairo_pattern_destroy(dial->fill_pattern[focus][dim]);
1909
1910 for (int dim = 0; dim <= 1; dim++)
1911 if (dial->outline_pattern[dim])
1912 cairo_pattern_destroy(dial->outline_pattern[dim]);
1913
1914 if (dial->peak_layout)
1915 g_object_unref(dial->peak_layout);
1916 if (dial->peak_font_desc)
1917 pango_font_description_free(dial->peak_font_desc);
1918
1919 g_object_unref(dial->adj);
1920 dial->adj = NULL;
1921 g_object_unref(dial->level_adj);
1922 dial->level_adj = NULL;
1923 G_OBJECT_CLASS(gtk_dial_parent_class)->dispose(o);
1924 }