-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDW.VCL.Buttons.pas
475 lines (414 loc) · 14.7 KB
/
DW.VCL.Buttons.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
unit DW.VCL.Buttons;
interface
uses
SysUtils, Classes, db, StrUtils, Controls, DW.VCL.Control, DWTypes,
DW.VCL.Container, DWElementTag;
type
// TDWCustomButton.BSButtonStyle
// Base class for TDWButton and TDWDropDown
TDWCustomButton = class(TDWControl)
private
FBlockLevel: boolean;
FButtonSize: TDWSize;
FButtonStyle: TDWButtonStyle;
FCaption: string;
FGlyphicon: string;
FRawText: boolean;
procedure SetGlyphicon(const Value: string);
procedure SetButtonStyle(const Value: TDWButtonStyle);
procedure SetBlockLevel(const Value: boolean);
procedure SetCaption(const Value: string);
procedure SetRawText(const Value: boolean);
public
constructor Create(AOwner: TComponent); override;
published
// Boostrap Block Level Button @br
// http://getbootstrap.com/css/#buttons-sizes
property BSBlockLevel: boolean read FBlockLevel write SetBlockLevel default False;
// Boostrap Button Size @br
// http://getbootstrap.com/css/#buttons-sizes
property BSButtonSize: TDWSize read FButtonSize write FButtonSize default bsszDefault;
// Bootstrap Button Style @br
// http://getbootstrap.com/css/#buttons-options
property BSButtonStyle: TDWButtonStyle read FButtonStyle write SetButtonStyle
default bsbsDefault;
// Bootstrap Glyphicon @br
// http://getbootstrap.com/components/#glyphicons
property BSGlyphicon: string read FGlyphicon write SetGlyphicon;
// Button Text
property Caption: string read FCaption write SetCaption;
// If true, the Caption will be rendered as Raw HTML
property RawText: boolean read FRawText write SetRawText default False;
end;
// TDWButton.DataDismiss
TDWButtonDataDismiss = (bsbdNone, bsbdModal, bsbdAlert);
// TDWButton.ButtonType
TDWButtonType = (bsbtButton, bsbtSubmit, bsbtReset);
// TDWButton.ElementTypeType
TDWButtonElementType = (bsetAuto, bsetAnchor, bsetButton);
// TDWButton.AsyncClickProc
TDWAsyncEventProc = reference to procedure(Sender: TObject; EventParams: TStringList);
// Bootstrap Button @br
// http://getbootstrap.com/css/#buttons @br
// http://www.w3schools.com/bootstrap/bootstrap_buttons.asp
TDWButton = class(TDWCustomButton)
private
FAsyncClickProc: TDWAsyncEventProc;
FButtonType: TDWButtonType;
FDataDismiss: TDWButtonDataDismiss;
FDataParent: TDWContainer;
FDataTarget: TDWContainer;
FElementType: TDWButtonElementType;
FHotKey: string;
FHref: string;
FTarget: string;
FImageSrc: string;
FImagePosition: TDWBtnImagePosition;
procedure DoAsyncClickProc(Sender: TObject; EventParams: TStringList);
procedure SetAsyncClickProc(Value: TDWAsyncEventProc);
function IsHrefStored: boolean;
function IsTargetStored: boolean;
function IsAnchor: boolean;
procedure SetDataTarget(const Value: TDWContainer);
procedure SetDataParent(const Value: TDWContainer);
procedure SetHref(const Value: string);
procedure SetTarget(const Value: string);
procedure SetDataDismiss(const Value: TDWButtonDataDismiss);
procedure SetImageSrc(const Value: string);
procedure SetImagePosition(const Value: TDWBtnImagePosition);
function GetDataToggle: string;
procedure SetElementType(const Value: TDWButtonElementType);
protected
procedure InternalRenderCss(var ACss: string); override;
procedure InternalRenderHTML(var AHTMLTag: TDWElementTag); override;
public
constructor Create(AOwner: TComponent); override;
// Anonymous procedure that let you execute code when button is pressed without the need of declare an event. @br
// Usefull when you create buttons at runtime
property AsyncClickProc: TDWAsyncEventProc read FAsyncClickProc write SetAsyncClickProc;
published
// Button type
property ButtonType: TDWButtonType read FButtonType write FButtonType default bsbtButton;
// Used when button in placed on a TDWModal or TDWAlert. @br
// Let the button automatically close the dialog.
property DataDismiss: TDWButtonDataDismiss read FDataDismiss write SetDataDismiss
default bsbdNone;
// Specifies the parent region off collapsable regions to autoclose other regions when one is toggled. See accordion example. @br
// http://getbootstrap.com/javascript/#collapse-example-accordion @br
// http://www.w3schools.com/bootstrap/bootstrap_collapse.asp
property DataParent: TDWContainer read FDataParent write SetDataParent;
// Specifies the target region for for toggle visiblity of TDWModal or TDWRegion.Collapse @br
// http://www.w3schools.com/bootstrap/bootstrap_modal.asp @br
// http://www.w3schools.com/bootstrap/bootstrap_collapse.asp
property DataTarget: TDWContainer read FDataTarget write SetDataTarget;
// Toogle operation
property DataToggle: string read GetDataToggle;
// acceskey tag atribute @br
// http://www.w3schools.com/tags/att_global_accesskey.asp
property HotKey: string read FHotKey write FHotKey;
// this property determines if the element will be type anchor or button, default is Auto
property ElementType: TDWButtonElementType read FElementType write SetElementType
default bsetAuto;
// Destination address to jump when button is pressed. @br
// Requires following property values: Anchor = true, DataTarget = nil, OnAsyncClic = nil. @br
// http://www.w3schools.com/html/html_links.asp
property Href: string read FHref write SetHref stored IsHrefStored;
// The target attribute specifies where to open the linked document. Apply when Href is used. @br
// http://www.w3schools.com/html/html_links.asp
property Target: string read FTarget write SetTarget stored IsTargetStored;
property ImageSrc: string read FImageSrc write SetImageSrc;
property ImagePosition: TDWBtnImagePosition read FImagePosition write SetImagePosition;
end;
const
// @exclude
aDWButtonStyle: array [bsbsDefault .. bsbsClose] of string = ('btn-default', 'btn-primary',
'btn-success', 'btn-info', 'btn-warning', 'btn-danger', 'btn-link', 'close');
implementation
uses DW.VCL.Region, DW.VCL.Common, DW.VCL.ButtonGroup, DW.VCL.NavBar,
DWUtils, DW.VCL.CustomRegion, DW.VCL.InputForm, DW.VCL.Modal, DW.VCL.List;
{$REGION 'TDWCustomButton'}
constructor TDWCustomButton.Create(AOwner: TComponent);
begin
inherited;
FButtonSize := bsszDefault;
FButtonStyle := bsbsDefault;
FCaption := '';
FGlyphicon := '';
// FNeedsFormTag := True;
Height := 25;
Width := 200;
end;
procedure TDWCustomButton.SetBlockLevel(const Value: boolean);
begin
FBlockLevel := Value;
Invalidate;
end;
procedure TDWCustomButton.SetButtonStyle(const Value: TDWButtonStyle);
begin
FButtonStyle := Value;
Invalidate;
end;
procedure TDWCustomButton.SetCaption(const Value: string);
begin
FCaption := Value;
AsyncRefreshControl;
end;
procedure TDWCustomButton.SetGlyphicon(const Value: string);
begin
FGlyphicon := Value;
Invalidate;
end;
procedure TDWCustomButton.SetRawText(const Value: boolean);
begin
FRawText := Value;
AsyncRefreshControl;
end;
{$ENDREGION}
{$REGION 'TDWButton'}
constructor TDWButton.Create(AOwner: TComponent);
begin
inherited;
FButtonType := bsbtButton;
FDataDismiss := bsbdNone;
FElementType := bsetAuto;
FHotKey := '';
FHref := '#';
FTarget := '';
end;
procedure TDWButton.InternalRenderCss(var ACss: string);
begin
inherited;
if (Parent is TDWRegion) and (TDWRegion(Parent).BSRegionType = bsrtListGroup) then
begin
TDWBSCommon.AddCssClass(ACss, 'list-group-item');
end
else if (FDataTarget <> nil) and (FDataTarget is TDWNavBarCollapse) then
begin
TDWBSCommon.AddCssClass(ACss, 'navbar-toggle');
end
else if (Parent is TDWButtonGroup) or not IsAnchor then
begin
TDWBSCommon.AddCssClass(ACss, 'btn');
if FButtonSize <> bsszDefault then
TDWBSCommon.AddCssClass(ACss, 'btn-' + aDWSize[FButtonSize]);
TDWBSCommon.AddCssClass(ACss, aDWButtonStyle[FButtonStyle]);
if FBlockLevel then
TDWBSCommon.AddCssClass(ACss, 'btn-block');
if (Parent is TDWNavBarBase) then
TDWBSCommon.AddCssClass(ACss, 'navbar-btn')
else if (Parent is TDWRegion) and (TDWRegion(Parent).BSRegionType = bsrtDropDown) then
TDWBSCommon.AddCssClass(ACss, 'dropdown-toggle');
end;
end;
procedure TDWButton.InternalRenderHTML(var AHTMLTag: TDWElementTag);
const
aDWButtonDataDismiss: array [bsbdNone .. bsbdAlert] of string = ('', 'modal', 'alert');
var
s: string;
lAnchor: boolean;
lTarget: string;
begin
inherited;
lAnchor := IsAnchor;
AHTMLTag := TDWElementTag.CreateHTMLTag(iif(lAnchor, 'a', 'button'));
try
AHTMLTag.AddStringParam('id', HTMLName);
AHTMLTag.AddClassParam(ActiveCss);
// button type
if not lAnchor then
if FButtonType = bsbtButton then
AHTMLTag.AddStringParam('type', 'button')
else if FButtonType = bsbtSubmit then
AHTMLTag.AddStringParam('type', 'submit')
else if FButtonType = bsbtReset then
AHTMLTag.AddStringParam('type', 'reset');
if ShowHint and (Hint <> '') then
AHTMLTag.AddStringParam('title', Hint);
if IsDisabled then
AHTMLTag.Add('disabled');
AHTMLTag.AddStringParam('style', ActiveStyle);
if TabIndex <> 0 then
AHTMLTag.AddStringParam('tabindex', IntToStr(TabIndex));
// caption
if FRawText then
s := Caption
else
s := TDWBSCommon.TextToHTML(Caption);
// hotkey
if FHotKey <> '' then
begin
AHTMLTag.AddStringParam('accesskey', FHotKey);
s := StringReplace(s, FHotKey, '<u>' + FHotKey + '</u>', [rfIgnoreCase]);
end;
// glyphicon
if FGlyphicon <> '' then
with AHTMLTag.Contents.AddElement('span') do
begin
AddClassParam('glyphicon glyphicon-' + FGlyphicon);
AddBooleanParam('aria-hidden', true);
s := ' ' + s;
end;
// close button
if (FButtonStyle = bsbsClose) then
begin
AHTMLTag.AddStringParam('aria-label', 'Close');
if (s = '') and (FGlyphicon = '') then
s := '×';
end;
// data-dismiss
if FDataDismiss <> bsbdNone then
AHTMLTag.AddStringParam('data-dismiss', aDWButtonDataDismiss[FDataDismiss]);
// datatarget / href
if FDataTarget = nil then
begin
if lAnchor then
begin
AHTMLTag.AddStringParam('href', FHref);
if FTarget = '' then
if AnsiStartsStr('#', FHref) then
lTarget := '_self'
else
lTarget := '_blank'
else
lTarget := FTarget;
AHTMLTag.AddStringParam('target', lTarget);
end;
end
else
begin
if lAnchor then
AHTMLTag.AddStringParam('href', '#' + FDataTarget.HTMLName)
else
AHTMLTag.AddStringParam('data-target', '#' + FDataTarget.HTMLName);
if FDataParent <> nil then
AHTMLTag.AddStringParam('data-parent', '#' + FDataParent.HTMLName);
// draw a menu button if no caption and no glyphicon
if (s = '') and (FGlyphicon = '') then
begin
AHTMLTag.Contents.AddElement('span').AddClassParam('icon-bar');
AHTMLTag.Contents.AddElement('span').AddClassParam('icon-bar');
AHTMLTag.Contents.AddElement('span').AddClassParam('icon-bar');
end;
end;
// datatoggle
AHTMLTag.AddStringParam('data-toggle', DataToggle);
// caption after glyphicon
if (s <> '') and (FImageSrc = '') then
AHTMLTag.Contents.AddText(s)
else if FImagePosition = bsbtimgLeft then
begin
// image before Caption
if FImageSrc <> '' then
AHTMLTag.Contents.AddText('<img src="' + FImageSrc + '"></img>');
AHTMLTag.Contents.AddText(s);
end
else if FImagePosition = bsbtimgRight then
begin
// image After Caption
AHTMLTag.Contents.AddText(s);
if FImageSrc <> '' then
AHTMLTag.Contents.AddText('<img src="' + FImageSrc + '"></img>');
end;
except
FreeAndNil(AHTMLTag);
raise;
end;
if Parent is TDWInputGroup then
AHTMLTag := DWCreateInputGroupAddOn(AHTMLTag, HTMLName, 'btn')
else
AHTMLTag := DWCreateFormGroup(Parent, DWFindParentInputForm(Parent), AHTMLTag, HTMLName, true);
// wrap item if parent is list
TDWList.WrapItem(Self, AHTMLTag);
end;
function TDWButton.IsAnchor: boolean;
begin
if FElementType = bsetAuto then
begin
if FDataTarget <> nil then
Result := False
else if FHref <> '#' then
Result := true
else if (Parent is TDWNavBarBase) then
Result := False
else if (Parent is TDWList) then
Result := true
else if (Parent is TDWRegion) and (TDWRegion(Parent).BSRegionType = bsrtListGroup) then
Result := true
else if (Parent is TDWButtonGroup) and TDWButtonGroup(Parent).BSJustified then
Result := true
else
Result := False;
end
else
Result := FElementType = bsetAnchor;
end;
function TDWButton.IsHrefStored: boolean;
begin
Result := FHref <> '#';
end;
function TDWButton.IsTargetStored: boolean;
begin
Result := FTarget <> '';
end;
procedure TDWButton.DoAsyncClickProc(Sender: TObject; EventParams: TStringList);
begin
FAsyncClickProc(Sender, EventParams);
end;
function TDWButton.GetDataToggle: string;
begin
if (Parent is TDWRegion) and (TDWRegion(Parent).BSRegionType = bsrtDropDown) then
Result := 'dropdown'
else if (FDataTarget = nil) then
Result := ''
else if (FDataTarget is TDWModal) then
Result := 'modal'
else if (FDataTarget is TDWCustomRegion) and TDWCustomRegion(FDataTarget).Collapse then
Result := 'collapse'
else if (FDataTarget is TDWNavBarCollapse) then
Result := 'collapse';
end;
procedure TDWButton.SetAsyncClickProc(Value: TDWAsyncEventProc);
begin
FAsyncClickProc := Value;
OnAsyncClick := DoAsyncClickProc
end;
procedure TDWButton.SetHref(const Value: string);
begin
FHref := Value;
AsyncRefreshControl;
end;
procedure TDWButton.SetImagePosition(const Value: TDWBtnImagePosition);
begin
FImagePosition := Value;
end;
procedure TDWButton.SetImageSrc(const Value: string);
begin
FImageSrc := Value;
end;
procedure TDWButton.SetTarget(const Value: string);
begin
FTarget := Value;
AsyncRefreshControl;
end;
procedure TDWButton.SetDataDismiss(const Value: TDWButtonDataDismiss);
begin
FDataDismiss := Value;
AsyncRefreshControl;
end;
procedure TDWButton.SetDataParent(const Value: TDWContainer);
begin
FDataParent := Value;
AsyncRefreshControl;
end;
procedure TDWButton.SetDataTarget(const Value: TDWContainer);
begin
FDataTarget := Value;
AsyncRefreshControl;
end;
procedure TDWButton.SetElementType(const Value: TDWButtonElementType);
begin
FElementType := Value;
AsyncRefreshControl;
end;
{$ENDREGION}
end.