GTK Cursor changing displays some garbage data.



Hi,
I am programming with Gtk+ v1.2 for mouse cursor changing.
But I have some garbage displaying on mouse cursor when I do double clicking of mouse buttons many times.
Here I attached the codes.
Down 3 files are the attached ones too.
Did I miss some flushing functions?

bkna

FILE 1: cursor_f.xpm

/* XPM */
static char *cursor_f[] = {
/* width height ncolors chars_per_pixel */
"24 39 13 1",
/* colors */
"` c black",
"a c lime green",
"b c cyan",
"c c green",
"d c dodger blue",
"e c gainsboro",
"f c grey",
"g c sienna",
"i c orange",
"j c gold",
"k c slate grey",
"l c orange",
"m c sea green",
/* pixels */
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"    aaaaaaaaaaaaaaaa    ",
"    aaaaaaaaaaaaaaaa    ",
"    aaa                           ",
"    aaa                           ",
"    aaa                           ",
"    aaa        aa               ",
"    aaaaaaaaaaaaa         ",
"    aaaaaaaaaaaaa         ",
"    aaa        aa               ",
"    aaa                          ",
"    aaa                          ",
"    aaa                          ",
"    aaa                          ",
"    aaa                          ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   ",
"                                   "
};

FILE 2: cusror_z.xpm

/* XPM */
static char *cursor_z[] = {
/* width height ncolors chars_per_pixel */
"24 39 13 1",
/* colors */
"` c black",
"a c lime green",
"b c cyan",
"c c green",
"d c dodger blue",
"e c gainsboro",
"f c grey",
"g c sienna",
"i c orange",
"j c gold",
"k c slate grey",
"l c orange",
"m c sea green",
/* pixels */
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"    aaaaaaaaaaaaaaaa    ",
"    aaaaaaaaaaaaaaaa    ",
"                aaa              ",
"               aaa               ",
"              aaa                ",
"             aaa                 ",
"            aaa                  ",
"     aaaaaaaaaaaaaa     ",
"     aaaaaaaaaaaaaa     ",
"         aaa                     ",
"        aaa                      ",
"       aaa                       ",
"      aaa                        ",
"     aaa                         ",
"    aaaaaaaaaaaaaaaa    ",
"    aaaaaaaaaaaaaaaa    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    ",
"                                    "
};

FILE 3: cursor.c

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

int doubleclicks;

#include "cursor_f.xpm"
#include "cursor_z.xpm"

GdkColor red = {0,0xFFFF,0x0000,0x0000};
GdkColor white = {0,0xFFFF,0xFFFF,0xFFFF};


GdkCursor *newCursorFromXPM (gchar *xpm[],
			     gchar blackLetter,GdkColor *blackColor,
			     gchar whiteLetter,GdkColor *whiteColor,
			     gint xHot,gint yHot)
{
 gint height, width, colors, pchars, x, y;
 GdkBitmap *bitmap, *mask;
 GdkCursor *cursor;
 guchar *bitmapData, *maskData;
 gint byteIndex = 0, bitCount = 0;

 sscanf (xpm[0],"%d %d %d %d", &width, &height, &colors, &pchars);
 g_assert(pchars == 1);

 bitmapData = (guchar *)g_malloc((width * height) / 8);
 maskData = (guchar *)g_malloc((width * height) / 8);

 for(y=(colors+4); y < (height+4); y++) {
   for(x=0; x<width; x++) {
     if(xpm[y][x] == whiteLetter) {
	maskData[byteIndex] |= 0x80;
	bitmapData[byteIndex] |= 0x80;
} else if(xpm[y][x] == blackLetter) {
	maskData[byteIndex] |= 0x80;
     }
     if(++bitCount == 8) {
	byteIndex++;
	bitCount = 0;
} else {
	maskData[byteIndex] >>= 1;
	bitmapData[byteIndex] >>= 1;
     }
   }
 }
 bitmap = gdk_bitmap_create_from_data (NULL, bitmapData, width, height);
 mask = gdk_bitmap_create_from_data (NULL, maskData, width, height);
 cursor = gdk_cursor_new_from_pixmap (bitmap, mask, whiteColor, blackColor, xHot, yHot);

 g_free(bitmapData);
 g_free(maskData);

 return(cursor);
}


void box_released(GtkWidget *widget, GdkEventButton *event)
{
 gtk_grab_remove(widget);
 gdk_pointer_ungrab(0);
}

void box_motion(GtkWidget *widget, GdkEventMotion *event)
{
}

void box_pressed(GtkWidget *widget, GdkEventButton *event, GtkWidget *topLevel)
{
 GdkCursor *cursor;

 if (event->type == GDK_2BUTTON_PRESS) {
   if (doubleclicks) {
     doubleclicks = 0;
     cursor = newCursorFromXPM (cursor_z, 'b', &white, 'a', &red, 12, 13);
     gdk_window_set_cursor (topLevel->window, cursor);
   }
   else {
     doubleclicks = 1;
     cursor = newCursorFromXPM (cursor_f, 'b', &white, 'a', &red, 12, 13);
     gdk_window_set_cursor (topLevel->window, cursor);
   }
 }
 gtk_grab_add(widget);
 gdk_pointer_grab(widget->window, TRUE,
		   GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, NULL, NULL, 0);

 return;
}

int main(int argc, char *argv[]) {
 GtkWidget *topwin;
 GtkWidget *button;
 GdkCursor *cursor;

 gtk_set_locale();
 gtk_init(&argc,&argv);
 topwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);

 doubleclicks = 0;
 gtk_container_border_width(GTK_CONTAINER(topwin),50);

 button = gtk_toggle_button_new_with_label("button");
 gtk_widget_set_events (button, GDK_BUTTON_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);
 gtk_signal_connect (GTK_OBJECT(button), "button_press_event", GTK_SIGNAL_FUNC(box_pressed), topwin);
 gtk_signal_connect (GTK_OBJECT(button), "button_release_event", GTK_SIGNAL_FUNC(box_released), NULL);
 gtk_signal_connect (GTK_OBJECT(button), "motion_notify_event", GTK_SIGNAL_FUNC(box_motion), NULL);
 gtk_container_add(GTK_CONTAINER(topwin), button);
 gtk_widget_show(button);
 gtk_widget_show(topwin);

 cursor = newCursorFromXPM (cursor_z, 'b', &white, 'a', &red, 12, 13);
 gdk_window_set_cursor (topwin->window, cursor);

 gtk_main();
 return 0;
}



X pixmap

X pixmap

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

int doubleclicks;

#include "cursor_f.xpm"
#include "cursor_z.xpm"

GdkColor red = {0,0xFFFF,0x0000,0x0000};
GdkColor white = {0,0xFFFF,0xFFFF,0xFFFF};


GdkCursor *newCursorFromXPM (gchar *xpm[],
			     gchar blackLetter,GdkColor *blackColor,
			     gchar whiteLetter,GdkColor *whiteColor,
			     gint xHot,gint yHot)
{
  gint height, width, colors, pchars, x, y;
  GdkBitmap *bitmap, *mask;
  GdkCursor *cursor;
  guchar *bitmapData, *maskData;
  gint byteIndex = 0, bitCount = 0;

  sscanf (xpm[0],"%d %d %d %d", &width, &height, &colors, &pchars);
  g_assert(pchars == 1);

  bitmapData = (guchar *)g_malloc((width * height) / 8);
  maskData = (guchar *)g_malloc((width * height) / 8);

  for(y=(colors+4); y < (height+4); y++) {
    for(x=0; x<width; x++) {
      if(xpm[y][x] == whiteLetter) {
	maskData[byteIndex] |= 0x80;
	bitmapData[byteIndex] |= 0x80;
      } 
      else if(xpm[y][x] == blackLetter) {
	maskData[byteIndex] |= 0x80;
      }
      if(++bitCount == 8) {
	byteIndex++;
	bitCount = 0;
      } 
      else {
	maskData[byteIndex] >>= 1;
	bitmapData[byteIndex] >>= 1;
      }
    }
  }
  bitmap = gdk_bitmap_create_from_data (NULL, bitmapData, width, height);
  mask = gdk_bitmap_create_from_data (NULL, maskData, width, height);
  cursor = gdk_cursor_new_from_pixmap (bitmap, mask, whiteColor, blackColor, xHot, yHot);

  g_free(bitmapData);
  g_free(maskData);

  return(cursor);
}


void box_released(GtkWidget *widget, GdkEventButton *event)
{
  gtk_grab_remove(widget);
  gdk_pointer_ungrab(0);
}

void box_motion(GtkWidget *widget, GdkEventMotion *event)
{
}

void box_pressed(GtkWidget *widget, GdkEventButton *event, GtkWidget *topLevel)
{
  GdkCursor *cursor;

  if (event->type == GDK_2BUTTON_PRESS) {
    if (doubleclicks) {
      doubleclicks = 0;
      cursor = newCursorFromXPM (cursor_z, 'b', &white, 'a', &red, 12, 13);
      gdk_window_set_cursor (topLevel->window, cursor);
    }
    else {
      doubleclicks = 1;
      cursor = newCursorFromXPM (cursor_f, 'b', &white, 'a', &red, 12, 13);
      gdk_window_set_cursor (topLevel->window, cursor);
    }
  }
  gtk_grab_add(widget);
  gdk_pointer_grab(widget->window, TRUE,
		   GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK, NULL, NULL, 0);

  return;
}

int main(int argc, char *argv[]) 
{
  GtkWidget *topwin;
  GtkWidget *button;
  GdkCursor *cursor;

  gtk_set_locale();
  gtk_init(&argc,&argv);
  topwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  doubleclicks = 0;
  gtk_container_border_width(GTK_CONTAINER(topwin),50);

  button = gtk_toggle_button_new_with_label("button");
  gtk_widget_set_events (button, GDK_BUTTON_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);
  gtk_signal_connect (GTK_OBJECT(button), "button_press_event", GTK_SIGNAL_FUNC(box_pressed), topwin);
  gtk_signal_connect (GTK_OBJECT(button), "button_release_event", GTK_SIGNAL_FUNC(box_released), NULL);
  gtk_signal_connect (GTK_OBJECT(button), "motion_notify_event", GTK_SIGNAL_FUNC(box_motion), NULL);
  gtk_container_add(GTK_CONTAINER(topwin), button);
  gtk_widget_show(button);
  gtk_widget_show(topwin);

  cursor = newCursorFromXPM (cursor_z, 'b', &white, 'a', &red, 12, 13);
  gdk_window_set_cursor (topwin->window, cursor);

  gtk_main();
  return 0;
}       

Attachment: makefile
Description: application/java-vm



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