unit BGLFme;

interface

uses
  BGLDm,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZjGridDBA, StdCtrls, DBCtrls, ZJGrid, ExtCtrls, ComCtrls,
  ToolWin, JimLabels, ActnList, DB, dxBar, ZJEdits;

type
  TBGLFrame = class(TFrame)
    labTitle: TJimGradLabel;
    ToolBar: TToolBar;
    tobtnNew: TToolButton;
    pnlBGLList: TPanel;
    zgBGL: TZJGrid;
    pnlBGLMemo: TPanel;
    lblPosition: TLabel;
    dbmPos_Reason: TDBMemo;
    lblDirection: TLabel;
    dbmDirection: TDBMemo;
    pnlBGBills: TPanel;
    zgBGBills: TZJGrid;
    zaBGL: TZjGridDBA;
    zaBGBills: TZjGridDBA;
    alBGL: TActionList;
    actnNew: TAction;
    actnApply: TAction;
    dxpmBGL: TdxBarPopupMenu;
    dxpmBGBills: TdxBarPopupMenu;
    actnBatchWritePos_Reason: TAction;
    pnlBGL: TPanel;
    sprBGL: TSplitter;
    actnCopyBGLBlock: TAction;
    cbbBGLType: TZjComboBox;
    procedure actnNewExecute(Sender: TObject);
    procedure dbmPos_ReasonExit(Sender: TObject);
    procedure zgBGLMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure zgBGBillsMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure dxpmBGLPopup(Sender: TObject);
    procedure actnBatchWritePos_ReasonExecute(Sender: TObject);
    procedure zaBGBillsGridSetCellText(Sender: TObject;
      const ACoord: TPoint; var Value: String);
    procedure actnCopyBGLBlockExecute(Sender: TObject);
    procedure zgBGLCopy(Sender: TObject; const ABounds: TRect;
      var Allow: Boolean);
    procedure zgBGLPaste(Sender: TObject; const ABounds: TRect;
      var Allow: Boolean);
    procedure actnCopyBGLBlockUpdate(Sender: TObject);
    procedure zgBGLGetCellEditor(Sender: TObject; ACoord: TPoint;
      var AControl: TWinControl);
    procedure zgBGLEditorSaveCell(Sender: TObject; ACoord: TPoint;
      AControl: TWinControl);
    procedure zgBGLEditorLoadCell(Sender: TObject; ACoord: TPoint;
      AControl: TWinControl);
    procedure zgBGLCellTextChanged(Sender: TObject; Col, Row: Integer);
  private
    FBGLData: TBGLData;

    procedure CopyBGLBlock(ABounds: TRect);
    procedure PasteBGLBlock;
  public
    constructor Create(AParent: TFrame; ABGLData: TBGLData);
  end;

implementation

uses
  UtilMethods, MergeTextFrm, ProjectData, MainFrm, mEncryptEditions,
  BGLClipboard, ConditionalDefines, ZjCells;

{$R *.dfm}

{ TBGLFrame }

constructor TBGLFrame.Create(AParent: TFrame; ABGLData: TBGLData);
begin
  inherited Create(AParent);
  FBGLData := ABGLData;
  zgBGL.CellClass.Cols[zaBGL.ColumnCol('BGLType')] := TZjComboCell;
  zaBGL.DataSet := FBGLData.cdsBGLView;

  zgBGL.OnGetCellEditor := zgBGLGetCellEditor;
  zgBGL.OnEditorLoadCell := zgBGLEditorLoadCell;
  zgBGL.OnEditorSaveCell := zgBGLEditorSaveCell;

  dbmPos_Reason.DataSource := FBGLData.dsBGL;
  dbmDirection.DataSource := FBGLData.dsBGL;
  zaBGBills.DataSet := FBGLData.cdsBGBillsView;
  SetDxBtnAction(actnNew, tobtnNew);
end;

procedure TBGLFrame.actnNewExecute(Sender: TObject);
var
  sCode: string;
begin
  sCode := Format('%s-%d-%d', ['', TProjectData(FBGLData.ProjectData).PhaseIndex,
      FBGLData.cdsBGL.RecordCount + 1]);
  if GetMergeText('±ä¸üÁîºÅ', sCode) then
    FBGLData.AddBGL(sCode);
end;

procedure TBGLFrame.dbmPos_ReasonExit(Sender: TObject);
begin
  if FBGLData.cdsBGLView.State in [dsEdit] then
    FBGLData.cdsBGLView.Post;
end;

procedure TBGLFrame.zgBGLMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmBGL.PopupFromCursorPos;
end;

procedure TBGLFrame.zgBGBillsMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
    dxpmBGBills.PopupFromCursorPos;
end;

procedure TBGLFrame.dxpmBGLPopup(Sender: TObject);
begin
  SetDxBtnAction(actnBatchWritePos_Reason, MainForm.dxbtnBatchWritePos_Reason);
  SetDxBtnAction(actnCopyBGLBlock, MainForm.dxbtnCopyBillsBlock);
end;

procedure TBGLFrame.actnBatchWritePos_ReasonExecute(Sender: TObject);
begin
  FBGLData.BatchWritePos_Reason;
end;

procedure TBGLFrame.zaBGBillsGridSetCellText(Sender: TObject;
  const ACoord: TPoint; var Value: String);
begin
  if SameText(Value, zgBGBills.Cells[ACoord.X, ACoord.Y].Text) then
    Abort;
end;

procedure TBGLFrame.actnCopyBGLBlockExecute(Sender: TObject);
var
  iCount: Integer;
begin
  if CheckMeasureEdition then
  begin
    zgBGL.Tag := 1;
    iCount := zgBGL.Selection.Bottom - zgBGL.Selection.Top;
    zgBGL.CopyToClipboard(zgBGL.Selection.Bounds);
    zgBGL.Selection.SelectRow(zgBGL.Selection.Top, zgBGL.Selection.Top + iCount - 1);
  end;
end;

procedure TBGLFrame.zgBGLCopy(Sender: TObject; const ABounds: TRect;
  var Allow: Boolean);
begin
  Allow := TZJGrid(Sender).Tag = 0;
  if not Allow then
  begin
    CopyBGLBlock(ABounds);
    TZJGrid(Sender).Tag := 0;
  end;
end;

procedure TBGLFrame.CopyBGLBlock(ABounds: TRect);
var
  Clipboard: TBGLClipboard;
begin
  Clipboard := TBGLClipboard.Create(FBGLData);
  try
    Clipboard.Copy(ABounds.Bottom - ABounds.Top);
  finally
    Clipboard.Free;
  end;
end;

procedure TBGLFrame.PasteBGLBlock;
var
  Clipboard: TBGLClipboard;
begin
  Clipboard := TBGLClipboard.Create(FBGLData);
  try
    Clipboard.Paste;
  finally
    Clipboard.Free;
  end;
end;

procedure TBGLFrame.zgBGLPaste(Sender: TObject; const ABounds: TRect;
  var Allow: Boolean);
begin
  if HasBGLBlockFormat then
  begin
    Allow := False;
    PasteBGLBlock;
  end;
end;

procedure TBGLFrame.actnCopyBGLBlockUpdate(Sender: TObject);
begin
  if _IsDebugView then
    TAction(Sender).Visible := True
  else
    TAction(Sender).Visible := False;
end;

procedure TBGLFrame.zgBGLGetCellEditor(Sender: TObject; ACoord: TPoint;
  var AControl: TWinControl);
begin
  if ACoord.X = zaBGL.ColumnCol('BGLType') then
    AControl := cbbBGLType;
end;

procedure TBGLFrame.zgBGLEditorSaveCell(Sender: TObject; ACoord: TPoint;
  AControl: TWinControl);
begin
  if AControl = cbbBGLType then
  begin
    zaBGL.DataSet.Edit;
    if cbbBGLType.ItemIndex = -1 then
      zaBGL.DataSet.FieldByName('BGLType').Clear
    else
      zaBGL.DataSet.FieldByName('BGLType').AsString := cbbBGLType.Text;
    zaBGL.DataSet.Post;
  end;
end;

procedure TBGLFrame.zgBGLEditorLoadCell(Sender: TObject; ACoord: TPoint;
  AControl: TWinControl);
begin
  if AControl = cbbBGLType then
    if zaBGL.DataSet.FieldByName('BGLType').AsString = '' then
      cbbBGLType.ItemIndex := -1
    else
      cbbBGLType.Text := zaBGL.DataSet.FieldByName('BGLType').AsString;
end;

procedure TBGLFrame.zgBGLCellTextChanged(Sender: TObject; Col,
  Row: Integer);
begin
  if Col = 6 then
  begin
    if (zgBGL.Cells[Col, Row].Text <> '') and (cbbBGLType.Items.IndexOf(zgBGL.Cells[Col, Row].Text) = -1) then
      zgBGL.Cells[Col, Row].Text := '';
  end;
end;

end.
