gecko-dev/content/shared/public/nsHTMLValue.h

169 строки
5.3 KiB
C
Исходник Обычный вид История

1998-04-14 00:24:54 +04:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape 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/NPL/
1998-04-14 00:24:54 +04:00
*
* 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.
1998-04-14 00:24:54 +04:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
1998-04-14 00:24:54 +04:00
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
1998-04-14 00:24:54 +04:00
*/
#ifndef nsHTMLValue_h___
#define nsHTMLValue_h___
#include "nscore.h"
#include "nsColor.h"
#include "nsString.h"
#include "nsISupports.h"
enum nsHTMLUnit {
eHTMLUnit_Null = 0, // (n/a) null unit, value is not specified
eHTMLUnit_Empty = 1, // (n/a) empty unit, value is not specified
eHTMLUnit_String = 10, // (nsString) a string value
eHTMLUnit_ISupports = 20, // (nsISupports*) a ref counted interface
eHTMLUnit_Integer = 50, // (int) simple value
eHTMLUnit_Enumerated = 51, // (int) value has enumerated meaning
eHTMLUnit_Proportional = 52, // (int) value is a relative proportion of some whole
eHTMLUnit_Color = 80, // (color) an RGBA value
1999-07-07 05:31:34 +04:00
eHTMLUnit_ColorName = 81, // (nsString/color) a color name value
eHTMLUnit_Percent = 90, // (float) 1.0 == 100%) value is percentage of something
1998-04-14 00:24:54 +04:00
// Screen relative measure
1998-05-20 03:11:28 +04:00
eHTMLUnit_Pixel = 600 // (int) screen pixels
1998-04-14 00:24:54 +04:00
};
class nsHTMLValue {
public:
1998-04-30 23:56:39 +04:00
nsHTMLValue(nsHTMLUnit aUnit = eHTMLUnit_Null);
nsHTMLValue(PRInt32 aValue, nsHTMLUnit aUnit);
nsHTMLValue(float aValue);
1999-07-07 05:31:34 +04:00
nsHTMLValue(const nsString& aValue, nsHTMLUnit aUnit = eHTMLUnit_String);
1998-04-14 00:24:54 +04:00
nsHTMLValue(nsISupports* aValue);
nsHTMLValue(nscolor aValue);
nsHTMLValue(const nsHTMLValue& aCopy);
~nsHTMLValue(void);
nsHTMLValue& operator=(const nsHTMLValue& aCopy);
PRBool operator==(const nsHTMLValue& aOther) const;
1999-08-08 05:18:40 +04:00
PRBool operator!=(const nsHTMLValue& aOther) const;
PRUint32 HashValue(void) const;
1998-04-14 00:24:54 +04:00
nsHTMLUnit GetUnit(void) const { return mUnit; }
PRInt32 GetIntValue(void) const;
1998-04-30 23:56:39 +04:00
PRInt32 GetPixelValue(void) const;
float GetPercentValue(void) const;
1998-04-14 00:24:54 +04:00
nsString& GetStringValue(nsString& aBuffer) const;
nsISupports* GetISupportsValue(void) const;
nscolor GetColorValue(void) const;
void Reset(void);
1998-04-30 23:56:39 +04:00
void SetIntValue(PRInt32 aValue, nsHTMLUnit aUnit);
void SetPixelValue(PRInt32 aValue);
void SetPercentValue(float aValue);
1999-07-07 05:31:34 +04:00
void SetStringValue(const nsString& aValue, nsHTMLUnit aUnit = eHTMLUnit_String);
void SetISupportsValue(nsISupports* aValue);
1998-04-30 23:56:39 +04:00
void SetColorValue(nscolor aValue);
void SetEmptyValue(void);
1998-04-14 00:24:54 +04:00
void AppendToString(nsString& aBuffer) const;
void ToString(nsString& aBuffer) const;
protected:
nsHTMLUnit mUnit;
union {
PRInt32 mInt;
float mFloat;
nsString* mString;
nsISupports* mISupports;
nscolor mColor;
} mValue;
};
inline PRInt32 nsHTMLValue::GetIntValue(void) const
{
1998-04-30 23:56:39 +04:00
NS_ASSERTION((mUnit == eHTMLUnit_Integer) ||
(mUnit == eHTMLUnit_Enumerated) ||
(mUnit == eHTMLUnit_Proportional), "not an int value");
1998-04-30 23:56:39 +04:00
if ((mUnit == eHTMLUnit_Integer) ||
(mUnit == eHTMLUnit_Enumerated) ||
(mUnit == eHTMLUnit_Proportional)) {
1998-04-14 00:24:54 +04:00
return mValue.mInt;
}
return 0;
}
1998-04-30 23:56:39 +04:00
inline PRInt32 nsHTMLValue::GetPixelValue(void) const
1998-04-14 00:24:54 +04:00
{
1998-04-30 23:56:39 +04:00
NS_ASSERTION((mUnit == eHTMLUnit_Pixel), "not a pixel value");
if (mUnit == eHTMLUnit_Pixel) {
return mValue.mInt;
}
return 0;
}
inline float nsHTMLValue::GetPercentValue(void) const
{
NS_ASSERTION(mUnit == eHTMLUnit_Percent, "not a percent value");
1998-04-14 00:24:54 +04:00
if (mUnit == eHTMLUnit_Percent) {
return mValue.mFloat;
}
return 0.0f;
}
inline nsString& nsHTMLValue::GetStringValue(nsString& aBuffer) const
{
1999-07-07 05:31:34 +04:00
NS_ASSERTION((mUnit == eHTMLUnit_String) || (mUnit == eHTMLUnit_ColorName) ||
(mUnit == eHTMLUnit_Null), "not a string value");
1998-04-14 00:24:54 +04:00
aBuffer.SetLength(0);
1999-07-07 05:31:34 +04:00
if (((mUnit == eHTMLUnit_String) || (mUnit == eHTMLUnit_ColorName)) &&
(nsnull != mValue.mString)) {
1998-04-14 00:24:54 +04:00
aBuffer.Append(*(mValue.mString));
}
return aBuffer;
}
inline nsISupports* nsHTMLValue::GetISupportsValue(void) const
{
NS_ASSERTION(mUnit == eHTMLUnit_ISupports, "not an ISupports value");
if (mUnit == eHTMLUnit_ISupports) {
NS_IF_ADDREF(mValue.mISupports);
return mValue.mISupports;
}
return nsnull;
}
inline nscolor nsHTMLValue::GetColorValue(void) const
{
1999-07-07 05:31:34 +04:00
NS_ASSERTION((mUnit == eHTMLUnit_Color) || (mUnit == eHTMLUnit_ColorName),
"not a color value");
1998-04-14 00:24:54 +04:00
if (mUnit == eHTMLUnit_Color) {
return mValue.mColor;
}
1999-07-07 05:31:34 +04:00
if ((mUnit == eHTMLUnit_ColorName) && (mValue.mString)) {
nscolor color;
1999-07-18 04:30:30 +04:00
if (NS_ColorNameToRGB(*(mValue.mString), &color)) {
1999-07-07 05:31:34 +04:00
return color;
}
}
1998-04-14 00:24:54 +04:00
return NS_RGB(0,0,0);
}
1999-08-08 05:18:40 +04:00
inline PRBool nsHTMLValue::operator!=(const nsHTMLValue& aOther) const
{
return PRBool(! ((*this) == aOther));
}
1998-04-14 00:24:54 +04:00
#endif /* nsHTMLValue_h___ */