IDEIntf: started copy-paste event

git-svn-id: trunk@30932 -
This commit is contained in:
mattias 2011-05-27 14:39:33 +00:00
parent b7e29033dd
commit 512b557964
5 changed files with 118 additions and 20 deletions

View File

@ -31,7 +31,7 @@ interface
uses
Classes, SysUtils, LResources, Controls,
IDECommands, MenuIntf, IDEWindowIntf,
IDECommands, MenuIntf, IDEWindowIntf, SrcEditorIntf,
CodyStrConsts, CodyCtrls, PPUListDlg, AddAssignMethodDlg,
CodyUtils, CodyNodeInfoDlg, CodyFrm, DeclareVarDlg;
@ -148,6 +148,8 @@ begin
CodyWindowCreator:=IDEWindowCreators.Add(CodyWindowName,@CreateCodyWindow,nil,
'80%','50%','+18%','+25%','CodeExplorer',alBottom);
// Global handlers - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SourceEditorManagerIntf.RegisterCopyPasteEvent(@Cody.SrcEditCopyPaste);
end;
end.

View File

@ -30,7 +30,7 @@ unit CodyUtils;
interface
uses
Classes, SysUtils, Dialogs, Controls, LCLIntf, Clipbrd, LCLType,
Classes, SysUtils, Dialogs, Controls, LCLIntf, Clipbrd, LCLType, LResources,
// IDEIntf
IDEDialogs, LazIDEIntf, SrcEditorIntf, IDEHelpIntf,
// codetools
@ -50,7 +50,7 @@ type
function ReadString(MemStream: TMemoryStream): string;
procedure WriteToStream(MemStream: TMemoryStream); virtual; abstract;
procedure ReadFromStream(MemStream: TMemoryStream); virtual; abstract;
procedure Execute; virtual;
procedure Execute({%H-}SrcEdit: TSourceEditorInterface; {%H-}LogXY: TPoint); virtual;
end;
TCodyClipboardFormat = class of TCodyClipboardData;
@ -82,7 +82,8 @@ type
// clipboard
class function ClipboardFormatId: TClipboardFormat;
function CanReadFromClipboard(AClipboard: TClipboard): Boolean;
function ReadFromClipboard(AClipboard: TClipboard): boolean;
function ReadFromClipboard(AClipboard: TClipboard;
SrcEdit: TSourceEditorInterface; LogXY: TPoint; AText: string): boolean;
function WriteToClipboard(Data: TCodyClipboardData;
AClipboard: TClipboard = nil): Boolean;
procedure RegisterClipboardFormat(ccFormat: TCodyClipboardFormat);
@ -90,6 +91,9 @@ type
function ClipboardFormatCount: integer;
property ClipboardFormats[Index: integer]: TCodyClipboardFormat
read GetClipboardFormats;
procedure SrcEditCopyPaste(SrcEdit: TSourceEditorInterface;
var AText: String; var {%H-}AMode: TSemSelectionMode; ALogStartPos: TPoint;
var AnAction: TSemCopyPasteAction);
end;
var
@ -358,15 +362,15 @@ end;
procedure TCodyClipboardSrcData.WriteToStream(MemStream: TMemoryStream);
begin
WriteString(MemStream,SourceFilename);
MemStream.Write(SourceX,4);
MemStream.Write(SourceY,4);
WriteLRSInteger(MemStream,SourceY);
WriteLRSInteger(MemStream,SourceX);
end;
procedure TCodyClipboardSrcData.ReadFromStream(MemStream: TMemoryStream);
begin
SourceFilename:=ReadString(MemStream);
MemStream.Read(SourceX,4);
MemStream.Read(SourceY,4);
SourceY:=ReadLRSInteger(MemStream);
SourceX:=ReadLRSInteger(MemStream);
end;
{ TCodyClipboardData }
@ -386,7 +390,7 @@ begin
b:=255;
MemStream.Write(b,1);
l:=length(s);
MemStream.Write(l,4);
WriteLRSInteger(MemStream,l);
MemStream.Write(s[1],l);
end;
end;
@ -404,15 +408,16 @@ begin
if Result<>'' then
MemStream.Read(Result[1],b);
end else begin
l:=0;
MemStream.Read(l,4);
l:=ReadLRSInteger(MemStream);
if l<=0 then exit;
SetLength(Result,l);
MemStream.Read(Result[1],l);
end;
debugln(['TCodyClipboardData.ReadString Result="',Result,'"']);
end;
procedure TCodyClipboardData.Execute;
procedure TCodyClipboardData.Execute(SrcEdit: TSourceEditorInterface;
LogXY: TPoint);
begin
raise Exception.Create('not implemented yet: '+ClassName+'.Execute');
end;
@ -461,7 +466,8 @@ begin
Result := AClipboard.HasFormat(ClipboardFormatId);
end;
function TCody.ReadFromClipboard(AClipboard: TClipboard): boolean;
function TCody.ReadFromClipboard(AClipboard: TClipboard;
SrcEdit: TSourceEditorInterface; LogXY: TPoint; AText: string): boolean;
procedure InvalidStream;
begin
@ -482,6 +488,7 @@ begin
try
Result:=AClipboard.GetFormat(ClipboardFormatId,MemStream);
ID:='';
MemStream.Position:=0;
if MemStream.Read(ID[0],1)<>1 then
InvalidStream;
if MemStream.Read(ID[1],ord(ID[0]))<>ord(ID[0]) then
@ -490,8 +497,9 @@ begin
if aFormat=nil then
InvalidStream;
Data:=aFormat.Create;
Data.AsText:=AText;
Data.ReadFromStream(MemStream);
Data.Execute;
Data.Execute(SrcEdit,LogXY);
finally
Data.Free;
MemStream.Free;
@ -503,6 +511,7 @@ function TCody.WriteToClipboard(Data: TCodyClipboardData; AClipboard: TClipboard
var
MemStream: TMemoryStream;
ID: ShortString;
s: string;
begin
if AClipboard=nil then AClipboard:=Clipboard;
AClipboard.AsText:=Data.AsText;
@ -510,9 +519,14 @@ begin
raise Exception.Create('Write to clipboard failed');
MemStream:=TMemoryStream.Create;
try
ID:=AClipboard.ClassName;
ID:=Data.ClassName;
MemStream.Write(ID[0],length(ID)+1);
Data.WriteToStream(MemStream);
MemStream.Position:=0;
SetLength(s,MemStream.Size);
MemStream.Read(s[1],length(s));
debugln(['TCody.WriteToClipboard Stream=',dbgstr(s)]);
MemStream.Position:=0;
Result:=AClipboard.AddFormat(ClipboardFormatId,MemStream);
finally
MemStream.Free;
@ -542,6 +556,25 @@ begin
Result:=FClipboardFormats.Count;
end;
procedure TCody.SrcEditCopyPaste(SrcEdit: TSourceEditorInterface;
var AText: String; var AMode: TSemSelectionMode; ALogStartPos: TPoint;
var AnAction: TSemCopyPasteAction);
var
AClipBoard: TClipboard;
begin
// ToDo: use the right clipboard
AClipBoard:=Clipboard;
try
if not ReadFromClipboard(AClipBoard,SrcEdit,ALogStartPos,AText) then exit;
except
on E: Exception do begin
IDEMessageDialog('Error','Unable to paste Cody data.'#13+E.Message,
mtError,[mbCancel]);
end;
end;
AnAction:=semcaAbort;
end;
initialization
Cody:=TCody.Create;
finalization

View File

@ -29,7 +29,6 @@
- guess parameter
- guess j:=<i>
- Extend uses section when adding to a class
- copy to clipboard
- paste from clipboard
}
unit DeclareVarDlg;
@ -69,7 +68,7 @@ type
TheUnitName: string;
procedure WriteToStream(MemStream: TMemoryStream); override;
procedure ReadFromStream(MemStream: TMemoryStream); override;
procedure Execute; override;
procedure Execute(SrcEdit: TSourceEditorInterface; {%H-}LogXY: TPoint); override;
end;
{ TCodyDeclareVarDialog }
@ -214,9 +213,12 @@ begin
TheUnitName:=ReadString(MemStream);
end;
procedure TCodyClipboardDeclareVar.Execute;
procedure TCodyClipboardDeclareVar.Execute(SrcEdit: TSourceEditorInterface;
LogXY: TPoint);
begin
inherited Execute;
debugln(['TCodyClipboardDeclareVar.Execute ']);
SrcEdit.Selection:=AsText;
end;
{ TCodyDeclareVarTarget }

View File

@ -808,6 +808,10 @@ type
property NotebookPages: TStrings read GetNotebookPages;
end;
TSrcEditMangerHandlerType = (
semhtCopyPaste
);
{ TSourceEditorManagerBase }
(* Implement all Methods with the Interface types *)
@ -826,6 +830,7 @@ type
protected
fProducers: TFPList; // list of TSourceMarklingProducer
FChangeNotifyLists: Array [TsemChangeReason] of TMethodList;
FHandlers: array[TSrcEditMangerHandlerType] of TMethodList;
function GetActiveSourceWindow: TSourceEditorWindowInterface; override;
procedure SetActiveSourceWindow(const AValue: TSourceEditorWindowInterface); override;
function GetSourceWindows(Index: integer): TSourceEditorWindowInterface; override;
@ -888,6 +893,8 @@ type
destructor Destroy; override;
procedure RegisterChangeEvent(AReason: TsemChangeReason; AHandler: TNotifyEvent); override;
procedure UnRegisterChangeEvent(AReason: TsemChangeReason; AHandler: TNotifyEvent); override;
procedure RegisterCopyPasteEvent(AHandler: TSemCopyPasteEvent); override;
procedure UnRegisterCopyPasteEvent(AHandler: TSemCopyPasteEvent); override;
// producers
function MarklingProducerCount: integer; override;
procedure RegisterMarklingProducer(aProducer: TSourceMarklingProducer); override;
@ -4333,7 +4340,24 @@ var
NewIndent: TFABIndentationPolicy;
Indent: LongInt;
NewSrc: string;
i: Integer;
SemMode: TSemSelectionMode;
SemAction: TSemCopyPasteAction;
begin
if Assigned(Manager) then begin
// call handlers
i:=Manager.FHandlers[semhtCopyPaste].Count;
while Manager.FHandlers[semhtCopyPaste].NextDownIndex(i) do begin
SemMode:=TSemSelectionMode(AMode);
SemAction:=TSemCopyPasteAction(AnAction);
TSemCopyPasteEvent(Manager.FHandlers[semhtCopyPaste][i])(Self,AText,
SemMode,ALogStartPos,SemAction);
AMode:=TSynSelectionMode(SemMode);
AnAction:=TSynCopyPasteAction(SemAction);
if AnAction=scaAbort then exit;
end;
end;
if AMode<>smNormal then exit;
if SyncroLockCount > 0 then exit;
if not CodeToolsOpts.IndentOnPaste then exit;
@ -7920,10 +7944,13 @@ end;
constructor TSourceEditorManagerBase.Create(AOwner: TComponent);
var
i: TsemChangeReason;
h: TSrcEditMangerHandlerType;
begin
FUpdateFlags := [];
for i := low(TsemChangeReason) to high(TsemChangeReason) do
FChangeNotifyLists[i] := TMethodList.Create;
for h:=low(FHandlers) to high(FHandlers) do
FHandlers[h] := TMethodList.Create;
SrcEditorIntf.SourceEditorManagerIntf := Self;
FSourceWindowList := TFPList.Create;
FSourceWindowByFocusList := TFPList.Create;
@ -7938,6 +7965,7 @@ destructor TSourceEditorManagerBase.Destroy;
var
i: integer;
cr: TsemChangeReason;
h: TSrcEditMangerHandlerType;
begin
for i:=MarklingProducerCount-1 downto 0 do
MarklingProducers[i].Free;
@ -7950,7 +7978,9 @@ begin
FreeAndNil(FSourceWindowList);
FreeAndNil(FSourceWindowByFocusList);
for cr := low(TsemChangeReason) to high(TsemChangeReason) do
FChangeNotifyLists[cr].Free;;
FreeAndNil(FChangeNotifyLists[cr]);
for h:=low(FHandlers) to high(FHandlers) do
FreeAndNil(FHandlers[h]);
inherited Destroy;
end;
@ -7966,6 +7996,18 @@ begin
FChangeNotifyLists[AReason].Remove(TMethod(AHandler));
end;
procedure TSourceEditorManagerBase.RegisterCopyPasteEvent(
AHandler: TSemCopyPasteEvent);
begin
FHandlers[semhtCopyPaste].Add(TMethod(AHandler));
end;
procedure TSourceEditorManagerBase.UnRegisterCopyPasteEvent(
AHandler: TSemCopyPasteEvent);
begin
FHandlers[semhtCopyPaste].Remove(TMethod(AHandler));
end;
function TSourceEditorManagerBase.MarklingProducerCount: integer;
begin
Result:=fProducers.Count;

View File

@ -253,6 +253,23 @@ type
semEditorStatus // any status change of the editor (Caret, Selection, topline, ...)
);
TSemSelectionMode = (
semsmNormal,
semsmLine,
semsmColumn,
semsmCurrent);
TSemCopyPasteAction = (
semcaContinue, // normal paste with specials like folding
semcaPlainText, // paste as normal text, ignore folding and other specials
semcaAbort // cancel, use this if you handled the paste yourself
);
// ToDo: use the right clipboard
TSemCopyPasteEvent = procedure(Sender: TSourceEditorInterface;
var AText: String; var AMode: TSemSelectionMode; ALogStartPos: TPoint;
var AnAction: TSemCopyPasteAction) of object;
{ TSourceEditorManagerInterface }
TSourceEditorManagerInterface = class(TComponent)
protected
function GetActiveSourceWindow: TSourceEditorWindowInterface; virtual; abstract;
@ -312,6 +329,8 @@ type
public
procedure RegisterChangeEvent(AReason: TsemChangeReason; AHandler: TNotifyEvent); virtual; abstract;
procedure UnRegisterChangeEvent(AReason: TsemChangeReason; AHandler: TNotifyEvent); virtual; abstract;
procedure RegisterCopyPasteEvent(AHandler: TSemCopyPasteEvent); virtual; abstract;
procedure UnRegisterCopyPasteEvent(AHandler: TSemCopyPasteEvent); virtual; abstract;
public
// source marklings
function MarklingProducerCount: integer; virtual; abstract;