Skip to content

Instantly share code, notes, and snippets.

@angstyloop
Created October 10, 2023 21:59
Show Gist options
  • Save angstyloop/cae639c1495f8c81e82b587e4a73630a to your computer and use it in GitHub Desktop.
Save angstyloop/cae639c1495f8c81e82b587e4a73630a to your computer and use it in GitHub Desktop.
Disable scrolling on a GtkScale in GTK4.
/* No-op to prevent @w from propagating "scroll" events it receives.
*/
void disable_scroll_cb( GtkWidget *w ) {}
/* Disable scroll on a widget by adding a capture phase event handler and
* connecting a no-op callback to the "scroll" event.
*/
static GtkWidget *
disable_scroll( GtkWidget *w )
{
GtkEventController *ec;
ec = gtk_event_controller_scroll_new(
GTK_EVENT_CONTROLLER_SCROLL_VERTICAL );
gtk_event_controller_set_propagation_phase( ec, GTK_PHASE_CAPTURE );
g_signal_connect( ec, "scroll", G_CALLBACK( disable_scroll_cb ), w );
gtk_widget_add_controller( w, ec );
return w;
}
/* Create a spin button with range, default value, and optionally enabled
* scrolling.
*/
GtkWidget *
create_scale( double min, double max, double step,
double value, bool scroll )
{
GtkWidget *s;
s = gtk_scale_new_with_range( GTK_ORIENTATION_HORIZONTAL, min, max, step );
gtk_range_set_value( GTK_RANGE( s ), value );
return scroll ? s : disable_scroll( s );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment