home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 August / VPR9708A.ISO / D3TRIAL / INSTALL / DATA.Z / DBGRIDS.INT < prev    next >
Text File  |  1997-03-21  |  13KB  |  333 lines

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