Menu

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

Download this file

196 lines (174 with data), 5.6 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
{
* UControlStateMgr.pas
*
* Implements a cass that can enable or disable all controls, actions and
* menu items on a form depending on the form's Enabled property
*
* $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 UControlStateMgr.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2009-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UControlStateMgr;
interface
uses
// Delphi
Forms, Controls, Menus, ActnList, Classes;
type
{
TControlStateMgr:
Class that can enable or disable all controls, actions and menu items on a
form depending on the form's Enabled property.
}
TControlStateMgr = class(TObject)
strict private
fForm: TCustomForm;
{Reference to form being managed}
procedure UpdateControl(const Ctrl: TControl);
{Updates a control if it has no associated action.
@param Ctrl [in] Control to be updated.
}
procedure UpdateMenuItem(const MI: TMenuItem);
{Updates a menu item if it has no action.
@param MI [in] Menu item to be updated.
}
procedure UpdateActionList(const AL: TActionList);
{Updates actions in an action list.
@param AL [in] Action list.
}
procedure ActionListUpdate(Action: TBasicAction; var Handled: Boolean);
{Handles OnUpdate event of an action list. Disabled all actions when form
is disabled. Enables all actions without their own OnUpdate event handlers
when form is enabled.
@param Action [in] Action being updated.
@param Handled [in/out] False when called. Set True to prevent further
processing if action's Enabled property has been set.
}
public
constructor Create(const Form: TCustomForm);
{Class constructor. Sets up object.
@param Form [in] Reference to form whose components are to be enabled /
disabled.
}
procedure Update;
{Updates state of form's components according to value of form's Enabled
property.
}
end;
implementation
{ TControlStateMgr }
procedure TControlStateMgr.ActionListUpdate(Action: TBasicAction;
var Handled: Boolean);
{Handles OnUpdate event of an action list. Disabled all actions when form is
disabled. Enables all actions without their own OnUpdate event handlers when
form is enabled.
@param Action [in] Action being updated.
@param Handled [in/out] False when called. Set True to prevent further
processing if action's Enabled property has been set.
}
begin
Handled := False;
if Action is TCustomAction then
if fForm.Enabled then
begin
// Form is enabled: we enabled any action without OnUpdate handler
if not Assigned(Action.OnUpdate) then
begin
(Action as TCustomAction).Enabled := True;
Handled := True;
end;
end
else
begin
// Form is disabled: we disable all actions
(Action as TCustomAction).Enabled := False;
Handled := True;
end;
end;
constructor TControlStateMgr.Create(const Form: TCustomForm);
{Class constructor. Sets up object.
@param Form [in] Reference to form whose components are to be enabled /
disabled.
}
begin
Assert(Assigned(Form), ClassName + '.Create: Form is nil');
inherited Create;
fForm := Form;
end;
procedure TControlStateMgr.Update;
{Updates state of form's components according to value of form's Enabled
property.
}
var
Idx: Integer; // loops through all components on form
Cmp: TComponent; // reference to each component
begin
for Idx := 0 to Pred(fForm.ComponentCount) do
begin
Cmp := fForm.Components[Idx];
if Cmp is TControl then
UpdateControl(Cmp as TControl)
else if Cmp is TMenuItem then
UpdateMenuItem(Cmp as TMenuItem)
else if Cmp is TActionList then
UpdateActionList(Cmp as TActionList);
end;
end;
procedure TControlStateMgr.UpdateActionList(const AL: TActionList);
{Updates actions in an action list.
@param AL [in] Action list.
}
var
Action: TContainedAction; // refers to each action in list
OldOnUpdate: TActionEvent; // stores action list's existing OnUpdate handler
begin
OldOnUpdate := AL.OnUpdate;
try
// use out own OnUpdate handler to update actions in list
AL.OnUpdate := ActionListUpdate;
for Action in AL do
AL.UpdateAction(Action)
finally
AL.OnUpdate := OldOnUpdate;
end;
end;
procedure TControlStateMgr.UpdateControl(const Ctrl: TControl);
{Updates a control if it has no associated action.
@param Ctrl [in] Control to be updated.
}
begin
if not Assigned(Ctrl.Action) then
Ctrl.Enabled := fForm.Enabled;
end;
procedure TControlStateMgr.UpdateMenuItem(const MI: TMenuItem);
{Updates a menu item if it has no action.
@param MI [in] Menu item to be updated.
}
begin
if not Assigned(MI.Action) then
MI.Enabled := fForm.Enabled;
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.