Re: Simple draw question



Le samedi 5 mai 2007 00:48, Kevin DeKorte a écrit :
Hi,

I have a situation where I need to highlight an eventbox when a user
enters it and unhighlight it when the users leaves. I'm using this code
but the color I'm drawing in the leave_button_callback is always wrong
and varies from run to run with the program. Any ideas what I am doing
wrong. I was looking for a XOR option or something but I don't see it.

gboolean enter_button_callback (GtkWidget *widget, GdkEventCrossing
*event, gpointer data)
{
      GdkGC *gc;
      gc = gdk_gc_new(widget->window);
    gdk_draw_rectangle(widget->window, gc, FALSE, 0, 0,
widget->allocation.width-1,
                           widget->allocation.height-1);
    gdk_gc_unref(gc);

      return FALSE;
}

gboolean leave_button_callback (GtkWidget *widget, GdkEventCrossing
*event, gpointer data)
{
      GdkGC *gc;
      GdkGCValues v;
      GdkColor color;
      GdkColormap *map;

      gc = gdk_gc_new(widget->window);
      map = gdk_gc_get_colormap(gc);
      gdk_gc_get_values(gc,&v);
      gdk_colormap_query_color(map,v.background.pixel,&color);

      gdk_gc_set_foreground(gc,&color);

    gdk_draw_rectangle(widget->window, gc, FALSE, 0, 0,
widget->allocation.width-1,
                           widget->allocation.height-1);
    gdk_gc_unref(gc);
      return FALSE;
}

You should never draw anything in an enter/leave callback, but only in an 
expose event. Also note that creating/destroying a GC each time you draw it 
is very slow, so you should do something like this instead:

enter_button_callback()
{
        gtk_widget_set_state(widget, GTK_STATE_PRELIGHT);

        /* not sure this is required, as it's maybe called by set_state? */
        /* you may use gdk_window_invalidate_* instead for better performances,
         * see the docs for more infos */
        gtk_widget_queue_draw(widget);
}

leave_button_callback()
{
        /* same with GTK_STATE_NORMAL */
}

expose_button_callback()
{
        gdk_draw_rectangle(widget->window, widget->state->fg_gc[widget->state], ...)
}

-- 
Cédric Lucantis



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