From 18e631e722ae2054b8c4f8519db7f8e1d7024522 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 7 Dec 2022 23:48:15 +0000 Subject: [PATCH] GridPrinter: Fix alignment and layout of checkbox columns in preview/print. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8639 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/stringgrid_columns/unit1.lfm | 258 +++++++++++++++++- .../examples/stringgrid_columns/unit1.pas | 52 ++++ components/gridprinter/source/gridprn.pas | 12 +- 3 files changed, 309 insertions(+), 13 deletions(-) diff --git a/components/gridprinter/examples/stringgrid_columns/unit1.lfm b/components/gridprinter/examples/stringgrid_columns/unit1.lfm index 268629da6..ad2e8f59b 100644 --- a/components/gridprinter/examples/stringgrid_columns/unit1.lfm +++ b/components/gridprinter/examples/stringgrid_columns/unit1.lfm @@ -2,36 +2,39 @@ object Form1: TForm1 Left = 256 Height = 324 Top = 134 - Width = 303 + Width = 470 Caption = 'Form1' ClientHeight = 324 - ClientWidth = 303 + ClientWidth = 470 LCLVersion = '2.3.0.0' object StringGrid1: TStringGrid - Left = 0 - Height = 287 - Top = 0 - Width = 303 + Left = 6 + Height = 250 + Top = 31 + Width = 426 Align = alClient + BorderSpacing.Around = 6 ColCount = 4 Columns = < item Title.Caption = 'Col 1' - Width = 64 + Width = 120 end item Alignment = taRightJustify Title.Alignment = taRightJustify Title.Caption = 'Col 2' - Width = 64 + Width = 120 end item ButtonStyle = cbsCheckboxColumn Color = clActiveBorder Title.Alignment = taCenter Title.Caption = 'Col 3' - Width = 64 + Width = 100 end> + DefaultColWidth = 40 + DefaultRowHeight = 40 Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goSmoothScroll, goFixedRowNumbering] TabOrder = 0 Cells = ( @@ -75,12 +78,12 @@ object Form1: TForm1 Left = 0 Height = 37 Top = 287 - Width = 303 + Width = 470 Align = alBottom AutoSize = True BevelOuter = bvNone ClientHeight = 37 - ClientWidth = 303 + ClientWidth = 470 TabOrder = 1 object Button1: TButton AnchorSideLeft.Control = Panel1 @@ -95,6 +98,239 @@ object Form1: TForm1 TabOrder = 0 end end + object Panel2: TPanel + Left = 6 + Height = 19 + Top = 6 + Width = 458 + Align = alTop + AutoSize = True + BorderSpacing.Around = 6 + BevelOuter = bvNone + ClientHeight = 19 + ClientWidth = 458 + TabOrder = 2 + object pnlCol1Alignment: TPanel + AnchorSideLeft.Control = Panel2 + Left = 42 + Height = 19 + Top = 0 + Width = 87 + AutoSize = True + BorderSpacing.Left = 42 + BevelOuter = bvNone + ClientHeight = 19 + ClientWidth = 87 + TabOrder = 0 + object rbLeft1: TRadioButton + Tag = 11 + AnchorSideLeft.Control = pnlCol1Alignment + AnchorSideTop.Control = pnlCol1Alignment + Left = 0 + Height = 19 + Top = 0 + Width = 24 + Caption = 'L' + Checked = True + OnChange = rbAlignmentChange + TabOrder = 2 + TabStop = True + end + object rbCenter1: TRadioButton + Tag = 13 + AnchorSideLeft.Control = rbLeft1 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol1Alignment + Left = 30 + Height = 19 + Top = 0 + Width = 26 + BorderSpacing.Left = 6 + Caption = 'C' + OnChange = rbAlignmentChange + TabOrder = 0 + end + object rbRight1: TRadioButton + Tag = 12 + AnchorSideLeft.Control = rbCenter1 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol1Alignment + Left = 62 + Height = 19 + Top = 0 + Width = 25 + BorderSpacing.Left = 6 + Caption = 'R' + OnChange = rbAlignmentChange + TabOrder = 1 + end + end + object pnlCol2Alignment: TPanel + AnchorSideLeft.Control = Panel2 + Left = 162 + Height = 19 + Top = 0 + Width = 87 + AutoSize = True + BorderSpacing.Left = 162 + BevelOuter = bvNone + ClientHeight = 19 + ClientWidth = 87 + TabOrder = 1 + object rbLeft2: TRadioButton + Tag = 21 + AnchorSideLeft.Control = pnlCol2Alignment + AnchorSideTop.Control = pnlCol2Alignment + Left = 0 + Height = 19 + Top = 0 + Width = 24 + Caption = 'L' + OnChange = rbAlignmentChange + TabOrder = 0 + end + object rbCenter2: TRadioButton + Tag = 23 + AnchorSideLeft.Control = rbLeft2 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol2Alignment + Left = 30 + Height = 19 + Top = 0 + Width = 26 + BorderSpacing.Left = 6 + Caption = 'C' + OnChange = rbAlignmentChange + TabOrder = 1 + end + object rbRight2: TRadioButton + Tag = 22 + AnchorSideLeft.Control = rbCenter2 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol2Alignment + Left = 62 + Height = 19 + Top = 0 + Width = 25 + BorderSpacing.Left = 6 + Caption = 'R' + Checked = True + OnChange = rbAlignmentChange + TabOrder = 2 + TabStop = True + end + end + object pnlCol3Alignment: TPanel + AnchorSideLeft.Control = Panel2 + AnchorSideTop.Control = Panel2 + Left = 282 + Height = 19 + Top = 0 + Width = 87 + AutoSize = True + BorderSpacing.Left = 282 + BevelOuter = bvNone + ClientHeight = 19 + ClientWidth = 87 + TabOrder = 2 + object rbLeft3: TRadioButton + Tag = 31 + AnchorSideLeft.Control = pnlCol3Alignment + AnchorSideTop.Control = pnlCol3Alignment + Left = 0 + Height = 19 + Top = 0 + Width = 24 + Caption = 'L' + OnChange = rbAlignmentChange + TabOrder = 0 + end + object rbCenter3: TRadioButton + Tag = 33 + AnchorSideLeft.Control = rbLeft3 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol3Alignment + Left = 30 + Height = 19 + Top = 0 + Width = 26 + BorderSpacing.Left = 6 + Caption = 'C' + Checked = True + OnChange = rbAlignmentChange + TabOrder = 1 + TabStop = True + end + object rbRight3: TRadioButton + Tag = 32 + AnchorSideLeft.Control = rbCenter3 + AnchorSideLeft.Side = asrBottom + AnchorSideTop.Control = pnlCol3Alignment + Left = 62 + Height = 19 + Top = 0 + Width = 25 + BorderSpacing.Left = 6 + Caption = 'R' + OnChange = rbAlignmentChange + TabOrder = 2 + end + end + end + object Panel3: TPanel + Left = 438 + Height = 256 + Top = 31 + Width = 26 + Align = alRight + AutoSize = True + BorderSpacing.Right = 6 + BevelOuter = bvNone + ClientHeight = 256 + ClientWidth = 26 + TabOrder = 3 + object rbLayoutTop: TRadioButton + Tag = 1 + AnchorSideLeft.Control = Panel3 + AnchorSideTop.Control = Panel3 + Left = 0 + Height = 19 + Top = 42 + Width = 24 + BorderSpacing.Top = 42 + Caption = 'T' + OnChange = rbLayoutChange + TabOrder = 0 + end + object rbLayoutCenter: TRadioButton + Tag = 2 + AnchorSideLeft.Control = Panel3 + AnchorSideTop.Control = rbLayoutTop + AnchorSideTop.Side = asrBottom + Left = 0 + Height = 19 + Top = 61 + Width = 26 + Caption = 'C' + Checked = True + OnChange = rbLayoutChange + TabOrder = 2 + TabStop = True + end + object rbLayoutBottom: TRadioButton + Tag = 3 + AnchorSideLeft.Control = Panel3 + AnchorSideTop.Control = rbLayoutCenter + AnchorSideTop.Side = asrBottom + Left = 0 + Height = 19 + Top = 80 + Width = 25 + Caption = 'B' + OnChange = rbLayoutChange + TabOrder = 1 + end + end object GridPrinter1: TGridPrinter Grid = StringGrid1 Footer.Font.Height = -11 diff --git a/components/gridprinter/examples/stringgrid_columns/unit1.pas b/components/gridprinter/examples/stringgrid_columns/unit1.pas index 7ea6c94ee..f6355a7f3 100644 --- a/components/gridprinter/examples/stringgrid_columns/unit1.pas +++ b/components/gridprinter/examples/stringgrid_columns/unit1.pas @@ -17,8 +17,27 @@ type GridPrinter1: TGridPrinter; GridPrintPreviewDialog1: TGridPrintPreviewDialog; Panel1: TPanel; + Panel2: TPanel; + Panel3: TPanel; + pnlCol1Alignment: TPanel; + pnlCol2Alignment: TPanel; + pnlCol3Alignment: TPanel; + rbLayoutTop: TRadioButton; + rbLayoutCenter: TRadioButton; + rbLayoutBottom: TRadioButton; + rbCenter2: TRadioButton; + rbCenter3: TRadioButton; + rbLeft1: TRadioButton; + rbCenter1: TRadioButton; + rbLeft2: TRadioButton; + rbLeft3: TRadioButton; + rbRight1: TRadioButton; + rbRight2: TRadioButton; + rbRight3: TRadioButton; StringGrid1: TStringGrid; procedure Button1Click(Sender: TObject); + procedure rbLayoutChange(Sender: TObject); + procedure rbAlignmentChange(Sender: TObject); private public @@ -39,5 +58,38 @@ begin GridPrintPreviewDialog1.Execute; end; +procedure TForm1.rbLayoutChange(Sender: TObject); +var + layoutIndex: Integer; + i: Integer; +begin + if not (Sender is TRadioButton) then + exit; + if not TRadioButton(Sender).Checked then + exit; + layoutIndex := TRadioButton(Sender).Tag - 1; + for i := 0 to StringGrid1.Columns.Count-1 do + begin + StringGrid1.Columns[i].Layout := TTextLayout(layoutIndex); + StringGrid1.Columns[i].Title.Layout := TTextLayout(layoutIndex); + end; +end; + +procedure TForm1.rbAlignmentChange(Sender: TObject); +var + colIndex, alignmentIndex: Integer; + col: TGridColumn; +begin + if not (Sender is TRadioButton) then + exit; + if not TRadioButton(Sender).Checked then + exit; + colIndex := TRadioButton(Sender).Tag div 10 - 1; + alignmentIndex := TRadioButton(Sender).Tag mod 10 - 1; + col := StringGrid1.Columns[colIndex]; + col.Alignment := TAlignment(alignmentIndex); + col.Title.Alignment := TAlignment(alignmentIndex); +end; + end. diff --git a/components/gridprinter/source/gridprn.pas b/components/gridprinter/source/gridprn.pas index b87bdf47d..48951df80 100644 --- a/components/gridprinter/source/gridprn.pas +++ b/components/gridprinter/source/gridprn.pas @@ -1409,8 +1409,16 @@ begin cSize.cx := ScaleX(cSize.cx); cSize.cy := ScaleY(cSize.cy); // Position the checkbox within the given rectangle, ARect. - R.Left := (ARect.Left + ARect.Right - cSize.cx) div 2; - R.Top := (ARect.Top + ARect.Bottom - cSize.cy) div 2; + case ACanvas.TextStyle.Alignment of + taLeftJustify: R.Left := ARect.Left + FPadding; + taCenter: R.Left := (ARect.Left + ARect.Right - cSize.cx) div 2; + taRightJustify: R.Left := ARect.Right - cSize.cx - FPadding; + end; + case ACanvas.TextStyle.Layout of + tlTop: R.Top := ARect.Top + FPadding; + tlCenter: R.Top := (ARect.Top + ARect.Bottom - cSize.cy) div 2; + tlBottom: R.Top := ARect.Bottom - cSize.cy - FPadding; + end; R.BottomRight := Point(R.Left + cSize.cx, R.Top + cSize.cy); // Prepare pen and brush ACanvas.Pen.Width := ScaleX(1);