home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d456 / DCSLIB25.ZIP / DCGridColEdit.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-25  |  21KB  |  786 lines

  1. {
  2.  BUSINESS CONSULTING
  3.  s a i n t - p e t e r s b u r g
  4.  
  5.          Components Library for Borland Delphi 4.x, 5.x
  6.          Copyright (c) 1998-2000 Alex'EM
  7.  
  8. }
  9. unit DCGridColEdit;
  10.  
  11. interface
  12. {$I DCConst.inc}
  13.  
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  16.   StdCtrls, ImgList, ComCtrls, ToolWin, DCDBGrids,
  17.   {$IFDEF DELPHI_V6}
  18.     DesignIntf, DesignEditors, DesignWindows,
  19.   {$ELSE}
  20.     Dsgnintf, DsgnWnds,
  21.   {$ENDIF}
  22.   Menus, ActnList, StdActns, DCGrids;
  23.  
  24. type
  25.   TGCDesigner = class;
  26.  
  27.   TGridColEditForm = class(TDesignWindow)
  28.     ToolBar: TToolBar;
  29.     tbNew: TToolButton;
  30.     tbDelete: TToolButton;
  31.     tbSeparator: TToolButton;
  32.     imToolBar: TImageList;
  33.     tbAll: TToolButton;
  34.     tbDefault: TToolButton;
  35.     ColumnsActionList: TActionList;
  36.     aNew: TAction;
  37.     aDelete: TAction;
  38.     aAll: TAction;
  39.     aDefault: TAction;
  40.     PopupMenu: TPopupMenu;
  41.     aToolBar: TAction;
  42.     AddField1: TMenuItem;
  43.     Delete1: TMenuItem;
  44.     aSelectAll: TAction;
  45.     SelectAll1: TMenuItem;
  46.     N1: TMenuItem;
  47.     AddAllFields1: TMenuItem;
  48.     RestoreDefault1: TMenuItem;
  49.     lvColumns: TListView;
  50.     ToolBar1: TMenuItem;
  51.     aDeleteAll: TAction;
  52.     procedure FormCreate(Sender: TObject);
  53.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  54.     procedure aNewExecute(Sender: TObject);
  55.     procedure aDeleteExecute(Sender: TObject);
  56.     procedure aDeleteAllExecute(Sender: TObject);
  57.     procedure aAllExecute(Sender: TObject);
  58.     procedure aToolBarExecute(Sender: TObject);
  59.     procedure aDefaultExecute(Sender: TObject);
  60.     procedure UpdateColumns;
  61.     procedure lvColumnsChange(Sender: TObject; Item: TListItem;
  62.       Change: TItemChange);
  63.     procedure aSelectAllExecute(Sender: TObject);
  64.     function CheckCollection: Boolean;
  65.     procedure FormResize(Sender: TObject);
  66.     procedure lvColumnsDragOver(Sender, Source: TObject; X, Y: Integer;
  67.       State: TDragState; var Accept: Boolean);
  68.     procedure lvColumnsDragDrop(Sender, Source: TObject; X, Y: Integer);
  69.     procedure FormDestroy(Sender: TObject);
  70.   protected
  71.     function UniqueName(Component: TComponent): string; override;
  72.     procedure Activated; override;
  73.   private
  74.     { Private declarations }
  75.     FClosing: boolean;
  76.     FDCDBGrid: TDCDBGrid;
  77.     FGCDesigner: TGCDesigner;
  78.     procedure SetDCDBGrid(const Value: TDCDBGrid);
  79.   public
  80.     destructor Destroy; override;
  81.     property DCDBGrid: TDCDBGrid read FDCDBGrid  write SetDCDBGrid;
  82.     {$IFDEF DELPHI_V6}
  83.       procedure ItemDeleted(const ADesigner: IDesigner; AItem: TPersistent); override;
  84.       procedure DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean); override;
  85.       procedure ItemsModified(const ADesigner: IDesigner); override;
  86.     {$ELSE}
  87.       procedure FormModified; override;
  88.       procedure FormClosed(Form: TCustomForm); override;
  89.       procedure ComponentDeleted(Component: IPersistent); override;
  90.     {$ENDIF}
  91.     property GCDesigner: TGCDesigner read FGCDesigner;
  92.   end;
  93.  
  94.   TGCDesigner = class(TDataGridDesigner)
  95.   private
  96.     FGridColEditor: TGridColEditForm;
  97.   public
  98.     destructor Destroy; override;
  99.     property GridColEditor: TGridColEditForm read FGridColEditor;
  100.   end;
  101.  
  102.   TGridColProperty = class(TClassProperty)
  103.   protected
  104.     function GetGrid(Component: TPersistent):TDCDBGrid; virtual;
  105.   public
  106.     procedure Edit; override;
  107.     function GetValue: string; override;
  108.     function GetAttributes: TPropertyAttributes; override;
  109.   end;
  110.  
  111.   TGridColEditor = class(TDefaultEditor)
  112.   protected
  113.    {$IFDEF DELPHI_V6}
  114.       procedure EditProperty(const Prop: IProperty;
  115.         var Continue: Boolean); override;
  116.    {$ELSE}
  117.       procedure EditProperty(PropertyEditor: TPropertyEditor;
  118.         var Continue, FreeEditor: Boolean); override;
  119.    {$ENDIF}
  120.   public
  121.     procedure ExecuteVerb(Index: Integer); override;
  122.     function GetVerb(Index: Integer): string; override;
  123.     function GetVerbCount: Integer; override;
  124.   end;
  125.  
  126.   TDCGridFieldEdit = class(TStringProperty)
  127.   public
  128.     function  GetAttributes: TPropertyAttributes; override;
  129.     procedure GetValueList(List: TStrings);
  130.     procedure GetValues(Proc: TGetStrProc); override;
  131.   end;
  132.  
  133. var
  134.   GridColEditForm: TGridColEditForm;
  135.  
  136. implementation
  137. uses DCChoice;
  138.  
  139. {$R *.DFM}
  140. {$R DCColEdit.RES}
  141.  
  142. const
  143.  bmNew    = 'DBC_NEW';
  144.  bmDelete = 'DBC_DELETE';
  145.  bmAll    = 'DBC_ALL';
  146.  bmDefault= 'DBC_DEFAULT';
  147.  
  148.  fmtListViewItem = '%2.2d - TColumn: %s';
  149.  
  150. // TDBGridColumns Component Property
  151. procedure TGridColProperty.Edit;
  152. var
  153.   ColumnsEditor: TGridColEditForm;
  154.   ADCDBGrid: TDCDBGrid;
  155.   I: integer;
  156. begin
  157.   ColumnsEditor := nil;
  158.   ADCDBGrid :=  GetGrid(GetComponent(0));
  159.  
  160.   if ADCDBGrid = nil then Exit;
  161.  
  162.   {  If ColumnsEditor fo ADCDBGrid not found create it
  163.     esle SetFocus }
  164.  
  165.   {Attempt to Find it}
  166.   for I := 0 to Screen.FormCount - 1 do begin
  167.     if Screen.Forms[I] is TGridColEditForm then
  168.       if TGridColEditForm(Screen.Forms[I]).DCDBGrid = ADCDBGrid then
  169.       begin
  170.         { Great!. Find }
  171.         ColumnsEditor := TGridColEditForm(Screen.Forms[I]);
  172.         Break;
  173.       end;
  174.   end;
  175.  
  176.   if ColumnsEditor = nil then
  177.   begin
  178.     ColumnsEditor := TGridColEditForm.Create(Application);
  179.     try
  180.       {$IFDEF DELPHI_V6}
  181.         ColumnsEditor.Designer := IDesigner(Designer);
  182.       {$ELSE}
  183.         ColumnsEditor.Designer := IFormDesigner(Designer);
  184.       {$ENDIF}
  185.       ColumnsEditor.DCDBGrid := ADCDBGrid;
  186.       with ColumnsEditor do
  187.         Caption := 'Editing ' + DCDBGrid.Name + '.' + GetName;
  188.       ColumnsEditor.Show;
  189.     except
  190.       ColumnsEditor.Free;
  191.       raise;
  192.     end;
  193.   end
  194.   else begin
  195.     ColumnsEditor.Show;
  196.     if ColumnsEditor.WindowState = wsMinimized then
  197.       ColumnsEditor.WindowState := wsNormal;
  198.   end;
  199. end;
  200.  
  201. function TGridColProperty.GetValue: string;
  202. begin
  203.   Result := Format('(%s)', [GetPropType^.Name]);
  204. end;
  205.  
  206. function TGridColProperty.GetAttributes: TPropertyAttributes;
  207. begin
  208.   Result := inherited GetAttributes + [paDialog] - [paSubProperties];
  209. end;
  210.  
  211. {$IFDEF DELPHI_V6}
  212.  
  213.   procedure TGridColEditor.EditProperty(const Prop: IProperty;
  214.     var Continue: Boolean);
  215.   var
  216.     PropName: string;
  217.   begin
  218.     PropName := Prop.GetName;
  219.     if (CompareText(PropName, 'COLUMNS') = 0) then
  220.     begin
  221.       Prop.Edit;
  222.       Continue := False;
  223.     end;
  224.   end;
  225.  
  226. {$ELSE}
  227.  
  228.   procedure TGridColEditor.EditProperty(PropertyEditor: TPropertyEditor;
  229.     var Continue, FreeEditor: Boolean);
  230.   var
  231.     PropName: string;
  232.   begin
  233.     PropName := PropertyEditor.GetName;
  234.     if (CompareText(PropName, 'COLUMNS') = 0) then
  235.     begin
  236.       PropertyEditor.Edit;
  237.       Continue := False;
  238.     end;
  239.   end;
  240.  
  241. {$ENDIF}
  242.  
  243.  
  244. function TGridColEditor.GetVerbCount: Integer;
  245. begin
  246.   Result := 1;
  247. end;
  248.  
  249. function TGridColEditor.GetVerb(Index: Integer): string;
  250. begin
  251.   if Index = 0 then
  252.     Result := 'Co&lumns Editor ...'
  253.   else Result := '';
  254. end;
  255.  
  256. procedure TGridColEditor.ExecuteVerb(Index: Integer);
  257. begin
  258.   if Index = 0 then Edit;
  259. end;
  260.  
  261. function TGridColEditForm.CheckCollection: Boolean;
  262. begin
  263.   Result := (DCDBGrid <> nil) and (DCDBGrid.Columns <> nil)
  264.   {$IFDEF DELPHI_V6}
  265.     and (Designer.Root <> nil);
  266.   {$ELSE}
  267.     and (Designer.Form <> nil);
  268.   {$ENDIF}
  269. end;
  270.  
  271. procedure TGridColEditForm.SetDCDBGrid(const Value: TDCDBGrid);
  272. begin
  273.   if FDCDBGrid <> Value then begin
  274.     FDCDBGrid := Value;
  275.     if FDCDBGrid <> nil then
  276.     begin
  277.       UpdateColumns;
  278.       FGCDesigner := TGCDesigner.Create(Value);
  279.       FGCDesigner.FGridColEditor := Self;
  280.     end
  281.     else
  282.       Release;
  283.   end;
  284. end;
  285.  
  286. procedure TGridColEditForm.FormCreate(Sender: TObject);
  287.  var
  288.   Bmp: TBitmap;
  289. begin
  290.   {Initialize Images}
  291.   inherited;
  292.   Bmp := TBitmap.Create;
  293.   imToolBar.Clear;
  294.  
  295.   Bmp.LoadFromResourceName(HInstance, bmNew);    // 0
  296.   imToolBar.AddMasked(Bmp, clBtnFace);
  297.   Bmp.LoadFromResourceName(HInstance, bmDelete); // 1
  298.   imToolBar.AddMasked(Bmp, clBtnFace);
  299.   Bmp.LoadFromResourceName(HInstance, bmAll);    // 2
  300.   imToolBar.AddMasked(Bmp, clBtnFace);
  301.   Bmp.LoadFromResourceName(HInstance, bmDefault);// 3
  302.   imToolBar.AddMasked(Bmp, clBtnFace);
  303.  
  304.   aNew.ImageIndex     := 0;
  305.   aDelete.ImageIndex  := 1;
  306.   aAll.ImageIndex     := 2;
  307.   aDefault.ImageIndex := 3;
  308.  
  309.   Bmp.Free;
  310. end;
  311.  
  312. procedure TGridColEditForm.FormClose(Sender: TObject;
  313.   var Action: TCloseAction);
  314. begin
  315.   Action := caFree;
  316. end;
  317.  
  318. procedure TGridColEditForm.aNewExecute(Sender: TObject);
  319. begin
  320.   {Add New Field}
  321.   DCDBGrid.Columns.Add;
  322.   UpdateColumns;
  323.   Designer.Modified;
  324.   lvColumns.Items[lvColumns.Items.Count-1].Selected := True;
  325.   lvColumnsChange(nil,nil,ctState);
  326. end;
  327.  
  328. procedure TGridColEditForm.aDeleteAllExecute(Sender: TObject);
  329.  var
  330.   I:Integer;
  331.   {$IFDEF DELPHI_V5UP}
  332.     FComponents: IDesignerSelections;
  333.   {$ELSE}
  334.     FComponents: TComponentList;
  335.   {$ENDIF}
  336. begin
  337.   {Delete All Fiedls}
  338.  
  339.   {$IFNDEF DELPHI_V5UP}
  340.      FComponents := TComponentList.Create;
  341.   {$ELSE}
  342.      FComponents := CreateSelectionList;
  343.   {$ENDIF}
  344.   try
  345.     DCDBGrid.Columns.BeginUpdate;
  346.     lvColumns.Items.BeginUpdate;
  347.  
  348.     for I := lvColumns.Items.Count - 1 downto 0 do
  349.      if Assigned(lvColumns.Items[i].Data)
  350.      then begin
  351.       {$IFDEF DELPHI_V5}
  352.          FComponents.Add(MakeIPersistent(TColumn(lvColumns.Items[i].Data)));
  353.       {$ELSE}
  354.          FComponents.Add(TColumn(lvColumns.Items[i].Data));
  355.       {$ENDIF}
  356.      end;
  357.     lvColumns.Items.Clear;
  358.  
  359.     for I := 0 to FComponents.Count - 1 do
  360.       {$IFDEF DELPHI_V5}
  361.          ExtractPersistent(FComponents[i]).Free;
  362.       {$ELSE}
  363.          FComponents[i].Free;
  364.       {$ENDIF}
  365.   finally
  366.     lvColumns.Items.EndUpdate;
  367.     DCDBGrid.Columns.EndUpdate;
  368.     {$IFNDEF DELPHI_V5UP}
  369.       FComponents.Free;
  370.     {$ENDIF}
  371.     Designer.Modified;
  372.   end;
  373. end;
  374.  
  375. procedure TGridColEditForm.aDeleteExecute(Sender: TObject);
  376.  var
  377.   I, SelectedItem:Integer;
  378.   {$IFDEF DELPHI_V5UP}
  379.     FComponents: IDesignerSelections;
  380.   {$ELSE}
  381.     FComponents: TComponentList;
  382.   {$ENDIF}
  383. begin
  384.   {Delete Selected Fiedls}
  385.   SelectedItem := -1;
  386.   if (lvColumns.SelCount > 0) then begin
  387.     {$IFNDEF DELPHI_V5UP}
  388.        FComponents := TComponentList.Create;
  389.     {$ELSE}
  390.        FComponents := CreateSelectionList;
  391.     {$ENDIF}
  392.     try
  393.       DCDBGrid.Columns.BeginUpdate;
  394.       lvColumns.Items.BeginUpdate;
  395.       SelectedItem := lvColumns.Items.IndexOf(lvColumns.Selected);
  396.  
  397.       for I := lvColumns.Items.Count - 1 downto 0 do
  398.         if (lvColumns.Items[i].Selected = True) and
  399.             Assigned(lvColumns.Items[i].Data)
  400.         then begin
  401.           {$IFDEF DELPHI_V5}
  402.              FComponents.Add(MakeIPersistent(TColumn(lvColumns.Items[i].Data)));
  403.           {$ELSE}
  404.              FComponents.Add(TColumn(lvColumns.Items[i].Data));
  405.           {$ENDIF}
  406.         end;
  407.  
  408.       lvColumns.Items.Clear;
  409.  
  410.       for I := 0 to FComponents.Count - 1 do
  411.       begin
  412.         {$IFDEF DELPHI_V5}
  413.           TColumn(ExtractPersistent(FComponents[i])).Free;
  414.         {$ELSE}
  415.            FComponents[i].Free;
  416.         {$ENDIF}
  417.        end;
  418.     finally
  419.       lvColumns.Items.EndUpdate;
  420.       DCDBGrid.Columns.EndUpdate;
  421.       {$IFNDEF DELPHI_V5UP}
  422.         FComponents.Free;
  423.       {$ENDIF}
  424.       Designer.Modified;
  425.       if (lvColumns.Items.Count > 0) then
  426.         if (SelectedItem > lvColumns.Items.Count - 1) then
  427.            lvColumns.Items[lvColumns.Items.Count-1].Selected := True
  428.         else
  429.            lvColumns.Items[SelectedItem].Selected := True;
  430.         lvColumnsChange(nil,nil,ctState);
  431.     end;
  432.    end;
  433. end;
  434.  
  435. procedure TGridColEditForm.aAllExecute(Sender: TObject);
  436.  var
  437.    I: Integer;
  438.    Column: TColumn;
  439. begin
  440.   {Add All Fields}
  441.   if (DCDBGrid.Columns.State = csDefault) then
  442.      DCDBGrid.Columns.State := csCustomized
  443.   else begin
  444.     for I := 0 to DCDBGrid.DataSource.DataSet.FieldCount - 1 do
  445.     begin
  446.       Column := DCDBGrid.Columns.Add;
  447.       Column.FieldName := DCDBGrid.DataSource.DataSet.Fields[i].FieldName;
  448.     end;
  449.   end;
  450.   UpdateColumns;
  451.   Designer.Modified;
  452. end;
  453.  
  454. procedure TGridColEditForm.aToolBarExecute(Sender: TObject);
  455. begin
  456.   aToolBar.Checked := not aToolBar.Checked;
  457.   ToolBar.Visible  := aToolBar.Checked
  458. end;
  459.  
  460. procedure TGridColEditForm.aDefaultExecute(Sender: TObject);
  461.  var
  462.   I:Integer;
  463.   ListItem: TListItem;
  464. begin
  465.   {Restore Default}
  466.   if (lvColumns.SelCount > 0) then begin
  467.     ListItem := lvColumns.Selected;
  468.     for i := 0 to lvColumns.SelCount - 1 do begin
  469.       TColumn(ListItem.Data).RestoreDefaults;
  470.       ListItem := lvColumns.GetNextItem(ListItem,sdBelow,[isSelected]);
  471.     end;
  472.     lvColumnsChange(nil,nil,ctState);
  473.     Designer.Modified;
  474.   end;
  475. end;
  476.  
  477. procedure TGridColEditForm.UpdateColumns;
  478.  var
  479.   I: longint;
  480.   UpdateColumns: boolean;
  481.   ListItem: TListItem;
  482. begin
  483.   if not CheckCollection then
  484.   begin
  485.     lvColumns.Items.Clear;
  486.     lvColumnsChange(nil,nil,ctState);
  487.     Exit;
  488.   end;
  489.  
  490.   if not Assigned(DCDBGrid) then Exit;
  491.  
  492.   lvColumns.StateImages := DCDBGrid.Images;
  493.  
  494.   UpdateColumns := (lvColumns.Items.Count <> DCDBGrid.Columns.Count );
  495.   I := 0;
  496.   while not(UpdateColumns) and (I <= DCDBGrid.Columns.Count - 1) do
  497.   begin
  498.     UpdateColumns := (lvColumns.Items[I].Data <> DCDBGrid.Columns[I]);
  499.     Inc(I);
  500.   end;
  501.  
  502.   if UpdateColumns then
  503.   begin
  504.     lvColumns.Items.BeginUpdate;
  505.     lvColumns.Items.Clear;
  506.       try
  507.         if (DCDBGrid.Columns.State = csCustomized) then
  508.            for I := 0 to DCDBGrid.Columns.Count - 1 do
  509.            begin
  510.              ListItem := lvColumns.Items.Add;
  511.              ListItem.Caption := Format(fmtListViewItem,[I,DCDBGrid.Columns[i].DisplayName]);
  512.              ListItem.Data := DCDBGrid.Columns[i];
  513.              ListItem.StateIndex := DCDBGrid.Columns[i].ItemIndex;
  514.            end;
  515.       finally
  516.         lvColumns.Items.EndUpdate;
  517.       end;
  518.   end else
  519.     for I := 0 to DCDBGrid.Columns.Count - 1 do
  520.     begin
  521.         lvColumns.Items[i].Caption    := Format(fmtListViewItem,[I,DCDBGrid.Columns[i].DisplayName]);
  522.         lvColumns.Items[i].StateIndex := DCDBGrid.Columns[i].ItemIndex;
  523.     end;
  524.   lvColumnsChange(nil,nil,ctState);
  525. end;
  526.  
  527.  
  528. procedure TGridColEditForm.lvColumnsChange(Sender: TObject; Item: TListItem;
  529.   Change: TItemChange);
  530.  var
  531.   {$IFDEF DELPHI_V5UP}
  532.     {$IFDEF DELPHI_V6}
  533.        FComponents: IDesignerSelections;
  534.     {$ELSE}
  535.        FComponents: TDesignerSelectionList;
  536.     {$ENDIF}
  537.   {$ELSE}
  538.      FComponents: TComponentList;
  539.   {$ENDIF}
  540.   I: Integer;
  541. begin
  542.   if (csDestroying in ComponentState) then Exit;
  543.   {Check Caption Staus}
  544.   aDelete.Enabled    := lvColumns.SelCount > 0;
  545.   aAll.Enabled       := Assigned(DCDBGrid) and
  546.                         Assigned(DCDBGrid.DataSource) and
  547.                         Assigned(DCDBGrid.DataSource.DataSet) and
  548.                         (DCDBGrid.DataSource.DataSet.FieldCount > 0) and
  549.                         (lvColumns.Items.Count = 0);
  550.   aDefault.Enabled   := (lvColumns.SelCount > 0);
  551.   aSelectAll.Enabled := (lvColumns.Items.Count > 0);
  552.  
  553.   if CheckCollection and Active then
  554.   begin
  555.     {$IFNDEF DELPHI_V5UP}
  556.        FComponents := TComponentList.Create;
  557.     {$ELSE}
  558.       {$IFDEF DELPHI_V6}
  559.          FComponents := CreateSelectionList;
  560.       {$ELSE}
  561.          FComponents := TDesignerSelectionList.Create;
  562.       {$ENDIF}
  563.     {$ENDIF}
  564.     if (lvColumns.SelCount > 0) then begin
  565.       for i := lvColumns.Items.Count - 1 downto 0 do
  566.         if lvColumns.Items[i].Selected and Assigned(lvColumns.Items[i].Data) then
  567.           FComponents.Add(TColumn(lvColumns.Items[i].Data));
  568.     end
  569.     else
  570.       if Assigned(FDCDBGrid) then FComponents.Add(DCDBGrid.Columns);
  571.     SetSelection(FComponents);
  572.   end;
  573.  
  574. end;
  575.  
  576. procedure TGridColEditForm.Activated;
  577. begin
  578.   if (csDestroying in ComponentState) then Exit;
  579.   lvColumnsChange(nil,nil,ctState);
  580. end;
  581.  
  582. function TGridColEditForm.UniqueName(Component: TComponent): string;
  583.  var
  584.   Temp: string;
  585. begin
  586.   if (Component <> nil) then Temp := Component.ClassName
  587.   else Temp := TColumn.ClassName;
  588.   if (UpCase(Temp[1]) = 'T') and (Length(Temp) > 1) then
  589.     System.Delete(Temp, 1, 1);
  590.   Result := Designer.UniqueName(Temp);
  591. end;
  592.  
  593. procedure TGridColEditForm.aSelectAllExecute(Sender: TObject);
  594.  var
  595.   I: integer;
  596. begin
  597.   for I := 0 to lvColumns.Items.Count - 1 do
  598.       lvColumns.Items[i].Selected := True;
  599. end;
  600.  
  601. procedure TGridColEditForm.FormResize(Sender: TObject);
  602. begin
  603.   if (csDestroying in ComponentState) then Exit;
  604.   lvColumns.Columns[0].Width := lvColumns.ClientWidth;
  605. end;
  606.  
  607. procedure TGridColEditForm.lvColumnsDragOver(Sender, Source: TObject; X,
  608.   Y: Integer; State: TDragState; var Accept: Boolean);
  609. begin
  610.   Accept := True;
  611. end;
  612.  
  613. procedure TGridColEditForm.lvColumnsDragDrop(Sender, Source: TObject; X,
  614.   Y: Integer);
  615.  var
  616.   ColumnTarget, Column: TColumn;
  617.   i, j, k: integer; 
  618. begin
  619.   { Move Column                           }
  620.   { lvColumns.Selected   - Dragged Column }
  621.   { lvColumns.DropTarget                  }
  622.  
  623.   if not Assigned(Sender) or not Assigned(Source) or
  624.      not Assigned(lvColumns.DropTarget) or
  625.      not Assigned(lvColumns.Selected) or
  626.     (lvColumns.Selected.Index =  lvColumns.DropTarget.Index) then Exit;
  627.  
  628.   Column := TColumn.Create(nil);
  629.   j := lvColumns.Selected.Index;
  630.   k := lvColumns.DropTarget.Index;
  631.  
  632.   ColumnTarget:= DCDBGrid.Columns[k];
  633.   Column.Assign(DCDBGrid.Columns[j]);
  634.   try
  635.     if j > k then
  636.     begin
  637.       for i := j - 1 downto k do
  638.         DCDBGrid.Columns[i+1].Assign(DCDBGrid.Columns[i]);
  639.     end
  640.     else begin
  641.       for i := j + 1 to k do
  642.         DCDBGrid.Columns[i-1].Assign(DCDBGrid.Columns[i]);
  643.     end;
  644.     ColumnTarget.Assign(Column);
  645.   finally
  646.     Column.Free;
  647.   end;
  648.   UpdateColumns;
  649.   Designer.Modified;
  650.   lvColumnsChange(nil,nil,ctState);
  651. end;
  652.  
  653. { TDCGridFieldEdit }
  654.  
  655. function TDCGridFieldEdit.GetAttributes: TPropertyAttributes;
  656.  var
  657.   gs : TDCCustomGridEdit;
  658. begin
  659.   Result := [paValueList, paSortList, paMultiSelect];
  660.   gs := GetComponent(0) as TDCCustomGridEdit;
  661.   try
  662.     if not((gs <> nil) and (gs.DataSet <> nil))
  663.     then
  664.       Result :=  Result - [paValueList];
  665.   except
  666.     Result :=  Result - [paValueList];
  667.   end;
  668. end;
  669.  
  670. procedure TDCGridFieldEdit.GetValueList(List: TStrings);
  671.  var
  672.   gs : TDCCustomGridEdit;
  673. begin
  674.   gs := GetComponent(0) as TDCCustomGridEdit;
  675.   if (gs <> nil) and (gs.DataSet <> nil) then
  676.   begin
  677.     gs.DataSet.GetFieldNames(List);
  678.   end;
  679. end;
  680.  
  681. procedure TDCGridFieldEdit.GetValues(Proc: TGetStrProc);
  682.  var
  683.   i: Integer;
  684.   Values: TStringList;
  685. begin
  686.   Values := TStringList.Create;
  687.   try
  688.     GetValueList(Values);
  689.     for i := 0 to Values.Count - 1 do Proc(Values[I]);
  690.   finally
  691.     Values.Free;
  692.   end;
  693. end;
  694.  
  695. destructor TGridColEditForm.Destroy;
  696. begin
  697.   inherited;
  698. end;
  699.  
  700. function TGridColProperty.GetGrid(Component: TPersistent): TDCDBGrid;
  701. begin
  702.   Result := TDCDBGrid(Component);
  703. end;
  704.  
  705. { TGCDesigner }
  706.  
  707. destructor TGCDesigner.Destroy;
  708. begin
  709.   if FGridColEditor <> nil then
  710.   begin
  711.     FGridColEditor.FGCDesigner := nil;
  712.     FGridColEditor.Release;
  713.   end;
  714.   inherited;
  715. end;
  716.  
  717. procedure TGridColEditForm.FormDestroy(Sender: TObject);
  718. begin
  719.   if FGCDesigner <> nil then
  720.   begin
  721.     { Destroy the designer if the editor is destroyed }
  722.     FGCDesigner.FGridColEditor := nil;
  723.     FGCDesigner.Free;
  724.     FGCDesigner := nil;
  725.   end;
  726. end;
  727.  
  728. {$IFDEF DELPHI_V6}
  729.   procedure TGridColEditForm.DesignerClosed(const ADesigner: IDesigner;
  730.     AGoingDormant: Boolean);
  731.   begin
  732.     if Designer = ADesigner then
  733.     begin
  734.       FClosing := True;
  735.       Close;
  736.     end;
  737.   end;
  738.  
  739.   procedure TGridColEditForm.ItemDeleted(const ADesigner: IDesigner;
  740.     AItem: TPersistent);
  741.   begin
  742.     if (AItem = nil) or FClosing then Exit;
  743.     if AItem = DCDBGrid then
  744.     begin
  745.       FDCDBGrid := nil;
  746.       Close;
  747.     end;
  748.   end;
  749.  
  750.   procedure TGridColEditForm.ItemsModified(const ADesigner: IDesigner);
  751.   begin
  752.     if FClosing then exit;
  753.     if not (csDestroying in ComponentState) then UpdateColumns;
  754.   end;
  755.  
  756. {$ELSE}
  757.  
  758.   procedure TGridColEditForm.FormModified;
  759.   begin
  760.     if FClosing then exit;
  761.     if not (csDestroying in ComponentState) then UpdateColumns;
  762.   end;
  763.  
  764.   procedure TGridColEditForm.FormClosed(Form: TCustomForm);
  765.   begin
  766.     if (Form = Designer.Form) then
  767.     begin
  768.       FClosing := True;
  769.       Close;
  770.     end;
  771.   end;
  772.  
  773.   procedure TGridColEditForm.ComponentDeleted(Component: IPersistent);
  774.   begin
  775.     if ExtractPersistent(Component) = DCDBGrid then
  776.     begin
  777.       FDCDBGrid := nil;
  778.       Close;
  779.     end;
  780.   end;
  781. {$ENDIF}
  782.  
  783. end.
  784.  
  785.  
  786.