isolated proj & doc MRU classes, item insertion by observation

This commit is contained in:
Basile Burg 2015-03-12 10:37:21 +01:00
parent e7611457b6
commit 557d1b77f5
7 changed files with 231 additions and 132 deletions

View File

@ -132,7 +132,7 @@
<PackageName Value="LCL"/>
</Item6>
</RequiredPackages>
<Units Count="37">
<Units Count="38">
<Unit0>
<Filename Value="coedit.lpr"/>
<IsPartOfProject Value="True"/>
@ -147,6 +147,7 @@
<Unit2>
<Filename Value="..\src\ce_common.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="ce_common"/>
</Unit2>
<Unit3>
<Filename Value="..\src\ce_d2syn.pas"/>
@ -339,7 +340,6 @@
<ComponentName Value="CEShortcutEditor"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Frame"/>
<UnitName Value="ce_shortcutseditor"/>
</Unit35>
<Unit36>
<Filename Value="..\src\ce_symlist.pas"/>
@ -349,6 +349,11 @@
<ResourceBaseClass Value="Form"/>
<UnitName Value="ce_symlist"/>
</Unit36>
<Unit37>
<Filename Value="..\src\ce_mru.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="ce_mru"/>
</Unit37>
</Units>
</ProjectOptions>
<CompilerOptions>

View File

@ -9,7 +9,7 @@ uses
Interfaces, Forms, lazcontrols, runtimetypeinfocontrols, ce_observer,
ce_libman, ce_tools, ce_dcd, ce_main, ce_writableComponent, ce_options,
ce_symstring, ce_staticmacro, ce_inspectors, LResources, ce_editoroptions,
ce_dockoptions, ce_shortcutseditor;
ce_dockoptions, ce_shortcutseditor, ce_mru;
{$R *.res}

View File

@ -42,38 +42,6 @@ type
{$ENDIF}
end;
(**
* MRU list for strings
*)
TMRUList = class(TStringList)
private
fMaxCount: Integer;
fObj: TObject;
protected
fChecking: boolean;
procedure clearOutOfRange;
procedure setMaxCount(aValue: Integer);
function checkItem(const S: string): boolean; virtual;
procedure Put(Index: Integer; const S: string); override;
procedure InsertItem(Index: Integer; const S: string); override;
published
property maxCount: Integer read fMaxCount write setMaxCount;
public
constructor Create;
procedure Insert(Index: Integer; const S: string); override;
property objectTag: TObject read fObj write fObj;
end;
(**
* MRU list for filenames
*)
TMRUFileList = class(TMRUList)
protected
function checkItem(const S: string): boolean; override;
public
procedure assign(src: TPersistent); override;
end;
(**
* TProcess with assign() 'overriden'.
*)
@ -318,81 +286,6 @@ begin
WriteListEnd;
end;
constructor TMRUList.Create;
begin
fMaxCount := 10;
end;
procedure TMRUList.clearOutOfRange;
begin
while Count > fMaxCount do
delete(Count-1);
end;
procedure TMRUList.setMaxCount(aValue: Integer);
begin
if aValue < 0 then
aValue := 0;
if fMaxCount = aValue then
exit;
fMaxCount := aValue;
clearOutOfRange;
end;
function TMRUList.checkItem(const S: string): boolean;
var
i: NativeInt;
begin
i := indexOf(S);
if i = -1 then
exit(true);
if i = 0 then
exit(false);
if Count < 2 then
exit(false);
exchange(i, i-1);
exit( false);
end;
procedure TMRUList.Put(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TMRUList.InsertItem(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TMRUList.Insert(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TMRUFileList.assign(src: TPersistent);
var
i: Integer;
begin
inherited;
for i := Count-1 downto 0 do
if not fileExists(Strings[i]) then
Delete(i);
end;
function TMRUFileList.checkItem(const S: string): boolean;
begin
exit( inherited checkItem(S) and fileExists(S));
end;
procedure saveCompToTxtFile(const aComp: TComponent; const aFilename: string);
var
str1, str2: TMemoryStream;
@ -951,7 +844,6 @@ end;
initialization
RegisterClasses([TMRUList, TMRUFileList]);
dExtList := TStringList.Create;
dExtList.AddStrings(['.d', '.D', '.di', '.DI', '.Di', '.dI']);
finalization

View File

@ -12,7 +12,7 @@ uses
ce_widget, ce_messages, ce_interfaces, ce_editor, ce_projinspect, ce_projconf,
ce_search, ce_miniexplorer, ce_libman, ce_libmaneditor, ce_todolist, ce_observer,
ce_toolseditor, ce_procinput, ce_optionseditor,{$IFDEF WIN32} ce_cdbcmd,{$ENDIF}
ce_symlist;
ce_symlist, ce_mru;
type
@ -187,8 +187,8 @@ type
fScCollectCount: Integer;
fUpdateCount: NativeInt;
fProject: TCEProject;
fProjMru: TMruFileList;
fFileMru: TMruFileList;
fProjMru: TCEMRUProjectList;
fFileMru: TCEMRUDocumentList;
fWidgList: TCEWidgetList;
fMesgWidg: TCEMessagesWidget;
fEditWidg: TCEEditorWidget;
@ -398,8 +398,8 @@ end;
procedure TCEMainForm.InitMRUs;
begin
fProjMru := TMruFileList.Create;
fFileMru := TMruFileList.Create;
fProjMru := TCEMRUProjectList.Create;
fFileMru := TCEMRUDocumentList.Create;
fProjMru.objectTag := mnuItemMruProj;
fFileMru.objectTag := mnuItemMruFile;
fProjMru.OnChange := @mruChange;
@ -828,14 +828,14 @@ end;
procedure TCEMainForm.mruChange(Sender: TObject);
var
srcLst: TMruFileList;
srcLst: TCEMruFileList;
trgMnu: TMenuItem;
itm: TMenuItem;
fname: string;
clickTrg: TNotifyEvent;
i: NativeInt;
begin
srcLst := TMruFileList(Sender);
srcLst := TCEMruFileList(Sender);
if srcLst = nil then exit;
trgMnu := TMenuItem(srcLst.objectTag);
if trgMnu = nil then exit;
@ -874,9 +874,9 @@ end;
procedure TCEMainForm.mruClearClick(Sender: TObject);
var
srcLst: TMruFileList;
srcLst: TCEMruFileList;
begin
srcLst := TMruFileList(TmenuItem(Sender).Tag);
srcLst := TCEMruFileList(TmenuItem(Sender).Tag);
if srcLst = nil then exit;
//
srcLst.Clear;
@ -1020,7 +1020,6 @@ end;
procedure TCEMainForm.openFile(const aFilename: string);
begin
fMultidoc.openDocument(aFilename);
fFileMru.Insert(0, aFilename);
end;
procedure TCEMainForm.saveFile(aDocument: TCESynMemo);
@ -1093,7 +1092,6 @@ begin
Filter := DdiagFilter;
if execute then
fDoc.saveToFile(filename);
fFileMru.Insert(0, filename);
finally
free;
end;
@ -1610,7 +1608,6 @@ procedure TCEMainForm.saveProjAs(const aFilename: string);
begin
fProject.fileName := aFilename;
fProject.saveToFile(fProject.fileName);
fProjMru.Insert(0,fProject.fileName);
end;
procedure TCEMainForm.openProj(const aFilename: string);
@ -1618,7 +1615,6 @@ begin
closeProj;
newProj;
fProject.loadFromFile(aFilename);
fProjMru.Insert(0,aFilename);
end;
procedure TCEMainForm.mruProjItemClick(Sender: TObject);

205
src/ce_mru.pas Normal file
View File

@ -0,0 +1,205 @@
unit ce_mru;
{$I ce_defines.inc}
interface
uses
Classes, SysUtils, ce_interfaces, ce_observer,
ce_project, ce_synmemo;
type
(**
* 'Most Recently Used' list for strings.
*)
TCEMruList = class(TStringList)
private
fMaxCount: Integer;
fObj: TObject;
protected
fChecking: boolean;
procedure clearOutOfRange;
procedure setMaxCount(aValue: Integer);
function checkItem(const S: string): boolean; virtual;
procedure Put(Index: Integer; const S: string); override;
procedure InsertItem(Index: Integer; const S: string); override;
published
property maxCount: Integer read fMaxCount write setMaxCount;
public
constructor Create; virtual;
procedure Insert(Index: Integer; const S: string); override;
property objectTag: TObject read fObj write fObj;
end;
(**
* MRU list for filenames.
*)
TCEMRUFileList = class(TCEMruList)
protected
function checkItem(const S: string): boolean; override;
public
procedure assign(src: TPersistent); override;
end;
(**
* MRU list for D/text files.
* Insertion is automatic (ICEMultiDocObserver).
*)
TCEMRUDocumentList = class(TCEMRUFileList, ICEMultiDocObserver)
private
procedure docNew(aDoc: TCESynMemo);
procedure docFocused(aDoc: TCESynMemo);
procedure docChanged(aDoc: TCESynMemo);
procedure docClosing(aDoc: TCESynMemo);
public
constructor create; override;
end;
(**
* MRU list for the ceodit projects.
* Insertion is automatic (ICEProjectObserver).
*)
TCEMRUProjectList = class(TCEMRUFileList, ICEProjectObserver)
private
procedure projNew(aProject: TCEProject);
procedure projChanged(aProject: TCEProject);
procedure projClosing(aProject: TCEProject);
procedure projFocused(aProject: TCEProject);
procedure projCompiling(aProject: TCEProject);
public
constructor create; override;
end;
implementation
constructor TCEMruList.Create;
begin
fMaxCount := 10;
end;
procedure TCEMruList.clearOutOfRange;
begin
while Count > fMaxCount do
delete(Count-1);
end;
procedure TCEMruList.setMaxCount(aValue: Integer);
begin
if aValue < 0 then
aValue := 0;
if fMaxCount = aValue then
exit;
fMaxCount := aValue;
clearOutOfRange;
end;
function TCEMruList.checkItem(const S: string): boolean;
var
i: NativeInt;
begin
i := indexOf(S);
if i = -1 then
exit(true);
if i = 0 then
exit(false);
if Count < 2 then
exit(false);
exchange(i, i-1);
exit( false);
end;
procedure TCEMruList.Put(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TCEMruList.InsertItem(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TCEMruList.Insert(Index: Integer; const S: string);
begin
if not (checkItem(S)) then
exit;
inherited;
clearOutOfRange;
end;
procedure TCEMRUFileList.assign(src: TPersistent);
var
i: Integer;
begin
inherited;
for i := Count-1 downto 0 do
if not fileExists(Strings[i]) then
Delete(i);
end;
function TCEMRUFileList.checkItem(const S: string): boolean;
begin
exit( inherited checkItem(S) and fileExists(S));
end;
constructor TCEMRUDocumentList.create;
begin
inherited;
EntitiesConnector.addObserver(self);
end;
procedure TCEMRUDocumentList.docNew(aDoc: TCESynMemo);
begin
end;
procedure TCEMRUDocumentList.docFocused(aDoc: TCESynMemo);
begin
end;
procedure TCEMRUDocumentList.docChanged(aDoc: TCESynMemo);
begin
end;
procedure TCEMRUDocumentList.docClosing(aDoc: TCESynMemo);
begin
if FileExists(aDoc.fileName) and (aDoc.fileName <> aDoc.tempFilename) then
Insert(0, aDoc.fileName);
end;
constructor TCEMRUProjectList.create;
begin
inherited;
EntitiesConnector.addObserver(self);
end;
procedure TCEMRUProjectList.projNew(aProject: TCEProject);
begin
end;
procedure TCEMRUProjectList.projFocused(aProject: TCEProject);
begin
end;
procedure TCEMRUProjectList.projChanged(aProject: TCEProject);
begin
end;
procedure TCEMRUProjectList.projCompiling(aProject: TCEProject);
begin
end;
procedure TCEMRUProjectList.projClosing(aProject: TCEProject);
begin
if FileExists(aProject.fileName) then
Insert(0, aProject.fileName);
end;
initialization
RegisterClasses([TCEMRUList, TCEMRUFileList, TCEMRUProjectList, TCEMRUDocumentList]);
end.

View File

@ -6,7 +6,8 @@ interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
Menus, StdCtrls, ce_widget, process, ce_common, ce_interfaces, ce_observer;
Menus, StdCtrls, ce_widget, process, ce_common, ce_interfaces, ce_observer,
ce_mru;
type
TCEProcInputWidget = class(TCEWidget, ICEProcInputHandler)
@ -17,7 +18,7 @@ type
procedure txtInpKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
fMruPos: Integer;
fMru: TMRUList;
fMru: TCEMRUList;
fProc: TProcess;
procedure sendInput;
//
@ -44,7 +45,7 @@ uses
constructor TCEProcInputWidget.create(aOwner: TComponent);
begin
inherited;
fMru := TMRUList.Create;
fMru := TCEMRUList.Create;
fMru.maxCount := 25;
EntitiesConnector.addSingleService(self);
end;

View File

@ -6,8 +6,8 @@ interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
Menus, StdCtrls, actnList, Buttons, SynEdit, SynEditSearch, SynEditTypes, ce_common,
ce_widget, ce_synmemo, ce_interfaces, ce_observer, SynEditHighlighter;
Menus, StdCtrls, actnList, Buttons, SynEdit, SynEditSearch, SynEditTypes,
ce_common, ce_mru, ce_widget, ce_synmemo, ce_interfaces, ce_observer;
type
@ -38,7 +38,7 @@ type
fReplaceWth: string;
fActFindNext, fActReplaceNext: TAction;
fActReplaceAll: TAction;
fSearchMru, fReplaceMru: TMruList;
fSearchMru, fReplaceMru: TCEMruList;
fCancelAll: boolean;
fHasSearched: boolean;
fHasRestarted: boolean;
@ -92,8 +92,8 @@ begin
btnReplace.Action := fActReplaceNext;
btnReplaceAll.Action := fActReplaceAll;
//
fSearchMru := TMruList.Create;
fReplaceMru:= TMruList.Create;
fSearchMru := TCEMruList.Create;
fReplaceMru:= TCEMruList.Create;
//
EntitiesConnector.addObserver(self);
end;