PresShell::HandleEvent(nsIView         *aView, nsGUIEvent*     aEvent, nsEventStatus*  aEventStatus, PRBool          aForceHandle, PRBool&         aHandled) { void*     clientData; nsIFrame* frame; nsresult  rv = NS_OK;  NS_ASSERTION(!(nsnull == aView), "null view");
void HandleEvent() { nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell); if (presShell) { #ifdef DEBUG if (VERIFY_REFLOW_NOISY_RC & gVerifyReflowFlags) { printf("\n*** Handling reflow event: PresShell=%p, event=%p\n", (void*)presShell.get(), (void*)this); } #endif    PresShell* ps = NS_REINTERPRET_CAST(PresShell*, presShell.get()); PRBool isBatching; ps->ClearReflowEventStatus(); ps->GetReflowBatchingStatus(&isBatching); if (!isBatching) { nsCOMPtr<nsIViewManager> viewManager;   presShell->GetViewManager(getter_AddRefs(viewManager)); ps->ProcessReflowCommands(PR_TRUE);
PresShell::HandleEvent(nsIView         *aView,                       nsGUIEvent*     aEvent,                       nsEventStatus*  aEventStatus,											 PRBool          aForceHandle,                       PRBool&         aHandled){  void*     clientData;  nsIFrame* frame;  nsresult  rv = NS_OK;    NS_ASSERTION(!(nsnull == aView), "null view");  aHandled = PR_TRUE;  if (mIsDestroying || mIsReflowing) {    return NS_OK;  }#ifdef ACCESSIBILITY  if (aEvent->eventStructType == NS_ACCESSIBLE_EVENT)    return HandleEventInternal(aEvent, aView, NS_EVENT_FLAG_INIT, aEventStatus);#endif  // Check for a theme change up front, since the frame type is irrelevant  if (aEvent->message == NS_THEMECHANGED && mPresContext)    return mPresContext->ThemeChanged();  // Check for a system color change up front, since the frame type is irrelevenat  if ((aEvent->message == NS_SYSCOLORCHANGED) && mPresContext) {    nsIViewManager *vm;    if ((NS_SUCCEEDED(GetViewManager(&vm))) && vm) {      // Only dispatch system color change when the message originates from      // from the root views widget. This is necessary to prevent us from       // dispatching the SysColorChanged notification for each child window       // which may be redundant.      nsIView *view;      vm->GetRootView(view);      if (view == aView) {        aHandled = PR_TRUE;        *aEventStatus = nsEventStatus_eConsumeDoDefault;        return mPresContext->SysColorChanged();      }    }    return NS_OK;  }  // We really don't want to just drop a focus event on the floor here,  // because the widget-level focus change will have already happened  // by the time we get the event.  Rather than dropping it, manually  // update the focus controller here.  if (aEvent->message == NS_GOTFOCUS && !mDidInitialReflow && mDocument) {    nsCOMPtr<nsIScriptGlobalObject> sgo;    mDocument->GetScriptGlobalObject(getter_AddRefs(sgo));    if (sgo) {      nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(sgo);      if (pWindow) {        nsCOMPtr<nsIFocusController> controller;        pWindow->GetRootFocusController(getter_AddRefs(controller));        if (controller) {          nsCOMPtr<nsIDOMWindowInternal> domWin = do_QueryInterface(sgo);          controller->SetFocusedWindow(domWin);          controller->SetFocusedElement(nsnull);        }      }    }  }  aView->GetClientData(clientData);  frame = (nsIFrame *)clientData;/*  if (mSelection && aEvent->eventStructType == NS_KEY_EVENT)  {//KEY HANDLERS WILL GET RID OF THIS     if (mSelectionFlags && NS_SUCCEEDED(mSelection->HandleKeyEvent(mPresContext, aEvent)))    {        return NS_OK;    }  }*/  if (nsnull != frame) {    PushCurrentEventInfo(nsnull, nsnull);    nsIEventStateManager *manager;    if (NS_OK == mPresContext->GetEventStateManager(&manager)) {      //change 6-01-00 mjudge,ftang adding ime as an event that needs focused element      if (NS_IS_KEY_EVENT(aEvent) || NS_IS_IME_EVENT(aEvent) || aEvent->message == NS_CONTEXTMENU_KEY) {         //Key events go to the focused frame, not point based.        manager->GetFocusedContent(&mCurrentEventContent);        if (mCurrentEventContent)          GetPrimaryFrameFor(mCurrentEventContent, &mCurrentEventFrame);        else {#if defined(MOZ_X11)          if (NS_IS_IME_EVENT(aEvent)) {            // bug 52416            // Lookup region (candidate window) of UNIX IME grabs            // input focus from Mozilla but wants to send IME event            // to redraw pre-edit (composed) string            // If Mozilla does not have input focus and event is IME,            // sends IME event to pre-focused element            nsCOMPtr<nsIScriptGlobalObject> ourGlobal;            mDocument->GetScriptGlobalObject(getter_AddRefs(ourGlobal));            nsCOMPtr<nsPIDOMWindow> ourWindow = do_QueryInterface(ourGlobal);            if (ourWindow) {              nsCOMPtr<nsIFocusController> focusController;              ourWindow->GetRootFocusController(getter_AddRefs(focusController));              if (focusController) {                PRBool active = PR_FALSE;                // check input focus is in Mozilla                focusController->GetActive(&active);                if (!active) {                  // if not, search for pre-focused element                  nsCOMPtr<nsIDOMElement> focusedElement;                  focusController->GetFocusedElement(getter_AddRefs(focusedElement));                  if (focusedElement) {                    // get mCurrentEventContent from focusedElement                    CallQueryInterface(focusedElement, &mCurrentEventContent);                  }                }              }            }          }          if (!mCurrentEventContent) {            // fallback, call existing codes            mDocument->GetRootContent(&mCurrentEventContent);          }#else /* defined(MOZ_X11) */          // XXX This is the way key events seem to work?  Why?????          // They spend time doing calls to GetFrameForPoint with the          // point as (0,0) (or sometimes something else).          // XXX If this code is really going to stay, it should          // probably go into a separate function, because its just          // a duplicate of the code a few lines below:          // This is because we want to give the point in the same          // coordinates as the frame's own Rect, so mRect.Contains(aPoint)          // works.  However, this point is relative to the frame's rect, so          // we need to add on the origin of the rect./*          nsPoint eventPoint;          frame->GetOrigin(eventPoint);          eventPoint += aEvent->point;          rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_FOREGROUND, &mCurrentEventFrame);          if (rv != NS_OK) {            rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_FLOATERS, &mCurrentEventFrame);            if (rv != NS_OK) {              rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_BACKGROUND, &mCurrentEventFrame);              if (rv != NS_OK) {#ifdef XP_MAC*/                // On the Mac it is possible to be running with no windows open, only the native menu bar.                // In this situation, we need to handle key board events but there are no frames, so                // we set mCurrentEventContent and that works itself out in HandleEventInternal.                mDocument->GetRootContent(&mCurrentEventContent);#endif /* defined(MOZ_X11) */                mCurrentEventFrame = nsnull;/*#else								if (aForceHandle) {									mCurrentEventFrame = frame;								}								else {									mCurrentEventFrame = nsnull;								}                aHandled = PR_FALSE;#endif                rv = NS_OK;              }            }          }          */        }      }      else if (!InClipRect(frame, aEvent->point)) {        // we only check for the clip rect on this frame ... all frames with clip have views so any        // viewless children of this frame cannot have clip. Furthermore if the event is not in the clip        // for this frame, then none of the children can get it either.        if (aForceHandle) {          mCurrentEventFrame = frame;        }        else {          mCurrentEventFrame = nsnull;        }        aHandled = PR_FALSE;        rv = NS_OK;      } else {        // aEvent->point is relative to aView's upper left corner. We need        // a point that is in the same coordinate system as frame's rect        // so that the frame->mRect.Contains(aPoint) calls in         // GetFrameForPoint() work. The assumption here is that frame->GetView()        // will return aView, and frame's parent view is aView's parent.        nsPoint eventPoint;        frame->GetOrigin(eventPoint);        eventPoint += aEvent->point;        nsPoint originOffset;        nsIView *view = nsnull;        frame->GetOriginToViewOffset(mPresContext, originOffset, &view);#ifdef DEBUG_kin        NS_ASSERTION(view == aView, "view != aView");#endif // DEBUG_kin        if (view == aView)          eventPoint -= originOffset;        rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_FOREGROUND, &mCurrentEventFrame);        if (rv != NS_OK) {          rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_FLOATERS, &mCurrentEventFrame);          if (rv != NS_OK) {            rv = frame->GetFrameForPoint(mPresContext, eventPoint, NS_FRAME_PAINT_LAYER_BACKGROUND, &mCurrentEventFrame);            if (rv != NS_OK) {							if (aForceHandle) {								mCurrentEventFrame = frame;							}							else {								mCurrentEventFrame = nsnull;							}              aHandled = PR_FALSE;              rv = NS_OK;            }          }        }      }      if (GetCurrentEventFrame()) {        rv = HandleEventInternal(aEvent, aView, NS_EVENT_FLAG_INIT, aEventStatus);      }      NS_RELEASE(manager);    }#ifdef NS_DEBUG    if ((nsIFrameDebug::GetShowEventTargetFrameBorder()) && (GetCurrentEventFrame())) {      nsIView *oldView = mCurrentTargetView;      nsPoint offset(0,0);      nsRect oldTargetRect(mCurrentTargetRect);      mCurrentEventFrame->GetRect(mCurrentTargetRect);      mCurrentEventFrame->GetView(mPresContext, &mCurrentTargetView);      if ( ! mCurrentTargetView ) {        mCurrentEventFrame->GetOffsetFromView(mPresContext, offset, &mCurrentTargetView);      }      if (mCurrentTargetView) {        mCurrentTargetRect.x = offset.x;        mCurrentTargetRect.y = offset.y;        // use aView or mCurrentTargetView??        if ( (mCurrentTargetRect != oldTargetRect) || (mCurrentTargetView != oldView)) {          nsIViewManager *vm;          if ((NS_OK == GetViewManager(&vm)) && vm) {            vm->UpdateView(mCurrentTargetView,mCurrentTargetRect,0);            if (oldView)              vm->UpdateView(oldView,oldTargetRect,0);            NS_IF_RELEASE(vm);          }        }      }    }#endif    PopCurrentEventInfo();  }  else {    aHandled = PR_FALSE;    rv = NS_OK;  }  return rv;}