diff --git a/.gitattributes b/.gitattributes index 13352cce7f..d9f7e7df44 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1290,6 +1290,10 @@ components/externhelp/languages/externhelpfrm.po svneol=native#text/plain components/externhelp/languages/externhelpfrm.pt_BR.po svneol=native#text/plain components/externhelp/languages/externhelpfrm.ru.po svneol=native#text/plain components/externhelp/languages/externhelpfrm.uk.po svneol=native#text/plain +components/favorites/README.txt svneol=native#text/plain +components/favorites/favorites.lpk svneol=native#text/plain +components/favorites/favorites.pas svneol=native#text/pascal +components/favorites/favorites_impl.pas svneol=native#text/pascal components/filebrowser/frmconfigfilebrowser.lfm svneol=native#text/plain components/filebrowser/frmconfigfilebrowser.pp svneol=native#text/plain components/filebrowser/frmfilebrowser.lfm svneol=native#text/plain @@ -7681,6 +7685,7 @@ packager/globallinks/editormacroscript-0.lpl svneol=native#text/pascal packager/globallinks/educationlaz-1.0.1.lpl svneol=native#text/plain packager/globallinks/exploreidemenu-0.lpl svneol=native#text/plain packager/globallinks/externhelp-1.3.lpl svneol=native#text/plain +packager/globallinks/favorites-0.1.1.lpl svneol=native#text/plain packager/globallinks/fcl-1.0.1.lpl svneol=native#text/plain packager/globallinks/filefindlaz-1.0.2.lpl svneol=native#text/plain packager/globallinks/fpcunitconsolerunner-1.lpl svneol=native#text/plain diff --git a/components/favorites/README.txt b/components/favorites/README.txt new file mode 100644 index 0000000000..c4eeb6c91f --- /dev/null +++ b/components/favorites/README.txt @@ -0,0 +1,6 @@ +IDE add-on for Favorites. +Adds favorite projects list into the drop-down menu of "Open" toolbar button. + +Author: Ondrej Pokorny + +Documentation: http://wiki.lazarus.freepascal.org/Favorites diff --git a/components/favorites/favorites.lpk b/components/favorites/favorites.lpk new file mode 100644 index 0000000000..a79de400ac --- /dev/null +++ b/components/favorites/favorites.lpk @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/components/favorites/favorites.pas b/components/favorites/favorites.pas new file mode 100644 index 0000000000..aaf815b70b --- /dev/null +++ b/components/favorites/favorites.pas @@ -0,0 +1,21 @@ +{ This file was automatically created by Lazarus. Do not edit! + This source is only used to compile and install the package. + } + +unit favorites; + +interface + +uses + favorites_impl, LazarusPackageIntf; + +implementation + +procedure Register; +begin + RegisterUnit('favorites_impl', @favorites_impl.Register); +end; + +initialization + RegisterPackage('favorites', @Register); +end. diff --git a/components/favorites/favorites_impl.pas b/components/favorites/favorites_impl.pas new file mode 100644 index 0000000000..2aa47fe97c --- /dev/null +++ b/components/favorites/favorites_impl.pas @@ -0,0 +1,282 @@ +{ + *************************************************************************** + * * + * This source is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This code is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * A copy of the GNU General Public License is available on the World * + * Wide Web at . You can also * + * obtain it by writing to the Free Software Foundation, * + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * * + *************************************************************************** + + Author: Ondrej Pokorny + + Abstract: + Adds favorite projects list into the drop-down menu of "Open" toolbar button. +} +unit favorites_impl; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils, ToolBarIntf, IDEImagesIntf, Graphics, PackageIntf, + Menus, LazIDEIntf, ProjectIntf, Laz2_XMLCfg, IDEOptionsIntf, + IDECommands, ComCtrls; + +type + TFavoritesHandler = class + private + FOldToolButtonClass: TIDEToolButtonClass; + FFavoriteProjects: TStringList; + FConfig: TXMLConfig; + + procedure AddToRecentProjectFiles(Sender: TObject; AFileName: string; + var AAllow: Boolean); + public + constructor Create; + destructor Destroy; override; + public + procedure LoadFromConfig; + procedure SaveToConfig; + function IsInFavoriteProjects(const aFileName: string): Boolean; + procedure AddToFavoriteProjects(const aFileName: string); + procedure RemoveFromFavoriteProjects(const aFileName: string); + end; + + TFileNameMenuItem = class(TMenuItem) + public + FileName: string; + end; + + TOpenFileFavToolButton = class(TIDEToolButton) + private + FOrigButton: TIDEToolButton; + FOrigOnPopup: TNotifyEvent; + + procedure RefreshMenu(Sender: TObject); + procedure mnuFavoriteFile(Sender: TObject); + procedure mnuAddRemoveActiveProject(Sender: TObject); + public + constructor Create(aOwner: TComponent); override; + procedure DoOnAdded; override; + end; + + +procedure Register; + +implementation + +var + FavHandler: TFavoritesHandler = nil; + +procedure Register; +begin + FavHandler := TFavoritesHandler.Create; +end; + +{ TOpenFileFavToolButton } + +constructor TOpenFileFavToolButton.Create(aOwner: TComponent); +begin + inherited Create(aOwner); + + if FavHandler.FOldToolButtonClass<>nil then + FOrigButton := FavHandler.FOldToolButtonClass.Create(Self) + else + FOrigButton := TIDEToolButton.Create(Self); +end; + +procedure TOpenFileFavToolButton.DoOnAdded; +begin + inherited DoOnAdded; + + FOrigButton.DoOnAdded; + + if FOrigButton.DropdownMenu<>nil then + DropdownMenu := FOrigButton.DropdownMenu + else + DropdownMenu := TPopupMenu.Create(Self); + + FOrigOnPopup := DropdownMenu.OnPopup; + DropdownMenu.OnPopup := @RefreshMenu; + Style := tbsDropDown; +end; + +procedure TOpenFileFavToolButton.mnuAddRemoveActiveProject(Sender: TObject); +var + xFileName: string; +begin + xFileName := (Sender as TFileNameMenuItem).FileName; + if FavHandler.IsInFavoriteProjects(xFileName) then + begin + FavHandler.RemoveFromFavoriteProjects(xFileName); + IDEEnvironmentOptions.AddToRecentProjectFiles(xFileName); + end else + begin + FavHandler.AddToFavoriteProjects(xFileName); + IDEEnvironmentOptions.RemoveFromRecentProjectFiles(xFileName); + end; +end; + +procedure TOpenFileFavToolButton.mnuFavoriteFile(Sender: TObject); +begin + LazarusIDE.DoOpenProjectFile((Sender as TFileNameMenuItem).FileName,[ofAddToRecent]); +end; + +procedure TOpenFileFavToolButton.RefreshMenu(Sender: TObject); +var + xM, xSep: TMenuItem; + xFavoriteFile: string; + xMI, xAddToFav: TFileNameMenuItem; + xProj: TLazProject; + xMIndex: Integer; +begin + if Assigned(FOrigOnPopup) then + FOrigOnPopup(Sender); + + xM := DropdownMenu.Items; + + xMIndex := 0; + for xFavoriteFile in FavHandler.FFavoriteProjects do + begin + xMI := TFileNameMenuItem.Create(Self); + xMI.FileName := xFavoriteFile; + xMI.Caption := xFavoriteFile; + xMI.OnClick := @mnuFavoriteFile; + xM.Insert(xMIndex, xMI); + Inc(xMIndex); + end; + + xProj := LazarusIDE.ActiveProject; + if (xProj<>nil) and FileExists(xProj.ProjectInfoFile) then + begin + xAddToFav := TFileNameMenuItem.Create(Self); + xAddToFav.FileName := xProj.ProjectInfoFile; + if not FavHandler.IsInFavoriteProjects(xProj.ProjectInfoFile) then + xAddToFav.Caption := Format('Add to favorites: %s', [xProj.ProjectInfoFile]) // To-Do: localize + else + xAddToFav.Caption := Format('Remove from favorites: %s', [xProj.ProjectInfoFile]); // To-Do: localize + xAddToFav.OnClick := @mnuAddRemoveActiveProject; + xM.Insert(xMIndex, xAddToFav); + Inc(xMIndex); + end; + + if xMIndex > 0 then + begin + xSep := TMenuItem.Create(Self); + xSep.Caption := '-'; + xM.Insert(xMIndex, xSep); + Inc(xMIndex); + end; +end; + +{ TFavoritesHandler } + +constructor TFavoritesHandler.Create; +var + I: Integer; + xToolButton: TIDEButtonCommand; +begin + IDEEnvironmentOptions.AddHandlerAddToRecentProjectFiles(@AddToRecentProjectFiles); + FFavoriteProjects := TStringList.Create; + FFavoriteProjects.Duplicates := dupIgnore; + FFavoriteProjects.CaseSensitive := False; + FFavoriteProjects.Sorted := True; + FConfig := TXMLConfig.Create(LazarusIDE.GetPrimaryConfigPath+'favorites.xml'); + LoadFromConfig; + + xToolButton := IDEToolButtonCategories.FindItemByCommand(ecOpen); + FOldToolButtonClass := xToolButton.ToolButtonClass; + xToolButton.ToolButtonClass := TOpenFileFavToolButton; + + for I := 0 to FFavoriteProjects.Count-1 do + IDEEnvironmentOptions.RemoveFromRecentProjectFiles(FFavoriteProjects[I]); +end; + +procedure TFavoritesHandler.AddToFavoriteProjects(const aFileName: string); +begin + FFavoriteProjects.Add(aFileName); +end; + +procedure TFavoritesHandler.AddToRecentProjectFiles(Sender: TObject; + AFileName: string; var AAllow: Boolean); +begin + if IsInFavoriteProjects(AFileName) then + AAllow := False; +end; + +destructor TFavoritesHandler.Destroy; +begin + SaveToConfig; + FFavoriteProjects.Free; + FConfig.Free; + + inherited Destroy; +end; + +function TFavoritesHandler.IsInFavoriteProjects(const aFileName: string + ): Boolean; +var + I: Integer; +begin + for I := 0 to FFavoriteProjects.Count-1 do + if SameFileName(aFileName, FFavoriteProjects[I]) then + Exit(True); + Result := False; +end; + +procedure TFavoritesHandler.LoadFromConfig; +var + I: Integer; + xItem: string; +begin + I := 1; + while True do + begin + xItem := FConfig.GetValue('projects/item'+IntToStr(I), ''); + if xItem = '' then + Break; + if FileExists(xItem) then + FFavoriteProjects.Add(xItem); + Inc(I); + end; +end; + +procedure TFavoritesHandler.RemoveFromFavoriteProjects(const aFileName: string); +var + xIndex: Integer; +begin + xIndex := FFavoriteProjects.IndexOf(aFileName); + if xIndex >= 0 then + FFavoriteProjects.Delete(xIndex); +end; + +procedure TFavoritesHandler.SaveToConfig; +var + I: Integer; + xItem: string; +begin + I := 1; + FConfig.DeletePath('projects'); + for xItem in FFavoriteProjects do + begin + FConfig.SetValue('projects/item'+IntToStr(I), xItem); + Inc(I); + end; +end; + +finalization + FreeAndNil(FavHandler); +end. + diff --git a/packager/globallinks/favorites-0.1.1.lpl b/packager/globallinks/favorites-0.1.1.lpl new file mode 100644 index 0000000000..487ff00f7e --- /dev/null +++ b/packager/globallinks/favorites-0.1.1.lpl @@ -0,0 +1 @@ +$(LazarusDir)/components/favorites/favorites.lpk