Continuing problem with GtkImage resizes (w/sample code)




I'm still having a problem with GtkImages/Frames and resizes.  The
following short program demonstrates the issue.  This sample
application shows an image in a frame.  The problem is that (at least
on my system) the left and upper edges of the image are not drawn.
Also, when image areas are exposed that were covered by window manager
decorations, they aren't updated, while the other areas are.

I can edit gtk/gtkimage.c, to draw the whole image every time by
modifying the gdk_draw_image call.  While this does fix the problem, I
feel pretty sure it's not the right solution.  Note that I believe the
problem doesn't occur if the image is just placed inside the main
window (without a frame).  If someone gives me a kick in the right
direction, I'll see if I can come up with a patch.  I'm testing this
against gtk+971025.

(Perhaps I'm setting up the gtkimage wrong?)

Thanks in advance.

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

typedef guint32 GtkPixel;
typedef unsigned short int_u2;

static GtkWidget *image_container;
static GtkPixel color1;
static GtkPixel color2;

static void
allocate_colors(GtkWidget *w) {
  GdkColor c;
  GdkColormap *cmap = gtk_widget_get_colormap(w);

  c.red = 0; c.green = 0; c.blue = 0;
  if(!gdk_color_alloc(cmap, &c)) {
    fprintf(stderr, "Couldn't allocate color for ImageDisplay.\n");
    exit(-1);
  }
  color1 = c.pixel;
  
  c.red = 0; c.green = 0x6FFF; c.blue = 0;
  if(!gdk_color_alloc(cmap, &c)) {
    fprintf(stderr, "Couldn't allocate color for ImageDisplay.\n");
    exit(-1);
  }
  color2 = c.pixel;
}

static void
fill_image_data(GdkImage *image, const int_u2 w, const int_u2 h) {  
  int_u2 row_jump = image->bpl - (w * image->bpp);

  switch(image->depth) {
  case 8:
    {
      int_u2 j, i;
      char *current_ximage_pt = (char *) image->mem;
      for(j = 0; j < h; j++) {        
        for(i = 0; i < w; i++) {        
          if((i%h) == j) {
            *current_ximage_pt = color1;
          } else {
            *current_ximage_pt = color2;            
          }
          current_ximage_pt++;
        }      
        current_ximage_pt += row_jump;
      }
    }  
  break;
  case 16:
    {
      int_u2 i, j;
      int_u2 *current_ximage_pt = (int_u2 *) image->mem;      
      for(j = 0; j < h; j++) {
        for(i = 0; i < w; i++) {        
          if((i%h) == j) {
            *current_ximage_pt = color1;
          } else {
            *current_ximage_pt = color2;            
          }
          current_ximage_pt++;
        }
        ((char *) current_ximage_pt) += row_jump;
      }
    }
  break;
  }
}

static gint
show_image (gpointer data) {
  GtkWidget *image_widget;
  GdkImage  *image;
  int_u2 w = 200;
  int_u2 h = 64;
  
  allocate_colors(GTK_WIDGET(image_container));
  
  image = gdk_image_new(GDK_IMAGE_FASTEST,
                        gtk_widget_get_visual(GTK_WIDGET(image_container)),
                        w, h);

  fill_image_data(image, w, h);
  
  image_widget = gtk_image_new(image, NULL);
  gtk_container_add(GTK_CONTAINER(image_container), GTK_WIDGET(image_widget));
  gtk_widget_show(image_widget);
  return 0;
}

int
main (int argc, char *argv[]) {
  
  GtkWidget *window;
  GtkWidget *frame;
  
  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_container_border_width(GTK_CONTAINER (window), 5);

  frame = gtk_frame_new("Test frame");
  image_container = frame;
  
  gtk_container_add(GTK_CONTAINER (window), frame);
  gtk_widget_show(frame);
  gtk_widget_show(window);

  gtk_timeout_add(5000, show_image, NULL);
  
  gtk_main();

  return 0;
}

-- 
Rob Browning <rlb@cs.utexas.edu>
PGP fingerprint = E8 0E 0D 04 F5 21 A0 94  53 2B 97 F5 D6 4E 39 30



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