source: trunk/src/gui/text/qcssparser_p.h@ 234

Last change on this file since 234 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 23.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QCSSPARSER_P_H
43#define QCSSPARSER_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists for the convenience
50// of the QLibrary class. This header file may change from
51// version to version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include <QtCore/QStringList>
57#include <QtCore/QVector>
58#include <QtCore/QVariant>
59#include <QtCore/QPair>
60#include <QtCore/QSize>
61#include <QtCore/QMultiHash>
62#include <QtGui/QFont>
63#include <QtGui/QPalette>
64#include <QtGui/QIcon>
65#include <QtCore/QSharedData>
66
67
68#ifndef QT_NO_CSSPARSER
69
70QT_BEGIN_NAMESPACE
71
72namespace QCss
73{
74
75enum Property {
76 UnknownProperty,
77 BackgroundColor,
78 Color,
79 Float,
80 Font,
81 FontFamily,
82 FontSize,
83 FontStyle,
84 FontWeight,
85 Margin,
86 MarginBottom,
87 MarginLeft,
88 MarginRight,
89 MarginTop,
90 QtBlockIndent,
91 QtListIndent,
92 QtParagraphType,
93 QtTableType,
94 QtUserState,
95 TextDecoration,
96 TextIndent,
97 TextUnderlineStyle,
98 VerticalAlignment,
99 Whitespace,
100 QtSelectionForeground,
101 QtSelectionBackground,
102 Border,
103 BorderLeft,
104 BorderRight,
105 BorderTop,
106 BorderBottom,
107 Padding,
108 PaddingLeft,
109 PaddingRight,
110 PaddingTop,
111 PaddingBottom,
112 PageBreakBefore,
113 PageBreakAfter,
114 QtAlternateBackground,
115 BorderLeftStyle,
116 BorderRightStyle,
117 BorderTopStyle,
118 BorderBottomStyle,
119 BorderStyles,
120 BorderLeftColor,
121 BorderRightColor,
122 BorderTopColor,
123 BorderBottomColor,
124 BorderColor,
125 BorderLeftWidth,
126 BorderRightWidth,
127 BorderTopWidth,
128 BorderBottomWidth,
129 BorderWidth,
130 BorderTopLeftRadius,
131 BorderTopRightRadius,
132 BorderBottomLeftRadius,
133 BorderBottomRightRadius,
134 BorderRadius,
135 Background,
136 BackgroundOrigin,
137 BackgroundClip,
138 BackgroundRepeat,
139 BackgroundPosition,
140 BackgroundAttachment,
141 BackgroundImage,
142 BorderImage,
143 QtSpacing,
144 Width,
145 Height,
146 MinimumWidth,
147 MinimumHeight,
148 MaximumWidth,
149 MaximumHeight,
150 QtImage,
151 Left,
152 Right,
153 Top,
154 Bottom,
155 QtOrigin,
156 QtPosition,
157 Position,
158 QtStyleFeatures,
159 QtBackgroundRole,
160 ListStyleType,
161 ListStyle,
162 QtImageAlignment,
163 TextAlignment,
164 Outline,
165 OutlineOffset,
166 OutlineWidth,
167 OutlineColor,
168 OutlineStyle,
169 OutlineRadius,
170 OutlineTopLeftRadius,
171 OutlineTopRightRadius,
172 OutlineBottomLeftRadius,
173 OutlineBottomRightRadius,
174 FontVariant,
175 TextTransform,
176 NumProperties
177};
178
179enum KnownValue {
180 UnknownValue,
181 Value_Normal,
182 Value_Pre,
183 Value_NoWrap,
184 Value_PreWrap,
185 Value_Small,
186 Value_Medium,
187 Value_Large,
188 Value_XLarge,
189 Value_XXLarge,
190 Value_Italic,
191 Value_Oblique,
192 Value_Bold,
193 Value_Underline,
194 Value_Overline,
195 Value_LineThrough,
196 Value_Sub,
197 Value_Super,
198 Value_Left,
199 Value_Right,
200 Value_Top,
201 Value_Bottom,
202 Value_Center,
203 Value_Native,
204 Value_Solid,
205 Value_Dotted,
206 Value_Dashed,
207 Value_DotDash,
208 Value_DotDotDash,
209 Value_Double,
210 Value_Groove,
211 Value_Ridge,
212 Value_Inset,
213 Value_Outset,
214 Value_Wave,
215 Value_Middle,
216 Value_Auto,
217 Value_Always,
218 Value_None,
219 Value_Transparent,
220 Value_Disc,
221 Value_Circle,
222 Value_Square,
223 Value_Decimal,
224 Value_LowerAlpha,
225 Value_UpperAlpha,
226 Value_SmallCaps,
227 Value_Uppercase,
228 Value_Lowercase,
229
230 /* keep these in same order as QPalette::ColorRole */
231 Value_FirstColorRole,
232 Value_WindowText = Value_FirstColorRole,
233 Value_Button,
234 Value_Light,
235 Value_Midlight,
236 Value_Dark,
237 Value_Mid,
238 Value_Text,
239 Value_BrightText,
240 Value_ButtonText,
241 Value_Base,
242 Value_Window,
243 Value_Shadow,
244 Value_Highlight,
245 Value_HighlightedText,
246 Value_Link,
247 Value_LinkVisited,
248 Value_AlternateBase,
249 Value_LastColorRole = Value_AlternateBase,
250
251 Value_Disabled,
252 Value_Active,
253 Value_Selected,
254 Value_On,
255 Value_Off,
256
257 NumKnownValues
258};
259
260enum BorderStyle {
261 BorderStyle_Unknown,
262 BorderStyle_None,
263 BorderStyle_Dotted,
264 BorderStyle_Dashed,
265 BorderStyle_Solid,
266 BorderStyle_Double,
267 BorderStyle_DotDash,
268 BorderStyle_DotDotDash,
269 BorderStyle_Groove,
270 BorderStyle_Ridge,
271 BorderStyle_Inset,
272 BorderStyle_Outset,
273 BorderStyle_Native,
274 NumKnownBorderStyles
275};
276
277enum Edge {
278 TopEdge,
279 RightEdge,
280 BottomEdge,
281 LeftEdge,
282 NumEdges
283};
284
285enum Corner {
286 TopLeftCorner,
287 TopRightCorner,
288 BottomLeftCorner,
289 BottomRightCorner
290};
291
292enum TileMode {
293 TileMode_Unknown,
294 TileMode_Round,
295 TileMode_Stretch,
296 TileMode_Repeat,
297 NumKnownTileModes
298};
299
300enum Repeat {
301 Repeat_Unknown,
302 Repeat_None,
303 Repeat_X,
304 Repeat_Y,
305 Repeat_XY,
306 NumKnownRepeats
307};
308
309enum Origin {
310 Origin_Unknown,
311 Origin_Padding,
312 Origin_Border,
313 Origin_Content,
314 Origin_Margin,
315 NumKnownOrigins
316};
317
318enum PositionMode {
319 PositionMode_Unknown,
320 PositionMode_Static,
321 PositionMode_Relative,
322 PositionMode_Absolute,
323 PositionMode_Fixed,
324 NumKnownPositionModes
325};
326
327enum Attachment {
328 Attachment_Unknown,
329 Attachment_Fixed,
330 Attachment_Scroll,
331 NumKnownAttachments
332};
333
334enum StyleFeature {
335 StyleFeature_None = 0,
336 StyleFeature_BackgroundColor = 1,
337 StyleFeature_BackgroundGradient = 2,
338 NumKnownStyleFeatures = 4
339};
340
341struct Q_GUI_EXPORT Value
342{
343 enum Type {
344 Unknown,
345 Number,
346 Percentage,
347 Length,
348 String,
349 Identifier,
350 KnownIdentifier,
351 Uri,
352 Color,
353 Function,
354 TermOperatorSlash,
355 TermOperatorComma
356 };
357 inline Value() : type(Unknown) { }
358 Type type;
359 QVariant variant;
360 QString toString() const;
361};
362
363struct ColorData {
364 ColorData() : type(Invalid) {}
365 ColorData(const QColor &col) : color(col) , type(Color) {}
366 ColorData(QPalette::ColorRole r) : role(r) , type(Role) {}
367 QColor color;
368 QPalette::ColorRole role;
369 enum { Invalid, Color, Role} type;
370};
371
372struct BrushData {
373 BrushData() : type(Invalid) {}
374 BrushData(const QBrush &br) : brush(br) , type(Brush) {}
375 BrushData(QPalette::ColorRole r) : role(r) , type(Role) {}
376 QBrush brush;
377 QPalette::ColorRole role;
378 enum { Invalid, Brush, Role, DependsOnThePalette } type;
379};
380
381struct BackgroundData {
382 BrushData brush;
383 QString image;
384 Repeat repeat;
385 Qt::Alignment alignment;
386};
387
388struct LengthData {
389 qreal number;
390 enum { None, Px, Ex, Em } unit;
391};
392
393struct BorderData {
394 LengthData width;
395 BorderStyle style;
396 BrushData color;
397};
398
399
400// 1. StyleRule - x:hover, y:clicked > z:checked { prop1: value1; prop2: value2; }
401// 2. QVector<Selector> - x:hover, y:clicked z:checked
402// 3. QVector<BasicSelector> - y:clicked z:checked
403// 4. QVector<Declaration> - { prop1: value1; prop2: value2; }
404// 5. Declaration - prop1: value1;
405
406struct Q_GUI_EXPORT Declaration
407{
408 struct DeclarationData : public QSharedData
409 {
410 inline DeclarationData() : propertyId(UnknownProperty), important(false) {}
411 QString property;
412 Property propertyId;
413 QVector<Value> values;
414 QVariant parsed;
415 bool important;
416 };
417 QExplicitlySharedDataPointer<DeclarationData> d;
418 inline Declaration() : d(new DeclarationData()) {}
419 inline bool isEmpty() const { return d->property.isEmpty() && d->propertyId == UnknownProperty; }
420
421 // helper functions
422 QColor colorValue(const QPalette & = QPalette()) const;
423 void colorValues(QColor *c, const QPalette & = QPalette()) const;
424 QBrush brushValue(const QPalette & = QPalette()) const;
425 void brushValues(QBrush *c, const QPalette & = QPalette()) const;
426
427 BorderStyle styleValue() const;
428 void styleValues(BorderStyle *s) const;
429
430 Origin originValue() const;
431 Repeat repeatValue() const;
432 Qt::Alignment alignmentValue() const;
433 PositionMode positionValue() const;
434 Attachment attachmentValue() const;
435 int styleFeaturesValue() const;
436
437 bool intValue(int *i, const char *unit = 0) const;
438 bool realValue(qreal *r, const char *unit = 0) const;
439
440 QSize sizeValue() const;
441 QRect rectValue() const;
442 QString uriValue() const;
443 QIcon iconValue() const;
444
445 void borderImageValue(QString *image, int *cuts, TileMode *h, TileMode *v) const;
446};
447
448const quint64 PseudoClass_Unknown = Q_UINT64_C(0x0000000000000000);
449const quint64 PseudoClass_Enabled = Q_UINT64_C(0x0000000000000001);
450const quint64 PseudoClass_Disabled = Q_UINT64_C(0x0000000000000002);
451const quint64 PseudoClass_Pressed = Q_UINT64_C(0x0000000000000004);
452const quint64 PseudoClass_Focus = Q_UINT64_C(0x0000000000000008);
453const quint64 PseudoClass_Hover = Q_UINT64_C(0x0000000000000010);
454const quint64 PseudoClass_Checked = Q_UINT64_C(0x0000000000000020);
455const quint64 PseudoClass_Unchecked = Q_UINT64_C(0x0000000000000040);
456const quint64 PseudoClass_Indeterminate = Q_UINT64_C(0x0000000000000080);
457const quint64 PseudoClass_Unspecified = Q_UINT64_C(0x0000000000000100);
458const quint64 PseudoClass_Selected = Q_UINT64_C(0x0000000000000200);
459const quint64 PseudoClass_Horizontal = Q_UINT64_C(0x0000000000000400);
460const quint64 PseudoClass_Vertical = Q_UINT64_C(0x0000000000000800);
461const quint64 PseudoClass_Window = Q_UINT64_C(0x0000000000001000);
462const quint64 PseudoClass_Children = Q_UINT64_C(0x0000000000002000);
463const quint64 PseudoClass_Sibling = Q_UINT64_C(0x0000000000004000);
464const quint64 PseudoClass_Default = Q_UINT64_C(0x0000000000008000);
465const quint64 PseudoClass_First = Q_UINT64_C(0x0000000000010000);
466const quint64 PseudoClass_Last = Q_UINT64_C(0x0000000000020000);
467const quint64 PseudoClass_Middle = Q_UINT64_C(0x0000000000040000);
468const quint64 PseudoClass_OnlyOne = Q_UINT64_C(0x0000000000080000);
469const quint64 PseudoClass_PreviousSelected = Q_UINT64_C(0x0000000000100000);
470const quint64 PseudoClass_NextSelected = Q_UINT64_C(0x0000000000200000);
471const quint64 PseudoClass_Flat = Q_UINT64_C(0x0000000000400000);
472const quint64 PseudoClass_Left = Q_UINT64_C(0x0000000000800000);
473const quint64 PseudoClass_Right = Q_UINT64_C(0x0000000001000000);
474const quint64 PseudoClass_Top = Q_UINT64_C(0x0000000002000000);
475const quint64 PseudoClass_Bottom = Q_UINT64_C(0x0000000004000000);
476const quint64 PseudoClass_Exclusive = Q_UINT64_C(0x0000000008000000);
477const quint64 PseudoClass_NonExclusive = Q_UINT64_C(0x0000000010000000);
478const quint64 PseudoClass_Frameless = Q_UINT64_C(0x0000000020000000);
479const quint64 PseudoClass_ReadOnly = Q_UINT64_C(0x0000000040000000);
480const quint64 PseudoClass_Active = Q_UINT64_C(0x0000000080000000);
481const quint64 PseudoClass_Closable = Q_UINT64_C(0x0000000100000000);
482const quint64 PseudoClass_Movable = Q_UINT64_C(0x0000000200000000);
483const quint64 PseudoClass_Floatable = Q_UINT64_C(0x0000000400000000);
484const quint64 PseudoClass_Minimized = Q_UINT64_C(0x0000000800000000);
485const quint64 PseudoClass_Maximized = Q_UINT64_C(0x0000001000000000);
486const quint64 PseudoClass_On = Q_UINT64_C(0x0000002000000000);
487const quint64 PseudoClass_Off = Q_UINT64_C(0x0000004000000000);
488const quint64 PseudoClass_Editable = Q_UINT64_C(0x0000008000000000);
489const quint64 PseudoClass_Item = Q_UINT64_C(0x0000010000000000);
490const quint64 PseudoClass_Closed = Q_UINT64_C(0x0000020000000000);
491const quint64 PseudoClass_Open = Q_UINT64_C(0x0000040000000000);
492const quint64 PseudoClass_EditFocus = Q_UINT64_C(0x0000080000000000);
493const quint64 PseudoClass_Alternate = Q_UINT64_C(0x0000100000000000);
494// The Any specifier is never generated, but can be used as a wildcard in searches.
495const quint64 PseudoClass_Any = Q_UINT64_C(0x0000ffffffffffff);
496const int NumPseudos = 46;
497
498struct Q_GUI_EXPORT Pseudo
499{
500 Pseudo() : negated(false) { }
501 quint64 type;
502 QString name;
503 QString function;
504 bool negated;
505};
506
507struct Q_GUI_EXPORT AttributeSelector
508{
509 enum ValueMatchType {
510 NoMatch,
511 MatchEqual,
512 MatchContains,
513 MatchBeginsWith
514 };
515 inline AttributeSelector() : valueMatchCriterium(NoMatch) {}
516
517 QString name;
518 QString value;
519 ValueMatchType valueMatchCriterium;
520};
521
522struct Q_GUI_EXPORT BasicSelector
523{
524 inline BasicSelector() : relationToNext(NoRelation) {}
525
526 enum Relation {
527 NoRelation,
528 MatchNextSelectorIfAncestor,
529 MatchNextSelectorIfParent,
530 MatchNextSelectorIfPreceeds
531 };
532
533 QString elementName;
534
535 QStringList ids;
536 QVector<Pseudo> pseudos;
537 QVector<AttributeSelector> attributeSelectors;
538
539 Relation relationToNext;
540};
541
542struct Q_GUI_EXPORT Selector
543{
544 QVector<BasicSelector> basicSelectors;
545 int specificity() const;
546 quint64 pseudoClass(quint64 *negated = 0) const;
547 QString pseudoElement() const;
548};
549
550struct StyleRule;
551struct MediaRule;
552struct PageRule;
553struct ImportRule;
554
555struct Q_GUI_EXPORT ValueExtractor
556{
557 ValueExtractor(const QVector<Declaration> &declarations, const QPalette & = QPalette());
558
559 bool extractFont(QFont *font, int *fontSizeAdjustment);
560 bool extractBackground(QBrush *, QString *, Repeat *, Qt::Alignment *, QCss::Origin *, QCss::Attachment *,
561 QCss::Origin *);
562 bool extractGeometry(int *w, int *h, int *minw, int *minh, int *maxw, int *maxh);
563 bool extractPosition(int *l, int *t, int *r, int *b, QCss::Origin *, Qt::Alignment *,
564 QCss::PositionMode *, Qt::Alignment *);
565 bool extractBox(int *margins, int *paddings, int *spacing = 0);
566 bool extractBorder(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii);
567 bool extractOutline(int *borders, QBrush *colors, BorderStyle *Styles, QSize *radii, int *offsets);
568 bool extractPalette(QBrush *fg, QBrush *sfg, QBrush *sbg, QBrush *abg);
569 int extractStyleFeatures();
570 bool extractImage(QIcon *icon, Qt::Alignment *a, QSize *size);
571
572 int lengthValue(const Declaration &decl);
573
574private:
575 void extractFont();
576 void borderValue(const Declaration &decl, int *width, QCss::BorderStyle *style, QBrush *color);
577 LengthData lengthValue(const Value& v);
578 void lengthValues(const Declaration &decl, int *m);
579 QSize sizeValue(const Declaration &decl);
580 void sizeValues(const Declaration &decl, QSize *radii);
581
582 QVector<Declaration> declarations;
583 QFont f;
584 int adjustment;
585 int fontExtracted;
586 QPalette pal;
587};
588
589struct Q_GUI_EXPORT StyleRule
590{
591 StyleRule() : order(0) { }
592 QVector<Selector> selectors;
593 QVector<Declaration> declarations;
594 int order;
595};
596
597struct Q_GUI_EXPORT MediaRule
598{
599 QStringList media;
600 QVector<StyleRule> styleRules;
601};
602
603struct Q_GUI_EXPORT PageRule
604{
605 QString selector;
606 QVector<Declaration> declarations;
607};
608
609struct Q_GUI_EXPORT ImportRule
610{
611 QString href;
612 QStringList media;
613};
614
615enum StyleSheetOrigin {
616 StyleSheetOrigin_Unspecified,
617 StyleSheetOrigin_UserAgent,
618 StyleSheetOrigin_User,
619 StyleSheetOrigin_Author,
620 StyleSheetOrigin_Inline
621};
622
623struct Q_GUI_EXPORT StyleSheet
624{
625 StyleSheet() : origin(StyleSheetOrigin_Unspecified), depth(0) { }
626 QVector<StyleRule> styleRules; //only contains rules that are not indexed
627 QVector<MediaRule> mediaRules;
628 QVector<PageRule> pageRules;
629 QVector<ImportRule> importRules;
630 StyleSheetOrigin origin;
631 int depth; // applicable only for inline style sheets
632 QMultiHash<QString, StyleRule> nameIndex;
633 QMultiHash<QString, StyleRule> idIndex;
634 void buildIndexes(Qt::CaseSensitivity nameCaseSensitivity = Qt::CaseSensitive);
635};
636
637class Q_GUI_EXPORT StyleSelector
638{
639public:
640 StyleSelector() : nameCaseSensitivity(Qt::CaseSensitive) {}
641 virtual ~StyleSelector();
642
643 union NodePtr {
644 void *ptr;
645 int id;
646 };
647
648 QVector<StyleRule> styleRulesForNode(NodePtr node);
649 QVector<Declaration> declarationsForNode(NodePtr node, const char *extraPseudo = 0);
650
651 virtual bool nodeNameEquals(NodePtr node, const QString& nodeName) const;
652 virtual QString attribute(NodePtr node, const QString &name) const = 0;
653 virtual bool hasAttributes(NodePtr node) const = 0;
654 virtual QStringList nodeIds(NodePtr node) const;
655 virtual QStringList nodeNames(NodePtr node) const = 0;
656 virtual bool isNullNode(NodePtr node) const = 0;
657 virtual NodePtr parentNode(NodePtr node) const = 0;
658 virtual NodePtr previousSiblingNode(NodePtr node) const = 0;
659 virtual NodePtr duplicateNode(NodePtr node) const = 0;
660 virtual void freeNode(NodePtr node) const = 0;
661
662 QVector<StyleSheet> styleSheets;
663 QString medium;
664 Qt::CaseSensitivity nameCaseSensitivity;
665private:
666 void matchRule(NodePtr node, const StyleRule &rules, StyleSheetOrigin origin,
667 int depth, QMap<uint, StyleRule> *weightedRules);
668 bool selectorMatches(const Selector &rule, NodePtr node);
669 bool basicSelectorMatches(const BasicSelector &rule, NodePtr node);
670};
671
672enum TokenType {
673 NONE,