From 4d145caa9f9f8e43e3b2086566e5bee79319e0dc Mon Sep 17 00:00:00 2001 From: juha Date: Thu, 22 Jan 2015 23:03:35 +0000 Subject: [PATCH] IDE: Refactor TPathEditorButton more. Copy values between dialog and edit here instead of clients' code. git-svn-id: trunk@47491 - --- debugger/frames/debugger_general_options.pas | 3 +- ide/frames/compiler_path_options.pas | 88 ++------------- ide/patheditordlg.pas | 16 ++- .../frames/package_integration_options.pas | 80 +++++-------- packager/frames/package_usage_options.pas | 105 +++++------------- 5 files changed, 83 insertions(+), 209 deletions(-) diff --git a/debugger/frames/debugger_general_options.pas b/debugger/frames/debugger_general_options.pas index 75141ab6f0..083f82dcfe 100644 --- a/debugger/frames/debugger_general_options.pas +++ b/debugger/frames/debugger_general_options.pas @@ -320,8 +320,7 @@ begin Result := lisGeneral; end; -procedure TDebuggerGeneralOptionsFrame.Setup( - ADialog: TAbstractOptionsEditorDialog); +procedure TDebuggerGeneralOptionsFrame.Setup(ADialog: TAbstractOptionsEditorDialog); begin gbDebuggerType.Caption := dlgDebugType; gbAdditionalSearchPath.Caption := lisDebugOptionsFrmAdditionalSearchPath; diff --git a/ide/frames/compiler_path_options.pas b/ide/frames/compiler_path_options.pas index ce5a519648..0b479d62ec 100644 --- a/ide/frames/compiler_path_options.pas +++ b/ide/frames/compiler_path_options.pas @@ -53,7 +53,7 @@ type out SrcPathChanged: boolean): boolean; procedure FileBrowseBtnClick(Sender: TObject); procedure PathEditBtnClick(Sender: TObject); - procedure PathEditBtnExecuted(Sender: TObject); + function PathEditBtnExecuted(Context: String; var NewPath: String): Boolean; procedure DoShowOptions(Sender: TObject); procedure DoCheck(Sender: TObject); procedure DoImport(Sender: TObject); @@ -429,87 +429,18 @@ begin end; procedure TCompilerPathOptionsFrame.PathEditBtnClick(Sender: TObject); -var - AButton: TPathEditorButton; - OldPath: string; begin if Sender is TPathEditorButton then - begin - AButton := TPathEditorButton(Sender); - if AButton = OtherUnitsPathEditBtn then - OldPath := OtherUnitsEdit.Text - else - if AButton = IncludeFilesPathEditBtn then - OldPath := IncludeFilesEdit.Text - else - if AButton = OtherSourcesPathEditBtn then - OldPath := OtherSourcesEdit.Text - else - if AButton = LibrariesPathEditBtn then - OldPath := LibrariesEdit.Text - else - if AButton = DebugPathEditBtn then - OldPath := DebugPathEdit.Text - else - Exit; - AButton.CurrentPathEditor.BaseDirectory := FCompilerOpts.BaseDirectory; - AButton.CurrentPathEditor.Path := OldPath; - end; + TPathEditorButton(Sender).CurrentPathEditor.BaseDirectory := FCompilerOpts.BaseDirectory; end; -procedure TCompilerPathOptionsFrame.PathEditBtnExecuted(Sender: TObject); - - function CheckPath(const Context, NewPath: string): boolean; - var - ExpandedPath: string; - BaseDir: string; - begin - BaseDir := FCompilerOpts.BaseDirectory; - ExpandedPath := TrimSearchPath(NewPath, BaseDir, true); - Result := CheckSearchPath(Context, ExpandedPath, ccomlHints); - end; - +function TCompilerPathOptionsFrame.PathEditBtnExecuted(Context: String; var NewPath: String): Boolean; var - AButton: TPathEditorButton; - NewPath: string; + ExpandedPath: string; begin - if Sender is TPathEditorButton then - begin - AButton := TPathEditorButton(Sender); - if AButton.CurrentPathEditor.ModalResult <> mrOk then - Exit; - NewPath := AButton.CurrentPathEditor.Path; - NewPath := FCompilerOpts.ShortenPath(NewPath, False); - if AButton = OtherUnitsPathEditBtn then - begin - if CheckPath(OtherUnitsLabel.Caption, NewPath) then - OtherUnitsEdit.Text := NewPath; - end - else - if AButton = IncludeFilesPathEditBtn then - begin - if CheckPath(IncludeFilesLabel.Caption, NewPath) then - IncludeFilesEdit.Text := NewPath; - end - else - if AButton = OtherSourcesPathEditBtn then - begin - if CheckPath(OtherSourcesLabel.Caption, NewPath) then - OtherSourcesEdit.Text := NewPath; - end - else - if AButton = LibrariesPathEditBtn then - begin - if CheckPath(LibrariesLabel.Caption, NewPath) then - LibrariesEdit.Text := NewPath; - end - else - if AButton = DebugPathEditBtn then - begin - if CheckPath(DebugPathLabel.Caption, NewPath) then - DebugPathEdit.Text := NewPath; - end; - end; + NewPath := FCompilerOpts.ShortenPath(NewPath, False); + ExpandedPath := TrimSearchPath(NewPath, FCompilerOpts.BaseDirectory, true); + Result := CheckSearchPath(Context, ExpandedPath, ccomlHints); end; procedure TCompilerPathOptionsFrame.FileBrowseBtnClick(Sender: TObject); @@ -572,6 +503,7 @@ begin AnchorParallel(akRight, 0, Self); AutoSize := True; AssociatedEdit := OtherUnitsEdit; + ContextCaption := OtherUnitsLabel.Caption; Templates:='$(LazarusDir)/lcl/units/$(TargetCPU)-$(TargetOS)' + ';$(LazarusDir)/lcl/units/$(TargetCPU)-$(TargetOS)/$(LCLWidgetType)' + ';$(LazarusDir)/components/codetools/units/$(TargetCPU)-$(TargetOS)' + @@ -599,6 +531,7 @@ begin AnchorParallel(akRight, 0, Self); AutoSize := True; AssociatedEdit := IncludeFilesEdit; + ContextCaption := IncludeFilesLabel.Caption; Templates := 'include;inc'; OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; @@ -622,6 +555,7 @@ begin AnchorParallel(akRight, 0, Self); AutoSize := True; AssociatedEdit := OtherSourcesEdit; + ContextCaption := OtherSourcesLabel.Caption; Templates := '$(LazarusDir)/lcl' + ';$(LazarusDir)/lcl/interfaces/$(LCLWidgetType)' + ';$(LazarusDir)/components/synedit' + @@ -648,6 +582,7 @@ begin AnchorParallel(akRight, 0, Self); AutoSize := True; AssociatedEdit := LibrariesEdit; + ContextCaption := LibrariesLabel.Caption; Templates := '/usr/X11R6/lib;/sw/lib'; OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; @@ -693,6 +628,7 @@ begin AnchorParallel(akRight, 0, Self); AutoSize := True; AssociatedEdit := DebugPathEdit; + ContextCaption := DebugPathLabel.Caption; Templates := '$(LazarusDir)/lcl/include' + ';$(LazarusDir)/lcl/interfaces/$(LCLWidgetType)' + ';$(LazarusDir)/include'; diff --git a/ide/patheditordlg.pas b/ide/patheditordlg.pas index aa8e5df173..4190efd5f1 100644 --- a/ide/patheditordlg.pas +++ b/ide/patheditordlg.pas @@ -100,7 +100,7 @@ type property Templates: string read GetTemplates write SetTemplates; end; - TOnPathEditorExecuted = TNotifyEvent; + TOnPathEditorExecuted = function (Context: String; var NewPath: String): Boolean of object; { TPathEditorButton } @@ -108,6 +108,7 @@ type private FCurrentPathEditor: TPathEditorDialog; FAssociatedEdit: TCustomEdit; + FContextCaption: String; FTemplates: String; FOnExecuted: TOnPathEditorExecuted; protected @@ -116,6 +117,7 @@ type procedure Click; override; property CurrentPathEditor: TPathEditorDialog read FCurrentPathEditor; property AssociatedEdit: TCustomEdit read FAssociatedEdit write FAssociatedEdit; + property ContextCaption: String read FContextCaption write FContextCaption; property Templates: String read FTemplates write FTemplates; property OnExecuted: TOnPathEditorExecuted read FOnExecuted write FOnExecuted; end; @@ -593,6 +595,7 @@ begin try inherited Click; FCurrentPathEditor.Templates := SetDirSeparators(FTemplates); + FCurrentPathEditor.Path := AssociatedEdit.Text; FCurrentPathEditor.ShowModal; DoOnPathEditorExecuted; finally @@ -601,8 +604,17 @@ begin end; procedure TPathEditorButton.DoOnPathEditorExecuted; +var + Ok: Boolean; + NewPath: String; begin - if Assigned(OnExecuted) then OnExecuted(Self); + NewPath := FCurrentPathEditor.Path; + Ok := (FCurrentPathEditor.ModalResult = mrOk) and (AssociatedEdit.Text <> NewPath); + if Ok and Assigned(OnExecuted) then + Ok := OnExecuted(ContextCaption, NewPath); + // Assign value only if old <> new and OnExecuted allows it. + if Ok then + AssociatedEdit.Text := NewPath; end; end. diff --git a/packager/frames/package_integration_options.pas b/packager/frames/package_integration_options.pas index fb07dd57d3..db30546e87 100644 --- a/packager/frames/package_integration_options.pas +++ b/packager/frames/package_integration_options.pas @@ -33,8 +33,7 @@ type FPDocPathButton: TPathEditorButton; FStoredPkgType: TLazPackageType; function GetSelectedPkgType: TLazPackageType; - procedure PathEditBtnClick(Sender: TObject); - procedure PathEditBtnExecuted(Sender: TObject); + function PathEditBtnExecuted(Context: String; var NewPath: String): Boolean; procedure SetSelectedPkgType(PkgType: TLazPackageType); function ShowMsgPackageTypeMustBeDesign: boolean; function GetFPDocPkgNameEditValue: string; @@ -83,11 +82,6 @@ begin FPDocPackageNameEdit.Text:=GetFPDocPkgNameEditValue; end; -procedure TPackageIntegrationOptionsFrame.PathEditBtnClick(Sender: TObject); -begin - (Sender as TPathEditorButton).CurrentPathEditor.Path := FPDocSearchPathsEdit.Text; -end; - function TPackageIntegrationOptionsFrame.GetSelectedPkgType: TLazPackageType; begin if RunTimeOnlyRadioButton.Checked then @@ -100,59 +94,44 @@ begin Result:=lptRunAndDesignTime; end; -procedure TPackageIntegrationOptionsFrame.PathEditBtnExecuted(Sender: TObject); +function TPackageIntegrationOptionsFrame.PathEditBtnExecuted(Context: String; + var NewPath: String): Boolean; var - AButton: TPathEditorButton absolute Sender; - NewPath: string; - OldPath: string; CurDir: string; - StartPos: integer; + StartPos, OldStartPos: integer; DlgResult: TModalResult; - OldStartPos: longint; begin - if AButton.CurrentPathEditor.ModalResult <> mrOk then - Exit; - NewPath := AButton.CurrentPathEditor.Path; - OldPath := FPDocSearchPathsEdit.Text; - if OldPath <> NewPath then - begin - // check NewPath - StartPos := 1; - repeat - OldStartPos := StartPos; - CurDir := GetNextDirectoryInSearchPath(NewPath, StartPos); - if CurDir <> '' then + // check NewPath + StartPos := 1; + repeat + OldStartPos := StartPos; + CurDir := GetNextDirectoryInSearchPath(NewPath, StartPos); + if CurDir <> '' then + begin + IDEMacros.SubstituteMacros(CurDir); + FLazPackage.LongenFilename(CurDir); + if not DirPathExists(CurDir) then begin - IDEMacros.SubstituteMacros(CurDir); - FLazPackage.LongenFilename(CurDir); - if not DirPathExists(CurDir) then - begin - DlgResult := QuestionDlg(lisEnvOptDlgDirectoryNotFound, - Format(lisDirectoryNotFound, [CurDir]), - mtError, [mrIgnore, mrYes, lisRemoveFromSearchPath, mrCancel], 0); - case DlgResult of - mrIgnore: ; - mrYes: - begin - // remove directory from search path - NewPath := copy(NewPath, 1, OldStartPos - 1) + - copy(NewPath, StartPos, length(NewPath)); - StartPos := OldStartPos; - end; - else - // undo - NewPath := OldPath; - break; + DlgResult := QuestionDlg(lisEnvOptDlgDirectoryNotFound, + Format(lisDirectoryNotFound, [CurDir]), + mtError, [mrIgnore, mrYes, lisRemoveFromSearchPath, mrCancel], 0); + case DlgResult of + mrIgnore: ; + mrYes: + begin // remove directory from search path + NewPath := copy(NewPath,1,OldStartPos-1) + copy(NewPath,StartPos,length(NewPath)); + StartPos := OldStartPos; end; + else // undo + Exit(False); end; end; - until StartPos > length(NewPath); - end; - FPDocSearchPathsEdit.Text := NewPath; + end; + until StartPos > length(NewPath); + Result := True; end; -procedure TPackageIntegrationOptionsFrame.SetSelectedPkgType( - PkgType: TLazPackageType); +procedure TPackageIntegrationOptionsFrame.SetSelectedPkgType(PkgType: TLazPackageType); begin case PkgType of lptRunTime: RunTimeRadioButton.Checked:=true; @@ -202,7 +181,6 @@ begin AnchorParallel(akTop, 0, FPDocSearchPathsEdit); AnchorParallel(akBottom, 0, FPDocSearchPathsEdit); AssociatedEdit := FPDocSearchPathsEdit; - OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; Parent := DocGroupBox; end; diff --git a/packager/frames/package_usage_options.pas b/packager/frames/package_usage_options.pas index 7174111e41..b8331677bc 100644 --- a/packager/frames/package_usage_options.pas +++ b/packager/frames/package_usage_options.pas @@ -36,9 +36,7 @@ type ObjectPathButton: TPathEditorButton; LibraryPathButton: TPathEditorButton; FLazPackage: TLazPackage; - procedure PathEditBtnClick(Sender: TObject); - procedure PathEditBtnExecuted(Sender: TObject); - function GetEditForPathButton(AButton: TPathEditorButton): TEdit; + function PathEditBtnExecuted(Context: String; var NewPath: String): Boolean; public function GetTitle: string; override; procedure Setup(ADialog: TAbstractOptionsEditorDialog); override; @@ -53,85 +51,40 @@ implementation { TPackageUsageOptionsFrame } -procedure TPackageUsageOptionsFrame.PathEditBtnClick(Sender: TObject); +function TPackageUsageOptionsFrame.PathEditBtnExecuted(Context: String; var NewPath: String): Boolean; var - AButton: TPathEditorButton; - AnEdit: TEdit; -begin - if not (Sender is TPathEditorButton) then exit; - AButton := TPathEditorButton(Sender); - AnEdit := GetEditForPathButton(AButton); - AButton.CurrentPathEditor.Path := AnEdit.Text; -end; - -procedure TPackageUsageOptionsFrame.PathEditBtnExecuted(Sender: TObject); -var - AButton: TPathEditorButton; - NewPath: string; - AnEdit: TEdit; - OldPath: string; CurDir: string; - StartPos: integer; + StartPos, OldStartPos: integer; DlgResult: TModalResult; - OldStartPos: longint; begin - if not (Sender is TPathEditorButton) then - exit; - AButton := TPathEditorButton(Sender); - if AButton.CurrentPathEditor.ModalResult <> mrOk then - exit; - NewPath := AButton.CurrentPathEditor.Path; - AnEdit := GetEditForPathButton(AButton); - OldPath := AnEdit.Text; - if OldPath <> NewPath then - begin - // check NewPath - StartPos := 1; - repeat - OldStartPos := StartPos; - CurDir := GetNextDirectoryInSearchPath(NewPath, StartPos); - if CurDir <> '' then + // check NewPath + StartPos := 1; + repeat + OldStartPos := StartPos; + CurDir := GetNextDirectoryInSearchPath(NewPath, StartPos); + if CurDir <> '' then + begin + IDEMacros.SubstituteMacros(CurDir); + FLazPackage.LongenFilename(CurDir); + if not DirPathExists(CurDir) then begin - IDEMacros.SubstituteMacros(CurDir); - FLazPackage.LongenFilename(CurDir); - if not DirPathExists(CurDir) then - begin - DlgResult := QuestionDlg(lisEnvOptDlgDirectoryNotFound, - Format(lisDirectoryNotFound, [CurDir]), - mtError, [mrIgnore, mrYes, lisRemoveFromSearchPath, mrCancel], 0); - case DlgResult of - mrIgnore: ; - mrYes: - begin - // remove directory from search path - NewPath := copy(NewPath,1,OldStartPos-1) + copy(NewPath,StartPos,length(NewPath)); - StartPos := OldStartPos; - end; - else - // undo - NewPath := OldPath; - break; + DlgResult := QuestionDlg(lisEnvOptDlgDirectoryNotFound, + Format(lisDirectoryNotFound, [CurDir]), + mtError, [mrIgnore, mrYes, lisRemoveFromSearchPath, mrCancel], 0); + case DlgResult of + mrIgnore: ; + mrYes: + begin // remove directory from search path + NewPath := copy(NewPath,1,OldStartPos-1) + copy(NewPath,StartPos,length(NewPath)); + StartPos := OldStartPos; end; + else // undo + Exit(False); end; end; - until StartPos > length(NewPath); - end; - AnEdit.Text := NewPath; -end; - -function TPackageUsageOptionsFrame.GetEditForPathButton( - AButton: TPathEditorButton): TEdit; -begin - if AButton = UnitPathButton then - Result := UnitPathEdit - else if AButton = IncludePathButton then - Result := IncludePathEdit - else if AButton = ObjectPathButton then - Result := ObjectPathEdit - else if AButton = LibraryPathButton then - Result := LibraryPathEdit - else - Result := nil; + end; + until StartPos > length(NewPath); + Result := True; end; function TPackageUsageOptionsFrame.GetTitle: string; @@ -168,7 +121,6 @@ begin ';$(LazarusDir)/components/codetools/units/$(TargetCPU)-$(TargetOS)' + ';$(LazarusDir)/components/custom' + ';$(LazarusDir)/packager/units/$(TargetCPU)-$(TargetOS)'; - OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; end; UnitPathEdit.AnchorToNeighbour(akRight,0,UnitPathButton); @@ -186,7 +138,6 @@ begin AnchorParallel(akBottom, 0, IncludePathEdit); AssociatedEdit := IncludePathEdit; Templates := 'include'; - OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; end; IncludePathEdit.AnchorToNeighbour(akRight,0,IncludePathButton); @@ -204,7 +155,6 @@ begin AnchorParallel(akBottom, 0, ObjectPathEdit); AssociatedEdit := ObjectPathEdit; Templates := 'objects'; - OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; end; ObjectPathEdit.AnchorToNeighbour(akRight,0,ObjectPathButton); @@ -221,7 +171,6 @@ begin AnchorParallel(akTop, 0, LibraryPathEdit); AnchorParallel(akBottom, 0, LibraryPathEdit); AssociatedEdit := LibraryPathEdit; - OnClick := @PathEditBtnClick; OnExecuted := @PathEditBtnExecuted; end; LibraryPathEdit.AnchorToNeighbour(akRight,0,LibraryPathButton);