cxdesignwindows.pas

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

PAS
1,433
字号
var
  ASelectionList: TDesignerSelectionList;
  I: Integer;
begin
  if Designer = nil then Exit;
  ASelectionList := CreateDesignerSelectionList;
  try
    for I := 0 to AList.Count - 1 do
      ASelectionList.Add(TPersistent(AList[I]));
    Designer.SetSelections(ASelectionList);
  finally
    DeleteDesignerSelectionList(ASelectionList);
  end;
end;

procedure TcxDesignHelper.UnselectObject(AObject: TPersistent);
var
  AList: TList;
begin
  if not IsObjectSelected(AObject) then Exit;
  AList := TList.Create;
  try
    GetSelection(AList);
    AList.Remove(AObject);
    SetSelection(AList);
  finally
    AList.Free;
  end;
end;

procedure TcxDesignHelper.AddSelectionChangedListener(AListener: TPersistent);
begin
  if (AListener <> nil) and Supports(AListener, IcxDesignSelectionChanged) and
    (FList.IndexOf(AListener) < 0) then
  begin
    if FWindow = nil then
    begin
      FWindow := TcxDesignWindow.Create(nil);
      FWindow.OnSelectionsChanged := SelectionsChanged;
    end;
    FList.Add(AListener);
  end;
end;

procedure TcxDesignHelper.RemoveSelectionChangedListener(AListener: TPersistent);
begin
  FList.Remove(AListener);
  if FList.Count = 0 then
    FreeAndNil(FWindow);
end;

function TcxDesignHelper.CanAddComponent(AOwner: TComponent): Boolean;
begin
  Result := cxDesignWindows.CanAddComponent(AOwner.Owner);
end;

function TcxDesignHelper.CanDeleteComponent(AOwner,
  AComponent: TComponent): Boolean;
begin
  Result := cxDesignWindows.CanDeleteComponent(AOwner.Owner, AComponent);
end;

procedure TcxDesignHelper.ChangeSelection(AOwner: TComponent; AObject: TPersistent);
begin
  Component := AOwner;
  ChangeSelection(AObject);
end;

function TcxDesignHelper.IsObjectSelected(AOwner: TComponent; AObject: TPersistent): Boolean;
begin
  Component := AOwner;
  Result := IsObjectSelected(AObject);
end;

procedure TcxDesignHelper.Modified;
begin
  Designer.Modified;
end;

procedure TcxDesignHelper.SelectObject(AOwner: TComponent;
  AObject: TPersistent; AClearSelection: Boolean = True;
  AActivateOwner: Boolean = True);
begin
  Component := AOwner;
  SelectObject(AObject, AClearSelection, AActivateOwner);
end;

procedure TcxDesignHelper.ShowComponentDefaultEventHandler(AComponent: TComponent);
var
  APropInfo: PPropInfo;
  AMethod: TMethod;
  AMethodName: string;
begin
  Component := AComponent;
  APropInfo := GetPropInfo(Component.ClassInfo, 'OnChange');
  if APropInfo = nil then
  begin
    APropInfo := GetPropInfo(Component.ClassInfo, 'OnClick');
    if APropInfo = nil then
      Exit
    else
      AMethodName := 'Click';
  end
  else
    AMethodName := 'Change';
  AMethod := GetMethodProp(Component, APropInfo);
  if AMethod.Code <> nil then
    AMethodName := Designer.GetMethodName(AMethod)
  else
  begin
    AMethodName := AComponent.Name + AMethodName;
    AMethod := GetObjectDesigner(Component).CreateMethod(AMethodName, GetTypeData(APropInfo.PropType^));
    SetMethodProp(Component, APropInfo, AMethod);
    Designer.Modified;
  end;
  Designer.ShowMethod(AMethodName);
end;

function TcxDesignHelper.UniqueName(const ABaseName: string): string;
begin
  Result := Designer.UniqueName(ABaseName);
end;

procedure TcxDesignHelper.UnselectObject(AOwner: TComponent; AObject: TPersistent);
begin
  Component := AOwner;
  UnselectObject(AObject);
end;

function TcxDesignHelper.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
begin
  if GetInterface(IID, Obj) then
    Result := 0
  else
    Result := E_NOINTERFACE;
end;

function TcxDesignHelper._AddRef: Integer; stdcall;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TcxDesignHelper._Release: Integer; stdcall;
begin
  Result := InterlockedDecrement(FRefCount);
  if FRefCount = 0 then
    Destroy;
end;

procedure TcxDesignHelper.NotifyListeners(AList: TList);
var
  I: Integer;
  AIntf: IcxDesignSelectionChanged;
begin
  for I := 0 to FList.Count - 1 do
  begin
    if Supports(TObject(FList[I]), IcxDesignSelectionChanged, AIntf) then
    begin
      AIntf.DesignSelectionChanged(AList);
      AIntf := nil;
    end;
  end;
end;

procedure TcxDesignHelper.SelectionsChanged(Sender: TObject;
  const ASelection: TDesignerSelectionList);
var
  L: TList;
begin
  L := TList.Create;
  try
    ConvertSelectionToList(ASelection, L);
    NotifyListeners(L);
  finally
    L.Free;
  end;
end;

function TcxDesignHelper.GetDesigner: IDesigner;
begin
  if FDesigner = nil then
    FDesigner := GetObjectDesigner(FComponent);
  Result := FDesigner;
end;

{ TcxDesignWindow }

constructor TcxDesignWindow.Create(AOwner: TComponent);
begin
  inherited;
  Font.Name := DefFontData.Name;
end;

procedure TcxDesignWindow.BeginUpdate;
begin
  Inc(FLockCount);
end;

procedure TcxDesignWindow.CancelUpdate;
begin
  Dec(FLockCount);
end;

procedure TcxDesignWindow.EndUpdate;
begin
  Dec(FLockCount);
  if FLockCount = 0 then
    UpdateSelection;
end;

class function TcxDesignWindow.GetBaseRegKey{$IFDEF DELPHI6}(ADesigner: IComponentDesigner = nil){$ENDIF}: string;
begin
{$IFDEF DELPHI6}
  if ADesigner = nil then
    ADesigner := ActiveDesigner;
  if ADesigner = nil then
    Result := ''
  else
    Result := ADesigner.Environment.GetBaseRegKey + '\' + SIniEditorsName;
{$ELSE}
  Result := DelphiIDE.GetBaseRegKey + '\' + SIniEditorsName;
{$ENDIF}
end;

procedure TcxDesignWindow.GetSelectionList(AList: TList);
var
  ASelectionList: TDesignerSelectionList;
begin
  ASelectionList := CreateDesignerSelectionList;
  try
    Designer.GetSelections(ASelectionList);
    ConvertSelectionToList(ASelectionList, AList);
  finally
    DeleteDesignerSelectionList(ASelectionList);
  end;
end;

procedure TcxDesignWindow.SelectionChanged({$IFDEF DELPHI6}const ADesigner: IDesigner;{$ENDIF}
  {$IFDEF DELPHI6}const{$ENDIF}ASelection: TDesignerSelectionList);
begin
  if LockCount <> 0 then Exit;
  if Assigned(FOnSelectionsChanged) then
    FOnSelectionsChanged(Self, ASelection);
  {$IFDEF DELPHI6}
  if ADesigner = Designer then
  {$ENDIF}
    SelectionsChanged(ASelection);
end;

procedure TcxDesignWindow.SelectionsChanged(const ASelection: TDesignerSelectionList);
begin
end;

procedure TcxDesignWindow.SetSelectionList(AList: TList);
var
  ASelectionList: TDesignerSelectionList;
  I: Integer;
begin
  ASelectionList := CreateDesignerSelectionList;
  try
    for I := 0 to AList.Count - 1 do
      ASelectionList.Add(TPersistent(AList[I]));
    Designer.SetSelections(ASelectionList);
  finally
    DeleteDesignerSelectionList(ASelectionList);
  end;
end;

procedure TcxDesignWindow.UpdateSelection;
var
  ASelectionList: TDesignerSelectionList;
begin
  ASelectionList := CreateDesignerSelectionList;
  try
    Designer.GetSelections(ASelectionList);
    SelectionChanged({$IFDEF DELPHI6}Designer, {$ENDIF}ASelectionList);
  finally
    DeleteDesignerSelectionList(ASelectionList);
  end;
end;

procedure TcxDesignWindow.Activated;
begin
{$IFDEF DELPHI6}
  Designer.Activate;
{$ENDIF}
//  UpdateSelection; // TODO:
end;

function TcxDesignWindow.UniqueName(Component: TComponent): string;
begin
  Result := ''; //inherited UniqueName(Component);
end;

{ TcxDesignFormEditor }

{$IFDEF DELPHI6}

procedure TcxDesignFormEditor.DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean);
begin
  if Designer = ADesigner then
    CloseEditor;
  inherited;
end;

{$ELSE}

procedure TcxDesignFormEditor.FormClosed(AForm: TCustomForm);
begin
  if Designer.Form = AForm then
    CloseEditor;
  inherited;
end;

procedure TcxDesignFormEditor.ComponentDeleted(Component: IPersistent);
var
  AItem: TPersistent;
begin
  AItem := TryExtractPersistent(Component);
  if AItem <> nil then
    ItemDeleted(Designer, AItem);
end;

procedure TcxDesignFormEditor.FormModified;
begin
  ItemsModified(Designer);
end;

{$ENDIF}

procedure TcxDesignFormEditor.DoItemsModified;
begin
end;

procedure TcxDesignFormEditor.ItemDeleted(const ADesigner: IDesigner; AItem: TPersistent);
begin
  if (AItem = nil) or Closing then Exit;
  if (Component = nil) or (csDestroying in Component.ComponentState) or
    (AItem = Component) then
    CloseEditor;
end;

procedure TcxDesignFormEditor.ItemsModified(const Designer: IDesigner);
begin
 if Closing or (Component = nil) or (csDestroying in Component.ComponentState) then
   Exit;
  UpdateCaption;
  DoItemsModified;
end;

procedure TcxDesignFormEditor.SelectComponent(AComponent: TPersistent);
begin
  Designer.SelectComponent(AComponent);
end;

procedure TcxDesignFormEditor.SelectComponents(AList: TList; ADefaultComponent: TPersistent);
begin
  if not Active then Exit;
  if AList.Count > 0 then
    SetSelectionList(AList)
  else
    if Component <> nil then
    begin
      if ADefaultComponent <> nil then
        SelectComponent(ADefaultComponent)
      else
        SelectComponent(Component);
    end;
end;

procedure TcxDesignFormEditor.ListBoxApplySelection(AListBox: TListBox;
  ADefaultComponent: TPersistent);
var
  AList: TList;
begin
  BeginUpdate;
  try
    AList := TList.Create;
    try
      ListBoxGetSelection(AListBox, AList);
      SelectComponents(AList, ADefaultComponent);
    finally
      AList.Free;
    end;
  finally
    EndUpdate;
  end;
end;

procedure TcxDesignFormEditor.ListBoxSynchronizeSelection(AListBox: TListBox);
var
  AList: TList;
begin
  AList := TList.Create;
  try
    GetSelectionList(AList);
    ListBoxSyncSelection(AListBox, AList);
  finally
    AList.Free;
  end;
end;

{$IFDEF DELPHI9}
procedure TcxDesignFormEditor.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.WndParent := Application.MainForm.Handle;
end;
{$ENDIF}

procedure TcxDesignFormEditor.CloseEditor;
begin
  FClosing := True;
  Component := nil;
  ComponentProperty := nil;
  ComponentPropertyName := '';
  Close;
end;

procedure TcxDesignFormEditor.UpdateCaption;
var
  S: string;
begin
  if (Component <> nil) and (Component.Name <> '') then
  begin
    S := Component.Name;
    if Component.Owner <> nil then
      S := Component.Owner.Name + '.' + S;
    Caption := S;
  end;
end;

procedure TcxDesignFormEditor.InitFormEditor;
begin
  UpdateCaption;
end;

procedure TcxDesignFormEditor.UpdateContent;
begin
end;

function TcxDesignFormEditor.CanAddComponent: Boolean;
begin
  Result := cxDesignWindows.CanAddComponent(Component.Owner, Designer);
end;

function TcxDesignFormEditor.CanDeleteComponent(AComponent: TComponent): Boolean;
begin
  Result := cxDesignWindows.CanDeleteComponent(Component.Owner, AComponent, Designer);
end;

procedure TcxDesignFormEditor.FormCreate(Sender: TObject);
begin
  if Editors <> nil then
    Editors.Add(Self);
end;

procedure TcxDesignFormEditor.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  // TODO: save pos
  Action := caFree;
end;

procedure TcxDesignFormEditor.FormDestroy(Sender: TObject);
begin
  if Editors <> nil then
    Editors.Remove(Self);
end;

initialization

finalization
  FreeAndNil(Editors);

end.

⌨️ 快捷键说明

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