listBox->AddItemAt(listStr1, i);
gListBox->AddItemAt(listStr1, i);
nsresult WidgetTest(int *argc, char **argv){    char str[256];    int i;    // Open global test log file    gFD = fopen(gLogFileName, "w");    if (gFD == nsnull) {      fprintf(stderr, "Couldn't open file[%s]\n", gLogFileName);      exit(1);    }    // register xpcom classes    nsComponentManager::RegisterComponentLib(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kEventQueueCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE);        // register widget classes    nsComponentManager::RegisterComponentLib(kLookAndFeelCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCWindowCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);        nsComponentManager::RegisterComponentLib(kCChildCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCButtonCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCCheckButtonCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCComboBoxCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCFileWidgetCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCListBoxCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCRadioButtonCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCHorzScrollbarCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCVertScrollbarCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCTextAreaCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCTextFieldCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCTabWidgetCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCTooltipWidgetCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCAppShellCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kCToolkitCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    nsComponentManager::RegisterComponentLib(kClipboardCID, NULL, NULL, WIDGET_DLL, PR_FALSE, PR_FALSE);    static NS_DEFINE_IID(kCRenderingContextIID, NS_RENDERING_CONTEXT_CID);     static NS_DEFINE_IID(kCDeviceContextIID, NS_DEVICE_CONTEXT_CID);     static NS_DEFINE_IID(kCFontMetricsIID, NS_FONT_METRICS_CID);     static NS_DEFINE_IID(kCImageIID, NS_IMAGE_CID);     nsComponentManager::RegisterComponentLib(kCRenderingContextIID, NULL, NULL, GFXWIN_DLL, PR_FALSE, PR_FALSE);     nsComponentManager::RegisterComponentLib(kCDeviceContextIID, NULL, NULL, GFXWIN_DLL, PR_FALSE, PR_FALSE);     nsComponentManager::RegisterComponentLib(kCFontMetricsIID, NULL, NULL, GFXWIN_DLL, PR_FALSE, PR_FALSE);     nsComponentManager::RegisterComponentLib(kCImageIID, NULL, NULL, GFXWIN_DLL, PR_FALSE, PR_FALSE);     nsresult  res;    // Create the Event Queue for the UI thread...    res = nsServiceManager::GetService(kEventQueueServiceCID,                                       kIEventQueueServiceIID,                                       (nsISupports **)&gEventQService);    if (NS_OK != res) {        NS_ASSERTION(PR_FALSE, "Could not obtain the event queue service");        return res;    }    printf("Going to create the event queue\n");    res = gEventQService->CreateThreadEventQueue();    if (NS_OK != res) {        NS_ASSERTION(PR_FALSE, "Could not create the event queue for the thread");	return res;    }      // Create a application shell    nsIAppShell *appShell;    nsComponentManager::CreateInstance(kCAppShellCID, nsnull, kIAppShellIID, (void**)&appShell);    if (appShell != nsnull) {      fputs("Created AppShell\n", stderr);      appShell->Create(argc, argv);    } else {      printf("AppShell is null!\n");    }    nsIDeviceContext* deviceContext = 0;    // Create a device context for the widgets    static NS_DEFINE_IID(kDeviceContextCID, NS_DEVICE_CONTEXT_CID);    static NS_DEFINE_IID(kDeviceContextIID, NS_IDEVICE_CONTEXT_IID);    //    // create the main window    //    nsComponentManager::CreateInstance(kCWindowCID, nsnull, kIWidgetIID, (void**)&window);    nsRect rect(100, 100, 600, 700);    window->Create((nsIWidget*) nsnull, rect, HandleEvent,                    (nsIDeviceContext *) nsnull,                   appShell);    window->SetTitle("TOP-LEVEL window");    window->Show(PR_TRUE);    window->SetBackgroundColor(NS_RGB(196, 196, 196));    //    // Create Device Context based on main window    //    res = nsComponentManager::CreateInstance(kDeviceContextCID, nsnull, kDeviceContextIID, (void **)&deviceContext);    if (NS_OK == res)    {      deviceContext->Init(window->GetNativeData(NS_NATIVE_WIDGET));      NS_ADDREF(deviceContext);    }#ifdef XP_PC    tooltipWindow = createTooltipWindow(window, "INSERT <tooltip> here", 0, 0, 150, 0);    NS_ShowWidget(tooltipWindow,PR_FALSE);    toolTipButton1 = createSimpleButton(window, "Tooltip \\/\\/",400, 100, 100, 0);    toolTipButton2 = createSimpleButton(window, "Tooltip /\\/\\",500, 200, 100, 0);    createTestButton(window, "Move Tooltip pos 1", 450, 150, 130, TooltipPos1);    createTestButton(window, "Move Tooltip pos 2", 450, 175, 130, TooltipPos2);    SetTooltipPos(1, window, toolTipButton1, toolTipButton2);#endif    //    // create a child    //    nsIWidget *child;    //    // create another child    //    int x = 5;    int y = 10;    rect.SetRect(x, y, 100, 100);      nsComponentManager::CreateInstance(kCChildCID, nsnull, kIWidgetIID, (void**)&child);      #if 0     child->SetBorderStyle(eBorderStyle_dialog);    child->Create(window, rect, HandleEvent, NULL);    //child->SetBackgroundColor(NS_RGB(255, 255, 0));    child->SetForegroundColor(NS_RGB(255, 0, 0));    child->SetBackgroundColor(NS_RGB(255, 128, 64));#endif      //NS_RELEASE(child); // the parent keeps a reference on this child    y += rect.height + 5;       //    // create a button    //    nsIButton *button;    rect.SetRect(x, y, 60, 25);      nsComponentManager::CreateInstance(kCButtonCID, nsnull, kIButtonIID, (void**)&button);    NS_CreateButton(window,button,rect,HandleEvent);    nsString label("Slider");    button->SetLabel(label);    nsAutoString strBuf;    button->GetLabel(strBuf);    button->QueryInterface(kIWidgetIID,(void**)&movingWidget);    y += rect.height + 5;    x = 5;    x = createTestButton(window, kHideBtn, x, y, 75, ButtonTestHandleEvent) + 5;    x = createTestButton(window, kShowBtn, x, y, 75, ButtonTestHandleEvent);    x = 5;    y += rect.height + 5;    // Create browse button    x = createTestButton(window, kBrowseBtn, x,   y, 75, HandleFileButtonEvent);    x = 5;    y += rect.height + 5;    //    // create a check button    //    nsICheckButton * checkButton;    rect.SetRect(x, y, 100, 25);      nsComponentManager::CreateInstance(kCCheckButtonCID, nsnull, kICheckButtonIID, (void**)&checkButton);    NS_CreateCheckButton(window,checkButton,rect,CheckButtonTestHandleEvent);    nsString cbLabel("CheckButton");    checkButton->SetLabel(cbLabel);    y += rect.height + 5;    //    // create a text widget    //    nsIWidget*      widget = nsnull;    nsITextWidget*  textWidget = nsnull;    rect.SetRect(x, y, 100, TEXT_HEIGHT);      PRUint32 actualSize;    nsFont font("Times", NS_FONT_STYLE_NORMAL,                         NS_FONT_VARIANT_NORMAL,                         NS_FONT_WEIGHT_BOLD,                         0,                         12);    nsComponentManager::CreateInstance(kCTextFieldCID, nsnull, kITextWidgetIID, (void**)&textWidget);    NS_CreateTextWidget(window,textWidget,rect,HandleEvent);    nsString initialText("0123456789");    textWidget->SetText(initialText,actualSize);    textWidget->SetMaxTextLength(12);    textWidget->SelectAll();    //NS_RELEASE(textWidget);     y += rect.height + 5;     //    // create a text password widget    //    nsITextWidget * ptextWidget;    rect.SetRect(x, y, 100, TEXT_HEIGHT);      nsComponentManager::CreateInstance(kCTextFieldCID, nsnull, kITextWidgetIID, (void**)&ptextWidget);    NS_CreateTextWidget(window, ptextWidget, rect, HandleEvent);        nsString pinitialText("password text");    ptextWidget->SetText(pinitialText,actualSize);    passwordText = ptextWidget;       ptextWidget->SetPassword(PR_TRUE);    x = x+180;    int saveX = x;    x = createTestButton(window, kSetSelection, saveX,y, 100, PasswordTextTestHandleEvent);    x = createTestButton(window, kClearSelection, x+5,y, 100, PasswordTextTestHandleEvent);    x = createTestButton(window, kSelectAll,      x+5,y, 100, PasswordTextTestHandleEvent);    // Next Row of texting Buttons    x = saveX;    y += 30;    x = createTestButton(window, kSetCaret, x+5,y, 75, PasswordTextTestHandleEvent);    x = createTestButton(window, kGetCaret, x+5,y, 75, PasswordTextTestHandleEvent);    x = createTestButton(window, kSetText,  x+5,y, 75, PasswordTextTestHandleEvent);    x = createTestButton(window, kGetText,  x+5,y, 75, PasswordTextTestHandleEvent);    x = 5;    //y += rect.height + 5;    //    // create a readonly text widget    //    nsITextWidget * rtextWidget;    rect.SetRect(x, y, 100, TEXT_HEIGHT);      nsComponentManager::CreateInstance(kCTextFieldCID, nsnull, kITextWidgetIID, (void**)&rtextWidget);    NS_CreateTextWidget(window, rtextWidget, rect, HandleEvent);    PRBool old;    nsString rinitialText("This is readonly");    rtextWidget->SetText(rinitialText,actualSize);    rtextWidget->SetReadOnly(PR_TRUE,old);     //NS_RELEASE(rtextWidget);     y += rect.height + 5;    //    // create a text area widget    //    nsITextAreaWidget * textAreaWidget;    rect.SetRect(x, y, 150, 100);      nsComponentManager::CreateInstance(kCTextAreaCID, nsnull, kITextAreaWidgetIID, (void**)&textAreaWidget);    NS_CreateTextAreaWidget(window,textAreaWidget,rect,HandleEvent);    nsString textAreaInitialText("Text Area Widget");    textWidgetInstance = textAreaWidget;    textAreaWidget->SetText(textAreaInitialText,actualSize);    //NS_RELEASE(textAreaWidget);     //y += rect.height + 5;    x += rect.width + 5;    // Save these for later    int saveY = y;    saveX = x;    //    // create a scrollbar    //    rect.SetRect(x, 10, 25, 300);      nsComponentManager::CreateInstance(kCVertScrollbarCID, nsnull, kIScrollbarIID, (void**)&scrollbar);    NS_CreateScrollBar(window,scrollbar,rect,HandleEvent);    scrollbar->SetMaxRange(300);    scrollbar->SetThumbSize(50);    scrollbar->SetPosition(100);    x += rect.width + 5;     //    // create a Status Text    //    y = 10;    rect.SetRect(x, y, 350, TEXT_HEIGHT);      nsComponentManager::CreateInstance(kCTextFieldCID, nsnull, kITextWidgetIID, (void**)&statusText);    statusText->QueryInterface(kIWidgetIID,(void**)&widget);    widget->Create(window, rect, HandleEvent, deviceContext);    widget->Show(PR_TRUE);    y += rect.height + 5;    //    // create a Failed Button    //    rect.SetRect(x, y, 100, 25);      nsComponentManager::CreateInstance(kCButtonCID, nsnull, kIButtonIID, (void**)&button);    NS_CreateButton(window,button,rect,FailedButtonHandleEvent);    nsString failedLabel("Failed");    button->SetLabel(failedLabel);    rect.SetRect(x, y+30, 150, 25);      nsComponentManager::CreateInstance(kCButtonCID, nsnull, kIButtonIID, (void**)&button);    NS_CreateButton(window,button,rect,DoSelfTests);    nsString selfTestLabel("Perform Self Tests");    button->SetLabel(selfTestLabel);    x += rect.width + 5;    //    // create a Succeeded Button    //    rect.SetRect(x, y, 100, 25);      nsComponentManager::CreateInstance(kCButtonCID, nsnull, kIButtonIID, (void**)&button);    NS_CreateButton(window,button,rect,SucceededButtonHandleEvent);    nsString succeededLabel("Succeeded");    button->SetLabel(succeededLabel);        //    // create a listbox widget    //    y = saveY;    x = saveX;    rect.SetRect(x, y, 150, 100);      nsComponentManager::CreateInstance(kCListBoxCID, nsnull, kIListBoxIID, (void**)&listBox);    if (listBox)    {	    NS_CreateListBox(window,listBox,rect,HandleEvent);	    for (i=0;i<NUM_COMBOBOX_ITEMS;i++) {	      sprintf(str, "%s %d", "List Item", i);	      nsString listStr1(str);	      listBox->AddItemAt(listStr1, i);	    }    }    x += rect.width+5;    x = createTestButton(window, kSetSelection,    x, y, 125, ListBoxTestHandleEvent) + 5;    x = createTestButton(window, kRemoveSelection, x,   y, 125, ListBoxTestHandleEvent);    x = 5;    y += rect.height + 5;    //    // create a multi-selection listbox widget    //    rect.SetRect(x, y, 150, 100);      nsComponentManager::CreateInstance(kCListBoxCID, nsnull, kIListBoxIID, (void**)&gMultiListBox);    if (gMultiListBox)    {	      // Notice the extra agrument PR_TRUE below which indicates that	      // the list widget is multi-select	    gMultiListBox->SetMultipleSelection(PR_TRUE);	    NS_CreateListBox(window,gMultiListBox,rect,HandleEvent);	    for (i=0;i<NUM_COMBOBOX_ITEMS;i++) {	      sprintf(str, "%s %d", "Multi List Item", i);	      nsString listStr1(str);	      gMultiListBox->AddItemAt(listStr1, i);	    }    }    x = createTestButton(window, kSetSelection,    x+150, y, 125, MultiListBoxTestHandleEvent);    x = createTestButton(window, kRemoveSelection, x+5,   y, 125, MultiListBoxTestHandleEvent);    x = createTestButton(window, kSetSelectedIndices, x+5,   y, 125, MultiListBoxTestHandleEvent);    y += rect.height + 5;    x = 5;    //    // create a tab widget    //    rect.SetRect(300, 500, 200, 50);      nsComponentManager::CreateInstance(kCTabWidgetCID, nsnull, kITabWidgetIID, (void**)&tabWidget);    if (tabWidget)    {	    NS_CreateTabWidget(window,tabWidget,rect,HandleTabEvent);	    nsString tabs[] = {"low", "medium", "high" };	    tabWidget->SetTabs(3, tabs);    }    //    // create a Radio button    //    nsIRadioButton * radioButton;    rect.SetRect(x, y, 120, 25);      nsComponentManager::CreateInstance(kCRadioButtonCID, nsnull, kIRadioButtonIID, (void**)&radioButton);    NS_CreateRadioButton(window,radioButton,rect,HandleEvent);    nsString rbLabel("RadioButton1");    radioButton->SetLabel(rbLabel);    gRadioBtns[gNumRadioBtns++] = radioButton;    y += rect.height + 5;    //    // create a Radio button    //    rect.SetRect(x, y, 120, 25);      nsComponentManager::CreateInstance(kCRadioButtonCID, nsnull, kIRadioButtonIID, (void**)&radioButton);    NS_CreateRadioButton(window,radioButton,rect,HandleEvent);    nsString rbLabel2("RadioButton2");    radioButton->SetLabel(rbLabel2);    gRadioBtns[gNumRadioBtns++] = radioButton;    y += rect.height + 5;    //window->SetBackgroundColor(NS_RGB(0,255,0));    //    // create a ComboBox    //    rect.SetRect(x, y, 120, 100);      nsComponentManager::CreateInstance(kCComboBoxCID, nsnull, kIComboBoxIID, (void**)&comboBox);    if (comboBox)    {	    NS_CreateComboBox(window,comboBox,rect,HandleEvent);	    for (i=0;i<NUM_COMBOBOX_ITEMS;i++) {	      sprintf(str, "%s %d", "List Item", i);	      nsString listStr1(str);	      comboBox->AddItemAt(listStr1, i);	    }    }    x = createTestButton(window, kSetSelection,    x+125, y, 125, ComboTestHandleEvent);    x = createTestButton(window, kRemoveSelection, x+5,   y, 125, ComboTestHandleEvent);    x = 5;    y += 30;       nsString status("The non-visual tests: ");    status.Append( (gOverallStatus  ? "PASSED":"FAILED"));    gNonVisualStatus = gOverallStatus;    statusText->SetText(status,actualSize);    // show    window->Show(PR_TRUE);    window->SetCursor(eCursor_hyperlink);    return(appShell->Run());}