can't get GDK_XOR to work, help wanted



Below is a very simple test case of GDK_XOR. I don't think it can get
much easier. If you run the program with an argument, it will attempt
to use the XOR function for the GC used to draw rectangles. If you run
it with no argument, it will use the default function (COPY). 

With XOR, nothing is ever drawn. With the default function, rectangles
are drawn all over the drawing area.

I'm sure I am doing something wrong, since the gdk code path through
the use of GDK_XOR is so close the X one that its hard to see where
there could be a bug. Can anyone tell me what might be wrong ?

--p

--------------------------------------------------------------------------
/* compile: cc -o gxor gxor.c `gtk-config --libs` */

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#define EVENT_MASK	(GDK_EXPOSURE_MASK |		\
			 GDK_POINTER_MOTION_MASK |	\
			 GDK_POINTER_MOTION_HINT_MASK |	\
			 GDK_ENTER_NOTIFY_MASK |	\
			 GDK_BUTTON_PRESS_MASK |	\
			 GDK_BUTTON_RELEASE_MASK |	\
			 GDK_BUTTON1_MOTION_MASK)
gint with_xor = FALSE;

void
event_handler (GtkWidget *w, GdkEvent *ev, gpointer *data)

{
	static GdkGC *gc = NULL;
	static int drawn = FALSE;
	static int has_auto_grab = FALSE;
	GdkWindow *win;
	static gint lx, ly;
	gint x, y;

	g_return_if_fail (w != NULL);
	g_return_if_fail (GTK_IS_DRAWING_AREA(w));
	
	win = w->window;

	gdk_window_get_pointer (win, &x, &y, NULL);

	switch (ev->type) {
		
	case GDK_BUTTON_PRESS:
		has_auto_grab = TRUE;
		if (gc == NULL) {
			gc = gdk_gc_new (win);
			if (with_xor) {
				gdk_gc_set_function (gc, GDK_XOR);
			}
		}
		XGrabServer (GDK_DISPLAY());
		break;
		
	case GDK_BUTTON_RELEASE:
		has_auto_grab = FALSE;
		if (drawn) {
			gdk_draw_rectangle (win, gc, 0, lx,ly,200, 200);
			drawn = FALSE;
		} 
		XUngrabServer (GDK_DISPLAY());
		gdk_flush ();
		break;
		
	case GDK_MOTION_NOTIFY:
		if (has_auto_grab) {
			if (drawn) {
				gdk_draw_rectangle(win, gc, 0,lx,ly, 200, 200);
				drawn = FALSE;
			} 
			gdk_draw_rectangle (win, gc, 0, x, y, 200, 200);
			drawn = TRUE;
		}
		break;
	}

	lx = x;
	ly = y;
	
	return;
}

main (int argc, char *argv[])

{
      GtkWidget *window;
      GtkWidget *da;
      gint old_mask;

      gtk_init (&argc, &argv);

      if (argc > 1) {
	      with_xor = TRUE;
      }
      
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

      da = gtk_drawing_area_new ();
      gtk_widget_set_usize (da, 400, 400);

      old_mask = gtk_widget_get_events (da);
      gtk_widget_set_events (da, old_mask | EVENT_MASK);
      gtk_signal_connect (GTK_OBJECT (da), "event",
			  GTK_SIGNAL_FUNC (event_handler),
			  (gpointer) 0);

      gtk_container_add (GTK_CONTAINER(window), da);

      gtk_widget_show (da);
      gtk_widget_show (window);
      gtk_main ();

      return(0);
}

















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