Re: some gtk_render_* blank?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Hi adlo,

With the event box you can draw with transparency. If you want to get the CSS and combine that with a cairo drawing and have transparency so that you can see the background window through the event box window, all that can be done.

Eric

//gcc -Wall render_css1.c -o render_css1 `pkg-config --cflags --libs gtk+-3.0`

//Tested with GTK3.18 and GTK3.22

#include<gtk/gtk.h>

static gboolean draw_event_box(GtkWidget *event_box, cairo_t *cr, gpointer user_data)
  {
    gint width=gtk_widget_get_allocated_width(event_box);
    gint height=gtk_widget_get_allocated_height(event_box);

    //Draw from CSS.
    GtkStyleContext *context=gtk_widget_get_style_context(event_box);
    gtk_render_background(context, cr, 0, 0, width, height);
    gtk_render_frame(context, cr, 0, 0, width, height);

    //Draw with cairo also.
    cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
    cairo_set_line_width(cr, 10);
    cairo_translate(cr, width/2, height/2);
    cairo_arc(cr, 0, 0, 0.3*width, 0.0, 2.0*G_PI);
    cairo_stroke(cr);

    return FALSE;
  }
int main(int argc, char *argv[])
  {
    gtk_init(&argc, &argv);

    GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Event Box");
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 300);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    GtkWidget *event_box=gtk_event_box_new();
    gtk_widget_set_app_paintable(event_box, TRUE);
    gtk_widget_set_hexpand(event_box, TRUE);
    gtk_widget_set_vexpand(event_box, TRUE);
    gtk_widget_set_name(event_box, "event1");
    g_signal_connect(event_box, "draw", G_CALLBACK(draw_event_box), NULL);

    GtkWidget *label=gtk_label_new("Label");
    gtk_container_add(GTK_CONTAINER(event_box), label);

    GtkWidget *grid=gtk_grid_new();
    gtk_grid_attach(GTK_GRID(grid), event_box, 0, 0, 1, 1);
    gtk_container_add(GTK_CONTAINER(window), grid);

    GError *css_error=NULL;
    gchar *css_string=NULL;
    //Set background alpha.
    css_string=g_strdup("#event1{background: rgba(255,0,255,0.3); border: solid 20px yellow;}");
    GtkCssProvider *provider=gtk_css_provider_new();
    GdkDisplay *display=gdk_display_get_default();
    GdkScreen *screen=gdk_display_get_default_screen(display);
    gtk_style_context_add_provider_for_screen(screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_css_provider_load_from_data(provider, css_string, -1, &css_error);
    if(css_error!=NULL)
      {
        g_print("CSS loader error %s\n", css_error->message);
        g_error_free(css_error);
      }
    g_object_unref(provider);
    if(css_string!=NULL) g_free(css_string);
  
    gtk_widget_show_all(window);
    gtk_main();
    return 0;  
  }



_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux