/**
 * This is called when we receive any registered events on the control.
 * We've only registered for drop, dragover and click events.
 */
NS_IMETHODIMP
nsFileControlFrame::BrowseMouseListener::HandleEvent(nsIDOMEvent* aEvent)
{
  NS_ASSERTION(mFrame, "We should have been unregistered");

  nsAutoString eventType;
  aEvent->GetType(eventType);
  if (eventType.EqualsLiteral("click")) {
    if (!ShouldProcessMouseClick(aEvent))
      return NS_OK;
    
    nsHTMLInputElement* input =
      nsHTMLInputElement::FromContent(mFrame->GetContent());
    return input ? input->FireAsyncClickHandler() : NS_OK;
  }

  bool defaultPrevented = false;
  aEvent->GetPreventDefault(&defaultPrevented);
  if (defaultPrevented) {
    return NS_OK;
  }
  
  nsCOMPtr<nsIDOMDragEvent> dragEvent = do_QueryInterface(aEvent);
  if (!dragEvent || !IsValidDropData(dragEvent)) {
    return NS_OK;
  }

  if (eventType.EqualsLiteral("dragover")) {
    // Prevent default if we can accept this drag data
    aEvent->PreventDefault();
    return NS_OK;
  }

  if (eventType.EqualsLiteral("drop")) {
    aEvent->StopPropagation();
    aEvent->PreventDefault();

    nsIContent* content = mFrame->GetContent();
    NS_ASSERTION(content, "The frame has no content???");

    nsHTMLInputElement* inputElement = nsHTMLInputElement::FromContent(content);
    NS_ASSERTION(inputElement, "No input element for this file upload control frame!");

    nsCOMPtr<nsIDOMDataTransfer> dataTransfer;
    dragEvent->GetDataTransfer(getter_AddRefs(dataTransfer));

    nsCOMPtr<nsIDOMFileList> fileList;
    dataTransfer->GetFiles(getter_AddRefs(fileList));

    
    inputElement->SetFiles(fileList, true);
    inputElement->FireChangeEventIfNeeded();
  }

  return NS_OK;
}
Beispiel #2
0
/**
 * This is called when we receive any registered events on the control.
 * We've only registered for drop, dragover and click events, and click events
 * already call MouseClick() for us. Here, we handle file drops.
 */
NS_IMETHODIMP
nsFileControlFrame::BrowseMouseListener::HandleEvent(nsIDOMEvent* aEvent)
{
    NS_ASSERTION(mFrame, "We should have been unregistered");
    nsCOMPtr<nsIDOMNSUIEvent> uiEvent = do_QueryInterface(aEvent);
    NS_ENSURE_STATE(uiEvent);
    PRBool defaultPrevented = PR_FALSE;
    uiEvent->GetPreventDefault(&defaultPrevented);
    if (defaultPrevented) {
        return NS_OK;
    }

    nsCOMPtr<nsIDOMDragEvent> dragEvent = do_QueryInterface(aEvent);
    if (!dragEvent || !IsValidDropData(dragEvent)) {
        return NS_OK;
    }

    nsAutoString eventType;
    aEvent->GetType(eventType);
    if (eventType.EqualsLiteral("dragover")) {
        // Prevent default if we can accept this drag data
        aEvent->PreventDefault();
        return NS_OK;
    }

    if (eventType.EqualsLiteral("drop")) {
        aEvent->StopPropagation();
        aEvent->PreventDefault();

        nsIContent* content = mFrame->GetContent();
        NS_ASSERTION(content, "The frame has no content???");

        nsHTMLInputElement* inputElement = nsHTMLInputElement::FromContent(content);
        NS_ASSERTION(inputElement, "No input element for this file upload control frame!");

        nsCOMPtr<nsIDOMDataTransfer> dataTransfer;
        dragEvent->GetDataTransfer(getter_AddRefs(dataTransfer));

        nsCOMPtr<nsIDOMFileList> fileList;
        dataTransfer->GetFiles(getter_AddRefs(fileList));

        PRBool oldState = mFrame->mTextFrame->GetFireChangeEventState();
        mFrame->mTextFrame->SetFireChangeEventState(PR_TRUE);
        inputElement->SetFiles(fileList, true);
        mFrame->mTextFrame->SetFireChangeEventState(oldState);
        mFrame->mTextFrame->CheckFireOnChange();
    }

    return NS_OK;
}