Menu

[r3207]: / trunk / Src / FrCategoryDescEdit.pas  Maximize  Restore  History

Download this file

261 lines (231 with data), 8.2 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
{
* 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) 2009-2013, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Implements a frame that accepts and validates a description for a snippet
* category.
}
unit FrCategoryDescEdit;
interface
uses
// Delphi
Forms, Controls, StdCtrls, Classes;
type
{
TCategoryDescEditFrame:
Frame that accepts and validates a description for a snippet category.
}
TCategoryDescEditFrame = class(TFrame)
edDescription: TEdit;
lblDescription: TLabel;
lblError: TLabel;
procedure edDescriptionChange(Sender: TObject);
procedure FrameEnter(Sender: TObject);
public
type
{
TDescriptionCheck:
Type of OnDescriptionCheck event handler.
@param Sender [in] Object that triggered event.
@param Desc [in] Description being checked.
@param Valid [in/out] When called Valid indicates whether the caller
has the decided the description is valid or not. Handler can change
the value to override the decision.
}
TDescriptionCheck = procedure(Sender: TObject; const Desc: string;
var Valid: Boolean) of object;
strict private
fOnChange: TNotifyEvent; // OnChange event handler
fOnCheckDescription: TDescriptionCheck; // OnCheckDescription event handler
function CategoryDescExists(const Desc: string): Boolean;
{Checks if a category with a specified description exists. Case is
ignored.
@param Desc [in] Required description.
@return True if a category exists with this description, False if not.
}
procedure UpdateControls;
{Updates state of controls depending on current entries in frame.
}
function IsValidDescription: Boolean;
{Checks if current content of description edit box is valid. Invalid
content causes an error message to be displayed. Triggers the
OnCheckDescription event to permit the owner to override the decision
about validity.
@return True if description is valid, False if not.
}
procedure DoChange;
{Triggers OnChange event.
}
function GetDescription: string;
{Read accessor for Description property.
@return Current description from edit control.
}
procedure SetDescription(const Value: string);
{Write accessor for Description property.
@param Value [in] New description to be displayed in edit control.
}
function GetPrompt: string;
{Read accessor for Prompt property.
@return Current prompt text.
}
procedure SetPrompt(const Value: string);
{Write accessor for Prompt property.
@param Value [in] New prompt text to display.
}
public
constructor Create(AOwner: TComponent); override;
{Class constructor. Sets up object.
@param AOwner [in] Component that owns this frame.
}
procedure ArrangeFrame;
{Arranges controls in frame and sizes it to fit the controls.
}
function IsValidEntry: Boolean;
{Checks if data entered in frame is valid.
@return True if entry is valid, False if not.
}
property Prompt: string read GetPrompt write SetPrompt;
{Text that is displayed above description edit control}
property Description: string read GetDescription write SetDescription;
{Description currently displayed in description edit control, stripped of
leading and trailing spaces}
property OnChange: TNotifyEvent read fOnChange write fOnChange;
{Event triggered when content of description edit control changes}
property OnCheckDescription: TDescriptionCheck
read fOnCheckDescription write fOnCheckDescription;
{Event triggered when validity of description is being checked. Provides
information about the description being checked and whether it is
considered valid. Handler can override validity}
end;
implementation
uses
// Delphi
Windows {for inlining},
// Project
DB.UCategory, DB.UMain, UColours, UCtrlArranger, UFontHelper, UStrUtils;
{$R *.dfm}
{ TCategoryDescEditFrame }
procedure TCategoryDescEditFrame.ArrangeFrame;
{Arranges controls in frame and sizes it to fit the controls.
}
begin
TCtrlArranger.SetLabelHeights(Self);
edDescription.Top := TCtrlArranger.BottomOf(lblDescription, 4);
lblError.Top := TCtrlArranger.BottomOf(edDescription, 4);
Self.ClientHeight := TCtrlArranger.TotalControlHeight(Self);
Self.ClientWidth := TCtrlArranger.TotalControlWidth(Self);
end;
function TCategoryDescEditFrame.CategoryDescExists(const Desc: string): Boolean;
{Checks if a category with a specified description exists. Case is ignored.
@param Desc [in] Required description.
@return True if a category exists with this description, False if not.
}
var
Cat: TCategory; // each category in database
begin
Result := False;
for Cat in Database.Categories do
if StrSameText(Desc, Cat.Description) then
begin
Result := True;
Break;
end;
end;
constructor TCategoryDescEditFrame.Create(AOwner: TComponent);
{Class constructor. Sets up object.
@param AOwner [in] Component that owns this frame.
}
begin
inherited;
lblError.Font.Color := clWarningText;
TFontHelper.SetDefaultBaseFont(lblError.Font);
UpdateControls;
end;
procedure TCategoryDescEditFrame.DoChange;
{Triggers OnChange event.
}
begin
if Assigned(fOnChange) then
fOnChange(Self);
end;
procedure TCategoryDescEditFrame.edDescriptionChange(Sender: TObject);
{Handles OnChange events in description edit control. Updates control state
and triggers frame's OnChange event.
@param Sender [in] Not used.
}
begin
UpdateControls;
DoChange;
end;
procedure TCategoryDescEditFrame.FrameEnter(Sender: TObject);
{Handles frame's OnEnter event. Sets focus on description edit control.
@param Sender [in] Not used.
}
begin
edDescription.SetFocus;
end;
function TCategoryDescEditFrame.GetDescription: string;
{Read accessor for Description property.
@return Current description from edit control.
}
begin
Result := StrTrim(edDescription.Text);
end;
function TCategoryDescEditFrame.GetPrompt: string;
{Read accessor for Prompt property.
@return Current prompt text.
}
begin
Result := lblDescription.Caption;
end;
function TCategoryDescEditFrame.IsValidDescription: Boolean;
{Checks if current content of description edit box is valid. Invalid content
causes an error message to be displayed. Triggers the OnCheckDescription event
to permit the owner to override the decision about validity.
@return True if description is valid, False if not.
}
begin
// Valid content is either the empty (or all white space) string or a
// description that is not used by any category other.
Result := (Description = '') or not CategoryDescExists(Description);
if Assigned(fOnCheckDescription) then
fOnCheckDescription(Self, Description, Result);
end;
function TCategoryDescEditFrame.IsValidEntry: Boolean;
{Checks if data entered in frame is valid.
@return True if entry is valid, False if not.
}
begin
// To be valid, Description must be non-empty and not the description of
// another category in the database.
Result := (Description <> '') and IsValidDescription;
end;
procedure TCategoryDescEditFrame.SetDescription(const Value: string);
{Write accessor for Description property.
@param Value [in] New description to be displayed in edit control.
}
begin
edDescription.Text := Value;
UpdateControls;
DoChange;
end;
procedure TCategoryDescEditFrame.SetPrompt(const Value: string);
{Write accessor for Prompt property.
@param Value [in] New prompt text to display.
}
begin
lblDescription.Caption := Value;
end;
procedure TCategoryDescEditFrame.UpdateControls;
{Updates state of controls depending on current entries in frame.
}
begin
lblError.Visible := not IsValidDescription;
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.