colortxt.pas 2.8 KB

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