File indexing completed on 2023-09-24 04:06:34
0001 /* 0002 This file is part of the KDE libraries 0003 0004 Copyright (C) 1999 Lars Knoll (knoll@kde.org) 0005 Copyright (C) 2006, 2008 Apple Inc. All rights reserved. 0006 0007 This library is free software; you can redistribute it and/or 0008 modify it under the terms of the GNU Library General Public 0009 License as published by the Free Software Foundation; either 0010 version 2 of the License, or (at your option) any later version. 0011 0012 This library is distributed in the hope that it will be useful, 0013 but WITHOUT ANY WARRANTY; without even the implied warranty of 0014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0015 Library General Public License for more details. 0016 0017 You should have received a copy of the GNU Library General Public License 0018 along with this library; see the file COPYING.LIB. If not, write to 0019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0020 Boston, MA 02110-1301, USA. 0021 0022 This widget holds some useful definitions needed for layouting Elements 0023 */ 0024 #ifndef HTML_LAYOUT_H 0025 #define HTML_LAYOUT_H 0026 0027 #include <QVector> 0028 #include <math.h> 0029 #include <assert.h> 0030 0031 /* 0032 * this namespace contains definitions for various types needed for 0033 * layouting. 0034 */ 0035 namespace khtml 0036 { 0037 0038 const int UNDEFINED = -1; 0039 const int PERCENT_SCALE_FACTOR = 128; 0040 0041 // alignment 0042 enum VAlign { VNone = 0, Bottom, VCenter, Top, Baseline }; 0043 enum HAlign { HDefault, Left, HCenter, Right, HNone = 0 }; 0044 0045 /* 0046 * %multiLength and %Length 0047 */ 0048 enum LengthType { Auto = 0, Relative, Percent, Fixed, Static }; 0049 struct Length { 0050 Length() : m_value(0) {} 0051 0052 Length(LengthType t): m_value(t) {} 0053 0054 Length(int v, LengthType t, bool q = false) 0055 : m_value((v * 16) | (q << 3) | t) 0056 { 0057 assert(t != Percent); 0058 } 0059 0060 Length(double v, LengthType t, bool q = false) 0061 : m_value(static_cast<int>(v *PERCENT_SCALE_FACTOR) * 16 | (q << 3) | t) 0062 { 0063 assert(t == Percent); 0064 } 0065 0066 bool operator==(const Length &o) const 0067 { 0068 return m_value == o.m_value; 0069 } 0070 bool operator!=(const Length &o) const 0071 { 0072 return m_value != o.m_value; 0073 } 0074 0075 int value() const 0076 { 0077 assert(type() != Percent); 0078 return rawValue(); 0079 } 0080 0081 int rawValue() const 0082 { 0083 return (m_value & ~0xF) / 16; 0084 } 0085 0086 double percent() const 0087 { 0088 assert(type() == Percent); 0089 return static_cast<double>(rawValue()) / PERCENT_SCALE_FACTOR; 0090 } 0091 0092 LengthType type() const 0093 { 0094 return static_cast<LengthType>(m_value & 7); 0095 } 0096 0097 void setValue(LengthType t, int value) 0098 { 0099 assert(t != Percent); 0100 setRawValue(t, value); 0101 } 0102 0103 void setRawValue(LengthType t, int value) 0104 { 0105 m_value = value * 16 | (m_value & 0x8) | t; 0106 } 0107 0108 void setValue(int value) 0109 { 0110 assert(!value || type() != Percent); 0111 setRawValue(value); 0112 } 0113 0114 void setRawValue(int value) 0115 { 0116 m_value = value * 16 | (m_value & 0xF); 0117 } 0118 0119 void setValue(LengthType t, double value) 0120 { 0121 assert(t == Percent); 0122 m_value = static_cast<int>(value * PERCENT_SCALE_FACTOR) * 16 | (m_value & 0x8) | t; 0123 } 0124 0125 void setValue(double value) 0126 { 0127 assert(type() == Percent); 0128 m_value = static_cast<int>(value * PERCENT_SCALE_FACTOR) * 16 | (m_value & 0xF); 0129 } 0130 0131 /* 0132 * works only for certain types, returns UNDEFINED otherwise 0133 */ 0134 int width(int maxValue) const 0135 { 0136 switch (type()) { 0137 case Fixed: 0138 return value(); 0139 case Percent: 0140 return maxValue * rawValue() / (100 * PERCENT_SCALE_FACTOR); 0141 case Auto: 0142 return maxValue; 0143 default: 0144 return UNDEFINED; 0145 } 0146 } 0147 /* 0148 * returns the minimum width value which could work... 0149 */ 0150 int minWidth(int maxValue) const 0151 { 0152 switch (type()) { 0153 case Fixed: 0154 return value(); 0155 case Percent: 0156 return maxValue * rawValue() / (100 * PERCENT_SCALE_FACTOR); 0157 case Auto: 0158 default: 0159 return 0; 0160 } 0161 } 0162 0163 int minWidthRounded(int maxValue) const 0164 { 0165 switch (type()) { 0166 case Fixed: 0167 return value(); 0168 case Percent: 0169 return static_cast<int>(round(maxValue * percent() / 100.0)); 0170 case Auto: 0171 default: 0172 return 0; 0173 } 0174 } 0175 0176 bool isUndefined() const 0177 { 0178 return rawValue() == UNDEFINED; 0179 } 0180 bool isZero() const 0181 { 0182 return !(m_value & ~0xF); 0183 } 0184 bool isPositive() const 0185 { 0186 return rawValue() > 0; 0187 } 0188 bool isNegative() const 0189 { 0190 return rawValue() < 0; 0191 } 0192 0193 bool isAuto() const 0194 { 0195 return type() == Auto; 0196 } 0197 bool isRelative() const 0198 { 0199 return type() == Relative; 0200 } 0201 bool isPercent() const 0202 { 0203 return type() == Percent; 0204 } 0205 bool isFixed() const 0206 { 0207 return type() == Fixed; 0208 } 0209 bool isQuirk() const 0210 { 0211 return (m_value >> 3) & 1; 0212 } 0213 0214 private: 0215 int m_value; 0216 }; 0217 0218 } 0219 0220 #endif