Menu

[r2218]: / trunk / Src / FmBase.pas  Maximize  Restore  History

Download this file

392 lines (355 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
384
385
386
387
388
389
390
{
* FmBase.pas
*
* Implements a form that provides the ancestor of all forms in the application.
* Provides default names for form window classes along with various operations
* that are common to all forms in application.
*
* $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 FmBase.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2012 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit FmBase;
interface
uses
// Delphi
Classes, Forms, Controls, Messages,
// Project
IntfAligner, UControlStateMgr;
type
{
TBaseForm:
Base class for all forms in application. Sets a unique window class name for
all derived forms and provides various operations that are common to all
forms in application.
}
TBaseForm = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
strict private
var
fCtrlStateMgr: TControlStateMgr; // Enables/disables all form's controls
const
WM_AFTERSHOW = WM_USER + 1; // Custom message used to call AfterShow
procedure AlignForm;
{Optionally aligns form to a "parent" window, using an IAligner instance.
Called from Form's OnShow event after form is customised and before it is
initialised.
}
procedure CMEnabledChanged(var Msg: TMessage); message CM_ENABLEDCHANGED;
{Called when enabled state of form changes. Updates state of all controls.
@param Msg [in/out] Unused.
}
procedure WMAfterShow(var Msg: TMessage); message WM_AFTERSHOW;
{Handles custom method that is posted just before form is shown and
handled just after form is shown.
@param Msg [in/out] Unused.
}
procedure ActivateContextMenu;
{Activates any context menu associated with active control or any of its
parents.
}
strict protected
procedure CreateParams(var Params: TCreateParams); override;
{Sets window class name to that provided by WindowClassName method if not
empty string.
@param Params [in/out] Parameters used in underlying call to
CreateWindowEx API function. Window class name member field is set.
}
function WindowClassName: string; virtual;
{Returns name of form's window class. This is a name comprised of company,
program and form class names. Subclasses may override.
@return Required window class name.
}
function GetAligner: IFormAligner; virtual;
{Gets object to be used to align form to owner.
@return Nul aligner that does nothing. Subclasses that require alignment
should override to return a suitable IAligner instance.
}
procedure CustomiseForm; virtual;
{Used to customise form. This method is called during the form's OnShow
event before the form is aligned. This implementation does nothing.
Subclasses should overrride to perform any customisation and to change
default size of form if necessary rather than handling the OnShow event.
}
procedure InitForm; virtual;
{Used to initialise form content. This method is called during the form's
OnShow event after the form is aligned. This implementation does nothing.
Subclasses should override to initialise the form rather than handling the
OnShow event. Form size must not be changed in this method.
}
procedure AfterShowForm; virtual;
{Used to perform any actions that need to occur after the form has been
shown and is visible on-screen. This implementation does nothing.
Subclasses that need this functionality should override this method.
}
constructor InternalCreate(AOwner: TComponent); virtual;
{Protected constructor. Does nothing but call inherited constructor.
Must be called by class methods of derived classes instead of inherited
Create if and only if the form supports the INoPublicConstruct interface.
@param AOwner [in] Component that owns form. May be nil.
}
public
constructor Create(AOwner: TComponent); override;
{Public constructor. Does nothing but call inherited constructor. Can be
called from descendant classes if necessary to override the constructor.
Must not be called, directly or indirectly if the descendant form supports
the INoPublicConstruct interface.
@param AOwner [in] Component that owns form. May be nil.
}
end;
implementation
uses
// Delphi
SysUtils, Windows, Menus,
// Project
UAppInfo, UBaseObjects, UFontHelper, UKeysHelper, UMenuHelper,
UNulFormAligner, UStrUtils;
{$R *.dfm}
type
/// <summary>Class helper that provides information about, and access to, the
/// protected PopupMenu property of TControl.</summary>
TControlHelper = class helper for TControl
public
/// <summary>Gets reference to pop-up menu assigned to protected PopupMenu
/// property.</summary>
function GetPopupMenu: TPopupMenu;
/// <summary>Checks if protected PopupMenu property is assigned.</summary>
function HasPopupMenu: Boolean;
end;
{ TBaseForm }
procedure TBaseForm.ActivateContextMenu;
{Activates any context menu associated with active control or any of its
parents.
}
var
Ctrl: TControl; // active control or a parent that supports pop-up menu
MenuIntf: IPopupMenu; // interface reference to controls supporting IPopupMenu
PopupPos: TPoint; // pop-up position of menu in screen co-ords
begin
// search active control parents to try to find if pop-up menu supported
Ctrl := ActiveControl;
if not Assigned(Ctrl) then
Ctrl := Self;
while Assigned(Ctrl)
and (Ctrl <> Self)
and not Ctrl.HasPopupMenu
and not (Supports(Ctrl, IPopupMenu, MenuIntf) and MenuIntf.HasPopup) do
Ctrl := Ctrl.Parent;
if not Assigned(Ctrl) then
Exit;
// we use an arbitrary pop-up position: may be able to improve on this
PopupPos := Ctrl.ClientToScreen(
Point(40, 40)
);
// show pop-up menu, either via PopupMenu property or via IPopupMenu interface
if Ctrl.HasPopupMenu then
Ctrl.GetPopupMenu.Popup(PopupPos.X, PopupPos.Y)
else if Supports(Ctrl, IPopupMenu, MenuIntf) and MenuIntf.HasPopup then
MenuIntf.Popup(PopupPos);
end;
procedure TBaseForm.AfterShowForm;
{Used to perform any actions that need to occur after the form has been shown
and is visible on-screen. This implementation does nothing. Subclasses that
need this functionality should override this method.
}
begin
// Do nothing
end;
procedure TBaseForm.AlignForm;
{Optionally aligns form to a "parent" window, using an IAligner instance.
Called from Form's OnShow event after form is customised and before it is
initialised.
}
begin
// Align the control. This does nothing by default, since default aligner is
// a do-nothing instance
GetAligner.AlignForm(Self);
end;
procedure TBaseForm.CMEnabledChanged(var Msg: TMessage);
{Called when enabled state of form changes. Updates state of all controls.
@param Msg [in/out] Unused.
}
begin
inherited;
// We update state of all controls, menu items and actions if possible
if Assigned(fCtrlStateMgr) then
fCtrlStateMgr.Update;
end;
constructor TBaseForm.Create(AOwner: TComponent);
{Public constructor. Does nothing but call inherited constructor. Can be
called from descendant classes if necessary to override the constructor. Must
not be called, directly or indirectly if the descendant form supports the
INoPublicConstruct interface.
@param AOwner [in] Component that owns form. May be nil.
}
begin
Assert(not Supports(Self, INoPublicConstruct),
ClassName + '.Create: Form''s public constructor can''t be called');
inherited;
end;
procedure TBaseForm.CreateParams(var Params: TCreateParams);
{Sets window class name to that provided by WindowClassName method if not
empty string.
@param Params [in/out] Parameters used in underlying call to
CreateWindowEx API function. Window class name member field is set.
}
var
ClassName: string; // window class name
begin
inherited;
ClassName := WindowClassName;
if ClassName <> '' then
StrLCopy(
Params.WinClassName,
PChar(ClassName),
SizeOf(Params.WinClassName) div SizeOf(Char) - 1
);
end;
procedure TBaseForm.CustomiseForm;
{Used to customise form. This method is called during the form's OnShow
event before the form is aligned. This implementation does nothing.
Subclasses should overrride to perform any customisation and to change
default size of form if necessary rather than handling the OnShow event.
}
begin
// Do nothing
end;
procedure TBaseForm.FormCreate(Sender: TObject);
{Handles form's OnCreate event. Creates owned objects.
@param Sender [in] Not used.
}
begin
inherited;
fCtrlStateMgr := TControlStateMgr.Create(Self);
// Set form font to OS default font. This will be used by all descendants and
// controls that have ParentFont true.
TFontHelper.SetDefaultFont(Self.Font, False);
end;
procedure TBaseForm.FormDestroy(Sender: TObject);
{Handles form's OnDestroy event. Unregisters form with object that works
around Delphi's Alt Key bug and frees control state manager.
@param Sender [in] Not used.
}
begin
FreeAndNil(fCtrlStateMgr);
end;
procedure TBaseForm.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
inherited;
if (Key = VK_F10) and (ExtractShiftKeys(Shift) = [ssAlt]) then
begin
outputdebugstring('Alt+F10 pressed');
ActivateContextMenu;
end;
end;
procedure TBaseForm.FormShow(Sender: TObject);
{Handles form's OnShow event. Calls a virtual method to customise form before
aligning it. A further virtual method is then called to initialise the form.
Finally a message is posted to the form that results in the AfterShowForm
method being called after the form has been displayed. Subclasses should
override the CustomiseForm, AlignForm and InitForm methods rather than
handling this event.
@param Sender [in] Not used.
}
begin
// Call virtual methods
CustomiseForm; // customise form: override if form size needs to be changed
AlignForm; // optionally align form using provided IAligner object
InitForm; // initialise form: do not change size of form in overrides
// Post message that causes AfterShowForm to be called after form has appeared
// on screen
PostMessage(Handle, WM_AFTERSHOW, 0, 0);
end;
function TBaseForm.GetAligner: IFormAligner;
{Gets object to be used to align form to owner.
@return Nul aligner that does nothing. Subclasses that require alignment
should override to return a suitable IAligner instance.
}
begin
Result := TNulAligner.Create;
end;
procedure TBaseForm.InitForm;
{Used to initialise form content. This method is called during the form's
OnShow event after the form is aligned. This implementation does nothing.
Subclasses should override to initialise the form rather than handling the
OnShow event. Form size must not be changed in this method.
}
begin
// Do nothing
end;
constructor TBaseForm.InternalCreate(AOwner: TComponent);
{Protected constructor. Does nothing but call inherited constructor. Must be
called by class methods of derived classes instead of inherited Create if and
only if the form supports the INoPublicConstruct interface.
@param AOwner [in] Component that owns form. May be nil.
}
begin
Assert(Supports(Self, INoPublicConstruct), ClassName + '.InternalCreate: '
+ 'Form''s protected constructor can''t be called');
inherited Create(AOwner);
end;
function TBaseForm.WindowClassName: string;
{Returns name of form's window class. This is a name comprised of company,
program and form class names. Subclasses may override.
@return Required window class name.
}
var
PostfixName: string; // Postfix to name, based on form's class name
begin
// Calculate window class name postfix. This is form class name, stripped of
// any preceeding 'T'
if StrStartsStr('T', ClassName) then
PostfixName := StrSliceRight(ClassName, Length(ClassName) - 1)
else
PostfixName := ClassName;
// Build window class name
Result := TAppInfo.CompanyName
+ '.' + TAppInfo.ProgramName
+ '.' + PostfixName;
end;
procedure TBaseForm.WMAfterShow(var Msg: TMessage);
{Handles custom method that is posted just before form is shown and handled
just after form is shown.
@param Msg [in/out] Unused.
}
begin
AfterShowForm;
end;
{ TControlHelper }
function TControlHelper.GetPopupMenu: TPopupMenu;
begin
Result := PopupMenu;
end;
function TControlHelper.HasPopupMenu: Boolean;
begin
Result := Assigned(PopupMenu);
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.