Re: filling a drawing area with a pattern



Hey Nickolay,

Thanks for your response. Your example, along with an article I managed to dig up after half a day of googling, helps me a great deal to understand the connection between widgets and pixmaps. This just shows that a good example is far better than frantically browsing the API specs, trying to make sense of it all :-)

Thanks!

--
Sander

PS: Your response didn't show at the list. I guess it only accepts mails directed to it, not just cc'ed.


Nickolay V. Shmyrev wrote:
В Втр, 14/03/2006 в 08:09 +0100, Sander Marechal пишет:

Hello,

I'm pretty new to gnome development and I have trouble understanding how to draw your own things. My eventual objective is to make a hearts game, but that's a long way off. At the moment I am having trouble to set a background in my UI, much like the green striped background in AisleRiot.

I am using libglade for my UI. The main window (where the cards are going to be) is a GtkDrawingArea called "table". I load up an image and try to fill the drawing area with it, but it doesn't show it. I've tried looking ar AilseRiot and see how they do it, but I can't figure it out. Here's my code, with comments as to what each step is supposed to do.

Any idea what I am doing wrong? Probably lots :-) Thanks in advance!

int main (int argc, char *argv[])
{
	GtkWidget *table_widget;
	GdkGC *table;

	/* Start up glade and show the UI */
	gnome_init (PACKAGE, VERSION, argc, argv);
	glade_gnome_init ();
	xml = glade_xml_new (PACKAGE_SOURCE_DIR"/hearts.glade", NULL, NULL);
	glade_xml_signal_autoconnect (xml);
	
/* Load the background pixmap. get_pixmap() uses gdk_pixbuf_new_from_file() to load the image and returns a GdkPixmap pointer. I got this from the AisleRiot code. background_pixmap is a global. It does return a valid image, not NULL. */
	background_pixmap = get_pixmap(PACKAGE_SOURCE_DIR"/pixmaps/baize.png");
	
/* From what I read, I need a GDK Graphics Context to draw in, so here I create one. I can verify that it does return a GdkGC, and not NULL */
	table_widget = glade_xml_get_widget (xml, "table");
	table = gdk_gc_new (table_widget->window);
	
	/* This should fill the GC with the background image, right? */
	gdk_gc_set_tile (table, background_pixmap);
	gdk_gc_set_fill (table, GDK_TILED);

	/* wait for events */
	gtk_main ();

	/* bye bye */
	return 0;
}



Hi Sander

There are multiple ways to archive your goal. If you just need to draw a
background on GtkDrawingArea, you can use the property of all X server
windows - they may have a background set from pixmap. See the example
attached.
That is simple and fastest way to draw some background but I suspect in
your game you'll need more advanced operations. Probably you need to
look at some canvas library like GooCanvas or another analog, they allow
much more intelligent work with drawing.



------------------------------------------------------------------------

#include <gtk/gtk.h>

static const char * xpm_data[] = {
"16 16 3 1",
"       c None",
".      c #000000000000",
"X      c #FFFFFFFFFFFF",
"                ",
"   ......       ",
"   .XXX.X.      ",
"   .XXX.XX.     ",
"   .XXX.XXX.    ",
"   .XXX.....    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .XXXXXXX.    ",
"   .........    ",
"                ",
"                "};

void quit ()
{
  exit (0);
}

int main( int argc, char *argv[] )
{
  GtkWidget *window;
  GtkWidget *drawing_area;
  GtkWidget *vbox;
  GtkStyle *style;
  GdkPixmap *pixmap;
  GdkBitmap *mask;
  GtkWidget *button;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (quit), NULL);

  /* Create the drawing area */

  drawing_area = gtk_drawing_area_new ();
  gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);

  /* And a button */
  button = gtk_button_new_with_label ("Quit");
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  g_signal_connect_swapped (G_OBJECT (button), "clicked",
			    G_CALLBACK (gtk_widget_destroy),
			    G_OBJECT (window));
  gtk_widget_show_all (window);

  /* If shown, GtkWidget is associated with GdkWindow and GtkStyle */

  style = gtk_widget_get_style (drawing_area);
  pixmap = gdk_pixmap_create_from_xpm_d (drawing_area->window,  &mask,
                                         &style->bg[GTK_STATE_NORMAL],
                                         (gchar **)xpm_data);
gdk_window_set_back_pixmap (drawing_area->window, pixmap, FALSE);


  gtk_main ();

  return 0;
}





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