Back to index

lightning-sunbird  0.9+nobinonly
TestHelloXPLoop.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is the Mozilla browser.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications, Inc.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Travis Bogard <travis@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIServiceManager.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsCNativeApp.h"
00043 #include "nsIEventLoop.h"
00044 #include <windows.h>
00045 
00046 static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID);
00047 
00048 LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
00049 
00050 void ErrorBox(LPSTR text)
00051 {
00052        MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONSTOP);
00053 }
00054 
00055 void InfoBox(LPSTR text)
00056 {
00057        MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION);
00058 }
00059 
00060 int WINAPI WinMain(HINSTANCE inst, 
00061                    HINSTANCE prevInstance, 
00062                    LPSTR lpszCmdLine,
00063                    int nShowCmd)
00064 {
00065        char* lpszAppName = "HelloWorld";
00066        HWND wnd;
00067        WNDCLASSEX wndclass;
00068        int retCode;
00069 
00070     {  //  Needed to scope all nsCOMPtr within XPCOM Init and Shutdown
00071       nsresult rv;
00072       nsCOMPtr<nsIServiceManager> servMan;
00073       rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00074       if(NS_FAILED(rv))
00075       {
00076         ErrorBox("Failed to initalize xpcom.");
00077         return -1;
00078       }
00079       
00080       nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00081       NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00082       registrar->AutoRegister(nsnull);
00083       
00084       nsCOMPtr<nsINativeApp> nativeAppService(do_GetService(kNativeAppCID, &rv));
00085 
00086       if(NS_FAILED(rv))
00087       {
00088         ErrorBox("Failed to get nativeAppService");
00089         return -1;
00090       }
00091       wndclass.cbSize        = sizeof(wndclass);
00092       wndclass.style         = CS_HREDRAW | CS_VREDRAW;
00093       wndclass.lpfnWndProc   = WndProc;
00094       wndclass.cbClsExtra    = 0;
00095       wndclass.cbWndExtra    = 0;
00096       wndclass.hInstance     = inst;
00097       wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
00098       wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
00099       wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
00100       wndclass.lpszMenuName  = NULL;
00101       wndclass.lpszClassName = lpszAppName;
00102       wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
00103 
00104       RegisterClassEx(&wndclass) ;
00105       
00106       wnd = CreateWindow(lpszAppName, "The Hello World",
00107                          WS_OVERLAPPEDWINDOW,
00108                          CW_USEDEFAULT, CW_USEDEFAULT,
00109                          CW_USEDEFAULT, CW_USEDEFAULT,
00110                          NULL, NULL, inst, NULL);                   
00111       
00112       ShowWindow(wnd, nShowCmd);
00113       UpdateWindow(wnd);
00114       
00115       nsCOMPtr<nsIEventLoop> eventLoop;
00116       
00117       if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop", 
00118                                                      nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop))))
00119         {
00120           ErrorBox("Failed to create event Loop");
00121           return 0;
00122         } 
00123       
00124       eventLoop->Run(nsnull, nsnull, nsnull, &retCode);
00125       eventLoop = nsnull; // Clear out before Shutting down XPCOM
00126       
00127       InfoBox("Hello World app is out of loop");
00128     }
00129        NS_ShutdownXPCOM(nsnull);
00130        InfoBox("Hello World app is exiting");
00131        return retCode;
00132 }
00133 
00134 LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
00135 {
00136        HDC         hdc;
00137        PAINTSTRUCT ps;
00138        RECT        rect;
00139 
00140        switch(msg)
00141               {
00142               case WM_PAINT:
00143                      hdc = BeginPaint(wnd, &ps);
00144 
00145                      GetClientRect(wnd, &rect);
00146 
00147                      DrawText(hdc, "Hello, XP Event Loop!", -1, &rect,
00148                                     DT_SINGLELINE | DT_CENTER | DT_VCENTER);
00149 
00150                      EndPaint(wnd, &ps);
00151                      return 0;
00152 
00153               case WM_DESTROY:
00154                      {
00155                      nsresult rv;
00156                      nsCOMPtr<nsINativeApp> nativeAppService = 
00157                               do_GetService(kNativeAppCID, &rv);
00158                      if(NS_FAILED(rv))
00159                             {
00160                             ErrorBox("Could not get NativeAppService");
00161                             return 0;
00162                             }
00163                      nsCOMPtr<nsIEventLoop> eventLoop;
00164 
00165                      if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop", 
00166                             getter_AddRefs(eventLoop))))
00167                             {
00168                             ErrorBox("Failed to find event Loop");
00169                             return 0;
00170                             }
00171                      eventLoop->Exit(0);
00172                      }
00173                      return 0;
00174               }
00175 
00176        return DefWindowProc(wnd, msg, wParam, lParam);
00177 }