cxradiogroup.pas

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

PAS
2,138
字号
procedure TcxRadioButton.ShortUpdateState;
begin
  if not HandleAllocated then
    Exit;
  UpdateState(cxmbNone, InternalGetShiftState, ScreenToClient(InternalGetCursorPos));
end;

procedure TcxRadioButton.UpdateState(Button: TcxMouseButton; Shift: TShiftState;
  const P: TPoint);
begin
  if not Enabled then
    State := rbsDisabled
  else
    if (csDesigning in ComponentState) then
      State := rbsNormal
    else
      if GetCaptureControl = Self then // VCL only
        if PtInRect(GetControlRect(Self), P) then
          State := rbsPressed
        else
          State := rbsHot
      else
        if PtInRect(GetControlRect(Self), P) then
        begin
            if Shift = [] then
              State := rbsHot
            else
              State := rbsNormal
        end
        else
          State := rbsNormal;
end;

// IcxMouseTrackingCaller
procedure TcxRadioButton.MouseTrackingCallerMouseLeave;
begin
  MouseLeave(nil);
end;

// IcxLookAndFeelContainer
function TcxRadioButton.GetLookAndFeel: TcxLookAndFeel;
begin
  Result := LookAndFeel;
end;

procedure TcxRadioButton.DrawItem(const DrawItemStruct: TDrawItemStruct);
begin
  FCanvas.Canvas.Handle := DrawItemStruct.hDC;
  Paint(DrawItemStruct.itemAction = ODA_FOCUS); // SC bug B19151
  FCanvas.Canvas.Handle := 0;
end;

function TcxRadioButton.GetRadioButtonRect(const ARadioButtonSize: TSize;
    ANativeStyle: Boolean): TRect;
begin
  Result.Top := (Height - ARadioButtonSize.cy) div 2;
  Result.Bottom := Result.Top + ARadioButtonSize.cy;
  if Alignment = taRightJustify then
  begin
    if ANativeStyle then
      Result.Left := 0
    else
      if IsInplace then
        Result.Left := 0
      else
        Result.Left := 1;
    Result.Right := Result.Left + ARadioButtonSize.cx;
  end else
  begin
    Result.Right := Width;
    Result.Left := Result.Right - ARadioButtonSize.cx;
  end;
end;

procedure TcxRadioButton.SetLookAndFeel(Value: TcxLookAndFeel);
begin
  FLookAndFeel.Assign(Value);
end;

procedure TcxRadioButton.SetPopupMenu(Value: TComponent);
var
  AIPopupMenu: IcxPopupMenu;
begin
  if (Value <> nil) and not((Value is TPopupMenu) or
    Supports(Value, IcxPopupMenu, AIPopupMenu)) then
      Value := nil;
  if FPopupMenu <> Value then
  begin
    {$IFDEF DELPHI5}
    if FPopupMenu <> nil then
      FPopupMenu.RemoveFreeNotification(Self);
    {$ENDIF}  
    FPopupMenu := Value;
    if FPopupMenu <> nil then
      FPopupMenu.FreeNotification(Self);
  end;
end;

procedure TcxRadioButton.SetState(Value: TcxRadioButtonState);
var
  R: TRect;
begin
  if Value <> FState then
  begin
    FState := Value;
    R := FButtonRect;
    InflateRect(R, 1, 1);
    InternalInvalidateRect(Self, R, False);
  end;
end;

procedure TcxRadioButton.SetTransparent(Value: Boolean);
begin
  if Value <> FTransparent then
  begin
    FTransparent := Value;
    Invalidate;
  end;
end;

{$IFNDEF DELPHI7}
procedure TcxRadioButton.SetParentBackground(Value: Boolean);
begin
  if Value <> FParentBackground then
  begin
    FParentBackground := Value;
    Invalidate;
  end;
end;

procedure TcxRadioButton.SetWordWrap(Value: Boolean);
begin
  if Value <> FWordWrap then
  begin
    FWordWrap := Value;
    Invalidate;
  end;
end;
{$ENDIF}

procedure TcxRadioButton.WMContextMenu(var Message: TWMContextMenu);
var
  AHandled: Boolean;
  P, P1: TPoint;
begin
  if Message.Result <> 0 then
    Exit;
  if csDesigning in ComponentState then
  begin
    inherited;
    Exit;
  end;

  P := SmallPointToPoint(Message.Pos);
  if (P.X = -1) and (P.Y = -1) then
    P1 := P
  else
  begin
    P1 := ScreenToClient(P);
    if not PtInRect(ClientRect, P1) then
    begin
      inherited;
      Exit;
    end;
  end;

  AHandled := False;
  DoContextPopup(P1, AHandled);
  Message.Result := Ord(AHandled);
  if not AHandled then
    inherited;
end;

procedure TcxRadioButton.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
  Message.Result := 1;
end;

procedure TcxRadioButton.WMWindowPosChanged(var Message: TWMWindowPosChanged);
begin
  inherited;
  if not (csDestroying in ComponentState) and IsTransparentBackground then
    Invalidate;
end;

procedure TcxRadioButton.BMSetCheck(var Message: TMessage);
begin
  inherited;
  InternalInvalidateRect(Self, FButtonRect, False);
end;

procedure TcxRadioButton.CMEnabledChanged(var Message: TMessage);
begin
  inherited;
  EnabledChanged;
end;

procedure TcxRadioButton.CMMouseEnter(var Message: TMessage);
begin
  inherited;
  if Message.lParam = 0 then
    MouseEnter(Self)
  else
    MouseEnter(TControl(Message.lParam));
end;

procedure TcxRadioButton.CMMouseLeave(var Message: TMessage);
begin
  inherited;
  if Message.lParam = 0 then
    MouseLeave(Self)
  else
    MouseLeave(TControl(Message.lParam));
end;

procedure TcxRadioButton.CMSysColorChange(var Message: TMessage);
begin
  inherited;
  Invalidate;
end;

procedure TcxRadioButton.CNDrawItem(var Message: TWMDrawItem);
begin
  if not (csDestroying in ComponentState) then
    DrawItem(Message.DrawItemStruct^);
end;

procedure TcxRadioButton.CNKeyDown(var Message: TWMKeyDown);
begin
  if IsPopupMenuShortCut(PopupMenu, Message) then
    Message.Result := 1
  else
    inherited;
end;

procedure TcxRadioButton.CNMeasureItem(var Message: TWMMeasureItem);
var
  ATempVar: TMeasureItemStruct;
begin
  ATempVar := Message.MeasureItemStruct^;
  ATempVar.itemWidth := Width;
  ATempVar.itemHeight := Height;
  Message.MeasureItemStruct^ := ATempVar;
end;

procedure TcxRadioButton.CNSysKeyDown(var Message: TWMSysKeyDown);
begin
  if IsPopupMenuShortCut(PopupMenu, Message) then
    Message.Result := 1
  else
    inherited;
end;

{  TcxRadioGroupButtonViewInfo  }

function TcxRadioGroupButtonViewInfo.GetGlyphRect(ACanvas: TcxCanvas;
  AGlyphSize: TSize; AAlignment: TLeftRight; AIsPaintCopy: Boolean): TRect;

  procedure CorrectRadioRect(var ACheckRect: TRect);
  begin
    if AIsPaintCopy and not Data.NativeStyle then
      OffsetRect(ACheckRect, 1, 0);
  end;

begin
  Result := inherited GetGlyphRect(ACanvas, AGlyphSize, AAlignment, AIsPaintCopy);
  CorrectRadioRect(Result);
end;

{ TcxCustomRadioGroupViewInfo }

constructor TcxCustomRadioGroupViewInfo.Create;
begin
  inherited Create;
  PrepareRadioButtonImageList;
end;

procedure TcxCustomRadioGroupViewInfo.DrawButtonCaption(ACanvas: TcxCanvas;
  AButtonViewInfo: TcxGroupBoxButtonViewInfo; const AGlyphRect: TRect);

  procedure CorrectTextRect(var R: TRect);
  const
    ANativeStyleTextRectCorrection: TRect = (Left: 3; Top: -1; Right: 0; Bottom: -1);
    ATextRectCorrection: TRect = (Left: 5; Top: 0; Right: 1; Bottom: 0);
  begin
    if AButtonViewInfo.Data.NativeStyle then
      ExtendRect(R, ANativeStyleTextRectCorrection)
    else
      ExtendRect(R, ATextRectCorrection);
    if (Edit <> nil) and Edit.IsDBEditPaintCopyDrawing then
      OffsetRect(R, 0, -1);
  end;

var
  R: TRect;
begin
  ACanvas.Font := Font;
  ACanvas.Font.Color := TextColor;
  PrepareCanvasFont(ACanvas.Canvas);
  R := AButtonViewInfo.Bounds;
  if Alignment = taRightJustify then
    R.Left := AGlyphRect.Right
  else
    R.Right := AGlyphRect.Left;
  ACanvas.Brush.Style := bsClear;
  CorrectTextRect(R);
  ACanvas.DrawText(AButtonViewInfo.Caption, R, DrawTextFlags);
  ACanvas.Brush.Style := bsSolid;
  if not IsInplace and Focused then
  begin
    ACanvas.TextExtent(AButtonViewInfo.Caption, R, DrawTextFlags);
    InflateRect(R, 1, 1);
    Inc(R.Bottom);
    ACanvas.Brush.Color := BackgroundColor;
    TCanvasAccess(ACanvas.Canvas).RequiredState([csFontValid]);
    ACanvas.Canvas.DrawFocusRect(R);
  end;
end;

procedure TcxCustomRadioGroupViewInfo.DrawButtonGlyph(ACanvas: TcxCanvas;
  AButtonViewInfo: TcxGroupBoxButtonViewInfo; const AGlyphRect: TRect);
const
  ALookAndFeelKindMap: array [TcxEditButtonStyle] of TcxLookAndFeelKind =
    (lfStandard, lfStandard, lfFlat, lfStandard, lfStandard,
    lfUltraFlat, lfOffice11);
  AButtonStateMap: array [TcxEditButtonState] of TcxButtonState =
    (cxbsDisabled, cxbsNormal, cxbsPressed, cxbsHot);
var
  APrevClipRegion: TcxRegion;
  ABackgroundColor: TColor;
  APainter: TcxCustomLookAndFeelPainterClass;
begin
  APrevClipRegion := ACanvas.GetClipRegion;
  try
    ACanvas.IntersectClipRect(AButtonViewInfo.Bounds);
    if AButtonViewInfo.Data.NativeStyle then
      DrawThemeBackground(ThemeHandle, ACanvas.Handle, BP_RADIOBUTTON,
        AButtonViewInfo.Data.NativeState, AGlyphRect)
    else
    begin
      if IsBackgroundTransparent {and (Painter = nil)} then
        ABackgroundColor := clDefault
      else
        ABackgroundColor := BackgroundColor;
      if Painter = nil then
        APainter := GetPainterClass(False, ALookAndFeelKindMap[AButtonViewInfo.Data.Style])
      else
        APainter := Painter;
      APainter.DrawRadioButton(ACanvas, AGlyphRect.Left, AGlyphRect.Top,
        AButtonStateMap[AButtonViewInfo.Data.State],
        ItemIndex = AButtonViewInfo.Index, False, ABackgroundColor, IsDesigning);
    end;
  finally
    ACanvas.SetClipRegion(APrevClipRegion, roSet);
  end;
end;

function TcxCustomRadioGroupViewInfo.GetButtonViewInfoClass: TcxEditButtonViewInfoClass;
begin
  Result := TcxRadioGroupButtonViewInfo;
end;

function TcxCustomRadioGroupViewInfo.IsButtonGlypthTransparent(AButtonViewInfo: TcxGroupBoxButtonViewInfo): Boolean;
begin
  Result := IsBackgroundTransparent or
    AButtonViewInfo.Data.NativeStyle and
      IsThemeBackgroundPartiallyTransparent(ThemeHandle, BP_RADIOBUTTON, AButtonViewInfo.Data.NativeState);
end;

function TcxCustomRadioGroupViewInfo.GetEdit: TcxCustomRadioGroup;
begin
  Result := TcxCustomRadioGroup(FEdit);
end;

function TcxCustomRadioGroupViewInfo.ThemeHandle: TdxTheme;
begin
  Result := OpenTheme(totButton);
end;

{ TcxCustomRadioGroupViewData }

procedure TcxCustomRadioGroupViewData.Calculate(ACanvas: TcxCanvas;
  const ABounds: TRect; const P: TPoint; Button: TcxMouseButton; Shift: TShiftState;
  AViewInfo: TcxCustomEditViewInfo; AIsMouseEvent: Boolean);
begin
  with TcxCustomRadioGroupViewInfo(AViewInfo) do
    Alignment := taRightJustify;
  inherited Calculate(ACanvas, ABounds, P, Button, Shift, AViewInfo, AIsMouseEvent);
  TcxCustomRadioGroupViewInfo(AViewInfo).GlyphSize :=
    Style.LookAndFeel.GetAvailablePainter(totButton).RadioButtonSize;
  AViewInfo.BackgroundColor := Style.Color;
end;

procedure TcxCustomRadioGroupViewData.EditValueToDrawValue(ACanvas: TcxCanvas;
  const AEditValue: TcxEditValue; AViewInfo: TcxCustomEditViewInfo);
begin
  if PreviewMode then
    TcxCustomRadioGroupViewInfo(AViewInfo).ItemIndex := 0
  else
    TcxCustomRadioGroupViewInfo(AViewInfo).ItemIndex :=
      Properties.GetRadioGroupItemIndex(AEditValue);
  if epoAutoHeight in PaintOptions then
    Include(AViewInfo.PaintOptions, epoAutoHeight);
end;

procedure TcxCustomRadioGroupViewData.GetEditMetrics(AAutoHeight: Boolean;
  ACanvas: TcxCanvas; out AMetrics: TcxEditMetrics);
const
  AColumnWidthCorrectionA: array [Boolean] of Integer = (7, 5);
  AAutoHeightColumnWidthCorrectionA: array [Boolean] of Integer = (3, 1);
var
  ANativeStyle: Boolean;
begin
  AMetrics.ClientLeftBoundCorrection := 6 - 5 * Integer(IsInplace);
  AMetrics.ClientWidthCorrection := 4 * Integer(IsInplace) - 6;
  AMetrics.ColumnOffset := 0;
  if ACanvas = nil then
    Exit;

  ANativeStyle := IsButtonNativeStyle(Style.LookAndFeel);
  AMetrics.ButtonSize := Style.LookAndFeel.GetAvailablePainter(totButton).RadioButtonSize;
  AMetrics.ColumnWidthCorrection := AColumnWidthCorrectionA[ANativeStyle];
  AMetrics.AutoHeightColumnWidthCorrection :=
    AAutoHeightColumnWidthCorrectionA[ANativeStyle];
  AMetrics.WidthCorrection := 6 - 5 * Integer(IsInplace);
  AMetrics.AutoHeightWidthCorrection := Integer(IsInplace) - 6;
end;

⌨️ 快捷键说明

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