Drawing pixmaps in GtkWindow no more possible since 1.1.3



Hello,

I relayed in my code in the possibility to copy pixmaps created by
Imlib into the GdkWindow of a GtkWindow.  To show what I mean I append
a little C-program.

This does the following:
0. Create a Button field to end the program
1. Read an image file via Imlib (hope you have any nice image :))
2. a) Open a GtkWindow.
      GtkWidget *show = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   b) Try to copy a pixmap into (GdkWindow *)show->window
  ... !! This worked up to GTK+ 1.1.3 !!
      My problem is that this doesn't work since 1.1.5!!
      In dead the pixmap is written but the window is cleared immediately
      after this.
3. a) Open another GtkWindow.
   b) Use these GtkWindow as container for a GtkDrawingArea.
   c) Set the size of the container to the image size.
      !! This could be avoided in the method 2!!
   d) Copy a pixmap into the GdkWindow of the GtkDrawingArea.
  ... This works so far.
4. a) Open a GdkWindow.
   b) Copy pixmap into this Window
  ... This works in all cases but I don't know how to handle events
      for those Windows.
      The GtkWidgets handle for instance key_press events.

So what I'm looking for?
I'm looking for the *fastest* way to put an image into a window which
can handle events.

Here is the code.  A simple Makefile is appended.

#include <gdk_imlib.h>
#include <gtk/gtk.h>

static void 
ShowGdkWindow(GdkWindow *win, GdkImlibImage *im)
{
  GdkPixmap     *p, *m;

  g_return_if_fail ( win );
  g_return_if_fail ( im );
   
  gdk_imlib_render(im, im->rgb_width, im->rgb_height);
  p = gdk_imlib_move_image(im);
  m = gdk_imlib_move_mask(im);
  gdk_window_resize(win, im->rgb_width, im->rgb_height);
  gdk_window_set_back_pixmap(win, p, 0);
  if (m) gdk_window_shape_combine_mask(win, m, 0, 0);
  gdk_window_show(win);
  gdk_flush();
}

static void
cut (GtkWidget *widget, GtkWidget *window)
{
   /* Do nothing at this moment */
}

static int
create_main_window (gchar *file)
{
  GtkWidget     *window, *box, *button, *show, *drawing;
  GdkImlibImage *im;
  GdkWindowAttr  attr;
  GdkWindow     *win;

  g_return_val_if_fail ( file, -1 );
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_uposition (window, 20, 20);

  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC(gtk_main_quit),
		      NULL);
  gtk_signal_connect (GTK_OBJECT (window), "delete-event",
		      GTK_SIGNAL_FUNC (gtk_false),
		      NULL);

  box = gtk_hbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), box);

  gtk_container_border_width (GTK_CONTAINER (box), 10);

  button = gtk_button_new_with_label ("cut image");
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      GTK_SIGNAL_FUNC (cut),
		      window);
  gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button);

  button = gtk_button_new_with_label ("close");
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
		      gtk_main_quit,
		      window);
  gtk_box_pack_start (GTK_BOX (box), button, TRUE, TRUE, 0);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_widget_show_all (window);

  gtk_widget_push_visual(gdk_imlib_get_visual());
  gtk_widget_push_colormap(gdk_imlib_get_colormap());

  im = gdk_imlib_load_image(file);

  /********* Try to show in a plain GtkWindow  ********************
   ********* This worked up to GTK+1.1.3       ********************/
  show = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(show), "GTK - window");
  gtk_widget_show(show);
  gtk_signal_connect(GTK_OBJECT(show), "key_press_event", gtk_main_quit, window);
  ShowGdkWindow(show->window, im);

  /********* Show the beast in a GtkDrawingArea ******************/
  show = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(show), "GTK - drawing area");
  /********* This requires to set the size of the container ******/
  gtk_widget_set_usize(GTK_WIDGET(show), im->rgb_width, im->rgb_height);
  drawing = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER(show), drawing);
  gtk_widget_show_all(show);
  gtk_signal_connect(GTK_OBJECT(show), "key_press_event", gtk_main_quit, window);
  ShowGdkWindow(drawing->window, im);

  /********* Showing in a plain GDK window works well ************/
  attr.window_type = GDK_WINDOW_TOPLEVEL;
  attr.wclass      = GDK_INPUT_OUTPUT;
  attr.event_mask  = GDK_STRUCTURE_MASK;
  win = gdk_window_new(NULL,&attr,0);
  gdk_window_set_title(win, "GDK - window");
  ShowGdkWindow(win, im);
    
  return 0;
}

gint 
main(gint argc, char *argv[])
{
  if ( argc <= 1 ) {
    g_warning("Usage:\n %s image_file",argv[0]);
    return -1;
  }

  gtk_init(&argc, &argv);
  gdk_imlib_init();
  
  g_return_val_if_fail ( !create_main_window(argv[1]), -1 );
  
  gtk_main();

  return 0;
}

Makefile:

NAME=image_test

CFLAGS=-g -O2 -Wall `gtk-config --cflags` `imlib-config --cflags-gdk`
LDFLAGS=`gtk-config --libs` `imlib-config --libs-gdk` -s

$(NAME): $(NAME).o
$(NAME).o: $(NAME).c

clean:
	rm -rf *.o *~



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