1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/tests/windows/TestHelloXPLoop.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsIServiceManager.h" 1.11 +#include "nsCOMPtr.h" 1.12 +#include "nsCNativeApp.h" 1.13 +#include "nsIEventLoop.h" 1.14 +#include <windows.h> 1.15 + 1.16 +static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID); 1.17 + 1.18 +LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam); 1.19 + 1.20 +void ErrorBox(LPSTR text) 1.21 +{ 1.22 + MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONSTOP); 1.23 +} 1.24 + 1.25 +void InfoBox(LPSTR text) 1.26 +{ 1.27 + MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION); 1.28 +} 1.29 + 1.30 +int WINAPI WinMain(HINSTANCE inst, 1.31 + HINSTANCE prevInstance, 1.32 + LPSTR lpszCmdLine, 1.33 + int nShowCmd) 1.34 +{ 1.35 + char* lpszAppName = "HelloWorld"; 1.36 + HWND wnd; 1.37 + WNDCLASSEX wndclass; 1.38 + int retCode; 1.39 + 1.40 + { // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown 1.41 + nsresult rv; 1.42 + nsCOMPtr<nsIServiceManager> servMan; 1.43 + rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.44 + if(NS_FAILED(rv)) 1.45 + { 1.46 + ErrorBox("Failed to initialize xpcom."); 1.47 + return -1; 1.48 + } 1.49 + 1.50 + nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); 1.51 + NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); 1.52 + registrar->AutoRegister(nullptr); 1.53 + 1.54 + nsCOMPtr<nsINativeApp> nativeAppService(do_GetService(kNativeAppCID, &rv)); 1.55 + 1.56 + if(NS_FAILED(rv)) 1.57 + { 1.58 + ErrorBox("Failed to get nativeAppService"); 1.59 + return -1; 1.60 + } 1.61 + wndclass.cbSize = sizeof(wndclass); 1.62 + wndclass.style = CS_HREDRAW | CS_VREDRAW; 1.63 + wndclass.lpfnWndProc = WndProc; 1.64 + wndclass.cbClsExtra = 0; 1.65 + wndclass.cbWndExtra = 0; 1.66 + wndclass.hInstance = inst; 1.67 + wndclass.hIcon = LoadIcon(nullptr, IDI_APPLICATION); 1.68 + wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW); 1.69 + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 1.70 + wndclass.lpszMenuName = nullptr; 1.71 + wndclass.lpszClassName = lpszAppName; 1.72 + wndclass.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); 1.73 + 1.74 + RegisterClassEx(&wndclass) ; 1.75 + 1.76 + wnd = CreateWindow(lpszAppName, "The Hello World", 1.77 + WS_OVERLAPPEDWINDOW, 1.78 + CW_USEDEFAULT, CW_USEDEFAULT, 1.79 + CW_USEDEFAULT, CW_USEDEFAULT, 1.80 + nullptr, nullptr, inst, nullptr); 1.81 + 1.82 + ShowWindow(wnd, nShowCmd); 1.83 + UpdateWindow(wnd); 1.84 + 1.85 + nsCOMPtr<nsIEventLoop> eventLoop; 1.86 + 1.87 + if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop", 1.88 + nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop)))) 1.89 + { 1.90 + ErrorBox("Failed to create event Loop"); 1.91 + return 0; 1.92 + } 1.93 + 1.94 + eventLoop->Run(nullptr, nullptr, nullptr, &retCode); 1.95 + eventLoop = nullptr; // Clear out before Shutting down XPCOM 1.96 + 1.97 + InfoBox("Hello World app is out of loop"); 1.98 + } 1.99 + NS_ShutdownXPCOM(nullptr); 1.100 + InfoBox("Hello World app is exiting"); 1.101 + return retCode; 1.102 +} 1.103 + 1.104 +LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) 1.105 +{ 1.106 + HDC hdc; 1.107 + PAINTSTRUCT ps; 1.108 + RECT rect; 1.109 + 1.110 + switch(msg) 1.111 + { 1.112 + case WM_PAINT: 1.113 + hdc = BeginPaint(wnd, &ps); 1.114 + 1.115 + GetClientRect(wnd, &rect); 1.116 + 1.117 + DrawText(hdc, "Hello, XP Event Loop!", -1, &rect, 1.118 + DT_SINGLELINE | DT_CENTER | DT_VCENTER); 1.119 + 1.120 + EndPaint(wnd, &ps); 1.121 + return 0; 1.122 + 1.123 + case WM_DESTROY: 1.124 + { 1.125 + nsresult rv; 1.126 + nsCOMPtr<nsINativeApp> nativeAppService = 1.127 + do_GetService(kNativeAppCID, &rv); 1.128 + if(NS_FAILED(rv)) 1.129 + { 1.130 + ErrorBox("Could not get NativeAppService"); 1.131 + return 0; 1.132 + } 1.133 + nsCOMPtr<nsIEventLoop> eventLoop; 1.134 + 1.135 + if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop", 1.136 + getter_AddRefs(eventLoop)))) 1.137 + { 1.138 + ErrorBox("Failed to find event Loop"); 1.139 + return 0; 1.140 + } 1.141 + eventLoop->Exit(0); 1.142 + } 1.143 + return 0; 1.144 + } 1.145 + 1.146 + return DefWindowProc(wnd, msg, wParam, lParam); 1.147 +}