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
|
{%mainunit fpgui.pas}
{ Listbox widget implementation }
{$IFDEF read_interface}
TFCustomListBox = class(TFWidget)
private
FHotTrack: Boolean;
FItems: TStrings;
FItemIndex: Integer;
function EvMousePressed(Event: TMousePressedEventObj): Boolean;
function EvMouseReleased(Event: TMouseReleasedEventObj): Boolean;
function EvMouseMoved(Event: TMouseMoveEventObj): Boolean;
function ProcessMouseEvent(Event: TMouseEventObj): Boolean;
protected
ScrollingSupport: TScrollingSupport;
FMaxItemWidth: Integer;
ItemHeight: Integer;
procedure Paint(Canvas: TFCanvas); override;
function ProcessEvent(Event: TEventObj): Boolean; override;
function DistributeEvent(Event: TEventObj): Boolean; override;
// procedure EvKeyPressed(Key: Word; Shift: TShiftState); override;
procedure CalcSizes; override;
procedure Resized; override;
procedure RecalcWidth;
procedure UpdateScrollBars;
procedure RedrawItem(AIndex: Integer);
property CanExpandWidth default True;
property CanExpandHeight default True;
property HotTrack: Boolean read FHotTrack write FHotTrack default False;
property ItemIndex: Integer read FItemIndex write FItemIndex default -1;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Items: TStrings read FItems write FItems;
end;
TFListBox = class(TFCustomListBox)
published
// TWidget properties
property OnClick;
property Enabled;
// TCustomListBox properties
property HotTrack;
property Items;
property ItemIndex;
end;
{$ENDIF read_interface}
{$IFDEF read_implementation}
// ===================================================================
// TFListBoxStrings
// ===================================================================
type
TFListBoxStrings = class(TStringList)
protected
ListBox: TFCustomListBox;
procedure SetUpdateState(Updating: Boolean); override;
public
constructor Create(AListBox: TFCustomListBox);
function Add(const s: String): Integer; override;
end;
constructor TFListBoxStrings.Create(AListBox: TFCustomListBox);
begin
inherited Create;
ListBox := AListBox;
end;
function TFListBoxStrings.Add(const s: String): Integer;
var
ItemWidth: Integer;
begin
Result := inherited Add(s);
if Assigned(ListBox.FindForm) and Assigned(ListBox.FindForm.Wnd) then
begin
ItemWidth := ListBox.FindForm.Wnd.Canvas.TextWidth(s) + 4;
if ItemWidth > ListBox.FMaxItemWidth then
ListBox.FMaxItemWidth := ItemWidth;
ListBox.UpdateScrollBars;
end;
end;
procedure TFListBoxStrings.SetUpdateState(Updating: Boolean);
begin
if not Updating then
ListBox.RecalcWidth;
end;
// ===================================================================
// TCustomListBox
// ===================================================================
constructor TFCustomListBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
FCanExpandWidth := True;
FCanExpandHeight := True;
ScrollingSupport := TScrollingSupport.Create(Self);
ScrollingSupport.HorzScrollBar.OnScroll :=
@ScrollingSupport.DefHorzScrollHandler;
ScrollingSupport.VerTFScrollBar.OnScroll :=
@ScrollingSupport.DefVertScrollHandler;
Items := TFListBoxStrings.Create(Self);
FItemIndex := -1;
UpdateScrollBars;
end;
destructor TFCustomListBox.Destroy;
begin
Items.Free;
ScrollingSupport.Free;
inherited Destroy;
end;
// protected methods
procedure TFCustomListBox.Paint(Canvas: TFCanvas);
var
i, StartIndex, EndIndex: Integer;
ItemRect: TRect;
ItemFlags: TItemFlags;
begin
inherited Paint(Canvas);
if not Canvas.IntersectClipRect(ScrollingSupport.ClientRect) then
exit; //==>
Style.SetUIColor(Canvas, clWindow);
// Style.DrawWindowBackground(Canvas, ScrollingSupport.ClientRect);
Canvas.FillRect(ScrollingSupport.ClientRect);
Style.SetUIColor(Canvas, clWindowText);
with ScrollingSupport.VerTFScrollBar do
begin
StartIndex := Position div ItemHeight;
EndIndex := (Position + PageSize) div ItemHeight;
end;
Canvas.AppendTranslation(ScrollingSupport.ClientRect.TopLeft -
ScrollingSupport.ScrollPos);
if StartIndex < 0 then
StartIndex := 0;
if EndIndex >= Items.Count then
EndIndex := Items.Count - 1;
for i := StartIndex to EndIndex do
begin
Canvas.SaveState;
ItemRect.Left := ScrollingSupport.HorzScrollBar.Position;
ItemRect.Top := i * ItemHeight;
ItemRect.Right := ScrollingSupport.ClientRect.Right -
ScrollingSupport.ClientRect.Left +
ScrollingSupport.HorzScrollBar.Position;
ItemRect.Bottom := (i + 1) * ItemHeight;
Canvas.IntersectClipRect(ItemRect);
ItemFlags := [];
if (wsHasFocus in WidgetState) and ((i = ItemIndex) or
((ItemIndex = -1) and (i = 0))) then
Include(ItemFlags, ifFocused);
if i = ItemIndex then
Include(ItemFlags, ifSelected);
Style.DrawItemBefore(Canvas, ItemRect, ItemFlags);
// Canvas.TextOut(Point(2, i * ItemHeight), Items[i]);
Style.DrawText(Canvas, Point(2, i * ItemHeight), Items[i], WidgetState);
Style.DrawItemAfter(Canvas, ItemRect, ItemFlags);
Canvas.RestoreState;
end;
end;
function TFCustomListBox.ProcessEvent(Event: TEventObj): Boolean;
begin
if Event.InheritsFrom(TMousePressedEventObj) then
Result := ScrollingSupport.ProcessEvent(Event) or
EvMousePressed(TMousePressedEventObj(Event)) or
inherited ProcessEvent(Event)
else if Event.InheritsFrom(TMouseReleasedEventObj) then
Result := ScrollingSupport.ProcessEvent(Event) or
EvMouseReleased(TMouseReleasedEventObj(Event)) or
inherited ProcessEvent(Event)
else if Event.InheritsFrom(TMouseMoveEventObj) then
Result := ScrollingSupport.ProcessEvent(Event) or
EvMouseMoved(TMouseMoveEventObj(Event)) or
inherited ProcessEvent(Event)
else
Result := ScrollingSupport.ProcessEvent(Event) or
inherited ProcessEvent(Event);
end;
function TFCustomListBox.DistributeEvent(Event: TEventObj): Boolean;
begin
Result := ScrollingSupport.DistributeEvent(Event) or
inherited DistributeEvent(Event);
end;
procedure TFCustomListBox.CalcSizes;
begin
ScrollingSupport.CalcSizes;
ItemHeight := FindForm.Wnd.Canvas.FontCellHeight;
ScrollingSupport.VerTFScrollBar.SmallChange := ItemHeight;
RecalcWidth;
end;
procedure TFCustomListBox.Resized;
begin
ScrollingSupport.Resized;
UpdateScrollBars;
end;
procedure TFCustomListBox.RecalcWidth;
var
i, ItemWidth: Integer;
begin
if (not Assigned(FindForm)) or (not Assigned(FindForm.Wnd)) then
exit; //==>
FMaxItemWidth := 0;
for i := 0 to Items.Count - 1 do
begin
ItemWidth := FindForm.Wnd.Canvas.TextWidth(Items[i]) + 4;
if ItemWidth > FMaxItemWidth then
FMaxItemWidth := ItemWidth;
end;
UpdateScrollBars;
end;
procedure TFCustomListBox.UpdateScrollBars;
begin
ScrollingSupport.SetVirtualSize(
Size(FMaxItemWidth, Items.Count * ItemHeight - 1));
end;
procedure TFCustomListBox.RedrawItem(AIndex: Integer);
var
ItemRect: TRect;
begin
if AIndex < 0 then
exit; //==>
ItemRect := ScrollingSupport.ClientRect;
Inc(ItemRect.Top, AIndex * ItemHeight -
ScrollingSupport.VerTFScrollBar.Position);
if (ItemRect.Top > ScrollingSupport.ClientRect.Bottom) or
(ItemRect.Top + ItemHeight <= ScrollingSupport.ClientRect.Top) then
exit;
ItemRect.Bottom := Min(ItemRect.Top + ItemHeight,
ScrollingSupport.ClientRect.Bottom);
Redraw(ItemRect);
end;
// private methods
function TFCustomListBox.EvMousePressed(Event: TMousePressedEventObj): Boolean;
begin
if HotTrack then
Result := False
else if Event.Button = mbLeft then
Result := ProcessMouseEvent(Event)
else
Result := False;
end;
function TFCustomListBox.EvMouseReleased(Event: TMouseReleasedEventObj): Boolean;
begin
if HotTrack and (Event.Button = mbLeft) then
Result := ProcessMouseEvent(Event)
else
Result := False;
end;
function TFCustomListBox.EvMouseMoved(Event: TMouseMoveEventObj): Boolean;
begin
if HotTrack then
Result := ProcessMouseEvent(Event)
else
Result := False;
end;
function TFCustomListBox.ProcessMouseEvent(Event: TMouseEventObj): Boolean;
var
Index: Integer;
begin
if not PtInRect(ScrollingSupport.ClientRect, Event.Position) then
begin
Result := False;
exit;
end;
Index := (Event.Position.y - ScrollingSupport.ClientRect.Top +
ScrollingSupport.VerTFScrollBar.Position) div ItemHeight;
if (Index >= 0) and (Index < Items.Count) and ((Index <> ItemIndex) or
(HotTrack and Event.InheritsFrom(TMouseReleasedEventObj))) then
begin
RedrawItem(ItemIndex);
FItemIndex := Index;
RedrawItem(ItemIndex);
if (not Event.InheritsFrom(TMouseMoveEventObj)) and Assigned(OnClick) then
OnClick(Self);
end;
{ !!!: Re-include this for correct focus handling. But at the moment a focus
change results in a complete widget redraw, which is not very brilliant. }
// inherited ProcessEvent(Event);
Result := True;
end;
{$ENDIF read_implementation}
|