Menu

[r2017]: / trunk / Src / FmPreferencesDlg.pas  Maximize  Restore  History

Download this file

385 lines (345 with data), 13.5 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
{
* FmPreferencesDlg.pas
*
* Implements a dialog box that is used to set 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 FmPreferencesDlg.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 FmPreferencesDlg;
interface
uses
// Delphi
ComCtrls, StdCtrls, Controls, ExtCtrls, Classes, Generics.Collections,
// Project
FmGenericOKDlg, FrPrefsBase, UBaseObjects, UPreferences;
type
/// <summary>
/// Dialog box that sets user preferences.
/// </summary>
/// <remarks>
/// This dialog box displays tabs for preferences frames registered with the
/// dialog box.
/// </remarks>
TPreferencesDlg = class(TGenericOKDlg, INoPublicConstruct)
pcMain: TPageControl;
/// <summary>OK button click event handler. Writes preference data to
/// persistent storage.</summary>
procedure btnOKClick(Sender: TObject);
/// <param>Called when current tab sheet has changed. Gets newly selected
/// page to re-initialise its controls from local preferences.</param>
/// <remarks>This enables any pages that depend on preferences that may
/// have been changed in other pages to update appropriately.</remarks>
procedure pcMainChange(Sender: TObject);
/// <summary>Called just before active tab sheet is changed. Causes page
/// about to be deselected to update local preferences with any changes.
/// </summary>
/// <remarks>We do this in case another page needs to update due to changes
/// made on current page.</remarks>
procedure pcMainChanging(Sender: TObject; var AllowChange: Boolean);
/// <summary>Handles event triggered when user clicks on one of page
/// control tabs. Ensures page control has focus.</summary>
/// <remarks>Without this fix, page control does not always get focus when
/// a tab is clicked.</remarks>
procedure pcMainMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
strict private
class var
/// <summary>List of registered page frames</summary>
fPages: TList<TPrefsFrameClass>;
var
/// <summary>Local copy of preferences.</summary>
fLocalPrefs: IPreferences;
/// <summary>Records if main UI needs to be updated to reflect changed
/// preferences.</summary>
fUpdateUI: Boolean;
/// <summary>Creates the required frames and displays each in a tab sheet
/// within the page control.</summary>
/// <param name="FrameClasses">array of TPrefsFrameClass [in] Class
/// references for each required frame.</param>
procedure CreatePages(const FrameClasses: array of TPrefsFrameClass);
/// <summary>Gets reference to preferences page frame associated with given
/// tab sheet.</summary>
function MapTabSheetToPage(const TabSheet: TTabSheet): TPrefsBaseFrame;
overload;
/// <summary>Gets reference to preferences page frame associated with given
/// tab index.</summary>
function MapTabSheetToPage(const TabIdx: Integer): TPrefsBaseFrame;
overload;
/// <summary>Gets reference to preferences frame on currently selected tab.
/// </summary>
function GetSelectedPage: TPrefsBaseFrame;
strict protected
/// <summary>Gets the help A-link keyword to be used when help button
/// clicked.</summary>
/// <remarks>Keyword depends on which preferences page is displayed to
/// permit each preferences page to have its own help topic.</remarks>
function CustomHelpKeyword: string; override;
/// <summary>Sizes frames providing content of pages of dialog and gets
/// each to arrange its controls.</summary>
procedure ArrangeForm; override;
/// <summary>Displays and initialises frames used to display pages of
/// dialog.</summary>
procedure InitForm; override;
public
/// <summary>Creates empty registered page frame list object.</summary>
class constructor Create;
/// <summary>Frees registered page frame list object.</summary>
class destructor Destroy;
/// <summary>Displays dialog with pages for each specified preferences
/// frame.</summary>
/// <param name="AOwner">TComponent [in] Component that owns dialog.
/// </param>
/// <param name="Pages">array of TPrefsFrameClass [in] Class references of
/// frames to be displayed.</param>
/// <param name="UpdateUI">Boolean [out] Indicates if main UI needs to
/// be updated as a result of preference changes.</param>
/// <returns>True if user clicks OK to accept changes or False if user
/// cancels and no changes made.</returns>
class function Execute(AOwner: TComponent;
const Pages: array of TPrefsFrameClass; out UpdateUI: Boolean): Boolean;
overload;
/// <summary>Displays dialog with pages for each specified preferences
/// frame.</summary>
/// <param name="AOwner">TComponent [in] Component that owns dialog.
/// </param>
/// <param name="Pages">array of TPrefsFrameClass [in] Class references of
/// frames to be displayed.</param>
/// <returns>True if user clicks OK to accept changes or False if user
/// cancels and no changes made.</returns>
class function Execute(AOwner: TComponent;
const Pages: array of TPrefsFrameClass): Boolean; overload;
/// <summary>Displays preferences dialog with all registered preference
/// frames.</summary>
/// <param name="AOwner">TComponent [in] Component that owns dialog.
/// </param>
/// <param name="UpdateUI">Boolean [out] Indicates if main UI needs to
/// be updated as a result of preference changes.</param>
/// <returns>True if user clicks OK to accept changes or False if user
/// cancels and no changes made.</returns>
class function Execute(AOwner: TComponent; out UpdateUI: Boolean): Boolean;
overload;
/// <summary>Registers given preferences frame class for inclusion in the
/// preferences dialog box.</summary>
/// <remarks>Registered frames are created when the dialog box is displayed
/// and freed when it closes.</remarks>
class procedure RegisterPage(const FrameCls: TPrefsFrameClass);
end;
implementation
{
Design notes
------------
This dialog box is a multi-page preferences dialog that provides access to
each page via a tab. The dialog box does not provide an implementation of each
page of the dialog. This representation must be provided by a frame descended
from TPrefsBaseFrame. Such frames must:
(a) register themselves with the dialog box by passing their class to the
TPreferencesDlg.RegisterPage class method.
(b) implement all the abstract methods of TPrefsBaseFrame.
The dialog box will create registered frames when needed and host them within
a tab sheet in the main page control.
There is no need to modify this unit when a new frame is to be addded to it.
}
uses
// Project
IntfCommon;
{$R *.dfm}
{ TPreferencesDlg }
procedure TPreferencesDlg.ArrangeForm;
var
Idx: Integer; // loops through all displayed tab sheets
Frame: TPrefsBaseFrame; // references each preference frame
TabSheet: TTabSheet; // references each tab sheet
begin
inherited;
for Idx := 0 to Pred(pcMain.PageCount) do
begin
TabSheet := pcMain.Pages[Idx];
Frame := MapTabSheetToPage(TabSheet);
Frame.Width := TabSheet.Width - 8;
Frame.Height := TabSheet.Height - 8;
Frame.ArrangeControls;
end;
end;
procedure TPreferencesDlg.btnOKClick(Sender: TObject);
var
TabIdx: Integer; // loops through all tab sheets on page
begin
inherited;
// Get each visible page to update local preferences
for TabIdx := 0 to Pred(pcMain.PageCount) do
begin
MapTabSheetToPage(TabIdx).SavePrefs(fLocalPrefs);
fUpdateUI := fUpdateUI or MapTabSheetToPage(TabIdx).UIUpdated;
end;
// Update global preferences with data from local preferences object
(Preferences as IAssignable).Assign(fLocalPrefs);
end;
class constructor TPreferencesDlg.Create;
begin
fPages := TList<TPrefsFrameClass>.Create;
end;
procedure TPreferencesDlg.CreatePages(
const FrameClasses: array of TPrefsFrameClass);
var
Idx: Integer; // loops through FrameClasses array
TS: TTabSheet; // references each tabsheet as it is created
Frame: TPrefsBaseFrame; // references each frame as it is created
begin
for Idx := Low(FrameClasses) to High(FrameClasses) do
begin
// create a tabsheet in page control
TS := TTabSheet.Create(Self);
TS.PageControl := pcMain;
// create a frame parented by tab sheet
Frame := FrameClasses[Idx].Create(Self);
Frame.Parent := TS;
Frame.Left := 4;
Frame.Top := 4;
// set tab sheet caption to frame's display name
TS.Caption := Frame.DisplayName;
end;
end;
function TPreferencesDlg.CustomHelpKeyword: string;
begin
// We expect keyword to be stored in frame's HelpKeyword property
Result := GetSelectedPage.HelpKeyword;
end;
class destructor TPreferencesDlg.Destroy;
begin
fPages.Free;
end;
class function TPreferencesDlg.Execute(AOwner: TComponent;
const Pages: array of TPrefsFrameClass; out UpdateUI: Boolean): Boolean;
begin
with InternalCreate(AOwner) do
try
CreatePages(Pages);
Result := ShowModal = mrOK;
if Result then
UpdateUI := fUpdateUI;
finally
Free;
end;
end;
class function TPreferencesDlg.Execute(AOwner: TComponent;
out UpdateUI: Boolean): Boolean;
var
FrameClasses: array of TPrefsFrameClass; // array of preference frame classes
Idx: Integer; // loops through registered frames
begin
// Copy registered frame classes into dynamic array and pass this to
// constructor that accepts a list of frames classes.
SetLength(FrameClasses, fPages.Count);
for Idx := 0 to Pred(fPages.Count) do
FrameClasses[Idx] := fPages[Idx];
Result := Execute(AOwner, FrameClasses, UpdateUI);
end;
class function TPreferencesDlg.Execute(AOwner: TComponent;
const Pages: array of TPrefsFrameClass): Boolean;
var
Dummy: Boolean; // unused UpdateUI parameters
begin
Result := Execute(AOwner, Pages, Dummy);
end;
function TPreferencesDlg.GetSelectedPage: TPrefsBaseFrame;
begin
Result := MapTabSheetToPage(pcMain.ActivePage);
end;
procedure TPreferencesDlg.InitForm;
var
TabIdx: Integer; // loops thru tabs in page control
begin
inherited;
// Take local copy of global preferences. This local copy will be updated as
// user enters data and global object is only updated if user OKs - local
// copy is discarded if user cancels.
fLocalPrefs := (Preferences as IClonable).Clone as IPreferences;
// Display and initialise required pages
for TabIdx := 0 to Pred(pcMain.PageCount) do
MapTabSheetToPage(TabIdx).LoadPrefs(fLocalPrefs);
// Select first TabSheet
pcMain.ActivePageIndex := 0;
end;
function TPreferencesDlg.MapTabSheetToPage(
const TabIdx: Integer): TPrefsBaseFrame;
begin
Result := MapTabSheetToPage(pcMain.Pages[TabIdx]);
end;
function TPreferencesDlg.MapTabSheetToPage(
const TabSheet: TTabSheet): TPrefsBaseFrame;
var
CtrlIdx: Integer; // loops through all child controls of tab sheet
begin
Assert(Assigned(TabSheet), ClassName + '.MapTabSheetToPage: TabSheet is nil');
Result := nil;
for CtrlIdx := 0 to Pred(TabSheet.ControlCount) do
begin
if (TabSheet.Controls[CtrlIdx] is TPrefsBaseFrame) then
begin
Result := TabSheet.Controls[CtrlIdx] as TPrefsBaseFrame;
Break;
end;
end;
Assert(Assigned(Result), ClassName + '.MapTabSheetToPage: Frame not found');
end;
procedure TPreferencesDlg.pcMainChange(Sender: TObject);
begin
GetSelectedPage.Activate(fLocalPrefs);
end;
procedure TPreferencesDlg.pcMainChanging(Sender: TObject;
var AllowChange: Boolean);
begin
GetSelectedPage.Deactivate(fLocalPrefs);
end;
procedure TPreferencesDlg.pcMainMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if htOnItem in pcMain.GetHitTestInfoAt(X, Y) then
pcMain.SetFocus;
end;
class procedure TPreferencesDlg.RegisterPage(const FrameCls: TPrefsFrameClass);
var
PageIdx: Integer; // loops through all registered frames
InsIdx: Integer; // place to insert new frame in list (per Index property)
begin
// Search for place in frame list to insert new frame (sorted on frame's Index
// property).
InsIdx := fPages.Count;
for PageIdx := 0 to Pred(fPages.Count) do
begin
if FrameCls.Index < fPages[PageIdx].Index then
begin
InsIdx := PageIdx;
Break;
end;
end;
// Record the frame's class reference
fPages.Insert(InsIdx, FrameCls);
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.