Menu

[r2035]: / trunk / Src / UPreferences.pas  Maximize  Restore  History

Download this file

1024 lines (929 with data), 36.2 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
{
* UPreferences.pas
*
* Implements a singletion object that exposes and persists user preferences.
*
* $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 UPreferences.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2006-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UPreferences;
interface
uses
// Delphi
Graphics,
// Project
Hiliter.UGlobals, UIStringList, UMeasurement, UPrintInfo, USourceFileInfo,
USourceGen, UWarnings;
type
// Possible values for startup state of overview treeview.
TOverviewStartState = (
ossExpanded, // start treeview fully expanded
ossCollapsed // start treeview fully collapsed
);
type
{
IPreferences:
Interface to object that exposes and persists user preferences.
}
IPreferences = interface(IInterface)
['{381B9A92-B528-47E1-AC04-90E1FFFDADA7}']
function GetSourceCommentStyle: TCommentStyle;
{Gets style of commenting used to describe snippets in generated code.
@return Current commenting style.
}
procedure SetSourceCommentStyle(const Value: TCommentStyle);
{Sets style of commenting to be used describe snippets in generated code.
@param Value [in] Required commenting style.
}
property SourceCommentStyle: TCommentStyle
read GetSourceCommentStyle write SetSourceCommentStyle;
{Commenting style used to describe snippets in generated source code}
function GetSourceDefaultFileType: TSourceFileType;
{Gets current default file extension / type used when writing code
snippets to file.
@return Current default file extension.
}
procedure SetSourceDefaultFileType(const Value: TSourceFileType);
{Sets default file extension / type to be used when writing code snippets
to file.
@param Value [in] Required default file extension.
}
property SourceDefaultFileType: TSourceFileType
read GetSourceDefaultFileType write SetSourceDefaultFileType;
{Default file extension / type used when writing code snippets to file}
function GetSourceSyntaxHilited: Boolean;
{Gets current indicator of whether generated source is highlighted by
default.
@return Current value indicating whether source is highlighted.
}
procedure SetSourceSyntaxHilited(const Value: Boolean);
{Sets flag indicating whether generated source is highlighted by default.
@param Value [in] Flag indicating whether source is highlighted.
}
property SourceSyntaxHilited: Boolean
read GetSourceSyntaxHilited write SetSourceSyntaxHilited;
{Indicates whether generated source is highlighted by default}
function GetMeasurementUnits: TMeasurementUnits;
{Gets measurement units used by application.
@return Current measurement units.
}
procedure SetMeasurementUnits(const Value: TMeasurementUnits);
{Sets measurement units to be used by application.
@param Value [in] Required measurement units.
}
property MeasurementUnits: TMeasurementUnits
read GetMeasurementUnits write SetMeasurementUnits;
{Measurement units used by application}
function GetOverviewStartState: TOverviewStartState;
{Gets startup state of overview tree view.
@return Current startup state.
}
procedure SetOverviewStartState(const Value: TOverviewStartState);
{Sets startup state of overview tree view.
@param Value [in] Required startup state.
}
property OverviewStartState: TOverviewStartState
read GetOverviewStartState write SetOverviewStartState;
{Startup state of overview treeview}
function GetShowEmptySections: Boolean;
{Gets flag that indicates whether empty sections are displayed in overview
pane.
@returns Flag value.
}
procedure SetShowEmptySections(const Value: Boolean);
{Sets flag that indicates whether empty sections are displayed in overview
pane.
@param Value [in] New flag value.
}
property ShowEmptySections: Boolean
read GetShowEmptySections write SetShowEmptySections;
{Indicates whether empty sections are displayed in overview pane}
function GetShowNewSnippetsInNewTabs: Boolean;
{Gets flag that indicates whether new snippets and categories are
displayed in new tabs in details pane.
@returns Flag value.
}
procedure SetShowNewSnippetsInNewTabs(const Value: Boolean);
{Sets flag that indicates whether new snippets and categories are
displayed in new tabs in details pane.
@param Value [in] New flag value.
}
property ShowNewSnippetsInNewTabs: Boolean
read GetShowNewSnippetsInNewTabs write SetShowNewSnippetsInNewTabs;
{Indicates whether new snippets and ca-tegories are displayed in new tabs
in details pane}
/// <summary>Gets heading colour for specified database.</summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <returns>TColor. Required colour.</returns>
function GetDBHeadingColour(UserDefined: Boolean): TColor;
/// <summary>Sets heading colour for specified database.</summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <param name="Value">TColor [in] Required heading colour.</param>
procedure SetDBHeadingColour(UserDefined: Boolean;
const Value: TColor);
/// <summary>Records colour to be used for headings of items from either
/// main database (UserDefined=False) or user database (UserDefined=True).
/// </summary>
property DBHeadingColours[UserDefined: Boolean]: TColor
read GetDBHeadingColour write SetDBHeadingColour;
/// <summary>Gets custom colours for headings for specified database.
/// </summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <returns>IStringList. String list containing custom colours.</returns>
function GetDBHeadingCustomColours(UserDefined: Boolean): IStringList;
/// <summary>Sets custom colours for headings for specified database.
/// </summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <param name="Value">IStringList [in] String list containing custom
/// colours.</returns>
procedure SetDBHeadingCustomColours(UserDefined: Boolean;
Value: IStringList);
/// <summary>Records custom colours for headings of items from either main
/// database (UserDefined=False) or user database (UserDefined=True).
/// </summary>
property DBHeadingCustomColours[UserDefined: Boolean]: IStringList
read GetDBHeadingCustomColours write SetDBHeadingCustomColours;
function GetPrinterOptions: TPrintOptions;
{Gets print options.
@return Default print options.
}
procedure SetPrinterOptions(const Options: TPrintOptions);
{Sets default print options.
@param Options [in] New print options.
}
property PrinterOptions: TPrintOptions
read GetPrinterOptions write SetPrinterOptions;
{Default print options}
function GetPrinterPageMargins: TPageMargins;
{Gets default page margins.
@return Default page margins.
}
procedure SetPrinterPageMargins(const Margins: TPageMargins);
{Sets new default page margins.
@param Margins [in] New page margins.
}
property PrinterPageMargins: TPageMargins
read GetPrinterPageMargins write SetPrinterPageMargins;
{Default page margins used in printing}
function GetHiliteAttrs: IHiliteAttrs;
{Gets user defined syntax highlighter.
@return Current syntax highlighter attributes.
}
procedure SetHiliteAttrs(const Attrs: IHiliteAttrs);
{Sets new user defined syntax highlighter.
@param Attrs [in] New highlighter attributes.
}
property HiliteAttrs: IHiliteAttrs
read GetHiliteAttrs write SetHiliteAttrs;
{Attributes of syntax highlighter}
function GetCustomHiliteColours: IStringList;
{Gets custom syntax highlighter colours.
@return String list containing custom colours.
}
procedure SetCustomHiliteColours(const Colours: IStringList);
{Sets new custom syntax highlighter colours.
@param Colours [in] String list defining new colours.
}
property CustomHiliteColours: IStringList
read GetCustomHiliteColours write SetCustomHiliteColours;
{Custom syntax highlighter colours}
function GetWarnings: IWarnings;
{Gets information about warnings to be inhibited by code generator.
@return Interface to object containing information.
}
procedure SetWarnings(Warnings: IWarnings);
{Updates object that provides information about warnings to be inhibited
by code generator.
@param New warnings object.
}
property Warnings: IWarnings
read GetWarnings write SetWarnings;
{Information about warnings to be inhibited by code generator}
function GetNewsAge: Integer;
{Gets maximum age of news items to be displayed.
@return Required age in days.
}
procedure SetNewsAge(const Age: Integer);
{Sets maximum age of news items to be displayed.
@param Age [in] Required age in days.
}
property NewsAge: Integer
read GetNewsAge write SetNewsAge;
{Maximum age of news items to be displayed}
end;
function Preferences: IPreferences;
{Provides access to a singleton implementation a IPreferences, ensuring it
exists.
@return Singleton object instance.
}
implementation
uses
// Delphi
SysUtils,
// Project
Hiliter.UAttrs, Hiliter.UPersist, IntfCommon, UExceptions, UColours,
USettings;
type
{
TPreferences:
Class that implements IPreferences interface and its property access
methods. Does not persist the data. Intended for use as a temporary local
copy of the singleton that cannot be saved. Data entered in this object is
assigned to singleton for saving. Objects should be created using
singleton's Clone method.
}
TPreferences = class(TInterfacedObject,
IPreferences, IAssignable
)
strict protected
fSourceDefaultFileType: TSourceFileType;
{Default file extension / type used when writing code snippets to file}
fSourceCommentStyle: TCommentStyle;
{Commenting style used to describe snippets in generated source code}
fSourceSyntaxHilited: Boolean;
{Indicates whether generated source is highlighted by default}
fMeasurementUnits: TMeasurementUnits;
{Measurement unit in use by application}
fOverviewStartState: TOverviewStartState;
{Startup state of overview treeview}
fShowEmptySections: Boolean;
{Indicates whether empty sections are displayed in overview pane}
fShowNewSnippetsInNewTabs: Boolean;
{Indicates whether new snippets and categories are displayed in new tabs
in details pane}
fDBHeadingColours: array[Boolean] of TColor;
fDBHeadingCustomColours: array[Boolean] of IStringList;
fPrinterOptions: TPrintOptions;
{Default print options}
fPrinterPageMargins: TPageMargins;
{Default print page margins}
fHiliteAttrs: IHiliteAttrs;
{User defined syntax highlighter}
fHiliteCustomColours: IStringList;
{Custom highlighter colours}
fWarnings: IWarnings;
{Information about warnings to be inhibited by code generator}
fNewsAge: Integer;
{Maximum age of news items in days}
protected // do not make strict
{ IPreferences methods }
function GetSourceCommentStyle: TCommentStyle;
{Gets style of commenting used to describe snippets in generated code.
@return Current commenting style.
}
procedure SetSourceCommentStyle(const Value: TCommentStyle);
{Sets style of commenting to be used describe snippets in generated code.
@param Value [in] Required commenting style.
}
function GetSourceDefaultFileType: TSourceFileType;
{Gets current default file extension / type used when writing code
snippets to file.
@return Current default file extension.
}
procedure SetSourceDefaultFileType(const Value: TSourceFileType);
{Sets default file extension / type to be used when writing code snippets
to file.
@param Value [in] Required default file extension.
}
function GetSourceSyntaxHilited: Boolean;
{Gets current indicator of whether generated source is highlighted by
default.
@return Current value indicating whether source is highlighted.
}
procedure SetSourceSyntaxHilited(const Value: Boolean);
{Sets flag indicating whether generated source is highlighted by default.
@param Value [in] Flag indicating whether source is highlighted.
}
function GetMeasurementUnits: TMeasurementUnits;
{Gets measurement units used by application.
@return Current measurement units.
}
procedure SetMeasurementUnits(const Value: TMeasurementUnits);
{Sets measurement units to be used by application.
@param Value [in] Required measurement units.
}
function GetOverviewStartState: TOverviewStartState;
{Gets startup state of overview tree view.
@return Current startup state.
}
procedure SetOverviewStartState(const Value: TOverviewStartState);
{Sets startup state of overview tree view.
@param Value [in] Required startup state.
}
function GetShowEmptySections: Boolean;
{Gets flag that indicates whether empty sections are displayed in overview
pane.
@returns Flag value.
}
procedure SetShowEmptySections(const Value: Boolean);
{Sets flag that indicates whether empty sections are displayed in overview
pane.
@param Value [in] New flag value.
}
function GetShowNewSnippetsInNewTabs: Boolean;
{Gets flag that indicates whether new snippets and categories are
displayed in new tabs in details pane.
@returns Flag value.
}
procedure SetShowNewSnippetsInNewTabs(const Value: Boolean);
{Sets flag that indicates whether new snippets and categories are
displayed in new tabs in details pane.
@param Value [in] New flag value.
}
/// <summary>Gets heading colour for specified database.</summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <returns>TColor. Required colour.</returns>
/// <remarks>Method of IPreferences.</remarks>
function GetDBHeadingColour(UserDefined: Boolean): TColor;
/// <summary>Sets heading colour for specified database.</summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <param name="Value">TColor [in] Required heading colour.</param>
/// <remarks>Method of IPreferences.</remarks>
procedure SetDBHeadingColour(UserDefined: Boolean;
const Value: TColor);
/// <summary>Gets custom colours for headings for specified database.
/// </summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <returns>IStringList. String list containing custom colours.</returns>
/// <remarks>Method of IPreferences.</remarks>
function GetDBHeadingCustomColours(UserDefined: Boolean): IStringList;
/// <summary>Sets custom colours for headings for specified database.
/// </summary>
/// <param name="UserDefined">Boolean [in] Required database: True for user
/// database and False for main database.</param>
/// <param name="Value">IStringList [in] String list containing custom
/// colours.</returns>
/// <remarks>Method of IPreferences.</remarks>
procedure SetDBHeadingCustomColours(UserDefined: Boolean;
Value: IStringList);
function GetPrinterOptions: TPrintOptions;
{Gets print options.
@return Print options.
}
procedure SetPrinterOptions(const Options: TPrintOptions);
{Sets default print options.
@param Options [in] New print options.
}
function GetPrinterPageMargins: TPageMargins;
{Gets default page margins.
@return Default page margins.
}
procedure SetPrinterPageMargins(const Margins: TPageMargins);
{Sets new default page margins.
@param Margins [in] New page margins.
}
function GetHiliteAttrs: IHiliteAttrs;
{Gets user defined syntax highlighter.
@return Current syntax highlighter attributes.
}
procedure SetHiliteAttrs(const Attrs: IHiliteAttrs);
{Sets new user defined syntax highlighter.
@param Attrs [in] New highlighter attributes.
}
function GetCustomHiliteColours: IStringList;
{Gets custom syntax highlighter colours.
@return String list containing custom colours.
}
procedure SetCustomHiliteColours(const Colours: IStringList);
{Sets new custom syntax highlighter colours.
@param Colours [in] String list defining new colours.
}
function GetWarnings: IWarnings;
{Gets information about warnings to be inhibited by code generator.
@return Interface to object containing information.
}
procedure SetWarnings(Warnings: IWarnings);
{Updates object that provides information about warnings to be inhibited
by code generator.
@param New warnings object.
}
function GetNewsAge: Integer;
{Gets maximum age of news items to be displayed.
@return Required age in days.
}
procedure SetNewsAge(const Age: Integer);
{Sets maximum age of news items to be displayed.
@param Age [in] Required age in days.
}
{ IAssignable method }
procedure Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied.
@except Bug raised if Src does not support IPreferences.
}
public
constructor Create;
{Class constructor. Sets up object.
}
end;
{
TPreferencesPersist:
Class that implements IPreferences interface and its property access
methods. Intended for use as a singleton that reads and writes preference
from and to persistent storage. Can clone itself as a non-persistent
temporary object containing all data. Can also copy data from such a cloned
object via Assign method.
}
TPreferencesPersist = class(TPreferences,
IPreferences, IAssignable, IClonable
)
strict private
const
// Sub-sections of ssPreferences ini file section
cGeneral = 'General';
cSourceCode = 'SourceCode';
cPrinting = 'Printing';
cHiliter = 'Hiliter';
cCodeGenerator = 'CodeGen';
cNews = 'News';
cDisplay = 'Display';
class var fInstance: IPreferences;
{Stores reference to singleton instance of this class}
class function GetInstance: IPreferences; static;
{Returns singleton IPreferences object initialised from persistent
storage.
@return Object instance.
}
protected // do not make strict
{ IClonable method }
function Clone: IInterface;
{Create a new instance of the object that is a non-persisting copy.
@return New object's IInterface interface.
}
public
constructor Create;
{Class constructor. Creates object and reads preferences from persistent
storage.
}
destructor Destroy; override;
{Class destructor. Saves preferences to persistent storage then tears down
object.
}
class property Instance: IPreferences
read GetInstance;
{Reference to singleton instance of this class}
end;
function Preferences: IPreferences;
{Provides access to a singleton implementation a IPreferences.
@return Singleton object instance.
}
begin
Result := TPreferencesPersist.Instance;
end;
{ TPreferences }
procedure TPreferences.Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied.
@except Bug raised if Src does not support IPreferences.
}
var
SrcPref: IPreferences; // IPreferences interface of Src
begin
// Get IPreferences interface of given object
if not Supports(Src, IPreferences, SrcPref) then
raise EBug.Create(ClassName + '.Assign: Src is wrong type');
// Copy the data
Self.fSourceDefaultFileType := SrcPref.SourceDefaultFileType;
Self.fSourceCommentStyle := SrcPref.SourceCommentStyle;
Self.fSourceSyntaxHilited := SrcPref.SourceSyntaxHilited;
Self.fMeasurementUnits := SrcPref.MeasurementUnits;
Self.fOverviewStartState := SrcPref.OverviewStartState;
Self.fShowEmptySections := SrcPref.ShowEmptySections;
Self.fShowNewSnippetsInNewTabs := SrcPref.ShowNewSnippetsInNewTabs;
Self.fDBHeadingColours[False] := SrcPref.DBHeadingColours[False];
Self.fDBHeadingCustomColours[False] := SrcPref.DBHeadingCustomColours[False];
Self.fDBHeadingColours[True] := SrcPref.DBHeadingColours[True];
Self.fDBHeadingCustomColours[True] := SrcPref.DBHeadingCustomColours[True];
Self.fPrinterOptions := SrcPref.PrinterOptions;
Self.fPrinterPageMargins := SrcPref.PrinterPageMargins;
Self.SetHiliteAttrs(SrcPref.HiliteAttrs);
Self.SetCustomHiliteColours(SrcPref.CustomHiliteColours);
Self.SetWarnings(SrcPref.Warnings);
Self.SetNewsAge(SrcPref.NewsAge);
end;
constructor TPreferences.Create;
{Class constructor. Sets up object.
}
begin
inherited Create;
fHiliteAttrs := THiliteAttrsFactory.CreateDefaultAttrs;
fHiliteCustomColours := TIStringList.Create;
fWarnings := TWarnings.Create;
fDBHeadingCustomColours[False] := TIStringList.Create;
fDBHeadingCustomColours[True] := TIStringList.Create;
end;
function TPreferences.GetCustomHiliteColours: IStringList;
{Gets custom syntax highlighter colours.
@return String list containing custom colours.
}
begin
Result := fHiliteCustomColours;
end;
function TPreferences.GetDBHeadingColour(UserDefined: Boolean): TColor;
begin
Result := fDBHeadingColours[UserDefined];
end;
function TPreferences.GetDBHeadingCustomColours(
UserDefined: Boolean): IStringList;
begin
Result := fDBHeadingCustomColours[UserDefined];
end;
function TPreferences.GetHiliteAttrs: IHiliteAttrs;
{Gets user defined syntax highlighter.
@return Current syntax highlighter attributes.
}
begin
Result := fHiliteAttrs;
end;
function TPreferences.GetMeasurementUnits: TMeasurementUnits;
{Gets measurement units used by application.
@return Current measurement units.
}
begin
Result := fMeasurementUnits;
end;
function TPreferences.GetNewsAge: Integer;
{Gets maximum age of news items to be displayed.
@return Required age in days.
}
begin
Result := fNewsAge;
end;
function TPreferences.GetOverviewStartState: TOverviewStartState;
{Gets startup state of overview tree view.
@return Current startup state.
}
begin
Result := fOverviewStartState;
end;
function TPreferences.GetPrinterOptions: TPrintOptions;
{Gets print options.
@return Print options.
}
begin
Result := fPrinterOptions;
end;
function TPreferences.GetPrinterPageMargins: TPageMargins;
{Gets default page margins.
@return Default page margins.
}
begin
Result := fPrinterPageMargins;
end;
function TPreferences.GetShowEmptySections: Boolean;
begin
Result := fShowEmptySections;
end;
function TPreferences.GetShowNewSnippetsInNewTabs: Boolean;
begin
Result := fShowNewSnippetsInNewTabs;
end;
function TPreferences.GetSourceCommentStyle: TCommentStyle;
{Gets style of commenting used to describe snippets in generated code.
@return Current commenting style.
}
begin
Result := fSourceCommentStyle;
end;
function TPreferences.GetSourceDefaultFileType: TSourceFileType;
{Gets current default file extension / type used when writing code snippets to
file.
@return Current default file extension.
}
begin
Result := fSourceDefaultFileType;
end;
function TPreferences.GetSourceSyntaxHilited: Boolean;
{Gets current indicator of whether generated source is highlighted by default.
@return Current value indicating whether source is highlighted.
}
begin
Result := fSourceSyntaxHilited;
end;
function TPreferences.GetWarnings: IWarnings;
begin
Result := fWarnings;
end;
procedure TPreferences.SetCustomHiliteColours(const Colours: IStringList);
{Sets new custom syntax highlighter colours.
@param Colours [in] String list defining new colours.
}
begin
fHiliteCustomColours := Colours;
end;
procedure TPreferences.SetDBHeadingColour(UserDefined: Boolean;
const Value: TColor);
begin
fDBHeadingColours[UserDefined] := Value;
end;
procedure TPreferences.SetDBHeadingCustomColours(UserDefined: Boolean;
Value: IStringList);
begin
fDBHeadingCustomColours[UserDefined] := Value;
end;
procedure TPreferences.SetHiliteAttrs(const Attrs: IHiliteAttrs);
{Sets new user defined syntax highlighter.
@param Attrs [in] New highlighter attributes.
}
begin
(fHiliteAttrs as IAssignable).Assign(Attrs);
end;
procedure TPreferences.SetMeasurementUnits(const Value: TMeasurementUnits);
{Sets measurement units to be used by application.
@param Value [in] Required measurement units.
}
begin
fMeasurementUnits := Value;
end;
procedure TPreferences.SetNewsAge(const Age: Integer);
{Sets maximum age of news items to be displayed.
@param Age [in] Required age in days.
}
begin
fNewsAge := Age;
end;
procedure TPreferences.SetOverviewStartState(const Value: TOverviewStartState);
{Sets startup state of overview tree view.
@param Value [in] Required startup state.
}
begin
fOverviewStartState := Value;
end;
procedure TPreferences.SetPrinterOptions(const Options: TPrintOptions);
{Sets default print options.
@param Options [in] New print options.
}
begin
fPrinterOptions := Options;
end;
procedure TPreferences.SetPrinterPageMargins(const Margins: TPageMargins);
{Sets new default page margins.
@param Margins [in] New page margins.
}
begin
fPrinterPageMargins := Margins;
end;
procedure TPreferences.SetShowEmptySections(const Value: Boolean);
begin
fShowEmptySections := Value;
end;
procedure TPreferences.SetShowNewSnippetsInNewTabs(const Value: Boolean);
begin
fShowNewSnippetsInNewTabs := Value;
end;
procedure TPreferences.SetSourceCommentStyle(const Value: TCommentStyle);
{Sets style of commenting to be used describe snippets in generated code.
@param Value [in] Required commenting style.
}
begin
fSourceCommentStyle := Value;
end;
procedure TPreferences.SetSourceDefaultFileType(const Value: TSourceFileType);
{Sets default file extension / type to be used when writing code snippets to
file.
@param Value [in] Required default file extension.
}
begin
fSourceDefaultFileType := Value;
end;
procedure TPreferences.SetSourceSyntaxHilited(const Value: Boolean);
{Sets flag indicating whether generated source is highlighted by default.
@param Value [in] Flag indicating whether source is highlighted.
}
begin
fSourceSyntaxHilited := Value;
end;
procedure TPreferences.SetWarnings(Warnings: IWarnings);
begin
(fWarnings as IAssignable).Assign(Warnings);
end;
{ TPreferencesPersist }
function TPreferencesPersist.Clone: IInterface;
{Create a new instance of the object that is a non-persisting copy.
@return New object's IInterface interface.
}
var
NewPref: IPreferences; // reference to new object's IPreferences interface
begin
// Create new object
Result := TPreferences.Create;
// Copy properties to it
NewPref := Result as IPreferences;
NewPref.SourceDefaultFileType := Self.fSourceDefaultFileType;
NewPref.SourceCommentStyle := Self.fSourceCommentStyle;
NewPref.SourceSyntaxHilited := Self.fSourceSyntaxHilited;
NewPref.MeasurementUnits := Self.fMeasurementUnits;
NewPref.OverviewStartState := Self.fOverviewStartState;
NewPref.ShowEmptySections := Self.fShowEmptySections;
NewPref.ShowNewSnippetsInNewTabs := Self.fShowNewSnippetsInNewTabs;
NewPref.DBHeadingColours[False] := Self.fDBHeadingColours[False];
NewPref.DBHeadingCustomColours[False] := Self.fDBHeadingCustomColours[False];
NewPref.DBHeadingColours[True] := Self.fDBHeadingColours[True];
NewPref.DBHeadingCustomColours[True] := Self.fDBHeadingCustomColours[True];
NewPref.PrinterOptions := Self.fPrinterOptions;
NewPref.PrinterPageMargins := Self.fPrinterPageMargins;
NewPref.HiliteAttrs := Self.GetHiliteAttrs;
NewPref.CustomHiliteColours := Self.GetCustomHiliteColours;
NewPref.Warnings := Self.GetWarnings;
NewPref.NewsAge := Self.fNewsAge;
end;
constructor TPreferencesPersist.Create;
{Class constructor. Creates object and reads preferences from persistent
storage.
}
var
Storage: ISettingsSection; // object used to access persistent storage
const
// Default margin size in millimeters
cPrintPageMarginSizeMM = 25.0;
// Default maximum age of news items
cDefNewsAge = 92;
begin
inherited Create;
// Read general section
Storage := Settings.ReadSection(ssPreferences, cGeneral);
fMeasurementUnits := TMeasurementUnits(
StrToIntDef(Storage.ItemValues['Units'], Ord(DefaultMeasurementUnits))
);
// Read display section
Storage := Settings.ReadSection(ssPreferences, cDisplay);
fOverviewStartState := TOverviewStartState(
StrToIntDef(Storage.ItemValues['OverviewStartState'], Ord(ossExpanded))
);
fShowEmptySections := Boolean(
StrToIntDef(Storage.ItemValues['ShowEmptySections'], Ord(False))
);
fShowNewSnippetsInNewTabs := Boolean(
StrToIntDef(Storage.ItemValues['ShowNewSnippetsInNewTabs'], Ord(False))
);
fDBHeadingColours[False] := TColor(
StrToIntDef(Storage.ItemValues['MainDBHeadingColour'], clMainSnippet)
);
fDBHeadingColours[True] := TColor(
StrToIntDef(Storage.ItemValues['UserDBHeadingColour'], clUserSnippet)
);
fDBHeadingCustomColours[False] := Storage.GetStrings(
'MainDBHeadingCustomColourCount', 'MainDBHeadingCustomColour%d'
);
fDBHeadingCustomColours[True] := Storage.GetStrings(
'UserDBHeadingCustomColourCount', 'UserDBHeadingCustomColour%d'
);
// Read source code section
Storage := Settings.ReadSection(ssPreferences, cSourceCode);
fSourceDefaultFileType := TSourceFileType(
StrToIntDef(Storage.ItemValues['FileType'], Ord(sfPascal))
);
fSourceCommentStyle := TCommentStyle(
StrToIntDef(Storage.ItemValues['CommentStyle'], Ord(csAfter))
);
fSourceSyntaxHilited := Boolean(
StrToIntDef(Storage.ItemValues['UseSyntaxHiliting'], Ord(False))
);
// Read printing section
Storage := Settings.ReadSection(ssPreferences, cPrinting);
fPrinterOptions := [];
if Boolean(StrToIntDef(Storage.ItemValues['UseColor'], Ord(True))) then
Include(fPrinterOptions, poUseColor);
if Boolean(StrToIntDef(Storage.ItemValues['SyntaxPrint'], Ord(True))) then
Include(fPrinterOptions, poSyntaxPrint);
fPrinterPageMargins := TPageMargins.Create(
StrToFloatDef(Storage.ItemValues['LeftMargin'], cPrintPageMarginSizeMM),
StrToFloatDef(Storage.ItemValues['TopMargin'], cPrintPageMarginSizeMM),
StrToFloatDef(Storage.ItemValues['RightMargin'], cPrintPageMarginSizeMM),
StrToFloatDef(Storage.ItemValues['BottomMargin'], cPrintPageMarginSizeMM)
);
// Read syntax highlighter section
Storage := Settings.ReadSection(ssPreferences, cHiliter);
// syntax highlighter attributes
THiliterPersist.Load(Storage, fHiliteAttrs);
// custom colours
fHiliteCustomColours := Storage.GetStrings(
'CustomColourCount', 'CustomColour%d'
);
// Read code generator section
Storage := Settings.ReadSection(ssPreferences, cCodeGenerator);
TWarningsPersist.Load(Storage, fWarnings);
// Read news section
Storage := Settings.ReadSection(ssPreferences, cNews);
fNewsAge := StrToIntDef(Storage.ItemValues['MaxAge'], cDefNewsAge);
end;
destructor TPreferencesPersist.Destroy;
{Class destructor. Saves preferences to persistent storage then tears down
object.
}
var
Storage: ISettingsSection; // object used to access persistent storage
begin
// Write general section
Storage := Settings.EmptySection(ssPreferences, cGeneral);
Storage.ItemValues['Units'] := IntToStr(Ord(fMeasurementUnits));
Storage.Save;
// Write display section
Storage := Settings.EmptySection(ssPreferences, cDisplay);
Storage.ItemValues['OverviewStartState'] := IntToStr(
Ord(fOverviewStartState)
);
Storage.ItemValues['ShowEmptySections'] := IntToStr(
Ord(fShowEmptySections)
);
Storage.ItemValues['ShowNewSnippetsInNewTabs'] := IntToStr(
Ord(fShowNewSnippetsInNewTabs)
);
Storage.ItemValues['MainDBHeadingColour'] := IntToStr(
fDBHeadingColours[False]
);
Storage.ItemValues['UserDBHeadingColour'] := IntToStr(
fDBHeadingColours[True]
);
Storage.SetStrings(
'MainDBHeadingCustomColourCount',
'MainDBHeadingCustomColour%d',
fDBHeadingCustomColours[False]
);
Storage.SetStrings(
'UserDBHeadingCustomColourCount',
'UserDBHeadingCustomColour%d',
fDBHeadingCustomColours[True]
);
Storage.Save;
// Write source code section
Storage := Settings.EmptySection(ssPreferences, cSourceCode);
Storage.ItemValues['FileType'] := IntToStr(Ord(fSourceDefaultFileType));
Storage.ItemValues['CommentStyle'] := IntToStr(Ord(fSourceCommentStyle));
Storage.ItemValues['UseSyntaxHiliting'] := IntToStr(
Ord(fSourceSyntaxHilited)
);
Storage.Save;
// Write printing section
Storage := Settings.EmptySection(ssPreferences, cPrinting);
Storage.ItemValues['UseColor'] := IntToStr(
Ord(poUseColor in fPrinterOptions)
);
Storage.ItemValues['SyntaxPrint'] := IntToStr(
Ord(poSyntaxPrint in fPrinterOptions)
);
Storage.ItemValues['LeftMargin'] := FloatToStr(fPrinterPageMargins.Left);
Storage.ItemValues['TopMargin'] := FloatToStr(fPrinterPageMargins.Top);
Storage.ItemValues['RightMargin'] := FloatToStr(fPrinterPageMargins.Right);
Storage.ItemValues['BottomMargin'] := FloatToStr(fPrinterPageMargins.Bottom);
Storage.Save;
// Write syntax highlighter section
Storage := Settings.EmptySection(ssPreferences, cHiliter);
// syntax highlighter attributes
THiliterPersist.Save(Storage, fHiliteAttrs);
// custom colours
Storage.SetStrings(
'CustomColourCount', 'CustomColour%d', fHiliteCustomColours
);
Storage.Save;
// Write code generation section
Storage := Settings.EmptySection(ssPreferences, cCodeGenerator);
TWarningsPersist.Save(Storage, fWarnings);
// Write news section
Storage := Settings.EmptySection(ssPreferences, cNews);
Storage.ItemValues['MaxAge'] := IntToStr(fNewsAge);
Storage.Save;
inherited;
end;
class function TPreferencesPersist.GetInstance: IPreferences;
{Returns singleton IPreferences object initialised from persistent storage.
@return Object instance.
}
begin
if not Assigned(fInstance) then
fInstance := TPreferencesPersist.Create;
Result := fInstance;
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.