merge nsToolkit and nsToolkitBase in cocoa widgets. b=363117 r=mento sr=pinkerton

This commit is contained in:
joshmoz%gmail.com 2006-12-08 08:29:46 +00:00
Родитель d2ae459df8
Коммит c593ecdf2f
3 изменённых файлов: 255 добавлений и 34 удалений

Просмотреть файл

@ -91,7 +91,6 @@ EXPORTS = \
$(NULL)
MAC_LCPPSRCS = \
nsToolkitBase.cpp \
nsDeviceContextSpecFactoryM.cpp \
nsDeviceContextSpecX.cpp \
nsPrintOptionsX.cpp \

Просмотреть файл

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -35,10 +36,10 @@
*
* ***** END LICENSE BLOCK ***** */
#ifndef TOOLKIT_H
#define TOOLKIT_H
#include "nsIToolkit.h"
#include "nsIObserver.h"
#include "nsToolkitBase.h"
#include <IOKit/IOKitLib.h>
/**
* The toolkit abstraction is necessary because the message pump must
@ -63,28 +64,40 @@
* per nsToolkit.
*/
struct PRThread;
#define MAC_OS_X_VERSION_10_0_HEX 0x00001000
#define MAC_OS_X_VERSION_10_1_HEX 0x00001010
#define MAC_OS_X_VERSION_10_2_HEX 0x00001020
#define MAC_OS_X_VERSION_10_3_HEX 0x00001030
#define MAC_OS_X_VERSION_10_4_HEX 0x00001040
class nsToolkit : public nsToolkitBase
class nsToolkit : public nsIToolkit, public nsIObserver
{
public:
nsToolkit();
virtual ~nsToolkit();
// Returns the OS X version as returned from
// Gestalt(gestaltSystemVersion, ...)
NS_DECL_ISUPPORTS
NS_DECL_NSITOOLKIT
NS_DECL_NSIOBSERVER
// Returns the OS X version as returned from Gestalt(gestaltSystemVersion, ...)
static long OSXVersion();
static void PostSleepWakeNotification(const char* aNotification);
protected:
virtual nsresult InitEventQueue(PRThread * aThread);
nsresult RegisterForSleepWakeNotifcations();
void RemoveSleepWakeNotifcations();
protected:
static void SetupQuartzRendering();
protected:
bool mInited;
CFRunLoopSourceRef mSleepWakeNotificationRLS;
io_object_t mPowerNotifier;
};
#endif // TOOLKIT_H
extern nsToolkit* NS_CreateToolkitInstance();

Просмотреть файл

@ -20,6 +20,8 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Simon Fraser <sfraser@netscape.com>
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -37,43 +39,250 @@
#include "nsToolkit.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <mach/mach_port.h>
#include <mach/mach_interface.h>
#include <mach/mach_init.h>
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOMessage.h>
#include <CoreFoundation/CoreFoundation.h>
#include <Carbon/Carbon.h>
#include "nsWidgetAtoms.h"
#include "nsIObserverService.h"
#include "nsIServiceManager.h"
#include "nsIPrefBranch2.h"
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include <Gestalt.h>
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
static io_connect_t gRootPort = MACH_PORT_NULL;
static const char kQuartzRenderingPref[] = "browser.quartz.enable";
static const char kAllFontSizesPref[] = "browser.quartz.enable.all_font_sizes";
// Static thread local storage index of the Toolkit
// object associated with a given thread...
static PRUintn gToolkitTLSIndex = 0;
nsToolkit::nsToolkit()
: mInited(false)
, mSleepWakeNotificationRLS(nsnull)
{
}
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
nsToolkit::~nsToolkit()
{
RemoveSleepWakeNotifcations();
// Remove the TLS reference to the toolkit...
PR_SetThreadPrivate(gToolkitTLSIndex, nsnull);
}
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
nsresult
nsToolkit::InitEventQueue(PRThread * aThread)
NS_IMPL_THREADSAFE_ISUPPORTS2(nsToolkit, nsIToolkit, nsIObserver);
NS_IMETHODIMP
nsToolkit::Init(PRThread * aThread)
{
// nothing to do
nsWidgetAtoms::RegisterAtoms();
mInited = true;
RegisterForSleepWakeNotifcations();
SetupQuartzRendering();
nsCOMPtr<nsIPrefBranch2> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (prefs) {
prefs->AddObserver(kQuartzRenderingPref, this, PR_FALSE);
prefs->AddObserver(kAllFontSizesPref, this, PR_FALSE);
}
return NS_OK;
}
//-------------------------------------------------------------------------
//
//-------------------------------------------------------------------------
nsToolkitBase* NS_CreateToolkitInstance()
// The pref changed, reset the app to use quartz rendering as dictated by the pref
NS_IMETHODIMP
nsToolkit::Observe(nsISupports* aSubject,
const char* aTopic,
const PRUnichar* aData)
{
SetupQuartzRendering();
return NS_OK;
}
nsToolkit* NS_CreateToolkitInstance()
{
return new nsToolkit();
}
void
nsToolkit::PostSleepWakeNotification(const char* aNotification)
{
nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1");
if (observerService)
observerService->NotifyObservers(nsnull, aNotification, nsnull);
}
// http://developer.apple.com/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/PowerMgmt/chapter_10_section_3.html
static void ToolkitSleepWakeCallback(void *refCon, io_service_t service, natural_t messageType, void * messageArgument)
{
switch (messageType)
{
case kIOMessageSystemWillSleep:
// System is going to sleep now.
nsToolkit::PostSleepWakeNotification("sleep_notification");
::IOAllowPowerChange(gRootPort, (long)messageArgument);
break;
case kIOMessageCanSystemSleep:
// In this case, the computer has been idle for several minutes
// and will sleep soon so you must either allow or cancel
// this notification. Important: if you dont respond, there will
// be a 30-second timeout before the computer sleeps.
// In Mozilla's case, we always allow sleep.
::IOAllowPowerChange(gRootPort,(long)messageArgument);
break;
case kIOMessageSystemHasPoweredOn:
// Handle wakeup.
nsToolkit::PostSleepWakeNotification("wake_notification");
break;
}
}
nsresult
nsToolkit::RegisterForSleepWakeNotifcations()
{
IONotificationPortRef notifyPortRef;
NS_ASSERTION(!mSleepWakeNotificationRLS, "Already registered for sleep/wake");
gRootPort = ::IORegisterForSystemPower(0, &notifyPortRef, ToolkitSleepWakeCallback, &mPowerNotifier);
if (gRootPort == MACH_PORT_NULL) {
NS_ASSERTION(0, "IORegisterForSystemPower failed");
return NS_ERROR_FAILURE;
}
mSleepWakeNotificationRLS = ::IONotificationPortGetRunLoopSource(notifyPortRef);
::CFRunLoopAddSource(::CFRunLoopGetCurrent(),
mSleepWakeNotificationRLS,
kCFRunLoopDefaultMode);
return NS_OK;
}
void
nsToolkit::RemoveSleepWakeNotifcations()
{
if (mSleepWakeNotificationRLS) {
::IODeregisterForSystemPower(&mPowerNotifier);
::CFRunLoopRemoveSource(::CFRunLoopGetCurrent(),
mSleepWakeNotificationRLS,
kCFRunLoopDefaultMode);
mSleepWakeNotificationRLS = nsnull;
}
}
// SetupQuartzRendering
//
// Use apple's technote for 10.1.5 to turn on quartz rendering with CG metrics. This
// slows us down about 12% when turned on.
void
nsToolkit::SetupQuartzRendering()
{
// from Apple's technote at http://developer.apple.com/qa/qa2001/qa1193.html
enum {
kQDDontChangeFlags = 0xFFFFFFFF, // don't change anything
kQDUseDefaultTextRendering = 0, // bit 0
kQDUseTrueTypeScalerGlyphs = (1 << 0), // bit 1
kQDUseCGTextRendering = (1 << 1), // bit 2
kQDUseCGTextMetrics = (1 << 2)
};
const int kFlagsWeUse = kQDUseCGTextRendering | kQDUseCGTextMetrics;
// turn on quartz rendering if we find the symbol in the app framework. Just turn
// on the bits that we need, don't turn off what someone else might have wanted. If
// the pref isn't found, assume we want it on. That way, we have to explicitly put
// in a pref to disable it, rather than force everyone who wants it to carry around
// an extra pref.
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (!prefs)
return;
PRBool enableQuartz = PR_TRUE;
nsresult rv = prefs->GetBoolPref(kQuartzRenderingPref, &enableQuartz);
UInt32 oldFlags = QDSwapTextFlags(kQDDontChangeFlags);
if (NS_FAILED(rv) || enableQuartz) {
QDSwapTextFlags(oldFlags | kFlagsWeUse);
// the system defaults to not anti-aliasing small fonts, but some people
// think it looks better that way. If the pref is set, turn them on
PRBool antiAliasAllFontSizes = PR_FALSE;
rv = prefs->GetBoolPref(kAllFontSizesPref, &antiAliasAllFontSizes);
if (NS_SUCCEEDED(rv) && antiAliasAllFontSizes)
SetOutlinePreferred(true);
}
else {
QDSwapTextFlags(oldFlags & !kFlagsWeUse);
}
}
// Return the nsIToolkit for the current thread. If a toolkit does not
// yet exist, then one will be created...
NS_METHOD NS_GetCurrentToolkit(nsIToolkit* *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nsnull;
// Create the TLS index the first time through...
if (gToolkitTLSIndex == 0) {
PRStatus status = PR_NewThreadPrivateIndex(&gToolkitTLSIndex, NULL);
if (PR_FAILURE == status)
return NS_ERROR_FAILURE;
}
// Create a new toolkit for this thread...
nsToolkit* toolkit = (nsToolkit*)PR_GetThreadPrivate(gToolkitTLSIndex);
if (!toolkit) {
toolkit = NS_CreateToolkitInstance();
if (!toolkit)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(toolkit);
toolkit->Init(PR_GetCurrentThread());
//
// The reference stored in the TLS is weak. It is removed in the
// nsToolkit destructor...
//
PR_SetThreadPrivate(gToolkitTLSIndex, (void*)toolkit);
}
else {
NS_ADDREF(toolkit);
}
*aResult = toolkit;
return NS_OK;
}
long nsToolkit::OSXVersion()
{