Menu

[r181]: / trunk / Src / FmUserDBEditDlg.pas  Maximize  Restore  History

Download this file

1417 lines (1336 with data), 49.1 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
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
{
* FmUserDBEditDlg.pas
*
* Implements a dialog box that enables the user to create or edit a user-
* defined routine.
*
* $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 FmUserDBEditDlg.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2008-2009 Peter
* Johnson. All Rights Reserved.
*
* Contributors:
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmUserDBEditDlg;
interface
uses
// Delphi
SysUtils, Classes, ActnList, ImgList, Controls, Buttons, StdCtrls, Forms,
CheckLst, ComCtrls, ExtCtrls, Windows,
// Project
FmGenericOKDlg, FrBrowserBase, FrFixedHTMLDlg, FrHTMLDlg,
IntfCompilers, UActiveText, UCompileMgr, UCSSBuilder, USnippets,
USnippetsChkListMgr;
type
{
TUserDBEditDlg:
Dialog box class that enables the user to create or edit a user-defined
snippet.
}
TUserDBEditDlg = class(TGenericOKDlg)
alMain: TActionList;
actAddUnit: TAction;
actCompile: TAction;
actSetAllQuery: TAction;
actSetAllSuccess: TAction;
btnAddUnit: TButton;
btnCompile: TButton;
btnSetAllQuery: TBitBtn;
btnSetAllSuccess: TBitBtn;
cbCategories: TComboBox;
clbDepends: TCheckListBox;
clbUnits: TCheckListBox;
clbXRefs: TCheckListBox;
edDescription: TEdit;
edExtra: TMemo;
edName: TEdit;
edSourceCode: TMemo;
edUnit: TEdit;
frmExtraInstructions: TFixedHTMLDlgFrame;
ilLEDs: TImageList;
lbCompilers: TListBox;
lbCompRes: TListBox;
lblCategories: TLabel;
lblCompilers: TLabel;
lblCompileShortcuts: TLabel;
lblCompRes: TLabel;
lblDepends: TLabel;
lblDescription: TLabel;
lblExtra: TLabel;
lblName: TLabel;
lblSourceCode: TLabel;
lblSnippetKindHelp: TLabel;
lblUnits: TLabel;
lblXRefs: TLabel;
pcMain: TPageControl;
tsCode: TTabSheet;
tsComments: TTabSheet;
tsCompileResults: TTabSheet;
tsReferences: TTabSheet;
actViewErrors: TAction;
pnlViewCompErrs: TPanel;
lblViewCompErrsKey: TLabel;
lblViewCompErrs: TLabel;
cbKind: TComboBox;
lblKind: TLabel;
btnDependencies: TButton;
actDependencies: TAction;
btnViewExtra: TButton;
actViewExtra: TAction;
procedure actAddUnitExecute(Sender: TObject);
procedure actAddUnitUpdate(Sender: TObject);
procedure actCompileExecute(Sender: TObject);
procedure actCompileUpdate(Sender: TObject);
procedure actDependenciesExecute(Sender: TObject);
procedure actSetAllQueryExecute(Sender: TObject);
procedure actSetAllSuccessExecute(Sender: TObject);
procedure actViewErrorsExecute(Sender: TObject);
procedure actViewErrorsUpdate(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure cbKindChange(Sender: TObject);
procedure CLBRoutineRefsDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure lbCompilersClick(Sender: TObject);
procedure lbCompilersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure lbCompResClick(Sender: TObject);
procedure lbCompResDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure lblSnippetKindHelpClick(Sender: TObject);
procedure lblViewCompErrsClick(Sender: TObject);
procedure pcMainChange(Sender: TObject);
procedure actViewExtraExecute(Sender: TObject);
strict private
fSnippet: TRoutine; // Snippet being edited (nil for new snippet)
fCatNames: TStringList; // List of names of available categories
fOrigName: string; // Original name of snippet ('' for new snippet)
fEditData: TRoutineEditData;// Record storing a snippet's editable data
fCompileMgr: TCompileMgr; // Manages compilations and display of results
fDependsCLBMgr: TSnippetsChkListMgr;// Manages dependencies check list box
fXRefsCLBMgr: TSnippetsChkListMgr; // Manages x-refs check list box
procedure UpdateTabSheetCSS(Sender: TObject; const CSSBuilder: TCSSBuilder);
{Updates CSS used for HTML displayed in frames on tab sheets.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to update CSS.
}
procedure PopulateControls;
{Populates controls with dynamic data.
}
procedure InitControls;
{Initialises controls to default values.
}
procedure FocusCtrl(const Ctrl: TWinControl);
{Displays and focusses a control, selecting its parent tab sheet if
necessary.
@param Ctrl [in] Control to be focussed.
}
procedure HandleException(const E: Exception);
{Handles trapped exceptions. EDataEntry exceptions are caught, an error
message is displayed and the control causing the exception is focussed.
Other exceptions are re-raised.
@param E [in] Exception to be handled.
@except Exceptions re-raised if not EDataEntry.
}
function BuildExtraActiveText: IActiveText;
{Creates an active text object from the REML text entered in the extra
information memo control.
@return Required active text object.
}
procedure SetAllCompilerResults(const CompRes: TCompileResult);
{Sets all compiler results to same value.
@param CompRes [in] Required compiler result.
}
procedure ValidateData;
{Checks all user-entered data in all tabs of the form.
@except EDataEntry raised if data is not valid.
}
function UpdateData: TRoutineEditData;
{Updates snippet's data from user entries. Assumes data has been
validated.
@return Record containing snippet's data.
}
function SelectedSnippetKind(out Kind: TSnippetKind): Boolean;
{Gets snippet kind selected in drop-down list.
@param Kind [out] Set to selected snippet kind. Not defined if False
returned.
@return True if a snippeet kind has been selected, False if not.
}
procedure UpdateReferences;
{Updates dependencies and cross-references check lists for snippet being
edited, depending on kind.
}
function CreateTempSnippet: TRoutine;
{Creates a temporary snippet from data entered in dialog box.
@return Required snippet instance.
@except EDataEntry raised if any of entered data is invalid.
}
procedure DisplayCompileResults(const Compilers: ICompilers);
{Displays results of a test compilation. Used as callback method for
compile manager.
@param Compilers [in] Object containing compilation results.
}
procedure Error(const Msg: string; const Ctrl: TWinControl); overload;
{Raises EDataEntry exception with a specified message and control where
error occured.
@param Msg [in] Exception message.
@param Ctrl [in] Control to which exception relates.
@except EDataEntry always raised.
}
procedure Error(const FmtStr: string; const Args: array of const;
const Ctrl: TWinControl); overload;
{Raises EDataEntry exception with a message built from a format string and
parameters, until with a reference to the control where the error occured.
@param FmtStr [in] Message's format string.
@param Args [in] Array of data displayed in format string.
@param Ctrl [in] Control to which exception relates.
@except EDataEntry always raised.
}
procedure CheckExtra;
{Checks the REML text entered in the extra information memo control.
@except EDataEntry on error.
}
strict protected
procedure ArrangeForm; override;
{Arranges controls on form and sizes it.
}
procedure ConfigForm; override;
{Configures form's controls. Sets font and colors of "link" labels. Also
sets item height of owner draw check list boxes.
}
procedure InitForm; override;
{Performs initialisation of form fields and controls.
}
public
class function AddNewRoutine(AOwner: TComponent): Boolean;
{Displays dialog box to enable user to enter a new snippet.
@param AOwner [in] Control that owns the dialog box, over which the
dialog is aligned. May be nil.
@return True if user OKs, False if cancels.
}
class function EditRoutine(AOwner: TComponent;
const Routine: TRoutine): Boolean;
{Displays dialog box to enable user to edit a snippet.
@param AOwner [in] Control that owns the dialog box, over which the
dialog is aligned. May be nil.
@param Routine [in] Reference to snippet to be edited.
@return True if user OKs, False if cancels.
}
end;
implementation
uses
// Delphi
StrUtils, Graphics, Menus,
// Project
FmDependenciesDlg, FmViewExtraDlg, IntfCommon, UColours, UConsts, UCSSUtils,
UCtrlArranger, UExceptions, UFontHelper, UGraphicUtils, UHTMLUtils, URoutineExtraHelper,
USnippetKindInfo, USnippetValidator, UIStringList, UMessageBox, USnippetIDs,
UStructs, UThemesEx, UUtils;
type
{
TCompilerInfo:
Helper class that records information about a snippet's compilation results.
}
TCompilerInfo = class(TObject)
strict private
fCompilerID: TCompilerID; // Value of Compiler property
fCompileResult: TCompileResult; // Value of CompileResult property
public
constructor Create(const CompilerID: TCompilerID;
const CompileResult: TCompileResult);
{Class constructor. Sets up and initialises object.
@param CompilerID [in] Id of compiler that result applies to.
@param CompileResult [in] Compiler result for compiler.
}
property CompilerID: TCompilerID read fCompilerID;
{Id of compiler to which CompileResult applies}
property CompileResult: TCompileResult
read fCompileResult write fCompileResult;
{Result of compilation with associated compiler}
end;
{$R *.dfm}
{ TUserDBEditDlg }
procedure TUserDBEditDlg.actAddUnitExecute(Sender: TObject);
{Adds a unit to the list of required units and selects it.
@param Sender [in] Not used.
}
var
UnitName: string; // name of new unit from edit control
InsertIdx: Integer; // index at which unit name is inserted in list box
resourcestring
// Error messages
sBadUnitName = 'Unit name is not a valid Pascal identifier';
sUnitNameExists = 'Unit name already in list';
begin
UnitName := Trim(edUnit.Text);
Assert(UnitName <> '',
ClassName + '.actAddUnitExecute: UnitName is empty string');
if not IsValidIdent(UnitName) then
raise ECodeSnip.Create(sBadUnitName);
if clbUnits.Items.IndexOf(UnitName) >= 0 then
raise ECodeSnip.Create(sUnitNameExists);
InsertIdx := clbUnits.Items.Add(Trim(UnitName));
clbUnits.Checked[InsertIdx] := True;
edUnit.Text := '';
end;
procedure TUserDBEditDlg.actAddUnitUpdate(Sender: TObject);
{Updates Add Unit action according to whether any unit name is entered in
associated edit control.
@param Sender [in] Action triggering this event.
}
begin
(Sender as TAction).Enabled := edUnit.Text <> '';
end;
procedure TUserDBEditDlg.actCompileExecute(Sender: TObject);
{Test compiles edited snippet and updates compilers from test result.
@param Sender [in] Not used.
}
var
TempSnippet: TRoutine; // temp snippet object for compilation
begin
// Hide view compile errors link
pnlViewCompErrs.Hide;
// Disable dialog box and all its controls and actions
Enabled := False;
try
try
TempSnippet := CreateTempSnippet;
except
on E: Exception do
begin
Enabled := True;
HandleException(E);
Exit;
end;
end;
// Test compile snippet
try
fCompileMgr.Compile(tsCompileResults, TempSnippet, DisplayCompileResults);
finally
FreeAndNil(TempSnippet);
end;
finally
// Re-enable dialog and controls
Enabled := True;
end;
end;
procedure TUserDBEditDlg.actCompileUpdate(Sender: TObject);
{Updates Test Compile action according to whether any compilers are available
and if a snippet kind is selected that is not freeform.
@param Sender [in] Action triggering this event.
}
var
Kind: TSnippetKind; // selected snippet kind
begin
(Sender as TAction).Enabled := fCompileMgr.HaveCompilers
and SelectedSnippetKind(Kind) and (Kind <> skFreeform);
end;
procedure TUserDBEditDlg.actDependenciesExecute(Sender: TObject);
{Displays dependencies for currently edited snippet, per entries in
"Dependencies" check list box.
@param Sender [in] Not used.
}
var
DependsList: TRoutineList;
begin
DependsList := TRoutineList.Create;
try
fDependsCLBMgr.GetCheckedSnippets(DependsList);
TDependenciesDlg.Execute(
Self, TSnippetID.Create(Trim(edName.Text), True), DependsList
);
finally
FreeAndNil(DependsList);
end;
end;
procedure TUserDBEditDlg.actSetAllQueryExecute(Sender: TObject);
{Sets all compiler results to "query".
@param Sender [in] Not used.
}
begin
SetAllCompilerResults(crQuery);
end;
procedure TUserDBEditDlg.actSetAllSuccessExecute(Sender: TObject);
{Sets all compiler results to "success".
@param Sender [in] Not used.
}
begin
SetAllCompilerResults(crSuccess);
end;
procedure TUserDBEditDlg.actViewErrorsExecute(Sender: TObject);
{Displays compiler errors.
@param Sender [in] Not used.
}
begin
fCompileMgr.ShowErrors;
end;
procedure TUserDBEditDlg.actViewErrorsUpdate(Sender: TObject);
{Disables view compiler errors action if panel containing link is not
displayed.
@param Sender [in] Reference to action to be updated.
}
begin
(Sender as TAction).Enabled := pnlViewCompErrs.Visible;
end;
procedure TUserDBEditDlg.actViewExtraExecute(Sender: TObject);
{Validates REML entered in the extra information memo control then displays it
as it will appear in the main form.
@param Sender [in] Not used.
}
begin
CheckExtra;
TViewExtraDlg.Execute(Self, BuildExtraActiveText);
end;
class function TUserDBEditDlg.AddNewRoutine(AOwner: TComponent): Boolean;
{Displays dialog box to enable user to enter a new snippet.
@param AOwner [in] Control that owns the dialog box, over which the dialog
is aligned. May be nil.
@return True if user OKs, False if cancels.
}
resourcestring
sCaption = 'Add a Snippet'; // dialog box caption
begin
with Create(AOwner) do
try
Caption := sCaption;
fSnippet := nil;
Result := ShowModal = mrOK;
finally
Free;
end;
end;
procedure TUserDBEditDlg.ArrangeForm;
{Arranges controls on form and sizes it.
}
begin
// tsCode
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(edSourceCode, 8),
[cbKind, lblKind, lblSnippetKindHelp]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([cbKind, lblKind, lblSnippetKindHelp], 8),
[edDescription, lblDescription]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([edDescription, lblDescription], 8),
[edName, lblName]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([edName, lblName], 8),
[cbCategories, lblCategories]
);
// tsReferences
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(clbXRefs, 6), [btnDependencies, edUnit, btnAddUnit]
);
// tsComments
frmExtraInstructions.Top := TCtrlArranger.BottomOf(edExtra, 4);
btnViewExtra.Top := TCtrlArranger.BottomOf(frmExtraInstructions);
// tsCompileResults
lblViewCompErrsKey.Top := TCtrlArranger.BottomOf(lblViewCompErrs);
// set body panel size to accommodate controls
pnlBody.ClientHeight := TCtrlArranger.MaxContainerHeight(
[tsCode, tsComments, tsCompileResults, tsReferences]
) + pnlBody.ClientHeight - tsCode.Height + 8;
inherited;
end;
procedure TUserDBEditDlg.btnOKClick(Sender: TObject);
{OnClick event handler for OK button. Validates entries and updates / adds
snippet if all is well.
@param Sender [in] Not used.
}
var
RoutineName: string; // name of snippet being edited / added
begin
inherited;
try
// Validate and record entered data
ValidateData;
fEditData.Assign(UpdateData);
RoutineName := Trim(edName.Text);
// Add or update snippet
if Assigned(fSnippet) then
fSnippet := (Snippets as ISnippetsEdit).UpdateRoutine(
fSnippet, fEditData, RoutineName
)
else
begin
fSnippet := (Snippets as ISnippetsEdit).AddRoutine(
RoutineName, fEditData
)
end;
except
on E: Exception do
HandleException(E);
end;
end;
function TUserDBEditDlg.BuildExtraActiveText: IActiveText;
{Creates an active text object from the REML text entered in the extra
information memo control.
@return Required active text object.
}
begin
Result := TRoutineExtraHelper.BuildActiveText(
Trim(CompressWhiteSpace(ReplaceStr(edExtra.Text, EOL, ' ')))
);
end;
procedure TUserDBEditDlg.cbKindChange(Sender: TObject);
{Handles change events on snippet kind drop down list. Updates list of
references depending on new kind.
@param Sender [in] Not used.
}
begin
UpdateReferences;
end;
procedure TUserDBEditDlg.CheckExtra;
{Checks the REML text entered in the extra information memo control.
@except EDataEntry on error.
}
// ---------------------------------------------------------------------------
procedure ValidateURL(URL: string);
{Validates a-link href URLs.
@param URL [in] URL to validate.
@except EDataEntry raised if validation fails.
}
const
cHTTPProtocol = 'http://'; // http protocol prefix
cFileProtocol = 'file://'; // file protocal prefix
resourcestring
// validation error messages
sLinkErr = 'Hyperlink URL "%s" in extra information must use either the '
+ '"http://" or "file://" protocols';
sURLLengthErr
= 'Hyperlink URL "%s" in extra information markup is not valid';
begin
URL := URLDecode(URL, False);
if AnsiStartsText(cHTTPProtocol, URL) then
begin
if Length(URL) < Length(cHTTPProtocol) + 6 then
Error(sURLLengthErr, [URL], edExtra);
end
else if AnsiStartsText(cFileProtocol, URL) then
begin
if Length(URL) < Length(cFileProtocol) + 4 then
Error(sURLLengthErr, [URL], edExtra);
end
else
Error(sLinkErr, [URL], edExtra);
end;
// ---------------------------------------------------------------------------
var
Elem: IActiveTextElem; // each element in active text
ActionElem: IActiveTextActionElem; // references action element
ActiveText: IActiveText; // active text created from text
resourcestring
// parse error message
sActiveTextErr = 'Error parsing extra information markup:' + EOL2 + '%s';
begin
try
// Try to create active text: this parses the text and raises exception
// if there is an error in the REML markup
ActiveText := BuildExtraActiveText;
except
// Convert active text parser to data exception
on E: EActiveTextParserError do
Error(sActiveTextErr, [E.Message], edExtra);
else
raise;
end;
// Scan all active text looking of hyperlinks: check that URL has a
// supported protocol and some url text after it
for Elem in ActiveText do
begin
if Supports(Elem, IActiveTextActionElem, ActionElem)
and (ActionElem.Kind = ekLink) then
ValidateURL(ActionElem.Param);
end;
end;
procedure TUserDBEditDlg.CLBRoutineRefsDrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for check list boxes that display snippet names.
Draws user defined snippet names in a special colour.
@param Control [in] Check list box that triggered the event.
@param Index [in] Index if item being drawn.
@param Rect [in] Rectangle in check list box's canvas where item is to be
drawn.
@param State [in] State of list item.
}
var
CLB: TCheckListBox; // reference to check list box
Canvas: TCanvas; // check list box's canvas
begin
inherited;
CLB := Control as TCheckListBox;
Canvas := CLB.Canvas;
if not (odSelected in State)
and (CLB.Items.Objects[Index] as TRoutine).UserDefined then
Canvas.Font.Color := clUserRoutine;
Canvas.TextRect(
Rect,
Rect.Left + 2,
(Rect.Top + Rect.Bottom - Canvas.TextHeight(CLB.Items[Index])) div 2,
CLB.Items[Index]
);
end;
procedure TUserDBEditDlg.ConfigForm;
{Configures form's controls. Sets font and colors of "link" labels. Also sets
item height of owner draw check list boxes.
}
begin
inherited;
// Set colour and actions of link labels
lblSnippetKindHelp.Font.Color := clHelpLinkText;
TFontHelper.SetDefaultBaseFont(lblSnippetKindHelp.Font, False);
lblViewCompErrs.Font.Color := clLinkText;
TFontHelper.SetDefaultBaseFont(lblViewCompErrs.Font, False);
lblViewCompErrs.Caption := actViewErrors.Caption;
lblViewCompErrsKey.Caption :=
'(' + ShortcutToText(actViewErrors.ShortCut) + ')';
// Set correct item height for owner drawn check list boxes
clbDepends.ItemHeight := StringExtent('Xy', clbDepends.Font).cy;
clbXRefs.ItemHeight := StringExtent('Xy', clbXRefs.Font).cy;
end;
function TUserDBEditDlg.CreateTempSnippet: TRoutine;
{Creates a temporary snippet from data entered in dialog box.
@return Required snippet instance.
@except EDataEntry raised if any of entered data is invalid.
}
var
EditData: TRoutineEditData; // stores snippet's properties and references
begin
ValidateData;
// Create snippet object from entered data
EditData.Init;
EditData.Assign(UpdateData);
Result := (Snippets as ISnippetsEdit).CreateTempRoutine(
Trim(edName.Text), EditData
);
end;
procedure TUserDBEditDlg.DisplayCompileResults(const Compilers: ICompilers);
{Displays results of a test compilation. Used as callback method for compile
manager.
@param Compilers [in] Object containing compilation results.
}
var
CompilerIdx: Integer; // loops through each compiler
CompilerInfo: TCompilerInfo; // provides information about a compiler
begin
// Update compiler results from test compilation
for CompilerIdx := 0 to Pred(lbCompilers.Count) do
begin
CompilerInfo := lbCompilers.Items.Objects[CompilerIdx] as TCompilerInfo;
CompilerInfo.CompileResult :=
Compilers[CompilerInfo.CompilerID].GetLastCompileResult;
end;
// Redisplay compilers list to reflect change
lbCompilers.Invalidate;
// Update visibility of show errors link
pnlViewCompErrs.Visible := fCompileMgr.HaveErrors;
end;
class function TUserDBEditDlg.EditRoutine(AOwner: TComponent;
const Routine: TRoutine): Boolean;
{Displays dialog box to enable user to edit a snippet.
@param AOwner [in] Control that owns the dialog box, over which the dialog
is aligned. May be nil.
@param Routine [in] Reference to snippet to be edited.
@return True if user OKs, False if cancels.
}
resourcestring
sCaption = 'Edit %s'; // dialog box caption
begin
with Create(AOwner) do
try
Caption := Format(sCaption, [Routine.Name]);
fSnippet := Routine;
Result := ShowModal = mrOK;
finally
Free;
end;
end;
procedure TUserDBEditDlg.Error(const Msg: string; const Ctrl: TWinControl);
{Raises EDataEntry exception with a specified message and control where
error occured.
@param Msg [in] Exception message.
@param Ctrl [in] Control to which exception relates.
@except EDataEntry always raised.
}
begin
raise EDataEntry.Create(Msg, Ctrl);
end;
procedure TUserDBEditDlg.Error(const FmtStr: string; const Args: array of const;
const Ctrl: TWinControl);
{Raises EDataEntry exception with a message built from a format string and
parameters, until with a reference to the control where the error occured.
@param FmtStr [in] Message's format string.
@param Args [in] Array of data displayed in format string.
@param Ctrl [in] Control to which exception relates.
@except EDataEntry always raised.
}
begin
raise EDataEntry.CreateFmt(FmtStr, Args, Ctrl);
end;
procedure TUserDBEditDlg.FocusCtrl(const Ctrl: TWinControl);
{Displays and focusses a control, selecting its parent tab sheet if necessary.
@param Ctrl [in] Control to be focussed.
}
var
ParentTab: TTabSheet; // tab sheet on which Ctrl sits
ParentCtrl: TWinControl; // walks up parents of Ctrl
begin
// Find tab sheet that contains edit control
ParentCtrl := Ctrl.Parent;
while Assigned(ParentCtrl) and not (ParentCtrl is TTabSheet) do
ParentCtrl := ParentCtrl.Parent;
if not Assigned(ParentCtrl) then
Exit;
// Display correct tab sheet
ParentTab := ParentCtrl as TTabSheet;
pcMain.ActivePage := ParentTab;
// Focus control
if Ctrl.Enabled then
Ctrl.SetFocus;
end;
procedure TUserDBEditDlg.FormCreate(Sender: TObject);
{Form creation event handler. Creates owned objects.
@param Sender [in] Not used.
}
begin
inherited;
fCatNames := TStringList.Create;
fCompileMgr := TCompileMgr.Create(Self); // auto-freed
fDependsCLBMgr := TSnippetsChkListMgr.Create(clbDepends);
fXRefsCLBMgr := TSnippetsChkListMgr.Create(clbXRefs);
end;
procedure TUserDBEditDlg.FormDestroy(Sender: TObject);
{Form destruction event handler. Frees owned objects.
@param Sender [in] Not used.
}
var
Idx: Integer; // loops through items in compiler list box
begin
inherited;
FreeAndNil(fXRefsCLBMgr);
FreeAndNil(fDependsCLBMgr);
FreeAndNil(fCatNames);
// Free TCompilerInfo objects associated with Compilers list box
for Idx := 0 to Pred(lbCompilers.Count) do
lbCompilers.Items.Objects[Idx].Free;
end;
procedure TUserDBEditDlg.HandleException(const E: Exception);
{Handles trapped exceptions. EDataEntry exceptions are caught, an error
message is displayed and the control causing the exception is focussed. Other
exceptions are re-raised.
@param E [in] Exception to be handled.
@except Exceptions re-raised if not EDataEntry.
}
begin
if E is EDataEntry then
begin
TMessageBox.Error(Self, E.Message);
FocusCtrl((E as EDataEntry).Ctrl);
ModalResult := mrNone;
end
else
raise E;
end;
procedure TUserDBEditDlg.InitControls;
{Initialises controls to default values.
}
// ---------------------------------------------------------------------------
procedure CheckEntry(const Entry: string; const CLB: TCheckListBox);
{Checks an item in a check list box that has specified text.
@param Entry [in] Text of item to be checked.
@param CLB [in] Reference to check list box.
}
var
Idx: Integer; // index of Entry in CLB
begin
Idx := CLB.Items.IndexOf(Entry);
if Idx >= 0 then
CLB.Checked[Idx] := True;
end;
procedure InitUnitCheckListBox;
{Checks all units in units check list box that are referenced by current
snippet. If unit is not in list box it is added.
}
var
AUnit: string; // name of each referenced unit
begin
Assert(Assigned(fSnippet),
ClassName + '.InitControls.InitUnitCheckListBox: fSnippet is nil');
for AUnit in fSnippet.Units do
begin
if clbUnits.Items.IndexOf(AUnit) = -1 then
clbUnits.Items.Add(AUnit);
CheckEntry(AUnit, clbUnits);
end;
end;
procedure SelectKind(const Kind: TSnippetKind);
{Selects a snippet kind in drop down list.
@param Kind [in] Snippet kind to be selected.
}
var
Idx: Integer; // loops through drop down list entries
begin
cbKind.ItemIndex := -1;
for Idx := 0 to Pred(cbKind.Items.Count) do
if TSnippetKind(cbKind.Items.Objects[Idx]) = Kind then
begin
cbKind.ItemIndex := Idx;
Break;
end;
UpdateReferences; // update references list to match kind
end;
procedure InitKindDropDownList;
{Initialises snippet kind drop down list by selecting kind corresponding to
snippet being edited.
}
begin
Assert(Assigned(fSnippet));
SelectKind(fSnippet.Kind);
end;
// ---------------------------------------------------------------------------
begin
if Assigned(fSnippet) then
begin
// We are editing a snippet: initialise controls from snippet's properties
edSourceCode.Text := fSnippet.SourceCode;
edDescription.Text := fSnippet.Description;
edName.Text := fSnippet.Name;
cbCategories.ItemIndex := fCatNames.IndexOf(fSnippet.Category);
edExtra.Text := TRoutineExtraHelper.BuildREMLMarkup(fSnippet.Extra);
InitKindDropDownList;
// check required items in references check list boxes
InitUnitCheckListBox;
fDependsCLBMgr.CheckSnippets(fSnippet.Depends);
fXRefsCLBMgr.CheckSnippets(fSnippet.XRef);
end
else
begin
// We are adding a new snippet: clear all controls
edSourceCode.Clear;
edDescription.Clear;
edName.Clear;
cbCategories.ItemIndex := -1;
edExtra.Clear;
SelectKind(skFreeform); // update references check boxes
end;
// Select first compiler and update compiler result list
lbCompilers.ItemIndex := 0;
lbCompilersClick(lbCompilers);
end;
procedure TUserDBEditDlg.InitForm;
{Performs initialisation of form fields and controls.
}
begin
inherited;
// Get data associated with snippet, or blank / default data if adding a new
// snippet
fEditData := (Snippets as ISnippetsEdit).GetEditableRoutineInfo(fSnippet);
// Record snippet's original name, if any
if Assigned(fSnippet) then
fOrigName := fSnippet.Name
else
fOrigName := '';
// Populate controls with dynamic data
PopulateControls;
// Initialise controls to default values
InitControls;
// Set up CSS builder for extra instructions frame (HTML loading is delayed
// until appropriate tab is displayed)
frmExtraInstructions.OnBuildCSS := UpdateTabSheetCSS;
// Select first tab sheet
pcMain.ActivePageIndex := 0;
end;
procedure TUserDBEditDlg.lbCompilersClick(Sender: TObject);
{OnClick event handler for Compilers list box. Selects item in Compile Result
list box that corresponds to result for the selected compiler.
@param Sender [in] Not used.
}
var
CompInfo: TCompilerInfo; // info about selected compiler
begin
inherited;
CompInfo := lbCompilers.Items.Objects[lbCompilers.ItemIndex] as TCompilerInfo;
lbCompRes.ItemIndex := Ord(CompInfo.CompileResult);
end;
procedure TUserDBEditDlg.lbCompilersDrawItem(Control: TWinControl;
Index: Integer; Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for Compilers list box. Custom draws compiler list
item to show compiler glyph, name of compilers and current compile result as
glyph.
@param Control [in] Reference to list box that triggered event.
@param Index [in] Index of list item being drawn.
@param Rect [in] Rectangle in list box canvas where item being drawn.
@param State [in] State of list item (not used).
}
var
LB: TListBox; // reference to list box
Cvs: TCanvas; // list box's canvas
CompInfo: TCompilerInfo; // info about compile result associated with item
Text: string; // text to be displayed
TextRect: TRect; // rectangle in which to display text
CompGlyph: TBitmap; // gylph associated with compiler
GlyphRect: TRect; // rectangle in which to display compiler glyph
begin
// Get reference to list box, its canvas and associated compiler info
LB := Control as TListBox;
Cvs := LB.Canvas;
CompInfo := LB.Items.Objects[Index] as TCompilerInfo;
// Clear item's rectangle
Cvs.FillRect(Rect);
// Display text
Text := LB.Items[Index];
TextRect := TRectEx.Create(
Rect.Left + 24,
(Rect.Bottom + Rect.Top - Cvs.TextHeight(Text)) div 2,
Rect.Right - 24,
Rect.Bottom
);
if odDisabled in State then
Cvs.Font.Color := clGrayText
else
Cvs.Font.Color := LB.Font.Color;
Cvs.TextRect(TextRect, Text, [tfLeft, tfNoPrefix, tfEndEllipsis, tfTop]);
// Display any compiler glyph
CompGlyph := fCompileMgr.Compilers[CompInfo.CompilerID].GetGlyph;
if Assigned(CompGlyph) then
begin
GlyphRect := TRectEx.CreateBounds(
Rect.Left + 2,
(Rect.Bottom + Rect.Top - CompGlyph.Height) div 2,
CompGlyph.Width,
CompGlyph.Height
);
Cvs.BrushCopy(
GlyphRect,
CompGlyph,
TRectEx.Create(0, 0, CompGlyph.Width, CompGlyph.Height),
clFuchsia
);
end;
// Display compile result "LED": assumes image index = Ord(CompileResult)
ilLEDs.Draw(
Cvs,
Rect.Right - 2 - ilLEDs.Width,
(Rect.Bottom + Rect.Top - ilLEDs.Height) div 2,
Ord(CompInfo.CompileResult)
);
end;
procedure TUserDBEditDlg.lbCompResClick(Sender: TObject);
{OnClick event handler for Compiler Result list box. Updates Compilers list
to reflect chosen result.
@param Sender [in] Not used.
}
var
CompilerIdx: Integer; // index of selected compiler in list
CompResIdx: Integer; // index of selected compile result in list
CompilerInfo: TCompilerInfo; // information about compiler / result
begin
// Get selected compiler and result
CompResIdx := lbCompRes.ItemIndex;
CompilerIdx := lbCompilers.ItemIndex;
if (CompilerIdx = -1) or (CompResIdx = -1) then
Exit;
// Update compiler with new result
CompilerInfo := lbCompilers.Items.Objects[CompilerIdx] as TCompilerInfo;
CompilerInfo.CompileResult :=
TCompileResult(lbCompRes.Items.Objects[CompResIdx]);
// Redisplay compilers list to reflect change
lbCompilers.Invalidate;
end;
procedure TUserDBEditDlg.lbCompResDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
{OnDrawItem event handler for Compiler Result list box. Custom draws compiler
result list item to description of result and its "LED".
@param Control [in] Reference to list box that triggered event.
@param Index [in] Index of list item being drawn.
@param Rect [in] Rectangle in list box canvas where item being drawn.
@param State [in] State of list item (not used).
}
var
LB: TListBox; // reference to list box
Cvs: TCanvas; // list box's canvas
CompRes: TCompileResult; // compile result for this item
Text: string; // text to be displayed
TextRect: TRect; // rectangle in which to draw text
begin
// Get reference to list box, its canvas and associated compile result
LB := Control as TListBox;
Cvs := LB.Canvas;
CompRes := TCompileResult(LB.Items.Objects[Index]);
// Clear item's rectangle
Cvs.FillRect(Rect);
// Draw text
Text := LB.Items[Index];
TextRect := TRectEx.Create(
Rect.Left + 4 + ilLEDs.Width,
(Rect.Bottom + Rect.Top - Cvs.TextHeight(Text)) div 2,
Rect.Right,
Rect.Bottom
);
if odDisabled in State then
Cvs.Font.Color := clGrayText
else
Cvs.Font.Color := LB.Font.Color;
Cvs.TextRect(TextRect, Text, [tfLeft, tfNoPrefix, tfEndEllipsis, tfTop]);
// Draw compiler result: assumes image index = Ord(CompRes)
ilLEDs.Draw(
Cvs,
Rect.Left + 2,
(Rect.Bottom + Rect.Top - ilLEDs.Height) div 2,
Ord(CompRes)
);
end;
procedure TUserDBEditDlg.lblSnippetKindHelpClick(Sender: TObject);
{OnClick event handler for Snippet Kind help link label. Displays help topic
that informs what a Snippet Kind is.
@param Sender [in] Not used.
}
begin
DisplayHelp('SnippetKinds');
end;
procedure TUserDBEditDlg.lblViewCompErrsClick(Sender: TObject);
{OnClick event handler for compiler errors link label. Displays compiler
warnings and errors in a dialog box.
@param Sender [in] Not used.
}
begin
actViewErrors.Execute;
end;
procedure TUserDBEditDlg.pcMainChange(Sender: TObject);
{Handler for OnChange event for page control. Used to load content into
instructions HTML frame for comments tab.
@param Sender [in] Not used.
}
begin
inherited;
if pcMain.ActivePage = tsComments then
begin
// We have to load instructions here, since loading them when comments tab
// is hidden, causes dialog box to freeze and not be displayed. We may only
// load the content when tab is visible. The HTML is loaded each time the
// tab is displayed, but there is no noticable lag as a result.
frmExtraInstructions.Initialise('dlg-userdb-extra.html');
frmExtraInstructions.Height := frmExtraInstructions.DocHeight;
end;
// We always hide "view errors" link whenever page changes since snippet
// properties may have changed since page was last accessed
pnlViewCompErrs.Hide;
end;
procedure TUserDBEditDlg.PopulateControls;
{Populates controls with dynamic data.
}
var
Cat: TCategory; // loops through all categories
Compiler: ICompiler; // loops thru all compilers
CompRes: TCompileResult; // loops thru all compile results
Kind: TSnippetKind; // loops thru all supported snippet kinds
resourcestring
// Text for list items in Compiler Result list box
sSuccess = 'Success';
sWarning = 'Warning';
sError = 'Error';
sQuery = 'Unknown';
const
// Map of compiler results onto descriptions
cCompRes: array[TCompileResult] of string = (
sSuccess, sWarning, sError, sQuery
);
begin
// Display all kinds in drop down list
for Kind := Low(TSnippetKind) to High(TSnippetKind) do
cbKind.Items.AddObject(
TSnippetKindInfoList.Instance[Kind].Description,
TObject(Kind)
);
// Display all available categories in drop down list
for Cat in Snippets.Categories do
begin
cbCategories.Items.Add(Cat.Description);
fCatNames.Add(Cat.Category);
end;
// Display all compilers
for Compiler in fCompileMgr.Compilers do
lbCompilers.Items.AddObject(
Compiler.GetName,
TCompilerInfo.Create(
Compiler.GetID, fEditData.Props.CompilerResults[Compiler.GetID]
)
);
// Display all compiler results
for CompRes := Low(TCompileResult) to High(TCompileResult) do
lbCompRes.Items.AddObject(cCompRes[CompRes], TObject(CompRes));
end;
function TUserDBEditDlg.SelectedSnippetKind(out Kind: TSnippetKind): Boolean;
{Gets snippet kind selected in drop-down list.
@param Kind [out] Set to selected snippet kind. Not defined if False
returned.
@return True if a snippeet kind has been selected, False if not.
}
begin
Result := (cbKind.ItemIndex >= 0);
if Result then
Kind := TSnippetKind(cbKind.Items.Objects[cbKind.ItemIndex]);
end;
procedure TUserDBEditDlg.SetAllCompilerResults(const CompRes: TCompileResult);
{Sets all compiler results to same value.
@param CompRes [in] Required compiler result.
}
var
CompilerIdx: Integer; // loops thru all compilers in list box
CompilerInfo: TCompilerInfo; // information about compiler / result
begin
// Update compile result of all compilers
for CompilerIdx := 0 to Pred(lbCompilers.Count) do
begin
CompilerInfo := lbCompilers.Items.Objects[CompilerIdx] as TCompilerInfo;
CompilerInfo.CompileResult := CompRes
end;
// Redisplay compilers list to reflect change
lbCompilers.Invalidate;
end;
function TUserDBEditDlg.UpdateData: TRoutineEditData;
{Updates snippet's data from user entries. Assumes data has been validated.
@return Record containing snippet's data.
}
// ---------------------------------------------------------------------------
function GetCompileResults: TCompileResults;
{Gets list of compiler results from compilers list box.
@return Array of compiler results.
}
var
Idx: Integer; // loops through all compilers in list box
CompInfo: TCompilerInfo; // information about a compiler
begin
for Idx := 0 to Pred(lbCompilers.Count) do
begin
CompInfo := lbCompilers.Items.Objects[Idx] as TCompilerInfo;
Result[CompInfo.CompilerID] := CompInfo.CompileResult;
end;
end;
procedure CheckedListItemsToStrings(const CLB: TCheckListBox;
const Strings: IStringList);
{Sets a string list to contain the text of all check items in a check list
box.
@param CLB [in] Reference to check list box.
@param Strings [in] String list to receive checked items.
}
var
Idx: Integer; // loops thru all items in check list box
begin
Strings.Clear;
for Idx := 0 to Pred(CLB.Items.Count) do
if CLB.Checked[Idx] then
Strings.Add(CLB.Items[Idx]);
end;
// ---------------------------------------------------------------------------
begin
Result.Init;
with Result do
begin
Props.Cat := fCatNames[cbCategories.ItemIndex];
SelectedSnippetKind(Props.Kind);
Props.Desc := Trim(edDescription.Text);
Props.SourceCode := TrimRight(edSourceCode.Text);
(Props.Extra as IAssignable).Assign(BuildExtraActiveText);
Props.CompilerResults := GetCompileResults;
CheckedListItemsToStrings(clbUnits, Refs.Units);
fDependsCLBMgr.GetCheckedSnippets(Refs.Depends);
fXRefsCLBMgr.GetCheckedSnippets(Refs.XRef);
end;
end;
procedure TUserDBEditDlg.UpdateReferences;
{Updates dependencies and cross-references check lists for snippet being
edited, depending on kind.
}
var
EditSnippetID: TSnippetID; // id of snippet being edited
EditSnippetKind: TSnippetKind; // kind of snippet being edited
Snippet: TRoutine; // each snippet in database
begin
// Save state of dependencies and x-ref check list boxes and clear them
fDependsCLBMgr.Save;
fDependsCLBMgr.Clear;
fXRefsCLBMgr.Save;
fXRefsCLBMgr.Clear;
EditSnippetID := TSnippetID.Create(fOrigName, True);
if SelectedSnippetKind(EditSnippetKind) then
begin
for Snippet in Snippets.Routines do
begin
// We ignore snippet being edited and main database snippets if there is
// a user-defined one with same name
if (Snippet.ID <> EditSnippetID) and
(
Snippet.UserDefined or
not Assigned(Snippets.Routines.Find(Snippet.Name, True))
) then
begin
case EditSnippetKind of
skFreeform, skRoutine:
begin
// For freeform and snippet's depends list can be anything except
// freeform
if Snippet.Kind in [skRoutine, skConstant, skTypeDef] then
fDependsCLBMgr.AddSnippet(Snippet);
end;
skTypeDef, skConstant:
begin
// For typedefs and constants depends list can only be other
// typedefs and consts
if Snippet.Kind in [skConstant, skTypeDef] then
fDependsCLBMgr.AddSnippet(Snippet);
end;
end;
// Anything can be in XRefs list
fXRefsCLBMgr.AddSnippet(Snippet);
end;
end;
end;
// Restore checks to any saved checked item that still exist in new list
fDependsCLBMgr.Restore;
fXRefsCLBMgr.Restore;
end;
procedure TUserDBEditDlg.UpdateTabSheetCSS(Sender: TObject;
const CSSBuilder: TCSSBuilder);
{Updates CSS used for HTML displayed in frames on tab sheets.
@param Sender [in] Not used.
@param CSSBuilder [in] Object used to update CSS.
}
var
DefaultFont: TFont; // font used for dialog box content (not controls)
begin
// Build default font and apply to HTML frame
DefaultFont := TFont.Create;
try
TFontHelper.SetDefaultFont(DefaultFont, True);
with CSSBuilder.Selectors['body'] do
begin
AddProperty(CSSFontProps(DefaultFont));
if ThemeServicesEx.ThemesEnabled then
// For themed windows only, modify background colour to suit tab sheet
// background
AddProperty(CSSBackgroundColorProp(ThemeServicesEx.GetTabBodyColour));
end;
// Add definitions of custom classes used in extra info example frame
// font style of REML tags
with CSSBuilder.AddSelector('.elem') do
begin
AddProperty(CSSColorProp(clREMLTags));
AddProperty(CSSFontFamilyProp('Courier New', cfgMonoSpace));
end;
finally
FreeAndNil(DefaultFont);
end;
end;
procedure TUserDBEditDlg.ValidateData;
{Checks all user-entered data in all tabs of the form.
@except EDataEntry raised if data is not valid.
}
// ---------------------------------------------------------------------------
procedure CheckDependencies;
{Checks a snippet's dependencies for validity.
@except EDataEntry on error.
}
var
EditData: TRoutineEditData; // data describing edited snippet
TempSnippet: TRoutine; // temporary snippet created from user entries
ErrorMessage: string; // receives any error message
resourcestring
// text added to any error messages
sPrompt = 'See the dependencies by clicking the View Dependencies button '
+ 'on the References tab.';
begin
EditData.Init;
EditData.Assign(UpdateData);
TempSnippet := (Snippets as ISnippetsEdit).CreateTempRoutine(
Trim(edName.Text), EditData
);
try
if not TSnippetValidator.HasValidDependsList(
TempSnippet, ErrorMessage
) then
Error(ErrorMessage + EOL2 + sPrompt, clbDepends);
finally
TempSnippet.Free;
end;
end;
// ---------------------------------------------------------------------------
resourcestring
// Error messages
sErrNoDesc = 'A description must be provided';
sErrDescHasClosingBrace = 'Description must not contain a ''}'' character';
sErrNoName = 'A name must be provided';
sErrDupName = '%s is already in the database. Please choose another name';
sErrBadName = '%s is not a valid Pascal identifier';
sErrNoSource = 'Some source code must be provided';
sErrNoCategory = 'A category must be selected';
sErrNoKind = 'A kind must be selected';
var
SnippetName: string; // trimmed snippet name
begin
// Source code must be provided
if Trim(edSourceCode.Text) = '' then
Error(sErrNoSource, edSourceCode);
// Description is required and must not contain closing brace character
if AnsiContainsText(edDescription.Text, '}') then
Error(sErrDescHasClosingBrace, edDescription);
if Trim(edDescription.Text) = '' then
Error(sErrNoDesc, edDescription);
// Unique name required and must be valid Pascal identifier
SnippetName := Trim(edName.Text);
if SnippetName = '' then
Error(sErrNoName, edName);
if (SnippetName <> fOrigName)
and (Snippets.Routines.Find(SnippetName, True) <> nil) then
Error(sErrDupName, [SnippetName], edName);
if not IsValidIdent(SnippetName) then
Error(sErrBadName, [SnippetName], edName);
// Check that a category has been selected
if cbCategories.ItemIndex = -1 then
Error(sErrNoCategory, cbCategories);
// Check that a kind has been selected
if cbKind.ItemIndex = -1 then
Error(sErrNoKind, cbKind);
// Check extra info
CheckExtra;
// Check dependencies
CheckDependencies;
end;
{ TCompilerInfo }
constructor TCompilerInfo.Create(const CompilerID: TCompilerID;
const CompileResult: TCompileResult);
{Class constructor. Sets up and initialises object.
@param CompilerID [in] Id of compiler that result applies to.
@param CompileResult [in] Compiler result for compiler.
}
begin
inherited Create;
fCompilerID := CompilerID;
fCompileResult := CompileResult;
end;
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.