KHtml

khtmllayout.h
1 /*
2  This file is part of the KDE libraries
3 
4  Copyright (C) 1999 Lars Knoll ([email protected])
5  Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 
22  This widget holds some useful definitions needed for layouting Elements
23 */
24 #ifndef HTML_LAYOUT_H
25 #define HTML_LAYOUT_H
26 
27 #include <QVector>
28 #include <math.h>
29 #include <assert.h>
30 
31 /*
32  * this namespace contains definitions for various types needed for
33  * layouting.
34  */
35 namespace khtml
36 {
37 
38 const int UNDEFINED = -1;
39 const int PERCENT_SCALE_FACTOR = 128;
40 
41 // alignment
42 enum VAlign { VNone = 0, Bottom, VCenter, Top, Baseline };
43 enum HAlign { HDefault, Left, HCenter, Right, HNone = 0 };
44 
45 /*
46  * %multiLength and %Length
47  */
48 enum LengthType { Auto = 0, Relative, Percent, Fixed, Static };
49 struct Length {
50  Length() : m_value(0) {}
51 
52  Length(LengthType t): m_value(t) {}
53 
54  Length(int v, LengthType t, bool q = false)
55  : m_value((v * 16) | (q << 3) | t)
56  {
57  assert(t != Percent);
58  }
59 
60  Length(double v, LengthType t, bool q = false)
61  : m_value(static_cast<int>(v *PERCENT_SCALE_FACTOR) * 16 | (q << 3) | t)
62  {
63  assert(t == Percent);
64  }
65 
66  bool operator==(const Length &o) const
67  {
68  return m_value == o.m_value;
69  }
70  bool operator!=(const Length &o) const
71  {
72  return m_value != o.m_value;
73  }
74 
75  int value() const
76  {
77  assert(type() != Percent);
78  return rawValue();
79  }
80 
81  int rawValue() const
82  {
83  return (m_value & ~0xF) / 16;
84  }
85 
86  double percent() const
87  {
88  assert(type() == Percent);
89  return static_cast<double>(rawValue()) / PERCENT_SCALE_FACTOR;
90  }
91 
92  LengthType type() const
93  {
94  return static_cast<LengthType>(m_value & 7);
95  }
96 
97  void setValue(LengthType t, int value)
98  {
99  assert(t != Percent);
100  setRawValue(t, value);
101  }
102 
103  void setRawValue(LengthType t, int value)
104  {
105  m_value = value * 16 | (m_value & 0x8) | t;
106  }
107 
108  void setValue(int value)
109  {
110  assert(!value || type() != Percent);
111  setRawValue(value);
112  }
113 
114  void setRawValue(int value)
115  {
116  m_value = value * 16 | (m_value & 0xF);
117  }
118 
119  void setValue(LengthType t, double value)
120  {
121  assert(t == Percent);
122  m_value = static_cast<int>(value * PERCENT_SCALE_FACTOR) * 16 | (m_value & 0x8) | t;
123  }
124 
125  void setValue(double value)
126  {
127  assert(type() == Percent);
128  m_value = static_cast<int>(value * PERCENT_SCALE_FACTOR) * 16 | (m_value & 0xF);
129  }
130 
131  /*
132  * works only for certain types, returns UNDEFINED otherwise
133  */
134  int width(int maxValue) const
135  {
136  switch (type()) {
137  case Fixed:
138  return value();
139  case Percent:
140  return maxValue * rawValue() / (100 * PERCENT_SCALE_FACTOR);
141  case Auto:
142  return maxValue;
143  default:
144  return UNDEFINED;
145  }
146  }
147  /*
148  * returns the minimum width value which could work...
149  */
150  int minWidth(int maxValue) const
151  {
152  switch (type()) {
153  case Fixed:
154  return value();
155  case Percent:
156  return maxValue * rawValue() / (100 * PERCENT_SCALE_FACTOR);
157  case Auto:
158  default:
159  return 0;
160  }
161  }
162 
163  int minWidthRounded(int maxValue) const
164  {
165  switch (type()) {
166  case Fixed:
167  return value();
168  case Percent:
169  return static_cast<int>(round(maxValue * percent() / 100.0));
170  case Auto:
171  default:
172  return 0;
173  }
174  }
175 
176  bool isUndefined() const
177  {
178  return rawValue() == UNDEFINED;
179  }
180  bool isZero() const
181  {
182  return !(m_value & ~0xF);
183  }
184  bool isPositive() const
185  {
186  return rawValue() > 0;
187  }
188  bool isNegative() const
189  {
190  return rawValue() < 0;
191  }
192 
193  bool isAuto() const
194  {
195  return type() == Auto;
196  }
197  bool isRelative() const
198  {
199  return type() == Relative;
200  }
201  bool isPercent() const
202  {
203  return type() == Percent;
204  }
205  bool isFixed() const
206  {
207  return type() == Fixed;
208  }
209  bool isQuirk() const
210  {
211  return (m_value >> 3) & 1;
212  }
213 
214 private:
215  int m_value;
216 };
217 
218 }
219 
220 #endif
This file is part of the HTML rendering engine for KDE.
Left
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
Type type(const QSqlDatabase &db)
Top
bool operator!=(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:04 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.