cxgridcustompopupmenu.pas

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

PAS
777
字号
begin
  FreeAndNil(FPopupMenuInfos);
  inherited Destroy;
end;

function TcxCustomGridPopupMenu.CheckShortCuts(var Message: TWMKeyDown): Boolean;
begin
  Result := not (csDesigning in ComponentState) and
    FPopupMenuInfos.IsShortCut(Message);
  Message.Result := Message.Result or Integer(Result);
end;

function TcxCustomGridPopupMenu.FindPopupMenuInfo(const AView: TcxCustomGridView;
  AHitType: TcxGridViewHitType; AHitTest: TcxCustomGridHitTest): TcxPopupMenuInfo;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Count - 1 do
    if AHitType in MenuInfos[I].HitTypes then
      if ((AHitType in [gvhtGridNone, gvhtGridTab]) or (MenuInfos[I].GridView = AView)) and
        MenuInfos[I].CanPopup(AHitTest) then
      begin
        Result := MenuInfos[I];
        Break;
      end
      else
        if AView.InheritsFrom(MenuInfos[I].GridViewClass) and
          ((Result = nil) or MenuInfos[I].GridViewClass.InheritsFrom(Result.GridViewClass)) and
          MenuInfos[I].CanPopup(AHitTest) then
          Result := MenuInfos[I];
end;

function TcxCustomGridPopupMenu.GetPopupMenuInfo(const AMenu: TComponent;
  const AView: TcxCustomGridView; ASearchDefault: Boolean): TcxPopupMenuInfo;
begin
  Result := Nil;
end;

function TcxCustomGridPopupMenu.GetPopupMenuInfo(const AMenu: TComponent;
  AViewClass: TcxCustomGridViewClass; ASearchDefault: Boolean = True): TcxPopupMenuInfo;
begin
  Result := nil;
end;

procedure TcxCustomGridPopupMenu.Notification(AComponent: TComponent;
  Operation: TOperation);
var
  i: Integer;
begin
  inherited Notification(AComponent, Operation);
  if (Operation = opRemove) then
  begin
    if AComponent.InheritsFrom(TcxCustomGridView) then
       ClearMenuInfosGridView(TcxCustomGridView(AComponent))
    else
      if Assigned(FPopupMenuInfos) then
      begin
        i := FPopupMenuInfos.Count - 1;
        while i >= 0 do
          with FPopupMenuInfos[i] do
          begin
            if PopupMenu = AComponent then
              FPopupMenu := nil;
            i := i - 1;
          end;
      end;
  end;
end;

procedure TcxCustomGridPopupMenu.AddMenuInfo(const AMenu: TComponent;
  AHitTypes: TcxGridViewHitTypes; ADoPopupProc: TcxGridOnPopupProc;
  AViewClass: TcxCustomGridViewClass; const AView: TcxCustomGridView;
  AIsBuiltIn: Boolean);
begin
  with TcxPopupMenuInfo(FPopupMenuInfos.Add) do
  begin
    BeginUpdate;
    try
      PopupMenu := AMenu;
      OnPopup := ADoPopupProc;
      GridView := AView;
      GridViewClass := AViewClass;
      HitTypes := AHitTypes;
      FIsBuiltIn := AIsBuiltIn;
    finally
      EndUpdate;
    end;
  end;
end;

procedure TcxCustomGridPopupMenu.AdjustMenuInfo(AMenuInfo: TcxPopupMenuInfo;
  const AMenu: TComponent; AHitTypes: TcxGridViewHitTypes;
  ADoPopupProc: TcxGridOnPopupProc; AViewClass: TcxCustomGridViewClass = nil;
  const AView: TcxCustomGridView = nil; AIsBuiltIn: Boolean = False);
begin
  if AMenuInfo <> nil then
    AMenuInfo.HitTypes := AMenuInfo.HitTypes + AHitTypes
  else
    AddMenuInfo(AMenu, AHitTypes, ADoPopupProc, AViewClass, AView, AIsBuiltIn);
end;

procedure TcxCustomGridPopupMenu.ClearMenuInfosGridView(
  const AView: TcxCustomGridView = nil);
var
  I: Integer;
begin
  for I := 0 to PopupMenuInfos.Count - 1 do
    with PopupMenuInfos[I] do
    begin
      if Assigned(AView) and (GridView <> AView) then
        Continue;
      BeginUpdate;
      try
        GridView := nil;
      finally
        EndUpdate;
      end;
    end;
end;

function TcxCustomGridPopupMenu.GetMenuInfo(AIndex: Integer): TcxPopupMenuInfo;
begin
  Result := FPopupMenuInfos[AIndex];
end;

function TcxCustomGridPopupMenu.GetMenuInfoCount: Integer;
begin
  Result := FPopupMenuInfos.Count;
end;

procedure TcxCustomGridPopupMenu.SetPopupMenuInfos(const AValue: TcxPopupMenuInfos);
begin
  FPopupMenuInfos := AValue;
end;

{ TcxGridDefaultPopupMenu }

function TcxGridDefaultPopupMenu.GetPopupMenuInfo(const AMenu: TComponent;
  AViewClass: TcxCustomGridViewClass; ASearchDefault: Boolean): TcxPopupMenuInfo;
var
  I : Integer;
  AClass: TcxCustomGridViewClass;
begin
  Result := nil;
  for I := 0 to Count - 1 do
    if AMenu = MenuInfos[I].PopupMenu then
    begin
      AClass := MenuInfos[I].GridViewClass;
      if (AViewClass = AClass) then
      begin
        Result := MenuInfos[I];
        exit;
      end;
      if AViewClass.InheritsFrom(AClass) and ASearchDefault then
        if (Result = nil) or AClass.InheritsFrom(Result.GridViewClass) then
        begin
          Result := MenuInfos[I];
        end;
    end;
end;

procedure TcxGridDefaultPopupMenu.RegisterPopupMenu(const AMenu: TComponent;
  AHitTypes: TcxGridViewHitTypes; ADoPopupProc: TcxGridOnPopupProc;
  AViewClass: TcxCustomGridViewClass);
begin
  InternalRegisterPopupMenu(AMenu, AHitTypes, ADoPopupProc, AViewClass);
end;

procedure TcxGridDefaultPopupMenu.UnRegisterPopupMenu(const AMenu: TComponent;
  AViewClass: TcxCustomGridViewClass; AHitTypes: TcxGridViewHitTypes);
var
  AMenuInfo: TcxPopupMenuInfo;
begin
  AMenuInfo := GetPopupMenuInfo(AMenu, AViewClass, false);
  If AMenuInfo <> nil then
    If AMenuInfo.HitTypes = AHitTypes then
      AMenuInfo.Free
    else
      AMenuInfo.HitTypes := AMenuInfo.HitTypes - AHitTypes;
end;

function TcxGridDefaultPopupMenu.GetPopupMenuInfo(const AMenu: TComponent;
  const AView: TcxCustomGridView; ASearchDefault: Boolean): TcxPopupMenuInfo;
begin
  Result := GetPopupMenuInfo(AMenu,TcxCustomGridViewClass(AView.ClassType),
    ASearchDefault);
end;

procedure TcxGridDefaultPopupMenu.InternalRegisterPopupMenu(
  const AMenu: TComponent; AHitTypes: TcxGridViewHitTypes;
  ADoPopupProc: TcxGridOnPopupProc; AViewClass: TcxCustomGridViewClass;
  AIsBuiltIn: Boolean);
begin
  if not Assigned(AViewClass) then exit;
  AdjustMenuInfo(GetPopupMenuInfo(AMenu, AViewClass), AMenu, AHitTypes,
    ADoPopupProc, AViewClass, nil ,AIsBuiltIn);
end;

{ TcxPopupMenuInfos }

constructor TcxPopupMenuInfos.Create(AOwner: TComponent);
begin
  FOwner := AOwner;
  inherited Create(TcxPopupMenuInfo);
end;

function TcxPopupMenuInfos.GetSameMenuCount(
  const AMenu: TComponent): Integer;
var
  I: Integer;
begin
  Result := 0;
  for I := 0 to Count - 1 do
    if Items[I].PopupMenu = AMenu then
      Inc(Result);
end;

function TcxPopupMenuInfos.GetOwner: TPersistent;
begin
  Result := FOwner;
end;

function TcxPopupMenuInfos.IsMenuInfoShortCut(AMenuInfo: TcxPopupMenuInfo;
  var Message: TWMKey): Boolean;
var
  AIcxPopupMenu: IcxPopupMenu;
begin
  Result := Assigned(AMenuInfo.PopupMenu);
  if not Result then
    Exit;
  Result := (AMenuInfo.PopupMenu is TPopupMenu);
  if Result then
    Result := (AMenuInfo.PopupMenu as TPopupMenu).IsShortCut(Message)
  else
  begin
    Result := Supports(AMenuInfo.PopupMenu, IcxPopupMenu, AIcxPopupMenu) and
      (AIcxPopupMenu <> nil);
    if Result then
      Result := AIcxPopupMenu.IsShortCutKey(Message);
  end;
end;

function TcxPopupMenuInfos.IsShortCut(var Message: TWMKey): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to Count - 1 do
  begin
    Result := IsMenuInfoShortCut(Items[I], Message);
    if Result then
      Break;
  end;
end;

{$IFDEF DELPHI6}
procedure TcxPopupMenuInfos.Notify(Item: TCollectionItem;
  Action: TCollectionNotification);
begin
  If Action = cnDeleting then TcxPopupMenuInfo(Item).PopupMenu := nil;
  inherited;
end;
{$ENDIF}

function TcxPopupMenuInfos.GetItem(AIndex: Integer): TcxPopupMenuInfo;
begin
  Result := TcxPopupMenuInfo(inherited GetItem(AIndex));
end;

procedure TcxPopupMenuInfos.SetItem(AIndex: Integer;
  const AValue: TcxPopupMenuInfo);
begin
  inherited SetItem(AIndex , AValue);
end;

{ TcxPopupMenuClassInfoRec }

constructor TcxPopupMenuClassInfoRec.Create(AMenuClass: TcxPopupMenuClass;
  AHitTypes: TcxGridViewHitTypes; AViewClass: TcxCustomGridViewClass);
begin
  inherited Create;
  MenuClass := AMenuClass;
  HitTypes := AHitTypes;
  ViewClass := AViewClass;
end;

{ TcxBuiltInPopupMenus }

constructor TcxBuiltInPopupMenus.Create;
begin
  inherited;
  FInfoRecs := TList.Create;
end;

destructor TcxBuiltInPopupMenus.Destroy;
begin
  Clear;
  FreeAndNil(FInfoRecs);
  inherited;
end;

function TcxBuiltInPopupMenus.GetCount: Integer;
begin
  Result := FInfoRecs.Count;
end;

function TcxBuiltInPopupMenus.GetItem(AIndex: Integer): TcxPopupMenuClassInfoRec;
begin
  Result := TcxPopupMenuClassInfoRec(FInfoRecs[AIndex]);
end;

function TcxBuiltInPopupMenus.Add(AMenuClass: TcxPopupMenuClass;
  AHitTypes: TcxGridViewHitTypes; AViewClass: TcxCustomGridViewClass): TcxPopupMenuClassInfoRec;
begin
  Result := TcxPopupMenuClassInfoRec.Create(AMenuClass, AHitTypes, AViewClass);
  FInfoRecs.Add(Result);
end;

procedure TcxBuiltInPopupMenus.Clear;
begin
  while Count <> 0 do
    Delete(Count - 1);
end;

procedure TcxBuiltInPopupMenus.Delete(AIndex: Integer);
begin
  Items[AIndex].Free;
  FInfoRecs.Delete(AIndex);
end;

procedure TcxBuiltInPopupMenus.RegisterItemTo(ADefaultPopupMenu: TcxGridDefaultPopupMenu;
  AItemIndex: Integer);
var
  AMenu: TComponent;
  AIDoPopup: IDoPopup;
  ADoPopupProc: TcxGridOnPopupProc;
begin
  AMenu := Items[AItemIndex].MenuClass.Create(nil);
  if Supports(AMenu, IDoPopup, AIDoPopup) then
    ADoPopupProc := AIDoPopup.GetPopupHandler
  else
    ADoPopupProc := nil;
  ADefaultPopupMenu.InternalRegisterPopupMenu(AMenu, Items[AItemIndex].HitTypes,
    ADoPopupProc, Items[AItemIndex].ViewClass, True);
end;

procedure TcxBuiltInPopupMenus.Remove(APopupMenuClass: TcxPopupMenuClass);
var
  I: Integer;
begin
  for I := Count - 1 downto 0 do
    if Items[I].MenuClass = APopupMenuClass then
      Delete(I);
end;

procedure TcxBuiltInPopupMenus.RegisterItemsTo(ADefaultPopupMenu: TcxGridDefaultPopupMenu);
var
  I: Integer;
begin
  for I := 0 to Count - 1 do
    RegisterItemTo(ADefaultPopupMenu, I);
end;

procedure RegisterPopupMenuClass(APopupMenuClass: TcxPopupMenuClass;
  AHitTypes: TcxGridViewHitTypes; AViewClass: TcxCustomGridViewClass);
begin
  BuiltInPopupMenuList.Add(APopupMenuClass, AHitTypes, AViewClass);
end;

procedure UnregisterPopupMenuClass(APopupMenuClass: TcxPopupMenuClass);
begin
  BuiltInPopupMenuList.Remove(APopupMenuClass);
end;

initialization
  BuiltInPopupMenuList := TcxBuiltInPopupMenus.Create;
{$IFDEF DELPHI6}
  StartClassGroup(TControl);
  GroupDescendentsWith(TcxCustomGridPopupMenu, TControl);
{$ENDIF}

finalization
  FreeAndNil(BuiltInPopupMenuList);

end.

⌨️ 快捷键说明

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