Skip to content

Instantly share code, notes, and snippets.

@angstyloop
Created October 10, 2023 21:01
Show Gist options
  • Save angstyloop/329c40500502a1c30e1b70edb5371ccc to your computer and use it in GitHub Desktop.
Save angstyloop/329c40500502a1c30e1b70edb5371ccc to your computer and use it in GitHub Desktop.
Disable scrolling on a GtkSpinButton 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_spin_button( double min, double max, double step,
double value, bool scroll )
{
GtkWidget *sb;
sb = gtk_spin_button_new_with_range( min, max, step );
gtk_spin_button_set_value( GTK_SPIN_BUTTON( sb ), value );
return scroll ? sb : disable_scroll( sb );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment