1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is the Mozilla browser.
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.
24 * Travis Bogard <travis@netscape.com>
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsIServiceManager.h"
42 #include "nsCNativeApp.h"
43 #include "nsIEventLoop.h"
46 static NS_DEFINE_CID(kNativeAppCID
, NS_NATIVE_APP_CID
);
48 LRESULT CALLBACK
WndProc(HWND wnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
);
50 void ErrorBox(LPSTR text
)
52 MessageBox(NULL
, text
, "XP Event Loop", MB_OK
| MB_ICONSTOP
);
55 void InfoBox(LPSTR text
)
57 MessageBox(NULL
, text
, "XP Event Loop", MB_OK
| MB_ICONINFORMATION
);
60 int WINAPI
WinMain(HINSTANCE inst
,
61 HINSTANCE prevInstance
,
65 char* lpszAppName
= "HelloWorld";
70 { // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown
72 nsCOMPtr
<nsIServiceManager
> servMan
;
73 rv
= NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
76 ErrorBox("Failed to initialize xpcom.");
80 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
81 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
82 registrar
->AutoRegister(nsnull
);
84 nsCOMPtr
<nsINativeApp
> nativeAppService(do_GetService(kNativeAppCID
, &rv
));
88 ErrorBox("Failed to get nativeAppService");
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
);
104 RegisterClassEx(&wndclass
) ;
106 wnd
= CreateWindow(lpszAppName
, "The Hello World",
108 CW_USEDEFAULT
, CW_USEDEFAULT
,
109 CW_USEDEFAULT
, CW_USEDEFAULT
,
110 NULL
, NULL
, inst
, NULL
);
112 ShowWindow(wnd
, nShowCmd
);
115 nsCOMPtr
<nsIEventLoop
> eventLoop
;
117 if(NS_FAILED(nativeAppService
->CreateEventLoop(L
"_MainLoop",
118 nsEventLoopTypes::MainAppLoop
, getter_AddRefs(eventLoop
))))
120 ErrorBox("Failed to create event Loop");
124 eventLoop
->Run(nsnull
, nsnull
, nsnull
, &retCode
);
125 eventLoop
= nsnull
; // Clear out before Shutting down XPCOM
127 InfoBox("Hello World app is out of loop");
129 NS_ShutdownXPCOM(nsnull
);
130 InfoBox("Hello World app is exiting");
134 LRESULT CALLBACK
WndProc(HWND wnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
143 hdc
= BeginPaint(wnd
, &ps
);
145 GetClientRect(wnd
, &rect
);
147 DrawText(hdc
, "Hello, XP Event Loop!", -1, &rect
,
148 DT_SINGLELINE
| DT_CENTER
| DT_VCENTER
);
156 nsCOMPtr
<nsINativeApp
> nativeAppService
=
157 do_GetService(kNativeAppCID
, &rv
);
160 ErrorBox("Could not get NativeAppService");
163 nsCOMPtr
<nsIEventLoop
> eventLoop
;
165 if(NS_FAILED(nativeAppService
->FindEventLoop(L
"_MainLoop",
166 getter_AddRefs(eventLoop
))))
168 ErrorBox("Failed to find event Loop");
176 return DefWindowProc(wnd
, msg
, wParam
, lParam
);