File: comain.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (290 lines) | stat: -rw-r--r-- 8,600 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
unit comain;

{$mode objfpc}{$H+}

interface

uses
  Classes, Controls, Dialogs, FileUtil, Forms, Graphics, Grids, ComCtrls,
  SysUtils, Types;

type

  TStringGrid = class(Grids.TStringGrid)
  protected
    procedure ColWidthsChanged; override;
    procedure DoEditorHide; override;
    procedure DoEditorShow; override;
    procedure DrawRow(ARow: Integer); override;
    function OverflowCellRect(ACol, ARow: integer; AState: TGridDrawState;
      out ANumColsNeededAtRight: Integer): TRect; overload;
    function OverflowCellRect(ACol, ARow: Integer): TRect; overload;
  end;

  { TMainForm }

  TMainForm = class(TForm)
    StatusBar: TStatusBar;
    StringGrid: TStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure StringGridClick(Sender: TObject);
    procedure StringGridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
      aState: TGridDrawState);
  private
  end;

var
  MainForm: TMainForm;

implementation

{$R *.lfm}

uses
  LCLIntf;

{ Helper routines copied from grids.pas }

function HorizontalIntersect(const aRect,bRect: TRect): boolean;
begin
  result := (aRect.Left < bRect.Right) and (aRect.Right > bRect.Left);
end;

function VerticalIntersect(const aRect,bRect: TRect): boolean;
begin
  result := (aRect.Top < bRect.Bottom) and (aRect.Bottom > bRect.Top);
end;


{------------------------------------------------------------------------------}
{                        Extended StringGrid                                   }
{------------------------------------------------------------------------------}

{ Makes sure that overflowing cells are painted correctly if they a column
  width changes. }
procedure TStringGrid.ColWidthsChanged;
begin
  inherited;
  InvalidateGrid;
end;

{ After editing a repaint of the current row is needed because the edited cell
  may overflow differently }
procedure TStringGrid.DoEditorHide;
begin
  inherited;
  InvalidateRow(Row);
end;

{ Make sure that the cell editor has the same size as the OverflowCellRect }
procedure TStringGrid.DoEditorShow;
begin
  inherited;
  with OverflowCellRect(Col, Row) do begin
    Editor.Width := Right - Left - 4;
  end;
  InvalidateRow(Row);
end;

{ Draws an entire row to correctly process overflowing cells.
  Most of the code is copied from TCustomGrid.DrawRow }
procedure TStringGrid.DrawRow(ARow: Integer);
var
  gds: TGridDrawState;
  aCol: Integer;
  ncols: Integer;
  Rs: Boolean;
  R: TRect;
  tmpR: TRect;
  ClipArea: Trect;

  function IsPushCellActive: boolean;
  begin
    with GCache do
      result := (PushedCell.X<>-1) and (PushedCell.Y<>-1);
  end;

  procedure DoDrawCell;
  begin
    with GCache do begin
      if (aCol=HotCell.x) and (aRow=HotCell.y) and not IsPushCellActive() then begin
        Include(gds, gdHot);
        HotCellPainted := True;
      end;
      if ClickCellPushed and (aCol=PushedCell.x) and (aRow=PushedCell.y) then begin
        Include(gds, gdPushed);
      end;
    end;

    Canvas.SaveHandleState;
    try
      InterSectClipRect(Canvas.Handle, R.Left, R.Top, R.Right, R.Bottom);
      DrawCell(aCol, aRow, R, gds);
    finally
      Canvas.RestoreHandleState;
    end;
  end;

begin
  // Upper and Lower bounds for this row
  ColRowToOffSet(False, True, aRow, R.Top, R.Bottom);

  // is this row within the ClipRect?
  ClipArea := Canvas.ClipRect;
  if (R.Top>=R.Bottom) or not VerticalIntersect(R, ClipArea) then begin
    {$IFDEF DbgVisualChange}
    DebugLn('Drawrow: Skipped row: ', IntToStr(aRow));
    {$ENDIF}
    exit;
  end;

  // Draw columns in this row
  with GCache.VisibleGrid do begin
    aCol := FixedCols;
    while (aCol < ColCount) do begin
      gds := GetGridDrawState(ACol, ARow);
      with OverflowCellRect(ACol, ARow, gds, ncols) do begin
        R.Left := Left;
        R.Right := Right;
      end;
      if (R.Left < R.Right) and HorizontalIntersect(R, ClipArea) then
        DoDrawCell;
      inc(aCol, ncols);
    end;

    Rs := (goRowSelect in Options);
    // Draw the focus Rect
    if FocusRectVisible and (ARow = Row) and
       ((Rs and (ARow >= Top) and (ARow <= Bottom)) or IsCellVisible(Col, ARow))
    then begin
      if not EditorMode then begin
        if Rs then
          CalcFocusRect(R, false) // will be adjusted when calling DrawFocusRect
        else
          ColRowToOffset(True, True, Col, R.Left, R.Right);
        // is this column within the ClipRect?
        if HorizontalIntersect(R, ClipArea) then
          DrawFocusRect(Col, Row, R);
      end;
    end;
  end;

  // Draw fixed columns
  For aCol:=0 to FixedCols-1 do begin
    gds := [gdFixed];
    ColRowToOffset(True, True, aCol, R.Left, R.Right);
    // Is this column within the ClipRect?
    if (R.Left<R.Right) and HorizontalIntersect(R, ClipArea) then
      DoDrawCell;
  end;
end;

{ Calculates the rectangle coordinates of the cell block covered by the
  overflowing cell at the specified column and row }
function TStringGrid.OverflowCellRect(ACol, ARow: Integer): TRect;
var
  n: Integer;
begin
  Result := OverflowCellRect(ACol, ARow, [], n);
end;

{ Calculates the pixel coordinates of the cell block covered by the
  overflowing cell at the specified column and row, and retunrs in
  ANumColsNeededAtRight the number of columns covered by the cell at
  ACol/ARow when going to the right }
function TStringGrid.OverflowCellRect(ACol, ARow: integer; AState: TGridDrawState;
  out ANumColsNeededAtRight: Integer): TRect;
var
  lIndex: integer;
  ts: TTextStyle;
  lCol, rCol: Integer;
  len: Integer;
begin
  Result := CellRect(ACol, ARow);
  ANumColsNeededAtRight := 1;
  len := Canvas.TextWidth(Cells[ACol, ARow]) + 2*constCellPadding;
  if len > ColWidths[ACol] then begin
    PrepareCanvas(ACol, ARow, AState);
    ts := Canvas.TextStyle;
    case ts.Alignment of
      taLeftJustify:
        for lIndex := ACol + 1 to ColCount - 1 do
          if (Cells[lIndex, ARow] = EmptyStr) then
          begin
            Result.Right := CellRect(lIndex, ARow).Right;
            ANumColsNeededAtRight := lIndex - ACol + 1;
            if Result.Right - Result.Left > len then Break;
          end else
            Break;
      taRightJustify:
        for lIndex := ACol - 1 downto FixedCols do
          if (Cells[lIndex, ARow] = EmptyStr) then
          begin
            Result.Left := CellRect(lIndex, ARow).Left;
            if Result.Right - Result.Left > len then Break;
          end else
            Break;
      taCenter:
        begin
          lIndex := 1;
          while true do begin
            lCol := ACol - lIndex;
            rCol := ACol + lIndex;
            if (lCol < FixedCols) or (rCol >= ColCount) then
              break;
            if (Cells[rCol, ARow] = EmptyStr) and (Cells[lCol, ARow] = EmptyStr)
            then begin
              Result.Left := CellRect(lCol, ARow).Left;
              Result.Right := CellRect(rCol, ARow).Right;
              ANumColsNeededAtRight := rCol - ACol + 1;
              if Result.Right - Result.Left > len then Break;
            end else
              Break;
            inc(lIndex);
          end;
        end;
    end;
  end;
end;


{------------------------------------------------------------------------------}
{                                  TMainForm                                   }
{------------------------------------------------------------------------------}

{ Sets up basic grid content }
procedure TMainForm.FormCreate(Sender: TObject);
begin
  StringGrid.Cells[1, 2] := 'ABC';
  StringGrid.Cells[1, 1] := 'This is a long string for my string grid';
  StringGrid.Cells[StringGrid.ColCount-1, 1] := '124';
  StringGrid.Cells[StringGrid.ColCount-1, StringGrid.RowCount-1] := 'Another long text, right-aligned now';
  StringGrid.Cells[4, 6] := 'This is a long string which is centered.';
end;

{ Displays information on the clicked cell in the status bar }
procedure TMainForm.StringGridClick(Sender: TObject);
begin
  Statusbar.SimpleText := Format('Column %d, row %d, text: "%s"',
    [StringGrid.Col, StringGrid.Row, StringGrid.Cells[StringGrid.Col, StringGrid.Row]]);
end;

{ Prepares centered text in column 4, and right-aligned text in the last column}
procedure TMainForm.StringGridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
var
  ts: TTextStyle;
begin
  if (aCol = StringGrid.ColCount-1) then begin
    ts := StringGrid.Canvas.TextStyle;
    ts.Alignment := taRightJustify;
    StringGrid.Canvas.TextStyle := ts;
  end else
  if (aCol = 4) then begin
    ts := StringGrid.Canvas.TextStyle;
    ts.Alignment := taCenter;
    StringGrid.Canvas.TextStyle := ts;
  end;
end;

end.