Re: GnomeCanvas and inverting Y cooordinates



On Wed, 10 Nov 1999 10:02:07 +0800 (WST), James Henstridge <james@daa.com.au> wrote:
>
> Are you using the correct colormap/visual for the canvas?  If it is a
> normal canvas, you should call the following before creating the canvas:
>   gtk_widget_push_colormap(gdk_imlib_get_colormap());
>   gtk_widget_push_visual(gdk_imlib_get_visual());
>
>
> If it is an antialiased canvas, call:
>   gtk_widget_push_colormap(gdk_rgb_get_cmap());
>   gtk_widget_push_visual(gdk_rgb_get_visual());
>
> You could probably call one of these at the start of your program so that
> all widgets use this colormap/visual.
>
> James.
>
> --
> Email: james@daa.com.au
> WWW:   http://www.daa.com.au/~james/
>
I wasn't, but making this change did not solve the odd color problems.
Here is a complete code example - sorry that it's a bit long, but I'm
hoping someone can just point at my code and show me my mistake.
To demonstrate, click on a rectangle and move it across the other
rectangle.

Thanks,
Kent

#define USE_AA (1)
#define INVERT (1)

#include <gnome.h>

static void zoom_changed (GtkAdjustment *adj, gpointer data) {
   gnome_canvas_set_pixels_per_unit (data, adj->value);
}

static gint item_event(GnomeCanvasItem *item, GdkEvent *event, gpointer data) {
   static double x, y;
   double new_x, new_y;
   GdkCursor *fleur;
   static int dragging;
   double item_x, item_y;

   /* set item_[xy] to the event x,y position in the parent's item-relative coordinates */
   item_x = event->button.x;
   item_y = event->button.y;
   gnome_canvas_item_w2i (item->parent, &item_x, &item_y);

   switch (event->type) {
   case GDK_BUTTON_PRESS:
      if (event->button.button == 1) {
         x = item_x;
         y = item_y;

         fleur = gdk_cursor_new( GDK_FLEUR);
         gnome_canvas_item_grab( item,
                                 GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
                                 fleur,
                                 event->button.time);

         gdk_cursor_destroy(fleur);
         dragging = TRUE;
      }
      break;
   case GDK_MOTION_NOTIFY:
      if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
         new_x = item_x;
         new_y = item_y;

         gnome_canvas_item_move (item, new_x - x, new_y - y);
         x = new_x;
         y = new_y;
      }
      break;
   case GDK_BUTTON_RELEASE:
      gnome_canvas_item_ungrab (item, event->button.time);
      dragging = FALSE;
      break;

   default:
      break;
   }

   return FALSE;
}

static gint delete_event_cb(GtkWidget * window, GdkEventAny * e, gpointer data) {
   gtk_main_quit();
   return FALSE;
}

int main(int argc, char *argv[]) {
   GtkWidget         *window;
   GtkWidget         *sw;
   GtkWidget         *canvas;
   GnomeCanvasGroup  *group;
   GnomeCanvasItem   *item;

   gnome_init("canvas-example", "0.0", argc, argv);

   gdk_rgb_init();

   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

   gtk_signal_connect(GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete_event_cb),NULL);

   sw = gtk_scrolled_window_new(NULL, NULL);

   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);

#if USE_AA
   gtk_widget_push_visual(gdk_rgb_get_visual());
   gtk_widget_push_colormap(gdk_rgb_get_cmap());

   canvas = gnome_canvas_new_aa();
#else
   gtk_widget_push_visual(gdk_imlib_get_visual());
   gtk_widget_push_colormap(gdk_imlib_get_colormap());

   canvas = gnome_canvas_new();
#endif

   gtk_widget_pop_colormap();
   gtk_widget_pop_visual();

   group    = gnome_canvas_root(GNOME_CANVAS(canvas));

#if INVERT
   {
      double   affine[] = {1,0,0,-1,0,0};
      gnome_canvas_item_affine_relative(GNOME_CANVAS_ITEM(group),affine);
   }
#endif

   item = gnome_canvas_item_new( group                               ,
                                 gnome_canvas_rect_get_type()        ,
                                 "x1"              , (double)5.0     ,
                                 "y1"              , (double)5.0     ,
                                 "x2"              , (double)100.0   ,
                                 "y2"              , (double)100.0   ,
                                 "fill_color"      , "orange"        ,
                                 "outline_color"   , "black"         ,
                                 "width_pixels"    , 1               ,
                                 NULL                                );

   gtk_signal_connect(GTK_OBJECT(item),"event",(GtkSignalFunc)item_event,NULL);

   item = gnome_canvas_item_new( group                               ,
                                 gnome_canvas_rect_get_type()        ,
                                 "x1"              , (double)105.0   ,
                                 "y1"              , (double)105.0   ,
                                 "x2"              , (double)200.0   ,
                                 "y2"              , (double)200.0   ,
                                 "fill_color"      , "blue"       ,
                                 "outline_color"   , "black"         ,
                                 "width_pixels"    , 1               ,
                                 NULL                                );

   gtk_signal_connect(GTK_OBJECT(item),"event",(GtkSignalFunc)item_event,NULL);

   gtk_container_add(GTK_CONTAINER(sw),canvas);
   gtk_container_add(GTK_CONTAINER(window),sw);

   gtk_window_set_default_size(GTK_WINDOW(window), 400, 400);

   gtk_widget_show_all(window);

   gtk_main();

   shutdown_program(0);

   return(0);
}






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