-
Notifications
You must be signed in to change notification settings - Fork 4
/
untbuttonlist.pas
425 lines (354 loc) · 11.7 KB
/
untbuttonlist.pas
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
{ A Lazarus component for button list
Copyright (C) 2010 Ido Kanner idokan at@at gmail dot.dot com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
}
unit untButtonList;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Buttons, Controls, Forms, LMessages;
type
TLayoutDirection = (ldHorizontal, ldVertical);
Const
DEFAULT_BREAK_ON = 5;
DEFAULT_BUTTON_COUNT = 10;
DEFAULT_LAYOUT_DIRECTION = ldVertical;
DEFAULT_SHOW_BUTTON_HINTS = true;
type
EButtonCount = class(Exception);
TButtonClick = procedure (Index : Word) of object;
{ TCustomButtonList }
TCustomButtonList = class(TScrollingWinControl)
private
procedure SetBreakOn ( const AValue : Word ) ;
procedure SetButtonCount ( const AValue : Word ) ;
procedure SetLayoutDirection ( const AValue : TLayoutDirection ) ;
procedure SetCaptions ( const AValue : TStringList ) ;
procedure SetHints ( const AValue : TStringList ) ;
procedure SetButtonHints ( const AValue : Boolean ) ;
protected
FBreakOn : Word;
FButtons : array of TBitBtn;
FButtonCount : Word;
FCaptions : TStringList;
FHints : TStringList;
FLayoutDirection : TLayoutDirection;
FShowButtonHints : Boolean;
FButtonClick : TButtonClick;
procedure CreateButtons(const aFrom, aTo : Word); virtual;
procedure CreateButtons; virtual;
procedure WMSize(var Message: TLMSize); message LM_SIZE;
function CalcHeight : Integer; virtual;
function LayoutDirectionToChildLayout(const aDirection : TLayoutDirection) : TControlChildrenLayout; virtual;
procedure UpdateCaptions; virtual;
procedure UpdateHints; virtual;
procedure CaptionChanged(Sender : TObject); virtual;
procedure HintChanged(Sender : TObject); virtual;
procedure ButtonClicked(Sender : TObject); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property BreakOn : Word read FBreakOn write SetBreakOn
default DEFAULT_BREAK_ON;
property Captions : TStringList read FCaptions write SetCaptions;
property Count : Word read FButtonCount write SetButtonCount
default DEFAULT_BUTTON_COUNT;
property Hints : TStringList read FHints write SetHints;
property LayoutDirection : TLayoutDirection read FLayoutDirection
write SetLayoutDirection
default DEFAULT_LAYOUT_DIRECTION;
property ShowHints : Boolean read FShowButtonHints write SetButtonHints
default DEFAULT_SHOW_BUTTON_HINTS;
property OnButtonClick : TButtonClick read FButtonClick write FButtonClick;
end;
TButtonList = class(TCustomButtonList)
published
property Align;
property Anchors;
property AutoScroll default True;
property AutoSize;
property BorderSpacing;
property BiDiMode;
property BorderStyle default bsSingle;
property BreakOn;
property Captions;
property ChildSizing;
property ClientHeight;
property ClientWidth;
property Constraints;
property Count;
property Color nodefault;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property LayoutDirection;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property ShowHints;
property TabOrder;
property TabStop;
property Visible;
property OnButtonClick;
property OnClick;
property OnConstrainedResize;
property OnDblClick;
property OnDockDrop;
property OnDockOver;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property OnPaint;
end;
procedure Register;
resourcestring
errCreationIndexOutOfRange = 'Creation index out of range';
errToIsBiggerThenFrom = 'To is bigger then from';
errCannotCreateEmptyButtonList = 'Cannot create empty button list';
implementation
{ TCustomButtonList }
constructor TCustomButtonList.Create ( AOwner : TComponent ) ;
begin
inherited Create ( AOwner ) ;
ControlStyle := ControlStyle - [csAcceptsControls, csSetCaption]
+ [csOpaque];
SetInitialBounds(
0, 0,
GetControlClassDefaultSize.X,
GetControlClassDefaultSize.Y
);
FBreakOn := DEFAULT_BREAK_ON;
FButtonCount := DEFAULT_BUTTON_COUNT;
FLayoutDirection := DEFAULT_LAYOUT_DIRECTION;
// Setting control layout
ChildSizing.Layout := LayoutDirectionToChildLayout(FLayoutDirection);
ChildSizing.ControlsPerLine := FBreakOn;
ChildSizing.EnlargeHorizontal := crsScaleChilds;
AutoScroll := True;
AutoSize := False;
FCaptions := TStringList.Create;
FCaptions.OnChange := @CaptionChanged;
FHints := TStringList.Create;
FHints.OnChange := @HintChanged;
FShowButtonHints := DEFAULT_SHOW_BUTTON_HINTS;
SetLength(FButtons, FButtonCount);
CreateButtons;
end;
destructor TCustomButtonList.Destroy;
var
i : word;
begin
FreeAndNil(FCaptions);
FreeAndNil(FHints);
for i := Low(FButtons) to High(FButtons) do
begin
if Assigned(FButtons[i]) then
FreeAndNil(FButtons[i]);
end;
inherited Destroy;
end;
procedure TCustomButtonList.CreateButtons ( const aFrom, aTo : Word ) ;
var
i : integer;
BtnHeight : integer;
begin
if aFrom > High(FButtons) then
raise EButtonCount.Create(errCreationIndexOutOfRange);
if aTo = 0 then raise EButtonCount.Create(errCreationIndexOutOfRange);
if aFrom > aTo then raise EButtonCount.Create(errToIsBiggerThenFrom);
BtnHeight := CalcHeight;
for i := aFrom to aTo do
begin
if not Assigned(FButtons[i]) then
FButtons[i] := TBitBtn.Create(self);
FButtons[i].AutoSize := true;
FButtons[i].Parent := Self;
FButtons[i].ShowHint := FShowButtonHints;
FButtons[i].Constraints.MinHeight := BtnHeight;
FButtons[i].Tag := i;
FButtons[i].OnClick := @ButtonClicked;
end;
end;
procedure TCustomButtonList.CreateButtons;
begin
CreateButtons(Low(FButtons), High(FButtons));
end;
procedure TCustomButtonList.WMSize ( var Message : TLMSize ) ;
var
i : integer;
BtnHeight : integer;
begin
Message.Result := 1;
// Recalculate the height of the buttons
// depands on the new height
BtnHeight := CalcHeight;
for i := Low(FButtons) to High(FButtons) do
begin
FButtons[i].Constraints.MinHeight := BtnHeight;
end;
if Assigned(OnResize) then
OnResize(Self);
end;
function TCustomButtonList.CalcHeight : Integer;
begin
Result := (Self.ClientHeight div FBreakOn);
// We can not go bellow 26 pixels on hight
if Result < 30 then
Result := 30;
end;
function TCustomButtonList.LayoutDirectionToChildLayout (
const aDirection : TLayoutDirection ) : TControlChildrenLayout;
const
Layout : array[TLayoutDirection] of TControlChildrenLayout =
(cclLeftToRightThenTopToBottom, cclTopToBottomThenLeftToRight);
begin
Result := Layout[aDirection];
end;
procedure TCustomButtonList.SetBreakOn ( const AValue : Word ) ;
begin
if AValue <> FBreakOn then
begin
FBreakOn := AValue;
ChildSizing.ControlsPerLine := FBreakOn;
end;
end;
procedure TCustomButtonList.SetButtonCount ( const AValue : Word ) ;
var
i : integer;
begin
if AValue = 0 then
raise EButtonCount.Create(errCannotCreateEmptyButtonList);
if AValue <> FButtonCount then
begin
if FButtonCount > AValue then
begin
for i := AValue to FButtonCount -1 do
FreeAndNil(FButtons[i]);
SetLength(FButtons, AValue); // shrink array
end
else begin
SetLength(FButtons, AValue); // resize array
CreateButtons(FButtonCount, AValue-1);
end;
FButtonCount := AValue;
end;
end;
procedure TCustomButtonList.SetLayoutDirection (
const AValue : TLayoutDirection ) ;
begin
if AValue <> FLayoutDirection then
begin
FLayoutDirection := AValue;
ChildSizing.Layout := LayoutDirectionToChildLayout(FLayoutDirection);
end;
end;
procedure TCustomButtonList.CaptionChanged ( Sender : TObject ) ;
begin
UpdateCaptions;
end;
procedure TCustomButtonList.HintChanged ( Sender : TObject ) ;
begin
UpdateHints;
end;
procedure TCustomButtonList.UpdateCaptions;
var
i : word;
max_captions : word;
begin
if FCaptions.Count <= 0 then exit;
if FCaptions.Count > Length(FButtons) then
max_captions := Length(FButtons)
else
max_captions := FCaptions.Count;
for i := 0 to max_captions -1 do
begin
FButtons[i].Caption := FCaptions.Strings[i];
end;
end;
procedure TCustomButtonList.UpdateHints;
var
i : word;
max_hints : word;
begin
if FHints.Count <= 0 then exit;
if FHints.Count > Length(FButtons) then
max_hints := Length(FButtons)
else
max_hints := FHints.Count;
for i := 0 to max_hints -1 do
begin
FButtons[i].Hint := FHints.Strings[i];
end;
end;
procedure TCustomButtonList.SetCaptions ( const AValue : TStringList ) ;
begin
if AValue.Equals(FCaptions) then exit;
FCaptions.Clear;
FCaptions.Assign(AValue);
UpdateCaptions;
end;
procedure TCustomButtonList.SetHints ( const AValue : TStringList ) ;
begin
if AValue.Equals(FHints) then exit;
FHints.Clear;
FHints.Assign(AValue);
UpdateHints;
end;
procedure TCustomButtonList.SetButtonHints ( const AValue : Boolean ) ;
var
i : word;
begin
if FShowButtonHints = AValue then exit;
FShowButtonHints := AValue;
for i := Low(FButtons) to High(FButtons) do
begin
FButtons[i].ShowHint := FShowButtonHints;
end;
end;
procedure TCustomButtonList.ButtonClicked ( Sender : TObject ) ;
begin
if Assigned(FButtonClick) then
FButtonClick(TBitBtn(Sender).Tag);
end;
procedure Register;
begin
RegisterComponents('LINESIP Buttons', [TButtonList]);
end;
end.