Extended & Expanded Summary of GridEdit.pas
The GridEdit.pas
unit defines a TGridEditFrame, a custom frame that provides a StringGrid enhanced with in-place numeric editing and a popup menu for quick data manipulations. Below is an in-depth look at its design, components, and functionality.
1. Purpose & Context
- Objective: Provide a specialized grid for editing numeric data.
- Key Feature: A
TNumEdit
control is placed over the selected cell, enabling direct numeric entry (instead of the standard text-based in-place editor). - Additional Tools: A PopupMenu offers editing shortcuts—Cut, Copy, Paste, Insert, and Delete—enabling quick table data manipulations.
2. Core Components & Their Roles
-
TGridEditFrame
(Frame):- The main container that holds:
- A
TStringGrid
(Grid
) for data display. - A
TPanel
(EditPanel
) housing aTNumEdit
control (EditBox
) for numeric editing. - A
TPopupMenu
with menu items for cut/copy/paste/insert/delete functions.
- A
- Publishes a few key properties:
AllowInsert
: Whether new rows can be added.CenterHeaders
: Whether header cells are centered.Modified
: Tracks if data changed.
- The main container that holds:
-
Grid: TStringGrid
:- Displays rows/columns of textual data (cells).
- Some columns or rows may represent numeric data that can be edited in numeric form.
- Integrates event handlers to open the numeric editor, handle key presses, and manage copy/paste operations.
-
EditPanel: TPanel
&EditBox: TNumEdit
:- The numeric edit box (a
TNumEdit
fromNumEdit.pas
) is placed on top of a panel, which is then located to overlap the selected grid cell when editing is triggered. - Hidden by default; becomes visible only during cell editing.
- Accepts only numeric input (supports decimal, negative or positive, depending on configuration).
- The numeric edit box (a
-
PopupMenu
with items:- Cut / Copy / Paste: Standard text-based operations for selected cells.
- Insert / Delete: Insert or remove data or rows in the grid.
3. Editing Mechanics
-
Initiating Edit:
- When user presses Enter or starts typing in a cell, the
ShowEditor
method:- Decides if the user wants to override the cell’s old text or append new input.
- Moves the
EditPanel
(withEditBox
) over that cell. - Sets visibility to
true
and passes the initial keystroke onward toEditBox
.
- When user presses Enter or starts typing in a cell, the
-
Handling Key Presses in
EditBox
:- Enter Key (
#13
):- Finishes edit →
EditBoxExit
is called → commits changes to the grid. - Optionally moves the active cell to the right if
AutoMove
istrue
.
- Finishes edit →
- Escape Key (
#27
):- Cancels the edit (reverts to old cell text).
- Hides the panel.
- Arrow Keys (
VK_UP
/VK_DOWN
):- Moves the selection up/down in the grid, ending current edit session.
- Enter Key (
-
EditBoxExit
:- Occurs when user leaves the editing control (either by key action or clicking away).
- The typed value in
EditBox
is assigned to the cell. If changed,Modified
is settrue
. - The edit panel is hidden.
-
GoAutoMove
:- If AutoMove is enabled and the user pressed Enter, the focus moves to the next cell (right column or next row if at row’s end).
- This simulates how spreadsheet applications often behave.
4. Popup Menu Operations
Popup provides typical data manipulations:
- Cut:
- Calls CopyCells, then ClearCells on the selected range.
- Copy:
- Extracts selected cells into a string list separated by tabs, places them on the Clipboard.
- Paste:
- Reads text from the Clipboard, splits it by rows/tabs, and places it into the grid starting at the current selection.
- If the text extends beyond current row capacity, the grid can grow row count as needed.
- Insert:
- Similar to Paste, but first calculates how many lines to insert, calls InsertRows, then does a normal PasteCells operation.
- Insert Row:
- Adds a single row below the currently selected row.
- Delete Row:
- Removes the current row from the grid, shifting subsequent rows up.
5. Implementation Highlights
- Grid Row & Col:
- The component sets
FixedCols
(often 1) if row indexing is used. - The user’s selected region is known through
Grid.Selection
.
- The component sets
- Clipboard Format:
- All data is stored as text. For multiple rows, each row ends with a CRLF, and columns within a row are separated by a TAB.
- Row Counting (
CountRows
):- For Insert operation, code counts how many line breaks occur in the copied text to decide how many new rows to insert.
6. Key Methods & Their Responsibilities
-
ShowEditor(Key: Char)
:- Activates numeric
EditBox
, re-initializes the text (blank if user typed a char, or old cell contents if the user pressed Enter). - Positions the panel over the cell.
- Activates numeric
-
SetEditBounds
:- Calculates the exact cell rectangle in the
Grid
and setsEditPanel
andEditBox
to overlay that cell.
- Calculates the exact cell rectangle in the
-
EditBoxKeyPress
:- Interprets Enter (finish edit → commit) or Escape (cancel edit → revert).
-
MenuItemClick(Sender: TObject)
:- The central handler for popup actions. Distinguishes Cut, Copy, Paste, Insert, etc., by the menu item’s
Tag
.
- The central handler for popup actions. Distinguishes Cut, Copy, Paste, Insert, etc., by the menu item’s
-
PasteCells
:- Splits the multiline string from Clipboard by row (CR or CRLF) and by column (TAB).
- Fills the cells sequentially, extending row count if needed.
-
InsertRows(Nrows: Integer)
:- Makes space by shifting existing row data downward, leaving blank rows for new content at the top.
-
DeleteOneRow
:- Removes the user’s current row from the grid.
7. Public Properties
AllowInsert
: Toggles whether Insert/Paste adds new rows or not.CenterHeaders
: Iftrue
, the top row cells get centered text.Modified
: Read-only boolean that indicates changes since creation/last set.
8. Typical Usage Scenario
- Drop a
TGridEditFrame
onto a form, setAllowInsert
totrue
if you want row insertion. - Populate the
Grid
with data or set row/column counts. - Run your app. The user can:
- Click or start typing in a numeric cell → an in-place numeric edit box appears.
- Use arrow keys or Enter to move around cells.
- Right-click to open the popup menu → cut/copy/paste/insert/delete.
- Data changes set
Modified = true
.
9. Summary
GridEdit.pas
extends the standard TStringGrid
with numeric editing (through a TNumEdit
) and a context menu for data manipulation. This design is particularly handy for editing numeric arrays or parameter sets, providing row insertion/deletion logic, a consistent numeric input interface, and an Excel-like cut/copy/paste user experience.
No comments:
Post a Comment