/* * Build: * gcc $(pkg-config --cflags gtk+-3.0) sample.c -o sample $(pkg-config --libs gtk+-3.0) */ #include gchar* format (GtkScale *scale, gdouble value, gpointer user_data) { gint h, min; if ( (gint)value <= 14 ) return g_strdup ("Never!"); else if ( (gint)value < 60 ) return g_strdup_printf ("%d %s", (gint) value, "minutes"); else if ( (gint)value == 60) return g_strdup ("One hour"); h = (gint)value/60; min = (gint)value%60; if ( h <= 1 ) if ( min == 0 ) return g_strdup_printf ("%s", "One hour"); else if ( min == 1 ) return g_strdup_printf ("%s %s", "One hour", "one minute"); else return g_strdup_printf ("%s %d %s", "One hour", min, "minutes"); else if ( min == 0 ) return g_strdup_printf ("%d %s", h, "hours"); else if ( min == 1 ) return g_strdup_printf ("%d %s %s", h, "hours", "one minute"); else return g_strdup_printf ("%d %s %d %s", h, "hours", min, "minutes"); } gboolean sync_label (GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data) { GtkWidget *label = GTK_WIDGET (user_data); gchar *formatted_value = format (NULL, value, NULL); gtk_label_set_text (GTK_LABEL (label), formatted_value); g_free (formatted_value); return FALSE; } int main (int argc, char *argv[]) { GtkWidget *window, *box, *box2, *scale, *scale2, *label; GtkCssProvider *css_provider; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER); gtk_window_set_default_size (GTK_WINDOW (window), 150, 70); box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10); gtk_container_add (GTK_CONTAINER (window), box); scale = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 14, 360, 5); gtk_scale_set_value_pos (GTK_SCALE (scale), GTK_POS_BOTTOM); gtk_container_add (GTK_CONTAINER (box), scale); box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); gtk_container_add (GTK_CONTAINER (box), box2); scale2 = gtk_scale_new_with_range (GTK_ORIENTATION_HORIZONTAL, 14, 360, 5); gtk_style_context_add_class (gtk_widget_get_style_context (scale2), "custom-scale"); gtk_scale_set_draw_value (GTK_SCALE (scale2), FALSE); gtk_container_add (GTK_CONTAINER (box2), scale2); label = gtk_label_new ("foo"); gtk_container_add (GTK_CONTAINER (box2), label); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL); g_signal_connect (G_OBJECT (scale), "format-value", G_CALLBACK (format), NULL); g_signal_connect (G_OBJECT (scale2), "change-value", G_CALLBACK (sync_label), label); css_provider = gtk_css_provider_new (); gtk_css_provider_load_from_data (css_provider, ".custom-scale { padding-bottom: 0; }", -1, NULL); gtk_style_context_add_provider_for_screen (gdk_screen_get_default (), GTK_STYLE_PROVIDER (css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); g_object_unref (css_provider); gtk_widget_show_all (window); gtk_main (); return 0; }