Gtk+Cairo+transparency



 Hi, how are you.

I don't know if this is best site to ask this question. If not, i appreciate you that 
indicate me the better site for this.

Well, i explain my problem.

I'm developing an aplicattion with gtk. 

I'm using Cairo library for make transparent the mainwindow of the aplication, but this 
window has also a "gtk theme" made for me(image background etc). 
If I only put gtk theme in the window i have no problem, all run correctly, but 
when I paint this window with cairo, my gtk theme dissapears.

The code that does this is:


==========================================

gboolean supports_alpha = FALSE;

static gboolean expose(GtkWidget *widget, GdkEventExpose *event, gpointer userdata)
{
    cairo_t *cr = gdk_cairo_create(widget->window);

    if (supports_alpha)
        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1); /* transparent */
    else
        cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* opaque white */

    /* draw the background */
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    
    cairo_paint (cr);

    cairo_destroy(cr);
    return FALSE;
}


static void screen_changed(GtkWidget *widget, GdkScreen *old_screen, gpointer userdata)
{
    /* To check if the display supports alpha channels, get the colormap */
    GdkScreen *screen = gtk_widget_get_screen(widget);
    GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);

    if (!colormap)
    {
        printf("Your screen does not support alpha channels!\n");
        colormap = gdk_screen_get_rgb_colormap(screen);
        supports_alpha = FALSE;
    }
    else
    {
        printf("Your screen supports alpha channels!\n");
        supports_alpha = TRUE;
    }

    /* Now we have a colormap appropriate for the screen, use it */
    gtk_widget_set_colormap(widget, colormap);
}



static GtkWidget *set_window_attr(GtkWindow *window)
{
char rc_style[512];
  
gtk_window_set_position(window, GTK_WIN_POS_MOUSE);
gtk_window_set_default_size(window, 300, 200);
gtk_window_set_decorated(window, FALSE);
sprintf(rc_style, "pixmap_path \"/\" \n" "style \"mywindow\" \n" "{\n"
"bg_pixmap[NORMAL]=\"%s\"\n"
"}"
 "class \"GtkWindow\" style \"mywindow\"\n", "player.png");
gtk_rc_parse_string(rc_style);
gtk_widget_set_app_paintable(GTK_WIDGET(window), TRUE);
g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
g_signal_connect(window, "expose-event", G_CALLBACK(expose), NULL);
     g_signal_connect(window, "screen-changed", G_CALLBACK(screen_changed), NULL);

    return GTK_WIDGET(window);
}

void player_intf(int c_argc, char **c_argv)
{
    GtkWidget *window;
    int i_args = c_argc;
    char **pp_args  = c_argv;

        gtk_init (&i_args, &pp_args);
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        window = set_window_attr(GTK_WINDOW(window));
        screen_changed(window, NULL, NULL);

        gtk_widget_show_all(window);
        gtk_main();
}


int main(int argc, char **argv)
{
    player_intf(argc, argv);

    return 0;
}


================================================


The part of code that done a transparent window is not mine, i searched how do a transparent windows with gtk in google, and i found this.




Thanks a lot,

Regards
//      maquetacion.c
//      
//      Copyright 2011 Leber Schutten <leber@unmerficulworld>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include <stdio.h>
#include <gtk/gtk.h>

static gboolean delete_event(GtkWidget *widget,
                                GdkEvent *event, gpointer data)
{
    gtk_main_quit();
    return FALSE;
}


gboolean supports_alpha = FALSE;

static gboolean expose(GtkWidget *widget, GdkEventExpose *event, gpointer userdata)
{
    cairo_t *cr = gdk_cairo_create(widget->window);

    if (supports_alpha)
        cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1); /* transparent */
    else
        cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); /* opaque white */

    /* draw the background */
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
    
    cairo_paint (cr);

    cairo_destroy(cr);
    return FALSE;
}


static void screen_changed(GtkWidget *widget, GdkScreen *old_screen, gpointer userdata)
{
    /* To check if the display supports alpha channels, get the colormap */
    GdkScreen *screen = gtk_widget_get_screen(widget);
    GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);

    if (!colormap)
    {
        printf("Your screen does not support alpha channels!\n");
        colormap = gdk_screen_get_rgb_colormap(screen);
        supports_alpha = FALSE;
    }
    else
    {
        printf("Your screen supports alpha channels!\n");
        supports_alpha = TRUE;
    }

    /* Now we have a colormap appropriate for the screen, use it */
    gtk_widget_set_colormap(widget, colormap);
}



static GtkWidget *set_window_attr(GtkWindow *window)
{
	char rc_style[512];
  
		gtk_window_set_position(window, GTK_WIN_POS_MOUSE);
		gtk_window_set_default_size(window, 300, 200);
		gtk_window_set_decorated(window, FALSE);
		
		sprintf(rc_style, "pixmap_path \"/\" \n" "style \"mywindow\" \n" "{\n"
							"bg_pixmap[NORMAL]=\"/home/oscar.salvador/c/dl/iconos/%s\"\n"
							"}"
						  "class \"GtkWindow\" style \"mywindow\"\n", "player.png");
		gtk_rc_parse_string(rc_style);
		gtk_widget_set_app_paintable(GTK_WIDGET(window), TRUE);
		g_signal_connect(window, "delete-event", G_CALLBACK(delete_event), NULL);
		g_signal_connect(window, "expose-event", G_CALLBACK(expose), NULL);
    		g_signal_connect(window, "screen-changed", G_CALLBACK(screen_changed), NULL);

    return GTK_WIDGET(window);
}

void player_intf(void)
{
    GtkWidget *window;
	const char *p_args[] = { "devicelost" };
    int i_args = sizeof(p_args)/sizeof(char *);
    char **pp_args  = (char **)p_args;
 		
		gtk_init (&i_args, &pp_args);       
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        window = set_window_attr(GTK_WINDOW(window));
		screen_changed(window, NULL, NULL);
			
        gtk_widget_show_all(window);
        gtk_main();
}


int main(int argc, char **argv)
{
	player_intf();
	
	return 0;
}



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