rubberbanding with GTK1* and GTK2*



Can anyone explain to me why the very small program beneath, demonstrating
a very basic form of rubberbanding, works excellent while compiled
with GTK1* but does not work correctly when compiled with GTK2*.
There seems to be a problem with reading the cursor movements, but whatever it
is, the program is useless when compiled with GTK2*.
I checked the incompatibles list on the GTK-site, but I could not find
a cause for this behaviour. Of course I did things like enable_depreciated
and a lot of other small changings of the program, but nothing seems to work.
I have written already a lot of rubber-banding functions/programs, using
different graphics libraries, so I am not a complete newby in this field.
As I have to use rubberbanding a lot in my programs and and I do not want
to go on using GTK1* , I am very anxious to find a solution!!
Thanks a lot for any help.
Kees de Jong, Delft, Holland.


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

  GtkWidget *window;
  GtkWidget *fixed;
  GtkWidget *drawing_area;
  GdkGC *gc;
  GdkColor *fore;
  GdkColor *back;
  gint ii;


 GdkColor *newcolor(long red, long green, long blue)
  {
  GdkColor *c = (GdkColor *) g_malloc (sizeof (GdkColor));
  c->red = red;
  c->green = green;
  c->blue = blue;
  gdk_color_alloc(gdk_colormap_get_system(), c);
  return (c);
  }


 void destroy (GtkWidget *widget, gpointer *data)
  {
  exit(0);
  }


 void cleararea(GtkWidget *drarea)
  {
  GdkDrawable *drawable;
  drawable = drarea->window;
  gdk_draw_rectangle (drawable,
                      drarea->style->white_gc,
                      TRUE,
                      0, 0,
                      drarea->allocation.width,
                      drarea->allocation.height);
  }


 gint drawareacb(GtkWidget *widget, GdkEventButton *event, gpointer data)
  {
  gint  x0, y0;
  gint x[2], y[2];
  if(ii == 0)
   {
   cleararea(drawing_area);
   ii=1;
   }
  gdk_window_get_pointer(drawing_area->window, &x0, &y0, NULL);
  switch(event->type)
  {
  case GDK_BUTTON_PRESS:
  x[0]=x0;
  y[0]=y0;
  gdk_draw_line(drawing_area->window, gc, 200, 400, x0, y0);
  break;
  case GDK_MOTION_NOTIFY:
  x[1]=x0;
  y[1]=y0;
  gdk_draw_line(drawing_area->window, gc, 200, 400, x[0], y[0]);
  gdk_draw_line(drawing_area->window, gc, 200, 400, x[1], y[1]);
  x[0]=x[1];
  y[0]=y[1];
  break;
  }
 return(0);
 }


 void createinterface (char *title) 
  {
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), title);
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (destroy), NULL);

  fixed=gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), fixed);
  gtk_widget_show(fixed);

  drawing_area=gtk_drawing_area_new();
  gtk_widget_set_events(drawing_area, GDK_BUTTON_PRESS_MASK |
                        GDK_BUTTON_MOTION_MASK);
  gtk_signal_connect(GTK_OBJECT (drawing_area), "motion_notify_event", 
                       GTK_SIGNAL_FUNC (drawareacb), NULL);
  gtk_signal_connect(GTK_OBJECT (drawing_area), "button_press_event", 
                       GTK_SIGNAL_FUNC (drawareacb), NULL);
  gtk_drawing_area_size(GTK_DRAWING_AREA (drawing_area), 800, 600);
  gtk_fixed_put(GTK_FIXED(fixed), drawing_area, 0, 0);
  gtk_widget_show (drawing_area);
  gtk_widget_show (window);

  gc = gdk_gc_new (drawing_area->window);
  gdk_gc_set_foreground (gc, fore);
  gdk_gc_set_background (gc, back);
  gdk_gc_set_function(gc, GDK_XOR);
  }


 int main (int argc, char *argv[])
  {
  gtk_init (&argc, &argv);
  ii=0;
  fore=newcolor(0, 0, 0);
  back=newcolor(65535, 65535, 65535);
  fore->pixel=(back->pixel ^ fore->pixel);
  createinterface("rubber");
  gtk_main ();
  exit (0);
  }








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