cxcolorcombobox.pas

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

PAS
1,962
字号
  const ADescription: string): TcxColorComboBoxItem;
begin
  Result := nil;
  if (FindColorItem(AColor) <> nil) then Exit;
  Result := Add;
  Result.Color := AColor;
  Result.Description := ADescription;
end;

function TcxColorComboBoxItems.InsertColor(Index: Integer; const AColor: TColor;
  const ADescription: string): TcxColorComboBoxItem;
begin
  Result := nil;
  if (FindColorItem(AColor) <> nil) then Exit;
  Result := Insert(Index);
  Result.Color := AColor;
  Result.Description := ADescription;
end;

procedure TcxColorComboBoxItems.Move(CurIndex, NewIndex: Integer);
var
  FNewColorItem, FOldColorItem: TcxColorComboBoxItem;
begin
  if CurIndex = NewIndex then Exit;
  FOldColorItem := Items[CurIndex];
  FNewColorItem := Insert(NewIndex);
  FNewColorItem.Assign(FOldColorItem);
  FOldColorItem.Free;
end;

procedure TcxColorComboBoxItems.ClearCustom;
var
  I: Integer;
begin
  for I := Count - 1 downto 0 do
    if Items[I].IsCustomColor then
      Delete(I);
end;

procedure TcxColorComboBoxItems.ClearNonCustom;
var
  I: Integer;
begin
  for I := (Count - 1) downto 0 do
    if not Items[I].IsCustomColor then
      Delete(I);
end;

{ TcxCustomColorComboBoxViewInfo }

procedure TcxCustomColorComboBoxViewInfo.Paint(ACanvas: TcxCanvas);
var
  FRealDefaultColorStyle: TcxDefaultColorStyle;
begin
  inherited Paint(ACanvas);
  if not FoundItem and (DefaultColorStyle = cxdcText) then
    Exit;

  FRealDefaultColorStyle := DefaultColorStyle;
  if (DefaultColorStyle <> cxdcText) and FoundItem then
    FRealDefaultColorStyle := cxdcColor;
  DrawColorBox(ACanvas, ColorBoxRect, ColorBoxFrameColor, ColorBoxColor,
    BkColor, FRealDefaultColorStyle);

  if not IsInplace and not ShowDescriptions and Focused and not HasPopupWindow then
  begin
    ACanvas.Font.Color := clBtnText;
    ACanvas.Brush.Color := BackgroundColor;
    TCanvasAccess(ACanvas.Canvas).RequiredState([csFontValid]);
    ACanvas.Canvas.DrawFocusRect(ClientRect);
  end;
end;

procedure TcxCustomColorComboBoxViewInfo.Offset(DX, DY: Integer);
begin
  inherited Offset(DX, DY);
  OffsetRect(FColorBoxRect, DX, DY);
end;

{ TcxCustomColorComboBoxViewData }

procedure TcxCustomColorComboBoxViewData.Calculate(ACanvas: TcxCanvas;
  const ABounds: TRect; const P: TPoint; Button: TcxMouseButton;
  Shift: TShiftState; AViewInfo: TcxCustomEditViewInfo; AIsMouseEvent: Boolean);
var
  R: TRect;
  FViewInfo: TcxCustomColorComboBoxViewInfo;
begin
  if IsRectEmpty(ABounds) then begin
    inherited;
    Exit;
  end;
  inherited Calculate(ACanvas, ABounds, P, Button, Shift, AViewInfo, AIsMouseEvent);
  if (ABounds.Right = MaxInt) or (ABounds.Bottom = MaxInt) then Exit;

  FViewInfo := TcxCustomColorComboBoxViewInfo(AViewInfo);
  CalculateViewInfoProperties(FViewInfo);
  R := FViewInfo.ClientRect;
  FViewInfo.FColorBoxRect := R;
  if (FViewInfo.DefaultColorStyle = cxdcText) and not FViewInfo.FoundItem then
    FViewInfo.FColorBoxRect.Right := FViewInfo.FColorBoxRect.Left
  else
  begin
    if FViewInfo.ShowDescriptions then
    begin
      if FViewInfo.ColorBoxAlign = cbaLeft then
      begin
        FViewInfo.FColorBoxRect.Right := FViewInfo.FColorBoxRect.Left + FViewInfo.ColorBoxWidth ;
        R.Left := FViewInfo.FColorBoxRect.Right;
      end
      else
      begin
        FViewInfo.FColorBoxRect.Left := FViewInfo.FColorBoxRect.Right - FViewInfo.ColorBoxWidth ;
        R.Right := FViewInfo.FColorBoxRect.Left;
      end;
    end;
  end;
  FViewInfo.ClientRect := R;
  if not IsInplace then
    InflateRect(R, -2, -2)
  else
    InflateRect(R, -2, -1);
  FViewInfo.TextRect := R;
  if not FViewInfo.ShowDescriptions then FViewInfo.Text := '';
  if not IsInplace then FViewInfo.DrawSelectionBar := False;
end;

procedure TcxCustomColorComboBoxViewData.CalculateViewInfoProperties(AViewInfo: TcxCustomEditViewInfo);
var
  AProperties: TcxCustomColorComboBoxProperties;
begin
  AProperties := TcxCustomColorComboBoxProperties(Properties);
  with TcxCustomColorComboBoxViewInfo(AViewInfo) do
  begin
    BkColor := BackgroundColor;
    ColorBoxAlign := AProperties.ColorBoxAlign;
    ColorBoxWidth := AProperties.ColorBoxWidth;
    ColorBoxFrameColor := AProperties.ColorBoxFrameColor;
    ShowDescriptions := AProperties.ShowDescriptions;
    DefaultColorStyle := AProperties.DefaultColorStyle;
    if (DefaultColorStyle = cxdcCustom) and (not FoundItem) then
      AProperties.DoGetDefaultColor(FColorBoxColor);
  end;
end;

function TcxCustomColorComboBoxViewData.InternalEditValueToDisplayText(
  AEditValue: TcxEditValue): string;
var
  AColor: TColor;
  AColorFound: Boolean;
  ADisplayText: string;
begin
  Properties.GetColorComboBoxDisplayValue(AEditValue, AColor, ADisplayText,
    AColorFound);
  Result := ADisplayText;
end;

function TcxCustomColorComboBoxViewData.InternalGetEditConstantPartSize(ACanvas: TcxCanvas; AIsInplace: Boolean;
  AEditSizeProperties: TcxEditSizeProperties; var MinContentSize: TSize;
  AViewInfo: TcxCustomEditViewInfo): TSize;
begin
  Result := inherited InternalGetEditConstantPartSize(ACanvas, AIsInplace,
    AEditSizeProperties, MinContentSize, AViewInfo);
  if TcxCustomColorComboBoxProperties(Properties).ShowDescriptions then
    Result.cx := Result.cx + TcxCustomColorComboBoxProperties(Properties).ColorBoxWidth + 6;
end;

function TcxCustomColorComboBoxViewData.IsComboBoxStyle: Boolean;
begin
  Result := True;
end;

function TcxCustomColorComboBoxViewData.GetProperties: TcxCustomColorComboBoxProperties;
begin
  Result := TcxCustomColorComboBoxProperties(FProperties);
end;

procedure TcxCustomColorComboBoxViewData.DisplayValueToDrawValue(const ADisplayValue: TcxEditValue;
  AViewInfo: TcxCustomEditViewInfo);
var
  AViewInfoAccess: TcxCustomColorComboBoxViewInfo;
begin
  if (Edit = nil) or IsVarEmpty(ADisplayValue) then
    Exit;
  AViewInfoAccess := TcxCustomColorComboBoxViewInfo(AViewInfo);
  Properties.InternalGetColorComboBoxDisplayValue(
    TcxCustomColorComboBox(Edit).ILookupData.CurrentKey, Edit.EditValue,
    AViewInfoAccess.FColorBoxColor, AViewInfoAccess.Text,
    AViewInfoAccess.FFoundItem);
end;

procedure TcxCustomColorComboBoxViewData.EditValueToDrawValue(ACanvas: TcxCanvas;
  const AEditValue: TcxEditValue; AViewInfo: TcxCustomEditViewInfo);
var
  AColorComboViewInfo: TcxCustomColorComboBoxViewInfo;
begin
  AColorComboViewInfo := TcxCustomColorComboBoxViewInfo(AViewInfo);
  Properties.GetColorComboBoxDisplayValue(AEditValue,
    AColorComboViewInfo.FColorBoxColor, AColorComboViewInfo.Text,
    AColorComboViewInfo.FFoundItem);
  if PreviewMode then
    AColorComboViewInfo.Text := '';
  DoOnGetDisplayText(string(AColorComboViewInfo.Text));
end;

{ TcxCustomColorComboBoxListBox }

function TcxCustomColorComboBoxListBox.GetItemHeight(AIndex: Integer = -1): Integer;
begin
  with Edit.ActiveProperties do
  begin
    if ItemHeight > 0 then
      Result := ItemHeight
    else
    begin
      Result := inherited GetItemHeight;
      if Result < 16 then
        Result := 16;
    end;
    if (AIndex >= 0) and Edit.IsOnMeasureItemEventAssigned then
      Edit.DoOnMeasureItem(AIndex, Canvas, Result);
    if AIndex = FMRUColors.Count - 1 then
      Inc(Result, MRUDelimiterWidth);
  end;
end;

procedure TcxCustomColorComboBoxListBox.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  AColorBoxRect, ATextRect: TRect;
  AFlags: Longint;
begin
  SaveCanvasParametersForFocusRect;
  try
    if DoDrawItem(Index, Rect, State) then
      Exit;

    Canvas.FillRect(Rect);
    AColorBoxRect := Rect;
    if Index = Edit.ActiveProperties.MRUColors.Count - 1 then
      Dec(AColorBoxRect.Bottom, MRUDelimiterWidth);
    ATextRect := AColorBoxRect;
    if Edit.ActiveProperties.ShowDescriptions then
    begin
      AFlags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_LEFT or DT_NOPREFIX);
      if Edit.ActiveProperties.ColorBoxAlign = cbaRight then
      begin
        Dec(ATextRect.Right, Edit.ActiveProperties.ColorBoxWidth);
        AColorBoxRect.Left := ATextRect.Right;
      end
      else
      begin
        Inc(ATextRect.Left, Edit.ActiveProperties.ColorBoxWidth);
        AColorBoxRect.Right := ATextRect.Left;
      end;
      Canvas.DrawText(GetItem(Index), ATextRect, AFlags);
    end
    else
      ATextRect.Left := AColorBoxRect.Right;
    DrawColorBox(Canvas, AColorBoxRect, Edit.ActiveProperties.ColorBoxFrameColor,
      Edit.ActiveProperties.GetColorByIndex(Index), Canvas.Brush.Color, cxdcColor);
    if Index = Edit.ActiveProperties.FMRUColors.Count - 1 then
      DrawMRUDelimiter(Canvas.Canvas, Rect, odSelected in State);
  finally
    RestoreCanvasParametersForFocusRect;
  end;
end;

procedure TcxCustomColorComboBoxListBox.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited MouseDown(Button, Shift, X, Y);
  if Button <> mbLeft then Exit;
  SetCaptureControl(nil);
  Edit.CloseUp(crEnter);
end;

function TcxCustomColorComboBoxListBox.GetEdit: TcxCustomColorComboBox;
begin
  Result := TcxCustomColorComboBox(inherited Edit);
end;

{ TcxColorComboBoxLookupData }

function TcxColorComboBoxLookupData.GetListBoxClass: TcxCustomEditListBoxClass;
begin
  Result := TcxCustomColorComboBoxListBox;
end;

function TcxColorComboBoxLookupData.GetItem(Index: Integer): string;
begin
  with TcxCustomColorComboBoxProperties(ActiveProperties) do
    Result := GetDescriptionByIndex(Index);
end;

function TcxColorComboBoxLookupData.GetItemCount: Integer;
begin
  with TcxCustomColorComboBoxProperties(ActiveProperties) do
    Result := MRUColors.Count + Items.Count;
end;

function TcxColorComboBoxLookupData.GetVisualAreaPreferredSize(
  AMaxHeight: Integer; AWidth: Integer = 0): TSize;
var
  AScrollWidth: Integer;
begin
  Result := inherited GetVisualAreaPreferredSize(AMaxHeight, AWidth);
  Result.cx := Result.cx +
    TcxCustomColorComboBoxProperties(ActiveProperties).ColorBoxWidth;
  AScrollWidth := List.ScrollWidth;
  Inc(AScrollWidth,
    TcxCustomColorComboBoxProperties(ActiveProperties).ColorBoxWidth);
  List.ScrollWidth := 0;
  List.ScrollWidth := AScrollWidth;
end;

procedure TcxColorComboBoxLookupData.TextChanged;
begin
  if not TcxCustomColorComboBox(Edit).EditModeSetting then
    with TcxCustomColorComboBoxProperties(ActiveProperties) do
      InternalSetCurrentKey(IndexByValue(Edit.EditValue));
end;

function TcxColorComboBoxLookupData.InternalChangeCurrentMRUColorPosition(
  const AColor: TColor): Integer;
var
  FIndex: Integer;
begin
  Result := ItemIndex;
  with TcxCustomColorComboBoxProperties(ActiveProperties) do
    if ItemIndex >= (FMRUColors.Count - 1) then
    begin
      FIndex := FMRUColors.GetIndexByColor(AColor);
      if FIndex >= 0 then
      begin
        Result := FIndex;
        InternalSetCurrentKey(FIndex);
      end;
    end;
end;
{ TcxColorComboBoxLookupData }

{ TcxCustomColorComboBoxProperties }

constructor TcxCustomColorComboBoxProperties.Create(AOwner: TPersistent);
begin
  inherited Create(AOwner);
  FItems := TcxColorComboBoxItems.Create(Self, TcxColorComboBoxItem);
  FCustomColors := TcxColorComboBoxItems.Create(Self, TcxColorComboBoxItem);
  FCustomColors.OnUpdate := CustomColorChanged;
  FMRUColors := TcxColorComboBoxItems.Create(Self, TcxColorComboBoxItem);

  DropDownListStyle := lsFixedList;
  FInternalUpdate := False;
  FColorBoxAlign := cbaLeft;
  FColorBoxWidth := 30;
  FColorBoxFrameColor := clBtnShadow;
  FColorDialogType := cxcdtDefault;
  FShowDescriptions := True;
  FDefaultColor := clWindow;
  FDefaultDescription := cxGetResourceString(@cxSColorComboBoxDefaultDescription);
  FDefaultColorStyle := cxdcColor;
  FAllowSelectColor := False;
  FColorComboStyle := cxccsComboEdit;
  FNamingConvention := cxncDelphi;
  FColorValueFormat := cxcvRGB;
  FMaxMRUColors := 10;
  PrepareList := cxplDelphi;
  Buttons.Add;
  GlyphButtonIndex := 1;
  Buttons[1].Kind := bkEllipsis;
  Buttons[1].Default := False;
  Buttons[1].Visible := False;
end;

destructor TcxCustomColorComboBoxProperties.Destroy;
begin
  FreeAndNil(FMRUColors);
  FreeAndNil(FCustomColors);
  FreeAndNil(FItems);
  inherited;
end;

procedure TcxCustomColorComboBoxProperties.Assign(Source: TPersistent);
begin
  if Source is TcxCustomColorComboBoxProperties then
  begin
    BeginUpdate;
    try
      inherited Assign(Source);
      with Source as TcxCustomColorComboBoxProperties do
      begin
        Self.ColorBoxWidth := ColorBoxWidth;
        Self.ColorBoxAlign := ColorBoxAlign;
        Self.ColorBoxFrameColor := ColorBoxFrameColor;

⌨️ 快捷键说明

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