cximagecombobox.pas

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

PAS
1,337
字号

{ TcxImageComboBoxItems }

function TcxImageComboBoxItems.GetItems(Index: Integer): TcxImageComboBoxItem;
begin
  Result := TcxImageComboBoxItem(inherited Items[Index]);
end;

procedure TcxImageComboBoxItems.SetItems(Index: Integer;
  const Value: TcxImageComboBoxItem);
begin
  inherited Items[Index] := Value;
end;

procedure TcxImageComboBoxItems.InternalChanged;
begin
  Changed;
end;

procedure TcxImageComboBoxItems.Update(Item: TCollectionItem);
begin
  with TcxCustomImageComboBoxProperties(Owner) do
    Changed;
end;

constructor TcxImageComboBoxItems.Create(AOwner: TPersistent);
begin
  inherited Create(AOwner, TcxImageComboBoxItem);
end;

function TcxImageComboBoxItems.Add: TcxImageComboBoxItem;
begin
  Result := TcxImageComboBoxItem(inherited Add);
end;

{$IFNDEF DELPHI6}
function TcxImageComboBoxItems.Owner: TPersistent;
begin
  Result := GetOwner;
end;
{$ENDIF}

{ TcxImageComboBoxListBox }

constructor TcxImageComboBoxListBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  BorderStyle := bsNone;
  Style := lbOwnerDrawVariable;
end;

function TcxImageComboBoxListBox.GetHeight(ARowCount: Integer; AMaxHeight: Integer): Integer;
var
  I, H: Integer;
  R: TRect;
begin
  if Properties.MultiLineText then
    with TcxCustomImageComboBox(Edit) do
    begin
      R := GetPopupWindowOwnerControlBounds;
      FClientWidth := R.Right - R.Left;
      R := PopupWindow.ViewInfo.GetClientExtent;
      Dec(FClientWidth, R.Left + R.Right);
    end
  else
    FClientWidth := 0;
  Result := 0;
  for I := 0 to ARowCount - 1 do
  begin
    H := 0;
    MeasureItem(I, H);
    Inc(Result, H);
  end;
  if Properties.MultiLineText then
  begin
    FHasScrollbar := (Result > AMaxHeight) or (ARowCount < Items.Count);
    if FHasScrollbar then
    begin
      Dec(FClientWidth, GetScrollBarSize.cx);
      Result := 0;
      for I := 0 to ARowCount - 1 do
      begin
        H := 0;
        MeasureItem(I, H);
        Inc(Result, H);
      end;
    end;
  end;
end;

function TcxImageComboBoxListBox.GetItemWidth(AIndex: Integer): Integer;
begin
  if Properties.MultiLineText then
    Result := 0
  else
    Result := inherited GetItemWidth(AIndex);
end;

procedure TcxImageComboBoxListBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
var
  Flags: Longint;
  Data: string;
  AImages: TCustomImageList;
  R: TRect;
  AImageIndex: Integer;
begin
  if not DoDrawItem(Index, Rect, State) then
  begin
    Canvas.FillRect(Rect);
    if (Index < Items.Count) and (Index > -1) then
    begin
      if Properties.MultiLineText then
        Flags := DrawTextBiDiModeFlags(DT_LEFT or DT_EXPANDTABS or
          DT_NOPREFIX or DT_WORDBREAK)
      else
        Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or
          DT_NOPREFIX);
      if not UseRightToLeftAlignment then
        Inc(Rect.Left, 2)
      else
        Dec(Rect.Right, 2);
      Data := Properties.Items[Index].Description;
      AImages := GetImages;
      if AImages <> nil then
      begin
        R := GetImageRect(Rect);
        AImageIndex := Properties.Items[Index].ImageIndex;
        if (AImageIndex > -1) and (AImageIndex < AImages.Count) then
          with R do
            AImages.Draw(Canvas.Canvas, Left + 1, (Bottom + Top - AImages.Height) div 2,
              AImageIndex, Enabled);
        if R.Left > Rect.Left then Rect.Right := R.Left;
        if R.Right < Rect.Right then Rect.Left := R.Right;
      end;
      if not IsRectEmpty(Rect) then
      begin
        SetBkMode(Handle, TRANSPARENT);
        DrawText(Canvas.Handle, PChar(Data), Length(Data), Rect, Flags);
      end;
    end;
  end;
end;

procedure TcxImageComboBoxListBox.MeasureItem(Index: Integer; var Height: Integer);
var
  AData: string;
  AImages: TCustomImageList;
  W, H, AFlags: Integer;
  R: TRect;
begin
  W := FClientWidth - 2;
  AImages := GetImages;
  if AImages <> nil then
  begin
    Dec(W, AImages.Width + 4);
    H := AImages.Height + 2;
  end
  else
    H := 0;
  if Properties.MultiLineText and (W > 0) then
  begin
    R := Rect(0, 0, W, H);
    AData := Properties.Items[Index].Description;
    AFlags := DT_LEFT or DT_EXPANDTABS or DT_NOPREFIX or DT_WORDBREAK or DT_CALCRECT;
    DrawText(Canvas.Handle, PChar(AData), Length(AData), R, AFlags);
    H := Max(H, R.Bottom - R.Top + 2);
  end
  else
    H := Max(Canvas.TextHeight('Wg') + 2, H);
  Height := H;
  if (Index >= 0) and Edit.IsOnMeasureItemEventAssigned then
    Edit.DoOnMeasureItem(Index, Canvas, Height);
end;

procedure TcxImageComboBoxListBox.RecreateWindow;
begin
  InternalRecreateWindow;
end;

function TcxImageComboBoxListBox.GetImageRect(const R: TRect): TRect;
var
  AImages: TCustomImageList;
begin
  AImages := GetImages;
  if AImages <> nil then
    with Properties do
    begin
      Result := R;
      with Result do
        if ImageAlign = iaLeft then
          Right := Left + AImages.Width + 4
        else
          Left := Right - AImages.Width - 4;
    end
    else
      Result := EmptyRect;
end;

function TcxImageComboBoxListBox.GetImages: TCustomImageList;
begin
  Result := Properties.LargeImages;
  if Result = nil then Result := Properties.Images;
end;

function TcxImageComboBoxListBox.GetMaxItemWidth: Integer;
var
  AImages: TCustomImageList;
  I, W, J: Integer;
begin
  AImages := GetImages;
  if AImages <> nil then Result := AImages.Width + 8 else Result := 4;
  with Properties do
  begin
    J := Result;
    for I := 0 to Items.Count - 1 do
    begin
      W := Canvas.TextWidth(Items[I].Description) + J;
      if W > Result then Result := W;
    end;
  end;
  if Properties.DropDownRows < Items.Count then
    Inc(Result, GetScrollBarSize.cx);
end;

function TcxImageComboBoxListBox.GetEdit: TcxCustomImageComboBox;
begin
  Result := TcxCustomImageComboBox(inherited Edit);
end;

function TcxImageComboBoxListBox.GetProperties: TcxCustomImageComboBoxProperties;
begin
  Result := TcxCustomImageComboBox(Edit).ActiveProperties;
end;

{ TcxImageComboBoxLookupData }

function TcxImageComboBoxLookupData.GetListBoxClass: TcxCustomEditListBoxClass;
begin
  Result := TcxImageComboBoxListBox;
end;

function TcxImageComboBoxLookupData.GetItem(Index: Integer): string;
begin
  with TcxCustomImageComboBox(Edit).ActiveProperties do
    if (Index > -1) and (Index < Items.Count) then
      Result := Items[Index].Description
    else
      Result := ''
end;

function TcxImageComboBoxLookupData.GetItemCount: Integer;
begin
  Result := TcxCustomImageComboBox(Edit).ActiveProperties.Items.Count;
end;

function TcxImageComboBoxLookupData.GetList: TcxImageComboBoxListBox;
begin
  Result := TcxImageComboBoxListBox(inherited List);
end;

procedure TcxImageComboBoxLookupData.TextChanged;
var
  AItem: TcxImageComboBoxItem;
begin
  if TcxCustomImageComboBox(Edit).EditModeSetting then
    Exit;
  with TcxCustomImageComboBoxProperties(ActiveProperties) do
    AItem := FindItemByValue(Edit.EditValue);
  if AItem <> nil then
    InternalSetCurrentKey(AItem.Index)
  else
    InternalSetCurrentKey(-1);
end;

{ TcxImageComboBoxViewData }

procedure TcxImageComboBoxViewData.Calculate(ACanvas: TcxCanvas;
  const ABounds: TRect; const P: TPoint; Button: TcxMouseButton;
  Shift: TShiftState; AViewInfo: TcxCustomEditViewInfo; AIsMouseEvent: Boolean);
var
  R: TRect;
begin
  inherited Calculate(ACanvas, ABounds, P, Button, Shift, AViewInfo,
    AIsMouseEvent);

  with TcxImageComboBoxViewInfo(AViewInfo) do
  begin
    ImageAlign := TcxCustomImageComboBoxProperties(Properties).ImageAlign;
    Images := TcxCustomImageComboBoxProperties(Properties).Images;
    ShowDescriptions := TcxCustomImageComboBoxProperties(Properties).ShowDescriptions;
    R := ClientRect;
    if Assigned(Images) then
    begin
      ImageRect := cxRectInflate(R, -2, 0);
      with ImageRect do
      begin
        if cxRectWidth(ImageRect) > Images.Width then
        begin
          if ShowDescriptions then
            if ImageAlign = iaLeft then
            begin
              Right := Left + Images.Width;
              R.Left := Right + 2;
            end
            else
            begin
              Left := Right - Images.Width;
              R.Right := Left - 2;
            end
          else
          begin
            Left := Left + (Right - Left - Images.Width) div 2;
            Right := Left + Images.Width;
          end;
        end
        else
          R.Left := R.Right;
        if cxRectHeight(ImageRect) > Images.Height then
        begin
          Top := Top + (Bottom - Top - Images.Height) div 2;
          Bottom := Top + Images.Height;
        end;
      end;
    end;

    if not IsInplace then
      ClientRect := R;
    InflateRect(R, -2, -1);
    TextRect := R;
    if not ShowDescriptions then
      Text := '';
    if not IsInplace then
      DrawSelectionBar := False;
  end;
end;

procedure TcxImageComboBoxViewData.EditValueToDrawValue(ACanvas: TcxCanvas;
  const AEditValue: TcxEditValue; AViewInfo: TcxCustomEditViewInfo);
var
  AText: string;
  ACaption: TCaption;
  AImageIndex: TImageIndex;
begin
  PrepareSelection(AViewInfo);
  Properties.GetImageComboBoxDisplayValue(AEditValue, ACaption, AImageIndex);
  TcxImageComboBoxViewInfo(AViewInfo).ImageIndex := AImageIndex;
  AText := ACaption;
  DoOnGetDisplayText(AText);
  TcxImageComboBoxViewInfo(AViewInfo).Text := AText;
end;

procedure TcxImageComboBoxViewData.DisplayValueToDrawValue(
  const ADisplayValue: TcxEditValue; AViewInfo: TcxCustomEditViewInfo);
var
  ACurrentKey: TcxEditValue;
  AItem: TcxImageComboBoxItem;
  ACaption: TCaption;
  AImageIndex: TImageIndex;
begin
  if Edit = nil then
    Exit;
  ACurrentKey := TcxCustomImageComboBox(Edit).ILookupData.CurrentKey;
  if ACurrentKey = -1 then
    AItem := Properties.FindItemByValue(Edit.EditValue)
  else
    AItem := Properties.Items[ACurrentKey];
  Properties.InternalGetImageComboBoxDisplayValue(AItem, ACaption, AImageIndex);
  TcxImageComboBoxViewInfo(AViewInfo).Text := ACaption;
  TcxImageComboBoxViewInfo(AViewInfo).ImageIndex := AImageIndex;
end;

function TcxImageComboBoxViewData.InternalEditValueToDisplayText(
  AEditValue: TcxEditValue): string;
var
  AIndex: TImageIndex;
  ACaption: TCaption;
begin
  Properties.GetImageComboBoxDisplayValue(AEditValue,
    ACaption, AIndex);
  Result := ACaption;  
end;

function TcxImageComboBoxViewData.InternalGetEditConstantPartSize(
  ACanvas: TcxCanvas; AIsInplace: Boolean;
  AEditSizeProperties: TcxEditSizeProperties; var MinContentSize: TSize;
  AViewInfo: TcxCustomEditViewInfo): TSize;
begin
  Result := inherited InternalGetEditConstantPartSize(ACanvas, AIsInplace,
    AEditSizeProperties, MinContentSize, AViewInfo);
  with TcxCustomImageComboBoxProperties(Properties) do
  begin
    if Assigned(Images) then
    begin
      if Images.Height > MinContentSize.cy then
        MinContentSize.cy := Images.Height;
      Result.cx := Result.cx + Images.Width + 5;
    end
    else
      Result.cx := Result.cx + 1;
  end;
end;

function TcxImageComboBoxViewData.IsComboBoxStyle: Boolean;
begin
  Result := IsCompositionEnabled;
end;

function TcxImageComboBoxViewData.GetProperties: TcxCustomImageComboBoxProperties;
begin
  Result := TcxCustomImageComboBoxProperties(FProperties);
end;

{ TcxImageComboBoxViewInfo }

procedure TcxImageComboBoxViewInfo.Offset(DX, DY: Integer);
begin
  inherited Offset(DX, DY);
  OffsetRect(ImageRect, DX, DY);
end;

procedure TcxImageComboBoxViewInfo.InternalPaint(ACanvas: TcxCanvas);
var
  R: TRect;
begin
  if not RectVisible(ACanvas.Handle, Bounds) then
    Exit;
  inherited InternalPaint(ACanvas);
  if Assigned(Images) and (ImageIndex > -1) and (ImageIndex < Images.Count) then
  begin
    ACanvas.SaveClipRegion;
    try
      IntersectRect(R, ImageRect, BorderRect);
      ACanvas.SetClipRegion(TcxRegion.Create(R), roIntersect);

      if Transparent or IsCompositionEnabled and NativeStyle then
        Images.Draw(ACanvas.Canvas, ImageRect.Left, ImageRect.Top,
          ImageIndex, Enabled)
      else
        cxEditUtils.DrawGlyph(ACanvas,
          Images, ImageIndex, ImageRect, BackgroundColor, Enabled);
    finally
      ACanvas.RestoreClipRegion;
    end;
  end;
end;

⌨️ 快捷键说明

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