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