home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 October / tst.iso / programs / borland / RUNIMAGE / DELPHI40 / DOC / DBGRIDS.INT < prev    next >
Encoding:
Text File  |  1998-06-17  |  14.5 KB  |  375 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,98 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit DBGrids;
  11.  
  12. {$R-}
  13.  
  14. interface
  15.  
  16. uses Windows, SysUtils, Messages, Classes, Controls, Forms, StdCtrls,
  17.   Graphics, Grids, DBCtrls, Db, Menus, ImgList;
  18.  
  19. type
  20.   TColumnValue = (cvColor, cvWidth, cvFont, cvAlignment, cvReadOnly, cvTitleColor,
  21.     cvTitleCaption, cvTitleAlignment, cvTitleFont, cvImeMode, cvImeName);
  22.   TColumnValues = set of TColumnValue;
  23.  
  24. const
  25.   ColumnTitleValues = [cvTitleColor..cvTitleFont];
  26.   cm_DeferLayout = WM_USER + 100;
  27.  
  28. { TColumn defines internal storage for column attributes.  If IsStored is
  29.   True, values assigned to properties are stored in this object, the grid-
  30.   or field-based default sources are not modified.  Values read from
  31.   properties are the previously assigned value, if any, or the grid- or
  32.   field-based default values if nothing has been assigned to that property.
  33.   This class also publishes the column attribute properties for persistent
  34.   storage.
  35.  
  36.   If IsStored is True, the column does not maintain local storage of
  37.   property values.  Assignments to column properties are passed through to
  38.   the underlying grid- or field-based default sources.  }
  39. type
  40.   TColumn = class;
  41.   TCustomDBGrid = class;
  42.  
  43.   TColumnTitle = class(TPersistent)
  44.   protected
  45.     procedure RefreshDefaultFont;
  46.   public
  47.     constructor Create(Column: TColumn);
  48.     destructor Destroy; override;
  49.     procedure Assign(Source: TPersistent); override;
  50.     function DefaultAlignment: TAlignment;
  51.     function DefaultColor: TColor;
  52.     function DefaultFont: TFont;
  53.     function DefaultCaption: string;
  54.     procedure RestoreDefaults; virtual;
  55.     property Column: TColumn;
  56.   published
  57.     property Alignment: TAlignment;
  58.     property Caption: string;
  59.     property Color: TColor;
  60.     property Font: TFont;
  61.   end;
  62.  
  63.   TColumnButtonStyle = (cbsAuto, cbsEllipsis, cbsNone);
  64.  
  65.   TColumn = class(TCollectionItem)
  66.   protected
  67.     function  CreateTitle: TColumnTitle; virtual;
  68.     function  GetGrid: TCustomDBGrid;
  69.     function GetDisplayName: string; override;
  70.     procedure RefreshDefaultFont;
  71.     procedure SetIndex(Value: Integer); override;
  72.     property IsStored: Boolean default True;
  73.   public
  74.     constructor Create(Collection: TCollection); override;
  75.     destructor Destroy; override;
  76.     procedure Assign(Source: TPersistent); override;
  77.     function  DefaultAlignment: TAlignment;
  78.     function  DefaultColor: TColor;
  79.     function  DefaultFont: TFont;
  80.     function  DefaultImeMode: TImeMode;
  81.     function  DefaultImeName: TImeName;
  82.     function  DefaultReadOnly: Boolean;
  83.     function  DefaultWidth: Integer;
  84.     function  Depth: Integer;
  85.     procedure RestoreDefaults; virtual;
  86.     property  Grid: TCustomDBGrid;
  87.     property  AssignedValues: TColumnValues;
  88.     property  Expandable: Boolean;
  89.     property  Field: TField;
  90.     property  ParentColumn: TColumn;
  91.     property  Showing: Boolean;
  92.   published
  93.     property  Alignment: TAlignment;
  94.     property  ButtonStyle: TColumnButtonStyle default cbsAuto;
  95.     property  Color: TColor;
  96.     property  DropDownRows: Cardinal default 7;
  97.     property  Expanded: Boolean default True;
  98.     property  FieldName: String;
  99.     property  Font: TFont;
  100.     property  ImeMode: TImeMode;
  101.     property  ImeName: TImeName;
  102.     property  PickList: TStrings;
  103.     property  PopupMenu: TPopupMenu;
  104.     property  ReadOnly: Boolean;
  105.     property  Title: TColumnTitle;
  106.     property  Width: Integer;
  107.     property  Visible: Boolean;
  108.   end;
  109.  
  110.   TColumnClass = class of TColumn;
  111.  
  112.   TDBGridColumnsState = (csDefault, csCustomized);
  113.  
  114.   TDBGridColumns = class(TCollection)
  115.   protected
  116.     function GetOwner: TPersistent; override;
  117.     procedure Update(Item: TCollectionItem); override;
  118.   public
  119.     constructor Create(Grid: TCustomDBGrid; ColumnClass: TColumnClass);
  120.     function  Add: TColumn;
  121.     procedure LoadFromFile(const Filename: string);
  122.     procedure LoadFromStream(S: TStream);
  123.     procedure RestoreDefaults;
  124.     procedure RebuildColumns;
  125.     procedure SaveToFile(const Filename: string);
  126.     procedure SaveToStream(S: TStream);
  127.     property State: TDBGridColumnsState;
  128.     property Grid: TCustomDBGrid;
  129.     property Items[Index: Integer]: TColumn; default;
  130.   end;
  131.  
  132.   TGridDataLink = class(TDataLink)
  133.   protected
  134.     procedure ActiveChanged; override;
  135.     procedure BuildAggMap;
  136.     procedure DataSetChanged; override;
  137.     procedure DataSetScrolled(Distance: Integer); override;
  138.     procedure FocusControl(Field: TFieldRef); override;
  139.     procedure EditingChanged; override;
  140.     function IsAggRow(Value: Integer): Boolean; virtual;
  141.     procedure LayoutChanged; override;
  142.     procedure RecordChanged(Field: TField); override;
  143.     procedure UpdateData; override;
  144.     function  GetMappedIndex(ColIndex: Integer): Integer;
  145.   public
  146.     constructor Create(AGrid: TCustomDBGrid);
  147.     destructor Destroy; override;
  148.     function AddMapping(const FieldName: string): Boolean;
  149.     procedure ClearMapping;
  150.     procedure Modified;
  151.     procedure Reset;
  152.     property DefaultFields: Boolean;
  153.     property FieldCount: Integer;
  154.     property Fields[I: Integer]: TField;
  155.     property SparseMap: Boolean;
  156.   end;
  157.  
  158.   TBookmarkList = class
  159.   protected
  160.     function CurrentRow: TBookmarkStr;
  161.     function Compare(const Item1, Item2: TBookmarkStr): Integer;
  162.     procedure LinkActive(Value: Boolean);
  163.   public
  164.     constructor Create(AGrid: TCustomDBGrid);
  165.     destructor Destroy; override;
  166.     procedure Clear;           // free all bookmarks
  167.     procedure Delete;          // delete all selected rows from dataset
  168.     function  Find(const Item: TBookmarkStr; var Index: Integer): Boolean;
  169.     function  IndexOf(const Item: TBookmarkStr): Integer;
  170.     function  Refresh: Boolean;// drop orphaned bookmarks; True = orphans found
  171.     property Count: Integer;
  172.     property CurrentRowSelected: Boolean;
  173.     property Items[Index: Integer]: TBookmarkStr; default;
  174.   end;
  175.  
  176.   TDBGridOption = (dgEditing, dgAlwaysShowEditor, dgTitles, dgIndicator,
  177.     dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect,
  178.     dgAlwaysShowSelection, dgConfirmDelete, dgCancelOnExit, dgMultiSelect);
  179.   TDBGridOptions = set of TDBGridOption;
  180.  
  181.   { The DBGrid's DrawDataCell virtual method and OnDrawDataCell event are only
  182.     called when the grid's Columns.State is csDefault.  This is for compatibility
  183.     with existing code. These routines don't provide sufficient information to
  184.     determine which column is being drawn, so the column attributes aren't
  185.     easily accessible in these routines.  Column attributes also introduce the
  186.     possibility that a column's field may be nil, which would break existing
  187.     DrawDataCell code.   DrawDataCell, OnDrawDataCell, and DefaultDrawDataCell
  188.     are obsolete, retained for compatibility purposes. }
  189.   TDrawDataCellEvent = procedure (Sender: TObject; const Rect: TRect; Field: TField;
  190.     State: TGridDrawState) of object;
  191.  
  192.   { The DBGrid's DrawColumnCell virtual method and OnDrawColumnCell event are
  193.     always called, when the grid has defined column attributes as well as when
  194.     it is in default mode.  These new routines provide the additional
  195.     information needed to access the column attributes for the cell being
  196.     drawn, and must support nil fields.  }
  197.  
  198.   TDrawColumnCellEvent = procedure (Sender: TObject; const Rect: TRect;
  199.     DataCol: Integer; Column: TColumn; State: TGridDrawState) of object;
  200.   TDBGridClickEvent = procedure (Column: TColumn) of object;
  201.  
  202.   TCustomDBGrid = class(TCustomGrid)
  203.   protected
  204.     FUpdateFields: Boolean;
  205.     FAcquireFocus: Boolean;
  206.     function  RawToDataColumn(ACol: Integer): Integer;
  207.     function  DataToRawColumn(ACol: Integer): Integer;
  208.     function  AcquireLayoutLock: Boolean;
  209.     procedure BeginLayout;
  210.     procedure BeginUpdate;
  211.     procedure CalcSizingState(X, Y: Integer; var State: TGridState;
  212.       var Index: Longint; var SizingPos, SizingOfs: Integer;
  213.       var FixedInfo: TGridDrawInfo); override;
  214.     procedure CancelLayout;
  215.     function  CanEditAcceptKey(Key: Char): Boolean; override;
  216.     function  CanEditModify: Boolean; override;
  217.     function  CanEditShow: Boolean; override;
  218.     procedure CellClick(Column: TColumn); dynamic;
  219.     procedure ColumnMoved(FromIndex, ToIndex: Longint); override;
  220.     function CalcTitleRect(Col: TColumn; ARow: Integer;
  221.       var MasterCol: TColumn): TRect;
  222.     function ColumnAtDepth(Col: TColumn; ADepth: Integer): TColumn;
  223.     procedure ColEnter; dynamic;
  224.     procedure ColExit; dynamic;
  225.     procedure ColWidthsChanged; override;
  226.     function  CreateColumns: TDBGridColumns; dynamic;
  227.     function  CreateEditor: TInplaceEdit; override;
  228.     procedure CreateWnd; override;
  229.     procedure DeferLayout;
  230.     procedure DefineFieldMap; virtual;
  231.     procedure DefineProperties(Filer: TFiler); override;
  232.     procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
  233.     procedure DrawDataCell(const Rect: TRect; Field: TField;
  234.       State: TGridDrawState); dynamic; { obsolete }
  235.     procedure DrawColumnCell(const Rect: TRect; DataCol: Integer;
  236.       Column: TColumn; State: TGridDrawState); dynamic;
  237.     procedure EditButtonClick; dynamic;
  238.     procedure EndLayout;
  239.     procedure EndUpdate;
  240.     function  GetColField(DataCol: Integer): TField;
  241.     function  GetEditLimit: Integer; override;
  242.     function  GetEditMask(ACol, ARow: Longint): string; override;
  243.     function  GetEditText(ACol, ARow: Longint): string; override;
  244.     function  GetFieldValue(ACol: Integer): string;
  245.     function  HighlightCell(DataCol, DataRow: Integer; const Value: string;
  246.       AState: TGridDrawState): Boolean; virtual;
  247.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  248.     procedure KeyPress(var Key: Char); override;
  249.     procedure InvalidateTitles;
  250.     procedure LayoutChanged; virtual;
  251.     procedure LinkActive(Value: Boolean); virtual;
  252.     procedure Loaded; override;
  253.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  254.       X, Y: Integer); override;
  255.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  256.       X, Y: Integer); override;
  257.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  258.     procedure Scroll(Distance: Integer); virtual;
  259.     procedure SetColumnAttributes; virtual;
  260.     procedure SetEditText(ACol, ARow: Longint; const Value: string); override;
  261.     function  StoreColumns: Boolean;
  262.     procedure TimedScroll(Direction: TGridScrollDirection); override;
  263.     procedure TitleClick(Column: TColumn); dynamic;
  264.     procedure TopLeftChanged; override;
  265.     function UseRightToLeftAlignmentForField(const AField: TField;
  266.       Alignment: TAlignment): Boolean;
  267.     function BeginColumnDrag(var Origin, Destination: Integer;
  268.       const MousePt: TPoint): Boolean; override;
  269.     function CheckColumnDrag(var Origin, Destination: Integer;
  270.       const MousePt: TPoint): Boolean; override;
  271.     function EndColumnDrag(var Origin, Destination: Integer;
  272.       const MousePt: TPoint): Boolean; override;
  273.     property Columns: TDBGridColumns;
  274.     property DefaultDrawing: Boolean default True;
  275.     property DataSource: TDataSource;
  276.     property DataLink: TGridDataLink;
  277.     property IndicatorOffset: Byte;
  278.     property LayoutLock: Byte;
  279.     property Options: TDBGridOptions default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit];
  280.     property ParentColor default False;
  281.     property ReadOnly: Boolean default False;
  282.     property SelectedRows: TBookmarkList;
  283.     property TitleFont: TFont;
  284.     property UpdateLock: Byte;
  285.     property OnColEnter: TNotifyEvent;
  286.     property OnColExit: TNotifyEvent;
  287.     property OnDrawDataCell: TDrawDataCellEvent; { obsolete }
  288.     property OnDrawColumnCell: TDrawColumnCellEvent;
  289.     property OnEditButtonClick: TNotifyEvent;
  290.     property OnColumnMoved: TMovedEvent;
  291.     property OnCellClick: TDBGridClickEvent;
  292.     property OnTitleClick: TDBGridClickEvent;
  293.   public
  294.     constructor Create(AOwner: TComponent); override;
  295.     destructor Destroy; override;
  296.     procedure DefaultDrawDataCell(const Rect: TRect; Field: TField;
  297.       State: TGridDrawState); { obsolete }
  298.     procedure DefaultDrawColumnCell(const Rect: TRect; DataCol: Integer;
  299.       Column: TColumn; State: TGridDrawState);
  300.     procedure DefaultHandler(var Msg); override;
  301.     function ExecuteAction(Action: TBasicAction): Boolean; override;
  302.     procedure ShowPopupEditor(Column: TColumn; X: Integer = Low(Integer);
  303.       Y: Integer = Low(Integer)); dynamic;
  304.     function UpdateAction(Action: TBasicAction): Boolean; override;
  305.     function ValidFieldIndex(FieldIndex: Integer): Boolean;
  306.     property EditorMode;
  307.     property FieldCount: Integer;
  308.     property Fields[FieldIndex: Integer]: TField;
  309.     property SelectedField: TField;
  310.     property SelectedIndex: Integer;
  311.   end;
  312.  
  313.   TDBGrid = class(TCustomDBGrid)
  314.   public
  315.     property Canvas;
  316.     property SelectedRows;
  317.   published
  318.     property Align;
  319.     property Anchors;
  320.     property BiDiMode;
  321.     property BorderStyle;
  322.     property Color;
  323.     property Columns stored False; //StoreColumns;
  324.     property Constraints;
  325.     property Ctl3D;
  326.     property DataSource;
  327.     property DefaultDrawing;
  328.     property DragCursor;
  329.     property DragKind;
  330.     property DragMode;
  331.     property Enabled;
  332.     property FixedColor;
  333.     property Font;
  334.     property ImeMode;
  335.     property ImeName;
  336.     property Options;
  337.     property ParentBiDiMode;
  338.     property ParentColor;
  339.     property ParentCtl3D;
  340.     property ParentFont;
  341.     property ParentShowHint;
  342.     property PopupMenu;
  343.     property ReadOnly;
  344.     property ShowHint;
  345.     property TabOrder;
  346.     property TabStop;
  347.     property TitleFont;
  348.     property Visible;
  349.     property OnCellClick;
  350.     property OnColEnter;
  351.     property OnColExit;
  352.     property OnColumnMoved;
  353.     property OnDrawDataCell;  { obsolete }
  354.     property OnDrawColumnCell;
  355.     property OnDblClick;
  356.     property OnDragDrop;
  357.     property OnDragOver;
  358.     property OnEditButtonClick;
  359.     property OnEndDock;
  360.     property OnEndDrag;
  361.     property OnEnter;
  362.     property OnExit;
  363.     property OnKeyDown;
  364.     property OnKeyPress;
  365.     property OnKeyUp;
  366.     property OnStartDock;
  367.     property OnStartDrag;
  368.     property OnTitleClick;
  369.   end;
  370.  
  371. const
  372.   IndicatorWidth = 11;
  373.  
  374. implementation
  375.