1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
---|
2 | *
|
---|
3 | * ***** BEGIN LICENSE BLOCK *****
|
---|
4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
7 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
8 | * the License. You may obtain a copy of the License at
|
---|
9 | * http://www.mozilla.org/MPL/
|
---|
10 | *
|
---|
11 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
13 | * for the specific language governing rights and limitations under the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * The Original Code is the Mozilla browser.
|
---|
17 | *
|
---|
18 | * The Initial Developer of the Original Code is
|
---|
19 | * Netscape Communications, Inc.
|
---|
20 | * Portions created by the Initial Developer are Copyright (C) 1999
|
---|
21 | * the Initial Developer. All Rights Reserved.
|
---|
22 | *
|
---|
23 | * Contributor(s):
|
---|
24 | * Travis Bogard <[email protected]>
|
---|
25 | *
|
---|
26 | * Alternatively, the contents of this file may be used under the terms of
|
---|
27 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
28 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
29 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
30 | * of those above. If you wish to allow use of your version of this file only
|
---|
31 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
32 | * use your version of this file under the terms of the MPL, indicate your
|
---|
33 | * decision by deleting the provisions above and replace them with the notice
|
---|
34 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
35 | * the provisions above, a recipient may use your version of this file under
|
---|
36 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
37 | *
|
---|
38 | * ***** END LICENSE BLOCK ***** */
|
---|
39 |
|
---|
40 | #include "nsIServiceManager.h"
|
---|
41 | #include "nsCOMPtr.h"
|
---|
42 | #include "nsCNativeApp.h"
|
---|
43 | #include "nsIEventLoop.h"
|
---|
44 | #include <windows.h>
|
---|
45 |
|
---|
46 | static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID);
|
---|
47 |
|
---|
48 | LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
---|
49 |
|
---|
50 | void ErrorBox(LPSTR text)
|
---|
51 | {
|
---|
52 | MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONSTOP);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void InfoBox(LPSTR text)
|
---|
56 | {
|
---|
57 | MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION);
|
---|
58 | }
|
---|
59 |
|
---|
60 | int WINAPI WinMain(HINSTANCE inst,
|
---|
61 | HINSTANCE prevInstance,
|
---|
62 | LPSTR lpszCmdLine,
|
---|
63 | int nShowCmd)
|
---|
64 | {
|
---|
65 | char* lpszAppName = "HelloWorld";
|
---|
66 | HWND wnd;
|
---|
67 | WNDCLASSEX wndclass;
|
---|
68 | int retCode;
|
---|
69 |
|
---|
70 | { // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown
|
---|
71 | nsresult rv;
|
---|
72 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
73 | rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
74 | if(NS_FAILED(rv))
|
---|
75 | {
|
---|
76 | ErrorBox("Failed to initalize xpcom.");
|
---|
77 | return -1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
81 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
82 | registrar->AutoRegister(nsnull);
|
---|
83 |
|
---|
84 | nsCOMPtr<nsINativeApp> nativeAppService(do_GetService(kNativeAppCID, &rv));
|
---|
85 |
|
---|
86 | if(NS_FAILED(rv))
|
---|
87 | {
|
---|
88 | ErrorBox("Failed to get nativeAppService");
|
---|
89 | return -1;
|
---|
90 | }
|
---|
91 | wndclass.cbSize = sizeof(wndclass);
|
---|
92 | wndclass.style = CS_HREDRAW | CS_VREDRAW;
|
---|
93 | wndclass.lpfnWndProc = WndProc;
|
---|
94 | wndclass.cbClsExtra = 0;
|
---|
95 | wndclass.cbWndExtra = 0;
|
---|
96 | wndclass.hInstance = inst;
|
---|
97 | wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
---|
98 | wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
---|
99 | wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
|
---|
100 | wndclass.lpszMenuName = NULL;
|
---|
101 | wndclass.lpszClassName = lpszAppName;
|
---|
102 | wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
|
---|
103 |
|
---|
104 | RegisterClassEx(&wndclass) ;
|
---|
105 |
|
---|
106 | wnd = CreateWindow(lpszAppName, "The Hello World",
|
---|
107 | WS_OVERLAPPEDWINDOW,
|
---|
108 | CW_USEDEFAULT, CW_USEDEFAULT,
|
---|
109 | CW_USEDEFAULT, CW_USEDEFAULT,
|
---|
110 | NULL, NULL, inst, NULL);
|
---|
111 |
|
---|
112 | ShowWindow(wnd, nShowCmd);
|
---|
113 | UpdateWindow(wnd);
|
---|
114 |
|
---|
115 | nsCOMPtr<nsIEventLoop> eventLoop;
|
---|
116 |
|
---|
117 | if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop",
|
---|
118 | nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop))))
|
---|
119 | {
|
---|
120 | ErrorBox("Failed to create event Loop");
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | eventLoop->Run(nsnull, nsnull, nsnull, &retCode);
|
---|
125 | eventLoop = nsnull; // Clear out before Shutting down XPCOM
|
---|
126 |
|
---|
127 | InfoBox("Hello World app is out of loop");
|
---|
128 | }
|
---|
129 | NS_ShutdownXPCOM(nsnull);
|
---|
130 | InfoBox("Hello World app is exiting");
|
---|
131 | return retCode;
|
---|
132 | }
|
---|
133 |
|
---|
134 | LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
---|
135 | {
|
---|
136 | HDC hdc;
|
---|
137 | PAINTSTRUCT ps;
|
---|
138 | RECT rect;
|
---|
139 |
|
---|
140 | switch(msg)
|
---|
141 | {
|
---|
142 | case WM_PAINT:
|
---|
143 | hdc = BeginPaint(wnd, &ps);
|
---|
144 |
|
---|
145 | GetClientRect(wnd, &rect);
|
---|
146 |
|
---|
147 | DrawText(hdc, "Hello, XP Event Loop!", -1, &rect,
|
---|
148 | DT_SINGLELINE | DT_CENTER | DT_VCENTER);
|
---|
149 |
|
---|
150 | EndPaint(wnd, &ps);
|
---|
151 | return 0;
|
---|
152 |
|
---|
153 | case WM_DESTROY:
|
---|
154 | {
|
---|
155 | nsresult rv;
|
---|
156 | nsCOMPtr<nsINativeApp> nativeAppService =
|
---|
157 | do_GetService(kNativeAppCID, &rv);
|
---|
158 | if(NS_FAILED(rv))
|
---|
159 | {
|
---|
160 | ErrorBox("Could not get NativeAppService");
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 | nsCOMPtr<nsIEventLoop> eventLoop;
|
---|
164 |
|
---|
165 | if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop",
|
---|
166 | getter_AddRefs(eventLoop))))
|
---|
167 | {
|
---|
168 | ErrorBox("Failed to find event Loop");
|
---|
169 | return 0;
|
---|
170 | }
|
---|
171 | eventLoop->Exit(0);
|
---|
172 | }
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | return DefWindowProc(wnd, msg, wParam, lParam);
|
---|
177 | }
|
---|