00001
00002
00003 #ifndef _G_CSS_H_
00004 #define _G_CSS_H_
00005
00008
00009 #include "LgiInc.h"
00010 #include "LgiOsDefs.h"
00011 #include "GMem.h"
00012 #include "Gdc2.h"
00013 #include "GAutoPtr.h"
00014 #include "GString.h"
00015 #include "GHashTable.h"
00016
00017 #pragma pack(push, 1)
00018
00020 class LgiClass GCss
00021 {
00022 public:
00023 enum ParsingStyle {
00024 ParseStrict,
00025 ParseRelaxed,
00026 };
00027
00028 enum PropTypes {
00029 TypeEnum = 1,
00030 TypeLen,
00031 TypeGRect,
00032 TypeColor,
00033 TypeImage,
00034 TypeBorder,
00035 TypeStrings,
00036 };
00037
00041 enum PropType {
00042 PropNull = 0,
00043
00044
00045 PropDisplay = TypeEnum<<8,
00046 PropFloat,
00047 PropPosition,
00048 PropOverflow,
00049 PropVisibility,
00050 PropFontStyle,
00051 PropFontVariant,
00052 PropFontWeight,
00053 PropBackgroundRepeat,
00054 PropBackgroundAttachment,
00055 PropTextDecoration,
00056 PropWordWrap,
00057 PropListStyleType,
00058 PropLetterSpacing,
00059 PropFont,
00060 PropBorderCollapse,
00061
00062
00063 PropZIndex = TypeLen<<8,
00064 PropWidth,
00065 PropMinWidth,
00066 PropMaxWidth,
00067 PropHeight,
00068 PropMinHeight,
00069 PropMaxHeight,
00070 PropTop,
00071 PropRight,
00072 PropBottom,
00073 PropLeft,
00074 PropMargin,
00075 PropMarginTop,
00076 PropMarginRight,
00077 PropMarginBottom,
00078 PropMarginLeft,
00079 PropPadding,
00080 PropPaddingTop,
00081 PropPaddingRight,
00082 PropPaddingBottom,
00083 PropPaddingLeft,
00084 PropLineHeight,
00085 PropVerticalAlign,
00086 PropFontSize,
00087 PropBackgroundX,
00088 PropBackgroundY,
00089 PropTextAlign,
00090 PropBorderSpacing,
00091 Prop_CellPadding,
00092
00093
00094 PropClip = TypeGRect<<8,
00095
00096
00097 PropColor = TypeColor<<8,
00098 PropBackground,
00099 PropBackgroundColor,
00100
00101
00102 PropBackgroundImage = TypeImage<<8,
00103
00104
00105 PropBorder = TypeBorder <<8,
00106 PropBorderTop,
00107 PropBorderRight,
00108 PropBorderBottom,
00109 PropBorderLeft,
00110
00111
00112 PropFontFamily = TypeStrings<<8,
00113 };
00114
00115 enum BorderCollapseType {
00116 CollapseInherit,
00117 CollapseCollapse,
00118 CollapseSeparate,
00119 };
00120
00121 enum WordWrapType {
00122 WrapNormal,
00123 WrapBreakWord,
00124 };
00125
00126 enum DisplayType {
00127 DispInherit,
00128 DispBlock,
00129 DispInline,
00130 DispInlineBlock,
00131 DispListItem,
00132 DispNone,
00133 };
00134
00135 enum PositionType {
00136 PosInherit,
00137 PosStatic,
00138 PosRelative,
00139 PosAbsolute,
00140 PosFixed,
00141 };
00142
00143 enum LengthType {
00144
00145
00146 LenInherit,
00147 LenAuto,
00148 LenPx,
00149 LenPt,
00150 LenEm,
00151 LenEx,
00152 LenCm,
00153 LenPercent,
00154 LenNormal,
00155
00156
00157 AlignLeft,
00158 AlignRight,
00159 AlignCenter,
00160 AlignJustify,
00161
00162
00163 VerticalBaseline,
00164 VerticalSub,
00165 VerticalSuper,
00166 VerticalTop,
00167 VerticalTextTop,
00168 VerticalMiddle,
00169 VerticalBottom,
00170 VerticalTextBottom,
00171
00172
00173 SizeXXSmall,
00174 SizeXSmall,
00175 SizeSmall,
00176 SizeMedium,
00177 SizeLarge,
00178 SizeXLarge,
00179 SizeXXLarge,
00180
00181
00182 SizeSmaller,
00183 SizeLarger,
00184 };
00185
00186 enum FloatType {
00187 FloatInherit,
00188 FloatLeft,
00189 FloatRight,
00190 FloatNone,
00191 };
00192
00193 enum OverflowType {
00194 OverflowInherit,
00195 OverflowVisible,
00196 OverflowHidden,
00197 OverflowScroll,
00198 OverflowAuto,
00199 };
00200
00201 enum VisibilityType {
00202 VisibilityInherit,
00203 VisibilityVisible,
00204 VisibilityHidden,
00205 VisibilityCollapse
00206 };
00207
00208 enum FontStyleType {
00209 FontStyleInherit,
00210 FontStyleNormal,
00211 FontStyleItalic,
00212 FontStyleOblique,
00213 };
00214
00215 enum FontVariantType {
00216 FontVariantInherit,
00217 FontVariantNormal,
00218 FontVariantSmallCaps,
00219 };
00220
00221 enum FontWeightType {
00222 FontWeightInherit,
00223 FontWeightNormal,
00224 FontWeightBold,
00225 FontWeightBolder,
00226 FontWeightLighter,
00227 FontWeight100,
00228 FontWeight200,
00229 FontWeight300,
00230 FontWeight400,
00231 FontWeight500,
00232 FontWeight600,
00233 FontWeight700,
00234 FontWeight800,
00235 FontWeight900,
00236 };
00237
00238 enum TextDecorType {
00239 TextDecorInherit,
00240 TextDecorNone,
00241 TextDecorUnderline,
00242 TextDecorOverline,
00243 TextDecorLineThrough,
00244 TextDecorBlink,
00245 };
00246
00247 enum ColorType {
00248 ColorInherit,
00249 ColorTransparent,
00250 ColorRgb,
00251 ColorLinearGradient,
00252 ColorRadialGradient,
00253 };
00254
00255 enum RepeatType {
00256 RepeatInherit,
00257 RepeatBoth,
00258 RepeatX,
00259 RepeatY,
00260 RepeatNone,
00261 };
00262
00263 enum AttachmentType {
00264 AttachmentInherit,
00265 AttachmentScroll,
00266 AttachmentFixed,
00267 };
00268
00269 enum ImageType {
00270 ImageInherit,
00271 ImageOwn,
00272 ImageRef,
00273 };
00274
00275 struct LgiClass Len
00276 {
00277 LengthType Type;
00278 float Value;
00279
00280 Len(char *init = 0)
00281 {
00282 Type = LenInherit;
00283 Value = 0.0;
00284 if (init)
00285 Parse(init);
00286 }
00287
00288 Len(LengthType t)
00289 {
00290 Type = t;
00291 Value = 0.0;
00292 }
00293
00294 bool Parse(char *&s, ParsingStyle Type = ParseStrict);
00295 bool IsValid() { return Type != LenInherit; }
00296 bool IsDynamic() { return Type == LenPercent || Type == LenInherit; }
00297 bool operator !=(Len &l) { return Type != l.Type || Value != l.Value; }
00298 bool ToString(GStream &p);
00299 };
00300
00301 struct LgiClass ColorStop
00302 {
00303 float Pos;
00304 uint32 Rgb32;
00305 };
00306
00307 struct LgiClass ColorDef
00308 {
00309 ColorType Type;
00310 uint32 Rgb32;
00311 GArray<ColorStop> Stops;
00312
00313 ColorDef(int init = 0)
00314 {
00315 Type = init ? ColorRgb : ColorInherit;
00316 Rgb32 = init;
00317 }
00318
00319 bool Parse(char *&s);
00320 bool operator !=(ColorDef &c)
00321 {
00322 return Type != c.Type ||
00323 Rgb32 != c.Rgb32;
00324 }
00325 ColorDef &operator =(const ColorDef &c)
00326 {
00327 Type = c.Type;
00328 Rgb32 = c.Rgb32;
00329 Stops = c.Stops;
00330 return *this;
00331 }
00332 bool ToString(GStream &p);
00333 };
00334
00335 enum BorderStyle {
00336 BorderNone,
00337 BorderHidden,
00338 BorderDotted,
00339 BorderDashed,
00340 BorderSolid,
00341 BorderDouble,
00342 BorderGroove,
00343 BorderRidge,
00344 BorderInset,
00345 BorderOutset,
00346 };
00347
00348 struct LgiClass BorderDef : public Len
00349 {
00350 ColorDef Color;
00351 BorderStyle Style;
00352
00353 BorderDef(char *init = 0)
00354 {
00355 Style = BorderNone;
00356 if (init)
00357 Parse(init);
00358 }
00359
00360 BorderDef(const BorderDef &db) : Len(db)
00361 {
00362 Color = db.Color;
00363 Style = db.Style;
00364 }
00365
00366 bool Parse(char *&s);
00367 BorderDef &operator =(const BorderDef &b)
00368 {
00369 Style = b.Style;
00370 Color = b.Color;
00371 Type = b.Type;
00372 Value = b.Value;
00373 return *this;
00374 }
00375 };
00376
00377 struct LgiClass ImageDef
00378 {
00379 ImageType Type;
00380 GAutoString Ref;
00381 GSurface *Img;
00382
00383 ImageDef()
00384 {
00385 Type = ImageInherit;
00386 Img = 0;
00387 }
00388
00389 ImageDef(const ImageDef &o)
00390 {
00391 *this = o;
00392 }
00393
00394 ~ImageDef()
00395 {
00396 if (Type == ImageOwn)
00397 DeleteObj(Img);
00398 }
00399
00400 bool Parse(char *&s);
00401 bool operator !=(const ImageDef &i)
00402 {
00403 if (Type != i.Type)
00404 return false;
00405 if (Ref && i.Ref)
00406 return stricmp(Ref, i.Ref) == 0;
00407 return true;
00408 }
00409
00410 ImageDef &operator =(const ImageDef &o)
00411 {
00412 Type = o.Type;
00413 Ref.Reset(NewStr(o.Ref));
00414 DeleteObj(Img);
00415 if (o.Img) Img = new GMemDC(o.Img);
00416 return *this;
00417 }
00418 };
00419
00420 class LgiClass StringsDef : public GArray<char*>
00421 {
00422 public:
00423 StringsDef(char *init = 0)
00424 {
00425 if (ValidStr(init))
00426 *this = init;
00427 else
00428 LgiAssert(init == 0);
00429 }
00430 StringsDef(const StringsDef &c) { *this = c; }
00431 ~StringsDef() { Empty(); }
00432 StringsDef &operator =(char *s) { Parse(s); return *this; }
00433 void Empty() { DeleteArrays(); }
00434
00435 StringsDef &operator =(const StringsDef &s)
00436 {
00437 Empty();
00438
00439 for (int i=0; i<s.Length(); i++)
00440 {
00441 char *str = ((StringsDef&)s)[i];
00442 if (ValidStr(str))
00443 Add(NewStr(str));
00444 else
00445 LgiAssert(!"Not a valid string.");
00446 }
00447
00448 return *this;
00449 }
00450
00451 bool operator !=(StringsDef &s)
00452 {
00453 if (Length() != s.Length())
00454 return true;
00455
00456 for (int i=0; i<Length(); i++)
00457 {
00458 char *a = (*this)[i];
00459 char *b = s[i];
00460
00461 if (stricmp(a, b))
00462 return true;
00463 }
00464
00465 return false;
00466 }
00467
00468 bool Parse(char *&s)
00469 {
00470 Empty();
00471
00472 char Delimiters[] = ";, \t\r\n";
00473 while (s && *s && *s != ';')
00474 {
00475 while (*s && strchr(Delimiters, *s)) s++;
00476 if (*s == '\'' || *s == '\"')
00477 {
00478 char Delim = *s++;
00479 char *Start = s;
00480 while (*s && *s != Delim) s++;
00481
00482 if (s > Start)
00483 {
00484 char *n = NewStr(Start, s-Start);
00485 if (ValidStr(n))
00486 Add(n);
00487 else
00488 LgiAssert(!"Not a valid string.");
00489 }
00490
00491 if (s) s++;
00492 }
00493 else
00494 {
00495 char *Start = s;
00496 while (*s && !strchr(Delimiters, *s)) s++;
00497
00498 if (s > Start)
00499 {
00500 char *n = NewStr(Start, s-Start);
00501 if (ValidStr(n))
00502 Add(n);
00503 else
00504 LgiAssert(!"Not a valid string.");
00505 }
00506 }
00507 }
00508
00509 return true;
00510 }
00511 };
00512
00513 GCss();
00514 GCss(const GCss &c);
00515 virtual ~GCss();
00516
00517 #define Accessor(PropName, Type, Default) \
00518 Type PropName() { Type *Member = (Type*)Props.Find(Prop##PropName); \
00519 if (Member) return *Member; \
00520 return Default; } \
00521 void PropName(Type t) { Type *Member = (Type*)Props.Find(Prop##PropName); \
00522 if (Member) *Member = t; \
00523 else { Props.Add(Prop##PropName, Member = new Type); \
00524 *Member = t; } \
00525 OnChange(Prop##PropName); }
00526
00527 Accessor(Display, DisplayType, DispInherit);
00528 Accessor(Float, FloatType, FloatInherit);
00529 Accessor(Position, PositionType, PosInherit);
00530 Accessor(ZIndex, Len, Len());
00531
00532 Accessor(TextAlign, Len, Len());
00533 Accessor(VerticalAlign, Len, Len());
00534
00535 Accessor(Width, Len, Len());
00536 Accessor(MinWidth, Len, Len());
00537 Accessor(MaxWidth, Len, Len());
00538
00539 Accessor(Height, Len, Len());
00540 Accessor(MinHeight, Len, Len());
00541 Accessor(MaxHeight, Len, Len());
00542 Accessor(LineHeight, Len, Len());
00543
00544 Accessor(Top, Len, Len());
00545 Accessor(Right, Len, Len());
00546 Accessor(Bottom, Len, Len());
00547 Accessor(Left, Len, Len());
00548
00549 Accessor(MarginTop, Len, Len());
00550 Accessor(MarginRight, Len, Len());
00551 Accessor(MarginBottom, Len, Len());
00552 Accessor(MarginLeft, Len, Len());
00553
00554 Accessor(PaddingTop, Len, Len());
00555 Accessor(PaddingRight, Len, Len());
00556 Accessor(PaddingBottom, Len, Len());
00557 Accessor(PaddingLeft, Len, Len());
00558
00559 Accessor(BorderTop, BorderDef, BorderDef());
00560 Accessor(BorderRight, BorderDef, BorderDef());
00561 Accessor(BorderBottom, BorderDef, BorderDef());
00562 Accessor(BorderLeft, BorderDef, BorderDef());
00563 Accessor(BorderSpacing, Len, Len());
00564 Accessor(_CellPadding, Len, Len());
00565
00566 Accessor(Overflow, OverflowType, OverflowInherit);
00567 Accessor(Clip, GRect, GRect(0, 0, -1, -1));
00568 Accessor(Visibility, VisibilityType, VisibilityInherit);
00569
00570 Accessor(FontFamily, StringsDef, StringsDef());
00571 Accessor(FontSize, Len, Len());
00572 Accessor(FontStyle, FontStyleType, FontStyleInherit);
00573 Accessor(FontVariant, FontVariantType, FontVariantInherit);
00574 Accessor(FontWeight, FontWeightType, FontWeightInherit);
00575 Accessor(TextDecoration, TextDecorType, TextDecorInherit);
00576
00577 Accessor(Color, ColorDef, ColorDef());
00578 Accessor(BackgroundColor, ColorDef, ColorDef());
00579 Accessor(BackgroundImage, ImageDef, ImageDef());
00580 Accessor(BackgroundRepeat, RepeatType, RepeatInherit);
00581 Accessor(BackgroundAttachment, AttachmentType, AttachmentInherit);
00582 Accessor(BackgroundX, Len, Len());
00583 Accessor(BackgroundY, Len, Len());
00584
00585 void Empty();
00586 void DeleteProp(PropType p);
00587 virtual void OnChange(PropType Prop);
00588 virtual bool Parse(char *&Defs, ParsingStyle Type = ParseStrict);
00589 bool operator ==(GCss &c);
00590 bool operator !=(GCss &c) { return !(*this == c); }
00591 bool CopyStyle(const GCss &c);
00592 GCss &operator =(const GCss &c) { CopyStyle(c); return *this; }
00593 void *PropAddress(PropType p) { return Props.Find(p); }
00594 bool ApplyInherit(GCss &c, GArray<PropType> *Types = 0);
00595 GAutoString ToString();
00596
00597 protected:
00598 inline void DeleteProp(PropType p, void *Ptr);
00599 GHashTbl<int, void*> Props;
00600 static GHashTbl<char*, PropType> Lut;
00601 static char *PropName(PropType p);
00602
00603 bool ParseFontStyle(PropType p, char *&s);
00604 bool ParseFontVariant(PropType p, char *&s);
00605 bool ParseFontWeight(PropType p, char *&s);
00606 virtual bool OnUnhandledColor(ColorDef *def, char *&s) { return false; }
00607 };
00608
00609 #pragma pack(pop)
00610
00611 #endif