summaryrefslogtreecommitdiff
path: root/src/corelib/fpg_imagelist.pas
blob: 8804741d016f18c47064975bf0541fee2431e2ac (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
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
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2010 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:

      A basic image list component for use by the GUI components like the
      treeview etc.
}

unit fpg_imagelist;

{$mode objfpc}{$H+}

{.$define DEBUG}

interface

uses
  Classes,
  SysUtils,
  fpg_base,
  fpg_main;
  
type

  EItemExists = class(Exception);

  TfpgImageList = class;  // forward declaration
  

  TfpgImageItem = class(TObject)
  private
    FImage: TfpgImage;
    FIndex: integer;
    FImageList: TfpgImageList;
    procedure   SetImageList(AImageList: TfpgImageList);
    procedure   SetIndex(AIndex: integer);
    procedure   SetImage(AImage: TfpgImage);
  public
    constructor Create; overload;
    constructor Create(AImageList: TfpgImageList; AIndex: integer; AImage: TfpgImage); overload;
    constructor Create(AFileName: TfpgString; AIndex: integer); overload;
    destructor  Destroy; override;
    property    Index: integer read FIndex write SetIndex;
    property    Image: TfpgImage read FImage write SetImage;
    property    ImageList: TfpgImageList read FImageList write SetImageList;
    procedure   LoadFromFile(AFileName: TfpgString);
  end;


  TfpgImageList = class(TObject)
  private
    FList: TList;
    function    GetFListIndex(AIndex: Integer): Integer;
    function    GetItem(AIndex: integer): TfpgImageItem;
    procedure   SetItem(AIndex: integer; AItem: TfpgImageItem);
    function    GetCount: integer;
  public
    constructor Create;
    destructor  Destroy; override;
    procedure   AddItemFromFile(AFileName: TfpgString; AIndex: integer = -1);
    procedure   AddImage(AImage: TfpgImage; AIndex: integer = -1);
    procedure   RemoveIndex(AIndex: integer);
    function    GetMaxItem: integer;
    procedure   Clear;
    property    Items[AIndex: integer]: TfpgImageItem read GetItem write SetItem; default;
    property    Count: integer read GetCount;
  end;

  

implementation

uses
  fpg_imgfmt_bmp,
  fpg_utils;

{ TfpgImageList }

function TfpgImageList.GetFListIndex(AIndex: Integer): Integer;
var
  i: integer;
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageList.GetFListIndex');
  {$ENDIF}
  result := -1;
  for i := 0 to FList.Count - 1 do
    if TfpgImageItem(FList[i]).Index = AIndex then
    begin
      result := i;
      Break;  //==>
    end;
end;

function TfpgImageList.GetItem(AIndex: integer): TfpgImageItem;
var
  AFindIndex: integer;
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageList.GetItem');
  {$ENDIF}
  result := nil;
  AFindIndex := GetFListIndex(AIndex);
  if AFindIndex > -1 then
    result := TfpgImageItem(FList[AFindIndex]);
end;

procedure TfpgImageList.SetItem(AIndex: integer; AItem: TfpgImageItem);
begin
  if AItem = nil then
    Exit; //==>
    
  if GetItem(AIndex) = AItem then
    Exit; //==>
    
  RemoveIndex(AIndex);      // delete existing Item
  AItem.Index := AIndex;
  FList.Add(AItem);
end;

function TfpgImageList.GetCount: integer;
begin
  Result := FList.Count;
end;

constructor TfpgImageList.Create;
begin
  FList := TList.Create;
end;

destructor TfpgImageList.Destroy;
var
  i: integer;
begin
  for i := FList.Count-1 downto 0 do
    TfpgImageItem(FList[i]).Destroy;  // frees images
  FList.Destroy;
  inherited Destroy
end;

procedure TfpgImageList.AddItemFromFile(AFileName: TfpgString; AIndex: integer);
var
  AImageItem: TfpgImageItem;
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageList.AddItemFromFile');
  {$ENDIF}
  
  if not fpgFileExists(AFileName) then
    Exit; //==>
  
  AImageItem := TfpgImageItem.Create;
  AImageItem.LoadFromFile(AFileName);
  if AIndex > -1 then
    Items[AIndex] := AImageItem
  else
  begin
    FList.Add(AImageItem);
    AImageItem.Index := GetMaxItem+1;
  end;
end;

procedure TfpgImageList.AddImage(AImage: TfpgImage; AIndex: integer);
var
  AImageItem: TfpgImageItem;
begin
  AImageItem := TfpgImageItem.Create;
  AImageItem.Image := AImage;
  if AIndex > -1 then
    Items[AIndex] := AImageItem
  else
  begin
    FList.Add(AImageItem);
    AImageItem.Index := GetMaxItem+1;
  end;
end;

procedure TfpgImageList.RemoveIndex(AIndex: integer);
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageList.RemoveIndex');
  {$ENDIF}
  AIndex := GetFListIndex(AIndex);
  if AIndex <> -1 then
  begin
    TfpgImageItem(FList[AIndex]).Destroy;
    FList.Delete(AIndex);
  end;
end;

function TfpgImageList.GetMaxItem: integer;
var
  i: integer;
begin
  result := -1;
  for i := 0 to FList.Count - 1 do
    if TfpgImageItem(FList[i]).Index > result then
      result := TfpgImageItem(FList[i]).Index;
end;

procedure TfpgImageList.Clear;
begin
  FList.Clear;
end;

{ TfpgImageItem }

procedure TfpgImageItem.SetImageList(AImageList: TfpgImageList);
begin
  if AImageList = nil then
  begin
    FImageList := nil;
  end
  else
  begin
    if FImageList <> nil then
      FImageList.RemoveIndex(Index);
    FImageList := AImageList;
    FImageList.Items[Index] := self;
  end;
end;

procedure TfpgImageItem.SetIndex(AIndex: integer);
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageItem.SetIndex');
  {$ENDIF}
  if AIndex <> FIndex then
  begin
    if ImageList <> nil then
      ImageList.RemoveIndex(AIndex);
    FIndex := AIndex;
  end;
end;

procedure TfpgImageItem.SetImage(AImage: TfpgImage);
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageItem.SetImage');
  {$ENDIF}
  FImage := AImage;
end;

constructor TfpgImageItem.Create;
begin
  ImageList := nil;
  FIndex    := -1;
  FImage    := nil;
end;

constructor TfpgImageItem.Create(AImageList: TfpgImageList; AIndex: integer;
    AImage: TfpgImage);
begin
  if AImageList = nil then
    Exit; //==>
  FImage      := AImage;
  FIndex      := AIndex;
  FImageList  := nil;
  ImageList   := AImageList;
end;

constructor TfpgImageItem.Create(AFileName: TfpgString; AIndex: integer);
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageItem.Create(', AFileName, ',', AIndex, ')');
  {$ENDIF}
  Index := AIndex;
  LoadFromFile(AFileName);
end;

destructor TfpgImageItem.Destroy;
begin
  if FImage <> nil then
     FImage.Free;
  inherited Destroy;
end;

procedure TfpgImageItem.LoadFromFile(AFileName: TfpgString);
begin
  {$IFDEF DEBUG}
  writeln('TfpgImageItem.LoadFromFile');
  {$ENDIF}
  if FImage <> nil then
    FImage.Destroy;
  FImage := LoadImage_BMP(AFileName);
end;

end.