css_valueimpl.h

00001 /*
00002  * This file is part of the DOM implementation for KDE.
00003  *
00004  * Copyright (C) 1999-2003 Lars Knoll (knoll@kde.org)
00005  *           (C) 2004, 2005, 2006 Apple Computer, Inc.
00006  *           (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public License
00019  * along with this library; see the file COPYING.LIB.  If not, write to
00020  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022  *
00023  */
00024 #ifndef _CSS_css_valueimpl_h_
00025 #define _CSS_css_valueimpl_h_
00026 
00027 #include "dom/css_value.h"
00028 #include "dom/dom_string.h"
00029 #include "css/css_base.h"
00030 #include "misc/loader_client.h"
00031 #include "misc/shared.h"
00032 
00033 #include <qintdict.h>
00034 
00035 namespace khtml {
00036     class RenderStyle;
00037     class CachedImage;
00038 }
00039 
00040 
00041 namespace DOM {
00042 
00043 class CSSRuleImpl;
00044 class CSSValueImpl;
00045 class NodeImpl;
00046 class CounterImpl;
00047 class PairImpl;
00048 
00049 
00050 class CSSStyleDeclarationImpl : public StyleBaseImpl
00051 {
00052 public:
00053     CSSStyleDeclarationImpl(CSSRuleImpl *parentRule);
00054     CSSStyleDeclarationImpl(CSSRuleImpl *parentRule, QPtrList<CSSProperty> *lstValues);
00055     virtual ~CSSStyleDeclarationImpl();
00056 
00057     CSSStyleDeclarationImpl& operator=( const CSSStyleDeclarationImpl&);
00058 
00059     virtual unsigned long length() const;
00060     CSSRuleImpl *parentRule() const;
00061     virtual DOM::DOMString removeProperty( int propertyID, bool NonCSSHints = false );
00062     virtual bool setProperty ( int propertyId, const DOM::DOMString &value, bool important = false, bool nonCSSHint = false);
00063     virtual void setProperty ( int propertyId, int value, bool important = false, bool nonCSSHint = false);
00064     // this treats integers as pixels!
00065     // needed for conversion of html attributes
00066     virtual void setLengthProperty(int id, const DOM::DOMString &value, bool important, bool nonCSSHint = true, bool multiLength = false);
00067 
00068     // add a whole, unparsed property
00069     virtual void setProperty ( const DOMString &propertyString);
00070     virtual DOM::DOMString item ( unsigned long index ) const;
00071 
00072     DOM::DOMString cssText() const;
00073     void setCssText(DOM::DOMString str);
00074 
00075     virtual bool isStyleDeclaration() const { return true; }
00076     virtual bool parseString( const DOMString &string, bool = false );
00077 
00078     virtual CSSValueImpl *getPropertyCSSValue( int propertyID ) const;
00079     virtual DOMString getPropertyValue( int propertyID ) const;
00080     virtual bool getPropertyPriority( int propertyID ) const;
00081 
00082     QPtrList<CSSProperty> *values() const { return m_lstValues; }
00083     void setNode(NodeImpl *_node) { m_node = _node; }
00084 
00085     void setChanged();
00086 
00087     void removeCSSHints();
00088 
00089 protected:
00090     DOMString getShortHandValue( const int* properties, int number ) const;
00091     DOMString get4Values( const int* properties ) const;
00092 
00093     QPtrList<CSSProperty> *m_lstValues;
00094     NodeImpl *m_node;
00095 
00096 private:
00097     // currently not needed - make sure its not used
00098     CSSStyleDeclarationImpl(const CSSStyleDeclarationImpl& o);
00099 };
00100 
00101 class CSSValueImpl : public StyleBaseImpl
00102 {
00103 public:
00104     CSSValueImpl() : StyleBaseImpl() {}
00105 
00106     virtual unsigned short cssValueType() const = 0;
00107 
00108     virtual DOM::DOMString cssText() const = 0;
00109 
00110     virtual bool isValue() const { return true; }
00111     virtual bool isFontValue() const { return false; }
00112 };
00113 
00114 class CSSInheritedValueImpl : public CSSValueImpl
00115 {
00116 public:
00117     CSSInheritedValueImpl() : CSSValueImpl() {}
00118     virtual ~CSSInheritedValueImpl() {}
00119 
00120     virtual unsigned short cssValueType() const;
00121     virtual DOM::DOMString cssText() const;
00122 };
00123 
00124 class CSSInitialValueImpl : public CSSValueImpl
00125 {
00126 public:
00127     virtual unsigned short cssValueType() const;
00128     virtual DOM::DOMString cssText() const;
00129 };
00130 
00131 class CSSValueListImpl : public CSSValueImpl
00132 {
00133 public:
00134     CSSValueListImpl() : CSSValueImpl() {}
00135 
00136     virtual ~CSSValueListImpl();
00137 
00138     unsigned long length() const { return m_values.count(); }
00139     CSSValueImpl *item ( unsigned long index ) { return m_values.at(index); }
00140 
00141     virtual bool isValueList() const { return true; }
00142 
00143     virtual unsigned short cssValueType() const;
00144 
00145     void append(CSSValueImpl *val);
00146     virtual DOM::DOMString cssText() const;
00147 
00148 protected:
00149     QPtrList<CSSValueImpl> m_values;
00150 };
00151 
00152 
00153 class Counter;
00154 class RGBColor;
00155 class Rect;
00156 
00157 class CSSPrimitiveValueImpl : public CSSValueImpl
00158 {
00159 public:
00160     CSSPrimitiveValueImpl();
00161     CSSPrimitiveValueImpl(int ident);
00162     CSSPrimitiveValueImpl(double num, CSSPrimitiveValue::UnitTypes type);
00163     CSSPrimitiveValueImpl(const DOMString &str, CSSPrimitiveValue::UnitTypes type);
00164     CSSPrimitiveValueImpl(CounterImpl *c);
00165     CSSPrimitiveValueImpl( RectImpl *r);
00166     CSSPrimitiveValueImpl(QRgb color);
00167     CSSPrimitiveValueImpl(PairImpl *p);
00168 
00169     virtual ~CSSPrimitiveValueImpl();
00170 
00171     void cleanup();
00172 
00173     unsigned short primitiveType() const { return m_type; }
00174 
00175     /*
00176      * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
00177      * the fontinfo in case val is defined in em or ex.
00178      *
00179      * The metrics have to be a bit different for screen and printer output.
00180      * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
00181      *
00182      * this is screen/printer dependent, so we probably need a config option for this,
00183      * and some tool to calibrate.
00184      */
00185     int computeLength( khtml::RenderStyle *style, QPaintDeviceMetrics *devMetrics );
00186 
00187     double computeLengthFloat( khtml::RenderStyle *style, QPaintDeviceMetrics *devMetrics );
00188 
00189     // use with care!!!
00190     void setPrimitiveType(unsigned short type) { m_type = type; }
00191     void setFloatValue ( unsigned short unitType, double floatValue, int &exceptioncode );
00192     double floatValue ( unsigned short/* unitType */) const { return m_value.num; }
00193 
00194     void setStringValue ( unsigned short stringType, const DOM::DOMString &stringValue, int &exceptioncode );
00195     DOM::DOMStringImpl *getStringValue () const {
00196     return ( ( m_type < CSSPrimitiveValue::CSS_STRING ||
00197            m_type > CSSPrimitiveValue::CSS_ATTR ||
00198            m_type == CSSPrimitiveValue::CSS_IDENT ) ? // fix IDENT
00199          0 : m_value.string );
00200     }
00201     CounterImpl *getCounterValue () const {
00202         return ( m_type != CSSPrimitiveValue::CSS_COUNTER ? 0 : m_value.counter );
00203     }
00204 
00205     RectImpl *getRectValue () const {
00206     return ( m_type != CSSPrimitiveValue::CSS_RECT ? 0 : m_value.rect );
00207     }
00208 
00209     QRgb getRGBColorValue () const {
00210     return ( m_type != CSSPrimitiveValue::CSS_RGBCOLOR ? 0 : m_value.rgbcolor );
00211     }
00212 
00213     PairImpl* getPairValue() const {
00214         return (m_type != CSSPrimitiveValue::CSS_PAIR ? 0 : m_value.pair);
00215     }
00216 
00217     virtual bool isPrimitiveValue() const { return true; }
00218     virtual unsigned short cssValueType() const;
00219 
00220     int getIdent();
00221 
00222     virtual bool parseString( const DOMString &string, bool = false);
00223     virtual DOM::DOMString cssText() const;
00224 
00225     virtual bool isQuirkValue() const { return false; }
00226 
00227 protected:
00228     int m_type;
00229     union {
00230     int ident;
00231     double num;
00232     DOM::DOMStringImpl *string;
00233     CounterImpl *counter;
00234     RectImpl *rect;
00235         QRgb rgbcolor;
00236         PairImpl* pair;
00237     } m_value;
00238 };
00239 
00240 // This value is used to handle quirky margins in reflow roots (body, td, and th) like WinIE.
00241 // The basic idea is that a stylesheet can use the value __qem (for quirky em) instead of em
00242 // in a stylesheet.  When the quirky value is used, if you're in quirks mode, the margin will
00243 // collapse away inside a table cell.
00244 class CSSQuirkPrimitiveValueImpl : public CSSPrimitiveValueImpl
00245 {
00246 public:
00247     CSSQuirkPrimitiveValueImpl(double num, CSSPrimitiveValue::UnitTypes type)
00248       :CSSPrimitiveValueImpl(num, type) {}
00249 
00250     virtual ~CSSQuirkPrimitiveValueImpl() {}
00251 
00252     virtual bool isQuirkValue() const { return true; }
00253 };
00254 
00255 class CounterImpl : public khtml::Shared<CounterImpl> {
00256 public:
00257     CounterImpl() : m_listStyle(0) { }
00258     DOMString identifier() const { return m_identifier; }
00259     unsigned int listStyle() const { return m_listStyle; }
00260     DOMString separator() const { return m_separator; }
00261 
00262     DOMString m_identifier;
00263     unsigned int m_listStyle;
00264     DOMString m_separator;
00265 };
00266 
00267 class RectImpl : public khtml::Shared<RectImpl> {
00268 public:
00269     RectImpl();
00270     ~RectImpl();
00271 
00272     CSSPrimitiveValueImpl *top() const { return m_top; }
00273     CSSPrimitiveValueImpl *right() const { return m_right; }
00274     CSSPrimitiveValueImpl *bottom() const { return m_bottom; }
00275     CSSPrimitiveValueImpl *left() const { return m_left; }
00276 
00277     void setTop( CSSPrimitiveValueImpl *top );
00278     void setRight( CSSPrimitiveValueImpl *right );
00279     void setBottom( CSSPrimitiveValueImpl *bottom );
00280     void setLeft( CSSPrimitiveValueImpl *left );
00281 protected:
00282     CSSPrimitiveValueImpl *m_top;
00283     CSSPrimitiveValueImpl *m_right;
00284     CSSPrimitiveValueImpl *m_bottom;
00285     CSSPrimitiveValueImpl *m_left;
00286 };
00287 
00288 // A primitive value representing a pair.  This is useful for properties like border-radius, background-size/position,
00289 // and border-spacing (all of which are space-separated sets of two values).  At the moment we are only using it for
00290 // border-radius and background-size, but (FIXME) border-spacing and background-position could be converted over to use
00291 // it (eliminating some extra -webkit- internal properties).
00292 class PairImpl : public khtml::Shared<PairImpl> {
00293 public:
00294     PairImpl() : m_first(0), m_second(0) { }
00295     PairImpl(CSSPrimitiveValueImpl* first, CSSPrimitiveValueImpl* second)
00296         : m_first(first), m_second(second) { if (first) first->ref(); if (second) second->ref(); }
00297     virtual ~PairImpl();
00298 
00299     CSSPrimitiveValueImpl* first() const { return m_first; }
00300     CSSPrimitiveValueImpl* second() const { return m_second; }
00301 
00302     void setFirst(CSSPrimitiveValueImpl* first);
00303     void setSecond(CSSPrimitiveValueImpl* second);
00304 
00305 protected:
00306     CSSPrimitiveValueImpl* m_first;
00307     CSSPrimitiveValueImpl* m_second;
00308 };
00309 
00310 
00311 class CSSImageValueImpl : public CSSPrimitiveValueImpl, public khtml::CachedObjectClient
00312 {
00313 public:
00314     CSSImageValueImpl(const DOMString &url, const StyleBaseImpl *style);
00315     CSSImageValueImpl();
00316     virtual ~CSSImageValueImpl();
00317 
00318     khtml::CachedImage *image() { return m_image; }
00319 protected:
00320     khtml::CachedImage *m_image;
00321 };
00322 
00323 class FontFamilyValueImpl : public CSSPrimitiveValueImpl
00324 {
00325 public:
00326     FontFamilyValueImpl( const QString &string);
00327     const QString &fontName() const { return parsedFontName; }
00328     int genericFamilyType() const { return _genericFamilyType; }
00329 protected:
00330     QString parsedFontName;
00331 private:
00332     int _genericFamilyType;
00333 };
00334 
00335 class FontValueImpl : public CSSValueImpl
00336 {
00337 public:
00338     FontValueImpl();
00339     virtual ~FontValueImpl();
00340 
00341     virtual unsigned short cssValueType() const { return CSSValue::CSS_CUSTOM; }
00342 
00343     virtual DOM::DOMString cssText() const;
00344 
00345     virtual bool isFontValue() const { return true; }
00346 
00347     CSSPrimitiveValueImpl *style;
00348     CSSPrimitiveValueImpl *variant;
00349     CSSPrimitiveValueImpl *weight;
00350     CSSPrimitiveValueImpl *size;
00351     CSSPrimitiveValueImpl *lineHeight;
00352     CSSValueListImpl *family;
00353 };
00354 
00355 // Used for quotes
00356 class QuotesValueImpl : public CSSValueImpl
00357 {
00358 public:
00359     QuotesValueImpl();
00360 //    virtual ~QuotesValueImpl();
00361 
00362     virtual unsigned short cssValueType() const { return CSSValue::CSS_CUSTOM; }
00363     virtual DOM::DOMString cssText() const;
00364 
00365     void addLevel(const QString& open, const QString& close);
00366     QString openQuote(int level) const;
00367     QString closeQuote(int level) const;
00368 
00369     unsigned int levels;
00370     QStringList data;
00371 };
00372 
00373 // Used for text-shadow and box-shadow
00374 class ShadowValueImpl : public CSSValueImpl
00375 {
00376 public:
00377     ShadowValueImpl(CSSPrimitiveValueImpl* _x, CSSPrimitiveValueImpl* _y,
00378                     CSSPrimitiveValueImpl* _blur, CSSPrimitiveValueImpl* _color);
00379     virtual ~ShadowValueImpl();
00380 
00381     virtual unsigned short cssValueType() const { return CSSValue::CSS_CUSTOM; }
00382 
00383     virtual DOM::DOMString cssText() const;
00384 
00385     CSSPrimitiveValueImpl* x;
00386     CSSPrimitiveValueImpl* y;
00387     CSSPrimitiveValueImpl* blur;
00388     CSSPrimitiveValueImpl* color;
00389 };
00390 
00391 // Used for counter-reset and counter-increment
00392 class CounterActImpl : public CSSValueImpl {
00393     public:
00394         CounterActImpl(const DOMString &c, short v) : m_counter(c), m_value(v) { }
00395         virtual ~CounterActImpl() {};
00396 
00397         virtual unsigned short cssValueType() const { return CSSValue::CSS_CUSTOM; }
00398         virtual DOM::DOMString cssText() const;
00399 
00400         const DOMString& counter() const { return m_counter; }
00401         short value() const { return m_value; }
00402         void setValue( const short v ) { m_value = v; }
00403 
00404         DOM::DOMString m_counter;
00405         short m_value;
00406 };
00407 
00408 
00409 // ------------------------------------------------------------------------------
00410 
00411 // another helper class
00412 class CSSProperty
00413 {
00414 public:
00415     CSSProperty()
00416     {
00417     m_id = -1;
00418     m_important = false;
00419     nonCSSHint = false;
00420         m_value = 0;
00421     }
00422     CSSProperty(const CSSProperty& o)
00423     {
00424         m_id = o.m_id;
00425         m_important = o.m_important;
00426         nonCSSHint = o.nonCSSHint;
00427         m_value = o.m_value;
00428         if (m_value) m_value->ref();
00429     }
00430     ~CSSProperty() {
00431     if(m_value) m_value->deref();
00432     }
00433 
00434     void setValue(CSSValueImpl *val) {
00435     if ( val != m_value ) {
00436         if(m_value) m_value->deref();
00437         m_value = val;
00438         if(m_value) m_value->ref();
00439     }
00440     }
00441 
00442     int id() const { return m_id; }
00443 
00444     bool isImportant() const { return m_important; }
00445 
00446     CSSValueImpl *value() const { return m_value; }
00447 
00448     DOM::DOMString cssText() const;
00449 
00450     // make sure the following fits in 4 bytes.
00451     signed int  m_id   : 29;
00452     bool m_important   : 1;
00453     bool nonCSSHint    : 1;
00454 protected:
00455     CSSValueImpl *m_value;
00456 };
00457 
00458 
00459 } // namespace
00460 
00461 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys