colortxt.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. unit ColorTxt;
  2. {
  3. TColoredText is a descendent of TStaticText designed to allow the writing
  4. of colored text when color monitors are used. With a monochrome or BW
  5. monitor, TColoredText acts the same as TStaticText.
  6. TColoredText is used in exactly the same way as TStaticText except that
  7. the constructor has an extra Byte parameter specifying the attribute
  8. desired. (Do not use a 0 attribute, black on black).
  9. }
  10. {$i platform.inc}
  11. {$ifdef PPC_FPC}
  12. {$H-}
  13. {$else}
  14. {$F+,O+,E+,N+}
  15. {$endif}
  16. {$X+,R-,I-,Q-,V-}
  17. {$ifndef OS_UNIX}
  18. {$S-}
  19. {$endif}
  20. interface
  21. uses
  22. Objects, Drivers, Views, Dialogs, App;
  23. type
  24. PColoredText = ^TColoredText;
  25. TColoredText = object(TStaticText)
  26. Attr : Byte;
  27. constructor Init(var Bounds: TRect; const AText: String; Attribute : Byte);
  28. constructor Load(var S: TStream);
  29. function GetTheColor : byte; virtual;
  30. procedure Draw; virtual;
  31. procedure Store(var S: TStream);
  32. end;
  33. const
  34. RColoredText: TStreamRec = (
  35. ObjType: 611;
  36. VmtLink: Ofs(TypeOf(TColoredText)^);
  37. Load: @TColoredText.Load;
  38. Store: @TColoredText.Store
  39. );
  40. implementation
  41. constructor TColoredText.Init(var Bounds: TRect; const AText: String;
  42. Attribute : Byte);
  43. begin
  44. TStaticText.Init(Bounds, AText);
  45. Attr := Attribute;
  46. end;
  47. constructor TColoredText.Load(var S: TStream);
  48. begin
  49. TStaticText.Load(S);
  50. S.Read(Attr, Sizeof(Attr));
  51. end;
  52. procedure TColoredText.Store(var S: TStream);
  53. begin
  54. TStaticText.Store(S);
  55. S.Write(Attr, Sizeof(Attr));
  56. end;
  57. function TColoredText.GetTheColor : byte;
  58. begin
  59. if AppPalette = apColor then
  60. GetTheColor := Attr
  61. else
  62. GetTheColor := GetColor(1);
  63. end;
  64. procedure TColoredText.Draw;
  65. var
  66. Color: Byte;
  67. Center: Boolean;
  68. I, J, L, P, Y: Sw_Integer;
  69. B: TDrawBuffer;
  70. S: String;
  71. begin
  72. Color := GetTheColor;
  73. GetText(S);
  74. L := Length(S);
  75. P := 1;
  76. Y := 0;
  77. Center := False;
  78. while Y < Size.Y do
  79. begin
  80. MoveChar(B, ' ', Color, Size.X);
  81. if P <= L then
  82. begin
  83. if S[P] = #3 then
  84. begin
  85. Center := True;
  86. Inc(P);
  87. end;
  88. I := P;
  89. repeat
  90. J := P;
  91. while (P <= L) and (S[P] = ' ') do Inc(P);
  92. while (P <= L) and (S[P] <> ' ') and (S[P] <> #13) do Inc(P);
  93. until (P > L) or (P >= I + Size.X) or (S[P] = #13);
  94. if P > I + Size.X then
  95. if J > I then P := J else P := I + Size.X;
  96. if Center then J := (Size.X - P + I) div 2 else J := 0;
  97. MoveBuf(B[J], S[I], Color, P - I);
  98. while (P <= L) and (S[P] = ' ') do Inc(P);
  99. if (P <= L) and (S[P] = #13) then
  100. begin
  101. Center := False;
  102. Inc(P);
  103. if (P <= L) and (S[P] = #10) then Inc(P);
  104. end;
  105. end;
  106. WriteLine(0, Y, Size.X, 1, B);
  107. Inc(Y);
  108. end;
  109. end;
  110. end.