NS_IMETHODIMP nsTextControlFrame::GetSelectionRange(PRInt32* aSelectionStart, PRInt32* aSelectionEnd) { // make sure we have an editor nsresult rv = EnsureEditorInitialized(); NS_ENSURE_SUCCESS(rv, rv); *aSelectionStart = 0; *aSelectionEnd = 0; nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(GetContent()); NS_ASSERTION(txtCtrl, "Content not a text control element"); nsISelectionController* selCon = txtCtrl->GetSelectionController(); NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE); nsCOMPtr<nsISelection> selection; rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE); PRInt32 numRanges = 0; selection->GetRangeCount(&numRanges); if (numRanges < 1) return NS_OK; // We only operate on the first range in the selection! nsCOMPtr<nsIDOMRange> firstRange; rv = selection->GetRangeAt(0, getter_AddRefs(firstRange)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(firstRange, NS_ERROR_FAILURE); nsCOMPtr<nsIDOMNode> startNode, endNode; PRInt32 startOffset = 0, endOffset = 0; // Get the start point of the range. rv = firstRange->GetStartContainer(getter_AddRefs(startNode)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE); rv = firstRange->GetStartOffset(&startOffset); NS_ENSURE_SUCCESS(rv, rv); // Get the end point of the range. rv = firstRange->GetEndContainer(getter_AddRefs(endNode)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(endNode, NS_ERROR_FAILURE); rv = firstRange->GetEndOffset(&endOffset); NS_ENSURE_SUCCESS(rv, rv); // Convert the start point to a selection offset. rv = DOMPointToOffset(startNode, startOffset, aSelectionStart); NS_ENSURE_SUCCESS(rv, rv); // Convert the end point to a selection offset. return DOMPointToOffset(endNode, endOffset, aSelectionEnd); }
NS_IMETHODIMP nsTextControlFrame::GetSelectionRange(PRInt32* aSelectionStart, PRInt32* aSelectionEnd, SelectionDirection* aDirection) { // make sure we have an editor nsresult rv = EnsureEditorInitialized(); NS_ENSURE_SUCCESS(rv, rv); if (aSelectionStart) { *aSelectionStart = 0; } if (aSelectionEnd) { *aSelectionEnd = 0; } if (aDirection) { *aDirection = eNone; } nsCOMPtr<nsITextControlElement> txtCtrl = do_QueryInterface(GetContent()); NS_ASSERTION(txtCtrl, "Content not a text control element"); nsISelectionController* selCon = txtCtrl->GetSelectionController(); NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE); nsCOMPtr<nsISelection> selection; rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE); PRInt32 numRanges = 0; selection->GetRangeCount(&numRanges); if (numRanges < 1) return NS_OK; // We only operate on the first range in the selection! if (aDirection) { nsCOMPtr<nsISelectionPrivate> selPriv = do_QueryInterface(selection); if (selPriv) { nsDirection direction = selPriv->GetSelectionDirection(); if (direction == eDirNext) { *aDirection = eForward; } else if (direction == eDirPrevious) { *aDirection = eBackward; } else { NS_NOTREACHED("Invalid nsDirection enum value"); } } } if (!aSelectionStart || !aSelectionEnd) { return NS_OK; } nsCOMPtr<nsIDOMRange> firstRange; rv = selection->GetRangeAt(0, getter_AddRefs(firstRange)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(firstRange, NS_ERROR_FAILURE); nsCOMPtr<nsIDOMNode> startNode, endNode; PRInt32 startOffset = 0, endOffset = 0; // Get the start point of the range. rv = firstRange->GetStartContainer(getter_AddRefs(startNode)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE); rv = firstRange->GetStartOffset(&startOffset); NS_ENSURE_SUCCESS(rv, rv); // Get the end point of the range. rv = firstRange->GetEndContainer(getter_AddRefs(endNode)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(endNode, NS_ERROR_FAILURE); rv = firstRange->GetEndOffset(&endOffset); NS_ENSURE_SUCCESS(rv, rv); // Convert the start point to a selection offset. rv = DOMPointToOffset(startNode, startOffset, aSelectionStart); NS_ENSURE_SUCCESS(rv, rv); // Convert the end point to a selection offset. return DOMPointToOffset(endNode, endOffset, aSelectionEnd); }