Menu

[r4761]: / branches / parsnip / Src / FrHiliterPrefs.pas  Maximize  Restore  History

Download this file

311 lines (272 with data), 9.8 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2014, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a frame that allows the user to set syntax highlighter
* preferences. Designed for use as one of the tabs in the Preferences dialogue
* box.
}
unit FrHiliterPrefs;
interface
uses
// Delphi
Controls, StdCtrls, Classes,
// Project
CS.SourceCode.Hiliter.Themes,
CS.UI.Helper.CollectionCtrlKVMgr,
FrPrefsBase,
UPreferences;
type
/// <summary>Frame that allows the user to select syntax highlighter themes
/// that are used in different parts of the application. The frame can
/// persist preferences entered by the user.</summary>
/// <remarks>Designed for use in preferences dialogue box.</remarks>
THiliterPrefsFrame = class(TPrefsBaseFrame)
btnReset: TButton;
lblUITheme: TLabel;
lblExportTheme: TLabel;
lblPrintTheme: TLabel;
cbUITheme: TComboBox;
cbExportTheme: TComboBox;
cbPrintTheme: TComboBox;
lblInstructions: TLabel;
btnEditThemes: TButton;
/// <summary>Restores default themes when Restore Defaults button is
/// clicked.</summary>
procedure btnResetClick(Sender: TObject);
/// <summary>Handles OnChange events on the three theme combo boxes.
/// Records the newly selected theme.</summary>
procedure ThemeComboChange(Sender: TObject);
/// <summary>Displays syntax highilight themes editor dialogue box to
/// permit themes to be edited.</summary>
procedure btnEditThemesClick(Sender: TObject);
strict private
var
/// <summary>Shorthand reference TConfig.Instance.HiliterThemes.
/// </summary>
fThemes: TSyntaxHiliteThemes;
/// <summary>Records syntax highlighter themes chosen by user.</summary>
fCurrentHiliteThemeIDs: TCurrentHiliteThemes;
/// <summary>Map of syntax highlighter theme kinds onto the combo boxes
/// used to select the themes.</summary>
fThemeCombos: array[TCurrentHiliteThemeKind] of TComboBox;
/// <summary>Objects used to manage theme combos and map their selections
/// to the related highlighter themes.</summary>
fComboBoxMgrs: array[TCurrentHiliteThemeKind] of
TUnsortedCollectionCtrlKVMgr<TSyntaxHiliteTheme>;
/// <summary>Indicates if any preference has changed.</summary>
fChanged: Boolean;
/// <summary>Stores names of all available themes in theme combo boxes and
/// selects the current theme in each of them.</summary>
procedure PopulateThemeCombos;
public
/// <summary>Constructs frame instance and initialises controls.</summary>
/// <param name="AOwner">TComponent [in] Component that owns the frame.
/// </param>
constructor Create(AOwner: TComponent); override;
/// <summary>Destroys frame instance.</summary>
destructor Destroy; override;
/// <summary>Updates controls from given preferences object.</summary>
/// <remarks>Called when the dialogue page containing the frame is
/// activated.</remarks>
procedure Activate(const Prefs: IPreferences); override;
/// <summary>Updates given preferences object with data entered in controls.
/// </summary>
/// <remarks>Called when the dialogue page containing the frame is
/// deactivated.</remarks>
procedure Deactivate(const Prefs: IPreferences); override;
/// <summary>Checks if preferences changed in this frame require that the
/// main window UI is updated.</summary>
/// <remarks>Called when the dialogue box containing the frame is closing.
/// </remarks>
function UIUpdated: Boolean; override;
/// <summary>Arranges controls on frame.</summary>
/// <remarks>Called after the frame has been sized.</remarks>
procedure ArrangeControls; override;
/// <summary>Returns the caption that is displayed in the tab of the
/// dialogue page that contains the frame.</summary>
/// <remarks>Called when the containing dialogue page is being created.
/// </remarks>
function DisplayName: string; override;
/// <summary>Returns an index number that determines the location of the
/// page containing the frame when displayed in the Preferences dialogue
/// box.</summary>
/// <remarks>Called when the frame is being registered with the Preferences
/// dialogue box.</remarks>
class function Index: Byte; override;
end;
implementation
uses
// Project
CS.Config,
CS.UI.Dialogs.HiliteThemesEditor,
FmPreferencesDlg,
UContainers,
UCtrlArranger,
UExceptions,
UIStringList,
UStrUtils;
{$R *.dfm}
{ THiliterPrefsFrame }
procedure THiliterPrefsFrame.Activate(const Prefs: IPreferences);
var
Kind: TCurrentHiliteThemeKind;
begin
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
fCurrentHiliteThemeIDs[Kind] := Prefs.CurrentHiliteThemeIds[Kind];
PopulateThemeCombos;
end;
procedure THiliterPrefsFrame.ArrangeControls;
begin
lblInstructions.Top := 3;
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf(lblInstructions, 12),
[lblUITheme, cbUITheme]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([lblUITheme, cbUITheme], 12),
[lblExportTheme, cbExportTheme]
);
TCtrlArranger.AlignVCentres(
TCtrlArranger.BottomOf([lblExportTheme, cbExportTheme], 12),
[lblPrintTheme, cbPrintTheme]
);
TCtrlArranger.MoveBelow([lblPrintTheme, cbPrintTheme], btnReset, 24);
TCtrlArranger.MoveBelow(btnReset, btnEditThemes, 12);
TCtrlArranger.AlignLefts(
[lblUITheme, lblExportTheme, lblPrintTheme, lblInstructions], 3
);
TCtrlArranger.AlignLefts(
[cbUITheme, cbExportTheme, cbPrintTheme, btnReset, btnEditThemes],
TCtrlArranger.RightOf([lblUITheme, lblExportTheme, lblPrintTheme], 18)
);
TCtrlArranger.StretchRightTo(
[cbUITheme, cbExportTheme, cbPrintTheme], Self.ClientWidth - 3
);
end;
procedure THiliterPrefsFrame.btnEditThemesClick(Sender: TObject);
begin
if THiliteThemesEditorDlg.Execute(Self, fCurrentHiliteThemeIDs) then
begin
PopulateThemeCombos;
fChanged := True;
end;
end;
procedure THiliterPrefsFrame.btnResetClick(Sender: TObject);
var
Kind: TCurrentHiliteThemeKind;
UIThemeID: TSyntaxHiliteThemeID;
begin
UIThemeID := fCurrentHiliteThemeIDs[htkUI];
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
begin
fComboBoxMgrs[Kind].Select(fThemes.DefaultTheme);
fCurrentHiliteThemeIDs[Kind] := fComboBoxMgrs[Kind].GetSelected.ID;
end;
if fCurrentHiliteThemeIDs[htkUI] <> UIThemeID then
fChanged := True;
end;
constructor THiliterPrefsFrame.Create(AOwner: TComponent);
resourcestring
// Colour dialogue style
sDlgTitle = 'Choose Element Colour'; // colour dialogue title
var
Kind: TCurrentHiliteThemeKind;
begin
inherited;
HelpKeyword := 'HiliterPrefs';
fThemes := TConfig.Instance.HiliterThemes;
fThemeCombos[htkUI] := cbUITheme;
fThemeCombos[htkExport] := cbExportTheme;
fThemeCombos[htkPrint] := cbPrintTheme;
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
begin
fCurrentHiliteThemeIDs[Kind] := TSyntaxHiliteThemeID.CreateNull;
fComboBoxMgrs[Kind] :=
TUnsortedCollectionCtrlKVMgr<TSyntaxHiliteTheme>.Create(
TComboBoxAdapter.Create(fThemeCombos[Kind]),
True,
function (const Left, Right: TSyntaxHiliteTheme): Boolean
begin
Result := Left.ID = Right.ID;
end
);
end;
// Clear dirty flag
fChanged := False;
end;
procedure THiliterPrefsFrame.Deactivate(const Prefs: IPreferences);
var
Kind: TCurrentHiliteThemeKind;
begin
// Prefs.HiliteThemes := fHiliteThemes;
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
Prefs.CurrentHiliteThemeIds[Kind] := fCurrentHiliteThemeIDs[Kind];
end;
destructor THiliterPrefsFrame.Destroy;
var
Kind: TCurrentHiliteThemeKind;
begin
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
fComboBoxMgrs[Kind].Free;
inherited;
end;
function THiliterPrefsFrame.DisplayName: string;
resourcestring
sDisplayName = 'Syntax Highlighter'; // display name
begin
Result := sDisplayName;
end;
class function THiliterPrefsFrame.Index: Byte;
begin
Result := 30;
end;
procedure THiliterPrefsFrame.PopulateThemeCombos;
var
Kind: TCurrentHiliteThemeKind;
Theme: TSyntaxHiliteTheme;
begin
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
begin
fComboBoxMgrs[Kind].Clear;
for Theme in fThemes do
fComboBoxMgrs[Kind].Add(Theme, Theme.FriendlyName);
fComboBoxMgrs[Kind].Select(
fThemes[fCurrentHiliteThemeIDs[Kind]]
);
end;
end;
procedure THiliterPrefsFrame.ThemeComboChange(Sender: TObject);
function ComboToThemeKind(CB: TComboBox): TCurrentHiliteThemeKind;
var
Kind: TCurrentHiliteThemeKind;
begin
for Kind := Low(TCurrentHiliteThemeKind) to High(TCurrentHiliteThemeKind) do
if fThemeCombos[Kind] = CB then
Exit(Kind);
raise EBug.Create(
ClassName + '.ThemeComboChange:ComboToThemeKind: Unknown combo box'
);
end;
var
Kind: TCurrentHiliteThemeKind;
begin
Kind := ComboToThemeKind(Sender as TComboBox);
fCurrentHiliteThemeIDs[Kind] := fComboBoxMgrs[Kind].GetSelected.ID;
if Kind = htkUI then
fChanged := True;
end;
function THiliterPrefsFrame.UIUpdated: Boolean;
begin
Result := fChanged;
end;
initialization
// Register frame with preferences dialogue box
TPreferencesDlg.RegisterPage(THiliterPrefsFrame);
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.