File: main.pas

package info (click to toggle)
lazarus 2.2.6%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 219,980 kB
  • sloc: pascal: 1,944,919; xml: 357,634; makefile: 270,608; cpp: 57,115; sh: 3,249; java: 609; perl: 297; sql: 222; ansic: 137
file content (130 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (8)
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
unit main;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Graphics, Dialogs, Grids, ComCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    grid: TStringGrid;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    ToolBar1: TToolBar;
    SaveButton: TToolButton;
    LoadButton: TToolButton;
    procedure gridBeforeSelection(Sender: TObject; aCol, aRow: Integer);
    procedure gridDrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect;
      aState: TGridDrawState);
    procedure gridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
      aState: TGridDrawState);
    procedure LoadButtonClick(Sender: TObject);
    procedure SaveButtonClick(Sender: TObject);
  private
    function IndexToAlphaIndex(AIndex: Integer): string;
  public

  end; 

var
  Form1: TForm1; 

implementation

{$R main.lfm}

{ TForm1 }

procedure TForm1.gridBeforeSelection(Sender: TObject; aCol, aRow: Integer);
begin

  if Grid.Col<>aCol then
  begin
    grid.InvalidateCell(grid.Col, 0);
    grid.InvalidateCell(aCol, 0);
  end;

  if Grid.Row<>aRow then
  begin
    grid.InvalidateCell(0, grid.Row);
    grid.InvalidateCell(0, aRow);
  end;

end;

procedure TForm1.gridDrawCell(Sender: TObject; aCol, aRow: Integer;
  aRect: TRect; aState: TGridDrawState);

  procedure HorizontalCenter;
  var
    aTextStyle : TTextStyle;
  begin
    aTextStyle := grid.Canvas.TextStyle;
    aTextStyle.Alignment:=taCenter;
    grid.Canvas.TextStyle:=aTextStyle;
  end;

begin

  if gdFixed in aState then
  begin
    if (aCol=0) and (aRow>=Grid.FixedRows) then
    begin
      HorizontalCenter;
      grid.Canvas.TextRect(aRect, aRect.Left, aRect.Top, IntToStr(aRow));
      exit;
    end else
    if (aRow=0) and (aCol>=Grid.FixedCols) then
    begin
      HorizontalCenter;
      grid.Canvas.TextRect(aRect, aRect.Left, aRect.Top,
                                 IndexToAlphaIndex(aCol-Grid.FixedCols));
      exit;
    end;
  end;

  grid.DefaultDrawCell(aCol,aRow,aRect,aState);
end;

procedure TForm1.gridPrepareCanvas(sender: TObject; aCol, aRow: Integer;
  aState: TGridDrawState);
begin
  if gdFixed in aState then
  begin
    if (aCol=grid.Col) or (aRow=grid.Row) then
      grid.Canvas.Brush.Color := clInactiveCaption;
  end;
end;

procedure TForm1.LoadButtonClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    grid.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.SaveButtonClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
    grid.SaveToFile(SaveDialog1.FileName);
end;

function TForm1.IndexToAlphaIndex(AIndex: Integer): string;
var
  i: Integer;
begin
  Result := chr((AIndex mod 26) + ord('A'));
  i := (AIndex div 26)-1;
  if i>25 then
    result := '['+IntToStr(AIndex)+']'
  else
  if i>=0 then
    result := chr(i + ord('A')) + Result;
end;

end.