PRBool batching = mBatching;
PRUint32 batching = mBatching;
nsRangeList::TakeFocus(nsIFocusTracker *aTracker, nsIFrame *aFrame, PRInt32 aOffset, PRInt32 aContentOffset, PRBool aContinueSelection){  if (!aTracker || !aFrame)    return NS_ERROR_NULL_POINTER;  if (GetBatching())    return NS_ERROR_FAILURE;  //HACKHACKHACK  nsCOMPtr<nsIContent> content;  nsCOMPtr<nsIDOMNode> domNode;  if (NS_SUCCEEDED(aFrame->GetContent(getter_AddRefs(content))) && content){    nsCOMPtr<nsIContent> parent;    nsCOMPtr<nsIContent> parent2;    if (NS_FAILED(content->GetParent(*getter_AddRefs(parent))) || !parent)      return NS_ERROR_FAILURE;    if (NS_FAILED(parent->GetParent(*getter_AddRefs(parent2))) || !parent2)      return NS_ERROR_FAILURE;    domNode = do_QueryInterface(content);    parent = nsCOMPtr<nsIContent>();//just force a release now even though we dont have to.    parent2 = nsCOMPtr<nsIContent>();    nsIFrame *frame;    nsIFrame *anchor;    PRBool direction(BACKWARD);//true == left to right    if (domNode && NS_SUCCEEDED(aTracker->GetFocus(&frame, &anchor))){      //traverse through document and unselect crap here      if (!aContinueSelection){ //single click? setting cursor down        if (anchor && frame && anchor != frame ){          //selected across frames, must "deselect" frames between in correct order          PRInt32 compareResult = compareFrames(anchor,frame);          if ( compareResult < 0 )            selectFrames(anchor,0,frame, 0, eDirNext, nsSelectionStruct::SELTOEND); //unselect all between          else if (compareResult > 0 )            selectFrames(frame,0,anchor, 0, eDirPrevious, nsSelectionStruct::SELTOEND); //unselect all between//          else real bad put in assert here        }        else if (frame && frame != aFrame){          nsSelectionStruct ss={nsSelectionStruct::SELTOEND, 0,0, 0,0, 0,0, eDirNext, PR_FALSE};          frame->SetSelected(&ss);        }        nsSelectionStruct ss={nsSelectionStruct::SELON, 0,0, aOffset, aOffset, 0,0, eDirNext, PR_FALSE};        aFrame->SetSelected(&ss);        aTracker->SetFocus(aFrame,aFrame);        PRBool batching = mBatching;//hack to use the collapse code.        PRBool changes = mChangesDuringBatching;        mBatching = PR_TRUE;        Collapse(domNode, aContentOffset + aOffset);        mBatching = batching;        mChangesDuringBatching = changes;      }      else {        if (aFrame == frame){ //drag to same frame          PRInt32 beginoffset = 0;          PRInt32 begincontentoffset = 0;          PRInt32 endoffset = 0;          PRBool selected = PR_FALSE;          if (NS_SUCCEEDED(aFrame->GetSelected(&selected,&beginoffset,&endoffset, &begincontentoffset))){            nsSelectionStruct ss={nsSelectionStruct::SELON, 0,0, beginoffset, aOffset, 0,0, eDirNext, PR_FALSE};            aFrame->SetSelected(&ss);            //PR_ASSERT(beginoffset == GetAnchorOffset());            aTracker->SetFocus(aFrame,anchor);            if (beginoffset <= aOffset)              direction = FORWARD; //selecting "english" right if true          }          else return NS_ERROR_FAILURE;        }        else if (frame){ //we need to check to see what the order is.          nsCOMPtr<nsIContent>oldContent;          if (NS_SUCCEEDED(frame->GetContent(getter_AddRefs(oldContent))) && oldContent){            nsCOMPtr<nsIDOMNode> oldDomNode(do_QueryInterface(oldContent));            if (oldDomNode && (oldDomNode.get() == GetFocusNode())) {              nsCOMPtr<nsIContent> anchorContent;              if (NS_SUCCEEDED(anchor->GetContent(getter_AddRefs(anchorContent))) && anchorContent){                nsCOMPtr<nsIDOMNode>anchorDomNode(do_QueryInterface(anchorContent));                if (anchorDomNode && anchorDomNode.get() == GetAnchorNode()) {                  //get offsets                  PRBool selected = PR_FALSE;                  PRInt32 anchorFrameOffsetBegin = 0;                  PRInt32 anchorFrameOffsetEnd = 0;                  PRInt32 anchorFrameContentOffset = 0;                  if (NS_FAILED(anchor->GetSelected(&selected, &anchorFrameOffsetBegin, &anchorFrameOffsetEnd, &anchorFrameContentOffset)) || !selected)                    return NS_ERROR_FAILURE;                  selected = PR_FALSE;                  PRInt32 focusFrameOffsetBegin = 0;                  PRInt32 focusFrameOffsetEnd = 0;                  PRInt32 focusFrameContentOffset = 0;                  if (NS_FAILED(frame->GetSelected(&selected, &focusFrameOffsetBegin, &focusFrameOffsetEnd, &focusFrameContentOffset)) || !selected)                    return NS_ERROR_FAILURE;                  //compare anchor to old cursor.                  PRInt32 result1 = compareFrames(anchor,frame); //nothing else matters. if they are the same frame.                  //compare old cursor to new cursor                  PRInt32 result2 = compareFrames(frame,aFrame);                  if (result2 == 0)                    result2 = ComparePoints(GetFocusNode(), GetFocusOffset(),                                            domNode, aOffset );                  //compare anchor to new cursor                  PRInt32 result3 = compareFrames(anchor,aFrame);                  if (result3 == 0)                    result3 = ComparePoints(GetAnchorNode(), GetAnchorOffset(),                                            domNode , aOffset );                  if (result1 == 0 && result3 < 0)                  {                    selectFrames(anchor,anchorFrameOffsetBegin, aFrame, aOffset , eDirNext, nsSelectionStruct::SELON);                   }                  else if (result1 == 0 && result3 > 0)                  {                    selectFrames(aFrame, aOffset , anchor,anchorFrameOffsetBegin, eDirPrevious,nsSelectionStruct::SELON);                  }                  else if (result1 <= 0 && result2 <= 0) {//a,1,2 or a1,2 or a,12 or a12                    //continue selection from 1 to 2                    selectFrames(frame,PR_MIN(focusFrameOffsetEnd,focusFrameOffsetBegin), aFrame, aOffset, eDirNext, nsSelectionStruct::SELON);                  }                  else if (result3 <= 0 && result2 >= 0) {//a,2,1 or a2,1 or a,21 or a21                    //deselect from 2 to 1                    selectFrames(aFrame, aOffset, frame,focusFrameOffsetEnd, eDirNext, 0);                    if (anchor != aFrame)                      selectFrames(aFrame, 0, aFrame,aOffset, eDirNext, nsSelectionStruct::SELON);                    else                      selectFrames(anchor, anchorFrameOffsetBegin, aFrame, aOffset, eDirNext, nsSelectionStruct::SELON);                  }                  else if (result1 >= 0 && result3 <= 0) {//1,a,2 or 1a,2 or 1,a2 or 1a2                    //deselect from 1 to a                    selectFrames(frame, focusFrameOffsetEnd, anchor, anchorFrameOffsetBegin, eDirPrevious, 0);                    //select from a to 2                    selectFrames(anchor, anchorFrameOffsetBegin, aFrame, aOffset, eDirNext, nsSelectionStruct::SELON);                  }                  else if (result2 <= 0 && result3 >= 0) {//1,2,a or 12,a or 1,2a or 12a                    //deselect from 1 to 2                    selectFrames(frame, focusFrameOffsetEnd, aFrame, aOffset, eDirPrevious, 0);                    if (anchor != aFrame)                      selectFrames(aFrame, aOffset, aFrame, -1, eDirPrevious, nsSelectionStruct::SELON);                    else                      selectFrames(aFrame, aOffset, anchor, anchorFrameOffsetBegin, eDirPrevious, nsSelectionStruct::SELON);                  }                  else if (result3 >= 0 && result1 <= 0) {//2,a,1 or 2a,1 or 2,a1 or 2a1                    //deselect from a to 1                    selectFrames(anchor, anchorFrameOffsetBegin, frame, focusFrameOffsetEnd, eDirNext, 0);                    //select from 2 to a                    selectFrames(aFrame, aOffset, anchor, anchorFrameOffsetBegin, eDirPrevious, nsSelectionStruct::SELON);                  }                  else if (result2 >= 0 && result1 >= 0) {//2,1,a or 21,a or 2,1a or 21a                    //continue selection from 2 to 1                    selectFrames(aFrame, aOffset, frame,PR_MAX(focusFrameOffsetEnd,focusFrameOffsetBegin), eDirPrevious, nsSelectionStruct::SELON);                  }                  if (result3 <= 0)                    direction = FORWARD;                }                aTracker->SetFocus(aFrame,anchor);              }            }          }        }      }      // Now update the range list:      if (aContinueSelection && domNode)      {        Extend(domNode, aOffset + aContentOffset);      }    }  }  else    return NS_ERROR_FAILURE;      return NotifySelectionListeners();}