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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
program gridtest;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes,
SysUtils,
fpg_base,
fpg_main,
fpg_form,
fpg_basegrid,
fpg_grid,
fpg_button,
fpg_checkbox,
fpg_tab,
fpg_edit;
type
{ TMainForm }
TMainForm = class(TfpgForm)
private
{@VFD_HEAD_BEGIN: MainForm}
btnQuit: TfpgButton;
stringgrid: TfpgStringGrid;
chkShowHeader: TfpgCheckBox;
chkShowGrid: TfpgCheckBox;
chkRowSelect: TfpgCheckBox;
chkDisabled: TfpgCheckBox;
chkHideFocus: TfpgCheckBox;
edtTopRow: TfpgEditInteger;
btnTopRow: TfpgButton;
btnAddFive: TfpgButton;
btnAddOne: TfpgButton;
btnFiveOnly: TfpgButton;
btnDelRow: TfpgButton;
chkSmoothScroll: TfpgCheckBox;
chkAlterColor: TfpgCheckBox;
{@VFD_HEAD_END: MainForm}
procedure StringGridDoubleClicked(Sender: TObject; AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
procedure btnAddFiveClicked(Sender: TObject);
procedure btnAddOneClicked(Sender: TObject);
procedure btnFiveOnlyClicked(Sender: TObject);
procedure btnDelRowClicked(Sender: TObject);
procedure chkDisabledChange(Sender: TObject);
procedure chkRowSelectChange(Sender: TObject);
procedure chkShowHeaderChange(Sender: TObject);
procedure chkShowGridChange(Sender: TObject);
procedure chkHideFocusChange(Sender: TObject);
procedure chkSmoothScrollChange(Sender: TObject);
procedure chkAlterColorChange(Sender: TObject);
procedure btnQuitClick(Sender: TObject);
procedure stringgridDrawCell(Sender: TObject; const ARow, ACol: Integer;
const ARect: TfpgRect; const AFlags: TfpgGridDrawState; var ADefaultDrawing: boolean);
procedure btnTopRowClicked(Sender: TObject);
public
procedure AfterCreate; override;
end;
{@VFD_NEWFORM_DECL}
{ TMainForm }
procedure TMainForm.StringGridDoubleClicked(Sender: TObject;
AButton: TMouseButton; AShift: TShiftState; const AMousePos: TPoint);
var
lCol, lRow: integer;
begin
StringGrid.MouseToCell(AMousePos.X, AMousePos.Y, lCol, lRow);
StringGrid.Cells[lCol, lRow] := Format('(c%d,r%d)', [lCol, lRow]);
end;
procedure TMainForm.btnAddFiveClicked(Sender: TObject);
begin
StringGrid.RowCount := StringGrid.RowCount + 5;
end;
procedure TMainForm.btnAddOneClicked(Sender: TObject);
begin
StringGrid.RowCount := StringGrid.RowCount + 1;
end;
procedure TMainForm.btnFiveOnlyClicked(Sender: TObject);
begin
StringGrid.RowCount := 5;
end;
procedure TMainForm.btnDelRowClicked(Sender: TObject);
begin
stringgrid.DeleteRow(stringgrid.FocusRow);
end;
procedure TMainForm.chkDisabledChange(Sender: TObject);
begin
stringgrid.Enabled := not chkDisabled.Checked;
end;
procedure TMainForm.chkRowSelectChange(Sender: TObject);
begin
stringgrid.RowSelect := chkRowSelect.Checked;
end;
procedure TMainForm.chkShowHeaderChange(Sender: TObject);
begin
stringgrid.ShowHeader := chkShowHeader.Checked;
end;
procedure TMainForm.chkShowGridChange(Sender: TObject);
begin
stringgrid.ShowGrid := chkShowGrid.Checked;
end;
procedure TMainForm.chkHideFocusChange(Sender: TObject);
begin
if chkHideFocus.Checked then
stringgrid.Options := stringgrid.Options + [go_HideFocusRect]
else
stringgrid.Options := stringgrid.Options - [go_HideFocusRect];
stringgrid.Invalidate;
end;
procedure TMainForm.chkSmoothScrollChange(Sender: TObject);
begin
if chkSmoothScroll.Checked then
stringgrid.Options := stringgrid.Options + [go_SmoothScroll]
else
stringgrid.Options := stringgrid.Options - [go_SmoothScroll];
stringgrid.Update;
end;
procedure TMainForm.chkAlterColorChange(Sender: TObject);
begin
if chkAlterColor.Checked then
stringgrid.Options := stringgrid.Options + [go_AlternativeColor]
else
stringgrid.Options := stringgrid.Options - [go_AlternativeColor];
stringgrid.Invalidate;
end;
procedure TMainForm.btnQuitClick(Sender: TObject);
begin
Close;
end;
procedure TMainForm.stringgridDrawCell(Sender: TObject; const ARow,
ACol: Integer; const ARect: TfpgRect; const AFlags: TfpgGridDrawState;
var ADefaultDrawing: boolean);
begin
if (ACol = 1) and (ARow = 3) then
begin
ADefaultDrawing := False;
StringGrid.Canvas.SetColor(clGreen);
fpgStyle.DrawDirectionArrow(StringGrid.Canvas, ARect.Left, ARect.Top,
ARect.Height, ARect.Height, adRight);
StringGrid.Canvas.SetTextColor(clTeal);
StringGrid.Canvas.DrawString(ARect.Height + ARect.Left + 2, ARect.Top, StringGrid.Cells[ACol, ARow]);
end;
// two rows with different background color
if (ARow = 7) or (ARow = 8) then
begin
if ((gdSelected in AFlags) and (gdFocused in AFlags)) or
(gdSelected in AFlags) then
Exit; // we want select cel to be painted as normal
// If we got here, we must do some painting. The background first.
StringGrid.Canvas.Color := clOrange;
StringGrid.Canvas.FillRectangle(ARect);
// NOTE: We want the grid to take care of the drawing of the text, which
// handles text layout and alignment, so we MUST NOT set the
// ADefaultDrawing to False. If we do, we need to handle text painting
// ourselves.
end;
end;
procedure TMainForm.btnTopRowClicked(Sender: TObject);
begin
if edtTopRow.Value < 0 then
Exit;
stringgrid.TopRow := edtTopRow.Value;
end;
procedure TMainForm.AfterCreate;
var
r: Longword;
begin
{@VFD_BODY_BEGIN: MainForm}
Name := 'MainForm';
SetPosition(351, 214, 515, 350);
WindowTitle := 'Grid control test';
Hint := '';
WindowPosition := wpScreenCenter;
btnQuit := TfpgButton.Create(self);
with btnQuit do
begin
Name := 'btnQuit';
SetPosition(425, 320, 80, 25);
Anchors := [anRight,anBottom];
Text := 'Quit';
FontDesc := '#Label1';
Hint := '';
ImageName := 'stdimg.Quit';
TabOrder := 1;
OnClick := @btnQuitClick;
end;
stringgrid := TfpgStringGrid.Create(self);
with stringgrid do
begin
Name := 'stringgrid';
SetPosition(10, 10, 375, 250);
Anchors := [anLeft,anRight,anTop,anBottom];
AddColumn('Column 1', 100, taLeftJustify);
AddColumn('Col 2', 50, taCenter);
AddColumn('Numbers', 150, taRightJustify);
FontDesc := '#Grid';
HeaderFontDesc := '#GridHeader';
Hint := '';
RowCount := 17;
RowSelect := False;
TabOrder := 1;
AddColumn('Column 0', 65);
// Alignment test
Cells[1, 2] := 'left';
Cells[2, 2] := 'center';
Cells[3, 2] := 'right';
// override default colors
BackgroundColor:= clKhaki;
ColumnBackgroundColor[2] := clLightGray;
TextColor:= clBlue;
ColumnTextColor[1] := clRed;
// add some text
Cells[0, 0] := '[c0, r0]';
Cells[1, 1] := '[c1, r1]';
Cells[1, 3] := 'Custom';
Cells[2, 3] := 'Hello';
Cells[3, 1] := '[c3, r1]';
// Add custom painting
OnDrawCell := @StringGridDrawCell;
OnDoubleClick := @StringGridDoubleClicked;
end;
chkShowHeader := TfpgCheckBox.Create(self);
with chkShowHeader do
begin
Name := 'chkShowHeader';
SetPosition(394, 12, 116, 24);
Anchors := [anRight,anTop];
Checked := True;
FontDesc := '#Label1';
Hint := '';
TabOrder := 2;
Text := 'Show Header';
OnChange := @chkShowHeaderChange;
end;
chkShowGrid := TfpgCheckBox.Create(self);
with chkShowGrid do
begin
Name := 'chkShowGrid';
SetPosition(394, 36, 120, 24);
Anchors := [anRight,anTop];
Checked := True;
FontDesc := '#Label1';
Hint := '';
TabOrder := 3;
Text := 'Show Grid';
OnChange := @chkShowGridChange;
end;
chkRowSelect := TfpgCheckBox.Create(self);
with chkRowSelect do
begin
Name := 'chkRowSelect';
SetPosition(394, 60, 116, 24);
Anchors := [anRight,anTop];
FontDesc := '#Label1';
Hint := '';
TabOrder := 4;
Text := 'Row Select';
OnChange := @chkRowSelectChange;
end;
chkDisabled := TfpgCheckBox.Create(self);
with chkDisabled do
begin
Name := 'chkDisabled';
SetPosition(394, 84, 116, 24);
Anchors := [anRight,anTop];
FontDesc := '#Label1';
Hint := '';
TabOrder := 5;
Text := 'Disabled';
OnChange := @chkDisabledChange;
end;
chkHideFocus := TfpgCheckBox.Create(self);
with chkHideFocus do
begin
Name := 'chkHideFocus';
SetPosition(394, 108, 120, 24);
Anchors := [anRight,anTop];
FontDesc := '#Label1';
Hint := '';
TabOrder := 6;
Text := 'Hide Focus';
OnChange := @chkHideFocusChange;
end;
edtTopRow := TfpgEditInteger.Create(self);
with edtTopRow do
begin
Name := 'edtTopRow';
SetPosition(12, 280, 56, 24);
Anchors := [anLeft,anBottom];
Hint := '';
TabOrder := 8;
FontDesc := '#Edit1';
Value := 0;
end;
btnTopRow := TfpgButton.Create(self);
with btnTopRow do
begin
Name := 'btnTopRow';
SetPosition(72, 280, 91, 23);
Anchors := [anLeft,anBottom];
Text := 'Set TopRow';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 7;
OnClick := @btnTopRowClicked;
end;
btnAddFive := TfpgButton.Create(self);
with btnAddFive do
begin
Name := 'btnAddFive';
SetPosition(168, 280, 80, 23);
Anchors := [anLeft,anBottom];
Text := 'Add 5 lines';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 8;
OnClick := @btnAddFiveClicked;
end;
btnAddOne := TfpgButton.Create(self);
with btnAddOne do
begin
Name := 'btnAddOne';
SetPosition(252, 280, 80, 23);
Anchors := [anLeft,anBottom];
Text := 'Add 1 line';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 9;
OnClick := @btnAddOneClicked;
end;
btnFiveOnly := TfpgButton.Create(self);
with btnFiveOnly do
begin
Name := 'btnFiveOnly';
SetPosition(336, 280, 80, 23);
Anchors := [anLeft,anBottom];
Text := '5 lines only';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 10;
OnClick := @btnFiveOnlyClicked;
end;
btnDelRow := TfpgButton.Create(self);
with btnDelRow do
begin
Name := 'btnDelRow';
SetPosition(168, 308, 80, 23);
Anchors := [anLeft,anBottom];
Text := 'Delete Row';
FontDesc := '#Label1';
Hint := '';
ImageName := '';
TabOrder := 12;
OnClick := @btnDelRowClicked;
end;
chkSmoothScroll := TfpgCheckBox.Create(self);
with chkSmoothScroll do
begin
Name := 'chkSmoothScroll';
SetPosition(394, 132, 120, 24);
Anchors := [anRight,anTop];
FontDesc := '#Label1';
Hint := '';
TabOrder := 14;
Text := 'Smooth Scroll';
OnChange := @chkSmoothScrollChange;
end;
chkAlterColor := TfpgCheckBox.Create(self);
with chkAlterColor do
begin
Name := 'chkAlterColor';
SetPosition(394, 156, 120, 24);
FontDesc := '#Label1';
Hint := '';
TabOrder := 15;
Text := 'Alternate Color';
OnChange := @chkAlterColorChange;
end;
{@VFD_BODY_END: MainForm}
for r := 0 to stringgrid.RowCount-1 do
stringgrid.Cells[3, r] := IntToStr(r);
end;
{@VFD_NEWFORM_IMPL}
procedure MainProc;
var
frm: TMainForm;
begin
fpgApplication.Initialize;
frm := TMainForm.Create(nil);
frm.Show;
fpgApplication.Run;
frm.Free;
end;
begin
MainProc;
end.
|