Re: window with widgets and adjustable transparency




Hi Pablo,

If you have a window compositor you can draw with transparency. If you want your main window transparent and have a compositor, you can tell GTK that you are going to take care of drawing to the window. That may get you in a bit of trouble with some other widgets that expect a background drawn from the main window. Fun to draw with transparency though.

Eric


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

    Tested on Ubuntu16.04 and GTK3.18
*/

#include<gtk/gtk.h>

//Alpha of the main window.
static gdouble alpha=0.0;

//Set alpha with the slider.
static void set_alpha(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer data);
//Draw the main window transparent and paned window cyan.
static gboolean draw_main_window(GtkWidget *window, cairo_t *cr, gpointer data);
//The drawing area.
static gboolean draw_da(GtkWidget *da, cairo_t *cr, gpointer data);

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), "Tranparency");
    gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_set_app_paintable(window, TRUE);
    //Try to set transparency of main window.
    GdkScreen *screen=gtk_widget_get_screen(window); 
    if(gdk_screen_is_composited(screen))
      {
        GdkVisual *visual=gdk_screen_get_rgba_visual(screen);
        gtk_widget_set_visual(window, visual);
      }
    else g_print("Can't set window transparency.\n");

    GtkWidget *da=gtk_drawing_area_new();
    gtk_widget_set_hexpand(da, TRUE);
    gtk_widget_set_vexpand(da, TRUE);
    g_signal_connect(da, "draw", G_CALLBACK(draw_da), NULL);

    GtkWidget *alpha_label=gtk_label_new("Alpha");
    gtk_widget_set_hexpand(alpha_label, TRUE);
   
    GtkWidget *alpha_slider=gtk_scale_new_with_range(GTK_ORIENTATION_VERTICAL, 0.0, 1.0, 0.01);
    gtk_widget_set_vexpand(alpha_slider, TRUE);
    gtk_range_set_value(GTK_RANGE(alpha_slider), 0.0);
    g_signal_connect(alpha_slider, "change-value", G_CALLBACK(set_alpha), da);  

    GtkWidget *grid1=gtk_grid_new();
    gtk_container_set_border_width(GTK_CONTAINER(grid1), 15);
    gtk_grid_set_row_spacing(GTK_GRID(grid1), 8);   
    gtk_grid_attach(GTK_GRID(grid1), alpha_label, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(grid1), alpha_slider, 0, 1, 1, 1);

    GtkWidget *scroll=gtk_scrolled_window_new(NULL, NULL);
    gtk_widget_set_hexpand(scroll, TRUE);
    gtk_widget_set_vexpand(scroll, TRUE);
    gtk_container_add(GTK_CONTAINER(scroll), grid1);
  
    GtkWidget *paned1=gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_paned_pack1(GTK_PANED(paned1), scroll, TRUE, TRUE);
    gtk_paned_pack2(GTK_PANED(paned1), da, TRUE, TRUE);
    gtk_paned_set_position(GTK_PANED(paned1), 250);

    g_signal_connect(window, "draw", G_CALLBACK(draw_main_window), paned1);
  
    gtk_container_add(GTK_CONTAINER(window), paned1);
   
    gtk_widget_show_all(window);

    gtk_main();

    return 0; 
  }
static void set_alpha(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer data)
  {
    alpha=value;
    gtk_widget_queue_draw(GTK_WIDGET(data));
  }
static gboolean draw_main_window(GtkWidget *window, cairo_t *cr, gpointer data)
  {
    //Paint backing window.
    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, alpha);
    cairo_paint(cr);

    //Paint paned window.
    cairo_set_source_rgba(cr, 0.0, 1.0, 1.0, 1.0);
    gint width=gtk_paned_get_position(GTK_PANED(data));
    gint height=gtk_widget_get_allocated_height(window);

    cairo_rectangle(cr, 0.0, 0.0, width, height);
    cairo_fill(cr);
    cairo_set_source_rgba(cr, 1.0, 1.0, 0.0, 1.0);
    cairo_rectangle(cr, width, 0.0, 10, height);
    cairo_fill(cr);

    return FALSE;
  }
//The top drawing function.
static gboolean draw_da(GtkWidget *da, cairo_t *cr, gpointer data)
  {
    gdouble width=(gdouble)gtk_widget_get_allocated_width(da);
    gdouble height=(gdouble)gtk_widget_get_allocated_height(da);
    gdouble w1=1.0*width/10.0;
    gdouble h1=1.0*height/10.0;

    //Paint background transparent.
    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
    cairo_paint(cr);

    //Cartesian coordinates for drawing.
    cairo_set_line_width(cr, 3.0);
    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
    cairo_rectangle(cr, w1, h1, 8.0*w1, 8.0*h1);
    cairo_stroke(cr);
    cairo_move_to(cr, 1.0*w1, 5.0*h1);
    cairo_line_to(cr, 9.0*w1, 5.0*h1);
    cairo_stroke(cr);
    cairo_move_to(cr, 5.0*w1, 1.0*h1);
    cairo_line_to(cr, 5.0*w1, 9.0*h1);
    cairo_stroke(cr);

    //Draw a circle in the center.
    cairo_translate(cr, width/2.0, height/2.0);     
    cairo_set_source_rgba(cr, 1.0, 0.0, 1.0, 1.0);
    cairo_arc (cr, 0.0, 0.0, 2.5*w1, 0.0, 2.0*G_PI);
    cairo_fill(cr);
 
    return FALSE;
  }





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