home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d456 / DCSLIB25.ZIP / DCListColEdit.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-25  |  17KB  |  602 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 DCListColEdit;
  10.  
  11. interface
  12. {$I DCConst.inc}
  13.  
  14. uses
  15.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  16.   StdCtrls, ImgList, ComCtrls, ToolWin,
  17.   {$IFDEF DELPHI_V6}
  18.     DesignIntf, DesignEditors, DesignWindows,
  19.   {$ELSE}
  20.     Dsgnintf, DsgnWnds,
  21.   {$ENDIF}
  22.   ActnList, Menus, StdActns, DCGrids, DCChoice, DCDBGrids;
  23.  
  24. type
  25.   TListColEditForm = class(TDesignWindow)
  26.     ToolBar: TToolBar;
  27.     tbNew: TToolButton;
  28.     tbDelete: TToolButton;
  29.     tbSeparator: TToolButton;
  30.     imToolBar: TImageList;
  31.     tbAll: TToolButton;
  32.     tbDefault: TToolButton;
  33.     ColumnsActionList: TActionList;
  34.     aNew: TAction;
  35.     aDelete: TAction;
  36.     aAll: TAction;
  37.     aDefault: TAction;
  38.     PopupMenu: TPopupMenu;
  39.     aToolBar: TAction;
  40.     AddField1: TMenuItem;
  41.     Delete1: TMenuItem;
  42.     aSelectAll: TAction;
  43.     SelectAll1: TMenuItem;
  44.     N1: TMenuItem;
  45.     AddAllFields1: TMenuItem;
  46.     RestoreDefault1: TMenuItem;
  47.     lvColumns: TListView;
  48.     ToolBar1: TMenuItem;
  49.     aDeleteAll: TAction;
  50.     procedure FormCreate(Sender: TObject);
  51.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  52.     procedure aNewExecute(Sender: TObject);
  53.     procedure aDeleteExecute(Sender: TObject);
  54.     procedure aDeleteAllExecute(Sender: TObject);
  55.     procedure aAllExecute(Sender: TObject);
  56.     procedure aToolBarExecute(Sender: TObject);
  57.     procedure aDefaultExecute(Sender: TObject);
  58.     procedure UpdateColumns;
  59.     procedure lvColumnsChange(Sender: TObject; Item: TListItem;
  60.       Change: TItemChange);
  61.     procedure aSelectAllExecute(Sender: TObject);
  62.     function CheckCollection: Boolean;
  63.     procedure FormResize(Sender: TObject);
  64.     procedure lvColumnsDragOver(Sender, Source: TObject; X, Y: Integer;
  65.       State: TDragState; var Accept: Boolean);
  66.     procedure lvColumnsDragDrop(Sender, Source: TObject; X, Y: Integer);
  67.   protected
  68.     function UniqueName(Component: TComponent): string; override;
  69.     procedure Activated; override;
  70.   private
  71.     { Private declarations }
  72.     FClosing: boolean;
  73.     FDCGridEdit: TDCGridEdit;
  74.     procedure SetDCGridEdit(const Value: TDCGridEdit);
  75.   public
  76.     property DCGridEdit: TDCGridEdit read FDCGridEdit  write SetDCGridEdit;
  77.     {$IFDEF DELPHI_V6}
  78.       procedure ItemDeleted(const ADesigner: IDesigner; AItem: TPersistent); override;
  79.       procedure DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean); override;
  80.       procedure ItemsModified(const ADesigner: IDesigner); override;
  81.     {$ELSE}
  82.       procedure FormModified; override;
  83.       procedure FormClosed(Form: TCustomForm); override;
  84.       procedure ComponentDeleted(Component: IPersistent); override;
  85.     {$ENDIF}
  86.   end;
  87.  
  88.   TListColProperty = class(TClassProperty)
  89.   public
  90.     procedure Edit; override;
  91.     function GetValue: string; override;
  92.     function GetAttributes: TPropertyAttributes; override;
  93.   end;
  94.  
  95. implementation
  96.  
  97. {$R *.DFM}
  98.  
  99. const
  100.  bmNew    = 'DBC_NEW';
  101.  bmDelete = 'DBC_DELETE';
  102.  bmAll    = 'DBC_ALL';
  103.  bmDefault= 'DBC_DEFAULT';
  104.  
  105.  fmtListViewItem = '%2.2d - TColumn: %s';
  106.  
  107. procedure TListColProperty.Edit;
  108. var
  109.   ColumnsEditor: TListColEditForm;
  110.   ADCGridEdit: TDCGridEdit;
  111.   I: integer;
  112. begin
  113.   ColumnsEditor := nil;
  114.   ADCGridEdit :=  TDCGridEdit(GetComponent(0));
  115.  
  116.   if ADCGridEdit = nil then Exit;
  117.  
  118.   {  If ColumnsEditor fo ADCGridEdit not found create it
  119.     esle SetFocus }
  120.  
  121.   {Attempt to Find it}
  122.   for I := 0 to Screen.FormCount - 1 do begin
  123.     if Screen.Forms[I] is TListColEditForm then
  124.       if (TListColEditForm(Screen.Forms[I]).DCGridEdit = ADCGridEdit) and
  125.          (TListColEditForm(Screen.Forms[I]).Name = ADCGridEdit.Name+'_'+GetName)
  126.       then begin
  127.         { Great!. Find }
  128.         ColumnsEditor := TListColEditForm(Screen.Forms[I]);
  129.         Break;
  130.       end;
  131.   end;
  132.  
  133.   if ColumnsEditor = nil then
  134.   begin
  135.     ColumnsEditor := TListColEditForm.Create(Application);
  136.     try
  137.       {$IFDEF DELPHI_V6}
  138.         ColumnsEditor.Designer := IDesigner(Designer);
  139.       {$ELSE}
  140.         ColumnsEditor.Designer := IFormDesigner(Designer);
  141.       {$ENDIF}
  142.       ColumnsEditor.DCGridEdit := ADCGridEdit;
  143.       ColumnsEditor.Name := ADCGridEdit.Name + '_' + GetName;
  144.       with ColumnsEditor do
  145.         Caption := 'Editing ' + DCGridEdit.Name + '.' + GetName;
  146.       ColumnsEditor.Show;
  147.     except
  148.       ColumnsEditor.Free;
  149.       raise;
  150.     end;
  151.   end
  152.   else begin
  153.     ColumnsEditor.Show;
  154.     if ColumnsEditor.WindowState = wsMinimized then
  155.       ColumnsEditor.WindowState := wsNormal;
  156.   end;
  157. end;
  158.  
  159. function TListColProperty.GetValue: string;
  160. begin
  161.   Result := Format('(%s)', [GetPropType^.Name]);
  162. end;
  163.  
  164. function TListColProperty.GetAttributes: TPropertyAttributes;
  165. begin
  166.   Result := inherited GetAttributes + [paDialog] - [paSubProperties];
  167. end;
  168.  
  169. function TListColEditForm.CheckCollection: Boolean;
  170. begin
  171.   Result := (DCGridEdit <> nil) and (DCGridEdit.ListBoxColumns <> nil)
  172.   {$IFDEF DELPHI_V6}
  173.     and (Designer.Root <> nil);
  174.   {$ELSE}
  175.     and (Designer.Form <> nil);
  176.   {$ENDIF}
  177. end;
  178.  
  179. procedure TListColEditForm.SetDCGridEdit(const Value: TDCGridEdit);
  180. begin
  181.   if FDCGridEdit <> Value then
  182.   begin
  183.     FDCGridEdit := Value;
  184.     if FDCGridEdit <> nil then
  185.       UpdateColumns
  186.     else
  187.       Release;
  188.   end;
  189. end;
  190.  
  191. procedure TListColEditForm.FormCreate(Sender: TObject);
  192.  var
  193.   Bmp: TBitmap;
  194. begin
  195.   {Initialize Images}
  196.   inherited;
  197.  
  198.   Bmp := TBitmap.Create;
  199.  
  200.   imToolBar.Clear;
  201.  
  202.   Bmp.LoadFromResourceName(HInstance, bmNew);    // 0
  203.   imToolBar.AddMasked(Bmp, clBtnFace);
  204.   Bmp.LoadFromResourceName(HInstance, bmDelete); // 1
  205.   imToolBar.AddMasked(Bmp, clBtnFace);
  206.   Bmp.LoadFromResourceName(HInstance, bmAll);    // 2
  207.   imToolBar.AddMasked(Bmp, clBtnFace);
  208.   Bmp.LoadFromResourceName(HInstance, bmDefault);// 3
  209.   imToolBar.AddMasked(Bmp, clBtnFace);
  210.  
  211.   aNew.ImageIndex     := 0;
  212.   aDelete.ImageIndex  := 1;
  213.   aAll.ImageIndex     := 2;
  214.   aDefault.ImageIndex := 3;
  215.  
  216. end;
  217.  
  218. procedure TListColEditForm.FormClose(Sender: TObject;
  219.   var Action: TCloseAction);
  220. begin
  221.   Action := caFree;
  222. end;
  223.  
  224. procedure TListColEditForm.aNewExecute(Sender: TObject);
  225.  var
  226.   Column: TColumn;
  227. begin
  228.   {Add New Field}
  229.   Column := DCGridEdit.ListBoxColumns.Add;
  230.   Column.Font := DCGridEdit.Font;
  231.   UpdateColumns;
  232.   Designer.Modified;
  233.   lvColumns.Items[lvColumns.Items.Count-1].Selected := True;
  234.   lvColumnsChange(nil,nil,ctState);
  235. end;
  236.  
  237. procedure TListColEditForm.aDeleteAllExecute(Sender: TObject);
  238.  var
  239.   I:Integer;
  240.   {$IFDEF DELPHI_V5UP}
  241.     FComponents: IDesignerSelections;
  242.   {$ELSE}
  243.     FComponents: TComponentList;
  244.   {$ENDIF}
  245. begin
  246.   {Delete All Fiedls}
  247.  
  248.   {$IFNDEF DELPHI_V5UP}
  249.      FComponents := TComponentList.Create;
  250.   {$ELSE}
  251.      FComponents := CreateSelectionList;
  252.   {$ENDIF}
  253.   try
  254.     DCGridEdit.ListBoxColumns.BeginUpdate;
  255.     lvColumns.Items.BeginUpdate;
  256.  
  257.     for I := lvColumns.Items.Count - 1 downto 0 do
  258.      if Assigned(lvColumns.Items[i].Data)
  259.      then begin
  260.       {$IFDEF DELPHI_V5}
  261.          FComponents.Add(MakeIPersistent(TColumn(lvColumns.Items[i].Data)));
  262.       {$ELSE}
  263.          FComponents.Add(TColumn(lvColumns.Items[i].Data));
  264.       {$ENDIF}
  265.      end;
  266.     lvColumns.Items.Clear;
  267.  
  268.     for I := 0 to FComponents.Count - 1 do
  269.       {$IFDEF DELPHI_V5}
  270.          ExtractPersistent(FComponents[i]).Free;
  271.       {$ELSE}
  272.          FComponents[i].Free;
  273.       {$ENDIF}
  274.   finally
  275.     lvColumns.Items.EndUpdate;
  276.     DCGridEdit.ListBoxColumns.EndUpdate;
  277.     {$IFNDEF DELPHI_V5UP}
  278.       FComponents.Free;
  279.     {$ENDIF}
  280.     Designer.Modified;
  281.   end;
  282. end;
  283.  
  284. procedure TListColEditForm.aDeleteExecute(Sender: TObject);
  285.  var
  286.   I, SelectedItem:Integer;
  287.   {$IFDEF DELPHI_V5UP}
  288.     FComponents: IDesignerSelections;
  289.   {$ELSE}
  290.     FComponents: TComponentList;
  291.   {$ENDIF}
  292. begin
  293.   {Delete Selected Fiedls}
  294.   SelectedItem := -1;
  295.   if (lvColumns.SelCount > 0) then begin
  296.     {$IFNDEF DELPHI_V5UP}
  297.        FComponents := TComponentList.Create;
  298.     {$ELSE}
  299.        FComponents := CreateSelectionList;
  300.     {$ENDIF}
  301.     try
  302.       DCGridEdit.ListBoxColumns.BeginUpdate;
  303.       lvColumns.Items.BeginUpdate;
  304.       SelectedItem := lvColumns.Items.IndexOf(lvColumns.Selected);
  305.  
  306.       for I := lvColumns.Items.Count - 1 downto 0 do
  307.         if (lvColumns.Items[i].Selected = True) and
  308.             Assigned(lvColumns.Items[i].Data)
  309.         then begin
  310.           {$IFDEF DELPHI_V5}
  311.              FComponents.Add(MakeIPersistent(TColumn(lvColumns.Items[i].Data)));
  312.           {$ELSE}
  313.              FComponents.Add(TColumn(lvColumns.Items[i].Data));
  314.           {$ENDIF}
  315.         end;
  316.  
  317.       lvColumns.Items.Clear;
  318.  
  319.       for I := 0 to FComponents.Count - 1 do
  320.         {$IFDEF DELPHI_V5}
  321.            ExtractPersistent(FComponents[i]).Free;
  322.         {$ELSE}
  323.            FComponents[i].Free;
  324.         {$ENDIF}
  325.     finally
  326.       lvColumns.Items.EndUpdate;
  327.       DCGridEdit.ListBoxColumns.EndUpdate;
  328.       {$IFNDEF DELPHI_V5UP}
  329.         FComponents.Free;
  330.       {$ENDIF}
  331.       Designer.Modified;
  332.       if (lvColumns.Items.Count > 0) then
  333.         if (SelectedItem > lvColumns.Items.Count - 1) then
  334.            lvColumns.Items[lvColumns.Items.Count-1].Selected := True
  335.         else
  336.            lvColumns.Items[SelectedItem].Selected := True;
  337.         lvColumnsChange(nil,nil,ctState);
  338.     end;
  339.    end;
  340. end;
  341.  
  342. procedure TListColEditForm.aAllExecute(Sender: TObject);
  343.  var
  344.    I: Integer;
  345.    Column: TColumn;
  346. begin
  347.   {Add All Fields}
  348.   for I := 0 to DCGridEdit.DataSet.FieldCount - 1 do
  349.   begin
  350.     Column := DCGridEdit.ListBoxColumns.Add;
  351.     Column.FieldName := DCGridEdit.DataSet.Fields[i].FieldName;
  352.     Column.Font := DCGridEdit.Font;
  353.   end;
  354.   UpdateColumns;
  355.   Designer.Modified;
  356. end;
  357.  
  358. procedure TListColEditForm.aToolBarExecute(Sender: TObject);
  359. begin
  360.   aToolBar.Checked := not aToolBar.Checked;
  361.   ToolBar.Visible  := aToolBar.Checked
  362. end;
  363.  
  364. procedure TListColEditForm.aDefaultExecute(Sender: TObject);
  365.  var
  366.   I:Integer;
  367.   ListItem: TListItem;
  368. begin
  369.   {Restore Default}
  370.   if (lvColumns.SelCount > 0) then begin
  371.     ListItem := lvColumns.Selected;
  372.     for i := 0 to lvColumns.SelCount - 1 do begin
  373.       TColumn(ListItem.Data).RestoreDefaults;
  374.       ListItem := lvColumns.GetNextItem(ListItem,sdBelow,[isSelected]);
  375.     end;
  376.     lvColumnsChange(nil,nil,ctState);
  377.     Designer.Modified;
  378.   end;
  379. end;
  380.  
  381. procedure TListColEditForm.UpdateColumns;
  382.  var
  383.   I: longint;
  384.   UpdateColumns: boolean;
  385.   ListItem: TListItem;
  386. begin
  387.   if not CheckCollection then
  388.   begin
  389.     lvColumns.Items.Clear;
  390.     lvColumnsChange(nil,nil,ctState);
  391.     Exit;
  392.   end;
  393.  
  394.   lvColumns.StateImages := DCGridEdit.Images;
  395.  
  396.   UpdateColumns := (lvColumns.Items.Count <> DCGridEdit.ListBoxColumns.Count );
  397.   I := 0;
  398.   while not(UpdateColumns) and (I <= DCGridEdit.ListBoxColumns.Count - 1) do
  399.   begin
  400.     UpdateColumns := (lvColumns.Items[I].Data <> DCGridEdit.ListBoxColumns[I]);
  401.     Inc(I);
  402.   end;
  403.  
  404.   if UpdateColumns then
  405.   begin
  406.     lvColumns.Items.BeginUpdate;
  407.     lvColumns.Items.Clear;
  408.       try
  409.         if (DCGridEdit.ListBoxColumns.State = csCustomized) then
  410.            for I := 0 to DCGridEdit.ListBoxColumns.Count - 1 do
  411.            begin
  412.              ListItem := lvColumns.Items.Add;
  413.              ListItem.Caption := Format(fmtListViewItem,[I,DCGridEdit.ListBoxColumns[i].DisplayName]);
  414.              ListItem.Data := DCGridEdit.ListBoxColumns[i];
  415.              ListItem.StateIndex := DCGridEdit.ListBoxColumns[i].ItemIndex;
  416.            end;
  417.       finally
  418.         lvColumns.Items.EndUpdate;
  419.       end;
  420.   end else
  421.     for I := 0 to DCGridEdit.ListBoxColumns.Count - 1 do
  422.     begin
  423.         lvColumns.Items[i].Caption    := Format(fmtListViewItem,[I,DCGridEdit.ListBoxColumns[i].DisplayName]);
  424.         lvColumns.Items[i].StateIndex := DCGridEdit.ListBoxColumns[i].ItemIndex;
  425.     end;
  426.   lvColumnsChange(nil,nil,ctState);
  427. end;
  428.  
  429.  
  430. procedure TListColEditForm.lvColumnsChange(Sender: TObject; Item: TListItem;
  431.   Change: TItemChange);
  432.  var
  433.   {$IFDEF DELPHI_V5UP}
  434.     {$IFDEF DELPHI_V6}
  435.        FComponents: IDesignerSelections;
  436.     {$ELSE}
  437.        FComponents: TDesignerSelectionList;
  438.     {$ENDIF}
  439.   {$ELSE}
  440.     FComponents: TComponentList;
  441.   {$ENDIF}
  442.   I: Integer;
  443. begin
  444.    {Check Caption Staus}
  445.    aDelete.Enabled    := lvColumns.SelCount > 0;
  446.    aAll.Enabled       := Assigned(DCGridEdit) and
  447.                          Assigned(DCGridEdit.DataSet) and
  448.                          (DCGridEdit.DataSet.FieldCount > 0) and
  449.                          (lvColumns.Items.Count = 0);
  450.    aDefault.Enabled   := (lvColumns.SelCount > 0);
  451.    aSelectAll.Enabled := (lvColumns.Items.Count > 0);
  452.  
  453.   if CheckCollection and Active then
  454.   begin
  455.     {$IFNDEF DELPHI_V5UP}
  456.       FComponents := TComponentList.Create;
  457.     {$ELSE}
  458.       {$IFDEF DELPHI_V6}
  459.          FComponents := CreateSelectionList;
  460.       {$ELSE}
  461.          FComponents := TDesignerSelectionList.Create;
  462.       {$ENDIF}
  463.     {$ENDIF}
  464.     if (lvColumns.SelCount > 0) then begin
  465.       for i := lvColumns.Items.Count - 1 downto 0 do
  466.         if (lvColumns.Items[i].Selected = True) and
  467.            Assigned(lvColumns.Items[i].Data)
  468.          then
  469.            FComponents.Add(TColumn(lvColumns.Items[i].Data));
  470.     end
  471.     else
  472.       FComponents.Add(DCGridEdit.ListBoxColumns);
  473.     SetSelection(FComponents);
  474.  
  475.   end;
  476.  
  477. end;
  478.  
  479. procedure TListColEditForm.Activated;
  480. begin
  481.   lvColumnsChange(nil,nil,ctState);
  482. end;
  483.  
  484. function TListColEditForm.UniqueName(Component: TComponent): string;
  485. var
  486.   Temp: string;
  487. begin
  488.   if (Component <> nil) then Temp := Component.ClassName
  489.   else Temp := TColumn.ClassName;
  490.   if (UpCase(Temp[1]) = 'T') and (Length(Temp) > 1) then
  491.     System.Delete(Temp, 1, 1);
  492.   Result := Designer.UniqueName(Temp);
  493. end;
  494.  
  495. {$IFDEF DELPHI_V6}
  496.  
  497.   procedure TListColEditForm.DesignerClosed(const ADesigner: IDesigner;
  498.     AGoingDormant: Boolean);
  499.   begin
  500.     if Designer = ADesigner then
  501.     begin
  502.       FClosing := True;
  503.       Close;
  504.     end;
  505.   end;
  506.  
  507.   procedure TListColEditForm.ItemDeleted(const ADesigner: IDesigner;
  508.     AItem: TPersistent);
  509.   begin
  510.     if (AItem = nil) or FClosing then Exit;
  511.     if AItem = DCGridEdit then
  512.     begin
  513.       FDCGridEdit := nil;
  514.       Close;
  515.     end;
  516.   end;
  517.  
  518.   procedure TListColEditForm.ItemsModified(const ADesigner: IDesigner);
  519.   begin
  520.     if FClosing then exit;
  521.     if not (csDestroying in ComponentState) then UpdateColumns;
  522.   end;
  523.  
  524. {$ELSE}
  525.  
  526.   procedure TListColEditForm.FormModified;
  527.   begin
  528.     if FClosing then Exit;
  529.     if not (csDestroying in ComponentState) then UpdateColumns;
  530.   end;
  531.  
  532.   procedure TListColEditForm.FormClosed(Form: TCustomForm);
  533.   begin
  534.     if (Form = Designer.Form) then
  535.     begin
  536.       aDeleteAllExecute(Self);
  537.       Close;
  538.     end;
  539.   end;
  540.  
  541.   procedure TListColEditForm.ComponentDeleted(Component: IPersistent);
  542.   begin
  543.     if ExtractPersistent(Component) = DCGridEdit then
  544.     begin
  545.       FDCGridEdit := nil;
  546.       Close;
  547.     end;
  548.   end;
  549.  
  550. {$ENDIF}
  551.  
  552. procedure TListColEditForm.aSelectAllExecute(Sender: TObject);
  553.  var
  554.   I: integer;
  555. begin
  556.   for I := 0 to lvColumns.Items.Count - 1 do
  557.       lvColumns.Items[i].Selected := True;
  558. end;
  559.  
  560. procedure TListColEditForm.FormResize(Sender: TObject);
  561. begin
  562.   lvColumns.Columns[0].Width := lvColumns.ClientWidth;
  563. end;
  564.  
  565. procedure TListColEditForm.lvColumnsDragOver(Sender, Source: TObject; X,
  566.   Y: Integer; State: TDragState; var Accept: Boolean);
  567. begin
  568.   Accept := True;
  569. end;
  570.  
  571. procedure TListColEditForm.lvColumnsDragDrop(Sender, Source: TObject; X,
  572.   Y: Integer);
  573.  var
  574.   Column: TColumn;
  575.   ColumnFrom, ColumnTarget: TColumn;
  576. begin
  577.   { Move Column                           }
  578.   { lvColumns.Selected   - Dragged Column }
  579.   { lvColumns.DropTarget                  }
  580.  
  581.   if not Assigned(Sender) or not Assigned(Source) or
  582.      not Assigned(lvColumns.DropTarget) or
  583.      not Assigned(lvColumns.Selected) or
  584.     (lvColumns.Selected.Index =  lvColumns.DropTarget.Index) then Exit;
  585.  
  586.   Column := TColumn.Create(nil);
  587.   ColumnFrom  := DCGridEdit.ListBoxColumns[lvColumns.Selected.Index];
  588.   ColumnTarget:= DCGridEdit.ListBoxColumns[lvColumns.DropTarget.Index];
  589.   Column.Assign(ColumnFrom);
  590.   try
  591.     ColumnFrom.Assign(ColumnTarget);
  592.     ColumnTarget.Assign(Column);
  593.   finally
  594.     Column.Free;
  595.   end;
  596.   UpdateColumns;
  597.   Designer.Modified;
  598.   lvColumnsChange(nil,nil,ctState);
  599. end;
  600.  
  601. end.
  602.