gecko-dev/xpcom/tests/windows/TestHelloXPLoop.cpp

155 строки
4.0 KiB
C++
Исходник Обычный вид История

/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is the Mozilla browser.
*
* The Initial Developer of the Original Code is Netscape
* Communications, Inc. Portions created by Netscape are
* Copyright (C) 1999, Mozilla. All Rights Reserved.
*
* Contributor(s):
* Travis Bogard <travis@netscape.com>
*/
#include "nsIServiceManager.h"
#include "nsCOMPtr.h"
#include "nsCNativeApp.h"
#include "nsIEventLoop.h"
#include <windows.h>
static NS_DEFINE_CID(kNativeAppCID, NS_NATIVE_APP_CID);
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam);
void ErrorBox(LPSTR text)
{
MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONSTOP);
}
void InfoBox(LPSTR text)
{
MessageBox(NULL, text, "XP Event Loop", MB_OK | MB_ICONINFORMATION);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prevInstance, LPSTR lpszCmdLine,
int nShowCmd)
{
char* lpszAppName = "HelloWorld";
HWND wnd;
WNDCLASSEX wndclass;
int retCode;
nsresult rv;
I know it's unorthodox to do a top level checkin like this, but I've got so many files in so many different directories, that I think it's the best way. I've pulled and clobber_all'd my tree and got r=dp on this checkin. Here are the touched files: M mozilla/embedding/browser/activex/src/control/MozillaBrowser.cpp M mozilla/embedding/browser/activex/src/control/MozillaBrowser.h M mozilla/js/src/xpconnect/shell/xpcshell.cpp M mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp M mozilla/xpcom/build/nsXPComInit.cpp M mozilla/xpcom/components/nsComponentManager.cpp M mozilla/xpcom/components/nsIServiceManager.h M mozilla/xpcom/components/nsServiceManager.cpp M mozilla/xpcom/io/nsSpecialSystemDirectory.cpp M mozilla/xpcom/io/nsSpecialSystemDirectory.h M mozilla/xpcom/tests/TestBuffers.cpp M mozilla/xpcom/tests/TestPipes.cpp M mozilla/xpcom/tests/TestShutdown.cpp M mozilla/xpcom/tests/windows/TestHelloXPLoop.cpp M mozilla/xpcom/tools/registry/regExport.cpp M mozilla/xpcom/tools/registry/regxpcom.cpp M mozilla/xpinstall/stub/xpistub.cpp M mozilla/webshell/embed/ActiveX/MozillaBrowser.cpp M mozilla/webshell/embed/ActiveX/MozillaBrowser.h M mozilla/webshell/tests/viewer/nsMacMain.cpp M mozilla/webshell/tests/viewer/nsPhMain.cpp M mozilla/webshell/tests/viewer/nsWinMain.cpp M mozilla/webshell/tests/viewer/unix/gtk/nsGtkMain.cpp M mozilla/xpfe/appshell/src/nsFileLocations.cpp M mozilla/xpfe/bootstrap/nsAppRunner.cpp The heart of this checkin is a change in the signature and symantics of NS_InitXPCOM. The new signature is extern NS_COM nsresult NS_InitXPCOM(nsIServiceManager* *result, nsFileSpec* binDirectory); I filed a bug for this problem: b=23157 The original manifestation of this bug was in mozilla/netwerk/protocol/res/src/nsResProtocolHandler.cpp It used the current process directory to find resources, which is not correct when the current process is not mozilla.exe. I have added a new type to nsSpecialSystemDirectory, Moz_BinDirectory, and made nsResProtocolHandler use that value.
2000-01-06 04:05:13 +03:00
rv = NS_InitXPCOM(NULL, NULL);
{ // Needed to scope all nsCOMPtr within XPCOM Init and Shutdown
if(NS_FAILED(rv))
{
ErrorBox("Failed to initalize xpcom.");
return -1;
}
nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, nsnull);
NS_WITH_SERVICE(nsINativeApp, nativeAppService, kNativeAppCID, &rv);
if(NS_FAILED(rv))
{
ErrorBox("Failed to get nativeAppService");
return -1;
}
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = inst;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass) ;
wnd = CreateWindow(lpszAppName, "The Hello World",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, inst, NULL);
ShowWindow(wnd, nShowCmd);
UpdateWindow(wnd);
nsCOMPtr<nsIEventLoop> eventLoop;
if(NS_FAILED(nativeAppService->CreateEventLoop(L"_MainLoop",
nsEventLoopTypes::MainAppLoop, getter_AddRefs(eventLoop))))
{
ErrorBox("Failed to create event Loop");
return 0;
}
eventLoop->Run(nsnull, nsnull, nsnull, &retCode);
eventLoop = nsnull; // Clear out before Shutting down XPCOM
InfoBox("Hello World app is out of loop");
}
NS_ShutdownXPCOM(nsnull);
InfoBox("Hello World app is exiting");
return retCode;
}
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
switch(msg)
{
case WM_PAINT:
hdc = BeginPaint(wnd, &ps);
GetClientRect(wnd, &rect);
DrawText(hdc, "Hello, XP Event Loop!", -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(wnd, &ps);
return 0;
case WM_DESTROY:
{
nsresult rv;
NS_WITH_SERVICE(nsINativeApp, nativeAppService, kNativeAppCID, &rv);
if(NS_FAILED(rv))
{
ErrorBox("Could not get NativeAppService");
return 0;
}
nsCOMPtr<nsIEventLoop> eventLoop;
if(NS_FAILED(nativeAppService->FindEventLoop(L"_MainLoop",
getter_AddRefs(eventLoop))))
{
ErrorBox("Failed to find event Loop");
return 0;
}
eventLoop->Exit(0);
}
return 0;
}
return DefWindowProc(wnd, msg, wParam, lParam);
}