Below is an extended and expanded summary of the PropEdit.pas
code. It gives a high-level description of how the TPropEdit
component operates, the sub-components it creates, and how it manages the display and editing of property values.
1. Purpose and Overview
PropEdit.pas
defines a Delphi component namedTPropEdit
, which visually displays a set of properties in a two-column layout—property names on the left, and corresponding values on the right.- The component manages reading, displaying, and editing property values via specialized in-place editors (an
TEdit
for text or numeric input and aTComboBox
for list-based selections). - Each individual “property” is described by a
TPropRecord
structure, which specifies how the property is labeled and how it can be edited.
2. Relationship to Delphi UI
TPropEdit
derives fromTCustomPanel
and includes:- A header panel with two sub-panels for column headings,
- A splitter that allows resizing of the two column headers,
- A string grid (named
FGrid
) that contains rows for each property and two columns (property name / property value), - Two ephemeral editor controls: a normal edit control (
FEdit
) and a combo box (FCombo
)—both appear temporarily, placed over a cell to allow user input.
3. The TPropRecord Structure
Each property is defined by a record named TPropRecord
:
Name
: The user-facing name of the property (appears in the left column).Style
: One of the following edit styles:esHeading
: Not editable; used as a descriptive heading.esReadOnly
: The property displays a value that cannot be edited.esEdit
: User can type text or numbers in anTEdit
control.esComboList
: A drop-down list (fixed choices, no user text input).esComboEdit
: A drop-down list (but users can also edit or type new text).esButton
: Special style that displays a “button” (drawn within the cell) which triggers an external event.
Mask
: An edit mask that further restrictsesEdit
entry (e.g., numeric only, positive numeric, no spaces, or none).Length
: Maximum text length allowed in theesEdit
style.List
: For combo box styles, a string containing the possible choices (separated by#13
).
4. TPropEdit Sub-Components
When TPropEdit
is first created:
FGrid
(aTStringGrid
)- Two columns: property name (col 0) and property value (col 1).
- Allows for custom drawing of cells (
OnDrawCell
) so that headings, read-only cells, and button cells can be visually distinct. - Manages row selection, key navigation, etc.
- Header Panel / Sub-panels
- A top panel
FPanel1
that holds:FHeader1
(the left header label, i.e., property name heading),FSplitter
(the movable splitter),FHeader2
(the right header label, i.e., property value heading).
- The user can drag
FSplitter
to adjust how much width is given to each column.
- A top panel
FEdit
(TEdit
)- Invisible by default.
- Placed over a grid cell for in-place editing of textual/numeric properties.
- Restricts user input according to the property’s
Mask
(e.g., numeric only).
FCombo
(TComboBox
)- Invisible by default.
- Used instead of
FEdit
when the property’s style requires a drop-down list of choices (esComboList
/esComboEdit
). - The user can pick from a set of items or, in
esComboEdit
, can also type a custom value.
5. Property Editor Lifecycle
- Loading Properties
- The
SetProps
method is called with two parameters:- An array of
TPropRecord
(defining each property’s name, edit style, etc.), - A matching stringlist of initial values for these properties.
- An array of
TPropEdit
sets up theFGrid
row count to match the number of properties, placing each property’s name in col 0 and each initial value in col 1.
- The
- Display and Interaction
- The user sees each property name in the left column and the current value in the right column.
- If the property style is:
esHeading
: Shows only a label, merges with the left column cells, not editable.esReadOnly
: The user cannot edit the value.esEdit
: The user can click or press a key to invoke theFEdit
control.esComboList
/esComboEdit
: The user sees or can invoke aFCombo
drop-down for selecting or typing a value.esButton
: The property row shows a small “button” on the right. When clicked, it fires theOnButtonClick
event.
- Editing
- On a keystroke or double-click in a cell,
TPropEdit
:- Positions and displays either
FEdit
orFCombo
exactly over that cell. - The user modifies the text or picks from the list.
- On pressing Enter, or leaving the cell,
TPropEdit
hides the edit control.
- Positions and displays either
- The
OnValidate
event (if assigned) is called to check the new value. If valid,TPropEdit
updatesFGrid
and its internal stringlist. Otherwise, it can revert or show an error message.
- On a keystroke or double-click in a cell,
- Finishing Edits
- The “modified” flag is set if the user changes a property.
- The user or code calls
EndEditing
or sets focus away from the editor to finalize changes.
6. Event Handling
- Row selection is tracked, and
OnRowSelect
can be triggered whenever a new row is chosen. OnValidate
is where code can confirm or reject new values.OnButtonClick
is triggered for anesButton
style property row if a user clicks that property’s “button.”
7. Key Implementation Details
GoValidate
- Utility method that checks if a user’s new value is valid via an
OnValidate
callback. If it is valid, it updates the grid and the underlying stringlist. - If not valid, raises an exception or discards changes.
- Utility method that checks if a user’s new value is valid via an
- StringGrid Drawing
- The component customizes the grid appearance for headings, read-only cells, etc.
- For an
esButton
property, the right column draws a small button-like rectangle.
FHeaderSplit
- A user-defined percentage controlling the relative widths of property vs. value columns. The user can drag a splitter at runtime to change these columns’ widths.
SplitterMoved
updates the grid column widths accordingly.
FReadOnlyColor
,FValueColor
,FHeadBackColor
,FHeadForeColor
- Control the background/foreground colors of read-only property cells, value cells, and heading cells.
8. Typical Usage
- Create a
TPropEdit
component on a form. - Call
SetProps(PropertyArray, Values)
to populate the editor with property definitions and initial values. - Display the form, letting the user interact with the property table.
- Validation of changed values occurs through the
OnValidate
event. - Button property rows use the
OnButtonClick
event for specialized actions. - At the end, retrieve updates (if any) from the property editor via the underlying stringlist or by calling
GetProp
for each property.
9. Summary
TPropEdit
is a self-contained property editing panel. It organizes properties (described byTPropRecord
) in a grid.- It seamlessly offers different editing styles: read-only, heading, direct text input, combo box selection, or custom “button event.”
- By hooking up
OnValidate
andOnButtonClick
handlers, one can create a robust, small-scale “property inspector” tailored to Delphi applications. - It features column headings, color customizations, automatic resizing with a splitter, and row-based event callbacks.
In short, TPropEdit
gives a developer the means to present a property table to end-users with minimal overhead, while also allowing easy customization for validation and specialized event handling.
No comments:
Post a Comment