void mouse(int button, int state, int x, int y)
{
    if ( state == 0 )
    {
        switch ( button )
        {
        case GLUT_LEFT_BUTTON:  break;
        case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(true);
                                break;
        case GLUT_RIGHT_BUTTON:     tball.setAutoPositionNeg(true);
                                break;
        }
        mouseb |= 1 << button;
    }
    else if ( state == 1 )
    {
        switch ( button )
        {
        case GLUT_LEFT_BUTTON:  break;
        case GLUT_MIDDLE_BUTTON:tball.setAutoPosition(false);
                                break;
        case GLUT_RIGHT_BUTTON:     tball.setAutoPositionNeg(false);
                                break;
        }       
        mouseb &= ~(1 << button);
    }
    lastx = x;
    lasty = y;
}
void MyOSGQGLWidget::mousePressEvent(QMouseEvent *me)
{
    switch(me->button()) 
    {
        case Qt::MidButton:
            tball.setAutoPosition(true);
            break;
        case Qt::RightButton:
            tball.setAutoPositionNeg(true);
            break;
        default:
            break;
    }

    mouseb |= me->button();     
    lastx = me->x();
    lasty = me->y();
}
static OSStatus handleMouseEvent(EventHandlerCallRef nextHandler, EventRef event, void *userData)
{
    OSStatus err;
    OSG::Real32 w,h,a,b,c,d;

    // Get the pressed mouse button
    EventMouseButton mouseButton;
    err = GetEventParameter(event, kEventParamMouseButton, typeMouseButton, 0, sizeof(mouseButton), 0, &mouseButton);
    if (err != noErr)
        return err;

    // Get the modifier keys
    ::UInt32 modifierKeys;
    err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, 0, sizeof(modifierKeys), 0, &modifierKeys);
    if (err != noErr)
        return err;

    // Traditionally, Apple mice just have one button. It is common practice to simulate
    // the middle and the right button by pressing the option or the control key.
    if (mouseButton == kEventMouseButtonPrimary)
    {
        if (modifierKeys & optionKey)
            mouseButton = kEventMouseButtonTertiary;
        if (modifierKeys & controlKey)
            mouseButton = kEventMouseButtonSecondary;
    }

    // Get the location of the mouse pointer
    ::Point location;
    err = GetEventParameter(event, kEventParamMouseLocation, typeQDPoint, 0, sizeof(location), 0, &location);
    if (err != noErr)
        return err;

    // Handle the different kinds of events
    ::UInt32 eventKind = GetEventKind(event);
    switch (eventKind)
    {
    // mouse button pressed
    case kEventMouseDown:
        lastx = location.h;
        lasty = location.v;
        switch (mouseButton)
        {
        case kEventMouseButtonPrimary: // left button
            break;
        case kEventMouseButtonSecondary: // right button
            tball.setAutoPositionNeg(true);
            break;
        case kEventMouseButtonTertiary: // middle button
            tball.setAutoPosition(true);
            break;
        }
        break;

    // mouse button released
    case kEventMouseUp:
        switch (mouseButton)
        {
        case kEventMouseButtonPrimary: // left button
            break;
        case kEventMouseButtonSecondary: // right button
            tball.setAutoPositionNeg(false);
            break;
        case kEventMouseButtonTertiary: // middle button
            tball.setAutoPosition(false);
            break;
        }
        break;

    // mouse moved while a button is pressed
    case kEventMouseDragged:
        w = win->getWidth();
        h = win->getHeight();
        a = -2. * ( lastx / w - .5 );
        b = -2. * ( .5 - lasty / h );
        c = -2. * ( location.h / w - .5 );
        d = -2. * ( .5 - location.v / h );
        switch (mouseButton)
        {
        case kEventMouseButtonPrimary: // left button
            tball.updateRotation( a, b, c, d );
            break;
        case kEventMouseButtonSecondary: // right button
            tball.updatePositionNeg( a, b, c, d );
            break;
        case kEventMouseButtonTertiary: // middle button
            tball.updatePosition( a, b, c, d );
            break;
        }
        lastx = location.h;
        lasty = location.v;

        // Redraw the whole window
        redraw();

        break;
    }

    // We have to return eventNotHandledErr, otherwise the system is
    // not able to operate the menu and the window border
    return eventNotHandledErr;
}