?? sbedit.pas
字號:
{*******************************************************}
{ }
{ Delphi VCL Extensions (RX) }
{ }
{ Copyright (c) 1995, 1996 AO ROSNO }
{ Copyright (c) 1997 Master-Bank }
{ }
{*******************************************************}
unit SbEdit;
{$I RX.INC}
interface
uses
Windows, RTLConsts, DesignIntf, DesignWindows, DesignEditors, VCLEditors,
SysUtils, Messages, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Grids, SpeedBar, Menus, Placemnt, RxConst, RxCtrls, VCLUtils;
type
{ TSpeedbarEditor }
TSelectData = record
bRowCount: Integer;
bRow: Integer;
sRowCount: Integer;
sRow: Integer;
end;
TSpeedbarEditor = class(TDesignWindow)
SectionsBox: TGroupBox;
NewSection: TButton;
DelSection: TButton;
ButtonsBox: TGroupBox;
UpBtn: TSpeedButton;
DownBtn: TSpeedButton;
AddButton: TButton;
RemoveButton: TButton;
CloseBtn: TButton;
SectionName: TEdit;
SectionNameLabel: TLabel;
SectionList: TDrawGrid;
ButtonsList: TDrawGrid;
LabelHint: TLabel;
PopupMenu: TPopupMenu;
CopyMenu: TMenuItem;
PasteMenu: TMenuItem;
CutMenu: TMenuItem;
FormPlacement1: TFormPlacement;
procedure DelSectionClick(Sender: TObject);
procedure AddButtonClick(Sender: TObject);
procedure RemoveButtonClick(Sender: TObject);
procedure CloseBtnClick(Sender: TObject);
procedure UpBtnClick(Sender: TObject);
procedure DownBtnClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure SectionNameExit(Sender: TObject);
procedure SectionListSelectCell(Sender: TObject; Col, Row: Longint;
var CanSelect: Boolean);
procedure SectionListDrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
procedure ButtonsListDblClick(Sender: TObject);
procedure ButtonsListKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ButtonsListMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ButtonsListMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure ButtonsListMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ButtonsListSelectCell(Sender: TObject; Col, Row: Longint;
var CanSelect: Boolean);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure NewSectionClick(Sender: TObject);
procedure SectionNameKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ButtonsListDrawCell(Sender: TObject; Col, Row: Longint;
Rect: TRect; State: TGridDrawState);
procedure SectionListMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure SectionListDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure SectionListDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure SectionListKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure CopyMenuClick(Sender: TObject);
procedure PasteMenuClick(Sender: TObject);
procedure CutMenuClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
FButton: TBtnControl;
FImage: TButtonImage;
FBar: TSpeedBar;
FDrag: Boolean;
FDragItem: TSpeedItem;
FLocked: Integer;
FSelectData: TSelectData;
procedure Copy;
procedure Cut;
procedure Paste;
procedure OnPasteItem(Item: TObject);
procedure SaveSelection;
procedure RestoreSelection;
procedure SelectButton(Section: Integer; Item: TSpeedItem; SelectBar: Boolean);
procedure UpdateEnabled(BtnRow, Section: Integer);
function CheckSpeedBar: Boolean;
function ConfirmDelete: Boolean;
function CurrentSection: Integer;
function GetForm: TCustomForm;
procedure SetSection(Section: Integer);
procedure UpdateData;
procedure UpdateListHeight;
procedure SetSpeedBar(Value: TSpeedBar);
function ItemByRow(Row: Integer): TSpeedItem;
function SectionByRow(Row: Integer): TSpeedbarSection;
function ItemBySectionRow(Section, Row: Integer): TSpeedItem;
procedure CMSpeedBarChanged(var Message: TMessage); message CM_SPEEDBARCHANGED;
protected
procedure Activated; override;
function UniqueName(Component: TComponent): string; override;
public
{ Public declarations }
procedure ItemsModified(const Designer : IDesigner); override;
procedure DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean); override;
function GetEditState: TEditState; override;
function EditAction(Action: TEditAction) : Boolean; override;
property SpeedBar: TSpeedBar read FBar write SetSpeedBar;
property OwnerForm: TCustomForm read GetForm;
end;
{ TSpeedbarCompEditor }
TSpeedbarCompEditor = class(TComponentEditor)
procedure ExecuteVerb(Index: Integer); override;
function GetVerb(Index: Integer): string; override;
function GetVerbCount: Integer; override;
end;
implementation
uses TypInfo, MaxMin, RXLConst, RxProps, RxDsgn;
{$R *.DFM}
{$IFDEF WIN32}
{$D-}
{$ENDIF}
{$IFDEF RX_D4}
type
TDesigner = IDesigner;
TFormDesigner = IDesigner;
{$ENDIF}
{ Utility routines }
function FindEditor(Speedbar: TSpeedbar): TSpeedbarEditor;
var
I: Integer;
begin
Result := nil;
for I := 0 to Screen.FormCount - 1 do begin
if Screen.Forms[I] is TSpeedbarEditor then begin
if TSpeedbarEditor(Screen.Forms[I]).SpeedBar = SpeedBar then
begin
Result := TSpeedbarEditor(Screen.Forms[I]);
Break;
end;
end;
end;
end;
procedure ShowSpeedbarDesigner(Designer: TDesigner; Speedbar: TSpeedbar);
var
Editor: TSpeedbarEditor;
begin
if Speedbar = nil then Exit;
Editor := FindEditor(Speedbar);
if Editor <> nil then begin
Editor.Show;
if Editor.WindowState = wsMinimized then Editor.WindowState := wsNormal;
end
else begin
Editor := TSpeedbarEditor.Create(Application);
try
Editor.Designer := TFormDesigner(Designer);
Editor.Speedbar := Speedbar;
Editor.Show;
except
Editor.Free;
raise;
end;
end;
end;
{ TSpeedbarCompEditor }
procedure TSpeedbarCompEditor.ExecuteVerb(Index: Integer);
begin
case Index of
0: ShowSpeedbarDesigner(Designer, TSpeedbar(Component));
end;
end;
function TSpeedbarCompEditor.GetVerb(Index: Integer): string;
begin
case Index of
0: Result := LoadStr(srSpeedbarDesigner);
end;
end;
function TSpeedbarCompEditor.GetVerbCount: Integer;
begin
Result := 1;
end;
{ TSpeedbarEditor }
const
MaxBtnListHeight = 158;
function TSpeedbarEditor.UniqueName(Component: TComponent): string;
var
Temp: string;
begin
Result := '';
if (Component <> nil) then Temp := Component.ClassName
else Temp := TSpeedItem.ClassName;
if (UpCase(Temp[1]) = 'T') and (Length(Temp) > 1) then
System.Delete(Temp, 1, 1);
Result := Designer.UniqueName(Temp);
end;
function TSpeedbarEditor.GetEditState: TEditState;
begin
Result := [];
if RemoveButton.Enabled then begin
Result := [esCanDelete, esCanCut, esCanCopy];
end;
if AddButton.Enabled and ClipboardComponents then
Include(Result, esCanPaste);
end;
function TSpeedbarEditor.EditAction(Action: TEditAction) : Boolean;
begin
Result := True;
case Action of
eaCut: Cut;
eaCopy: Copy;
eaPaste: Paste;
eaDelete: RemoveButtonClick(Self);
end;
end;
procedure TSpeedbarEditor.SelectButton(Section: Integer; Item: TSpeedItem;
SelectBar: Boolean);
var
FCompList: IDesignerSelections;
Sect: TSpeedbarSection;
begin
if CheckSpeedBar and Active then begin
//Designer.GetSelections(FCompList);
FCompList := CreateSelectionList;
if not SelectBar then begin
if (ActiveControl = SectionList) or (ActiveControl = SectionName) then
begin
Sect := SectionByRow(Section);
if Sect <> nil then FCompList.Add(Sect);
end;
if (FCompList.Count = 0) and (Item <> nil) then FCompList.Add(Item);
end;
if (FBar <> nil) and (FCompList.Count = 0) then FCompList.Add(FBar);
SetSelection(FCompList);
end;
end;
procedure TSpeedbarEditor.DesignerClosed(const ADesigner: IDesigner; AGoingDormant: Boolean);
begin
if ADesigner.Root = OwnerForm then Free;
end;
procedure TSpeedbarEditor.ItemsModified(const Designer : IDesigner);
begin
if not (csDestroying in ComponentState) then UpdateData;
end;
procedure TSpeedbarEditor.Activated;
begin
SelectButton(CurrentSection, ItemByRow(ButtonsList.Row), False);
PasteMenu.Enabled := CheckSpeedBar and (FBar.SectionCount > 0) and
ClipboardComponents;
end;
function TSpeedbarEditor.ConfirmDelete: Boolean;
begin
Result := MessageDlg(LoadStr(srConfirmSBDelete), mtWarning, mbYesNoCancel, 0) = mrYes;
end;
procedure TSpeedbarEditor.SaveSelection;
begin
with FSelectData do begin
bRowCount := ButtonsList.RowCount;
bRow := ButtonsList.Row;
sRowCount := SectionList.RowCount;
sRow := SectionList.Row;
end;
end;
procedure TSpeedbarEditor.RestoreSelection;
var
NewSRow, NewBRow: Integer;
begin
NewSRow := FSelectData.sRow;
if (SectionList.RowCount > FSelectData.sRowCount) or
(NewSRow > SectionList.RowCount - 1) then
NewSRow := SectionList.RowCount - 1;
if NewSRow < 0 then NewSRow := 0;
SectionList.Row := NewSRow;
SetSection(SectionList.Row); { set ButtonsList to current section }
NewBRow := FSelectData.bRow;
if (ButtonsList.RowCount > FSelectData.bRowCount) or
(NewBRow > ButtonsList.RowCount - 1) then
NewBRow := ButtonsList.RowCount - 1;
if NewBRow < 0 then NewBRow := 0;
ButtonsList.Row := NewBRow;
end;
procedure TSpeedbarEditor.UpdateEnabled(BtnRow, Section: Integer);
var
EnableSect, EnableBtn: Boolean;
begin
EnableSect := CheckSpeedBar and (FBar.SectionCount > 0);
EnableBtn := EnableSect and (BtnRow >= 0) and (ItemBySectionRow(Section,
BtnRow) <> nil);
DelSection.Enabled := EnableSect;
SectionName.Enabled := EnableSect;
AddButton.Enabled := EnableSect;
RemoveButton.Enabled := EnableBtn;
CopyMenu.Enabled := EnableBtn;
CutMenu.Enabled := EnableBtn;
PasteMenu.Enabled := EnableSect and ClipboardComponents;
UpBtn.Enabled := EnableBtn and (BtnRow > 0);
DownBtn.Enabled := EnableBtn and (BtnRow < ButtonsList.RowCount - 1);
end;
function TSpeedbarEditor.CheckSpeedBar: Boolean;
begin
Result := (FBar <> nil) and (FBar.Owner <> nil) and (FBar.Parent <> nil)
and (Designer.Root <> nil);
end;
function TSpeedbarEditor.CurrentSection: Integer;
begin
if CheckSpeedBar and (FBar.SectionCount > 0) then
Result := SectionList.Row
else Result := -1;
end;
procedure TSpeedbarEditor.SetSection(Section: Integer);
var
I: Integer;
begin
if CheckSpeedBar then begin
I := Section;
if (I >= 0) and (I < FBar.SectionCount) then begin
SectionName.Text := TSpeedbarSection(FBar.Sections[I]).Caption;
ButtonsList.RowCount := FBar.ItemsCount(I);
end
else begin
SectionName.Text := '';
ButtonsList.RowCount := 0;
end;
SectionList.DefaultColWidth := SectionList.ClientWidth;
ButtonsList.DefaultColWidth := ButtonsList.ClientWidth;
end;
end;
procedure TSpeedbarEditor.UpdateData;
begin
Inc(FLocked);
try
SaveSelection;
if CheckSpeedBar then SectionList.RowCount := FBar.SectionCount
else SectionList.RowCount := 0;
RestoreSelection; { set section }
finally
Dec(FLocked);
end;
UpdateEnabled(ButtonsList.Row, SectionList.Row);
SelectButton(CurrentSection, ItemByRow(ButtonsList.Row), False);
end;
function TSpeedbarEditor.GetForm: TCustomForm;
begin
Result := TCustomForm(Designer.Root); { GetParentForm(FBar) }
end;
procedure TSpeedbarEditor.UpdateListHeight;
var
Cnt: Integer;
MaxHeight: Integer;
begin
Canvas.Font := Font;
MaxHeight := MulDiv(MaxBtnListHeight, Screen.PixelsPerInch, 96);
ButtonsList.DefaultRowHeight := FBar.BtnHeight + 2;
Cnt := Max(1, Max(ButtonsList.ClientHeight, MaxHeight) div
(FBar.BtnHeight + 2));
ButtonsList.ClientHeight := Min(ButtonsList.DefaultRowHeight * Cnt,
MaxHeight);
SectionList.DefaultRowHeight := Canvas.TextHeight('Wg') + 2;
end;
procedure TSpeedbarEditor.SetSpeedBar(Value: TSpeedBar);
var
I: Integer;
begin
if FBar <> Value then begin
if FBar <> nil then FBar.SetEditing(0);
FBar := Value;
if FBar <> nil then FBar.SetEditing(Handle);
Inc(FLocked);
try
if FBar <> nil then UpdateListHeight;
if FBar.SectionCount = 0 then NewSectionClick(Self)
else
for I := 0 to FBar.SectionCount - 1 do begin
if FBar.Sections[I].Name = '' then begin
FBar.Sections[I].Name := UniqueName(FBar.Sections[I]);
Designer.Modified;
end;
end;
if ButtonsList.RowCount > 0 then ActiveControl := ButtonsList
else ActiveControl := SectionList;
UpdateData;
ButtonsList.Row := 0;
finally
Dec(FLocked);
end;
SectionList.Row := 0;
end;
end;
procedure TSpeedbarEditor.CMSpeedBarChanged(var Message: TMessage);
begin
if Pointer(Message.LParam) = FBar then begin
case Message.WParam of
SBR_CHANGED: Designer.Modified;
SBR_DESTROYED: Close;
SBR_BTNSIZECHANGED: if FBar <> nil then UpdateListHeight;
end;
end
else if (Message.WParam = SBR_BTNSELECT) and CheckSpeedBar then begin
SelectButton(-1, nil, True);
Designer.Modified;
end;
end;
function TSpeedbarEditor.ItemBySectionRow(Section, Row: Integer): TSpeedItem;
begin
if CheckSpeedBar then Result := FBar.Items(Section, Row)
else Result := nil;
end;
function TSpeedbarEditor.SectionByRow(Row: Integer): TSpeedbarSection;
begin
if CheckSpeedBar and (Row >= 0) and (Row < FBar.SectionCount) then
Result := FBar.Sections[Row]
else Result := nil;
end;
function TSpeedbarEditor.ItemByRow(Row: Integer): TSpeedItem;
begin
Result := ItemBySectionRow(CurrentSection, Row);
end;
procedure TSpeedbarEditor.NewSectionClick(Sender: TObject);
var
S: string;
I: Integer;
begin
if CheckSpeedBar then begin
I := 0;
repeat
S := Format(LoadStr(srNewSectionName), [I]);
Inc(I);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -