nsCOMPtr<nsIPresState> presState; nsCOMPtr<nsISupportsArray> value; res = GetPresState(getter_AddRefs(presState), getter_AddRefs(value)); if (NS_SUCCEEDED(res) && !presState) { return NS_OK; }    PRBool doesExist = PR_FALSE; if (!value) { res = NS_NewISupportsArray(getter_AddRefs(value)); } else { doesExist = PR_TRUE; }  if (NS_SUCCEEDED(res) && value) {    PRUint32 count = 0; value->Count(&count);    if (!isMultiple || (-1 == aIndex)) { value->Clear(); count = 0; }   if (-1 != aIndex) { nsCOMPtr<nsISupportsPRInt32> thisVal; res = nsComponentManager::CreateInstance(NS_SUPPORTS_PRINT32_PROGID, nsnull, NS_GET_IID(nsISupportsPRInt32), (void**)getter_AddRefs(thisVal)); if (NS_SUCCEEDED(res) && thisVal) { res = thisVal->SetData(aIndex); if (NS_SUCCEEDED(res)) { PRBool okay = value->InsertElementAt((nsISupports *)thisVal, count); if (!okay) res = NS_ERROR_OUT_OF_MEMORY; } } }    if (!doesExist) { presState->SetStatePropertyAsSupports(NS_ConvertASCIItoUCS2("selecteditems"), value); } }
result = mSelect->SetSelectedIndex(aSelectedIndex);
nsHTMLSelectElement::SetSelectedIndex(PRInt32 aIndex){  nsIFormControlFrame* formControlFrame = nsnull;  if (NS_OK == nsGenericHTMLElement::GetPrimaryFrame(this, formControlFrame)) {    nsString value;    value.AppendInt(aIndex, 10);    nsIPresContext* presContext;    nsGenericHTMLElement::GetPresContext(this, &presContext);    formControlFrame->SetProperty(presContext, nsHTMLAtoms::selectedindex, value);    NS_IF_RELEASE(presContext);  } else {    // Since there are no frames, then we must adjust which options    // are selcted in the content and the current select state is kept in    // the PreState    //    // First, get whether this is a select with     // multiple items that can be slected    PRBool isMultiple;    nsresult res = GetMultiple(&isMultiple);         // Must not be multiple    if (NS_FAILED(res)) {      isMultiple = PR_FALSE;    }    // Call the helper method to get the PresState and     // the SupportsArray that contains the value    //    nsCOMPtr<nsIPresState> presState;    nsCOMPtr<nsISupportsArray> value;    res = GetPresState(getter_AddRefs(presState), getter_AddRefs(value));    if (NS_SUCCEEDED(res) && !presState) {      return NS_OK;    }    // If the nsISupportsArray doesn't exist then we must creat one    // cache the values    PRBool doesExist = PR_FALSE;    if (!value) {      res = NS_NewISupportsArray(getter_AddRefs(value));    } else {      doesExist = PR_TRUE;    }    if (NS_SUCCEEDED(res) && value) {      // The nsISupportArray will hold a list of indexes       // of the items that are selected      // get the count of selected items      PRUint32 count = 0;      value->Count(&count);      // if not multiple then only one item can be selected      // so we clear the list of selcted items      if (!isMultiple || (-1 == aIndex)) { // -1 -> deselect all (bug 28143)        value->Clear();        count = 0;      }      // Set the index as selected and add it to the array      if (-1 != aIndex) {        nsCOMPtr<nsISupportsPRInt32> thisVal;        res = nsComponentManager::CreateInstance(NS_SUPPORTS_PRINT32_PROGID,	                     nsnull, NS_GET_IID(nsISupportsPRInt32), (void**)getter_AddRefs(thisVal));        if (NS_SUCCEEDED(res) && thisVal) {          res = thisVal->SetData(aIndex);          if (NS_SUCCEEDED(res)) {            PRBool okay = value->InsertElementAt((nsISupports *)thisVal, count);            if (!okay) res = NS_ERROR_OUT_OF_MEMORY; // Most likely cause;          }        }      }      // If it is a new nsISupportsArray then       // set it into the PresState      if (!doesExist) {        presState->SetStatePropertyAsSupports(NS_ConvertASCIItoUCS2("selecteditems"), value);      }    } // if  }  return NS_OK;}