nsIFrame *parent = aFrame; for (int i=0; i <3; i++){ if (NS_FAILED(parent->GetParent(parent)) || !parent) return NS_ERROR_NULL_POINTER; }  Clear(); nsIFrame *frame; nsIFrame *anchor;
nsRangeList::TakeFocus(nsIFocusTracker *aTracker, nsIFrame *aFrame, PRInt32 aOffset, PRInt32 aContentOffset, PRBool aContinueSelection){  if (!aTracker || !aFrame)    return NS_ERROR_NULL_POINTER;  //HACKHACKHACK  nsIFrame *parent = aFrame;  for (int i=0; i <3; i++){    if (NS_FAILED(parent->GetParent(parent)) || !parent)      return NS_ERROR_NULL_POINTER;  }  //END HACK  Clear(); //change this later   nsIFrame *frame;  nsIFrame *anchor;  nsCOMPtr<nsIContent> content;  nsCOMPtr<nsIDOMNode> domNode;  PRBool direction(PR_TRUE);//true == left to right  if (NS_SUCCEEDED(aFrame->GetContent(*getter_AddRefs(content)))){    domNode = content;    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, -1, PR_FALSE, PR_TRUE); //unselect all between          else if (compareResult > 0 )            selectFrames(frame,0,anchor, -1, PR_FALSE, PR_FALSE); //unselect all between//          else real bad put in assert here        }        else if (frame && frame != aFrame){          frame->SetSelected(PR_FALSE, 0, -1, PR_FALSE);//just turn off selection if the previous frame        }        mAnchorNode = domNode;        mAnchorOffset = aOffset + aContentOffset;        direction = PR_TRUE; //slecting "english" right        aFrame->SetSelected(PR_TRUE,aOffset,aOffset,PR_FALSE);        aTracker->SetFocus(aFrame,aFrame);        nsCOMPtr<nsIDOMRange> range;      }      else {        if (aFrame == frame){ //drag to same frame          PRInt32 beginoffset;          PRInt32 begincontentoffset;          PRInt32 endoffset;          PRBool selected;          if (NS_SUCCEEDED(aFrame->GetSelected(&selected,&beginoffset,&endoffset, &begincontentoffset))){            aFrame->SetSelected(PR_TRUE, beginoffset, aOffset,PR_FALSE);            //PR_ASSERT(beginoffset == mAnchorOffset);            aTracker->SetFocus(aFrame,anchor);            direction = (PRBool)(beginoffset<=aOffset); //slecting "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)))){            nsCOMPtr<nsIDOMNode>oldDomNode(oldContent);            if (oldDomNode && oldDomNode == mFocusNode) {              nsCOMPtr<nsIContent>anchorContent;              if (NS_SUCCEEDED(anchor->GetContent(*getter_AddRefs(anchorContent)))){                nsCOMPtr<nsIDOMNode>anchorDomNode(anchorContent);                if (anchorDomNode && anchorDomNode == mAnchorNode) {                  //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(mFocusNode, mFocusOffset, domNode, aOffset );                  //compare anchor to new cursor                  PRInt32 result3 = compareFrames(anchor,aFrame);                  if (result3 == 0)                    result3 = ComparePoints(mAnchorNode, mAnchorOffset ,domNode , aOffset );                  if (result1 == 0 && result3 < 0)                  {                    selectFrames(anchor,anchorFrameOffsetBegin, aFrame, aOffset , PR_TRUE, PR_TRUE); //last true is forwards                  }                  else if (result1 == 0 && result3 > 0)                  {                    selectFrames(aFrame, aOffset , anchor,anchorFrameOffsetBegin, PR_TRUE, PR_FALSE);//last true is backwards                  }                  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, PR_TRUE, PR_TRUE);                  }                  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, PR_FALSE, PR_TRUE);                    if (anchor != aFrame)                      selectFrames(aFrame, 0, aFrame,aOffset, PR_TRUE, PR_TRUE);                    else                      selectFrames(anchor, anchorFrameOffsetBegin, aFrame, aOffset, PR_TRUE ,PR_TRUE);                  }                  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, PR_FALSE, PR_FALSE);                    //select from a to 2                    selectFrames(anchor, anchorFrameOffsetBegin, aFrame, aOffset, PR_TRUE, PR_TRUE);                  }                  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, PR_FALSE, PR_FALSE);                    if (anchor != aFrame)                      selectFrames(aFrame, aOffset, aFrame, -1, PR_TRUE, PR_FALSE);                    else                      selectFrames(aFrame, aOffset, anchor, anchorFrameOffsetBegin, PR_TRUE ,PR_FALSE);                  }                  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, PR_FALSE, PR_TRUE);                    //select from 2 to a                    selectFrames(aFrame, aOffset, anchor, anchorFrameOffsetBegin, PR_TRUE, PR_FALSE);                  }                  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), PR_TRUE, PR_FALSE);                  }                  direction = (PRBool) (result3 <= 0);                }                aTracker->SetFocus(aFrame,anchor);              }            }          }        }/*#1 figure out the orderwe can tell the direction of the selection by asking for the anchors selectionif the begin is less than the end then we know the selection is to the "right".else it is a backwards selection.a = anchor1 = old cursor2 = new cursor  if (a <= 1 && 1 <=2)    a,1,2  or (a1,2)  if (a < 2 && 1 > 2)     a,2,1  if (1 < a && a <2)      1,a,2  if (a > 2 && 2 >1)      1,2,a  if (2 < a && a <1)      2,a,1  if (a > 1 && 1 >2)      2,1,athen executea  1  2 select from 1 to 2a  2  1 deselect from 2 to 11  a  2 deselect from 1 to a select from a to 21  2  a deselect from 1 to 22  1  a = continue selection from 2 to 1      */      }      nsCOMPtr<nsIDOMRange> range;      if (NS_SUCCEEDED(nsRepository::CreateInstance(kRangeCID, nsnull, kIDOMRangeIID, getter_AddRefs(range)))){ //create an irange        if (domNode){          mFocusNode = domNode;          mFocusOffset = aOffset + aContentOffset;          if (direction){            range->SetStart(mAnchorNode,mAnchorOffset);            range->SetEnd(mFocusNode,mFocusOffset);          }          else {            range->SetStart(mFocusNode,mFocusOffset);            range->SetEnd(mAnchorNode,mAnchorOffset);          }          DEBUG_OUT_RANGE(range);          nsCOMPtr<nsISupports> rangeISupports(range);          if (rangeISupports) {            AddItem(rangeISupports);            return NS_OK;          }        }      }    }  }  return NS_OK;}