示例#1
0
OSStatus MouseHandler(EventHandlerCallRef next,
		      EventRef event, void *data)
{
    HIViewRef control;
    WindowRef window;
    ControlKind view;
    UInt32 kind;

    // Get the event kind

    kind = GetEventKind(event);

    // Get the window

    GetEventParameter(event, kEventParamWindowRef,
                      typeWindowRef, NULL, sizeof(window),
                      NULL, &window);

    // Get the view

    HIViewGetViewForMouseEvent(HIViewGetRoot(window), event,
			       &control);

    // Get the control kind

    GetControlKind(control, &view);

    // Switch on event kind

    switch (kind)
    {
        // Mouse down

    case kEventMouseDown:

        // Switch on control kind

        switch (view.kind)
        {
            // Combo box

        case kControlKindHIComboBox:

            // Do nothing, let the default event handler do it's thing

            break;

            // Everything else

        default:

            // Clear the keyboard focus, otherwise the focus stays on the
            // combo box and makes it drop down when the user presses a key

            ClearKeyboardFocus(window);
            break;
        }

    default:
        return eventNotHandledErr;
    }

    // Return event not handled so that the default event handler gets
    // to do it's thing

    return eventNotHandledErr;
}
示例#2
0
// static
pascal OSStatus IGraphicsCarbon::MainEventHandler(EventHandlerCallRef pHandlerCall, EventRef pEvent, void* pGraphicsCarbon)
{
  IGraphicsCarbon* _this = (IGraphicsCarbon*) pGraphicsCarbon;
  IGraphicsMac* pGraphicsMac = _this->mGraphicsMac;
  UInt32 eventClass = GetEventClass(pEvent);
  UInt32 eventKind = GetEventKind(pEvent);

  switch (eventClass)
  {
    case kEventClassKeyboard:
    {
      switch (eventKind)
      {
        case kEventRawKeyDown:
        {
          if (_this->mTextEntryView)
            return eventNotHandledErr;

          bool handle = true;
          int key;
          UInt32 k;
          GetEventParameter(pEvent, kEventParamKeyCode, typeUInt32, NULL, sizeof(UInt32), NULL, &k);

          char c;
          GetEventParameter(pEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &c);

          if (k == 49) key = KEY_SPACE;
          else if (k == 125) key = KEY_DOWNARROW;
          else if (k == 126) key = KEY_UPARROW;
          else if (k == 123) key = KEY_LEFTARROW;
          else if (k == 124) key = KEY_RIGHTARROW;
          else if (c >= '0' && c <= '9') key = KEY_DIGIT_0+c-'0';
          else if (c >= 'A' && c <= 'Z') key = KEY_ALPHA_A+c-'A';
          else if (c >= 'a' && c <= 'z') key = KEY_ALPHA_A+c-'a';
          else handle = false;

          if(handle)
            handle = pGraphicsMac->OnKeyDown(_this->mPrevX, _this->mPrevY, key);

          if(handle)
            return noErr;
          else
            return eventNotHandledErr;

        }
      }
    }
    case kEventClassControl:
    {
      switch (eventKind)
      {
        case kEventControlDraw:
        {
          int gfxW = pGraphicsMac->Width(), gfxH = pGraphicsMac->Height();

          IRECT r = GetRegionRect(pEvent, gfxW, gfxH);

          CGrafPtr port = 0;

          if (_this->mIsComposited)
          {
            GetEventParameter(pEvent, kEventParamCGContextRef, typeCGContextRef, 0, sizeof(CGContextRef), 0, &(_this->mCGC));
            CGContextTranslateCTM(_this->mCGC, 0, gfxH);
            CGContextScaleCTM(_this->mCGC, 1.0, -1.0);
            pGraphicsMac->Draw(&r);
          }
          else
          {
            GetEventParameter(pEvent, kEventParamGrafPort, typeGrafPtr, 0, sizeof(CGrafPtr), 0, &port);
            QDBeginCGContext(port, &(_this->mCGC));
            
            //RgnHandle clipRegion = NewRgn();
            //GetPortClipRegion(port, clipRegion);
            
            Rect portBounds;
            GetPortBounds(port, &portBounds);

            int offsetW = 0;
            int offsetH = -portBounds.top;
            //int offsetH = (portBounds.bottom - portBounds.top) - gfxH; // this almost works with AS, but clip rect seems wrong when previewing/breaks RTAS
            
            if ((portBounds.right - portBounds.left) >= gfxW)
            {
              offsetW = 0.5 * ((portBounds.right - portBounds.left) - gfxW);
            }
            
            CGContextTranslateCTM(_this->mCGC, portBounds.left + offsetW, offsetH);
            
            r = IRECT(0, 0, pGraphicsMac->Width(), pGraphicsMac->Height());
            pGraphicsMac->Draw(&r); // Carbon non-composited will redraw everything, the IRECT passed here is the entire plugin-gui
            
            QDEndCGContext(port, &(_this->mCGC));
            
            //DisposeRgn(clipRegion);
          }
          return noErr;
        }
      }
      break;
    }
    case kEventClassMouse:
    {
      HIPoint hp;
      GetEventParameter(pEvent, kEventParamWindowMouseLocation, typeHIPoint, 0, sizeof(HIPoint), 0, &hp);

      #ifdef RTAS_API
      // Header offset
      hp.x -= _this->GetLeftOffset();
      hp.y -= _this->GetTopOffset();

      Rect bounds;
      GetWindowBounds(_this->mWindow, kWindowTitleBarRgn, &bounds);

      // adjust x mouse coord if the gui is less wide than the window
//      int windowWidth = (bounds.right - bounds.left);
//
//      if (windowWidth > pGraphicsMac->Width())
//      {
//        hp.x -= (int) floor((windowWidth - pGraphicsMac->Width()) / 2.);
//      }

      // Title bar Y offset
      hp.y -= bounds.bottom - bounds.top;

      int x = (int) hp.x;
      int y = (int) hp.y;

      #else // NOT RTAS
      HIPointConvert(&hp, kHICoordSpaceWindow, _this->mWindow, kHICoordSpaceView, _this->mView);
      int x = (int) hp.x - 2;
      int y = (int) hp.y - 3;
      #endif

      UInt32 mods;
      GetEventParameter(pEvent, kEventParamKeyModifiers, typeUInt32, 0, sizeof(UInt32), 0, &mods);
      EventMouseButton button;
      GetEventParameter(pEvent, kEventParamMouseButton, typeMouseButton, 0, sizeof(EventMouseButton), 0, &button);
      if (button == kEventMouseButtonPrimary && (mods & cmdKey)) button = kEventMouseButtonSecondary;
      IMouseMod mmod(true, button == kEventMouseButtonSecondary, (mods & shiftKey), (mods & controlKey), (mods & optionKey));

      switch (eventKind)
      {
        case kEventMouseDown:
        {
          if (_this->mTextEntryView)
          {
            #if !(USE_MLTE)
            HIViewRef view;
            HIViewGetViewForMouseEvent(_this->mView, pEvent, &view);
            if (view == _this->mTextEntryView) break;
            #endif
            _this->EndUserInput(true);
          }

          #ifdef RTAS_API // RTAS triple click
          if (mmod.L && mmod.R && mmod.C && (pGraphicsMac->GetParamIdxForPTAutomation(x, y) > -1))
          {
            return CallNextEventHandler(pHandlerCall, pEvent);
          }
          #endif

          CallNextEventHandler(pHandlerCall, pEvent);

          UInt32 clickCount = 0;
          GetEventParameter(pEvent, kEventParamClickCount, typeUInt32, 0, sizeof(UInt32), 0, &clickCount);

          if (clickCount > 1)
          {
            pGraphicsMac->OnMouseDblClick(x, y, &mmod);
          }
          else
          {
            pGraphicsMac->OnMouseDown(x, y, &mmod);
          }

          return noErr;
        }

        case kEventMouseUp:
        {
          pGraphicsMac->OnMouseUp(x, y, &mmod);
          return noErr;
        }

        case kEventMouseMoved:
        {
          _this->mPrevX = x;
          _this->mPrevY = y;
          pGraphicsMac->OnMouseOver(x, y, &mmod);
          return noErr;
        }

        case kEventMouseDragged:
        {
          if (!_this->mTextEntryView)
            pGraphicsMac->OnMouseDrag(x, y, &mmod);
          return noErr;
        }

        case kEventMouseWheelMoved:
        {
          EventMouseWheelAxis axis;
          GetEventParameter(pEvent, kEventParamMouseWheelAxis, typeMouseWheelAxis, 0, sizeof(EventMouseWheelAxis), 0, &axis);

          if (axis == kEventMouseWheelAxisY)
          {
            int d;
            GetEventParameter(pEvent, kEventParamMouseWheelDelta, typeSInt32, 0, sizeof(SInt32), 0, &d);

            if (_this->mTextEntryView) _this->EndUserInput(false);

            pGraphicsMac->OnMouseWheel(x, y, &mmod, d);
            return noErr;
          }
        }
      }

      break;
    }

    case kEventClassWindow:
    {
      WindowRef window;

      if (GetEventParameter(pEvent, kEventParamDirectObject, typeWindowRef, NULL, sizeof (WindowRef), NULL, &window) != noErr)
        break;

      switch (eventKind)
      {
        case kEventWindowDeactivated:
        {
          if (_this->mTextEntryView)
            _this->EndUserInput(false);
          break;
        }
      }
      break;
    }
  }

  return eventNotHandledErr;
}