[9525] in The GTK GIMP ToolKit mailing list archive

home help back first fref pref prev next nref lref last post

[gtk-list] Re: Transparency in Pixmaps

daemon@ATHENA.MIT.EDU (Brandon Long)
Thu Oct 22 18:45:49 1998

Date: Thu, 22 Oct 1998 15:45:29 -0700
From: Brandon Long <blong@fiction.net>
To: gtk-list@redhat.com
Reply-To: blong@fiction.net
Mail-Followup-To: gtk-list@redhat.com
In-Reply-To: <Pine.LNX.3.96.981022125749.14530D-100000@genie.localnet>; from David Cunningham on Thu, Oct 22, 1998 at 01:01:06PM +0100
Resent-From: gtk-list@redhat.com


--uAKRQypu60I7Lcqm
Content-Type: text/plain; charset=us-ascii

On 10/22/98 David Cunningham uttered the following other thing:
> Hi,
> 
> I have a background pixmap onto which i want to draw another pixmap which
> has transparency. All my attempts so far have failed to get the
> transparency to work, as it keeps removing the background where it
> shouldnt.
> Can anyone show me how its done, or point me to an example?

Here is an example I worked through earlier on the list.

Brandon
-- 
 Brandon Long             "Faith strikes me as intellectual laziness."
 MD6 Crash Test Dummy            -- Robert A. Heinlein
 Intel Corporation       
          I'm too low on the totem pole to speak for Intel.
		  http://www.fiction.net/blong/

--uAKRQypu60I7Lcqm
Content-Type: message/rfc822

Received: from proxy1.ba.best.com (root@proxy1.ba.best.com [206.184.139.12])
	by shell9.ba.best.com (8.9.0/8.9.0/best.sh) with ESMTP id WAA11852
	for <blong+XRCPT.626c6f6e674066696374696f6e2e6e6574@shell9.ba.best.com>; Sun, 11 Oct 1998 22:26:06 -0700 (PDT)
Received: from mail2.redhat.com (mail2.redhat.com [199.183.24.247])
	by proxy1.ba.best.com (8.9.0/8.9.0/best.in) with SMTP id WAA09011
	for <blong@fiction.net>; Sun, 11 Oct 1998 22:25:11 -0700 (PDT)
Received: (qmail 12461 invoked by uid 501); 12 Oct 1998 05:30:48 -0000
Resent-Date: 12 Oct 1998 05:30:48 -0000
Resent-Cc: recipient list not shown: ;
MBOX-Line: From gtk-list-request@redhat.com  Mon Oct 12 01:30:47 1998
Message-ID: <19981011222401.A5475@shell9.ba.best.com>
Date: Sun, 11 Oct 1998 22:24:01 -0700
From: Brandon Long <blong@fiction.net>
To: gtk-list@redhat.com
Reply-To: blong@fiction.net
Mail-Followup-To: gtk-list@redhat.com
References: <13857.10471.214612.248142@afroblue.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Mailer: Mutt 0.92.13i
In-Reply-To: <13857.10471.214612.248142@afroblue.zedat.fu-berlin.de>; from Stephan Krings on Mon, Oct 12, 1998 at 12:02:48AM +0200
Organization: Fiction L Networks
X-Pgp-Public-Key: <http://www.fiction.net/blong/pgpkey.html>
X-Uri: <http://www.fiction.net/blong/>
Resent-Message-ID: <"WcWVQ2.0.123.7GP8s"@mail2.redhat.com>
Resent-From: gtk-list@redhat.com
X-Mailing-List: <gtk-list@redhat.com> archive/latest/63
X-Loop: gtk-list@redhat.com
Precedence: list
Resent-Sender: gtk-list-request@redhat.com
X-URL: http://archive.redhat.com/gtk-list/
Subject: [gtk-list] Re: Clipping-Masks in Gdk
X-Rcpt-To: blong@fiction.net

On 10/12/98 Stephan Krings uttered the following other thing:
> 
> Hello out there,
> 
> could anyone please explain me while the code appended does not
> work. Forgive me if this is a stupid question, but although I'm
> beginning to feel comfortable with Gtk and Gdk, I don't know much
> about the underlying Xlib-calls and -mechanisms (maybe I need to
> invest in one of those expensive Xlib-books), and I'd guess this is
> the problem.
> 
> I hope my intention is clear, when reading the code: I want to create
> an offscreen clipping-mask for a window or pixmap. Unfortunatly I'm
> not sure, how to create the GdkBitmap in memory (it works, when
> loading the bitmap from a file with gdk_bitmap_create_from_data for
> example).
> 
> Oh yes, the program crashes with an abort-signal and BadMatch error
> message.
> 
> So, how do I create this magic GdkBitmap or mask in memory?

Ok, you've made a couple mistakes here.  I'll try to point them out:

> ---------------------------------------------------------------------------
> #include <gtk/gtk.h>
> 
> int main (int argc, char **argv) {
> 
>   GtkWidget *window;
>   GtkWidget *drawarea;
> 
>   GdkPixmap *mask;
>   GdkGC *gc;
>   GdkColor red;
>   GdkColor white;
> 
>   gtk_init(&argc, &argv);
> 
>   drawarea = gtk_drawing_area_new();
>   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>   gtk_container_add(GTK_CONTAINER (window), drawarea);
>   gtk_widget_show(GTK_WIDGET (drawarea));
>   gtk_widget_show(GTK_WIDGET (window));
> 
>   gdk_color_parse("red", &red);
>   gdk_color_alloc(gtk_widget_get_colormap(drawarea), &red);
> 
>   gdk_color_parse("white", &white);
>   gdk_color_alloc(gtk_widget_get_colormap(drawarea), &white);
> 
>   gc = gdk_gc_new(drawarea->window);
>   gdk_gc_copy(gc, drawarea->style->black_gc);
> 
>   gdk_gc_set_foreground(gc, &white);
> 
>   mask = gdk_pixmap_new(drawarea->window,
> 			drawarea->allocation.width,
> 			drawarea->allocation.height,
> 			-1);

A bitmask has 1 bit per pixel, and you probably aren't running X in
black & white, so the default (-1) isn't what you want.  Just take away
the -.

Additionally, the GC is dependent on the pixel depth, so you should
create your GC for the mask using the mask instead of the drawing area
window, so add this:

    gc = gdk_gc_new(mask);

You also can't copy from the drawing area GC, as that is for a different
depth.
 
Move this down here:
    gdk_gc_set_foreground(gc, &white);
 
>   gdk_draw_rectangle(mask, gc, TRUE, 10, 10, 20, 20);

Ok, the pixmap is completely uninitialized, so you should really draw a
rectangle to the full area:

    gdk_draw_rectangle(mask, gc, TRUE, 0, 0, drawarea->allocation.width,
      drawarea->allocation.height);

Additionally, white is the color used for "not" masked, so you should
use black for the actual area you want to blank:

    GdkColor black;

    gdk_color_parse("black", &black);
    gdk_color_alloc(gtk_widget_get_colormap(drawarea), &black);

    gdk_gc_set_foreground(gc, &black);
 
    gdk_draw_rectangle(mask, gc, TRUE, 10, 10, 20, 20);
 
Now, your GC is for the wrong window, you need a different one:

    gdk_gc_unref (gc);

    gc = gdk_gc_new (drawarea->window);
    gdk_gc_copy (gc, drawarea->style->black_gc);
  
>   gdk_gc_set_foreground(gc, &red);
> 
>   gdk_gc_set_clip_mask(gc, mask);
>   gdk_gc_set_clip_origin(gc, 0, 0);
> 
>   gdk_draw_rectangle(drawarea->window, gc, TRUE, 0, 0,
> 		     drawarea->allocation.width,
> 		     drawarea->allocation.height);
> 
>   gtk_main();
> 
>   return 0;
> }

And that should do it.

Something that might help in the future, gdk has a bunch of flags you
can pass on the command line, including --sync.  --sync will enable
synchronous X communication, meaning that the program will wait after
every X call for a response.  If you run under gdb, your program will
abort when the X error occurs, and you can use a bt to see what the
calling function was that caused the error, which might help lead you
to what is going wrong.

Brandon
-- 
 Brandon Long        "The most merciful thing in the world is the inability
 MD6 Crash Test Dummy    of the human mind to correlate all its contents."
 Intel Corporation               -- H. P. Lovecraft
          I'm too low on the totem pole to speak for Intel.
		  http://www.fiction.net/blong/

-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null

--uAKRQypu60I7Lcqm--

-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null


home help back first fref pref prev next nref lref last post