cxblobedit.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,542 行 · 第 1/4 页

PAS
1,542
字号
    Result := nil;
end;

function TcxCustomBlobEditProperties.GetPictureGraphicClass(AItem: TObject;
  ARecordIndex: Integer; APastingFromClipboard: Boolean = False): TGraphicClass;
begin
  Result := FPictureGraphicClass;
  if Result = nil then
  begin
    if APastingFromClipboard then
      Result := TBitmap;
    if Assigned(FOnGetGraphicClass) then
      FOnGetGraphicClass(AItem, ARecordIndex,
        APastingFromClipboard, Result);
  end;
end;

procedure TcxCustomBlobEditProperties.Assign(Source: TPersistent);
begin
  if Source is TcxCustomBlobEditProperties then
  begin
    BeginUpdate;
    try
      inherited Assign(Source);
      with TcxCustomBlobEditProperties(Source) do
      begin
        // Common
        Self.AlwaysSaveData := AlwaysSaveData;
        Self.BlobEditKind := BlobEditKind;
        Self.BlobPaintStyle := BlobPaintStyle;
        // Memo
        Self.MemoAutoReplace := MemoAutoReplace;
        Self.MemoCharCase := MemoCharCase;
        Self.MemoMaxLength := MemoMaxLength;
        Self.MemoOEMConvert := MemoOEMConvert;
        Self.MemoScrollBars := MemoScrollBars;
        Self.MemoWantReturns := MemoWantReturns;
        Self.MemoWantTabs := MemoWantTabs;
        Self.MemoWordWrap := MemoWordWrap;
        // Picture
        Self.PictureAutoSize := PictureAutoSize;
        Self.PictureFilter := PictureFilter;
        Self.PictureGraphicClass := PictureGraphicClass;
        Self.PictureTransparency := PictureTransparency;
        Self.ShowExPopupItems := ShowExPopupItems;
        Self.ShowPicturePopup := ShowPicturePopup;
        Self.OnAssignPicture := OnAssignPicture;
        Self.OnGetGraphicClass := OnGetGraphicClass;
      end;
    finally
      EndUpdate
    end
  end  
  else
    inherited Assign(Source);
end;

function TcxCustomBlobEditProperties.CompareDisplayValues(
  const AEditValue1, AEditValue2: TcxEditValue): Boolean;
begin
  CorrectBlobEditKind;
  Result := (BlobPaintStyle = bpsText) and (BlobEditKind = bekMemo) and
    VarIsStr(AEditValue1) and VarIsStr(AEditValue2) and
    InternalCompareString(AEditValue1, AEditValue2, True);
end;

class function TcxCustomBlobEditProperties.GetContainerClass: TcxContainerClass;
begin
  Result := TcxBlobEdit;
end;

function TcxCustomBlobEditProperties.GetDisplayText(
  const AEditValue: TcxEditValue; AFullText: Boolean = False;
  AIsInplace: Boolean = True): WideString;
begin
  Result := '';
  if not VarIsNull(AEditValue) then
    case BlobEditKind of
      bekMemo:
        Result := GetBlobText(AEditValue, Self, AFullText);
      bekPict:
        Result := cxGetResourceString(@cxSBlobPicture);
      bekOle:
        Result := 'OLE';
      bekBlob:
        Result := 'BLOB';
    end;
end;

function TcxCustomBlobEditProperties.GetEditValueSource(AEditFocused: Boolean): TcxDataEditValueSource;
begin
  CorrectBlobEditKind;
  if BlobEditKind = bekMemo then
    if (IDefaultValuesProvider <> nil) and IDefaultValuesProvider.IsBlob then
      Result := evsValue
    else
      Result := evsText
  else
    Result := evsValue;
end;

function TcxCustomBlobEditProperties.GetSpecialFeatures: TcxEditSpecialFeatures;
begin
  Result := inherited GetSpecialFeatures;
  if BlobEditKind = bekPict then
    Include(Result, esfBlobEditValue);
end;

function TcxCustomBlobEditProperties.GetSupportedOperations: TcxEditSupportedOperations;
begin
  Result := [esoEditing];
  if Buttons.Count > 0 then
    Include(Result, esoHotTrack);
end;

class function TcxCustomBlobEditProperties.GetViewInfoClass: TcxContainerViewInfoClass;
begin
  Result := TcxBlobEditViewInfo;
end;

function TcxCustomBlobEditProperties.IsEditValueValid(
  var AEditValue: TcxEditValue; AEditFocused: Boolean): Boolean;
begin
  Result := True;
end;

function KeyDownToModalResult(var Key: Word; Shift: TShiftState;
  AReturnClosed, AAlwaysSaveData: Boolean): TModalResult;
begin
  Result := mrNone;
  if ((Key = VK_F4) and not (ssAlt in Shift)) or (Key = VK_ESCAPE) or
    ((Key in [VK_UP, VK_DOWN]) and (ssAlt in Shift)) then
  begin
    if AAlwaysSaveData and not (Key = VK_ESCAPE) then
      Result := mrOk
    else
      Result := mrCancel
  end
  else
    if (TranslateKey(Key) = VK_RETURN) and (AReturnClosed or (ssCtrl in Shift)) then
      Result := mrOk;
  if Result <> mrNone then
    Key := 0;
end;

{ TcxPopupMemo }

procedure TcxPopupMemo.KeyDown(var Key: Word; Shift: TShiftState);
begin
  TcxCustomEditPopupWindow(Parent).ModalResult :=
    KeyDownToModalResult(Key, Shift, not ActiveProperties.WantReturns,
      FBlobEdit.ActiveProperties.AlwaysSaveData);
  if Key = 0 then
    KillMessages(InnerControl.Handle, WM_CHAR, WM_CHAR) // to block Ctrl+Enter
  else
    inherited KeyDown(Key, Shift);
end;

procedure TcxPopupMemo.SpellCheckerSetValue(const AValue: Variant);
begin
  FBlobEdit.SpellCheckerSetValue(AValue);
end;

{ TcxPopupImage }

procedure TcxPopupImage.KeyDown(var Key: Word; Shift: TShiftState);
begin
  TcxCustomEditPopupWindow(Parent).ModalResult :=
    KeyDownToModalResult(Key, Shift, True,
      FBlobEdit.ActiveProperties.AlwaysSaveData);
  inherited KeyDown(Key, Shift);
end;

{ TcxCustomBlobEdit }

destructor TcxCustomBlobEdit.Destroy;
begin
  DestroyPopupControls;
  inherited Destroy;
end;

procedure TcxCustomBlobEdit.DoPopupImageGetGraphicClass(Sender: TObject;
  APastingFromClipboard: Boolean; var AGraphicClass: TGraphicClass);
begin
  AGraphicClass := GetPictureGraphicClass(APastingFromClipboard);
end;

procedure TcxCustomBlobEdit.DoPopupImagePropertiesGetGraphicClass(
  AItem: TObject; ARecordIndex: Integer; APastingFromClipboard: Boolean;
  var AGraphicClass: TGraphicClass);
begin
  DoPopupImageGetGraphicClass(nil, APastingFromClipboard, AGraphicClass);
end;

function TcxCustomBlobEdit.GetActiveProperties: TcxCustomBlobEditProperties;
begin
  Result := TcxCustomBlobEditProperties(InternalGetActiveProperties);
end;

function TcxCustomBlobEdit.GetProperties: TcxCustomBlobEditProperties;
begin
  Result := TcxCustomBlobEditProperties(FProperties);
end;

procedure TcxCustomBlobEdit.InternalChanged(Sender: TObject);
begin
  if FOkButton <> nil then
  begin
    FOkButton.Enabled := True(*FStorage.ModifiedAfterEnter*);
    FOkButton.Default := FOkButton.Enabled;
  end;
end;

procedure TcxCustomBlobEdit.PictureClosePopup(Sender: TcxControl; AReason: TcxEditCloseUpReason);
begin
  if not HasPopupWindow then
  begin
    if AReason = crEnter then
      SaveStorage;
    DoClosePopup(AReason);
  end;
end;

procedure TcxCustomBlobEdit.SaveStorage;

  function NeedSaveStorage: Boolean;
  begin
    Result := FStorage.EditModified and ((PopupWindow.ModalResult = mrOk) or
      ((PopupWindow.ModalResult <> mrCancel) and ActiveProperties.AlwaysSaveData)) and
      DoEditing;
  end;

begin
  if NeedSaveStorage then
  begin
    if FStorage is TcxMemo then
      InternalEditValue := TcxMemo(FStorage).Lines.Text
    else
    begin
      if FStorage is TcxImage then
        with TcxImage(FStorage).Picture do
          if Graphic = nil then
            FGraphicClass := nil
          else
            FGraphicClass := TGraphicClass(Graphic.ClassType);
      InternalEditValue := FStorage.EditValue;
    end;
    ModifiedAfterEnter := True;

    DoAssignPicture;
    if ActiveProperties.ImmediatePost and CanPostEditValue and ValidateEdit(True) then
      InternalPostEditValue;
  end;
end;

procedure TcxCustomBlobEdit.SetProperties(Value: TcxCustomBlobEditProperties);
begin
  FProperties.Assign(Value);
end;

procedure TcxCustomBlobEdit.ColorChanged;
begin
  inherited ColorChanged;
  if not IsDestroying and HasPopupWindow and (FStorage is TcxPopupMemo) then
    TcxPopupMemo(FStorage).Style.Color := ActiveStyle.Color;
end;

procedure TcxCustomBlobEdit.CreatePopupControls;

  procedure InitStorage(AStorageClass: TcxCustomEditClass);
  begin
    if (FStorage <> nil) and (AStorageClass <> FStorage.ClassType) then
      DestroyPopupControls;
    if FStorage = nil then
    begin
      FStorage := AStorageClass.Create(nil);
      FStorage.OnEditing := StorageEditingHandler;
      FStorage.Style.BorderStyle := ebsNone;
      FStorage.ActiveProperties.OnChange := InternalChanged;
    end;
    FStorage.ActiveProperties.ReadOnly := not CanModify;
    ActiveProperties.PopupControl := FStorage;
    ActiveProperties.PopupSysPanelStyle := True;
    FStorage.Parent := PopupWindow;
  end;

  procedure InitButton(var AButton: TcxButton; const ACaption: string; AHeight, AWidth: Integer);
  begin
    if AButton = nil then
    begin
      AButton := TcxButton.Create(nil);
      AButton.UseSystemPaint := False;
    end;
    AButton.Caption := ACaption;
    AButton.Font.Assign(ActiveStyle.GetVisibleFont);
    AButton.LookAndFeel.MasterLookAndFeel := PopupControlsLookAndFeel;
    AButton.LookAndFeel.SkinPainter := ViewInfo.Painter;
    AButton.Height := AHeight;
    AButton.Width := AWidth;
    AButton.Parent := PopupWindow;
  end;

  procedure InitControls;
  var
    ACancelCaption, AOkCaption: string;
    ATempWidth, AButtonHeight: Integer;
    AFont: TFont;
  begin
    if ActiveProperties.ReadOnly or not DataBinding.IsDataAvailable then
      ACancelCaption := cxGetResourceString(@cxSBlobButtonClose)
    else
      ACancelCaption := cxGetResourceString(@cxSBlobButtonCancel);
    AOkCaption := cxGetResourceString(@cxSBlobButtonOK);

    AFont := ActiveStyle.GetVisibleFont;
    FButtonWidth := Max(cxTextWidth(AFont, ACancelCaption + '00'), cxTextWidth(AFont, AOkCaption + '00'));
    AButtonHeight := MulDiv(cxTextHeight(AFont), 20, 13);

    InitButton(FCancelButton, ACancelCaption, AButtonHeight, FButtonWidth);
    FCancelButton.Cancel := True;
    FCancelButton.ModalResult := mrCancel;

    if (not ActiveProperties.ReadOnly) and DataBinding.IsDataAvailable then
    begin
      InitButton(FOkButton, AOkCaption, AButtonHeight, FButtonWidth);
      FOkButton.Enabled := FStorage.EditModified;
      FOkButton.ModalResult := mrOk;
    end
    else
      FreeAndNil(FOkButton);

    PopupWindow.MinSysPanelHeight := AButtonHeight + (AButtonHeight div 3);
    ATempWidth := (AButtonHeight div 3) + FButtonWidth;
    if FOkButton <> nil then ATempWidth := ATempWidth * 2;
    ActiveProperties.PopupMinWidth := ATempWidth + GetSystemMetrics(SM_CXVSCROLL) +
      AButtonHeight div 2;
  end;

begin
  case ActiveProperties.BlobEditKind of
    bekPict:
      begin
        InitStorage(TcxPopupImage);
        with TcxPopupImage(FStorage) do
        begin
          FBlobEdit := Self;
          ActiveProperties.ShowFocusRect := False;
          ActiveProperties.Stretch := True;
          ActiveProperties.CustomFilter := Self.ActiveProperties.PictureFilter;
          ActiveProperties.GraphicTransparency := Self.ActiveProperties.PictureTransparency;
          ActiveProperties.OnClosePopup := PictureClosePopup;
          if not Self.ActiveProperties.ShowPicturePopup then
            ActiveProperties.PopupMenuLayout.MenuItems := []
          else
            if not Self.ActiveProperties.ShowExPopupItems then
              ActiveProperties.PopupMenuLayout.MenuItems :=
                ActiveProperties.PopupMenuLayout.MenuItems - [pmiSave, pmiLoad];

          ActiveProperties.GraphicClass := Self.ActiveProperties.PictureGraphicClass;
          //OnAssignPicture := Self.ActiveProperties.OnAssignPicture;
          //OnGetGraphicClass := Self.ActiveProperties.OnGetGraphicClass;
          OnGetGraphicClass := Self.DoPopupImageGetGraphicClass;
          ActiveProperties.OnGetGraphicClass := Self.DoPopupImagePropertiesGetGraphicClass;
          LoadPicture(Picture, Self.GetPictureGraphicClass, Self.EditValue);
          EditModified := False;
          Style.LookAndFeel.MasterLookAndFeel := Self.PopupControlsLookAndFeel;
        end;
        ActiveProperties.PopupAutoSize := ActiveProperties.PictureAutoSize;
        InitControls;
      end;
    bekMemo:
      begin
        InitStorage(TcxPopupMemo);
        with TcxPopupMemo(FStorage) do
        begin
          FBlobEdit := Self;
          ActiveProperties.CharCase := Self.ActiveProperties.MemoCharCase;
          ActiveProperties.ImeMode := Self.ActiveProperties.ImeMode;
          ActiveProperties.ImeName := Self.ActiveProperties.ImeName;
          ActiveProperties.MaxLength := Self.ActiveProperties.MemoMaxLength;
          ActiveProperties.OEMConvert := Self.ActiveProperties.MemoOEMConvert;
          ActiveProperties.ScrollBars := Self.ActiveProperties.MemoScrollBars;
          ActiveProperties.WantReturns := Self.ActiveProperties.MemoWantReturns;
          ActiveProperties.WantTabs := Self.ActiveProperties.MemoWantTabs;
          ActiveProperties.WordWrap := Self.ActiveProperties.MemoWordWrap;
          Self.ActiveProperties.PopupAutoSize := False;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?