home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / ChipCD_1.03.iso / zkuste / delphi / kompon / D34567 / SETDBGR / SMDBGSET.ZIP / SMDBGSet.pas < prev   
Pascal/Delphi Source File  |  2002-10-09  |  18KB  |  589 lines

  1. { Copyright (C) 1998-2002, written by Shkolnik Mike, Scalabium
  2.   E-Mail:  mshkolnik@scalabium.com
  3.            mshkolnik@yahoo.com
  4.   WEB: http://www.scalabium.com
  5.        http://www.geocities.com/mshkolnik
  6.   tel: 380-/44/-552-10-29
  7.  
  8.   In this unit I described the visual dialog for TDBGrid's Columns property tuning.
  9. }
  10.  
  11. unit SMDBGSet;
  12.  
  13. interface
  14.  
  15. uses
  16.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  17.   Dialogs, stdctrls, extctrls, checklst, DBGrids, Menus;
  18.  
  19. type
  20.   TSMSetDBGridDialog = class(TComponent)
  21.   private
  22.     { Private declarations }
  23.     FCaption: TCaption;
  24.     FDBGrid: TCustomDBGrid;
  25.     FAllowedFields: TStrings;
  26.  
  27.     FOnBeforeExecute: TNotifyEvent;
  28.     FOnAfterExecute: TNotifyEvent;
  29.     FOnShow: TNotifyEvent;
  30.  
  31.     procedure SetAllowedFields(Value: TStrings);
  32.   protected
  33.     { Protected declarations }
  34.   public
  35.     { Public declarations }
  36.     constructor Create(AOwner: TComponent); override;
  37.     destructor Destroy; override;
  38.     function Execute: Boolean;
  39.   published
  40.     { Published declarations }
  41.     property Caption: TCaption read FCaption write FCaption;
  42.     property DBGrid: TCustomDBGrid read FDBGrid write FDBGrid;
  43.     property AllowedFields: TStrings read FAllowedFields write SetAllowedFields;
  44.  
  45.     property OnBeforeExecute: TNotifyEvent read FOnBeforeExecute write FOnBeforeExecute;
  46.     property OnAfterExecute: TNotifyEvent read FOnAfterExecute write FOnAfterExecute;
  47.     property OnShow: TNotifyEvent read FOnShow write FOnShow;
  48.   end;
  49.  
  50.   TSMGridSetupItem = class
  51.     FieldIndex: Integer;
  52.     FieldName: string;
  53.  
  54.     TitleAlignment: TAlignment;
  55.     TitleCaption: string;
  56.     TitleColor: TColor;
  57.     TitleFont: TFont;
  58.  
  59.     DataAlignment: TAlignment;
  60.     DataColor: TColor;
  61.     DataFont: TFont;
  62.     Width: Integer;
  63.   end;
  64.  
  65.   TfrmGridSetup = class(TForm)
  66.     btnOk: TButton;
  67.     btnCancel: TButton;
  68.     bvlButton: TBevel;
  69.     clbFields: TCheckListBox;
  70.     gbTitle: TGroupBox;
  71.     lblTitleCaption: TLabel;
  72.     lblTitleAlignment: TLabel;
  73.     lblTitleColor: TLabel;
  74.     lblTitleFont: TLabel;
  75.     edTitleCaption: TEdit;
  76.     edTitleFont: TEdit;
  77.     cbTitleAlignment: TComboBox;
  78.     gbData: TGroupBox;
  79.     lblDataAlignment: TLabel;
  80.     lblDataColor: TLabel;
  81.     lblDataFont: TLabel;
  82.     edDataFont: TEdit;
  83.     cbDataAlignment: TComboBox;
  84.     lblWidth: TLabel;
  85.     lblWidthFix: TLabel;
  86.     edWidth: TEdit;
  87.     FontDlg: TFontDialog;
  88.     SMColorsCBTitle: TComboBox;
  89.     SMColorsCBData: TComboBox;
  90.     btnTitleFont: TButton;
  91.     btnDataFont: TButton;
  92.     pmColumns: TPopupMenu;
  93.     miSelectAll: TMenuItem;
  94.     miUnselectAll: TMenuItem;
  95.     miInvertSelection: TMenuItem;
  96.     procedure PropertyExit(Sender: TObject);
  97.     procedure clbFieldsClick(Sender: TObject);
  98.     procedure clbFieldsDragDrop(Sender, Source: TObject; X, Y: Integer);
  99.     procedure clbFieldsDragOver(Sender, Source: TObject; X, Y: Integer;
  100.       State: TDragState; var Accept: Boolean);
  101.     procedure edTitleFontButtonClick(Sender: TObject);
  102.     procedure SMColorsCBTitleDrawItem(Control: TWinControl; Index: Integer;
  103.       Rect: TRect; State: TOwnerDrawState);
  104.     procedure miSelectAllClick(Sender: TObject);
  105.     procedure SMColorsCBTitleClick(Sender: TObject);
  106.     procedure SMColorsCBTitleChange(Sender: TObject);
  107.   private
  108.     { Private declarations }
  109.     function GetItemCaption(item: TSMGridSetupItem): string;
  110.     function GetCaptionFont(Font: TFont): string;
  111.   public
  112.     { Public declarations }
  113.   end;
  114.  
  115. procedure Register;
  116.  
  117. implementation
  118. {$R *.DFM}
  119. uses SMCnst;
  120.  
  121. procedure Register;
  122. begin
  123.   RegisterComponents('SMComponents', [TSMSetDBGridDialog]);
  124. end;
  125.  
  126. const
  127.   clCream = TColor($A6CAF0);
  128.   clMoneyGreen = TColor($C0DCC0);
  129.   clSkyBlue = TColor($FFFBF0);
  130.  
  131.   ColorsInList = 46;
  132.   ColorValues: array [0..ColorsInList - 1] of TColor = (
  133.     clBlack, clMaroon, clGreen, clOlive, clNavy, clPurple, clTeal, clGray,
  134.     clSilver, clRed, clLime, clYellow, clBlue, clFuchsia, clAqua, clWhite,
  135.     clScrollBar, clBackground, clActiveCaption, clInactiveCaption,
  136.     clMenu, clWindow, clWindowFrame, clMenuText, clWindowText, clCaptionText,
  137.     clActiveBorder, clInactiveBorder, clAppWorkSpace, clHighlight,
  138.     clHighlightText, clBtnFace, clBtnShadow, clGrayText, clBtnText,
  139.     clInactiveCaptionText, clBtnHighlight, cl3DDkShadow, cl3DLight,
  140.     clInfoText, clInfoBk, clCream, clMoneyGreen, clSkyBlue, 0, 0);
  141.  
  142.  
  143. { TSMSetDBGridDialog }
  144. constructor TSMSetDBGridDialog.Create(AOwner: TComponent);
  145. begin
  146.   inherited Create(AOwner);
  147.  
  148.   FCaption := 'Grid setup';
  149.   FDBGrid := nil;
  150.   FAllowedFields := TStringList.Create;
  151. end;
  152.  
  153. destructor TSMSetDBGridDialog.Destroy;
  154. begin
  155.   FAllowedFields.Free;
  156.  
  157.   inherited Destroy;
  158. end;
  159.  
  160. procedure TSMSetDBGridDialog.SetAllowedFields(Value: TStrings);
  161. begin
  162.   FAllowedFields.Assign(Value);
  163. end;
  164.  
  165. function TSMSetDBGridDialog.Execute: Boolean;
  166. var
  167.   i, j: Integer;
  168.   item: TSMGridSetupItem;
  169.   TM: TTextMetric;
  170.   IsSMDBGrid: Boolean;
  171. begin
  172.   if Assigned(FOnBeforeExecute) then
  173.     FOnBeforeExecute(Self);
  174.  
  175.   Result := False;
  176.  
  177.   with TDBGrid(DBGrid), TfrmGridSetup.Create(Application) do
  178.     try
  179.       if not (Assigned(DataSource) and
  180.          Assigned(DataSource.DataSet)) then exit;
  181.  
  182.       Caption := FCaption;
  183.       btnOk.Caption := SBtnOk;
  184.       btnCancel.Caption := SBtnCancel;
  185.  
  186.       gbTitle.Caption := SgbTitle;
  187.       lblTitleCaption.Caption := STitleCaption;
  188.       lblTitleAlignment.Caption := STitleAlignment;
  189.       cbTitleAlignment.Items.Add(SAlignLeft);
  190.       cbTitleAlignment.Items.Add(SAlignRight);
  191.       cbTitleAlignment.Items.Add(SAlignCenter);
  192.       lblTitleColor.Caption := STitleColor;
  193.       lblTitleFont.Caption := STitleFont;
  194.  
  195.       gbData.Caption := SgbData;
  196.       lblDataAlignment.Caption := STitleAlignment;
  197.       cbDataAlignment.Items.Add(SAlignLeft);
  198.       cbDataAlignment.Items.Add(SAlignRight);
  199.       cbDataAlignment.Items.Add(SAlignCenter);
  200.       lblDataColor.Caption := STitleColor;
  201.       lblDataFont.Caption := STitleFont;
  202.  
  203.       lblWidth.Caption := SWidth;
  204.       lblWidthFix.Caption := SWidthFix;
  205.  
  206.  
  207.       {fill the field list}
  208.       with Columns do
  209.         for i := 0 to Count-1 do
  210.         begin
  211.           if (AllowedFields.Count = 0) or
  212.              (AllowedFields.IndexOf(Items[i].FieldName) > -1) then
  213.           begin
  214.             item := TSMGridSetupItem.Create;
  215.             item.TitleFont := TFont.Create;
  216.             item.DataFont := TFont.Create;
  217.  
  218.             item.FieldIndex := i;
  219.             item.FieldName := Items[i].FieldName;
  220.  
  221.             item.TitleAlignment := Items[i].Title.Alignment;
  222.             item.TitleCaption := Items[i].Title.Caption;
  223.             item.TitleColor := Items[i].Title.Color;
  224.             item.TitleFont.Assign(Items[i].Title.Font);
  225.  
  226.             item.DataAlignment := Items[i].Alignment;
  227.             item.DataColor := Items[i].Color;
  228.             item.DataFont.Assign(Items[i].Font);
  229.  
  230.             if (Items[i].Width > 0) then
  231.             begin
  232.               GetTextMetrics(Canvas.Handle, TM);
  233.               item.Width := (Items[i].Width - TM.tmOverhang - 4) div (Canvas.TextWidth('0') - TM.tmOverhang);
  234.             end;
  235.  
  236.  
  237.             j := clbFields.Items.AddObject(GetItemCaption(item), item);
  238.             {$IFDEF VER120} //D4
  239.             clbFields.Checked[j] := Items[i].Visible;
  240.             {$ELSE}
  241.             {$IFDEF VER125} //CB4
  242.             clbFields.Checked[j] := Items[i].Visible;
  243.             {$ELSE}
  244.             {$IFDEF VER130} //D5
  245.             clbFields.Checked[j] := Items[i].Visible;
  246.             {$ELSE}
  247.             clbFields.Checked[j] := True;
  248.             {$ENDIF}
  249.             {$ENDIF}
  250.             {$ENDIF}
  251.           end;
  252.         end;
  253.  
  254.       IsSMDBGrid := (Columns.Count > 0);
  255.       for i := 0 to DataSource.DataSet.FieldCount - 1 do
  256.         if (AllowedFields.Count = 0) or
  257.            (AllowedFields.IndexOf(DataSource.DataSet.Fields[i].FieldName) > -1) then
  258.         begin
  259.           for j := 0 to clbFields.Items.Count-1 do
  260.           begin
  261.             IsSMDBGrid := (TSMGridSetupItem(clbFields.Items.Objects[j]).FieldName = DataSource.DataSet.Fields[i].FieldName);
  262.             if IsSMDBGrid then
  263.               break;
  264.           end;
  265.           if not IsSMDBGrid then
  266.           begin
  267.             item := TSMGridSetupItem.Create;
  268.             item.TitleFont := TFont.Create;
  269.             item.DataFont := TFont.Create;
  270.  
  271.             item.FieldIndex := clbFields.Items.Count;
  272.             item.FieldName := DataSource.DataSet.Fields[i].FieldName;
  273.  
  274.             item.TitleAlignment := DataSource.DataSet.Fields[i].Alignment;
  275.             item.TitleCaption := DataSource.DataSet.Fields[i].DisplayName;
  276.             item.TitleColor := FixedColor;
  277.             item.TitleFont.Assign(Font);
  278.  
  279.             item.DataAlignment := DataSource.DataSet.Fields[i].Alignment;
  280.             item.DataColor := Color;
  281.             item.DataFont.Assign(Font);
  282.  
  283.             item.Width := DataSource.DataSet.Fields[i].DisplayWidth;
  284.  
  285.             j := clbFields.Items.AddObject(GetItemCaption(item), item);
  286.             clbFields.Checked[j] := False;
  287.           end;
  288.       end;
  289.  
  290.       clbFields.ItemIndex := 0;
  291.       clbFieldsClick(clbFields);
  292.  
  293.       if Assigned(FOnShow) then
  294.         FOnShow(Self);
  295.  
  296.       Result := (ShowModal = mrOk);
  297.       if Result then
  298.       begin
  299.         if (Columns.Count > 0) then
  300.         begin
  301.           Columns.Clear;
  302.           for i := 0 to clbFields.Items.Count-1 do
  303.             if clbFields.Checked[i] then
  304.             begin
  305.               item := TSMGridSetupItem(clbFields.Items.Objects[i]);
  306.               with Columns.Add do
  307.               begin
  308.                 FieldName := item.FieldName;
  309.                 Title.Alignment := item.TitleAlignment;
  310.                 Title.Caption := item.TitleCaption;
  311.                 Title.Color := item.TitleColor;
  312.                 Title.Font.Assign(item.TitleFont);
  313.  
  314.                 Alignment := item.DataAlignment;
  315.                 Color := item.DataColor;
  316.                 Font.Assign(item.DataFont);
  317.  
  318.                 if (item.Width > 0) then
  319.                 begin
  320.                   GetTextMetrics(Canvas.Handle, TM);
  321.                   Width := item.Width*(Canvas.TextWidth('0') - TM.tmOverhang)
  322.                              + TM.tmOverhang + 4;
  323.                 end;
  324.               end;
  325.             end;
  326.         end
  327.         else
  328.         begin
  329.           for i := 0 to clbFields.Items.Count-1 do
  330.           begin
  331.             item := TSMGridSetupItem(clbFields.Items.Objects[i]);
  332.             with DataSource.DataSet.Fields[i] do
  333.             begin
  334.               FieldName := item.FieldName;
  335.  
  336.               Alignment := item.DataAlignment;
  337.               DisplayLabel := item.TitleCaption;
  338.               Color := item.DataColor;
  339.               Font.Assign(item.DataFont);
  340.  
  341.               if (item.Width > 0) then
  342.               begin
  343.                 GetTextMetrics(Canvas.Handle, TM);
  344.                 DisplayWidth := item.Width*(Canvas.TextWidth('0') - TM.tmOverhang)
  345.                                  + TM.tmOverhang + 4;
  346.               end;
  347.               Visible := clbFields.Checked[i];
  348.             end;
  349.           end;
  350.         end;
  351.       end
  352.     finally
  353.       for i := clbFields.Items.Count-1 downto 0 do
  354.         with TSMGridSetupItem(clbFields.Items.Objects[i]) do
  355.         begin
  356.           TitleFont.Free;
  357.           DataFont.Free;
  358.           Free;
  359.         end;
  360.       Free
  361.     end;
  362.  
  363.   if Assigned(FOnAfterExecute) then
  364.     FOnAfterExecute(Self);
  365. end;
  366.  
  367. { TfrmGridSetup }
  368. procedure TfrmGridSetup.clbFieldsClick(Sender: TObject);
  369.  
  370.   function GetColorID(cl: TColor): Integer;
  371.   var
  372.     i: Integer;
  373.   begin
  374.     Result := -1;
  375.  
  376.     for i := 0 to ColorsInList do
  377.       if ColorValues[i] = cl then
  378.       begin
  379.         Result := i;
  380.         break;
  381.       end;
  382.     if Result < 0 then
  383.       Result := 0;
  384.   end;
  385.  
  386. begin
  387.   if clbFields.ItemIndex > -1 then
  388.     with TSMGridSetupItem(clbFields.Items.Objects[clbFields.ItemIndex]) do
  389.     begin
  390.       edTitleCaption.Text := TitleCaption;
  391.  
  392.       cbTitleAlignment.ItemIndex := Ord(TitleAlignment);
  393.       SMColorsCBTitle.ItemIndex := GetColorID(TitleColor);
  394.       edTitleFont.Font.Assign(TitleFont);
  395.       edTitleFont.Text := GetCaptionFont(edTitleFont.Font);
  396.  
  397.       cbDataAlignment.ItemIndex := Ord(DataAlignment);
  398.       SMColorsCBData.ItemIndex := GetColorID(DataColor);
  399.       edDataFont.Font.Assign(DataFont);
  400.       edDataFont.Text := GetCaptionFont(edDataFont.Font);
  401.  
  402.       edWidth.Text := IntToStr(Width);
  403.  
  404.       ColorValues[ColorsInList-2] := TitleColor;
  405.       ColorValues[ColorsInList-1] := DataColor;
  406.  
  407.       SMColorsCBTitleChange(SMColorsCBTitle)
  408.     end;
  409. end;
  410.  
  411. procedure TfrmGridSetup.clbFieldsDragDrop(Sender, Source: TObject; X,
  412.   Y: Integer);
  413. var intItemIndex, intNewItemIndex: Integer;
  414.     boolChecked: Boolean;
  415.     coordXY: TPoint;
  416. begin
  417.   with Source as TCheckListBox do
  418.   begin
  419.     intItemIndex := clbFields.ItemIndex;
  420.     coordXY.x := X;
  421.     coordXY.y := Y;
  422.     intNewItemIndex := clbFields.ItemAtPos(coordXY, True);
  423.     if (intNewItemIndex <> -1) then
  424.     begin
  425.       boolChecked := clbFields.Checked[intItemIndex];
  426.       clbFields.Items.Move(intItemIndex, intNewItemIndex);
  427.       clbFields.Checked[intNewItemIndex] := boolChecked;
  428.       clbFields.ItemIndex := intNewItemIndex;
  429.     end;
  430.   end;
  431. end;
  432.  
  433. procedure TfrmGridSetup.clbFieldsDragOver(Sender, Source: TObject; X,
  434.   Y: Integer; State: TDragState; var Accept: Boolean);
  435. begin
  436.   if (Source = clbFields) then
  437.     Accept := True
  438.   else
  439.     Accept := False;
  440. end;
  441.  
  442. procedure TfrmGridSetup.PropertyExit(Sender: TObject);
  443. var
  444.   item: TSMGridSetupItem;
  445. begin
  446.   if clbFields.ItemIndex > -1 then
  447.   begin
  448.     item := TSMGridSetupItem(clbFields.Items.Objects[clbFields.ItemIndex]);
  449.  
  450.     case (Sender as TControl).Tag of
  451.       1: begin
  452.            item.TitleCaption := edTitleCaption.Text;
  453.            clbFields.Items[clbFields.ItemIndex] := GetItemCaption(item);
  454.          end;
  455.       2: item.TitleAlignment := TAlignment(cbTitleAlignment.ItemIndex);
  456.       3: item.TitleColor := ColorValues[SMColorsCBTitle.ItemIndex];
  457.       4: item.TitleFont.Assign(edTitleFont.Font);
  458.  
  459.       5: item.DataAlignment := TAlignment(cbDataAlignment.ItemIndex);
  460.       6: if (SMColorsCBData.ItemIndex = SMColorsCBData.Items.Count-1) then
  461.            item.DataColor := ColorValues[SMColorsCBData.ItemIndex+1]
  462.          else
  463.            item.DataColor := ColorValues[SMColorsCBData.ItemIndex];
  464.       7: item.DataFont.Assign(edDataFont.Font);
  465.  
  466.       8, 9: item.Width := StrToIntDef(edWidth.Text, 0);
  467.       10: item.Width := 10;
  468.     end;
  469.   end;
  470. end;
  471.  
  472. procedure TfrmGridSetup.edTitleFontButtonClick(Sender: TObject);
  473. var cntr: TEdit;
  474. begin
  475.   if TButton(Sender) = btnTitleFont then
  476.     cntr := edTitleFont
  477.   else
  478.     cntr := edDataFont;
  479.  
  480.   with FontDlg do
  481.   begin
  482.     Font.Assign(cntr.Font);
  483.     if Execute then
  484.     begin
  485.       cntr.Font.Assign(Font);
  486.       cntr.Text := GetCaptionFont(Font);
  487.  
  488.       PropertyExit(Sender);
  489.     end;
  490.   end;
  491. end;
  492.  
  493. function TfrmGridSetup.GetItemCaption(item: TSMGridSetupItem): string;
  494. begin
  495.   Result := item.FieldName + '  :  ' + item.TitleCaption;
  496. end;
  497.  
  498. function TfrmGridSetup.GetCaptionFont(Font: TFont): string;
  499. begin
  500.   Result := Font.Name + ', ' + IntToStr(Font.Size);
  501. end;
  502.  
  503.  
  504. procedure TfrmGridSetup.SMColorsCBTitleDrawItem(Control: TWinControl;
  505.   Index: Integer; Rect: TRect; State: TOwnerDrawState);
  506. var
  507.   ARect: TRect;
  508.   Safer: TColor;
  509.   i: Integer;
  510. begin
  511.   ARect := Rect;
  512.   InflateRect(ARect, -2, -2);
  513.  
  514.   with (Control as TComboBox) do
  515.   begin
  516.     Canvas.FillRect(Rect);
  517.     Safer := Canvas.Brush.Color;
  518.     Canvas.Pen.Color := clWindowText;
  519.     Canvas.Rectangle(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom);
  520.     i := Index;
  521.     if (Control = SMColorsCBData) and
  522.        (i = SMColorsCBData.Items.Count-1) then
  523.       Inc(i);
  524.     Canvas.Brush.Color := ColorValues[i];
  525.     try
  526.       InflateRect(ARect, -1, -1);
  527.       Canvas.FillRect(ARect);
  528.  
  529. {      if (Index = Items.Count-1) then
  530.       begin
  531.         Canvas.Pen.Color := clWhite;
  532.         Canvas.TextOut(ARect.Left, ARect.Top, 'Custom...')
  533.       end;
  534. }    finally
  535.       Canvas.Brush.Color := Safer;
  536.     end;
  537.   end;
  538. end;
  539.  
  540. procedure TfrmGridSetup.miSelectAllClick(Sender: TObject);
  541. var
  542.   i, intTag: Integer;
  543. begin
  544.   intTag := TComponent(Sender).Tag;
  545.   for i := 0 to clbFields.Items.Count-1 do
  546.     case intTag of
  547.       1: //unselect all
  548.          clbFields.Checked[i] := False;
  549.       2: //invert selection
  550.          clbFields.Checked[i] := not clbFields.Checked[i];
  551.     else //select all
  552.       clbFields.Checked[i] := True;
  553.     end;
  554.   clbFields.Invalidate  
  555. end;
  556.  
  557. procedure TfrmGridSetup.SMColorsCBTitleClick(Sender: TObject);
  558. var
  559.   i: Integer;
  560. begin
  561.   with TCombobox(Sender) do
  562.     if (ItemIndex = ColorsInList-2) then
  563.     begin
  564.       with TColorDialog.Create(Self) do
  565.         try
  566.           i := ColorsInList-1;
  567.           if (Sender = SMColorsCBTitle) then
  568.             Dec(i);
  569.  
  570.           Color := ColorValues[i];
  571.           if Execute then
  572.             ColorValues[i] := Color
  573.         finally
  574.           Free
  575.         end
  576.     end;
  577. end;
  578.  
  579. procedure TfrmGridSetup.SMColorsCBTitleChange(Sender: TObject);
  580. begin
  581.   edTitleFont.Color := ColorValues[SMColorsCBTitle.ItemIndex];
  582.   if (SMColorsCBData.ItemIndex = SMColorsCBData.Items.Count-1) then
  583.     edDataFont.Color := ColorValues[SMColorsCBData.ItemIndex+1]
  584.   else
  585.     edDataFont.Color := ColorValues[SMColorsCBData.ItemIndex];
  586. end;
  587.  
  588. end.
  589.