Menu

[r3136]: / branches / 3.x / Src / FrCodeGenPrefs.pas  Maximize  Restore  History

Download this file

650 lines (596 with data), 21.3 kB

  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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
{
* FrCodeGenPrefs.pas
*
* Implements a frame that allows user to set source code generation
* preferences. Designed for use as one of the tabs in the preferences dialog
* box.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is FrCodeGenPrefs.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2010-2013 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FrCodeGenPrefs;
interface
uses
// Delphi
Classes, ActnList, StdCtrls, Controls, ComCtrls, Menus, Buttons,
// Project
FrPrefsBase, UPreferences, UWarnings;
type
TCodeGenPrefsFrame = class(TPrefsBaseFrame)
chkSwitchOff: TCheckBox;
lvWarnings: TListView;
lblSymbol: TLabel;
lblMinCompiler: TLabel;
edSymbol: TEdit;
edMinCompiler: TEdit;
btnAdd: TButton;
btnDelete: TButton;
btnUpdate: TButton;
actMain: TActionList;
actAdd: TAction;
actUpdate: TAction;
actDelete: TAction;
btnPreview: TButton;
actPreview: TAction;
btnPredefined: TBitBtn;
mnuPreDefCompilers: TPopupMenu;
procedure actAddExecute(Sender: TObject);
procedure actAddUpdate(Sender: TObject);
procedure actUpdateExecute(Sender: TObject);
procedure actUpdateUpdate(Sender: TObject);
procedure actDeleteExecute(Sender: TObject);
procedure actDeleteUpdate(Sender: TObject);
procedure chkSwitchOffClick(Sender: TObject);
procedure lvWarningsClick(Sender: TObject);
procedure edMinCompilerKeyPress(Sender: TObject; var Key: Char);
procedure actPreviewExecute(Sender: TObject);
procedure actPreviewUpdate(Sender: TObject);
procedure btnPredefinedClick(Sender: TObject);
private
fWarnings: IWarnings; // Object that stores details of warnings
procedure PopulateLV;
{Populates list view with details of warnings.
}
procedure AddWarningToLV(const Warning: TWarning);
{Adds details of a warning to list view.
@param Warning [in] Warning to be added.
}
function FormatCompilerVer(const Ver: Single): string;
{Formats compiler version number as a string.
@param Ver [in] Version number to be formatted.
@return Formatted string.
}
function IndexOfSymbolInLV(const Symbol: string): Integer;
{Gets the index of the list item containing a warning symbol in the list
view.
@param Symbol [in] Symbol to be found.
@return Index of required list view item or -1 if not found.
}
function GetSymbol(out Symbol: string): Boolean;
{Gets warning symbol from edit control.
@param Symbol [out] Required symbol, trimmed of white space. Undefined
if not valid.
@return True if symbol is non-empty string, False if not.
}
function IsValidSymbol: Boolean;
{Checks if symbol entered in edit control is valid.
@return True if valid, False if not.
}
function GetCompilerVersion(out Ver: Single): Boolean;
{Gets a compiler version number from edit control.
@param Ver [out] Required version number. Undefined if not valid.
@return True if version number is valid, False if not.
}
function IsValidCompilerVersion: Boolean;
{Checks if compiler version entered in edit control is valid.
@return True if valid, False if not.
}
procedure PopulatePreDefCompilerMenu;
{Populates pre-defined compiler version pop-up menu with menu items.
}
procedure PreDefCompilerMenuClick(Sender: TObject);
{Handler for click events on any pre-defined compiler menu item. Stores
required compiler version in associated edit control.
@param Sender [in] Reference to clicked menu item.
}
public
constructor Create(AOwner: TComponent); override;
{Constructor. Sets up frame and populates controls.
@param AOwner [in] Component that owns frame.
}
procedure Activate(const Prefs: IPreferences); override;
{Called when page activated. Records details of warnings and updates
controls accordingly.
@param Prefs [in] Object that provides info used to update controls.
}
procedure Deactivate(const Prefs: IPreferences); override;
{Called when page is deactivated. Updates warnings and preferences objects
from information entered in tab.
@param Prefs [in] Object used to store information.
}
procedure ArrangeControls; override;
{Arranges controls on frame. Called after frame has been sized.
}
function DisplayName: string; override;
{Provides caption that is displayed in the tab sheet that contains this
frame when displayed in the preference dialog box.
@return Required display name.
}
class function Index: Byte; override;
{Provides an index number that determines the order in which the tabs
containing frames are displayed in the preferences dialog box.
@return Required index number.
}
end;
implementation
uses
// Delphi
SysUtils, Types,
// Project
FmPreferencesDlg, FmPreviewDlg, IntfCommon, UCtrlArranger, UKeysHelper,
UUtils;
{$R *.dfm}
type
{
TPreDefCompilerMenuItem:
Custom menu item with additional property to store a compiler version and
extended constructor that takes all required properties and event handlers.
}
TPreDefCompilerMenuItem = class(TMenuItem)
strict private
fCompilerVer: Single; // Value of CompilerVer property
public
constructor Create(AOwner: TComponent; const ACaption: string;
const ACompilerVer: Single; const AClickHandler: TNotifyEvent);
reintroduce;
{Constructor. Used to fully create a menu item with all required
properties and event handlers.
@param AOwner [in] Component that owns menu item.
@param ACaption [in] Name of compiler displayed in menu item's caption.
@param ACompilerVer [in] Version number of compiler named in ACaption.
@param AClickHandler [in] Menu item's OnClick event handler.
}
property CompilerVer: Single read fCompilerVer write fCompilerVer;
{Version number of compiler whose name is displayed in menu item's
Caption}
end;
{ TCodeGenPrefsFrame }
procedure TCodeGenPrefsFrame.actAddExecute(Sender: TObject);
{Creates a new warning from data entered in edit controls and adds it to the
list view and records in warnings list.
@param Sender [in] Not used.
}
var
W: TWarning; // new warning
Symbol: string; // name of warning's symbol
CompilerVer: Single; // min compiler version that supports warning
begin
// Add new warning from edit controls to fWarnings and list view
GetSymbol(Symbol);
GetCompilerVersion(CompilerVer);
W := TWarning.Create(Symbol, CompilerVer);
fWarnings.Add(W);
AddWarningToLV(W);
// Select new list item and make it visible
lvWarnings.Selected := lvWarnings.Items[IndexOfSymbolInLV(Symbol)];
lvWarnings.Selected.MakeVisible(False);
end;
procedure TCodeGenPrefsFrame.actAddUpdate(Sender: TObject);
{Updates state of action that creates and records a warning. Action enabled
only if data in edit controls is valid.
@param Sender [in] Not used.
}
var
CanAdd: Boolean; // flag that records whether a warning can be added to list
// ---------------------------------------------------------------------------
function SymbolIsDuplicate: Boolean;
{Checks if symbol entered in edit control is a duplicate of an existing
symbol in the list view.
@return True if duplicate, False if not.
}
var
Symbol: string; // symbol from edit control
begin
GetSymbol(Symbol);
Result := IndexOfSymbolInLV(Symbol) >= 0;
end;
// ---------------------------------------------------------------------------
begin
CanAdd := True;
if not IsValidSymbol then
CanAdd := False
else if SymbolIsDuplicate then
CanAdd := False
else if not IsValidCompilerVersion then
CanAdd := False;
actAdd.Enabled := CanAdd;
end;
procedure TCodeGenPrefsFrame.actDeleteExecute(Sender: TObject);
{Deletes selected warning from warnings list and list view.
@param Sender [in] Not used.
}
var
Symbol: string; // symbol of selected warning
begin
// Delete selected warning
Symbol := Trim(lvWarnings.Selected.Caption);
lvWarnings.Selected.Delete;
fWarnings.Delete(Symbol);
// Ensure nothing selected in list view and clear edit controls
lvWarnings.Selected := nil;
edSymbol.Text := '';
edMinCompiler.Text := '';
end;
procedure TCodeGenPrefsFrame.actDeleteUpdate(Sender: TObject);
{Updates state of action that deletes a warning. Action enabled only if an
item is selected in list view.
@param Sender [in] Not used.
}
begin
actDelete.Enabled := Assigned(lvWarnings.Selected);
end;
procedure TCodeGenPrefsFrame.Activate(const Prefs: IPreferences);
{Called when page activated. Records details of warnings and updates controls
accordingly.
@param Prefs [in] Object that provides info used to update controls.
}
begin
(fWarnings as IAssignable).Assign(Prefs.Warnings);
chkSwitchOff.Checked := fWarnings.SwitchOff;
PopulateLV;
end;
procedure TCodeGenPrefsFrame.actPreviewExecute(Sender: TObject);
{Displays compiler directives used to switch off listed warnings in a dialog
box.
@param Sender [in] Not used.
}
resourcestring
sCaption = 'Compiler Directives Preview';
begin
TPreviewDlg.Execute(Self.Owner, fWarnings.Render, sCaption);
end;
procedure TCodeGenPrefsFrame.actPreviewUpdate(Sender: TObject);
{Updates state of action that displays preview of compiler directives. Enabled
only if switching off of warnings is allowed and there are warnings in list
view.
@param Sender [in] Not used.
}
begin
actPreview.Enabled := (lvWarnings.Items.Count > 0) and chkSwitchOff.Checked;
end;
procedure TCodeGenPrefsFrame.actUpdateExecute(Sender: TObject);
{Updates selected warning from data entered in edit controls.
@param Sender [in] Not used.
}
var
OldSymbol: string; // symbol associated with selected warning
NewSymbol: string; // new symbol from edit control
NewCompilerVer: Single; // new compiler version from edit control
SelItem: TListItem; // reference to selected item in list view
begin
// Update selected warning with new values: update display and warning list
GetSymbol(NewSymbol);
GetCompilerVersion(NewCompilerVer);
SelItem := lvWarnings.Selected;
OldSymbol := Trim(SelItem.Caption);
SelItem.Caption := NewSymbol;
SelItem.SubItems[0] := FormatCompilerVer(NewCompilerVer);
// we update warnings by deleting old one and adding updated version
fWarnings.Delete(OldSymbol);
fWarnings.Add(TWarning.Create(NewSymbol, NewCompilerVer));
// Ensure updated item is still selected in list view and is visible
lvWarnings.Selected := SelItem;
SelItem.MakeVisible(False);
end;
procedure TCodeGenPrefsFrame.actUpdateUpdate(Sender: TObject);
{Updates state of action that updates a warning. Action enabled only if data
in edit controls is valid.
@param Sender [in] Not used.
}
var
CanUpdate: Boolean; // flag that indicates if a warning can be updated
// ---------------------------------------------------------------------------
function SymbolIsDuplicate: Boolean;
{Checks if symbol entered in edit control is a duplicate of an existing
symbol in the list view, other than that of the selected warning.
@return True if duplicate, False if not.
}
var
Symbol: string; // symbol entered in edit control
LIIdx: Integer; // index of list item with Symbol as caption
begin
GetSymbol(Symbol);
LIIdx := IndexOfSymbolInLV(Symbol);
Result := (LIIdx >= 0) and (LIIdx <> lvWarnings.Selected.Index);
end;
// ---------------------------------------------------------------------------
begin
CanUpdate := True;
if lvWarnings.Selected = nil then
CanUpdate := False
else if not IsValidSymbol then
CanUpdate := False
else if SymbolIsDuplicate then
CanUpdate := False
else if not IsValidCompilerVersion then
CanUpdate := False;
actUpdate.Enabled := CanUpdate;
end;
procedure TCodeGenPrefsFrame.AddWarningToLV(const Warning: TWarning);
{Adds details of a warning to list view.
@param Warning [in] Warning to be added.
}
var
LI: TListItem; // new list item for warning
begin
LI := lvWarnings.Items.Add;
LI.Caption := Warning.Symbol;
LI.SubItems.Add(FormatCompilerVer(Warning.MinCompiler));
end;
procedure TCodeGenPrefsFrame.ArrangeControls;
{Arranges controls on frame. Called after frame has been sized.
}
begin
btnPreview.Left := Width - btnPreview.Width;
lvWarnings.Width := Width;
TCtrlArranger.AlignVCentres(0, [chkSwitchOff, btnPreview]);
lvWarnings.Top := TCtrlArranger.BottomOf([chkSwitchOff, btnPreview], 8);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(lvWarnings, 8),
[lblSymbol, edSymbol]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([lblSymbol, edSymbol], 8),
[lblMinCompiler, edMinCompiler, btnPredefined]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([lblMinCompiler, edMinCompiler, btnPredefined], 8),
[btnAdd, btnDelete, btnUpdate]
);
end;
procedure TCodeGenPrefsFrame.btnPredefinedClick(Sender: TObject);
{Click event handler for pre-defined compiler versions button. Drops down
associated popup menu.
@param Sender [in] Not used.
}
var
PopupPos: TPoint; // place where menu pops up
begin
PopupPos := ClientToScreen(
Point(btnPredefined.Left, btnPredefined.Top + btnPredefined.Height)
);
mnuPreDefCompilers.Popup(PopupPos.X, PopupPos.Y);
end;
procedure TCodeGenPrefsFrame.chkSwitchOffClick(Sender: TObject);
{Click event handler for "switch off warnings" check box. Updated warnings
object per state of check box.
@param Sender [in] Not used.
}
begin
fWarnings.SwitchOff := chkSwitchOff.Checked;
end;
constructor TCodeGenPrefsFrame.Create(AOwner: TComponent);
{Constructor. Sets up frame and populates controls.
@param AOwner [in] Component that owns frame.
}
begin
inherited;
HelpKeyword := 'CodeGenPrefs';
fWarnings := TWarnings.Create;
PopulatePreDefCompilerMenu;
end;
procedure TCodeGenPrefsFrame.Deactivate(const Prefs: IPreferences);
{Called when page is deactivated. Updates warnings and preferences objects
from information entered in tab.
@param Prefs [in] Object used to store information.
}
begin
fWarnings.SwitchOff := chkSwitchOff.Checked;
Prefs.Warnings := fWarnings;
end;
function TCodeGenPrefsFrame.DisplayName: string;
{Provides caption that is displayed in the tab sheet that contains this frame
when displayed in the preference dialog box.
@return Required display name.
}
resourcestring
sDisplayName = 'Code Generation'; // display name
begin
Result := sDisplayName;
end;
procedure TCodeGenPrefsFrame.edMinCompilerKeyPress(Sender: TObject;
var Key: Char);
{Called whenever a key is pressed in a minimum compiler edit control. We
prevent all but valid number keys, a single decimal point and backspace.
@param Sender [in] Not used.
@param Key [in/out] Key that was pressed. Set to #0 to inhibit if not valid.
}
begin
if not IsValidDecimalNumberKey((Sender as TEdit).Text, Key) then
KeyErrorBeep;
end;
function TCodeGenPrefsFrame.FormatCompilerVer(const Ver: Single): string;
{Formats compiler version number as a string.
@param Ver [in] Version number to be formatted.
@return Formatted string.
}
begin
Result := Format('%.2f', [Ver]);
end;
function TCodeGenPrefsFrame.GetCompilerVersion(out Ver: Single): Boolean;
{Gets a compiler version number from edit control.
@param Ver [out] Required version number. Undefined if not valid.
@return True if version number is valid, False if not.
}
var
ExtVer: Extended; // version number as Extended float
begin
Result := TryStrToFloat(Trim(edMinCompiler.Text), ExtVer);
if Result then
Ver := ExtVer;
end;
function TCodeGenPrefsFrame.GetSymbol(out Symbol: string): Boolean;
{Gets warning symbol from edit control.
@param Symbol [out] Required symbol, trimmed of white space. Undefined if
not valid.
@return True if symbol is non-empty string, False if not.
}
begin
Symbol := Trim(edSymbol.Text);
Result := Symbol <> '';
end;
class function TCodeGenPrefsFrame.Index: Byte;
{Provides an index number that determines the order in which the tabs
containing frames are displayed in the preferences dialog box.
@return Required index number.
}
begin
Result := 15;
end;
function TCodeGenPrefsFrame.IndexOfSymbolInLV(const Symbol: string): Integer;
{Gets the index of the list item containing a warning symbol in the list view.
@param Symbol [in] Symbol to be found.
@return Index of required list view item or -1 if not found.
}
var
LI: TListItem; // each list item in list view
begin
for LI in lvWarnings.Items do
begin
if AnsiSameText(Symbol, LI.Caption) then
Exit(LI.Index);
end;
Result := -1;
end;
function TCodeGenPrefsFrame.IsValidCompilerVersion: Boolean;
{Checks if compiler version entered in edit control is valid.
@return True if valid, False if not.
}
var
Ver: Single; // compiler version from edit control
begin
Result := GetCompilerVersion(Ver);
if Result then
Result := Ver >= TWarning.MinSupportedCompiler;
end;
function TCodeGenPrefsFrame.IsValidSymbol: Boolean;
{Checks if symbol entered in edit control is valid.
@return True if valid, False if not.
}
var
Symbol: string; // stores symbol: unused
begin
Result := GetSymbol(Symbol);
end;
procedure TCodeGenPrefsFrame.lvWarningsClick(Sender: TObject);
{Click event handler for warnings list view. Copies details of warning
associated with selected (clicked) list item to edit controls.
@param Sender [in] Not used.
}
begin
if not Assigned(lvWarnings.Selected) then
Exit;
edSymbol.Text := lvWarnings.Selected.Caption;
edMinCompiler.Text := lvWarnings.Selected.SubItems[0];
end;
procedure TCodeGenPrefsFrame.PopulateLV;
{Populates list view with details of warnings.
}
var
W: TWarning; // references each warning
begin
lvWarnings.Items.BeginUpdate;
try
lvWarnings.Clear;
for W in fWarnings do
AddWarningToLV(W);
finally
lvWarnings.Items.EndUpdate;
end;
end;
procedure TCodeGenPrefsFrame.PopulatePreDefCompilerMenu;
{Populates pre-defined compiler version pop-up menu with menu items.
}
// ---------------------------------------------------------------------------
procedure AddMenuItem(const Compiler: string; const Ver: Single);
{Adds a menu item to the pop-up menu.
@param Compiler [in] Name of compiler (for menu item caption).
@param Ver [in] Version of named compiler.
}
begin
mnuPreDefCompilers.Items.Add(
TPreDefCompilerMenuItem.Create(
mnuPreDefCompilers, Compiler, Ver, PreDefCompilerMenuClick
)
);
end;
// ---------------------------------------------------------------------------
begin
AddMenuItem('Delphi 6', 14.0);
AddMenuItem('Delphi 7', 15.0);
AddMenuItem('Delphi 2005', 17.0);
AddMenuItem('Delphi 2006', 18.0);
AddMenuItem('Delphi 2007 (Win 32)', 18.5);
AddMenuItem('Delphi 2009', 20.0);
AddMenuItem('Delphi 2010', 21.0);
AddMenuItem('Delphi XE', 22.0);
AddMenuItem('Delphi XE2', 23.0);
AddMenuItem('Delphi XE3', 24.0);
AddMenuItem('Delphi XE4', 25.0);
end;
procedure TCodeGenPrefsFrame.PreDefCompilerMenuClick(Sender: TObject);
{Handler for click events on any pre-defined compiler menu item. Stores
required compiler version in associated edit control.
@param Sender [in] Reference to clicked menu item.
}
begin
edMinCompiler.Text := FormatCompilerVer(
(Sender as TPreDefCompilerMenuItem).CompilerVer
);
end;
{ TPreDefCompilerMenuItem }
constructor TPreDefCompilerMenuItem.Create(AOwner: TComponent;
const ACaption: string; const ACompilerVer: Single;
const AClickHandler: TNotifyEvent);
{Constructor. Used to fully create a menu item with all required properties
and event handlers.
@param AOwner [in] Component that owns menu item.
@param ACaption [in] Name of compiler displayed in menu item's caption.
@param ACompilerVer [in] Version number of compiler named in ACaption.
@param AClickHandler [in] Menu item's OnClick event handler.
}
begin
inherited Create(AOwner);
Caption := ACaption;
CompilerVer := ACompilerVer;
OnClick := AClickHandler;
end;
initialization
// Register frame with preferences dialog box
TPreferencesDlg.RegisterPage(TCodeGenPrefsFrame);
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.