Re: Why do I have a grey square instead of a red square?



> 2nd Can you beg me a few lines how to draw a red / green square into a
> pixmap

OK, here you go.  Here's a fairly minimal, but complete program
that paints a color pixmap and then displays it in a window.  This
includes all the code to set up the window, etc.  The actual pixmap
creating and rendering is fairly simple.

The basic theory of operation is this:  Every window has its own
visual/depth/colormap properties and a pixmap which is to be copied
to this window must match all of these properties or it will look
bad (or even crash if the depths doesn't match.)

So what we do is to first create the widget and "realize" it (that is,
make sure the window has been allocated and assigned.)  Once the widget
is realized, its window and colormap can be obtained and used to
create the pixmap.

In practice, a pixmap may be used with any and all windows which match
the depth/colormap attributes of the window for which the pixmap was
created.  In most applications, this means all of the windows.

Here's the program; enjoy:


#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>

static	GtkWidget	*drawingarea = NULL;
static	GdkPixmap	*pixmap = NULL ;

static	gint	area_event(GtkWidget *, GdkEventExpose *, gpointer) ;

int
main (int argc, char **argv)
{
	GtkWidget	*window;
	GdkColormap	*colormap;
	GdkGC		*gc ;
	GdkColor	red, green, *bg ;

	gtk_init (&argc,&argv);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title (GTK_WINDOW(window), "Red/green square");
	gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, TRUE);

	gtk_signal_connect (GTK_OBJECT(window), "destroy",
	 (GtkSignalFunc)gtk_main_quit, NULL);


	/* Create drawingarea, set size and catch expose events */

	drawingarea = gtk_drawing_area_new ();

	gtk_drawing_area_size (GTK_DRAWING_AREA(drawingarea), 200, 200);

	gtk_widget_set_events (drawingarea, GDK_EXPOSURE_MASK);

	gtk_signal_connect (GTK_OBJECT(drawingarea), "expose-event", 
	 (GtkSignalFunc)area_event, (gpointer)drawingarea);


	/* Add drawingarea to window, then show them both */

	gtk_container_add (GTK_CONTAINER(window), drawingarea);

	gtk_widget_show (drawingarea);
	gtk_widget_show (window);

	/* By now, the drawingarea widget has been realized, meaning that
	 * its window and colormap have been assigned.  We may now create
	 * pixmaps and allocate colors, which depend on this information.
	 */



	/* We need three colors to paint the pixmap:  the background,
	 * red, and green.  The background can be found in the drawingarea's
	 * style structure.  The others we'll allocate ourselves.
	 */

	colormap = gdk_window_get_colormap(drawingarea->window) ;

	red.red = 0xffff ;
	red.green = 0 ;
	red.blue = 0 ;
	gdk_color_alloc (colormap, &red);

	green.red = 0 ;
	green.green = 0xffff ;
	green.blue = 0 ;
	gdk_color_alloc (colormap, &green);

	bg = &drawingarea->style->bg[GTK_STATE_NORMAL] ;


	/* Create a single pixmap with two color squares */

	pixmap = gdk_pixmap_new(drawingarea->window, 100, 100, -1) ;

	/* Create a graphics context to use with this pixmap.  It might
	 * make sense to use the pixmap as the argument to gdk_gc_new(),
	 * but the docs specify that the argument is a window, not a
	 * generic drawable, so I'll use the window as the argument.
	 */

	gc = gdk_gc_new( drawingarea->window ) ;

	/* Paint the pixmap */
	gdk_gc_set_foreground(gc, bg) ;
	gdk_draw_rectangle(pixmap, gc, TRUE, 0,0, 100,100) ;
	gdk_gc_set_foreground(gc, &red) ;
	gdk_draw_rectangle(pixmap, gc, TRUE, 0,0, 75,75) ;
	gdk_gc_set_foreground(gc, &green) ;
	gdk_draw_rectangle(pixmap, gc, TRUE, 25,25, 75,75) ;



	/* All set, enter the gtk main loop */

	gtk_main ();

	exit(0);
}



/* Drawingarea expose event handler */

static	gint
area_event (GtkWidget *widget, GdkEventExpose *event, gpointer client_data)
{
	/* Draw the pixmap */
	gdk_draw_pixmap(widget->window, widget->style->fg_gc[GTK_STATE_NORMAL],
		pixmap, 0,0, 50,50, 100,100) ;

	return TRUE ;
}



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