|
| 1 | +#include "nsIServiceManager.h" |
| 2 | +#include "nsCOMPtr.h" |
| 3 | +#include "nsCNativeApp.h" |
| 4 | +#include "nsIEventLoop.h" |
| 5 | +#include <windows.h> |
| 6 | + |
| 7 | +static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID); |
| 8 | + |
| 9 | +LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 10 | + |
| 11 | +void ErrorBox(LPSTR text) |
| 12 | +{ |
| 13 | + MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONSTOP); |
| 14 | +} |
| 15 | + |
| 16 | +void InfoBox(LPSTR text) |
| 17 | +{ |
| 18 | + MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION); |
| 19 | +} |
| 20 | + |
| 21 | +int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInstance, LPSTR lpszCmdLine, |
| 22 | + int nShowCmd) |
| 23 | +{ |
| 24 | + char* lpszAppName = "HelloWorld"; |
| 25 | + HWND wnd; |
| 26 | + WNDCLASSEX wndclass; |
| 27 | + |
| 28 | + nsCOMPtr<nsIServiceManager> servMgr; |
| 29 | + NS_InitXPCOM(getter_AddRefs(servMgr)); |
| 30 | + nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, nsnull); |
| 31 | + |
| 32 | + nsresult rv; |
| 33 | + NS_WITH_SERVICE(nsINativeApp, nativeAppService, kNativeAppCID, &rv); |
| 34 | + |
| 35 | + if(NS_FAILED(rv)) |
| 36 | + { |
| 37 | + ErrorBox("Failed to get nativeAppService"); |
| 38 | + return -1; |
| 39 | + } |
| 40 | + wndclass.cbSize = sizeof(wndclass); |
| 41 | + wndclass.style = CS_HREDRAW | CS_VREDRAW; |
| 42 | + wndclass.lpfnWndProc = WndProc; |
| 43 | + wndclass.cbClsExtra = 0; |
| 44 | + wndclass.cbWndExtra = 0; |
| 45 | + wndclass.hInstance = inst; |
| 46 | + wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); |
| 47 | + wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 48 | + wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
| 49 | + wndclass.lpszMenuName = NULL; |
| 50 | + wndclass.lpszClassName = lpszAppName; |
| 51 | + wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); |
| 52 | + |
| 53 | + RegisterClassEx(&wndclass) ; |
| 54 | + |
| 55 | + wnd = CreateWindow(lpszAppName, "The Hello World", |
| 56 | + WS_OVERLAPPEDWINDOW, |
| 57 | + CW_USEDEFAULT, CW_USEDEFAULT, |
| 58 | + CW_USEDEFAULT, CW_USEDEFAULT, |
| 59 | + NULL, NULL, inst, NULL); |
| 60 | + |
| 61 | + ShowWindow(wnd, nShowCmd); |
| 62 | + UpdateWindow(wnd); |
| 63 | + |
| 64 | + nsCOMPtr<nsIEventLoop> eventLoop; |
| 65 | + |
| 66 | + if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop", |
| 67 | + nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop)))) |
| 68 | + { |
| 69 | + ErrorBox("Failed to create event Loop"); |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + int retCode; |
| 75 | + eventLoop->Run(nsnull, nsnull, nsnull, &retCode); |
| 76 | + |
| 77 | + InfoBox("Hello World app is out of loop"); |
| 78 | + |
| 79 | + NS_ShutdownXPCOM(servMgr); |
| 80 | + InfoBox("Hello World app is exiting"); |
| 81 | + return retCode; |
| 82 | +} |
| 83 | + |
| 84 | +LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 85 | +{ |
| 86 | + HDC hdc; |
| 87 | + PAINTSTRUCT ps; |
| 88 | + RECT rect; |
| 89 | + |
| 90 | + switch(msg) |
| 91 | + { |
| 92 | + case WM_PAINT: |
| 93 | + hdc = BeginPaint(wnd, &ps); |
| 94 | + |
| 95 | + GetClientRect(wnd, &rect); |
| 96 | + |
| 97 | + DrawText(hdc, "Hello, XP Event Loop!", -1, &rect, |
| 98 | + DT_SINGLELINE | DT_CENTER | DT_VCENTER); |
| 99 | + |
| 100 | + EndPaint(wnd, &ps); |
| 101 | + return 0; |
| 102 | + |
| 103 | + case WM_DESTROY: |
| 104 | + { |
| 105 | + nsresult rv; |
| 106 | + NS_WITH_SERVICE(nsINativeApp, nativeAppService, kNativeAppCID, &rv); |
| 107 | + if(NS_FAILED(rv)) |
| 108 | + { |
| 109 | + ErrorBox("Could not get NativeAppService"); |
| 110 | + return 0; |
| 111 | + } |
| 112 | + nsCOMPtr<nsIEventLoop> eventLoop; |
| 113 | + |
| 114 | + if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop", |
| 115 | + getter_AddRefs(eventLoop)))) |
| 116 | + { |
| 117 | + ErrorBox("Failed to find event Loop"); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + eventLoop->Exit(0); |
| 121 | + } |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + return DefWindowProc(wnd, msg, wParam, lParam); |
| 126 | +} |
0 commit comments