Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Member Functions | Protected Attributes
nsTextEditorDragListener Class Reference

editor Implementation of the DragListener interface More...

#include <nsEditorEventListeners.h>

Inheritance diagram for nsTextEditorDragListener:
Inheritance graph
[legend]
Collaboration diagram for nsTextEditorDragListener:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsTextEditorDragListener ()
 default constructor
virtual ~nsTextEditorDragListener ()
 default destructor
void SetEditor (nsIEditor *aEditor)
 SetEditor gives an address to the editor that will be accessed.
void SetPresShell (nsIPresShell *aPresShell)
NS_DECL_ISUPPORTS NS_IMETHOD HandleEvent (nsIDOMEvent *aEvent)
NS_IMETHOD DragEnter (nsIDOMEvent *aDragEvent)
 Processes a drag enter event.
NS_IMETHOD DragOver (nsIDOMEvent *aDragEvent)
 Processes a drag over event.
NS_IMETHOD DragExit (nsIDOMEvent *aDragEvent)
 Processes a drag Exit event.
NS_IMETHOD DragDrop (nsIDOMEvent *aDragEvent)
 Processes a drag drop event.
NS_IMETHOD DragGesture (nsIDOMEvent *aDragEvent)
 Processes a drag gesture event.
void handleEvent (in nsIDOMEvent event)
 This method is called whenever an event occurs of the type for which the EventListener interface was registered.

Protected Member Functions

PRBool CanDrop (nsIDOMEvent *aEvent)

Protected Attributes

nsIEditormEditor
nsWeakPtr mPresShell
nsCOMPtr< nsICaretmCaret
PRBool mCaretDrawn

Detailed Description

editor Implementation of the DragListener interface

Definition at line 198 of file nsEditorEventListeners.h.


Constructor & Destructor Documentation

default constructor

Definition at line 473 of file nsEditorEventListeners.cpp.

default destructor

Definition at line 480 of file nsEditorEventListeners.cpp.

{
}

Member Function Documentation

Definition at line 654 of file nsEditorEventListeners.cpp.

{
  // if the target doc is read-only, we can't drop
  PRUint32 flags;
  if (NS_FAILED(mEditor->GetFlags(&flags)))
    return PR_FALSE;

  if ((flags & nsIPlaintextEditor::eEditorDisabledMask) || 
      (flags & nsIPlaintextEditor::eEditorReadonlyMask)) {
    return PR_FALSE;
  }

  // XXX cache this between drag events?
  nsresult rv;
  nsCOMPtr<nsIDragService> dragService = do_GetService("@mozilla.org/widget/dragservice;1", &rv);

  // does the drag have flavors we can accept?
  nsCOMPtr<nsIDragSession> dragSession;
  if (dragService)
    dragService->GetCurrentSession(getter_AddRefs(dragSession));
  if (!dragSession) return PR_FALSE;

  PRBool flavorSupported = PR_FALSE;
  dragSession->IsDataFlavorSupported(kUnicodeMime, &flavorSupported);

  // if we aren't plaintext editing, we can accept more flavors
  if (!flavorSupported 
     && (flags & nsIPlaintextEditor::eEditorPlaintextMask) == 0)
  {
    dragSession->IsDataFlavorSupported(kHTMLMime, &flavorSupported);
    if (!flavorSupported)
      dragSession->IsDataFlavorSupported(kFileMime, &flavorSupported);
#if 0
    if (!flavorSupported)
      dragSession->IsDataFlavorSupported(kJPEGImageMime, &flavorSupported);
#endif
  }

  if (!flavorSupported)
    return PR_FALSE;     

  nsCOMPtr<nsIDOMDocument> domdoc;
  rv = mEditor->GetDocument(getter_AddRefs(domdoc));
  if (NS_FAILED(rv)) return PR_FALSE;

  nsCOMPtr<nsIDOMDocument> sourceDoc;
  rv = dragSession->GetSourceDocument(getter_AddRefs(sourceDoc));
  if (NS_FAILED(rv)) return PR_FALSE;
  if (domdoc == sourceDoc)      // source and dest are the same document; disallow drops within the selection
  {
    nsCOMPtr<nsISelection> selection;
    rv = mEditor->GetSelection(getter_AddRefs(selection));
    if (NS_FAILED(rv) || !selection)
      return PR_FALSE;
    
    PRBool isCollapsed;
    rv = selection->GetIsCollapsed(&isCollapsed);
    if (NS_FAILED(rv)) return PR_FALSE;
  
    // Don't bother if collapsed - can always drop
    if (!isCollapsed)
    {
      nsCOMPtr<nsIDOMNSUIEvent> nsuiEvent (do_QueryInterface(aEvent));
      if (!nsuiEvent) return PR_FALSE;

      nsCOMPtr<nsIDOMNode> parent;
      rv = nsuiEvent->GetRangeParent(getter_AddRefs(parent));
      if (NS_FAILED(rv) || !parent) return PR_FALSE;

      PRInt32 offset = 0;
      rv = nsuiEvent->GetRangeOffset(&offset);
      if (NS_FAILED(rv)) return PR_FALSE;

      PRInt32 rangeCount;
      rv = selection->GetRangeCount(&rangeCount);
      if (NS_FAILED(rv)) return PR_FALSE;

      for (PRInt32 i = 0; i < rangeCount; i++)
      {
        nsCOMPtr<nsIDOMRange> range;
        rv = selection->GetRangeAt(i, getter_AddRefs(range));
        nsCOMPtr<nsIDOMNSRange> nsrange(do_QueryInterface(range));
        if (NS_FAILED(rv) || !nsrange) 
          continue; //don't bail yet, iterate through them all

        PRBool inRange = PR_TRUE;
        (void)nsrange->IsPointInRange(parent, offset, &inRange);
        if (inRange)
          return PR_FALSE;  //okay, now you can bail, we are over the orginal selection
      }
    }
  }
  
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Processes a drag drop event.

Parameters:
aMouseEvent
See also:
nsIDOMEvent.h
Returns:
whether the event was consumed or ignored.
See also:
nsresult

Implements nsIDOMDragListener.

Definition at line 613 of file nsEditorEventListeners.cpp.

{
  if (mCaret)
  {
    if (mCaretDrawn)
    {
      mCaret->EraseCaret();
      mCaretDrawn = PR_FALSE;
    }
    mCaret->SetCaretVisible(PR_FALSE);    // hide it, so that it turns off its timer
    mCaret = nsnull;      // release it
  }

  if (!mEditor)
    return NS_ERROR_FAILURE;

  PRBool canDrop = CanDrop(aMouseEvent);
  if (!canDrop)
  {
    // was it because we're read-only?

    PRUint32 flags;
    if (NS_SUCCEEDED(mEditor->GetFlags(&flags))
        && ((flags & nsIPlaintextEditor::eEditorDisabledMask) ||
            (flags & nsIPlaintextEditor::eEditorReadonlyMask)) )
    {
      // it was decided to "eat" the event as this is the "least surprise"
      // since someone else handling it might be unintentional and the 
      // user could probably re-drag to be not over the disabled/readonly 
      // editfields if that is what is desired.
      return aMouseEvent->StopPropagation();
    }
    return NS_OK;
  }

  aMouseEvent->StopPropagation();
  aMouseEvent->PreventDefault();
  return mEditor->InsertFromDrop(aMouseEvent);
}

Here is the call graph for this function:

Processes a drag enter event.

Parameters:
aMouseEvent
See also:
nsIDOMEvent.h
Returns:
whether the event was consumed or ignored.
See also:
nsresult

Implements nsIDOMDragListener.

Definition at line 510 of file nsEditorEventListeners.cpp.

{
  if (!mCaret)
  {
    nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShell);
    if (presShell)
    {
      mCaret = do_CreateInstance("@mozilla.org/layout/caret;1");
      if (mCaret)
      {
        mCaret->Init(presShell);
        mCaret->SetCaretReadOnly(PR_TRUE);
      }
      mCaretDrawn = PR_FALSE;
    }
  }
  
  return DragOver(aDragEvent);
}

Here is the call graph for this function:

Processes a drag Exit event.

Parameters:
aMouseEvent
See also:
nsIDOMEvent.h
Returns:
whether the event was consumed or ignored.
See also:
nsresult

Implements nsIDOMDragListener.

Definition at line 599 of file nsEditorEventListeners.cpp.

{
  if (mCaret && mCaretDrawn)
  {
    mCaret->EraseCaret();
    mCaretDrawn = PR_FALSE;
  }

  return NS_OK;
}

Processes a drag gesture event.

Parameters:
aMouseEvent
See also:
nsIDOMEvent.h
Returns:
whether the event was consumed or ignored.
See also:
nsresult

Implements nsIDOMDragListener.

Definition at line 494 of file nsEditorEventListeners.cpp.

{
  if ( !mEditor )
    return NS_ERROR_NULL_POINTER;
  
  // ...figure out if a drag should be started...
  PRBool canDrag;
  nsresult rv = mEditor->CanDrag(aDragEvent, &canDrag);
  if ( NS_SUCCEEDED(rv) && canDrag )
    rv = mEditor->DoDrag(aDragEvent);

  return rv;
}

Processes a drag over event.

Parameters:
aMouseEvent
See also:
nsIDOMEvent.h
Returns:
whether the event was consumed or ignored.
See also:
nsresult

Implements nsIDOMDragListener.

Definition at line 532 of file nsEditorEventListeners.cpp.

{
  // XXX cache this between drag events?
  nsresult rv;
  nsCOMPtr<nsIDragService> dragService = do_GetService("@mozilla.org/widget/dragservice;1", &rv);
  if (!dragService) return rv;

  // does the drag have flavors we can accept?
  nsCOMPtr<nsIDragSession> dragSession;
  dragService->GetCurrentSession(getter_AddRefs(dragSession));
  if (!dragSession) return NS_ERROR_FAILURE;

  PRBool canDrop = CanDrop(aDragEvent);
  if (canDrop)
  {
    nsCOMPtr<nsIDOMDocument> domdoc;
    mEditor->GetDocument(getter_AddRefs(domdoc));
    canDrop = nsEditorHookUtils::DoAllowDropHook(domdoc, aDragEvent, dragSession);
  }

  dragSession->SetCanDrop(canDrop);

  // We need to consume the event to prevent the browser's
  // default drag listeners from being fired. (Bug 199133)

  aDragEvent->PreventDefault(); // consumed
    
  if (canDrop)
  {
    if (mCaret)
    {
      nsCOMPtr<nsIDOMNSUIEvent> nsuiEvent (do_QueryInterface(aDragEvent));
      if (nsuiEvent)
      {
        nsCOMPtr<nsIDOMNode> parent;
        rv = nsuiEvent->GetRangeParent(getter_AddRefs(parent));
        if (NS_FAILED(rv)) return rv;
        if (!parent) return NS_ERROR_FAILURE;

        PRInt32 offset = 0;
        rv = nsuiEvent->GetRangeOffset(&offset);
        if (NS_FAILED(rv)) return rv;

        // to avoid flicker, we could track the node and offset to see if we moved
        if (mCaretDrawn)
          mCaret->EraseCaret();
        
        //mCaret->SetCaretVisible(PR_TRUE);   // make sure it's visible
        mCaret->DrawAtPosition(parent, offset);
        mCaretDrawn = PR_TRUE;
      }
    }
  }
  else
  {
    if (mCaret && mCaretDrawn)
    {
      mCaret->EraseCaret();
      mCaretDrawn = PR_FALSE;
    } 
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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

Parameters:
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 487 of file nsEditorEventListeners.cpp.

{
  return NS_OK;
}

SetEditor gives an address to the editor that will be accessed.

Parameters:
aEditorthe editor this listener calls for editing operations

Definition at line 211 of file nsEditorEventListeners.h.

{ mEditor = aEditor; }

Here is the caller graph for this function:

Definition at line 212 of file nsEditorEventListeners.h.

                                              {
    mPresShell = do_GetWeakReference(aPresShell);
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 237 of file nsEditorEventListeners.h.

Definition at line 238 of file nsEditorEventListeners.h.

Definition at line 234 of file nsEditorEventListeners.h.

Definition at line 235 of file nsEditorEventListeners.h.


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