home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk14 / doc.pak / GRIDS.INT < prev    next >
Encoding:
Text File  |  1995-08-24  |  16.1 KB  |  400 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Delphi Visual Component Library                 }
  4. {                                                       }
  5. {       Copyright (c) 1995 Borland International        }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit Grids;
  10.  
  11. interface
  12.  
  13. uses SysUtils, Messages, WinTypes, Classes, Graphics, Menus, Controls, Forms,
  14.   StdCtrls, Mask;
  15.  
  16. const
  17.   MaxCustomExtents = 65520 div SizeOf(Integer);
  18.  
  19. type
  20.   EInvalidGridOperation = class(Exception);
  21.  
  22.   { Internal grid types }
  23.  
  24.   { TInplaceEdit }
  25.   { The inplace editor is not intended to be used outside the grid }
  26.  
  27.   TCustomGrid = class;
  28.  
  29.   TInplaceEdit = class(TCustomMaskEdit)
  30.   protected
  31.     procedure CreateParams(var Params: TCreateParams); override;
  32.     procedure DblClick; override;
  33.     function EditCanModify: Boolean; override;
  34.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  35.     procedure KeyPress(var Key: Char); override;
  36.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  37.     procedure WndProc(var Message: TMessage); override;
  38.   public
  39.     constructor Create(AOwner: TComponent);
  40.     procedure Deselect;
  41.     procedure Hide;
  42.     procedure Invalidate;
  43.     procedure Move(const Loc: TRect);
  44.     function PosEqual(const Rect: TRect): Boolean;
  45.     procedure SetFocus;
  46.     procedure UpdateLoc(const Loc: TRect);
  47.     function Visible: Boolean;
  48.   end;
  49.  
  50.   { TCustomGrid }
  51.  
  52.   { TCustomGrid is an abstract base class that can be used to implement
  53.     general purpose grid style controls.  The control will call DrawCell for
  54.     each of the cells allowing the derived class to fill in the contents of
  55.     the cell.  The base class handles scrolling, selection, cursor keys, and
  56.     scrollbars.
  57.       DrawCell
  58.         Called by Paint. If DefaultDrawing is true the font and brush are
  59.         intialized to the control font and cell color.  The cell is prepainted
  60.         in the cell color and a focus rect is draw in the focused cell after
  61.         DrawCell returns.  The state passed will reflect whether the cell is
  62.         a fixed cell, the focused cell or in the selection.
  63.       SizeChanged
  64.         Called when the size of the grid has changed.
  65.       BorderStyle
  66.         Allows a single line border to be drawn around the control.
  67.       Col
  68.         The current column of the focused cell (runtime only).
  69.       ColCount
  70.         The number of columns in the grid.
  71.       ColWidths
  72.         The width of each column (up to a maximum MaxCustomExtents, runtime
  73.         only).
  74.       DefaultColWidth
  75.         The default column width.  Changing this value will throw away any
  76.         customization done either visually or through ColWidths.
  77.       DefaultDrawing
  78.         Indicates whether the Paint should do the drawing talked about above in
  79.         DrawCell.
  80.       DefaultRowHeight
  81.         The default row height.  Changing this value will throw away any
  82.         customization done either visually or through RowHeights.
  83.       FixedCols
  84.         The number of non-scrolling columns.  This value must be at least one
  85.         below ColCount.
  86.       FixedRows
  87.         The number of non-scrolling rows.  This value must be at least one
  88.         below RowCount.
  89.       GridLineWidth
  90.         The width of the lines drawn between the cells.
  91.       LeftCol
  92.         The index of the left most displayed column (runtime only).
  93.       Options
  94.         The following options are available:
  95.           goFixedHorzLine:     Draw horizontal grid lines in the fixed cell area.
  96.           goFixedVertLine:     Draw veritical grid lines in the fixed cell area.
  97.           goHorzLine:          Draw horizontal lines between cells.
  98.           goVertLine:          Draw vertical lines between cells.
  99.           goRangeSelect:       Allow a range of cells to be selected.
  100.           goDrawFocusSelected: Draw the focused cell in the selected color.
  101.           goRowSizing:         Allows rows to be individually resized.
  102.           goColSizing:         Allows columns to be individually resized.
  103.           goRowMoving:         Allows rows to be moved with the mouse
  104.           goColMoving:         Allows columns to be moved with the mouse.
  105.           goEditing:           Places an edit control over the focused cell.
  106.           goAlwaysShowEditor:  Always shows the editor in place instead of
  107.                                waiting for a keypress or F2 to display it.
  108.           goTabs:              Enables the tabbing between columns.
  109.           goRowSelect:         Selection and movement is done a row at a time.
  110.       Row
  111.         The row of the focused cell (runtime only).
  112.       RowCount
  113.         The number of rows in the grid.
  114.       RowHeights
  115.         The hieght of each row (up to a maximum MaxCustomExtents, runtime
  116.         only).
  117.       ScrollBars
  118.         Determines whether the control has scrollbars.
  119.       Selection
  120.         A TGridRect of the current selection.
  121.       TopLeftChanged
  122.         Called when the TopRow or LeftCol change.
  123.       TopRow
  124.         The index of the top most row displayed (runtime only)
  125.       VisibleColCount
  126.         The number of columns fully displayed.  There could be one more column
  127.         partially displayed.
  128.       VisibleRowCount
  129.         The number of rows fully displayed.  There could be one more row
  130.         partially displayed. }
  131.  
  132.   TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
  133.     goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
  134.     goColMoving, goEditing, goTabs, goRowSelect,
  135.     goAlwaysShowEditor, goThumbTracking);
  136.   TGridOptions = set of TGridOption;
  137.   TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
  138.   TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
  139.  
  140.   TGridCoord = record
  141.     X: Longint;
  142.     Y: Longint;
  143.   end;
  144.  
  145.   TGridRect = record
  146.     case Integer of
  147.       0: (Left, Top, Right, Bottom: Longint);
  148.       1: (TopLeft, BottomRight: TGridCoord);
  149.   end;
  150.  
  151.   TSelectCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  152.     var CanSelect: Boolean) of object;
  153.   TDrawCellEvent = procedure (Sender: TObject; Col, Row: Longint;
  154.     Rect: TRect; State: TGridDrawState) of object;
  155.  
  156.   TCustomGrid = class(TCustomControl)
  157.   protected
  158.     FSaveCellExtents: Boolean;
  159.     function CreateEditor: TInplaceEdit; virtual;
  160.     procedure CreateParams(var Params: TCreateParams); override;
  161.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  162.     procedure KeyPress(var Key: Char); override;
  163.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  164.       X, Y: Integer); override;
  165.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  166.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  167.       X, Y: Integer); override;
  168.     procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); dynamic;
  169.     function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
  170.     procedure DoExit; override;
  171.     function CellRect(ACol, ARow: Longint): TRect;
  172.     function CanEditAcceptKey(Key: Char): Boolean; dynamic;
  173.     function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
  174.     function CanEditModify: Boolean; dynamic;
  175.     function CanEditShow: Boolean; virtual;
  176.     function GetEditText(ACol, ARow: Longint): string; dynamic;
  177.     procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
  178.     function GetEditMask(ACol, ARow: Longint): string; dynamic;
  179.     function GetEditLimit: Integer; dynamic;
  180.     function GetGridWidth: Integer;
  181.     function GetGridHeight: Integer;
  182.     procedure HideEditor;
  183.     procedure ShowEditor;
  184.     procedure ShowEditorChar(Ch: Char);
  185.     procedure InvalidateEditor;
  186.     procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
  187.     procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
  188.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  189.       AState: TGridDrawState); virtual; abstract;
  190.     procedure DefineProperties(Filer: TFiler); override;
  191.     function MouseCoord(X, Y: Integer): TGridCoord;
  192.     procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
  193.     function SelectCell(ACol, ARow: Longint): Boolean; virtual;
  194.     procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
  195.     function Sizing(X, Y: Integer): Boolean;
  196.     procedure ScrollData(DX, DY: Integer);
  197.     procedure InvalidateCell(ACol, ARow: Longint);
  198.     procedure InvalidateRow(ARow: Longint);
  199.     procedure TopLeftChanged; dynamic;
  200.     procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
  201.     procedure Paint; override;
  202.     procedure ColWidthsChanged; dynamic;
  203.     procedure RowHeightsChanged; dynamic;
  204.     property BorderStyle: TBorderStyle default bsSingle;
  205.     property Col: Longint;
  206.     property Color default clWindow;
  207.     property ColCount: Longint default 5;
  208.     property ColWidths[Index: Longint]: Integer;
  209.     property DefaultColWidth: Integer default 64;
  210.     property DefaultDrawing: Boolean default True;
  211.     property DefaultRowHeight: Integer default 24;
  212.     property EditorMode: Boolean;
  213.     property FixedColor: TColor default clBtnFace;
  214.     property FixedCols: Integer default 1;
  215.     property FixedRows: Integer default 1;
  216.     property GridHeight: Integer;
  217.     property GridLineWidth: Integer default 1;
  218.     property GridWidth: Integer;
  219.     property InplaceEditor: TInplaceEdit;
  220.     property LeftCol: Longint;
  221.     property Options: TGridOptions default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect];
  222.     property ParentColor default False;
  223.     property Row: Longint;
  224.     property RowCount: Longint default 5;
  225.     property RowHeights[Index: Longint]: Integer;
  226.     property ScrollBars: TScrollStyle default ssBoth;
  227.     property Selection: TGridRect;
  228.     property TabStops[Index: Longint]: Boolean;
  229.     property TopRow: Longint;
  230.     property VisibleColCount: Integer;
  231.     property VisibleRowCount: Integer;
  232.   public
  233.     constructor Create(AOwner: TComponent); override;
  234.     destructor Destroy; override;
  235.   published
  236.     property TabStop default True;
  237.   end;
  238.  
  239.   { TDrawGrid }
  240.  
  241.   { A grid relies on the OnDrawCell event to display the cells.
  242.      CellRect
  243.        This method returns control relative screen coordinates of the cell or
  244.        an empty rectangle if the cell is not visible.
  245.      EditorMode
  246.        Setting to true shows the editor, as if the F2 key was pressed, when
  247.        goEditing is turned on and goAlwaysShowEditor is turned off.
  248.      MouseToCell
  249.        Takes control relative screen X, Y location and fills in the column and
  250.        row that contain that point.
  251.      OnColumnMoved
  252.        Called when the user request to move a column with the mouse when
  253.        the goColMoving option is on.
  254.      OnDrawCell
  255.        This event is passed the same information as the DrawCell method
  256.        discussed above.
  257.      OnGetEditMask
  258.        Called to retrieve edit mask in the inplace editor when goEditing is
  259.        turned on.
  260.      OnGetEditText
  261.        Called to retrieve text to edit when goEditing is turned on.
  262.      OnRowMoved
  263.        Called when the user request to move a row with the mouse when
  264.        the goRowMoving option is on.
  265.      OnSetEditText
  266.        Called when goEditing is turned on to reflect changes to the text
  267.        made by the editor.
  268.      OnTopLeftChanged
  269.        Invoked when TopRow or LeftCol change. }
  270.  
  271.   TGetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; var Value: string) of object;
  272.   TSetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; const Value: string) of object;
  273.   TMovedEvent = procedure (Sender: TObject; FromIndex, ToIndex: Longint) of object;
  274.  
  275.   TDrawGrid = class(TCustomGrid)
  276.   protected
  277.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  278.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  279.       AState: TGridDrawState); override;
  280.     function GetEditMask(ACol, ARow: Longint): string; override;
  281.     function GetEditText(ACol, ARow: Longint): string; override;
  282.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  283.     function SelectCell(ACol, ARow: Longint): Boolean; override;
  284.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  285.     procedure TopLeftChanged; override;
  286.   public
  287.     function CellRect(ACol, ARow: Longint): TRect;
  288.     procedure MouseToCell(X, Y: Integer; var ACol, ARow: Longint);
  289.     property Canvas;
  290.     property Col;
  291.     property ColWidths;
  292.     property EditorMode;
  293.     property GridHeight;
  294.     property GridWidth;
  295.     property LeftCol;
  296.     property Selection;
  297.     property Row;
  298.     property RowHeights;
  299.     property TabStops;
  300.     property TopRow;
  301.   published
  302.     property Align;
  303.     property BorderStyle;
  304.     property Color;
  305.     property ColCount;
  306.     property Ctl3D;
  307.     property DefaultColWidth;
  308.     property DefaultRowHeight;
  309.     property DefaultDrawing;
  310.     property DragCursor;
  311.     property DragMode;
  312.     property Enabled;
  313.     property FixedColor;
  314.     property FixedCols;
  315.     property FixedRows;
  316.     property Font;
  317.     property GridLineWidth;
  318.     property Options;
  319.     property ParentColor;
  320.     property ParentCtl3D;
  321.     property ParentFont;
  322.     property ParentShowHint;
  323.     property PopupMenu;
  324.     property RowCount;
  325.     property ScrollBars;
  326.     property ShowHint;
  327.     property TabOrder;
  328.     property TabStop;
  329.     property Visible;
  330.     property VisibleColCount;
  331.     property VisibleRowCount;
  332.     property OnClick;
  333.     property OnColumnMoved: TMovedEvent;
  334.     property OnDblClick;
  335.     property OnDragDrop;
  336.     property OnDragOver;
  337.     property OnDrawCell: TDrawCellEvent;
  338.     property OnEndDrag;
  339.     property OnEnter;
  340.     property OnExit;
  341.     property OnGetEditMask: TGetEditEvent;
  342.     property OnGetEditText: TGetEditEvent;
  343.     property OnKeyDown;
  344.     property OnKeyPress;
  345.     property OnKeyUp;
  346.     property OnMouseDown;
  347.     property OnMouseMove;
  348.     property OnMouseUp;
  349.     property OnRowMoved: TMovedEvent;
  350.     property OnSelectCell: TSelectCellEvent;
  351.     property OnSetEditText: TSetEditEvent;
  352.     property OnTopLeftChanged: TNotifyEvent;
  353.   end;
  354.  
  355.   { TStringGrid }
  356.  
  357.   { TStringGrid adds to TDrawGrid the ability to save a string and associated
  358.     object (much like TListBox).  It also adds to the DefaultDrawing the drawing
  359.     of the string associated with the current cell.
  360.       Cells
  361.         A ColCount by RowCount array of strings which are associated with each
  362.         cell.  By default, the string is drawn into the cell before OnDrawCell
  363.         is called.  This can be turned off (along with all the other default
  364.         drawing) by setting DefaultDrawing to false.
  365.       Cols
  366.         A TStrings object that contains the strings and objects in the column
  367.         indicated by Index.  The TStrings will always have a count of RowCount.
  368.         If a another TStrings is assigned to it, the strings and objects passed
  369.         RowCount are ignored.
  370.       Objects
  371.         A ColCount by Rowcount array of TObject's associated with each cell.
  372.         Object put into this array will *not* be destroyed automatically when
  373.         the grid is destroyed.
  374.       Rows
  375.         A TStrings object that contains the strings and objects in the row
  376.         indicated by Index.  The TStrings will always have a count of ColCount.
  377.         If a another TStrings is assigned to it, the strings and objects passed
  378.         ColCount are ignored. }
  379.  
  380.   TStringGrid = class;
  381.  
  382.   TStringGrid = class(TDrawGrid)
  383.   protected
  384.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  385.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  386.       AState: TGridDrawState); override;
  387.     function GetEditText(ACol, ARow: Longint): string; override;
  388.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  389.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  390.   public
  391.     constructor Create(AOwner: TComponent); override;
  392.     destructor Destroy; override;
  393.     property Cells[ACol, ARow: Integer]: string;
  394.     property Cols[Index: Integer]: TStrings;
  395.     property Objects[ACol, ARow: Integer]: TObject;
  396.     property Rows[Index: Integer]: TStrings;
  397.   end;
  398.  
  399. implementation
  400.