Re: Simple draw question

2007-05-06 Thread Cédric Lucantis
 
  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:

 Ok, I have questions here and they are because I really want to know why
 and not start anything.

 1. Why is drawing in the enter in leave callback not something you
 should do? Is it because if you do to much it can cause a backlog of
 events?

Merely a 'good design' question, but also because some prepare/release stuff 
is required before and after drawing on a GdkWindow (basicaly to set the 
clipping region and to setup the double buffer and copy its contents after 
the drawing operations). This is done by gdk_window_begin_paint_rect() and 
gdk_window_end_paint() which are automatically called by gtk when an expose 
event is processed. I think you can also call these yourself if you really 
want to keep your drawing within the leave/enter events, but a 
more 'conventional' way is to invalidate the window and then call 
gdk_window_process_updates() to process the expose event immediately.
You'll find a lot of interesting things about that in the GdkWindow doc.


 2. Actually while creating and destroying a GC may be slow (and I don't
 think it is actually that slow) it seems that in this case I should have
 just looked them up off the widget (which I didn't know there were
 several GCs off of the widget style).

Not sure it is that slow, but using an existing one is obviously faster than 
creating it. It's also better (but of course not mandatory) to use the ones 
provided by the widget style, first because that's why they are here, and 
because it'll keep your app consistent if the user modify some style/resource 
settings (but I don't know much about that).
For info, manys GCs are created by the style, see gtkstyle.h for a list of 
them.


 3. I did try what you recommended and basically I found this. The expose
 event was not fired enough. I _think_ what happens is that GTK optimizes
 calls to event handlers and so if you send it 5 requests to expose on
 the same object and they stack up in the dispatcher, you only get one
 expose event called (this is actually the right thing to do) and so when
 I was testing this, I would get only one call to expose and so the box
 would never be drawn. (my guess could be totally wrong here).

I don't understand what you mean here. It's true that gtk optimizes the 
invalidate calls, not by stacking them but by keeping an 'update_area' for 
each window and thus only redrawing the dirty part of it when nothing else 
has to be done (this is managed by an idle event). But even if you get only 
one call to expose, shouldn't it be enough to redraw it ?

Finally, note that I'm definitely not a gtk wizard, so maybe am I only saying 
stupid things here ;)

-- 
Cédric Lucantis
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Simple draw question

2007-05-05 Thread Cédric Lucantis
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
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Simple draw question

2007-05-05 Thread Kevin DeKorte
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Cédric Lucantis wrote:
 Le samedi 5 mai 2007 00:48, Kevin DeKorte a écrit :
 
 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:


Ok, I have questions here and they are because I really want to know why
and not start anything.

1. Why is drawing in the enter in leave callback not something you
should do? Is it because if you do to much it can cause a backlog of
events?

2. Actually while creating and destroying a GC may be slow (and I don't
think it is actually that slow) it seems that in this case I should have
just looked them up off the widget (which I didn't know there were
several GCs off of the widget style).

3. I did try what you recommended and basically I found this. The expose
event was not fired enough. I _think_ what happens is that GTK optimizes
calls to event handlers and so if you send it 5 requests to expose on
the same object and they stack up in the dispatcher, you only get one
expose event called (this is actually the right thing to do) and so when
I was testing this, I would get only one call to expose and so the box
would never be drawn. (my guess could be totally wrong here).

 
 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], 
 ...)
 }
 

Oh for others reading this the line should be

gdk_draw_rectangle(widget-window, widget-style-fg_gc[widget-state], ...)

Kevin
- --
Get my public GnuPG key from
http://pgp.mit.edu:11371/pks/lookup?op=getsearch=0x7D0BD5D1
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFGPIaa6w2kMH0L1dERAnTrAJ9dP2jchOhrHVAGf8FBNjCm4lMtPQCdHjtT
516nI5zuTdUxRaXbRHpJ/Ew=
=/rw5
-END PGP SIGNATURE-
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Simple draw question

2007-05-05 Thread Chris Sparks
I have been able, using GDK, to draw while entering and leaving.  One 
example could be in trying to create
a highlight effect as you enter a window.

Chris

Kevin DeKorte wrote:
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1

 Cédric Lucantis wrote:
   
 Le samedi 5 mai 2007 00:48, Kevin DeKorte a écrit :

 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:
 


 Ok, I have questions here and they are because I really want to know why
 and not start anything.

 1. Why is drawing in the enter in leave callback not something you
 should do? Is it because if you do to much it can cause a backlog of
 events?

 2. Actually while creating and destroying a GC may be slow (and I don't
 think it is actually that slow) it seems that in this case I should have
 just looked them up off the widget (which I didn't know there were
 several GCs off of the widget style).

 3. I did try what you recommended and basically I found this. The expose
 event was not fired enough. I _think_ what happens is that GTK optimizes
 calls to event handlers and so if you send it 5 requests to expose on
 the same object and they stack up in the dispatcher, you only get one
 expose event called (this is actually the right thing to do) and so when
 I was testing this, I would get only one call to expose and so the box
 would never be drawn. (my guess could be totally wrong here).

   
 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], 
 ...)
 }

 

 Oh for others reading this the line should be

 gdk_draw_rectangle(widget-window, widget-style-fg_gc[widget-state], ...)

 Kevin
 - --
 Get my public GnuPG key from
 http://pgp.mit.edu:11371/pks/lookup?op=getsearch=0x7D0BD5D1
 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.7 (GNU/Linux)
 Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

 iD8DBQFGPIaa6w2kMH0L1dERAnTrAJ9dP2jchOhrHVAGf8FBNjCm4lMtPQCdHjtT
 516nI5zuTdUxRaXbRHpJ/Ew=
 =/rw5
 -END PGP SIGNATURE-
 ___
 gtk-app-devel-list mailing list
 gtk-app-devel-list@gnome.org
 http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

   

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Simple draw question

2007-05-04 Thread Kevin DeKorte
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

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;
}




- --
Get my public GnuPG key from
http://pgp.mit.edu:11371/pks/lookup?op=getsearch=0x7D0BD5D1
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Remi - http://enigmail.mozdev.org

iD8DBQFGO7gy6w2kMH0L1dERAqmZAJ0UORbfl3tgr0wRdruDvk8yRNUrOwCbB/LQ
HwpMx+9hMb9iGwEX6WCr+KM=
=hEvS
-END PGP SIGNATURE-
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list