Re: [gtk-list] Re: Mouse buttons in callbacks



On 22 Aug, Owen Taylor wrote:
> 
> "Robert S. Mallozzi" <mallors@crazyhorse.msfc.nasa.gov> writes:
> 
>> Hello,
>> 
>> I would like to use the different mouse buttons within
>> a gtk button callback to do different tasks.  How do I
>> discriminate among the different mouse buttons in the
>> callback function?  Currently, I have button_press_events,
>> and button_release_events registered with the button, and
>> this works OK, execpt I must also trap 
>> enter_notify_events, and leave_notify_events to ensure that
>> mouse button 1 executes its command only upon button release.
>> This involves a static variable in the callback to keep 
>> track of whether the cursor is on the button when it is
>> released.  Surely there is a simple way to find which
>> button was pressed when in a "regular" callback function?
> 
> You can do something like:
> 
>  GdkEvent *event = gtk_get_current_event();
> 
>  if (event && 
>      (event->type == GTK_BUTTON_RELEASE) && 
>      (event->button == 3))
>    {
>      /* do whatever is necessary for a right-mouse-button click */
>    }
> 
>  gdk_event_free (event);
> 
> Hope this helps,
>                                         Owen
> 


Hi,

Attaching the "clicked" signal to a button, the callback is 
never called for mouse buttons 2 and 3, only button 1. (Also,
a button press event is never sent, only release).  The only way
I can get all mouse buttons to invoke the callback is by trapping
"button_press_event", "button_release_event", etc.   This results
in a lot of code to perform such a simple action.  

Create a single button, and add a callback with

    gtk_signal_connect (GTK_OBJECT (myButton), "clicked",
        GTK_SIGNAL_FUNC (myCallback), NULL);

Try using the following callback, and you will get only 
"button 1 release".  Is it the design of the "clicked" signal
to only trap button 1 release events?




/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void myCallback (GtkWidget *widget, gpointer data)
{
    
    GdkEvent *event = gtk_get_current_event();
    
    
    
    if (event && (event->type == GDK_BUTTON_PRESS)) { 
       
       switch (event->button.button) {
           case 1: g_print ("button 1 press\n"); break;
           case 2: g_print ("button 2 press\n"); break;
           case 3: g_print ("button 3 press\n"); break;
       }           
    }

    if (event && (event->type == GDK_BUTTON_RELEASE)) { 
       
       switch (event->button.button) {
           case 1: g_print ("button 1 release\n"); break;
           case 2: g_print ("button 2 release\n"); break;
           case 3: g_print ("button 3 release\n"); break;
       }           
    }

    gdk_event_free (event);

}


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Robert S. Mallozzi                                          256-544-0887
                                                         Mail Code ES 84
Work: http://crazyhorse.msfc.nasa.gov/      Marshall Space Flight Center 
Play: http://cspar.uah.edu/~mallozzir/              Huntsville, AL 35812
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



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