Search code examples
cssclinuxgtk

Changing slider size on a gtk scale


I want to change the slider of a Gtk scale in a Linux c program to show an image. I'm using a CSS file to set the background image. the image is showing, but the problem is that the slider is too small to show the image completely.

I tried using min-width and min-height in the CSS file, but it does not seem to work.

Here is the CSS that I used. The border-radius and background-image work, but the min-width and min-height do nothing.

.scale .slider{
    min-width: 100px;
    min-height: 150px;
    border-radius: 0px;
    background-image: url("fader.png");
}

Edit: Here is the C code that applies the css style to the gtk elements:

...
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider, "gtktut01.css", NULL);
GtkStyleContext *context;
context = gtk_widget_get_style_context(GTK_WIDGET(window));
gtk_style_context_add_provider    (context,GTK_STYLE_PROVIDER(provider),GTK_STYLE_PROVIDER_PRIORITY_USER);

context = gtk_widget_get_style_context(GTK_WIDGET(scale));
gtk_style_context_add_provider (context,GTK_STYLE_PROVIDER(provider),GTK_STYLE_PROVIDER_PRIORITY_USER);

gtk_widget_show_all (GTK_WIDGET(window));
gtk_main ();
...

The window and scale are created using glade.


Solution

  • I commented I couldn't change the size of the knob earlier. Well, I just did. If this is not what you're asking please be more specific. Here's what I did:

    big slider knob with resized image

    Here's my fader.png I made in paint:

    enter image description here

    Here's a MWE for main.c:

    #include <gtk/gtk.h>                                                                                
    
    static void activate(GtkApplication *app, gpointer user_data)                                       
    {                                                                                                   
        GtkWidget *window;                                                                              
        GtkWidget *box;                                                                                 
        GtkWidget *scale;                                                                               
        GtkStyleContext *context;                                                                       
        GtkCssProvider *provider;                                                                       
    
        window = gtk_application_window_new(app);                                                       
        gtk_window_set_title(GTK_WINDOW(window), "Window");                                             
        gtk_container_set_border_width(GTK_CONTAINER(window), 10);                                      
        gtk_window_set_default_size(GTK_WINDOW(window), 500, 100);                                      
    
        box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);                                                
        gtk_container_add(GTK_CONTAINER(window), box);                                                  
    
        scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 100, 5);                        
        gtk_container_add(GTK_CONTAINER(box), scale);                                                   
    
        provider = gtk_css_provider_new();                                                              
        gtk_css_provider_load_from_path(provider, "mystyle.css", NULL);                                 
    
        context = gtk_widget_get_style_context(GTK_WIDGET(window));                                     
        gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    
        context = gtk_widget_get_style_context(GTK_WIDGET(scale));                                      
        gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    
        gtk_widget_show_all(window);                                                                    
    }                                                                                                   
    
    int main(int argc, char **argv)                                                                     
    {                                                                                                   
        GtkApplication *app;                                                                            
        int status;                                                                                     
    
        app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);                         
        g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);                                  
        status = g_application_run(G_APPLICATION(app), argc, argv);                                     
        g_object_unref(app);                                                                            
    
        return status;                                                                                  
    }             
    

    And here's mystyle.css:

    scale slider {                                                                                      
        background-image: -gtk-scaled(url("fader.png"));                                                
        background-size: 100px 100px;                                                                   
        min-width: 100px;                                                                               
        min-height: 100px;                                                                              
    }