Back to index

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

The nsTextEditorKeyListener public nsIDOMKeyListener This class will delegate events to its editor according to the translation it is responsible for. More...

#include <nsEditorEventListeners.h>

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

List of all members.

Public Member Functions

 nsTextEditorKeyListener ()
 the default constructor
virtual ~nsTextEditorKeyListener ()
 the default destructor.
void SetEditor (nsIEditor *aEditor)
 SetEditor gives an address to the editor that will be accessed.
NS_DECL_ISUPPORTS NS_IMETHOD HandleEvent (nsIDOMEvent *aEvent)
NS_IMETHOD KeyDown (nsIDOMEvent *aKeyEvent)
 Processes a key pressed event.
NS_IMETHOD KeyUp (nsIDOMEvent *aKeyEvent)
 Processes a key release event.
NS_IMETHOD KeyPress (nsIDOMEvent *aKeyEvent)
 Processes a key typed 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 Attributes

nsIEditormEditor

Detailed Description

The nsTextEditorKeyListener public nsIDOMKeyListener This class will delegate events to its editor according to the translation it is responsible for.

i.e. 'c' becomes a keydown, but 'ESC' becomes nothing.

Definition at line 63 of file nsEditorEventListeners.h.


Constructor & Destructor Documentation

the default constructor

Definition at line 86 of file nsEditorEventListeners.cpp.

{
}

the default destructor.

virtual due to the possibility of derivation.

Definition at line 92 of file nsEditorEventListeners.cpp.

{
}

Member Function Documentation

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 98 of file nsEditorEventListeners.cpp.

{
  return NS_OK;
}

Processes a key pressed event.

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

Implements nsIDOMKeyListener.

Definition at line 107 of file nsEditorEventListeners.cpp.

{
  return NS_OK;
}

Processes a key typed event.

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

Implements nsIDOMKeyListener.

Definition at line 121 of file nsEditorEventListeners.cpp.

{
  // DOM event handling happens in two passes, the client pass and the system
  // pass.  We do all of our processing in the system pass, to allow client
  // handlers the opportunity to cancel events and prevent typing in the editor.
  // If the client pass cancelled the event, defaultPrevented will be true
  // below.

  nsCOMPtr<nsIDOMNSUIEvent> nsUIEvent = do_QueryInterface(aKeyEvent);
  if(nsUIEvent) 
  {
    PRBool defaultPrevented;
    nsUIEvent->GetPreventDefault(&defaultPrevented);
    if(defaultPrevented)
      return NS_OK;
  }

  nsCOMPtr<nsIDOMKeyEvent>keyEvent = do_QueryInterface(aKeyEvent);
  if (!keyEvent) 
  {
    //non-key event passed to keypress.  bad things.
    return NS_OK;
  }

  // we should check a flag here to see if we should be using built-in key bindings
  // mEditor->GetFlags(&flags);
  // if (flags & ...)

  PRUint32 keyCode;
  keyEvent->GetKeyCode(&keyCode);

  // if we are readonly or disabled, then do nothing.
  PRUint32 flags;
  if (NS_SUCCEEDED(mEditor->GetFlags(&flags)))
  {
    if (flags & nsIPlaintextEditor::eEditorReadonlyMask || 
        flags & nsIPlaintextEditor::eEditorDisabledMask) 
      return NS_OK;
  }
  else
    return NS_ERROR_FAILURE;  // Editor unable to handle this.

  nsCOMPtr<nsIPlaintextEditor> textEditor (do_QueryInterface(mEditor));
  if (!textEditor) return NS_ERROR_NO_INTERFACE;

  // if there is no charCode, then it's a key that doesn't map to a character,
  // so look for special keys using keyCode.
  if (0 != keyCode)
  {
    PRBool isAnyModifierKeyButShift;
    nsresult rv;
    rv = keyEvent->GetAltKey(&isAnyModifierKeyButShift);
    if (NS_FAILED(rv)) return rv;
    
    if (!isAnyModifierKeyButShift)
    {
      rv = keyEvent->GetMetaKey(&isAnyModifierKeyButShift);
      if (NS_FAILED(rv)) return rv;
      
      if (!isAnyModifierKeyButShift)
      {
        rv = keyEvent->GetCtrlKey(&isAnyModifierKeyButShift);
        if (NS_FAILED(rv)) return rv;
      }
    }

    switch (keyCode)
    {
      case nsIDOMKeyEvent::DOM_VK_META:
      case nsIDOMKeyEvent::DOM_VK_SHIFT:
      case nsIDOMKeyEvent::DOM_VK_CONTROL:
      case nsIDOMKeyEvent::DOM_VK_ALT:
        aKeyEvent->PreventDefault(); // consumed
        return NS_OK;
        break;

      case nsIDOMKeyEvent::DOM_VK_BACK_SPACE: 
        if (isAnyModifierKeyButShift)
          return NS_OK;

        mEditor->DeleteSelection(nsIEditor::ePrevious);
        aKeyEvent->PreventDefault(); // consumed
        return NS_OK;
        break;
 
      case nsIDOMKeyEvent::DOM_VK_DELETE:
        /* on certain platforms (such as windows) the shift key
           modifies what delete does (cmd_cut in this case).
           bailing here to allow the keybindings to do the cut.*/
        PRBool isShiftModifierKey;
        rv = keyEvent->GetShiftKey(&isShiftModifierKey);
        if (NS_FAILED(rv)) return rv;

        if (isAnyModifierKeyButShift || isShiftModifierKey)
           return NS_OK;
        mEditor->DeleteSelection(nsIEditor::eNext);
        aKeyEvent->PreventDefault(); // consumed
        return NS_OK; 
        break;
 
      case nsIDOMKeyEvent::DOM_VK_TAB:
        if ((flags & nsIPlaintextEditor::eEditorSingleLineMask) ||
            (flags & nsIPlaintextEditor::eEditorPasswordMask)   ||
            (flags & nsIPlaintextEditor::eEditorWidgetMask))
          return NS_OK; // let it be used for focus switching

        if (isAnyModifierKeyButShift)
          return NS_OK;

        // else we insert the tab straight through
        textEditor->HandleKeyPress(keyEvent);
        // let HandleKeyPress consume the event
        return NS_OK; 

      case nsIDOMKeyEvent::DOM_VK_RETURN:
      case nsIDOMKeyEvent::DOM_VK_ENTER:
        if (isAnyModifierKeyButShift)
          return NS_OK;

        if (!(flags & nsIPlaintextEditor::eEditorSingleLineMask))
        {
          textEditor->HandleKeyPress(keyEvent);
          aKeyEvent->PreventDefault(); // consumed
        }
        return NS_OK;
    }
  }

  textEditor->HandleKeyPress(keyEvent);
  return NS_OK; // we don't PreventDefault() here or keybindings like control-x won't work 
}

Here is the call graph for this function:

Processes a key release event.

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

Implements nsIDOMKeyListener.

Definition at line 114 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 75 of file nsEditorEventListeners.h.

{mEditor = aEditor;}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 90 of file nsEditorEventListeners.h.


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