summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fpgtk/src/fpglib.pp
blob: 824b9b42b9168cf52c373fa9dec1f2ca837dfd5a (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
{$mode objfpc}{$h+}
unit FPGlib;

interface

uses classes, glib;

type

  TLukForEachProcedure = procedure (item : pointer; data : pointer) of object;

  TLList = class (TList)
  private
    FGSList : PGSList;
    FGList : PGList;
    FNotUpdating,
    FClassesChanged,FSlistChanged,FListChanged : Boolean;
    procedure FreeList;
    procedure FreeSList;
    function CreateGList : PGList;
    function GetTheGtkList : PGList;
    procedure SetGtkList (Value : PGList);
    function CreateGSList : PGSList;
    function GetTheGtkSList : PGSlist;
    procedure SetGtkSList (Value : PGSList);
    procedure BuildFromGtkList;
    procedure BuildFromGtkSList;
  protected
    procedure Notify (Ptr: Pointer; Action: TListNotification); override;
    function GetData (index : integer) : pointer; dynamic;
    function UngetData (data : pointer) : pointer; dynamic;
    // GetData needs to give the pointer to the data in the List or SList of GTK
    // UngetData needs to give the item in this list from the datapointer of GTK
  public
    constructor create;
    destructor destroy; override;
    function GetGtkList (buffered : boolean) : PGList;
    function GetGtkSList (buffered : boolean) : PGSlist;
    procedure BeginUpdate;  // Currently only changes in 1 list are possible
    procedure EndUpdate;    // without memory leaks and/or errors in the list
    procedure ForEach (Proc : TLukForEachprocedure; data : pointer);
    property GtkList : PGList read GetTheGtkList write SetGtkList;
    property GtkSList : PGSList read GetTheGtkSList write SetGtkSList;
  end;

implementation

{ TLList }

procedure TLList.FreeList;
begin
  if FGList <> null then
    begin
    g_list_free (FGList);
    FGList := null;
    end;
end;

procedure TLList.FreeSList;
begin
  if FGSList <> null then
    begin
    g_slist_free (FGSList);
    FGSlist := null;
    end;
end;

procedure TLList.Notify(Ptr: Pointer; Action: TListNotification);
begin
  inherited;
  FClassesChanged := True;
end;

constructor TLList.create;
begin
  inherited create;
  FClassesChanged := False;
  FListChanged := false;
  FSListChanged := False;
  FGList := null;
  FGSList := null;
  FNotUpdating := True;
end;

destructor TLList.destroy;
begin
  FreeList;
  FreeSList;
  inherited Destroy;
end;

function TLList.GetGtkList (buffered : boolean) : PGList;
begin
  if buffered then
    if FClasseschanged then
      result := CreateGList
    else if FSListChanged then
      begin
      BuildFromGtkSList;
      result := CreateGList;
      end
    else
      result := FGlist
  else
    result := CreateGList;
end;

function TLList.GetGtkSList (buffered : boolean) : PGSList;
begin
  if buffered then
    if FClassesChanged then
      result := CreateGSList
    else if FListChanged then
      begin
      BuildFromGtkList;
      result := CreateGSList;
      end
    else
      result := FGSlist
  else
    result := CreateGSList;
end;

function TLList.CreateGList : PGList;
var r : integer;
begin
  FreeList;
  result := null;
  for r := pred(count) downto 0 do
    result := g_list_prepend (result, GetData(r));
  FGList := result;
end;

function TLList.CreateGSList : PGSList;
var r : integer;
begin
  FreeSList;
  result := null;
  for r := pred(count) downto 0 do
    result := g_slist_prepend (result, GetData(r));
  FGSList := result;
end;

function TLList.GetData (index : integer) : pointer;
begin
  result := items[index];
end;

function TLList.UngetData (data : pointer) : pointer;
begin
  result := data
end;

function TLList.GetTheGtkList : PGList;
begin
  result := GetGtkList (True);
end;

procedure TLList.SetGtkList (Value : PGList);
begin
  FGList := Value;
  if FNotUpdating then
    BuildFromGtkList
  else
    FListChanged := True;
end;

function TLList.GetTheGtkSList : PGSlist;
begin
  result := GetGtkSList (True);
end;

procedure TLList.SetGtkSList (Value : PGSList);
begin
  FGSlist := Value;
  if FNotUpdating then
    BuildFromGtkSList
  else
    FSListChanged := True;
end;

procedure TLList.BuildFromGtkList;
var p : PGList;
begin
  clear;
  p := FGList;
  while p <> null do
    begin
    add (UngetData(p^.data));
    p := p^.Next;
    end;
  FListChanged := False;
  FSListChanged := False;
  FClassesChanged := False;
  FreeSList;
end;

procedure TLList.BuildFromGtkSList;
var p :PGSList;
begin
  clear;
  p := FGSList;
  while p <> null do
    begin
    add (UngetData(p^.data));
    p := p^.Next;
    end;
  FListChanged := False;
  FSListChanged := False;
  FClassesChanged := False;
  FreeList;
end;

procedure TLList.BeginUpdate;
begin
  FNotUpdating := False;
end;

procedure TLList.EndUpdate;
begin
  FNotUpdating := True;
  if FlistChanged then
    BuildFromGtkSList
  else if FSListChanged then
    BuildFromGtkSList
  else if FClassesChanged then
    begin
    FreeSList;
    FreeList;
    end;
end;

procedure TLList.ForEach (Proc : TLukForEachProcedure; data : pointer);
var r: integer;
begin
  for r := 0 to pred(count) do
    Proc (items[r], data);
end;

end.