Skip to content

Commit 452bd3a

Browse files
Merge pull request #2 from bruzzoneale/bruzzoneale_2023-05
Bruzzoneale 2023 05
2 parents dcc4849 + 8a0230f commit 452bd3a

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

Source/Vcl.DbAwareLabeledUtils.pas

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ function GetStyledColor(Color: TColor): TColor;
6161
function PadL(Const InString: String; Len: Integer; FChar: Char): String;
6262
function PadR(Const InString: String; Len: Integer; FChar: Char): String;
6363

64+
function LightenColor(Color: TColor; Percentage: Cardinal): TColor;
65+
6466
implementation
6567

6668
uses
@@ -69,6 +71,7 @@ implementation
6971
, Vcl.DbAwareLabeledConsts
7072
, Vcl.Controls
7173
, Vcl.Themes
74+
, Vcl.GraphUtil
7275
;
7376

7477
//sostituisce un carattere in un altro all'interno di una stringa (zero-based)
@@ -368,4 +371,15 @@ function PadR(Const InString: String; Len: Integer; FChar: Char): String;
368371
Result := InString + StringOfChar(FChar,Len-Length(InString));
369372
end;
370373

374+
function LightenColor(Color: TColor; Percentage: Cardinal): TColor;
375+
var
376+
rgb: LongInt;
377+
h, s, l: Word;
378+
begin
379+
rgb := ColorToRGB(Color);
380+
ColorRGBToHLS(rgb, h, l, s);
381+
l := (Cardinal(l) * Percentage) div 100;
382+
Result := TColor(ColorHLSToRGB(h, l, s));
383+
end;
384+
371385
end.

Source/Vcl.LabeledDBCtrls.pas

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ TLabeledDbGrid = class(TDBGrid)
319319
FOnIsCheckBoxedColumn: TCBCheckBoxedColumnEvent;
320320
FLinesPerRow: Integer;
321321
FRowMargin: Integer;
322+
FWrapAllText: Boolean;
322323
function TitleOffset: Integer;
323324
procedure OnSearchTimer(Sender : TObject);
324325
procedure SetBoundCaption(const Value: TCaption);
@@ -356,6 +357,7 @@ TLabeledDbGrid = class(TDBGrid)
356357
procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
357358
const AField: TField; Const AColumn: TColumn);
358359
function CalcRowMargin(const ARect: TRect): Integer;
360+
procedure SetWrapAllText(const Value: Boolean);
359361
protected
360362
procedure SetParent(AParent: TWinControl); override;
361363
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
@@ -428,6 +430,7 @@ TLabeledDbGrid = class(TDBGrid)
428430
property UnsortableFields: string read FUnsortableFields write FUnsortableFields;
429431
property CanEditColumn: TCBCanEditColumn read FCanEditColumn write FCanEditColumn;
430432
property RowMargin: Integer read FRowMargin write SetRowMargin default 0;
433+
property WrapAllText: Boolean read FWrapAllText write SetWrapAllText default False;
431434
end;
432435

433436
TNavInsMode = (imInsert, imAppend);
@@ -1045,6 +1048,27 @@ procedure TLabeledDBLookupComboBox.SetBoundCaption(const Value: TCaption);
10451048

10461049
{ TLabeledDbGrid }
10471050

1051+
function TruncStringInRect(ACanvas: TCanvas; ARect: TRect; const AText: string; AOffset: Integer): string;
1052+
const
1053+
DOTS = '...';
1054+
var
1055+
maxDim, idx: Integer ;
1056+
begin
1057+
maxDim := (ARect.Right - ARect.Left + 1) - (AOffset*2) ;
1058+
if ACanvas.TextWidth(AText) <= maxDim then
1059+
Result := AText
1060+
else
1061+
for idx := 1 to Length(AText) do
1062+
begin
1063+
Result := Copy(AText, 1, idx) + DOTS ;
1064+
if ACanvas.TextWidth(Result) > MaxDim then
1065+
begin
1066+
Result := Copy(AText, 1, idx-1) + DOTS ;
1067+
Break ;
1068+
end;
1069+
end;
1070+
end;
1071+
10481072
//Same as VCL source
10491073
procedure TLabeledDbGrid.WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
10501074
const AField: TField; Const AColumn: TColumn);
@@ -1055,14 +1079,15 @@ procedure TLabeledDbGrid.WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integ
10551079
DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
10561080
RTL: array [Boolean] of Integer = (0, DT_RTLREADING);
10571081
var
1058-
Text: string;
1082+
Text, TruncText: string;
10591083
Alignment: TAlignment;
10601084
ARightToLeft: Boolean;
10611085
B, R: TRect;
10621086
Hold, Left: Integer;
10631087
I: TColorRef;
10641088
LFormat: Integer;
10651089
LMemoField: Boolean;
1090+
LWrapText: Boolean;
10661091
begin
10671092
ACanvas.Font.Name := Font.Name;
10681093
ACanvas.Font.Style := Font.Style;
@@ -1071,15 +1096,26 @@ procedure TLabeledDbGrid.WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integ
10711096
//Verifiy that Field is a Memofield
10721097
LMemoField := Assigned(AField) and (AField.DataType in [ftMemo, ftFmtMemo, ftWideMemo]);
10731098
if LMemoField then
1074-
begin
1075-
Text := AField.AsString;
1076-
ACanvas.FillRect(ARect);
1077-
end
1099+
Text := AField.AsString
10781100
else if Assigned(AField) and not (FDrawCheckBoxImages and isCheckBoxedField(AField)) then //Empty Text if drawing checkbox
10791101
Text := AField.DisplayText
10801102
else
10811103
Text := '';
10821104

1105+
TruncText := TruncStringInRect(Canvas, ARect, Text, DX) ;
1106+
1107+
if (LinesPerRow = 1) or (Text = TruncText) or not FWrapAllText or
1108+
((Text <> TruncText) and not Text.Contains(' ')) then
1109+
begin
1110+
Text := TruncText;
1111+
LWrapText := False
1112+
end
1113+
else
1114+
LWrapText := True;
1115+
1116+
if LMemoField or LWrapText then
1117+
ACanvas.FillRect(ARect);
1118+
10831119
Alignment := AColumn.Alignment;
10841120
ARightToLeft := UseRightToLeftAlignmentForField(AField, AColumn.Alignment);
10851121

@@ -1100,7 +1136,7 @@ procedure TLabeledDbGrid.WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integ
11001136
- (ACanvas.TextWidth(Text) div 2);
11011137
end;
11021138
//Se il campo è un memo lo stampa su n righe:
1103-
if LMemoField then
1139+
if LMemoField or LWrapText then
11041140
begin
11051141
LFormat := dt_WordBreak or dt_NoPrefix;
11061142
//Riduce l'area di stampa in base ai margini
@@ -1461,6 +1497,15 @@ procedure TLabeledDbGrid.SetTitleFont(const Value: TFont);
14611497
inherited TitleFont.Assign(Value);
14621498
end;
14631499

1500+
procedure TLabeledDbGrid.SetWrapAllText(const Value: Boolean);
1501+
begin
1502+
if FWrapAllText <> Value then
1503+
begin
1504+
FWrapAllText := Value;
1505+
Invalidate;
1506+
end;
1507+
end;
1508+
14641509
procedure TLabeledDbGrid.StandardSort(Field: TField; var SortOrder: TCBSortOrder);
14651510
var
14661511
DataSet: TDataSet;
@@ -1730,7 +1775,11 @@ procedure TLabeledDbGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
17301775
if DefaultWindowsStyleEnabled then
17311776
CellColor := clInfoBk
17321777
else
1733-
CellColor := GetCellColor;
1778+
//CellColor := GetCellColor;
1779+
begin
1780+
CellColor := LightenColor(GetStyledColor(clHighlight), 150);
1781+
Canvas.Font.Color :=LightenColor(GetStyledColor(clHighlightText), 25);
1782+
end;
17341783
end
17351784
else
17361785
CellColor := GetCellColor;
@@ -1762,6 +1811,26 @@ procedure TLabeledDbGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
17621811
CellColor := GetCellColor;
17631812
end;
17641813

1814+
if dgAlwaysShowSelection in Options then
1815+
begin
1816+
if (DataSource = nil) or (DataSource.DataSet = nil) or (DataSource.DataSet.EOF and DataSource.DataSet.BOF) then
1817+
Canvas.Brush.Color := GetStyledColor(Color)
1818+
else
1819+
if not Focused and ( gdSelected in State ) then
1820+
begin
1821+
if DefaultWindowsStyleEnabled then
1822+
begin
1823+
Canvas.Brush.Color := clInactiveCaption;
1824+
Canvas.Font.Color := clInactiveCaptionText;
1825+
end
1826+
else
1827+
begin
1828+
Canvas.Brush.Color := LightenColor(GetStyledColor(clHighlight), 150);
1829+
Canvas.Font.Color := LightenColor(GetStyledColor(clHighlightText), 25);
1830+
end;
1831+
end;
1832+
end;
1833+
17651834
// Se il tipo di dato è Boolean, mostra in alternativa alle diciture
17661835
// false e true, l'immagine check e uncheck
17671836
if (not (csLoading in ComponentState)) and isCheckBoxedColumn(Column) and FDrawCheckBoxImages then

0 commit comments

Comments
 (0)