unit JkPanel;

interface
uses
	Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
  	ExtCtrls,dbctrls,stdctrls,db,ADODB,ComCtrls,Variants,Gauges,
   SqlExpr,Grids,DBGrids,MovePanel,JKFootpanel,Buttons,dsr;

type
   TEditorstyle = (TsMemo,Tscombox,Tsedit);

type
  TJkPanel = class(TPanel)
  private
    { Private declarations }
    This_Jth :string; //机台编号
    This_TaskOrder :string; //任务序次

    FEditorstyle:Teditorstyle;
    FLeft :Integer;
    FTop :Integer;
    maxTextLen :Integer;
    maxLabelLen :Integer;
    maxDgheight :Integer;
    maxFootHeight :integer;
    FTitleVisible :Boolean;
    FEditorVisible :Boolean;
    FDataReadOnly :Boolean;
    FPageCount :Integer;

    FPass_Grid :TDBGrid;
    FP_Move :TMovePanel;
    FP_Parent :TPanel;

    ColorLabels :array of TLabel;
    Co_Shapes :array of TShape;
    Co_DllBtn :array of TSpeedButton;
    Co_Panels :array of TPanel;

    FPageControl: TPageControl; {分页控件}
    FTabSheets :array of TTabSheet;

    ScrollBoxs :array of TScrollBox; {滚动控件}
    Splitters :array of TSplitter; {分割控件}
    FootPanels :array of TJKFootpanel; {Foot页显示控件}
    FLineHeight :Integer;

    //数据数组控件，动态生成
    MemoEditors :array of TMemo;
    comEditors :array of TCombobox;
    edEditors :array of Tedit;
    
    DGLists :array of TDbGrid;
    AdoLists :array of TadoDataset;
    DsLists :array of TDataSource;

    DGGlides :array of TDbGrid;
    AdoGlides :TadoDataset;
    DsGlides :TDataSource;

    ProgressEditor :array of Tedit;
    Labels :array of TLabel;  //字段标题，动态生成
    
    ProgressBars :array of TGauge;
    Shapes :array of TShape;
    FDataSource :TDataSource; // 数据源
    FDataField_A  :String;  // DataField
    FDataField_B :String;  // DataField
    FDataField_C :String;  // DataField
    FDataField_ColA :String;  // DataField
    FDataField_ColB :String;  // DataField
    Fcnnstr :String;
    FPageSql :string;
    FListSql :String;
    FFootSql :string;
    FGlideSql :String;
    FCheJianFieldName :String;
    
    FColumns :Integer; //显示列数
    tmpado :TadoDataset;
    tmpDs :TDataSource;
    TmpDgListIdx,TmpDgListCol:integer;
    
    procedure FreeEditors; //释放数据输入控件的内存
    procedure AKeyDown(Sender:TObject; var Key :Word; Shift:TShiftState);
    procedure AKeyPress(Sender:TObject; var Key :Char);
    procedure AProgressEditorChange(Sender :TObject);
    //procedure inti_Grid(sender :TObject);
    procedure LabelsClick(Sender: TObject);
    procedure Co_DllBtnClick(Sender: TObject); 
    procedure AdolistsAfterScroll(DataSet: TDataSet);
    procedure DGListsCellClick(Column: TColumn);
    procedure DGListsDrawColumnCell(Sender: TObject;
      const Rect: TRect;DataCol: Integer;
      Column: TColumn;State: TGridDrawState);
   procedure refreshFootpanel(Tag :integer);
   procedure DGGlideDrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);

    procedure FP_MovePanelDblClick(Sender: TObject);
    procedure FootPanelDblclick(Sender: TObject);
    //function comEditor(Index :Integer):TComboBox;
    //function edEditor(Index :Integer):Tedit;
    //function MemoEditor(Index :Integer) :TMemo;
  protected
    { Protected declarations }

  public
      constructor Create(AOwner:TComponent); override;
  //    destructor Destroy; override;
    	procedure Setedit(Value  :TEditorstyle);
      procedure CreateEditors(var DS :TDataSource; cnnstr :String;
          Const Defaultsheet :String = '');  //创建各字段的数据输入控件
    	procedure ClearHits(ItemIndex :Integer);
    	procedure AddHits(ItemIndex:Integer; Hits :array of string);

    { Public declarations }
    published

    	property LimitLeft :Integer read FLeft write FLeft default 10;
    	property LimitTop :Integer read FTop write FTop default 10;
    	property Editorstyle :TEditorstyle read FEditorstyle write Setedit default TsMemo;

    	property EditorWidth :Integer read maxTextLen write maxTextLen  default 100;
    	property TitleWidth :Integer read maxLabelLen write maxLabelLen  default 100;
      property DgHeight :Integer read maxDgheight write maxDgheight  default 300;
      property FootHeight :Integer read maxFootHeight write maxFootHeight  default 200;
    	property TitleVisible :Boolean read FTitleVisible write FTitleVisible default True;
      property EditorVisible :Boolean read FEditorVisible write FEditorVisible default True;
      property DataReadOnly :Boolean read FDataReadOnly write FDataReadOnly; //default True;
    	property LineHeight :Integer read FLineHeight write FLineHeight  default 15;
    	property DataSource :TDataSource read FDataSource write FDataSource; //数据源
      property DataField_ColA :String read FDataField_A write FDataField_ColA;
      property DataField_ColB :String read FDataField_B write FDataField_ColB;
      property DataField_Editor :String read FDataField_A write FDataField_A;
      property DataField_Title :String read FDataField_B write FDataField_B;
      property DataField_Progress :String read FDataField_C write FDataField_C;

      property Data_PageSql :String read FPageSql write FPageSql;
      property Data_DgListSql :String read FListSql write FListSql;
      property Data_FootSql :String read FFootSql write FFootSql;
      property Data_GlideSql :String read FGlideSql write FGlideSql;
      property Data_CheJianField :String read FCheJianFieldName write FCheJianFieldName;
      property Columns :Integer read FColumns write FColumns default 4;//表列数

    { Published declarations }
  end;

procedure Register;

implementation


constructor TJkPanel.Create(AOwner :TComponent);
begin
   Inherited Create(AOWner);
   FLeft := 20;
   FTop := 20;
   maxTextLen := 250;
   maxLabelLen := 250;
   maxDgheight := 200;
   maxFootHeight := 150;
   FLineHeight := 15;
   FTitleVisible := True;
   FDataReadOnly := True;
end;

procedure TJkPanel.Setedit(Value  :TEditorstyle);
begin
  	if FEditorstyle <> Value then
   begin
   	FEditorstyle := Value;
   	Invalidate;
   end;
end;

{ 为第I字段增加提示信息的方法}
procedure TJkPanel.AddHits(ItemIndex :Integer; Hits :array of string);
var
  m,n,i :Integer;
begin

	if FEditorstyle = Tscombox then
 	begin
   	n := Length(comEditors);
    	m := Length(Hits);
   	if ItemIndex< n then
      for i:= 0 to m - 1 do
        	comEditors[ItemIndex].Items.Add(Hits[i]);
	end
  	else if FEditorstyle = Tsedit then
  	begin
   	n := Length(edEditors);
    	m := Length(Hits);
   	if ItemIndex< n then
      for i:=0 to m-1 do
      	edEditors[ItemIndex].Hint:= Hits[i];
   end
  	else if FEditorstyle = TsMemo then
  	begin
   	n := Length(memoEditors);
    	m := Length(Hits);
   	if ItemIndex< n then
      for i:=0 to m-1 do
      	memoEditors[ItemIndex].Hint:= Hits[i];
   end;
end;

procedure TJkPanel.AKeyDown(Sender :TObject; var Key :Word; Shift :TShiftState);
begin
   //
end;

procedure TJkPanel.AProgressEditorChange(Sender :TObject);
begin
   //
end;

procedure TJkPanel.AKeyPress(Sender :TObject; var Key :Char);
begin
   if (Sender is TComboBox) or (Sender is Tedit) or (Sender is TMemo) then
   if Key=#13 then
   	(Owner as TForm).Perform(WM_NEXTDLGCTL, 0, 0);
end;

procedure TJkPanel.ClearHits(ItemIndex :Integer);
var
  n :Integer;
begin
 	if FEditorstyle = Tscombox then
  	begin
   	n := Length(comEditors);
   	if ItemIndex< n then comEditors[ItemIndex].Items.Clear;
   end
   else if FEditorstyle = Tsedit then
   begin
      n := Length(edEditors);
      if ItemIndex< n then edEditors[ItemIndex].Hint:='';;
   end
   else if FEditorstyle = TsMemo then
   begin
      n := Length(MemoEditors);
      if ItemIndex< n then MemoEditors[ItemIndex].Hint:='';;
   end;

end;


{ 创建各字段的数据输入控件的方法}
procedure TJkPanel.CreateEditors(var DS :TDataSource; cnnstr :String; Const Defaultsheet :String = '');
var
   Tmp_CheJian,Tmp_Parm :String;
   i,j,n,k,This_Index,TextHeight :Integer;
   tmp_col0,Tmp_Row0,Tmp_Row1 :Integer;
   XXX :TStringList;
   kkk,RRR :array of integer;
   DefaultIndex :Integer; //默认车间
begin
   if (Data_CheJianField = '') then exit;
   { 释放全部控件内存}
   FreeEditors;
	if DS = nil then exit;
   if DataSource = nil then FDataSource := Ds;
   if not DataSource.DataSet.Active then exit;
   if  (DataSource.DataSet is TAdoDataSet) = False then exit;
   TmpDgListIdx := -1;
   FPageCount := 0;
   DefaultIndex := 0;
   n := DataSource.DataSet.RecordCount;
   if n <= 0 then exit;
   DataSource.DataSet.DisableControls;
   if TitleWidth < maxTextLen then
      maxTextLen := TitleWidth;
    	
   { 计算最大的标题长度及显示长度}
   DataSource.DataSet.First;
   { 计算高度}
   TextHeight := Canvas.TextHeight(DataSource.DataSet.Fields[0].DisplayLabel) + LineHeight; //10;

   { 分配内存}
   SetLength(AdoLists,n);
   SetLength(DsLists,n);
   SetLength(DGLists,n);

   SetLength(Labels,n);
   SetLength(ColorLabels,n);
   SetLength(ProgressEditor,n);
   SetLength(Shapes,n);

   { 创建数据感知控件 AdoGlides、DsGlides}
   AdoGlides :=  TadoDataset.Create(owner);
   DsGlides :=  TDatasource.Create(owner);
   DsGlides.DataSet := AdoGlides;

   if Columns = 0 then Columns := 6;
   try
      tmpado := TadoDataset.Create(Owner);
      tmpDs := TDatasource.Create(Owner);
      tmpDs.DataSet := tmpado;
      with tmpado do
      begin
         Active := False;
         Fcnnstr := cnnstr;
         tmpado.ConnectionString := Fcnnstr;
         tmpado.CommandText := Data_PageSql;
         Active := True;
         FPageCount := RecordCount;
         if FPageCount = 0 then exit;

         setlength(kkk,FPageCount);
         for i := 0 to high(kkk) do
            kkk[i] := -1;
         SetLength(RRR,FPageCount);
         for i := 0 to high(RRR) do
            RRR[i] := 0;
         first;
         // 创建PageControl
         FPageControl := TPageControl.Create(Owner);
         FPageControl.Parent := Self;
         FPageControl.Font.Name := '宋体';
         FPageControl.Font.Size := 9;
         FPageControl.Align := alClient;
         FPageControl.Visible := False;
         
         { 分配载体内存}
         setlength(FTabSheets,FPageCount+1);
         setlength(ScrollBoxs,FPageCount+1);
         setlength(FootPanels,FPageCount+1);
         setlength(Splitters,FPageCount+1);
         setlength(Co_Shapes,FPageCount+1);
         setlength(Co_DllBtn,FPageCount+1);
         setlength(Co_Panels,FPageCount+1);
         SetLength(DGGlides,FPageCount+1);
         
         XXX := TStringList.Create();
         for j := 0 to FPageCount  do
         begin
            { FPageControl分页}
            FTabSheets[j] := TTabSheet.Create(Owner);
            FTabSheets[j].Parent := FPageControl;
            FTabSheets[j].ParentFont := True;
            FTabSheets[j].PageControl := FPageControl;
            FTabSheets[j].Visible := True;
            FTabSheets[j].PageIndex := j;

            if j < FPageCount then
            begin
               Tmp_CheJian := trim(fieldByName(Data_CheJianField).AsString);
               if Defaultsheet <> '' then  //指定默认车间
               begin
                  if Tmp_CheJian = Defaultsheet then
                     DefaultIndex := j;
               end;
               FTabSheets[j].Caption := '车间：' + Tmp_CheJian;
               FTabSheets[j].Hint := Tmp_CheJian;
               XXX.Append(Tmp_CheJian);
            end
            else
            begin
               Tmp_CheJian := '';
               FTabSheets[j].Caption := '未指定车间';
               FTabSheets[j].Hint := Tmp_CheJian;
               XXX.Append('未指定车间');
            end;
            FTabSheets[j].ShowHint := False;
            FTabSheets[j].Visible := True;
            FTabSheets[j].Align := alClient;

            //创建Foot页显示控件
    	      FootPanels[j] := TJKFootpanel.Create(Owner);
            with FootPanels[j] do
            begin
               Visible := True;
               Parent := FTabSheets[j];
              // BevelInner := bvLowered;
              //BevelOuter := bvRaised;
               Left := 0;
               Width := 1;
    	         Align := alRight;
               Hint := FTabSheets[j].Hint;
               ShowHint := False;
               onDblclick := FootPanelDblclick;
               LimitLeft := 8;
               Ctl3D := False;
            end;

            //创建颜色块显示控件载体
    	      Co_Panels[j] := Tpanel.Create(Owner);
            with Co_Panels[j] do
            begin
               Visible := True;
               Parent := FootPanels[j];
               BevelInner := bvLowered;
               BevelOuter := bvRaised;
               height := 40;
               Co_Panels[j].BorderWidth := 2;
    	         Align := alTop;
            end;

            //创建颜色块显示控件
            Co_Shapes[j] := TShape.Create(Owner);
            with Co_Shapes[j] do
            begin
               Visible := True;
               Parent := Co_Panels[j];
               Width := FootHeight;
    	         Align := alClient;
            end;

            //创建按钮控件
            Co_DllBtn[j] := TSpeedButton.Create(Owner);
            with Co_DllBtn[j] do
            begin
               Visible := True;
               Parent := FootPanels[j];
               height := 25;
               Tag := j;
               Font.name := '宋体';
               Font.Size := 9;
    	         Align := alTop;
               Font.Color := ClBlue;
               //Flat := true;
               caption := '流程处理';
               OnClick := Co_DllBtnClick;
            end;

        { 创建DbGrid控件 DGGlides}
         DGGlides[j]:= TDBGrid.Create(owner);
         with DGGlides[j] do
         begin
            Visible := True;
            Parent := FootPanels[j];
            height := 110;
            Align := alTop;
            Tag := j;
            Font.name := '宋体';
            Font.Size := 9;
            ReadOnly := True;
            Options := [dgEditing,dgMultiSelect]; //,dgRowLines,dgColumnResize,
            FixedColor := clSkyBlue;
            OnDrawColumnCell := DGGlideDrawColumnCell;
         end;

            //创建分割控件
    	      Splitters[j] := TSplitter.Create(Owner);
            with Splitters[j] do
            begin
               Visible := True;
               Parent := FTabSheets[j];
               Width := 4;
               Align := alRight;
            end;

    	      //创建滚动盒
    	      ScrollBoxs[j] := TScrollBox.Create(Owner);
            with ScrollBoxs[j] do
            begin
               Visible := True;
    	         Parent := FTabSheets[j];
               Color := RGB(100,100,100);//clTeal;
    	         Align := alClient;
               Hint := FTabSheets[j].Hint;
               ShowHint := False;
            end;

            next;
         end;
         
      end;

    	if FEditorstyle = Tscombox then
    		SetLength(comEditors,n)
    	else if FEditorstyle = Tsedit then
    		SetLength(edEditors,n)
    	else
    		SetLength(MemoEditors,n);


    	{ 创建编辑}
    	for i := 0 to n - 1 do
    	begin
         if DataSource.DataSet.Fieldbyname(Data_CheJianField).AsVariant = null then
            This_Index := FPageCount
         else
            This_Index := XXX.IndexOf(trim(DataSource.DataSet.Fieldbyname(Data_CheJianField).Asstring));
         if kkk[This_Index] >= Columns -1 then
         begin
            kkk[This_Index] := 0;
            RRR[This_Index] := RRR[This_Index] +1;
         end
         else
            kkk[This_Index] := kkk[This_Index] + 1;
         tmp_col0 := kkk[This_Index];
         tmp_Row0 := RRR[This_Index];

         { 创建数据感知控件 AdoLists、DsLists}
         AdoLists[i] :=  TadoDataset.Create(owner);
         AdoLists[i].Tag := i;
         AdoLists[i].AfterScroll:= AdolistsAfterScroll;
         DsLists[i] :=  TDatasource.Create(owner);
         DsLists[i].DataSet := AdoLists[i];
         DsLists[i].Tag := i;
         //DsLists[i].OnDataChange := DsListsDataChange;
      	{ 创建标题}
      	Labels[i] := TLabel.Create(owner);
         Labels[i].visible := FTitleVisible;
      	Labels[i].Parent := (ScrollBoxs[This_Index] as TScrollBox);
         Labels[i].Font.Name := '宋体';
         Labels[i].Font.Size := 9;
         Labels[i].Font.Color := ClBlue;
         Labels[i].OnClick := LabelsClick;
         Labels[i].Tag := i;
         //Labels[i].Font.Style := [FsBold];
         Labels[i].Transparent := True;

         //机台名称
         if DataSource.DataSet.Fieldbyname(DataField_Title).AsVariant <> null then
      		Labels[i].caption := DataSource.DataSet.Fieldbyname(DataField_Title).AsString
         else
            Labels[i].caption := '';
         Tmp_CheJian := trim((ScrollBoxs[This_Index] as TScrollBox).Hint);

         //机台编号
         if DataSource.DataSet.Fieldbyname(DataField_ColA).AsVariant <> null then
            Labels[i].Hint := trim(DataSource.DataSet.Fieldbyname(DataField_ColA).AsString)
         else
            Labels[i].Hint := '';
         Labels[i].ShowHint := False;

         if FEditorstyle = TsMemo then
         begin
      		Labels[i].Top := FTop + tmp_Row0 * (TextHeight*3+DgHeight+5) + 2;
            Labels[i].Left := FLeft + (TitleWidth + 20) * tmp_Col0 + 5;
            Labels[i].Width := TitleWidth;
         end
         else
         begin
         	Labels[i].Top := FTop + tmp_Row0 * (TextHeight*2+DgHeight+5) + 2;
            Labels[i].Left := FLeft + (TitleWidth + 12) * tmp_Col0;
            Labels[i].Width := TitleWidth;
         end;

      	{ 创建信息显示数据对象}
	 		if FEditorstyle = Tscombox then
    		begin
      		comEditors[i] := TComboBox.Create(Owner);
      		comEditors[i].Parent := ScrollBoxs[This_Index]; //Self;
      		comEditors[i].Left := Labels[i].Left; //+ Labels[i].Width;
      		comEditors[i].Width := maxTextLen;
            if FTitleVisible then
      		   comEditors[i].Top := Labels[i].Top+14
            else
               comEditors[i].Top := Labels[i].Top;

            if DataSource.DataSet.Fieldbyname(DataField_Editor).AsVariant <> null then
 					comEditors[i].Text := DataSource.DataSet.Fieldbyname(DataField_Editor).AsString;
      		comEditors[i].OnKeyPress := OnKeyPress;
      		comEditors[i].OnKeyDown := OnKeyDown;
            comEditors[i].Visible := EditorVisible;
            comEditors[i].Tag := i;

         	DGLists[i] := TDbGrid.Create(Owner);
         	DGLists[i].Parent := ScrollBoxs[This_Index];
            DGLists[i].Font.name := '宋体';
            DGLists[i].Font.Size := 9;
            DGLists[i].ReadOnly := True;
            DGLists[i].FixedColor := clSkyBlue;
         	DGLists[i].Left := comEditors[i].Left;
         	DGLists[i].Width := comEditors[i].Width;
         	DGLists[i].Height := DgHeight;
            DGLists[i].Options := [dgRowSelect];
            if EditorVisible then
         	   DGLists[i].Top := comEditors[i].Top+comEditors[i].height+2
            else
         	   DGLists[i].Top := comEditors[i].Top+2;
            DGLists[i].Hint := trim(Labels[i].caption);
            DGLists[i].ShowHint := False;

            DGLists[i].OnDrawColumnCell := DGListsDrawColumnCell;
            DGLists[i].OnCellClick := DGListsCellClick;
            DGLists[i].Tag := i;
         	ColorLabels[i] := TLabel.Create(Owner);
         	ColorLabels[i].Parent := ScrollBoxs[This_Index];
            ColorLabels[i].AutoSize := true;
            ColorLabels[i].Font.name := '宋体';
            ColorLabels[i].Font.Size := 9;
            ColorLabels[i].Top := 	Labels[i].top;
         	ColorLabels[i].Left := Labels[i].Left + round(Labels[i].Width/2)+20;
         	ColorLabels[i].Width := round(Labels[i].Width/2)-20;
            ColorLabels[i].BringToFront;
            ColorLabels[i].Tag := i;
            ColorLabels[i].Font.Color := ClWhite;
            ColorLabels[i].Color := RGB(100,100,100);
            ColorLabels[i].Caption := '待机中';
    		end
    		else if FEditorstyle = Tsedit then
    		begin
      		edEditors[i] := Tedit.Create(Owner);
      		edEditors[i].Parent := ScrollBoxs[This_Index];
      		edEditors[i].Left := Labels[i].Left; //+ Labels[i].Width;
      		edEditors[i].Width := maxTextLen;
            if FTitleVisible then
      		   edEditors[i].Top := Labels[i].Top+14
            else
               edEditors[i].Top := Labels[i].Top;
            edEditors[i].ReadOnly := DataReadOnly;
            if DataSource.DataSet.Fieldbyname(DataField_Editor).AsVariant <> null then
            	edEditors[i].Text := DataSource.DataSet.Fieldbyname(DataField_Editor).AsString;
      		edEditors[i].OnKeyPress := OnKeyPress;
      		edEditors[i].OnKeyDown := OnKeyDown;
            edEditors[i].Visible := EditorVisible;
         	DGLists[i] := TDbGrid.Create(Owner);
         	DGLists[i].Parent := ScrollBoxs[This_Index];
            DGLists[i].Font.name := '宋体';
            DGLists[i].Font.Size := 9;
            DGLists[i].ReadOnly := True;
            DGLists[i].Options := [dgRowSelect];
            DGLists[i].FixedColor := clSkyBlue;
         	DGLists[i].Left := edEditors[i].Left;
         	DGLists[i].Width := edEditors[i].Width;
         	DGLists[i].Height := DgHeight;
            DGLists[i].Tag := i;
            if EditorVisible then
         	   DGLists[i].Top := edEditors[i].Top+edEditors[i].height+2
            else
         	   DGLists[i].Top := edEditors[i].Top+2;
            DGLists[i].Hint := trim(Labels[i].caption);
            DGLists[i].ShowHint := False;
            DGLists[i].OnDrawColumnCell := DGListsDrawColumnCell;
            DGLists[i].OnCellClick := DGListsCellClick;
            DGLists[i].Tag := i;

         	ColorLabels[i] := TLabel.Create(Owner);
         	ColorLabels[i].Parent := ScrollBoxs[This_Index];
            ColorLabels[i].AutoSize := False;
            ColorLabels[i].Font.name := '宋体';
            ColorLabels[i].Font.Size := 9;
            ColorLabels[i].Top := 	Labels[i].top;
         	ColorLabels[i].Left := Labels[i].Left + round(Labels[i].Width/2)+20;
         	ColorLabels[i].Width := round(Labels[i].Width/2)-20;
            ColorLabels[i].BringToFront;
            ColorLabels[i].Tag := i;
            ColorLabels[i].Font.Color := ClWhite;
            ColorLabels[i].Color := RGB(100,100,100);
            ColorLabels[i].Caption := '待机中';
    		end
    		else
    		begin
      		MemoEditors[i] := Tmemo.Create(Owner);
      		MemoEditors[i].Parent := ScrollBoxs[This_Index];
      		MemoEditors[i].Left := Labels[i].Left;
      		MemoEditors[i].Width := maxTextLen;
            if FTitleVisible then
      		   MemoEditors[i].Top := Labels[i].Top+14
            else
               MemoEditors[i].Top := Labels[i].Top;
            MemoEditors[i].Height := 60;
            MemoEditors[i].ReadOnly := DataReadOnly;
            if DataSource.DataSet.Fieldbyname(DataField_Editor).AsVariant <> null then
            	MemoEditors[i].Lines.Add(DataSource.DataSet.Fieldbyname(DataField_Editor).AsString);
      		MemoEditors[i].OnKeyPress := OnKeyPress;
      		MemoEditors[i].OnKeyDown := OnKeyDown;
            MemoEditors[i].Visible := EditorVisible;
            MemoEditors[i].Tag := i;
         	DGLists[i] := TDbGrid.Create(Owner);
         	DGLists[i].Parent := ScrollBoxs[This_Index];
            DGLists[i].Font.name := '宋体';
            DGLists[i].Font.Size := 9;
            DGLists[i].ReadOnly := True;
            DGLists[i].FixedColor := clSkyBlue;
         	DGLists[i].Left := MemoEditors[i].Left;
         	DGLists[i].Width := MemoEditors[i].Width;
         	DGLists[i].Height := DgHeight;
            DGLists[i].Options := [dgRowSelect];
            if EditorVisible then
         	   DGLists[i].Top := MemoEditors[i].Top+MemoEditors[i].height+2
            else
         	   DGLists[i].Top := MemoEditors[i].Top+2;
            DGLists[i].Hint := trim(Labels[i].caption);
            DGLists[i].ShowHint := False;
            DGLists[i].OnDrawColumnCell := DGListsDrawColumnCell;
            DGLists[i].OnCellClick := DGListsCellClick;
            DGLists[i].Tag := i;
            
         	ColorLabels[i] := TLabel.Create(Owner);
         	ColorLabels[i].Parent := ScrollBoxs[This_Index];
            ColorLabels[i].AutoSize := False;
            ColorLabels[i].Font.name := '宋体';
            ColorLabels[i].Font.Size := 9;
         	ColorLabels[i].Left := DGLists[i].Left + DGLists[i].Width +1;
         	ColorLabels[i].Height := DGLists[i].Height + MemoEditors[i].Height;
         	ColorLabels[i].Top := DGLists[i].Top;
            ColorLabels[i].Tag := i;
            ColorLabels[i].Font.Color := ClWhite;
            ColorLabels[i].Color := RGB(100,100,100);
            ColorLabels[i].Caption := '待机中';
    		end;
         Tmp_Parm := trim(DataSource.DataSet.Fieldbyname(DataField_Editor).Value);
         with AdoLists[i] do
         begin
            DisableConTrols;
            Active := False;
            ConnectionString := Fcnnstr; //数据库连接串
            CursorType := ctStatic;
            LockType := ltReadOnly;
            CommandText := Data_DgListSql;
            Parameters.ParamValues['ListParm'] := Tmp_Parm;
            Active := True;
 {
   tmpSql := 'select a.AutoID,case a.MachMiss when ''0000000000'' then '
      + 'convert(varchar(20),f.GlideName) else convert(varchar(20),d.ShortName) end'
      + ' as ShortName,rtrim(b.MP_Crock) as MP_Crock,'
      + 'convert(varchar(20),c.ChnName) as ChnName,a.MachLen,'
      + 'convert(varchar(20),f.GlideName) as GlideName,'
      + 'a.MachMiss,a.TaskOrder,a.Status,'
      + 'c.RedCode,c.GreenCode,c.BlueCode'
      + ' from MD_Dev_Mission a '+#10#13
      + ' left join MD_PlanCrock b on b.mp_autocrk = left(a.MachMiss,10)'
      + ' left join Mc_color c on c.ColorId = b.MP_Color '
      + ' left join Mc_Glide f on f.GlideNo = a.MachGlide'+#10#13
      + ' left join BC_Customer d on d.customno = b.MP_Customno'+#10#13
      + ' where a.Status <> ''6'''
      + ' and a.Machine =:ListParm'+#10#13
      + ' order by a.Machine,a.TaskOrder';
     }
            for k := 0 to Fields.Count -1 do
               Fields[k].Visible := False;
            Fieldbyname('ShortName').Visible := True; //加工单位
            Fieldbyname('MP_Crock').Visible := True;  //助记缸号
            Fieldbyname('ChnName').Visible := True;   //颜色
            Fieldbyname('MachLen').Visible := True; //单缸重量
            Fieldbyname('GlideName').Visible := True; //工序
            DGLists[i].DataSource := DsLists[i];
            EnableConTrols;
            DGLists[i].Columns[0].width := 50;
            DGLists[i].Columns[1].width := 30;
            DGLists[i].Columns[2].width := 55;
            DGLists[i].Columns[3].width := 45;
            DGLists[i].Columns[4].width := 50;
            //DGLists[i].Columns[0].Alignment := TaCenter;
            //DGLists[i].Columns[1].Alignment := TaCenter;
            //DGLists[i].Columns[2].Alignment := TaCenter;
            //DGLists[i].Columns[3].Alignment := TaCenter;
            //DGLists[i].Columns[4].Alignment := TaCenter;
            First;
         end;

      	Shapes[i] := TShape.Create(Owner);
      	Shapes[i].Parent := ScrollBoxs[This_Index];
         Shapes[i].Left := Labels[i].Left - 5;
         Shapes[i].top := Labels[i].Top  - 5;
         if FEditorstyle = TsMemo then
         begin
         	Shapes[i].height := ColorLabels[i].height + Labels[i].height +20 +10;
            Shapes[i].Width := (ColorLabels[i].left - Labels[i].left) + ColorLabels[i].Width + 10;
         end
         else
         begin
         	Shapes[i].height := (DGLists[i].Top + DGLists[i].Height - Labels[i].Top)+ 10;
            Shapes[i].Width := Labels[i].Width +10;
         end;
         Shapes[i].Brush.Color := clSkyBlue;
         Shapes[i].Visible := True;
         Shapes[i].SendToBack;
         Shapes[i].Tag := i;
         if not DataSource.DataSet.Eof then
         	DataSource.DataSet.next;
  		end;
      DataSource.DataSet.EnableControls;
      tmpado.Close;
      XXX.Free;
      if FPageControl.PageCount > 0 then
      begin
         FPageControl.ActivePageIndex := FPageControl.PageCount-1;
         FPageControl.ActivePageIndex := DefaultIndex;
      end;
	Finally
      FPageControl.Visible := True;
   end;
end;

procedure TJkPanel.Co_DllBtnClick(Sender: TObject);
var
   this_Gh :String;
   k :Integer;

begin
   if not CanLCCL then  //判断是否有权操作此项功能
   begin
      application.messagebox('对不起，目前您还没有被授权操作此项功能。','信息提示',mb_ok or MB_ICONInformation);
      Exit;
   end;
   k := (sender as TSpeedButton).Tag;
   if not FootPanels[k].DataSource.dataset.active then exit;
   if Co_DllBtn[k].Caption = '流程处理' then
   begin
      if FootPanels[k].DataSource.dataset.FieldValues['缸号'] = null then exit;
      this_Gh := FootPanels[k].DataSource.dataset.fieldbyname('缸号').AsString;
      //执行流程处理
      DoAbnormalC(TmpHandle,cnn_Base,V_UserId,V_User,this_Gh);
   end
   else
   begin
      //执行洗缸领料
      if This_Jth <> '' then  
         DoRinseprt(TmpHandle,cnn_Base,V_UserId,V_User,This_Jth,This_TaskOrder);
   end;
end;  

procedure TJkPanel.DGListsDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
var
   Redcode,GreenCode,BlueCode,TmpStatus :Integer;
begin
  	with (Sender as TdbGrid) do
  	begin
      if DataSource = nil then exit;
      if not DataSource.DataSet.Active then exit;
      Redcode := (DataSource.DataSet as TAdoDataset).fieldByName('Redcode').AsInteger;
      Greencode := (DataSource.DataSet as TAdoDataset).fieldByName('Greencode').AsInteger;
      Bluecode := (DataSource.DataSet as TAdoDataset).fieldByName('Bluecode').AsInteger;
      TmpStatus := (DataSource.DataSet as TAdoDataset).fieldByName('Status').AsInteger;
      if gdFocused in State then 
      begin
         Canvas.Brush.Color := clblack;
         Canvas.Font.Color := Clred;
      end
      else if TmpStatus = 5 then
      begin
         Canvas.Font.Color := clBlue;
         Canvas.Brush.Color := clWhite;
         Canvas.Font.Style := [fsBold]; //fsItalic
         ColorLabels[(Sender as TDbGrid).Tag].Color := RGB(Redcode,Greencode,Bluecode);
         if (Redcode <=10) or (Bluecode <=10) or (Greencode <=10) then
            ColorLabels[(Sender as TDbGrid).Tag].Font.Color := ClWhite
         else
            ColorLabels[(Sender as TDbGrid).Tag].Font.Color := ClBlack;
         ColorLabels[(Sender as TDbGrid).Tag].Caption := '生产中';
      end
      else if (Redcode <=100) or (Greencode <=100) then
      begin
         Canvas.Font.Color := ClWhite;
         Canvas.Brush.Color := RGB(Redcode,Greencode,Bluecode);
      end
      else
      begin
         Canvas.Font.Color := ClBlack;
         Canvas.Brush.Color := RGB(Redcode,Greencode,Bluecode);
      end;
     	DefaultDrawColumnCell(Rect,DataCol,Column,State);
  	end;
end;

procedure TJkPanel.DGGlideDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
   with (Sender as TDbGrid) do
   begin
      if DataSource = nil then exit;
      if not DataSource.DataSet.Active then exit;
      if gdFocused in State then exit;
      if (Sender as TDbGrid).SelectedRows.CurrentRowSelected then
      begin
         Canvas.Brush.Color := clHighlight;
         Canvas.Font.Color := ClWhite;
      end
      else if DataSource.DataSet.RecNo mod 2 = 0 then
      begin
         Canvas.Brush.Color := $00DEDEDE;
         Canvas.Font.Color := ClBlack;
      end
      else
      begin
         Canvas.Brush.Color := clwhite;
         Canvas.Font.Color := ClBlack;
      end;
      DefaultDrawColumnCell(Rect,DataCol,Column,State);
   end;
end;

procedure TJkPanel.refreshFootpanel(Tag :integer);
var
   i,Listindex,PageIndex :integer;
   R,G,B :Integer;
   tmpCarN0 :String;
   TmpMachMiss :String; //单缸号
begin
   try
      PageIndex := FPageControl.ActivePageIndex;
      Listindex := Tag;
      if AdoLists[Listindex].RecordCount = 0 then
      begin
         FootPanels[PageIndex].DataSource := Nil;
         exit;
      end;
      //获取机台号
      This_Jth := Labels[Listindex].Hint;
      //获取任务序次
      This_TaskOrder := trim(AdoLists[Listindex].FieldByName('TaskOrder').AsString);
      TmpMachMiss := trim(AdoLists[Listindex].FieldByName('MachMiss').AsString);
      if TmpMachMiss = '0000000000' then
         Co_DllBtn[PageIndex].Caption := '洗缸领料'
      else
         Co_DllBtn[PageIndex].Caption := '流程处理';
      
      with tmpado do
      begin
         DisableConTrols;
         Active := False;
         ConnectionString := Fcnnstr;
         CursorType := ctStatic;
         LockType := ltReadOnly;
         CommandText := Data_FootSql;
         Parameters.ParamValues['FootParm'] := AdoLists[Listindex].Fields[0].Value;
         Active := True;
         for i := 0 to fieldCount -1 do
            tmpado.Fields[i].Alignment := taLeftJustify;
         tmpDs.dataset := tmpado;
         EnableConTrols;
      end;
      with adoGlides do
      begin
         DisableConTrols;
         Active := False;
         ConnectionString := Fcnnstr;
         CursorType := ctStatic;
         LockType := ltReadOnly;
         CommandText := Data_GlideSql;
         if AdoLists[Listindex].Fieldbyname('MachMiss').AsString <> null then
         begin
            tmpCarN0 := trim(AdoLists[Listindex].Fieldbyname('MachMiss').AsString);
            tmpCarN0 := Copy(tmpCarN0,1,10);
         end
         else
            tmpCarN0 := '////';
         Parameters.ParamValues['PCarNo'] := tmpCarN0;
         Active := True;
         First;
         EnableConTrols;
      end;
      DGGlides[PageIndex].DataSource := DsGlides;
      DGGlides[PageIndex].Columns[0].Width := 74;
      DGGlides[PageIndex].Columns[1].Width := 40;
      FootPanels[PageIndex].Width := 140;
      FootPanels[PageIndex].Columns := 1;
      FootPanels[PageIndex].DataSource := tmpDs;
      FootPanels[PageIndex].CreateEditors(tmpDs,1);
      R := 255;  G:= 255; B := 255;
      R := AdoLists[Listindex].Fieldbyname('RedCode').AsInteger;
      G := AdoLists[Listindex].Fieldbyname('GreenCode').AsInteger;
      B := AdoLists[Listindex].Fieldbyname('BlueCode').AsInteger;
      Co_Shapes[PageIndex].Brush.Color := RGB(R,G,B);
   except
      //
   end;
end;

procedure TJkPanel.DGListsCellClick(Column: TColumn);
var
   tmpIdx,TmpCol,TmpTag :Integer;
begin
   if not Column.Grid.DataSource.DataSet.Active then exit;
   if Column.Grid.DataSource.DataSet.RecordCount = 0 then exit;

   if Column.Grid.DataSource.DataSet.RecNo = 1 then
   begin
      tmpIdx := (Column.Grid as TCustomDbGrid).ComponentIndex;
      TmpCol := Column.Index;
      if TmpDgListIdx = -1 then
      begin
         TmpDgListIdx := tmpIdx;
         TmpDgListCol := TmpCol;
         refreshFootpanel((Column.Grid as TCustomDbGrid).DataSource.DataSet.Tag);
      end
      else
      begin
         if TmpDgListIdx <> tmpIdx then
            refreshFootpanel((Column.Grid as TCustomDbGrid).DataSource.DataSet.Tag);
         TmpDgListIdx := tmpIdx;
         TmpDgListCol := TmpCol;
      end;
      (Column.Grid as TCustomDbGrid).SetFocus;
   end
end;

procedure TJkPanel.AdolistsAfterScroll(DataSet: TDataSet);
begin
   //刷新 Footpanel
   refreshFootpanel(DataSet.tag);
end;


//响应Labels[i]的Click事件
procedure TJkPanel.LabelsClick(Sender: TObject);
begin
   //
end;

{
procedure TJkPanel.LabelsClick(Sender: TObject);
var
   i :integer;
   Tmp_CheJian,Tmp_JiTai :String;
begin

   try
      if FP_Parent = nil then inti_Grid(Sender);
      if FP_Parent = nil then exit;
      Tmp_JiTai := trim((Sender as Tlabel).Caption);
      Tmp_CheJian := trim(((Sender as Tlabel).Parent as TScrollBox).Hint);
      if tmpado.Active then
      begin
         if tmpado.FieldValues['机台'] <> null then
         if tmpado.FieldByName('机台').AsString = Tmp_JiTai then
            exit;
      end;
      screen.Cursor := crSQLWait;
      //FP_Parent.Visible := False;
      //FP_Parent.Left := FPageControl.Left + (Sender as Tlabel).Left + (Sender as Tlabel).Width + 28;
      //FP_Parent.Top := FPageControl.Top + (Sender as Tlabel).top + (Sender as Tlabel).Height +4;
      FP_Move.Caption := trim((Sender as Tlabel).Hint) + '明细列表';
      with tmpado do
      begin
         DisableConTrols;
         Active := False;
         ConnectionString := Fcnnstr;
         tmpado.CursorType := ctStatic;
         tmpado.LockType := ltReadOnly;
         CommandText := 'Exec '+Data_BerthListSql+' '''+Tmp_CheJian+''','''+Tmp_JiTai+'''';
         Active := True;
         First;
         EnableConTrols;
         for i := 0 to Fields.Count -1 do
         begin
            Fields[i].Alignment := taCenter;
            if i = 0 then
               fields[i].DisplayWidth := 20
            else
               fields[i].DisplayWidth := 8;

            Fpass_Grid.Columns[i].Title.Alignment := taCenter;
            Fpass_Grid.Columns[i].Title.Font.Style:= [];
         end;

      end;
      screen.Cursor := crDefault;
      FP_Parent.Visible := True;
   except
      screen.Cursor := crDefault;
   end;
end;
    }

procedure TJkPanel.FootPanelDblclick(Sender: TObject);
begin
   if sender is TScrollBox then
      (sender as TScrollBox).Parent.width := 1;
end;

procedure TJkPanel.FP_MovePanelDblClick(Sender: TObject);
begin
   FP_Parent.Visible := False;
end;

 //Register控件
procedure Register;
begin
  RegisterComponents('Data Controls', [TJkPanel]);
end;

// 内存的释放是要有顺序的！必须以创建的相反的顺序进行！尤其是当组件之间有父子关系时
procedure TJkPanel.FreeEditors;
begin
  	if FPageControl <> nil then
   begin
      if FP_Parent <> nil then
      begin
         try
            FPass_Grid.Free;
            FP_Move.Free;
   	      FP_Parent.Free;
         except
            //
         end;
      end;
      DGGlides := nil;
      DsGlides  := nil;
      AdoGlides  := nil;
      DsGlides.Free;
      AdoGlides.Free;

      DGLists := nil;
      DsLists  := nil;
      AdoLists  := nil;

      FPass_Grid := nil;
      FP_Move := nil;
   	FP_Parent := nil;
      Shapes := nil;
      ProgressBars := nil;
   	comEditors := nil;
   	edEditors := nil;
   	MemoEditors := nil;
    	ScrollBoxs := nil;

      Splitters := nil;
      Co_Shapes:= nil;
      Co_DllBtn := Nil;
      Co_Panels:= nil;
      FootPanels := nil;

      tmpDs.Free;
      tmpado.Free;

      FPageControl.Free;
  	end;
end;

end.

