home *** CD-ROM | disk | FTP | other *** search
- {*******************************************************}
- { }
- { jardas.com Visual Component Library }
- { }
- { Copyright (c) 2001-2002 jardas.com }
- { }
- { http://www.jardas.com }
- { }
- {*******************************************************}
-
- unit JCommon;
-
- interface
-
- uses Windows, Graphics, Classes, Controls, Forms, StdCtrls, SysUtils;
-
- type
- TJStyle = (jsNormal, jsFlat);
- TJTextStyle = (tsNormal, tsEllipsis, tsFilePath);
- TJTextInfo = record
- Alignment: TAlignment;
- Enabled: Boolean;
- Layout: TTextLayout;
- Font: TFont;
- ShowAccelChar: Boolean;
- Style: TJTextStyle;
- Text: String;
- WordWrap: Boolean;
- end;
-
-
- function InitTextInfo: TJTextInfo;
- procedure DoDrawFrame(ACanvas: TCanvas; ARect: TRect; AStyle: TJStyle; ADown: Boolean);
- function CalckTextRect(ACanvas: TCanvas; ARect: TRect; Info: TJTextInfo): TRect;
- procedure DoDrawText(ACanvas: TCanvas; ARect: TRect; Info: TJTextInfo);
- function GetCheckRect(ARect: TRect): TRect;
- procedure DrawCheckBox(Canvas: TCanvas; ARect: TRect; Checked: Boolean);
- procedure ShrinkRect(var ARect: TRect; n: Integer);
- function EmptyRect: TRect;
- function GetHTMLTextInfo(I: TJTextInfo): String;
- function GetHTMLColor(c: TColor): String;
- function GetCSVText(S: String): String;
-
- implementation
-
-
- function InitTextInfo: TJTextInfo;
- begin
- with Result do
- begin
- Alignment := taLeftJustify;
- Enabled := True;
- Layout := tlTop;
- ShowAccelChar := False;
- Style := tsNormal;
- Text := '';
- WordWrap := False;
- end;
- end;
-
- procedure DoDrawFrame(ACanvas: TCanvas; ARect: TRect; AStyle: TJStyle;
- ADown: Boolean);
- var
- Style: LongInt;
- begin
- case AStyle of
- jsNormal: begin
- Style := 0;
- if ADown then
- Style := BF_FLAT;
- DrawEdge(ACanvas.Handle, ARect, EDGE_RAISED, BF_TOPLEFT or Style);
- DrawEdge(ACanvas.Handle, ARect, EDGE_RAISED, BF_BOTTOMRIGHT or Style);
- end;
- jsFlat: begin
- if not ADown then
- begin
- DrawEdge(ACanvas.Handle, ARect, BDR_RAISEDINNER, BF_TOPLEFT);
- DrawEdge(ACanvas.Handle, ARect, BDR_RAISEDINNER, BF_BOTTOMRIGHT);
- end else
- begin
- DrawEdge(ACanvas.Handle, ARect, BDR_SUNKENINNER, BF_TOPLEFT);
- DrawEdge(ACanvas.Handle, ARect, BDR_SUNKENINNER, BF_BOTTOMRIGHT);
- end;
- end;
- end;
- end;
-
- function CalckTextRect(ACanvas: TCanvas; ARect: TRect; Info: TJTextInfo): TRect;
- const
- Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
- var
- Style: LongInt;
- R: TRect;
- begin
- if Info.Text <> '' then
- begin
- ACanvas.Font.Assign(Info.Font);
- if Info.WordWrap then
- Style := DT_WORDBREAK
- else
- Style := DT_SINGLELINE;
- Style := Style or Alignments[Info.Alignment] or DT_EXPANDTABS or DT_NOCLIP;
- if not Info.ShowAccelChar then
- Style := Style or DT_NOPREFIX;
- R := ARect;
- DrawText(ACanvas.Handle, PChar(Info.Text), Length(Info.Text), R, Style or DT_CALCRECT);
- Result := R;
- end else
- begin
- Result := R;
- Result.Right := Result.Left;
- end;
- end;
-
- procedure _DrawText(ACanvas: TCanvas; ARect: TRect; Info: TJTextInfo);
- const
- Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
- var
- Style: LongInt;
- R: TRect;
- h: Integer;
- begin
- if Info.WordWrap then
- Style := DT_WORDBREAK
- else
- Style := DT_SINGLELINE;
- Style := Style or Alignments[Info.Alignment] or DT_EXPANDTABS {or DT_NOCLIP};
- case Info.Style of
- tsEllipsis: Style := Style or DT_END_ELLIPSIS;
- tsFilePath: begin
- ARect.Right := ARect.Right - ACanvas.TextWidth('...');
- Style := Style or DT_PATH_ELLIPSIS;
- end;
- end;
- if not Info.ShowAccelChar then
- Style := Style or DT_NOPREFIX;
- R := ARect;
- h := DrawText(ACanvas.Handle, PChar(Info.Text), Length(Info.Text), R, Style or DT_CALCRECT);
- R := ARect;
- case Info.Layout of
- tlBottom: R.Top := R.Bottom - h - 1;
- tlCenter: begin
- R.Top := R.Top + (R.Bottom - R.Top - h) div 2;
- R.Bottom := R.Top + h;
- end;
- end;
- DrawText(ACanvas.Handle, PChar(Info.Text), Length(Info.Text), R, Style);
- end;
-
- procedure DoDrawText(ACanvas: TCanvas; ARect: TRect; Info: TJTextInfo);
- var
- f: TFont;
- begin
- if not Info.Enabled then
- begin
- ACanvas.Font.Assign(Info.Font);
- OffsetRect(ARect, 1, 1);
- ACanvas.Font.Color := clBtnHighlight;
- _DrawText(ACanvas, ARect, Info);
- OffsetRect(ARect, -1, -1);
- ACanvas.Font.Color := clBtnShadow;
- _DrawText(ACanvas, ARect, Info);
- end else
- begin
- f := ACanvas.Font;
- ACanvas.Font := Info.Font;
- _DrawText(ACanvas, ARect, Info);
- ACanvas.Font := f;
- end;
- end;
-
- procedure ShrinkRect(var ARect: TRect; n: Integer);
- begin
- ARect.Top := ARect.Top + n;
- ARect.Left := ARect.Left + n;
- ARect.Right := ARect.Right - n;
- ARect.Bottom := ARect.Bottom - n;
- end;
-
- function EmptyRect: TRect;
- begin
- Result := Rect(0, 0, 0, 0);
- end;
-
- function GetCheckRect(ARect: TRect): TRect;
- const
- CKBOX_SIZE = 13;
- var
- X, Y: Integer;
- begin
- X := ((ARect.Right - ARect.Left) - CKBOX_SIZE) div 2;
- Y := ((ARect.Bottom - ARect.Top) - CKBOX_SIZE) div 2;
- Result := Rect(ARect.Left + X, ARect.Top + Y, ARect.Left + X + CKBOX_SIZE, ARect.Top + Y + CKBOX_SIZE);
- end;
-
-
- procedure DrawCheckBox(Canvas: TCanvas; ARect: TRect; Checked: Boolean);
- begin
- with Canvas do
- begin
- ARect := GetCheckRect(ARect);
- Brush.Style := bsSolid;
- Brush.Color := clWindow;
- Pen.Style := psSolid;
- Pen.Color := clBtnShadow;
- Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
-
- if Checked then
- begin
- Pen.Width := 1;
- Pen.Color := clBlack;
- InflateRect(ARect, -3, -3);
- MoveTo(ARect.Left, ARect.Top + 2);
- LineTo(ARect.Left + 3, ARect.Bottom - 2);
- MoveTo(ARect.Left, ARect.Top + 3);
- LineTo(ARect.Left + 3, ARect.Bottom - 1);
- MoveTo(ARect.Left, ARect.Top + 4);
- LineTo(ARect.Left + 3, ARect.Bottom);
-
- MoveTo(ARect.Left + 3, ARect.Top + 3);
- LineTo(ARect.Right, ARect.Top - 1);
- MoveTo(ARect.Left + 3, ARect.Top + 4);
- LineTo(ARect.Right, ARect.Top);
- MoveTo(ARect.Left + 3, ARect.Top + 5);
- LineTo(ARect.Right, ARect.Top + 1);
- end;
- end;
- end;
-
- function GetHTMLTextInfo(I: TJTextInfo): String;
- var
- b, e, t: String;
- a: Integer;
- begin
- b := '<p';
- e := '</p>';
- case I.Alignment of
- taCenter: b := b + ' align=center';
- taLeftJustify: b := b + ' align=left';
- taRightJustify: b := b + ' align=right';
- end;
- b := b + '><font';
- e := '</font>' + e;
- b := b + ' face="' + I.Font.Name + '"';
- case I.Font.Size of
- 0..8: b := b + ' size=1';
- 9..10: b := b + ' size=2';
- 11..12: b := b + ' size=3';
- 13..14: b := b + ' size=4';
- 15..18: b := b + ' size=5';
- 19..24: b := b + ' size=6';
- else
- b := b + ' size=7';
- end;
- b := b + ' color=' + GetHTMLColor(I.Font.Color);
- b := b + '>';
- if fsBold in I.Font.Style then
- begin
- b := b + '<b>';
- e := '</b>' + e;
- end;
- if fsItalic in I.Font.Style then
- begin
- b := b + '<i>';
- e := '</i>' + e;
- end;
- if fsUnderline in I.Font.Style then
- begin
- b := b + '<u>';
- e := '</u>' + e;
- end;
-
- t := '';
- for a := 1 to Length(I.Text) do
- begin
- if (I.Text[a] in ['<', '>', ' ']) then
- t := t + '' + IntToStr(Ord(I.Text[a])) + ';'
- else
- t := t + I.Text[a];
- end;
- if t = '' then
- t := ' ';
- Result := b + t + e;
- end;
-
- function GetHTMLColor(c: TColor): String;
- var
- r, g, b: Word;
- begin
- c := ColorToRGB(c);
- r := c shl 24 shr 24;
- g := c shl 16 shr 24;
- b := c shl 8 shr 24;
- Result := '#' + IntToHex(r, 2) + IntToHex(g, 2) + IntToHex(b, 2);
- end;
-
- function GetCSVText(S: String): String;
- var
- a: Integer;
- c: String;
- begin
- Result := '';
- a := 1;
- while a <= Length(S) do
- begin
- c := S[a];
- if S[a] = '"' then
- c := '""';
- if Copy(S, a, a + 1) = #13#10 then
- begin
- c := '\n';
- Inc(a);
- end;
- Result := Result + c;
- Inc(a);
- end;
- end;
- end.
-