Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
ChromeContextMenuListener Class Reference

#include <nsDocShellTreeOwner.h>

Inheritance diagram for ChromeContextMenuListener:
Inheritance graph
Collaboration diagram for ChromeContextMenuListener:
Collaboration graph

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS ChromeContextMenuListener (nsWebBrowser *inBrowser, nsIWebBrowserChrome *inChrome)
virtual ~ChromeContextMenuListener ()
NS_IMETHOD HandleEvent (nsIDOMEvent *aEvent)
NS_IMETHOD ContextMenu (nsIDOMEvent *aEvent)
 Processes a context menu event.
NS_IMETHOD AddChromeListeners ()
NS_IMETHOD RemoveChromeListeners ()
void handleEvent (in nsIDOMEvent event)
 This method is called whenever an event occurs of the type for which the EventListener interface was registered.

Private Member Functions

NS_IMETHOD AddContextMenuListener ()
NS_IMETHOD RemoveContextMenuListener ()

Private Attributes

PRPackedBool mContextMenuListenerInstalled
nsCOMPtr< nsIDOMEventReceivermEventReceiver
nsCOMPtr< nsIWebBrowserChromemWebBrowserChrome

Detailed Description

Definition at line 272 of file nsDocShellTreeOwner.h.

Constructor & Destructor Documentation

Definition at line 1546 of file nsDocShellTreeOwner.cpp.

Here is the caller graph for this function:

Definition at line 1557 of file nsDocShellTreeOwner.cpp.

} // dtor

Member Function Documentation

Definition at line 1608 of file nsDocShellTreeOwner.cpp.

  if ( !mEventReceiver )
    GetEventReceiver(mWebBrowser, getter_AddRefs(mEventReceiver));
  // Register the appropriate events for context menus, but only if
  // the embedding chrome cares.
  nsresult rv = NS_OK;

  nsCOMPtr<nsIContextMenuListener2> contextListener2 ( do_QueryInterface(mWebBrowserChrome) );
  nsCOMPtr<nsIContextMenuListener> contextListener ( do_QueryInterface(mWebBrowserChrome) );
  if ( (contextListener || contextListener2) && !mContextMenuListenerInstalled )
    rv = AddContextMenuListener();

  return rv;
} // AddChromeListeners

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1569 of file nsDocShellTreeOwner.cpp.

Here is the caller graph for this function:

Processes a context menu event.

See also:
whether the event was consumed or ignored.
See also:

Implements nsIDOMContextMenuListener.

Definition at line 1655 of file nsDocShellTreeOwner.cpp.

  nsCOMPtr<nsIDOMNSUIEvent> uievent(do_QueryInterface(aMouseEvent));

  if (uievent) {
    PRBool isDefaultPrevented = PR_FALSE;

    if (isDefaultPrevented) {
      return NS_OK;

  nsCOMPtr<nsIDOMEventTarget> targetNode;
  nsresult res = aMouseEvent->GetTarget(getter_AddRefs(targetNode));
  if (NS_FAILED(res))
    return res;
  if (!targetNode)

  nsCOMPtr<nsIDOMNode> targetDOMnode;
  nsCOMPtr<nsIDOMNode> node = do_QueryInterface(targetNode);
  if (!node)
    return NS_OK;

  // Stop the context menu event going to other windows (bug 78396)
  // If the listener is a nsIContextMenuListener2, create the info object
  nsCOMPtr<nsIContextMenuListener2> menuListener2(do_QueryInterface(mWebBrowserChrome));
  nsContextMenuInfo *menuInfoImpl = nsnull;
  nsCOMPtr<nsIContextMenuInfo> menuInfo;
  if (menuListener2) {
    menuInfoImpl = new nsContextMenuInfo;
    if (!menuInfoImpl)
      return NS_ERROR_OUT_OF_MEMORY;
    menuInfo = menuInfoImpl; 

  // Find the first node to be an element starting with this node and
  // working up through its parents.

  PRUint32 flags = nsIContextMenuListener::CONTEXT_NONE;
  PRUint32 flags2 = nsIContextMenuListener2::CONTEXT_NONE;
  nsCOMPtr<nsIContent> content;
  do {
    // XXX test for selected text
    content = do_QueryInterface(node);
    if (content && content->IsContentOfType(nsIContent::eHTML)) {
      const char *tagStr;

      // Test what kind of element we're dealing with here
      if (strcmp(tagStr, "img") == 0)
        flags |= nsIContextMenuListener::CONTEXT_IMAGE;
        flags2 |= nsIContextMenuListener2::CONTEXT_IMAGE;
        targetDOMnode = node;
        // if we see an image, keep searching for a possible anchor
      else if (strcmp(tagStr, "input") == 0)
        // INPUT element - button, combo, checkbox, text etc.
        flags |= nsIContextMenuListener::CONTEXT_INPUT;
        flags2 |= nsIContextMenuListener2::CONTEXT_INPUT;
        targetDOMnode = node;
        // See if the input type is an image
        if (menuListener2) {
          nsCOMPtr<nsIDOMHTMLInputElement> inputElement(do_QueryInterface(node));
          if (inputElement) {
            nsAutoString inputElemType;
            if (inputElemType.LowerCaseEqualsLiteral("image"))
              flags2 |= nsIContextMenuListener2::CONTEXT_IMAGE;
        break; // exit do-while
      else if (strcmp(tagStr, "textarea") == 0)
        // text area
        flags |= nsIContextMenuListener::CONTEXT_TEXT;
        flags2 |= nsIContextMenuListener2::CONTEXT_TEXT;
        targetDOMnode = node;
        break; // exit do-while
      else if (strcmp(tagStr, "html") == 0)
        if (!flags && !flags2) { 
        // only care about this if no other context was found.
            flags |= nsIContextMenuListener::CONTEXT_DOCUMENT;
            flags2 |= nsIContextMenuListener2::CONTEXT_DOCUMENT;
            targetDOMnode = node;
        if (!(flags & nsIContextMenuListener::CONTEXT_IMAGE)) {
          // first check if this is a background image that the user was trying to click on
          // and if the listener is ready for that (only nsIContextMenuListener2 and up)
          if (menuInfoImpl && menuInfoImpl->HasBackgroundImage(node)) {
            flags2 |= nsIContextMenuListener2::CONTEXT_BACKGROUND_IMAGE;
        break; // exit do-while
      else if (strcmp(tagStr, "object") == 0 ||  /* XXX what about images and documents? */
               strcmp(tagStr, "embed") == 0 ||
               strcmp(tagStr, "applet") == 0)
      {                // always consume events for plugins and Java 
        return NS_OK;  // who may throw their own context menus

      // Test if the element has an associated link
      nsCOMPtr<nsIDOMElement> element(do_QueryInterface(node));

      PRBool hasAttr = PR_FALSE;
      res = element->HasAttribute(NS_LITERAL_STRING("href"), &hasAttr);

      if (NS_SUCCEEDED(res) && hasAttr)
        flags |= nsIContextMenuListener::CONTEXT_LINK;
        flags2 |= nsIContextMenuListener2::CONTEXT_LINK;
        if (!targetDOMnode)
          targetDOMnode = node;
        if (menuInfoImpl)
        break; // exit do-while

    // walk-up-the-tree
    nsCOMPtr<nsIDOMNode> parentNode;
    node = parentNode;
  } while (node);

  // we need to cache the event target into the focus controller's popupNode
  // so we can get at it later from command code, etc.:

  // get the dom window
  nsCOMPtr<nsIDOMWindow> win;
  res = mWebBrowser->GetContentDOMWindow(getter_AddRefs(win));
  NS_ENSURE_SUCCESS(res, res);
  // get the private dom window
  nsCOMPtr<nsPIDOMWindow> privateWin(do_QueryInterface(win, &res));
  NS_ENSURE_SUCCESS(res, res);
  // get the focus controller
  nsIFocusController *focusController = privateWin->GetRootFocusController();
  // set the focus controller's popup node to the event target
  res = focusController->SetPopupNode(targetDOMnode);
  NS_ENSURE_SUCCESS(res, res);

  // Tell the listener all about the event
  if ( menuListener2 ) {
    menuListener2->OnShowContextMenu(flags2, menuInfo);
  else {
    nsCOMPtr<nsIContextMenuListener> menuListener(do_QueryInterface(mWebBrowserChrome));
    if ( menuListener )
      menuListener->OnShowContextMenu(flags, aMouseEvent, targetDOMnode);

  return NS_OK;

} // MouseDown

Here is the call graph for this function:

This method is called whenever an event occurs of the type for which the EventListener interface was registered.

evtThe Event contains contextual information about the event. It also contains the stopPropagation and preventDefault methods which are used in determining the event's flow and default action.

Definition at line 281 of file nsDocShellTreeOwner.h.

{      return NS_OK; }

Definition at line 1633 of file nsDocShellTreeOwner.cpp.

  if ( mContextMenuListenerInstalled )
  mEventReceiver = nsnull;
  // it really doesn't matter if these fail...
  return NS_OK;
} // RemoveChromeTooltipListeners

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1588 of file nsDocShellTreeOwner.cpp.

Here is the caller graph for this function:

Member Data Documentation

Definition at line 294 of file nsDocShellTreeOwner.h.

Definition at line 297 of file nsDocShellTreeOwner.h.

Definition at line 296 of file nsDocShellTreeOwner.h.

Definition at line 298 of file nsDocShellTreeOwner.h.

The documentation for this class was generated from the following files: