Menu

[r3204]: / branches / experimental / Src / TrunkSrc / FrSourcePrefs.pas  Maximize  Restore  History

Download this file

429 lines (376 with data), 13.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
{
* 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-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a frame that allows user to set source code preferences.
* Designed for use as one of the tabs in the Preferences dialogue box.
}
unit FrSourcePrefs;
interface
uses
// Delphi
StdCtrls, Forms, Controls, Classes,
// Project
FrPrefsBase, FrRTFShowCase, Hiliter.UGlobals, UPreferences, USourceFileInfo,
USourceGen;
type
{
TSourcePrefsFrame:
Frame that allows user to set source code preferences. Can persist
preferences entered by user. Note: Designed for use in preferences dialog
box.
}
TSourcePrefsFrame = class(TPrefsBaseFrame)
cbCommentStyle: TComboBox;
cbSnippetFileType: TComboBox;
chkSyntaxHighlighting: TCheckBox;
frmPreview: TRTFShowCaseFrame;
gbFileFormat: TGroupBox;
gbSourceCode: TGroupBox;
lblCommentStyle: TLabel;
lblSnippetFileType: TLabel;
chkTruncateComments: TCheckBox;
procedure cbCommentStyleChange(Sender: TObject);
procedure cbSnippetFileTypeChange(Sender: TObject);
strict private
fHiliteAttrs: IHiliteAttrs;
{Style of syntax highlighting to use in sample output}
procedure SelectSourceFileType(const FT: TSourceFileType);
{Selects entry in file type combo box that matches specified source code
file type.
@param FT [in] File type to select.
}
procedure SelectCommentStyle(const CommentStyle: TCommentStyle);
{Selects entry in comment style combo box that matches specified style.
@param CommentStyle [in] Comment style to select.
}
function GetSourceFileType: TSourceFileType;
{Gets source file type selected by user.
@return Source file type.
}
function GetCommentStyle: TCommentStyle;
{Gets comment style selected by user.
@return Comment style.
}
procedure UpdateControlState;
{Updates state of dialog's controls depending on values entered.
}
procedure UpdatePreview;
{Updates source code preview according to user selections.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Initialises controls.
}
procedure Activate(const Prefs: IPreferences); override;
{Called when page activated. Updates controls.
@param Prefs [in] Object that provides info used to update controls.
}
procedure Deactivate(const Prefs: IPreferences); override;
{Called when page is deactivated. Stores information entered by user.
@param Prefs [in] Object used to store information.
}
/// <summary>Checks if preference changes require that main window UI is
/// updated.</summary>
/// <remarks>Called when dialog box containing frame is closing. Always
/// returns False because these preferences never affect UI.</remarks>
function UIUpdated: Boolean; override;
procedure ArrangeControls; override;
{Arranges controls on frame. Called after frame has been sized.
}
function DisplayName: string; override;
{Caption that is displayed in the tab sheet that contains this frame when
displayed in the preference dialog box.
@return Required display name.
}
class function Index: Byte; override;
{Index number that determines the location of the tab containing this
frame when displayed in the preferences dialog box.
@return Required index number.
}
end;
implementation
uses
// Delphi
SysUtils, Math,
// Project
FmPreferencesDlg, Hiliter.UAttrs, Hiliter.UFileHiliter, Hiliter.UHiliters,
IntfCommon, UConsts, UCtrlArranger, URTFUtils;
{$R *.dfm}
resourcestring
// File type descriptions
sHTMLFileDesc = 'HTML';
sRTFFileDesc = 'Rich text';
sPascalFileDesc = 'Pascal';
sTextFileDesc = 'Plain text';
const
// Maps source code file types to descriptions
cFileDescs: array[TSourceFileType] of string = (
sTextFileDesc, sPascalFileDesc, sHTMLFileDesc, sRTFFileDesc
);
type
{
TSourcePrefsPreview:
Class used to generate source code preview displayed in source preferences
frame.
}
TSourcePrefsPreview = class(TObject)
strict private
fHiliteAttrs: IHiliteAttrs;
{Attributes of syntax highlighter to use to render preview}
fCommentStyle: TCommentStyle;
{Value of CommentStyle property}
function SourceCode: string;
{Create raw source code used in preview.
@return Required raw source code.
}
public
constructor Create(const CommentStyle: TCommentStyle;
const HiliteAttrs: IHiliteAttrs);
{Class constructor. Sets up object.
@param CommentStyle [in] Style of commenting to use in preview.
@param HiliteAttrs [in] Attributes of highlighter used to render
preview.
}
function Generate: TRTF;
{Generate RTF code used to render preview.
@return Required RTF code.
}
end;
{ TSourcePrefsFrame }
procedure TSourcePrefsFrame.Activate(const Prefs: IPreferences);
{Called when page activated. Updates controls.
@param Prefs [in] Object that provides info used to update controls.
}
begin
// Update control values per settings
SelectSourceFileType(Prefs.SourceDefaultFileType);
SelectCommentStyle(Prefs.SourceCommentStyle);
chkTruncateComments.Checked := Prefs.TruncateSourceComments;
chkSyntaxHighlighting.Checked := Prefs.SourceSyntaxHilited;
(fHiliteAttrs as IAssignable).Assign(Prefs.HiliteAttrs);
fHiliteAttrs.ResetDefaultFont;
// Update state of controls and preview
UpdateControlState;
UpdatePreview;
end;
procedure TSourcePrefsFrame.ArrangeControls;
{Arranges controls on frame. Called after frame has been sized.
}
var
Col2Left: Integer; // position of second column of controls
begin
TCtrlArranger.AlignVCentres(20, [lblCommentStyle, cbCommentStyle]);
TCtrlArranger.MoveBelow([lblCommentStyle, cbCommentStyle], frmPreview, 8);
TCtrlArranger.MoveBelow(frmPreview, chkTruncateComments, 8);
gbSourceCode.ClientHeight := TCtrlArranger.TotalControlHeight(gbSourceCode)
+ 10;
TCtrlArranger.AlignVCentres(20, [lblSnippetFileType, cbSnippetFileType]);
TCtrlArranger.MoveBelow(
[lblSnippetFileType, cbSnippetFileType], chkSyntaxHighlighting, 8
);
gbFileFormat.ClientHeight := TCtrlArranger.TotalControlHeight(gbFileFormat)
+ 10;
TCtrlArranger.MoveBelow(gbSourceCode, gbFileFormat, 8);
TCtrlArranger.AlignLefts([lblCommentStyle, lblSnippetFileType], 8);
Col2Left := Max(
TCtrlArranger.RightOf(lblCommentStyle),
TCtrlArranger.RightOf(lblSnippetFileType)
) + 12;
TCtrlArranger.AlignLefts(
[
cbCommentStyle, frmPreview, cbSnippetFileType, chkSyntaxHighlighting,
chkTruncateComments
],
Col2Left
);
end;
procedure TSourcePrefsFrame.cbCommentStyleChange(Sender: TObject);
{Handles OnChange event in Comment Style combo box by updating preview.
@param Sender [in] Not used.
}
begin
UpdateControlState;
UpdatePreview;
end;
procedure TSourcePrefsFrame.cbSnippetFileTypeChange(Sender: TObject);
{Handles OnChange event in Snippets File Type combo box by updating other
dialog controls with respect to changed value.
@param Sender [in] Not used.
}
begin
UpdateControlState;
end;
constructor TSourcePrefsFrame.Create(AOwner: TComponent);
{Class constructor. Initialises controls.
@param AOwner [in] Not used.
}
var
FileType: TSourceFileType; // loops thru source file types
CSIdx: TCommentStyle; // loops thru comment styles
begin
inherited;
HelpKeyword := 'SourceCodePrefs';
// Create syntax highlighter object for use in sample output
fHiliteAttrs := THiliteAttrsFactory.CreateDefaultAttrs;
// Populate file type combo
for FileType := Low(TSourceFileType) to High(TSourceFileType) do
cbSnippetFileType.Items.AddObject(cFileDescs[FileType], TObject(FileType));
// Populate comment style combo
for CSIdx := Low(TCommentStyle) to High(TCommentStyle) do
cbCommentStyle.Items.AddObject(
TSourceComments.CommentStyleDesc(CSIdx), TObject(CSIdx)
);
end;
procedure TSourcePrefsFrame.Deactivate(const Prefs: IPreferences);
{Called when page is deactivated. Stores information entered by user.
@param Prefs [in] Object used to store information.
}
begin
Prefs.SourceCommentStyle := GetCommentStyle;
Prefs.TruncateSourceComments := chkTruncateComments.Checked;
Prefs.SourceDefaultFileType := GetSourceFileType;
Prefs.SourceSyntaxHilited := chkSyntaxHighlighting.Checked;
end;
function TSourcePrefsFrame.DisplayName: string;
{Caption that is displayed in the tab sheet that contains this frame when
displayed in the preference dialog box.
@return Required display name.
}
resourcestring
sDisplayName = 'Code Formatting'; // display name
begin
Result := sDisplayName;
end;
function TSourcePrefsFrame.GetCommentStyle: TCommentStyle;
{Gets comment style selected by user.
@return Comment style.
}
begin
Result := TCommentStyle(
cbCommentStyle.Items.Objects[cbCommentStyle.ItemIndex]
);
end;
function TSourcePrefsFrame.GetSourceFileType: TSourceFileType;
{Gets source file type selected by user.
@return Source file type.
}
begin
Result := TSourceFileType(
cbSnippetFileType.Items.Objects[cbSnippetFileType.ItemIndex]
);
end;
class function TSourcePrefsFrame.Index: Byte;
{Index number that determines the location of the tab containing this
frame when displayed in the preferences dialog box.
@return Required index number.
}
begin
Result := 20;
end;
procedure TSourcePrefsFrame.SelectCommentStyle(
const CommentStyle: TCommentStyle);
{Selects entry in comment style combo box that matches specified style.
@param CommentStyle [in] Comment style to select.
}
begin
cbCommentStyle.ItemIndex :=
cbCommentStyle.Items.IndexOfObject(TObject(CommentStyle));
end;
procedure TSourcePrefsFrame.SelectSourceFileType(
const FT: TSourceFileType);
{Selects entry in file type combo box that matches specified source code file
type.
@param FT [in] File type to select.
}
begin
cbSnippetFileType.ItemIndex :=
cbSnippetFileType.Items.IndexOfObject(TObject(FT));
end;
function TSourcePrefsFrame.UIUpdated: Boolean;
begin
Result := False;
end;
procedure TSourcePrefsFrame.UpdateControlState;
{Updates state of dialog's controls depending on values entered.
}
begin
chkSyntaxHighlighting.Enabled :=
TFileHiliter.IsHilitingSupported(GetSourceFileType);
chkTruncateComments.Enabled := GetCommentStyle <> csNone;
end;
procedure TSourcePrefsFrame.UpdatePreview;
{Updates source code preview according to user selections.
}
var
Preview: TSourcePrefsPreview; // object that creates preview
begin
// We always use same font size, regardless of user preferences
fHiliteAttrs.FontSize := Font.Size;
// Generate and display preview with required comment style
Preview := TSourcePrefsPreview.Create(GetCommentStyle, fHiliteAttrs);
try
// Display preview
TRichEditHelper.Load(frmPreview.RichEdit, Preview.Generate);
finally
Preview.Free;
end;
end;
{ TSourcePrefsPreview }
resourcestring
// Localisable source preview text
sPrevProcName = 'Example'; // name of example proc
sPrevDesc = 'Description goes here.'; // sample proc description
sPrevCalledProc = 'DoSomethingHere'; // name of proc called from example
const
// Example procedure prototype and body
cPrevProcProto = 'procedure %0:s;' + EOL;
cPrevProcBody = 'begin' + EOL +' %1:s;'+ EOL + 'end;';
// Map of comment style to sample code
cPrevSamples: array[TCommentStyle] of string = (
// no comments: just prototype followed by body
cPrevProcProto + cPrevProcBody,
// comments after snippet header: prototype then comments then body
cPrevProcProto + ' {%2:s}' + EOL + cPrevProcBody,
// comments before prototype: comments then prototype then body
'{' + EOL + ' %2:s' + EOL + '}' + EOL + cPrevProcProto + cPrevProcBody
);
constructor TSourcePrefsPreview.Create(const CommentStyle: TCommentStyle;
const HiliteAttrs: IHiliteAttrs);
{Class constructor. Sets up object.
@param CommentStyle [in] Style of commenting to use in preview.
@param HiliteAttrs [in] Attributes of highlighter used to render preview.
}
begin
inherited Create;
fCommentStyle := CommentStyle;
fHiliteAttrs := HiliteAttrs;
end;
function TSourcePrefsPreview.Generate: TRTF;
{Generate RTF code used to render preview.
@return Required RTF code.
}
begin
Result := TRTF.Create(TRTFDocumentHiliter.Hilite(SourceCode, fHiliteAttrs));
end;
function TSourcePrefsPreview.SourceCode: string;
{Create raw source code used in preview.
@return Required raw source code.
}
begin
// Source code layout depends on commenting style
Result := Format(
cPrevSamples[fCommentStyle],
[sPrevProcName, sPrevCalledProc, sPrevDesc]
);
end;
initialization
// Register frame with preferences dialog box
TPreferencesDlg.RegisterPage(TSourcePrefsFrame);
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.