|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "nsIServiceManager.h" |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsCNativeApp.h" |
|
10 #include "nsIEventLoop.h" |
|
11 #include <windows.h> |
|
12 |
|
13 static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID); |
|
14 |
|
15 LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam); |
|
16 |
|
17 void ErrorBox(LPSTR text) |
|
18 { |
|
19 MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONSTOP); |
|
20 } |
|
21 |
|
22 void InfoBox(LPSTR text) |
|
23 { |
|
24 MessageBox(nullptr, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION); |
|
25 } |
|
26 |
|
27 int WINAPI WinMain(HINSTANCE inst, |
|
28 HINSTANCE prevInstance, |
|
29 LPSTR lpszCmdLine, |
|
30 int nShowCmd) |
|
31 { |
|
32 char* lpszAppName = "HelloWorld"; |
|
33 HWND wnd; |
|
34 WNDCLASSEX wndclass; |
|
35 int retCode; |
|
36 |
|
37 { // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown |
|
38 nsresult rv; |
|
39 nsCOMPtr<nsIServiceManager> servMan; |
|
40 rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
|
41 if(NS_FAILED(rv)) |
|
42 { |
|
43 ErrorBox("Failed to initialize xpcom."); |
|
44 return -1; |
|
45 } |
|
46 |
|
47 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); |
|
48 NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); |
|
49 registrar->AutoRegister(nullptr); |
|
50 |
|
51 nsCOMPtr<nsINativeApp> nativeAppService(do_GetService(kNativeAppCID, &rv)); |
|
52 |
|
53 if(NS_FAILED(rv)) |
|
54 { |
|
55 ErrorBox("Failed to get nativeAppService"); |
|
56 return -1; |
|
57 } |
|
58 wndclass.cbSize = sizeof(wndclass); |
|
59 wndclass.style = CS_HREDRAW | CS_VREDRAW; |
|
60 wndclass.lpfnWndProc = WndProc; |
|
61 wndclass.cbClsExtra = 0; |
|
62 wndclass.cbWndExtra = 0; |
|
63 wndclass.hInstance = inst; |
|
64 wndclass.hIcon = LoadIcon(nullptr, IDI_APPLICATION); |
|
65 wndclass.hCursor = LoadCursor(nullptr, IDC_ARROW); |
|
66 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); |
|
67 wndclass.lpszMenuName = nullptr; |
|
68 wndclass.lpszClassName = lpszAppName; |
|
69 wndclass.hIconSm = LoadIcon(nullptr, IDI_APPLICATION); |
|
70 |
|
71 RegisterClassEx(&wndclass) ; |
|
72 |
|
73 wnd = CreateWindow(lpszAppName, "The Hello World", |
|
74 WS_OVERLAPPEDWINDOW, |
|
75 CW_USEDEFAULT, CW_USEDEFAULT, |
|
76 CW_USEDEFAULT, CW_USEDEFAULT, |
|
77 nullptr, nullptr, inst, nullptr); |
|
78 |
|
79 ShowWindow(wnd, nShowCmd); |
|
80 UpdateWindow(wnd); |
|
81 |
|
82 nsCOMPtr<nsIEventLoop> eventLoop; |
|
83 |
|
84 if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop", |
|
85 nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop)))) |
|
86 { |
|
87 ErrorBox("Failed to create event Loop"); |
|
88 return 0; |
|
89 } |
|
90 |
|
91 eventLoop->Run(nullptr, nullptr, nullptr, &retCode); |
|
92 eventLoop = nullptr; // Clear out before Shutting down XPCOM |
|
93 |
|
94 InfoBox("Hello World app is out of loop"); |
|
95 } |
|
96 NS_ShutdownXPCOM(nullptr); |
|
97 InfoBox("Hello World app is exiting"); |
|
98 return retCode; |
|
99 } |
|
100 |
|
101 LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|
102 { |
|
103 HDC hdc; |
|
104 PAINTSTRUCT ps; |
|
105 RECT rect; |
|
106 |
|
107 switch(msg) |
|
108 { |
|
109 case WM_PAINT: |
|
110 hdc = BeginPaint(wnd, &ps); |
|
111 |
|
112 GetClientRect(wnd, &rect); |
|
113 |
|
114 DrawText(hdc, "Hello, XP Event Loop!", -1, &rect, |
|
115 DT_SINGLELINE | DT_CENTER | DT_VCENTER); |
|
116 |
|
117 EndPaint(wnd, &ps); |
|
118 return 0; |
|
119 |
|
120 case WM_DESTROY: |
|
121 { |
|
122 nsresult rv; |
|
123 nsCOMPtr<nsINativeApp> nativeAppService = |
|
124 do_GetService(kNativeAppCID, &rv); |
|
125 if(NS_FAILED(rv)) |
|
126 { |
|
127 ErrorBox("Could not get NativeAppService"); |
|
128 return 0; |
|
129 } |
|
130 nsCOMPtr<nsIEventLoop> eventLoop; |
|
131 |
|
132 if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop", |
|
133 getter_AddRefs(eventLoop)))) |
|
134 { |
|
135 ErrorBox("Failed to find event Loop"); |
|
136 return 0; |
|
137 } |
|
138 eventLoop->Exit(0); |
|
139 } |
|
140 return 0; |
|
141 } |
|
142 |
|
143 return DefWindowProc(wnd, msg, wParam, lParam); |
|
144 } |