Re: [gtk-list] Clipping-Masks in Gdk



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/



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