Re: Event triggering with primitives



Catherine Dunn wrote:
Hello,

I am new to GTK. I am trying to create an application with several primitives
drawn in a drawing area. I would like certain events to occur whenever the user
clicks on/rolls over the primitives, but I am having trouble doing this since
the primitive functions return void not GtkWidget. I'd appreciate some advice.

By "primitive functions" you mean GDK Drawing primitives ?

Hmmm,
    So you have a Drawing Area, you can gdk_draw_* to it's
`GTK_WIDGET (darea)->window' inside an expose handler.

You can trap mouse events by connecting to "button-press-event"
"button-release-event" & "motion-notify-event" (ofcourse on the
said Drawing Area widget).

Since drawing area widgets are just drawing areas, they dont recieve
these events by default, you'll have to use gtk_widget_add_events ()
to modify the event mask.

/********************************************/

/* Create your darea ... add events to it
 */
gtk_widget_add_events
    (GTK_WIDGET (darea),
     GDK_POINTER_MOTION_MASK | // Might be GDK_BUTTON_MOTION_MASK
     GDK_BUTTON_PRESS_MASK   |
     GDK_BUTTON_RELEASE_MASK);

g_signal_connect (G_OBJECT (darea), "motion-notify-event",
                  my_cb, NULL);
g_signal_connect (G_OBJECT (darea), "button-press-event",
                  my_cb, NULL);
g_signal_connect (G_OBJECT (darea), "button-release-event",
                  my_cb, NULL);

/* Later in your "my_cb":
 */
gint
my_cb (GtkWidget *darea,
       GdkEvent  *event,
       gpointer   data) {

    /* Here you'll want to decide what you'll do depending on what
     * kind of event you recieved, and optionally you'll want to update
     * an offscreen pixmap (GdkPixmap) which you can later use to copy
     * directly to widget->window during expose events, you'll want to
     * do that because your drawing area will be potentialy be exposed
     * more often than the content will change.
     */

    /* Mark dirty region/rectangle: */
    gdk_window_invalidate_region/rect (widget->window);
    /* or the lazy version: */
    gtk_widget_queue_draw (darea);
}

/********************************************/
    You might also consider using `GDK_POINTER_MOTION_HINT_MASK'
instead of just the motion mask, this will make sure you only recieve
ONE "motion-notify-event" untill you call:
    `gdk_window_get_pointer (darea->window, ...)'

This is usefull to let your code breathe easier by dropping motion
events that you just didn't have time to handle.

Dont worry, by the time you go through the API reference looking
for all these functions I put down, you'll be an expert :)

Cheers,
                           -Tristan



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