michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIServiceManager.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCNativeApp.h" michael@0: #include "nsIEventLoop.h" michael@0: #include michael@0: michael@0: static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID); michael@0: michael@0: LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam); michael@0: michael@0: void ErrorBox(LPSTR text) michael@0: { michael@0: MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONSTOP); michael@0: } michael@0: michael@0: void InfoBox(LPSTR text) michael@0: { michael@0: MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION); michael@0: } michael@0: michael@0: int WINAPI WinMain(HINSTANCE inst, michael@0: HINSTANCE prevInstance, michael@0: LPSTR lpszCmdLine, michael@0: int nShowCmd) michael@0: { michael@0: char* lpszAppName = "HelloWorld"; michael@0: HWND wnd; michael@0: WNDCLASSEX wndclass; michael@0: int retCode; michael@0: michael@0: { // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown michael@0: nsresult rv; michael@0: nsCOMPtr servMan; michael@0: rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: if(NS_FAILED(rv)) michael@0: { michael@0: ErrorBox("Failed to initialize xpcom."); michael@0: return -1; michael@0: } michael@0: michael@0: nsCOMPtr registrar = do_QueryInterface(servMan); michael@0: NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); michael@0: registrar->AutoRegister(nullptr); michael@0: michael@0: nsCOMPtr nativeAppService(do_GetService(kNativeAppCID, &rv)); michael@0: michael@0: if(NS_FAILED(rv)) michael@0: { michael@0: ErrorBox("Failed to get nativeAppService"); michael@0: return -1; michael@0: } michael@0: wndclass.cbSize = sizeof(wndclass); michael@0: wndclass.style = CS_HREDRAW | CS_VREDRAW; michael@0: wndclass.lpfnWndProc = WndProc; michael@0: wndclass.cbClsExtra = 0; michael@0: wndclass.cbWndExtra = 0; michael@0: wndclass.hInstance = inst; michael@0: wndclass.hIcon = LoadIcon(nullptr, IDI_APPLICATION); michael@0: wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW); michael@0: wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); michael@0: wndclass.lpszMenuName = nullptr; michael@0: wndclass.lpszClassName = lpszAppName; michael@0: wndclass.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); michael@0: michael@0: RegisterClassEx(&wndclass) ; michael@0: michael@0: wnd = CreateWindow(lpszAppName, "The Hello World", michael@0: WS_OVERLAPPEDWINDOW, michael@0: CW_USEDEFAULT, CW_USEDEFAULT, michael@0: CW_USEDEFAULT, CW_USEDEFAULT, michael@0: nullptr, nullptr, inst, nullptr); michael@0: michael@0: ShowWindow(wnd, nShowCmd); michael@0: UpdateWindow(wnd); michael@0: michael@0: nsCOMPtr eventLoop; michael@0: michael@0: if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop", michael@0: nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop)))) michael@0: { michael@0: ErrorBox("Failed to create event Loop"); michael@0: return 0; michael@0: } michael@0: michael@0: eventLoop->Run(nullptr, nullptr, nullptr, &retCode); michael@0: eventLoop = nullptr; // Clear out before Shutting down XPCOM michael@0: michael@0: InfoBox("Hello World app is out of loop"); michael@0: } michael@0: NS_ShutdownXPCOM(nullptr); michael@0: InfoBox("Hello World app is exiting"); michael@0: return retCode; michael@0: } michael@0: michael@0: LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) michael@0: { michael@0: HDC hdc; michael@0: PAINTSTRUCT ps; michael@0: RECT rect; michael@0: michael@0: switch(msg) michael@0: { michael@0: case WM_PAINT: michael@0: hdc = BeginPaint(wnd, &ps); michael@0: michael@0: GetClientRect(wnd, &rect); michael@0: michael@0: DrawText(hdc, "Hello, XP Event Loop!", -1, &rect, michael@0: DT_SINGLELINE | DT_CENTER | DT_VCENTER); michael@0: michael@0: EndPaint(wnd, &ps); michael@0: return 0; michael@0: michael@0: case WM_DESTROY: michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr nativeAppService = michael@0: do_GetService(kNativeAppCID, &rv); michael@0: if(NS_FAILED(rv)) michael@0: { michael@0: ErrorBox("Could not get NativeAppService"); michael@0: return 0; michael@0: } michael@0: nsCOMPtr eventLoop; michael@0: michael@0: if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop", michael@0: getter_AddRefs(eventLoop)))) michael@0: { michael@0: ErrorBox("Failed to find event Loop"); michael@0: return 0; michael@0: } michael@0: eventLoop->Exit(0); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: return DefWindowProc(wnd, msg, wParam, lParam); michael@0: }