fix logic
[personal-kdelibs.git] / khtml / dom / css_value.cpp
blobcee8f3a5650a4b95a16cf8ae4c642c7f45cf80c8
1 /*
2 * This file is part of the DOM implementation for KDE.
4 * Copyright 1999 Lars Knoll (knoll@kde.org)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "dom/css_rule.h"
24 #include "dom/dom_exception.h"
26 #include "css/css_renderstyledeclarationimpl.h"
27 #include "css/css_valueimpl.h"
29 namespace DOM {
31 CSSStyleDeclaration::CSSStyleDeclaration()
33 impl = 0;
36 CSSStyleDeclaration::CSSStyleDeclaration(const CSSStyleDeclaration &other)
38 impl = other.impl;
39 if(impl) impl->ref();
42 CSSStyleDeclaration::CSSStyleDeclaration(CSSStyleDeclarationImpl *i)
44 impl = i;
45 if(impl) impl->ref();
48 CSSStyleDeclaration &CSSStyleDeclaration::operator = (const CSSStyleDeclaration &other)
50 if ( impl != other.impl ) {
51 if(impl) impl->deref();
52 impl = other.impl;
53 if(impl) impl->ref();
55 return *this;
58 CSSStyleDeclaration::~CSSStyleDeclaration()
60 if(impl) impl->deref();
63 DOMString CSSStyleDeclaration::cssText() const
65 if(!impl) return DOMString();
66 return static_cast<CSSStyleDeclarationImpl *>(impl)->cssText();
69 void CSSStyleDeclaration::setCssText( const DOMString &value )
71 if(!impl) return;
72 impl->setCssText(value);
75 DOMString CSSStyleDeclaration::getPropertyValue( const DOMString &propertyName ) const
77 if(!impl) return DOMString();
78 return static_cast<CSSStyleDeclarationImpl *>(impl)->getPropertyValue(propertyName);
81 CSSValue CSSStyleDeclaration::getPropertyCSSValue( const DOMString &propertyName ) const
83 if(!impl) return 0;
84 return static_cast<CSSStyleDeclarationImpl *>(impl)->getPropertyCSSValue(propertyName);
87 DOMString CSSStyleDeclaration::removeProperty( const DOMString &propertyName )
89 if(!impl) return DOMString();
90 return static_cast<CSSStyleDeclarationImpl *>(impl)->removeProperty(propertyName);
93 DOMString CSSStyleDeclaration::getPropertyPriority( const DOMString &propertyName ) const
95 if(!impl) return DOMString();
96 return impl->getPropertyPriority(propertyName);
99 void CSSStyleDeclaration::setProperty( const DOMString &propName, const DOMString &value, const DOMString &priority )
101 if(!impl) return;
102 static_cast<CSSStyleDeclarationImpl *>(impl)->setProperty( propName, value, priority );
105 unsigned long CSSStyleDeclaration::length() const
107 if(!impl) return 0;
108 return static_cast<CSSStyleDeclarationImpl *>(impl)->length();
111 DOMString CSSStyleDeclaration::item( unsigned long index )
113 return const_cast<const CSSStyleDeclaration*>( this )->item( index );
116 DOMString CSSStyleDeclaration::item( unsigned long index ) const
118 if(!impl) return DOMString();
119 return static_cast<CSSStyleDeclarationImpl *>(impl)->item( index );
121 CSSRule CSSStyleDeclaration::parentRule() const
123 if(!impl) return 0;
124 return static_cast<CSSStyleDeclarationImpl *>(impl)->parentRule();
127 CSSStyleDeclarationImpl *CSSStyleDeclaration::handle() const
129 return impl;
132 bool CSSStyleDeclaration::isNull() const
134 return (impl == 0);
137 // ----------------------------------------------------------
139 CSSValue::CSSValue()
141 impl = 0;
144 CSSValue::CSSValue(const CSSValue &other)
146 impl = other.impl;
147 if(impl) impl->ref();
150 CSSValue::CSSValue(CSSValueImpl *i)
152 impl = i;
153 if(impl) impl->ref();
156 CSSValue &CSSValue::operator = (const CSSValue &other)
158 if ( impl != other.impl ) {
159 if(impl) impl->deref();
160 impl = other.impl;
161 if(impl) impl->ref();
163 return *this;
166 CSSValue::~CSSValue()
168 if(impl) impl->deref();
171 DOMString CSSValue::cssText() const
173 if(!impl) return DOMString();
174 return ((CSSValueImpl *)impl)->cssText();
177 void CSSValue::setCssText(const DOMString &value)
179 if(!impl) return;
180 ((CSSValueImpl *)impl)->setCssText(value);
183 unsigned short CSSValue::cssValueType() const
185 if(!impl) return 0;
186 return ((CSSValueImpl *)impl)->cssValueType();
189 bool CSSValue::isCSSValueList() const
191 if(!impl) return false;
192 return ((CSSValueImpl *)impl)->isValueList();
195 bool CSSValue::isCSSPrimitiveValue() const
197 if(!impl) return false;
198 return ((CSSValueImpl *)impl)->isPrimitiveValue();
201 CSSValueImpl *CSSValue::handle() const
203 return impl;
206 bool CSSValue::isNull() const
208 return (impl == 0);
211 // ----------------------------------------------------------
213 CSSValueList::CSSValueList() : CSSValue()
217 CSSValueList::CSSValueList(const CSSValueList &other) : CSSValue(other)
221 CSSValueList::CSSValueList(const CSSValue &other)
223 impl = 0;
224 operator=(other);
227 CSSValueList::CSSValueList(CSSValueListImpl *impl) : CSSValue(impl)
231 CSSValueList &CSSValueList::operator = (const CSSValueList &other)
233 if ( impl != other.impl ) {
234 if (impl) impl->deref();
235 impl = other.handle();
236 if (impl) impl->ref();
238 return *this;
241 CSSValueList &CSSValueList::operator = (const CSSValue &other)
243 CSSValueImpl *ohandle = other.handle() ;
244 if ( impl != ohandle ) {
245 if (impl) impl->deref();
246 if (!other.isNull() && !other.isCSSValueList()) {
247 impl = 0;
248 } else {
249 impl = ohandle;
250 if (impl) impl->ref();
253 return *this;
256 CSSValueList::~CSSValueList()
260 unsigned long CSSValueList::length() const
262 if(!impl) return 0;
263 return ((CSSValueListImpl *)impl)->length();
266 CSSValue CSSValueList::item( unsigned long index )
268 if(!impl) return 0;
269 return ((CSSValueListImpl *)impl)->item( index );
272 // ----------------------------------------------------------
274 CSSPrimitiveValue::CSSPrimitiveValue() : CSSValue()
278 CSSPrimitiveValue::CSSPrimitiveValue(const CSSPrimitiveValue &other) : CSSValue(other)
282 CSSPrimitiveValue::CSSPrimitiveValue(const CSSValue &other) : CSSValue(other)
284 impl = 0;
285 operator=(other);
288 CSSPrimitiveValue::CSSPrimitiveValue(CSSPrimitiveValueImpl *impl) : CSSValue(impl)
292 CSSPrimitiveValue &CSSPrimitiveValue::operator = (const CSSPrimitiveValue &other)
294 if ( impl != other.impl ) {
295 if (impl) impl->deref();
296 impl = other.handle();
297 if (impl) impl->ref();
299 return *this;
302 CSSPrimitiveValue &CSSPrimitiveValue::operator = (const CSSValue &other)
304 CSSValueImpl *ohandle = other.handle();
305 if ( impl != ohandle ) {
306 if (impl) impl->deref();
307 if (!other.isNull() && !other.isCSSPrimitiveValue()) {
308 impl = 0;
309 } else {
310 impl = ohandle;
311 if (impl) impl->ref();
314 return *this;
317 CSSPrimitiveValue::~CSSPrimitiveValue()
321 unsigned short CSSPrimitiveValue::primitiveType() const
323 if(!impl) return 0;
324 return ((CSSPrimitiveValueImpl *)impl)->primitiveType();
327 void CSSPrimitiveValue::setFloatValue( unsigned short unitType, float floatValue )
329 if(!impl) return;
330 int exceptioncode = 0;
331 ((CSSPrimitiveValueImpl *)impl)->setFloatValue( unitType, floatValue, exceptioncode );
332 if ( exceptioncode >= CSSException::_EXCEPTION_OFFSET )
333 throw CSSException( exceptioncode - CSSException::_EXCEPTION_OFFSET );
334 if ( exceptioncode )
335 throw DOMException( exceptioncode );
338 float CSSPrimitiveValue::getFloatValue( unsigned short unitType ) const
340 if(!impl) return 0;
341 // ### add unit conversion
342 if(primitiveType() != unitType)
343 throw CSSException(CSSException::SYNTAX_ERR);
344 return ((CSSPrimitiveValueImpl *)impl)->floatValue( unitType );
347 void CSSPrimitiveValue::setStringValue( unsigned short stringType, const DOMString &stringValue )
349 int exceptioncode = 0;
350 if(impl)
351 ((CSSPrimitiveValueImpl *)impl)->setStringValue( stringType, stringValue, exceptioncode );
352 if ( exceptioncode >= CSSException::_EXCEPTION_OFFSET )
353 throw CSSException( exceptioncode - CSSException::_EXCEPTION_OFFSET );
354 if ( exceptioncode )
355 throw DOMException( exceptioncode );
359 DOMString CSSPrimitiveValue::getStringValue( ) const
361 if(!impl) return DOMString();
362 return ((CSSPrimitiveValueImpl *)impl)->getStringValue( );
365 Counter CSSPrimitiveValue::getCounterValue( ) const
367 if(!impl) return Counter();
368 return ((CSSPrimitiveValueImpl *)impl)->getCounterValue( );
371 Rect CSSPrimitiveValue::getRectValue( ) const
373 if(!impl) return Rect();
374 return ((CSSPrimitiveValueImpl *)impl)->getRectValue( );
377 RGBColor CSSPrimitiveValue::getRGBColorValue( ) const
379 // ###
380 return RGBColor();
381 //if(!impl) return RGBColor();
382 //return ((CSSPrimitiveValueImpl *)impl)->getRGBColorValue( );
385 // -------------------------------------------------------------------
387 Counter::Counter()
391 Counter::Counter(const Counter &/*other*/)
393 impl = 0;
396 Counter &Counter::operator = (const Counter &other)
398 if ( impl != other.impl ) {
399 if (impl) impl->deref();
400 impl = other.impl;
401 if (impl) impl->ref();
403 return *this;
406 Counter::Counter(CounterImpl *i)
408 impl = i;
409 if (impl) impl->ref();
412 Counter::~Counter()
414 if (impl) impl->deref();
417 DOMString Counter::identifier() const
419 if (!impl) return DOMString();
420 return impl->identifier();
423 DOMString Counter::listStyle() const
425 if (!impl) return DOMString();
426 return khtml::stringForListStyleType((khtml::EListStyleType)impl->listStyle());
429 DOMString Counter::separator() const
431 if (!impl) return DOMString();
432 return impl->separator();
435 CounterImpl *Counter::handle() const
437 return impl;
440 bool Counter::isNull() const
442 return (impl == 0);
445 // --------------------------------------------------------------------
447 RGBColor::RGBColor()
451 RGBColor::RGBColor(const RGBColor &other)
453 m_color = other.m_color;
456 RGBColor::RGBColor(QRgb color)
458 m_color = color;
461 RGBColor &RGBColor::operator = (const RGBColor &other)
463 m_color = other.m_color;
464 return *this;
467 RGBColor::~RGBColor()
471 CSSPrimitiveValue RGBColor::red() const
473 return new CSSPrimitiveValueImpl(float(qAlpha(m_color) ? qRed(m_color) : 0), CSSPrimitiveValue::CSS_DIMENSION);
476 CSSPrimitiveValue RGBColor::green() const
478 return new CSSPrimitiveValueImpl(float(qAlpha(m_color) ? qGreen(m_color) : 0), CSSPrimitiveValue::CSS_DIMENSION);
481 CSSPrimitiveValue RGBColor::blue() const
483 return new CSSPrimitiveValueImpl(float(qAlpha(m_color) ? qBlue(m_color) : 0), CSSPrimitiveValue::CSS_DIMENSION);
487 // ---------------------------------------------------------------------
489 Rect::Rect()
491 impl = 0;
494 Rect::Rect(const Rect &other)
496 impl = other.impl;
497 if (impl) impl->ref();
500 Rect::Rect(RectImpl *i)
502 impl = i;
503 if (impl) impl->ref();
506 Rect &Rect::operator = (const Rect &other)
508 if ( impl != other.impl ) {
509 if (impl) impl->deref();
510 impl = other.impl;
511 if (impl) impl->ref();
513 return *this;
516 Rect::~Rect()
518 if (impl) impl->deref();
521 CSSPrimitiveValue Rect::top() const
523 if (!impl) return 0;
524 return impl->top();
527 CSSPrimitiveValue Rect::right() const
529 if (!impl) return 0;
530 return impl->right();
533 CSSPrimitiveValue Rect::bottom() const
535 if (!impl) return 0;
536 return impl->bottom();
539 CSSPrimitiveValue Rect::left() const
541 if (!impl) return 0;
542 return impl->left();
545 RectImpl *Rect::handle() const
547 return impl;
550 bool Rect::isNull() const
552 return (impl == 0);
555 } // namespace