summaryrefslogtreecommitdiff
path: root/src/gui/gui_customgrid.pas
blob: 47cd2e9fe1c7bda7f4f1e3212802a3b944d38a1f (plain)
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
unit gui_customgrid; 

{$mode objfpc}{$H+}

{
  TODO:
    * Column text alignment needs to be implemented. Currently always Centre.
    * AlternateColor for rows need to be implemented.
}

{.$Define DEBUG}

interface

uses
  Classes,
  SysUtils,
  gfxbase,
  fpgfx,
  gui_basegrid;
  
type

  // data object for grid columns
  TfpgGridColumn = class(TObject)
  private
    FAlignment: TAlignment;
    FTitle: string;
    FWidth: integer;
  public
    constructor Create; virtual;
    property    Width: integer read FWidth write FWidth;
    property    Title: string read FTitle write FTitle;
    property    Alignment: TAlignment read FAlignment write FAlignment;
  end;
  
  
  TfpgCustomGrid = class(TfpgBaseGrid)
  protected
    FRowCount: integer;
    FColumns: TList;
    function    GetColumns(AIndex: integer): TfpgGridColumn; virtual;
    procedure   DoDeleteColumn(ACol: integer); virtual;
    procedure   DoSetRowCount(AValue: integer); virtual;
    function    DoCreateColumnClass: TfpgGridColumn; virtual;
    function    GetColumnCount: integer; override;
    procedure   SetColumnCount(const AValue: integer); virtual;
    function    GetRowCount: integer; override;
    procedure   SetRowCount(const AValue: integer); virtual;
    function    GetColumnWidth(ACol: integer): integer; override;
    procedure   SetColumnWidth(ACol: integer; const AValue: integer); override;
    function    GetHeaderText(ACol: integer): string; override;
    property    RowCount: integer read GetRowCount write SetRowCount;
    property    ColumnCount: integer read GetColumnCount write SetColumnCount;
    property    Columns[AIndex: integer]: TfpgGridColumn read GetColumns;
//    property AlternateColor: TColor read FAlternateColor write SetAlternateColor stored IsAltColorStored;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    function    AddColumn(ATitle: string; AWidth: integer): TfpgGridColumn; virtual;
  end;
  
  
implementation

{ TfpgGridColumn }

constructor TfpgGridColumn.Create;
begin
  Width     := 64;
  Title     := '';
  Alignment := taCenter;
end;

{ TfpgCustomGrid }

function TfpgCustomGrid.GetRowCount: integer;
begin
  Result := FRowCount;
end;

function TfpgCustomGrid.GetColumns(AIndex: integer): TfpgGridColumn;
begin
  if (AIndex < 0) or (AIndex > FColumns.Count-1) then
    Result := nil
  else
    Result := TfpgGridColumn(FColumns[AIndex]);
end;

procedure TfpgCustomGrid.DoDeleteColumn(ACol: integer);
begin
  TfpgGridColumn(FColumns.Items[ACol-1]).Free;
  FColumns.Delete(ACol-1);
end;

procedure TfpgCustomGrid.DoSetRowCount(AValue: integer);
begin
  // do nothing
end;

function TfpgCustomGrid.DoCreateColumnClass: TfpgGridColumn;
begin
  Result := TfpgGridColumn.Create;
end;

function TfpgCustomGrid.GetColumnCount: integer;
begin
  Result := FColumns.Count;
end;

procedure TfpgCustomGrid.SetColumnCount(const AValue: integer);
var
  n: integer;
begin
  n := FColumns.Count;
  if (n = AValue) or (AValue < 0) then
    Exit; //==>

  if n < AValue then
  begin
    // adding columns
    while n < AValue do
    begin
      AddColumn('', DefaultColWidth);
      inc(n);
    end;
  end
  else
  begin
    while n > AValue do
    begin
      DoDeleteColumn(n);
      dec(n);
    end;
  end;
  UpdateScrollBars;
  RePaint;
end;

procedure TfpgCustomGrid.SetRowCount(const AValue: integer);
begin
  if FRowCount = AValue then
    Exit; //==>
  FRowCount := AValue;
  if FocusRow > FRowCount then
  begin
    FocusRow := FRowCount;
  end;
  DoSetRowCount(AValue);  // could be implemented by descendants
  RePaint;
end;

function TfpgCustomGrid.GetColumnWidth(ACol: integer): integer;
begin
  if (ACol > 0) and (ACol <= ColumnCount) then
    Result := TfpgGridColumn(FColumns[ACol-1]).Width
  else
    result := DefaultColWidth;
end;

procedure TfpgCustomGrid.SetColumnWidth(ACol: integer; const AValue: integer);
var
  lCol: TfpgGridColumn;
begin
  lCol := TfpgGridColumn(FColumns[ACol-1]);
  
  if lCol.Width <> AValue then
  begin
    if AValue < 1 then
      lCol.Width := 1
    else
      lCol.Width := AValue;
    UpdateScrollBars;
    Repaint;
  end;
end;

function TfpgCustomGrid.GetHeaderText(ACol: integer): string;
begin
  Result := TfpgGridColumn(FColumns[ACol-1]).Title;
end;

constructor TfpgCustomGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FColumns := TList.Create;
  ColumnCount := 5;
  RowCount    := 5;
end;

destructor TfpgCustomGrid.Destroy;
begin
  while FColumns.Count > 0 do
  begin
    TfpgGridColumn(FColumns.Items[0]).Free;
    FColumns.Delete(0);
  end;

  FColumns.Free;
  inherited Destroy;
end;

function TfpgCustomGrid.AddColumn(ATitle: string; AWidth: integer): TfpgGridColumn;
begin
  Result := DoCreateColumnClass;
  Result.Title := ATitle;
  Result.Width := AWidth;
  FColumns.Add(Result);
  
  UpdateScrollBars;
  RePaint;
end;

end.