nsresult nsXPLookAndFeel::InitFromPref(nsLookAndFeelIntPref* aPref, nsIPref* aPrefService) { PRInt32 intpref; nsresult rv = aPrefService->GetIntPref(aPref->name, &intpref); if (NS_SUCCEEDED(rv)) { aPref->isSet = PR_TRUE; aPref->intVar = intpref; } aPrefService->RegisterCallback(aPref->name, intPrefChanged, aPref); return rv; }  nsresult nsXPLookAndFeel::InitFromPref(nsLookAndFeelFloatPref* aPref, nsIPref* aPrefService) { PRInt32 intpref; nsresult rv = aPrefService->GetIntPref(aPref->name, &intpref); if (NS_SUCCEEDED(rv)) { aPref->isSet = PR_TRUE; aPref->floatVar = (float)intpref / 100.; } aPrefService->RegisterCallback(aPref->name, floatPrefChanged, aPref); return rv; }  nsresult nsXPLookAndFeel::InitFromPref(nsLookAndFeelColorPref* aPref, nsIPref* aPrefService) { char *colorStr = 0; nsresult rv = aPrefService->CopyCharPref(aPref->name, &colorStr); if (NS_SUCCEEDED(rv) && colorStr[0]) { nsAutoString colorNSStr(colorStr); nscolor thecolor; if (NS_SUCCEEDED(NS_ColorNameToRGB(colorNSStr, &thecolor))) { aPref->isSet = PR_TRUE; aPref->colorVar = (nscolor)thecolor; PL_strfree(colorStr); } }  aPrefService->RegisterCallback(aPref->name, colorPrefChanged, aPref); return rv; }        void nsXPLookAndFeel::Init() {   sInitialized = PR_TRUE;  nsresult rv; NS_WITH_SERVICE(nsIPref, prefService, kPrefServiceCID, &rv); if (NS_FAILED(rv) || !prefService) return;  unsigned int i; for (i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i) InitFromPref(&sIntPrefs[i], prefService);  for (i = 0; i < ((sizeof (sFloatPrefs) / sizeof (*sFloatPrefs))); ++i) InitFromPref(&sFloatPrefs[i], prefService);  for (i = 0; i < ((sizeof (sColorPrefs) / sizeof (*sColorPrefs))); ++i) InitFromPref(&sColorPrefs[i], prefService); }  nsXPLookAndFeel::~nsXPLookAndFeel() { }        NS_IMETHODIMP nsXPLookAndFeel::GetColor(const nsColorID aID, nscolor &aColor) { if (!sInitialized) Init();  for (unsigned int i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i) if (sColorPrefs[i].isSet && (sColorPrefs[i].id == aID)) { aColor = sColorPrefs[i].colorVar; return NS_OK; }  return NS_ERROR_NOT_AVAILABLE; }  NS_IMETHODIMP nsXPLookAndFeel::GetMetric(const nsMetricID aID, PRInt32& aMetric) { if (!sInitialized) Init();  for (unsigned int i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i) if (sIntPrefs[i].isSet && (sIntPrefs[i].id == aID)) { aMetric = sIntPrefs[i].intVar; return NS_OK; }  return NS_ERROR_NOT_AVAILABLE; }  NS_IMETHODIMP nsXPLookAndFeel::GetMetric(const nsMetricFloatID aID, float& aMetric) { if (!sInitialized) Init();  for (unsigned int i = 0; i < ((sizeof (sFloatPrefs) / sizeof (*sFloatPrefs))); ++i) if (sFloatPrefs[i].isSet && sFloatPrefs[i].id == aID) { aMetric = sFloatPrefs[i].floatVar; return NS_OK; }  return NS_ERROR_NOT_AVAILABLE; }  #ifdef NS_DEBUG     NS_IMETHODIMP nsXPLookAndFeel::GetNavSize(const nsMetricNavWidgetID aWidgetID, const nsMetricNavFontID   aFontID, const PRInt32             aFontSize, nsSize &aSize) { return NS_ERROR_NOT_AVAILABLE; } #endif  nsresult NS_NewXPLookAndFeel(nsILookAndFeel** aXPLookAndFeel) { if (!aXPLookAndFeel) return NS_ERROR_INVALID_ARG; *aXPLookAndFeel = new nsXPLookAndFeel; if (!*aXPLookAndFeel) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(*aXPLookAndFeel); return NS_OK; }
static int colorPrefChanged (const char *newpref, void *data){  nsLookAndFeelColorPref* np = (nsLookAndFeelColorPref*)data;  if (np)  {    nsresult rv;    NS_WITH_SERVICE(nsIPref, prefService, kPrefServiceCID, &rv);    if (NS_SUCCEEDED(rv) && prefService)    {      char *colorStr = 0;      rv = prefService->CopyCharPref(np->name, &colorStr);      if (NS_SUCCEEDED(rv) && colorStr[0])      {        nsString colorNSStr(colorStr);        nscolor thecolor;        if (NS_SUCCEEDED(NS_ColorNameToRGB(colorNSStr, &thecolor)))        {          np->isSet = PR_TRUE;          np->colorVar = (nscolor)thecolor;#ifdef DEBUG_akkana          printf("====== Changed color pref %s to 0x%lx\n",                 np->name, (long)np->colorVar);        }#endif        PL_strfree(colorStr);      }    }  }  return 0;}nsresultnsXPLookAndFeel::InitFromPref(nsLookAndFeelIntPref* aPref, nsIPref* aPrefService){  PRInt32 intpref;  nsresult rv = aPrefService->GetIntPref(aPref->name, &intpref);  if (NS_SUCCEEDED(rv))  {    aPref->isSet = PR_TRUE;    aPref->intVar = intpref;  }  aPrefService->RegisterCallback(aPref->name, intPrefChanged, aPref);  return rv;}nsresultnsXPLookAndFeel::InitFromPref(nsLookAndFeelFloatPref* aPref, nsIPref* aPrefService){  PRInt32 intpref;  nsresult rv = aPrefService->GetIntPref(aPref->name, &intpref);  if (NS_SUCCEEDED(rv))  {    aPref->isSet = PR_TRUE;    aPref->floatVar = (float)intpref / 100.;  }  aPrefService->RegisterCallback(aPref->name, floatPrefChanged, aPref);  return rv;}nsresultnsXPLookAndFeel::InitFromPref(nsLookAndFeelColorPref* aPref, nsIPref* aPrefService){  char *colorStr = 0;  nsresult rv = aPrefService->CopyCharPref(aPref->name, &colorStr);  if (NS_SUCCEEDED(rv) && colorStr[0])  {    nsAutoString colorNSStr(colorStr);    nscolor thecolor;    if (NS_SUCCEEDED(NS_ColorNameToRGB(colorNSStr, &thecolor)))    {      aPref->isSet = PR_TRUE;      aPref->colorVar = (nscolor)thecolor;      PL_strfree(colorStr);    }  }  aPrefService->RegisterCallback(aPref->name, colorPrefChanged, aPref);  return rv;}//// Read values from the user's preferences.// This is done once at startup, but since the user's preferences// haven't actually been read yet at that time, we also have to// set a callback to inform us of changes to each pref.//voidnsXPLookAndFeel::Init(){  // Say we're already initialized, and take the chance that it might fail;  // protects against some other process writing to our static variables.  sInitialized = PR_TRUE;  nsresult rv;  NS_WITH_SERVICE(nsIPref, prefService, kPrefServiceCID, &rv);  if (NS_FAILED(rv) || !prefService)    return;  unsigned int i;  for (i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i)    InitFromPref(&sIntPrefs[i], prefService);  for (i = 0; i < ((sizeof (sFloatPrefs) / sizeof (*sFloatPrefs))); ++i)    InitFromPref(&sFloatPrefs[i], prefService);  for (i = 0; i < ((sizeof (sColorPrefs) / sizeof (*sColorPrefs))); ++i)    InitFromPref(&sColorPrefs[i], prefService);}nsXPLookAndFeel::~nsXPLookAndFeel(){}//// All these routines will return NS_OK if they have a value,// in which case the nsLookAndFeel should use that value;// otherwise we'll return NS_ERROR_NOT_AVAILABLE, in which case, the// platform-specific nsLookAndFeel should use its own values instead.//NS_IMETHODIMP nsXPLookAndFeel::GetColor(const nsColorID aID, nscolor &aColor){  if (!sInitialized)    Init();  for (unsigned int i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i)    if (sColorPrefs[i].isSet && (sColorPrefs[i].id == aID))    {      aColor = sColorPrefs[i].colorVar;      return NS_OK;    }  return NS_ERROR_NOT_AVAILABLE;}  NS_IMETHODIMP nsXPLookAndFeel::GetMetric(const nsMetricID aID, PRInt32& aMetric){  if (!sInitialized)    Init();  for (unsigned int i = 0; i < ((sizeof (sIntPrefs) / sizeof (*sIntPrefs))); ++i)    if (sIntPrefs[i].isSet && (sIntPrefs[i].id == aID))    {      aMetric = sIntPrefs[i].intVar;      return NS_OK;    }  return NS_ERROR_NOT_AVAILABLE;}NS_IMETHODIMPnsXPLookAndFeel::GetMetric(const nsMetricFloatID aID, float& aMetric){  if (!sInitialized)    Init();  for (unsigned int i = 0; i < ((sizeof (sFloatPrefs) / sizeof (*sFloatPrefs))); ++i)    if (sFloatPrefs[i].isSet && sFloatPrefs[i].id == aID)    {      aMetric = sFloatPrefs[i].floatVar;      return NS_OK;    }  return NS_ERROR_NOT_AVAILABLE;}#ifdef NS_DEBUG  // This method returns the actual (or nearest estimate)   // of the Navigator size for a given form control for a given font  // and font size. This is used in NavQuirks mode to see how closely  // we match its sizeNS_IMETHODIMPnsXPLookAndFeel::GetNavSize(const nsMetricNavWidgetID aWidgetID,                            const nsMetricNavFontID   aFontID,                             const PRInt32             aFontSize,                             nsSize &aSize){  return NS_ERROR_NOT_AVAILABLE;}#endifnsresultNS_NewXPLookAndFeel(nsILookAndFeel** aXPLookAndFeel){  if (!aXPLookAndFeel)    return NS_ERROR_INVALID_ARG;  *aXPLookAndFeel = new nsXPLookAndFeel;  if (!*aXPLookAndFeel)    return NS_ERROR_OUT_OF_MEMORY;  NS_ADDREF(*aXPLookAndFeel);  return NS_OK;}