home *** CD-ROM | disk | FTP | other *** search
/ PC World Plus! (NZ) 2001 June / HDC50.iso / Runimage / Delphi50 / Doc / GRIDS.INT < prev    next >
Text File  |  1999-08-11  |  21KB  |  505 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,99 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit Grids;
  11.  
  12. {$R-,T-,H+,X+}
  13.  
  14. interface
  15.  
  16. uses Messages, Windows, SysUtils, Classes, Graphics, Menus, Controls, Forms,
  17.   StdCtrls, Mask;
  18.  
  19. const
  20.   MaxCustomExtents = MaxListSize;
  21.   MaxShortInt = High(ShortInt);
  22.  
  23. type
  24.   EInvalidGridOperation = class(Exception);
  25.  
  26.   { Internal grid types }
  27.   TGetExtentsFunc = function(Index: Longint): Integer of object;
  28.  
  29.   TGridAxisDrawInfo = record
  30.     EffectiveLineWidth: Integer;
  31.     FixedBoundary: Integer;
  32.     GridBoundary: Integer;
  33.     GridExtent: Integer;
  34.     LastFullVisibleCell: Longint;
  35.     FullVisBoundary: Integer;
  36.     FixedCellCount: Integer;
  37.     FirstGridCell: Integer;
  38.     GridCellCount: Integer;
  39.     GetExtent: TGetExtentsFunc;
  40.   end;
  41.  
  42.   TGridDrawInfo = record
  43.     Horz, Vert: TGridAxisDrawInfo;
  44.   end;
  45.  
  46.   TGridState = (gsNormal, gsSelecting, gsRowSizing, gsColSizing,
  47.     gsRowMoving, gsColMoving);
  48.   TGridMovement = gsRowMoving..gsColMoving;
  49.  
  50.   { TInplaceEdit }
  51.   { The inplace editor is not intended to be used outside the grid }
  52.  
  53.   TCustomGrid = class;
  54.  
  55.   TInplaceEdit = class(TCustomMaskEdit)
  56.   protected
  57.     procedure CreateParams(var Params: TCreateParams); override;
  58.     procedure DblClick; override;
  59.     function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;
  60.       MousePos: TPoint): Boolean; override;
  61.     function EditCanModify: Boolean; override;
  62.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  63.     procedure KeyPress(var Key: Char); override;
  64.     procedure KeyUp(var Key: Word; Shift: TShiftState); override;
  65.     procedure BoundsChanged; virtual;
  66.     procedure UpdateContents; virtual;
  67.     procedure WndProc(var Message: TMessage); override;
  68.     property  Grid: TCustomGrid;
  69.   public
  70.     constructor Create(AOwner: TComponent); override;
  71.     procedure Deselect;
  72.     procedure Hide;
  73.     procedure Invalidate; reintroduce;
  74.     procedure Move(const Loc: TRect);
  75.     function PosEqual(const Rect: TRect): Boolean;
  76.     procedure SetFocus; reintroduce;
  77.     procedure UpdateLoc(const Loc: TRect);
  78.     function Visible: Boolean;
  79.   end;
  80.  
  81.   { TCustomGrid }
  82.  
  83.   { TCustomGrid is an abstract base class that can be used to implement
  84.     general purpose grid style controls.  The control will call DrawCell for
  85.     each of the cells allowing the derived class to fill in the contents of
  86.     the cell.  The base class handles scrolling, selection, cursor keys, and
  87.     scrollbars.
  88.       DrawCell
  89.         Called by Paint. If DefaultDrawing is true the font and brush are
  90.         intialized to the control font and cell color.  The cell is prepainted
  91.         in the cell color and a focus rect is drawn in the focused cell after
  92.         DrawCell returns.  The state passed will reflect whether the cell is
  93.         a fixed cell, the focused cell or in the selection.
  94.       SizeChanged
  95.         Called when the size of the grid has changed.
  96.       BorderStyle
  97.         Allows a single line border to be drawn around the control.
  98.       Col
  99.         The current column of the focused cell (runtime only).
  100.       ColCount
  101.         The number of columns in the grid.
  102.       ColWidths
  103.         The width of each column (up to a maximum MaxCustomExtents, runtime
  104.         only).
  105.       DefaultColWidth
  106.         The default column width.  Changing this value will throw away any
  107.         customization done either visually or through ColWidths.
  108.       DefaultDrawing
  109.         Indicates whether the Paint should do the drawing talked about above in
  110.         DrawCell.
  111.       DefaultRowHeight
  112.         The default row height.  Changing this value will throw away any
  113.         customization done either visually or through RowHeights.
  114.       FixedCols
  115.         The number of non-scrolling columns.  This value must be at least one
  116.         below ColCount.
  117.       FixedRows
  118.         The number of non-scrolling rows.  This value must be at least one
  119.         below RowCount.
  120.       GridLineWidth
  121.         The width of the lines drawn between the cells.
  122.       LeftCol
  123.         The index of the left most displayed column (runtime only).
  124.       Options
  125.         The following options are available:
  126.           goFixedHorzLine:     Draw horizontal grid lines in the fixed cell area.
  127.           goFixedVertLine:     Draw veritical grid lines in the fixed cell area.
  128.           goHorzLine:          Draw horizontal lines between cells.
  129.           goVertLine:          Draw vertical lines between cells.
  130.           goRangeSelect:       Allow a range of cells to be selected.
  131.           goDrawFocusSelected: Draw the focused cell in the selected color.
  132.           goRowSizing:         Allows rows to be individually resized.
  133.           goColSizing:         Allows columns to be individually resized.
  134.           goRowMoving:         Allows rows to be moved with the mouse
  135.           goColMoving:         Allows columns to be moved with the mouse.
  136.           goEditing:           Places an edit control over the focused cell.
  137.           goAlwaysShowEditor:  Always shows the editor in place instead of
  138.                                waiting for a keypress or F2 to display it.
  139.           goTabs:              Enables the tabbing between columns.
  140.           goRowSelect:         Selection and movement is done a row at a time.
  141.       Row
  142.         The row of the focused cell (runtime only).
  143.       RowCount
  144.         The number of rows in the grid.
  145.       RowHeights
  146.         The hieght of each row (up to a maximum MaxCustomExtents, runtime
  147.         only).
  148.       ScrollBars
  149.         Determines whether the control has scrollbars.
  150.       Selection
  151.         A TGridRect of the current selection.
  152.       TopLeftChanged
  153.         Called when the TopRow or LeftCol change.
  154.       TopRow
  155.         The index of the top most row displayed (runtime only)
  156.       VisibleColCount
  157.         The number of columns fully displayed.  There could be one more column
  158.         partially displayed.
  159.       VisibleRowCount
  160.         The number of rows fully displayed.  There could be one more row
  161.         partially displayed.
  162.  
  163.     Protected members, for implementors of TCustomGrid descendents
  164.       DesignOptionBoost
  165.         Options mixed in only at design time to aid design-time editing.
  166.         Default = [goColSizing, goRowSizing], which makes grid cols and rows
  167.         resizeable at design time, regardless of the Options settings.
  168.       VirtualView
  169.         Controls the use of maximum screen clipping optimizations when the
  170.         grid window changes size.  Default = False, which means only the
  171.         area exposed by the size change will be redrawn, for less flicker.
  172.         VirtualView = True means the entire data area of the grid is redrawn
  173.         when the size changes.  This is required when the data displayed in
  174.         the grid is not bound to the number of rows or columns in the grid,
  175.         such as the dbgrid (a few grid rows displaying a view onto a million
  176.         row table).
  177.      }
  178.  
  179.   TGridOption = (goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
  180.     goRangeSelect, goDrawFocusSelected, goRowSizing, goColSizing, goRowMoving,
  181.     goColMoving, goEditing, goTabs, goRowSelect,
  182.     goAlwaysShowEditor, goThumbTracking);
  183.   TGridOptions = set of TGridOption;
  184.   TGridDrawState = set of (gdSelected, gdFocused, gdFixed);
  185.   TGridScrollDirection = set of (sdLeft, sdRight, sdUp, sdDown);
  186.  
  187.   TGridCoord = record
  188.     X: Longint;
  189.     Y: Longint;
  190.   end;
  191.  
  192.   TGridRect = record
  193.     case Integer of
  194.       0: (Left, Top, Right, Bottom: Longint);
  195.       1: (TopLeft, BottomRight: TGridCoord);
  196.   end;
  197.  
  198.   TSelectCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
  199.     var CanSelect: Boolean) of object;
  200.   TDrawCellEvent = procedure (Sender: TObject; ACol, ARow: Longint;
  201.     Rect: TRect; State: TGridDrawState) of object;
  202.  
  203.   TCustomGrid = class(TCustomControl)
  204.   protected
  205.     FGridState: TGridState;
  206.     FSaveCellExtents: Boolean;
  207.     DesignOptionsBoost: TGridOptions;
  208.     VirtualView: Boolean;
  209.     procedure CalcDrawInfo(var DrawInfo: TGridDrawInfo);
  210.     procedure CalcFixedInfo(var DrawInfo: TGridDrawInfo);
  211.     procedure CalcSizingState(X, Y: Integer; var State: TGridState;
  212.       var Index: Longint; var SizingPos, SizingOfs: Integer;
  213.       var FixedInfo: TGridDrawInfo); virtual;
  214.     function CreateEditor: TInplaceEdit; virtual;
  215.     procedure CreateParams(var Params: TCreateParams); override;
  216.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  217.     procedure KeyPress(var Key: Char); override;
  218.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  219.       X, Y: Integer); override;
  220.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  221.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  222.       X, Y: Integer); override;
  223.     procedure AdjustSize(Index, Amount: Longint; Rows: Boolean); reintroduce; dynamic;
  224.     function BoxRect(ALeft, ATop, ARight, ABottom: Longint): TRect;
  225.     procedure DoExit; override;
  226.     function CellRect(ACol, ARow: Longint): TRect;
  227.     function CanEditAcceptKey(Key: Char): Boolean; dynamic;
  228.     function CanGridAcceptKey(Key: Word; Shift: TShiftState): Boolean; dynamic;
  229.     function CanEditModify: Boolean; dynamic;
  230.     function CanEditShow: Boolean; virtual;
  231.     function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override;
  232.     function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override;
  233.     function GetEditText(ACol, ARow: Longint): string; dynamic;
  234.     procedure SetEditText(ACol, ARow: Longint; const Value: string); dynamic;
  235.     function GetEditMask(ACol, ARow: Longint): string; dynamic;
  236.     function GetEditLimit: Integer; dynamic;
  237.     function GetGridWidth: Integer;
  238.     function GetGridHeight: Integer;
  239.     procedure HideEditor;
  240.     procedure ShowEditor;
  241.     procedure ShowEditorChar(Ch: Char);
  242.     procedure InvalidateEditor;
  243.     procedure MoveColumn(FromIndex, ToIndex: Longint);
  244.     procedure ColumnMoved(FromIndex, ToIndex: Longint); dynamic;
  245.     procedure MoveRow(FromIndex, ToIndex: Longint);
  246.     procedure RowMoved(FromIndex, ToIndex: Longint); dynamic;
  247.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  248.       AState: TGridDrawState); virtual; abstract;
  249.     procedure DefineProperties(Filer: TFiler); override;
  250.     procedure MoveColRow(ACol, ARow: Longint; MoveAnchor, Show: Boolean);
  251.     function SelectCell(ACol, ARow: Longint): Boolean; virtual;
  252.     procedure SizeChanged(OldColCount, OldRowCount: Longint); dynamic;
  253.     function Sizing(X, Y: Integer): Boolean;
  254.     procedure ScrollData(DX, DY: Integer);
  255.     procedure InvalidateCell(ACol, ARow: Longint);
  256.     procedure InvalidateCol(ACol: Longint);
  257.     procedure InvalidateRow(ARow: Longint);
  258.     procedure TopLeftChanged; dynamic;
  259.     procedure TimedScroll(Direction: TGridScrollDirection); dynamic;
  260.     procedure Paint; override;
  261.     procedure ColWidthsChanged; dynamic;
  262.     procedure RowHeightsChanged; dynamic;
  263.     procedure DeleteColumn(ACol: Longint); virtual;
  264.     procedure DeleteRow(ARow: Longint); virtual;
  265.     procedure UpdateDesigner;
  266.     function BeginColumnDrag(var Origin, Destination: Integer;
  267.       const MousePt: TPoint): Boolean; dynamic;
  268.     function BeginRowDrag(var Origin, Destination: Integer;
  269.       const MousePt: TPoint): Boolean; dynamic;
  270.     function CheckColumnDrag(var Origin, Destination: Integer;
  271.       const MousePt: TPoint): Boolean; dynamic;
  272.     function CheckRowDrag(var Origin, Destination: Integer;
  273.       const MousePt: TPoint): Boolean; dynamic;
  274.     function EndColumnDrag(var Origin, Destination: Integer;
  275.       const MousePt: TPoint): Boolean; dynamic;
  276.     function EndRowDrag(var Origin, Destination: Integer;
  277.       const MousePt: TPoint): Boolean; dynamic;
  278.     property BorderStyle: TBorderStyle default bsSingle;
  279.     property Col: Longint;
  280.     property Color default clWindow;
  281.     property ColCount: Longint default 5;
  282.     property ColWidths[Index: Longint]: Integer;
  283.     property DefaultColWidth: Integer default 64;
  284.     property DefaultDrawing: Boolean default True;
  285.     property DefaultRowHeight: Integer default 24;
  286.     property EditorMode: Boolean;
  287.     property FixedColor: TColor default clBtnFace;
  288.     property FixedCols: Integer default 1;
  289.     property FixedRows: Integer default 1;
  290.     property GridHeight: Integer;
  291.     property GridLineWidth: Integer default 1;
  292.     property GridWidth: Integer;
  293.     property HitTest: TPoint;
  294.     property InplaceEditor: TInplaceEdit;
  295.     property LeftCol: Longint;
  296.     property Options: TGridOptions default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect];
  297.     property ParentColor default False;
  298.     property Row: Longint;
  299.     property RowCount: Longint default 5;
  300.     property RowHeights[Index: Longint]: Integer;
  301.     property ScrollBars: TScrollStyle default ssBoth;
  302.     property Selection: TGridRect;
  303.     property TabStops[Index: Longint]: Boolean;
  304.     property TopRow: Longint;
  305.     property VisibleColCount: Integer;
  306.     property VisibleRowCount: Integer;
  307.   public
  308.     constructor Create(AOwner: TComponent); override;
  309.     destructor Destroy; override;
  310.     function MouseCoord(X, Y: Integer): TGridCoord;
  311.   published
  312.     property TabStop default True;
  313.   end;
  314.  
  315.   { TDrawGrid }
  316.  
  317.   { A grid relies on the OnDrawCell event to display the cells.
  318.      CellRect
  319.        This method returns control relative screen coordinates of the cell or
  320.        an empty rectangle if the cell is not visible.
  321.      EditorMode
  322.        Setting to true shows the editor, as if the F2 key was pressed, when
  323.        goEditing is turned on and goAlwaysShowEditor is turned off.
  324.      MouseToCell
  325.        Takes control relative screen X, Y location and fills in the column and
  326.        row that contain that point.
  327.      OnColumnMoved
  328.        Called when the user request to move a column with the mouse when
  329.        the goColMoving option is on.
  330.      OnDrawCell
  331.        This event is passed the same information as the DrawCell method
  332.        discussed above.
  333.      OnGetEditMask
  334.        Called to retrieve edit mask in the inplace editor when goEditing is
  335.        turned on.
  336.      OnGetEditText
  337.        Called to retrieve text to edit when goEditing is turned on.
  338.      OnRowMoved
  339.        Called when the user request to move a row with the mouse when
  340.        the goRowMoving option is on.
  341.      OnSetEditText
  342.        Called when goEditing is turned on to reflect changes to the text
  343.        made by the editor.
  344.      OnTopLeftChanged
  345.        Invoked when TopRow or LeftCol change. }
  346.  
  347.   TGetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; var Value: string) of object;
  348.   TSetEditEvent = procedure (Sender: TObject; ACol, ARow: Longint; const Value: string) of object;
  349.   TMovedEvent = procedure (Sender: TObject; FromIndex, ToIndex: Longint) of object;
  350.  
  351.   TDrawGrid = class(TCustomGrid)
  352.   protected
  353.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  354.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  355.       AState: TGridDrawState); override;
  356.     function GetEditMask(ACol, ARow: Longint): string; override;
  357.     function GetEditText(ACol, ARow: Longint): string; override;
  358.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  359.     function SelectCell(ACol, ARow: Longint): Boolean; override;
  360.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  361.     procedure TopLeftChanged; override;
  362.   public
  363.     function CellRect(ACol, ARow: Longint): TRect;
  364.     procedure MouseToCell(X, Y: Integer; var ACol, ARow: Longint);
  365.     property Canvas;
  366.     property Col;
  367.     property ColWidths;
  368.     property EditorMode;
  369.     property GridHeight;
  370.     property GridWidth;
  371.     property LeftCol;
  372.     property Selection;
  373.     property Row;
  374.     property RowHeights;
  375.     property TabStops;
  376.     property TopRow;
  377.   published
  378.     property Align;
  379.     property Anchors;
  380.     property BiDiMode;
  381.     property BorderStyle;
  382.     property Color;
  383.     property ColCount;
  384.     property Constraints;
  385.     property Ctl3D;
  386.     property DefaultColWidth;
  387.     property DefaultRowHeight;
  388.     property DefaultDrawing;
  389.     property DragCursor;
  390.     property DragKind;
  391.     property DragMode;
  392.     property Enabled;
  393.     property FixedColor;
  394.     property FixedCols;
  395.     property RowCount;
  396.     property FixedRows;
  397.     property Font;
  398.     property GridLineWidth;
  399.     property Options;
  400.     property ParentBiDiMode;
  401.     property ParentColor;
  402.     property ParentCtl3D;
  403.     property ParentFont;
  404.     property ParentShowHint;
  405.     property PopupMenu;
  406.     property ScrollBars;
  407.     property ShowHint;
  408.     property TabOrder;
  409.     property TabStop;
  410.     property Visible;
  411.     property VisibleColCount;
  412.     property VisibleRowCount;
  413.     property OnClick;
  414.     property OnColumnMoved: TMovedEvent;
  415.     property OnContextPopup;
  416.     property OnDblClick;
  417.     property OnDragDrop;
  418.     property OnDragOver;
  419.     property OnDrawCell: TDrawCellEvent;
  420.     property OnEndDock;
  421.     property OnEndDrag;
  422.     property OnEnter;
  423.     property OnExit;
  424.     property OnGetEditMask: TGetEditEvent;
  425.     property OnGetEditText: TGetEditEvent;
  426.     property OnKeyDown;
  427.     property OnKeyPress;
  428.     property OnKeyUp;
  429.     property OnMouseDown;
  430.     property OnMouseMove;
  431.     property OnMouseUp;
  432.     property OnMouseWheelDown;
  433.     property OnMouseWheelUp;
  434.     property OnRowMoved: TMovedEvent;
  435.     property OnSelectCell: TSelectCellEvent;
  436.     property OnSetEditText: TSetEditEvent;
  437.     property OnStartDock;
  438.     property OnStartDrag;
  439.     property OnTopLeftChanged: TNotifyEvent;
  440.   end;
  441.  
  442.   { TStringGrid }
  443.  
  444.   { TStringGrid adds to TDrawGrid the ability to save a string and associated
  445.     object (much like TListBox).  It also adds to the DefaultDrawing the drawing
  446.     of the string associated with the current cell.
  447.       Cells
  448.         A ColCount by RowCount array of strings which are associated with each
  449.         cell.  By default, the string is drawn into the cell before OnDrawCell
  450.         is called.  This can be turned off (along with all the other default
  451.         drawing) by setting DefaultDrawing to false.
  452.       Cols
  453.         A TStrings object that contains the strings and objects in the column
  454.         indicated by Index.  The TStrings will always have a count of RowCount.
  455.         If a another TStrings is assigned to it, the strings and objects beyond
  456.         RowCount are ignored.
  457.       Objects
  458.         A ColCount by Rowcount array of TObject's associated with each cell.
  459.         Object put into this array will *not* be destroyed automatically when
  460.         the grid is destroyed.
  461.       Rows
  462.         A TStrings object that contains the strings and objects in the row
  463.         indicated by Index.  The TStrings will always have a count of ColCount.
  464.         If a another TStrings is assigned to it, the strings and objects beyond
  465.         ColCount are ignored. }
  466.  
  467.   TStringGrid = class;
  468.  
  469.   TStringGridStrings = class(TStrings)
  470.   protected
  471.     function Get(Index: Integer): string; override;
  472.     function GetCount: Integer; override;
  473.     function GetObject(Index: Integer): TObject; override;
  474.     procedure Put(Index: Integer; const S: string); override;
  475.     procedure PutObject(Index: Integer; AObject: TObject); override;
  476.     procedure SetUpdateState(Updating: Boolean); override;
  477.   public
  478.     constructor Create(AGrid: TStringGrid; AIndex: Longint);
  479.     function Add(const S: string): Integer; override;
  480.     procedure Assign(Source: TPersistent); override;
  481.     procedure Clear; override;
  482.     procedure Delete(Index: Integer); override;
  483.     procedure Insert(Index: Integer; const S: string); override;
  484.   end;
  485.  
  486.  
  487.   TStringGrid = class(TDrawGrid)
  488.   protected
  489.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  490.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
  491.       AState: TGridDrawState); override;
  492.     function GetEditText(ACol, ARow: Longint): string; override;
  493.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  494.     procedure RowMoved(FromIndex, ToIndex: Longint); override;
  495.   public
  496.     constructor Create(AOwner: TComponent); override;
  497.     destructor Destroy; override;
  498.     property Cells[ACol, ARow: Integer]: string;
  499.     property Cols[Index: Integer]: TStrings;
  500.     property Objects[ACol, ARow: Integer]: TObject;
  501.     property Rows[Index: Integer]: TStrings;
  502.   end;
  503.  
  504. implementation
  505.