Extending the IDE/es

From Free Pascal wiki
Jump to: navigation, search

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)

Contents

Extendiendo el IDE

Visión general

El IDE de Lazarus soporta varios tipos de plugins:

Componentes 
Estos son los elementos que se encuentrar en la Paleta de Componentes. Por ejemplo, TButton (que se encuentra en la solapa Standard) que se utiliza para crear Buttons.
Component Editors 
Cuando haces doble click sobre un componente en el Diseñador, se invoka un editor de componente. El editor de componente puede además añadir un elemento extra al menú emergente (accedido cuando haces click en el pulsador derecho en un componente dentro del Diseñador).
Property Editors 
Cada fila en la sección de la rejilla (grid) del Inspector de Objetos utiliza un editor de propiedades que permite establecer o alterar el valor de su propiedad.
Experts 
Experts abarca todos los demás tipos de plugin. Pueden registrar nuevos elementos de menú, registrar atajos (short cuts), o extender otras características del IDE.

Hay dos maneras posibles de añadir tus propios plugins al IDE de Lazarus:

  1. Escribir un paquete, instalarlo y registrar tu plugin(s) en el procedimiento 'Register' procedure de una unit.
  2. Extender el cógigo de Lazarus, y enviar tu svn diff a la lista de correo de Lazarus.

Paquetes y Dependencias MakeFile

Los siguientes pasos son necesarios para añadir nuevas dependencias de paquete.

  • Crear/Actualizar el MakeFile para el paquete vía Editor de Paquete / Más ... / Crear Makefile.
  • Corregir todas las dependencias. Ver tools/updatemakefiles.lpi

Elementos del Menú

Puedes añadir nuevos elementos al menú, ocultar o mover los existentes y añadir sub menús y secciones. Puedes registrar TMainMenu/TPopupMenu para tus propios formularios de forma que otros paquetes puedan extender más tu formulario. La unidad MenuIntf del paquete IDEIntf contiene todos todos los registros para menús y algunos elementos menú estandar del propio IDE.

Añadiendo un elemento al menú

Un simple elemento de menú tal como 'View Object Inspector' se llama TIDEMenuCommand. Puedes crear uno con RegisterIDEMenuCommand, que tiene dos fomas con un montón de parámetros:

function RegisterIDEMenuCommand(Parent: TIDEMenuSection;
                                const Name, Caption: string;
                                const OnClickMethod: TNotifyEvent = nil;
                                const OnClickProc: TNotifyProcedure = nil;
                                const Command: TIDECommand = nil;
                                const ResourceName: String = ''
                                ): TIDEMenuCommand; overload;
function RegisterIDEMenuCommand(const Path, Name, Caption: string;
                                const OnClickMethod: TNotifyEvent = nil;
                                const OnClickProc: TNotifyProcedure = nil;
                                const Command: TIDECommand = nil;
                                const ResourceName: String = ''
                                ): TIDEMenuCommand; overload;

La diferencia entre los dos consiste solamente en como se especifica la sección padre del menú. Puedes dar la sección del menú directamente o bien indirectamente a través de su trayecto. Algunas secciones estandar las puedes encontrar en la unidad MenuIntf Por ejemplo, la sección mnuTools, la cual es el menú Tools de la barra principal del IDE. Contiene una secciónon itmSecondaryTools, que es la sección recomendada para utilidades de terceros.

Lo siguiente registra un nuevo mandato de menú con el nombre 'MyTool', el caption 'Arranca mi utilidad' y cuando es clickeada ejecuta el procedimiento StartMyTool:

procedure StartMyTool;
begin
  ...ejecutada cuando el elemento del menú es clickeado...
end;
 
procedure Register;
begin
  RegisterIDEMenuCommand(itmSecondaryTools, 'MyTool','Aranca mi utilidad',nil,@StartMyTool);
end;

If you want to call a method instead of a procedure use the OnClickMethod parameter.

Este elemento de menú no tiene un atajo. En caso de necesitar uno debes crear un TIDECommand y pasarlo en el parámetro Command. Por ejemplo:

uses ... IDECommands, MenuIntf;
...
var
  Key: TIDEShortCut;
  Cat: TIDECommandCategory;
  CmdMyTool: TIDECommand;
begin
  // register IDE shortcut and menu item
  Key := IDEShortCut(VK_UNKNOWN,[],VK_UNKNOWN,[]);
  Cat:=IDECommandList.FindCategoryByName(CommandCategoryToolMenuName);
  CmdMyTool := RegisterIDECommand(Cat,'Start my tool', 'Starts my tool to do some stuff', Key, nil, @StartMyTool);
  RegisterIDEMenuCommand(itmSecondaryTools, 'MyTool', 'Start my tool', nil, nil, CmdMyTool);
end;

Atajos (Shortcuts)

Todos los atajos (teclas que invocan alguna acción) se encuentran registradas en el IDECommandList. Este es un ejemplo sobre como encontrar todos los mandatos que reaccionan a la pulsación de Ctrl-D o la combinación de teclas pulsadas que comiencen por Ctrl-D:

uses
  Classes, SysUtils, LCLProc, IDECommands;
 
procedure ListCtrlD;
// Lista todos los idecommands con el atajo Ctrl-D
var
  i: integer;
  Cmd: TIDECommand;
  Commands: TFPList;
begin
  Commands:=IDECommandList.FindCommandsByShortCut(IDEShortCut(VK_D,[],VK_UNKNOWN,[])); // VK = Virtual Key
  try
    for i:=0 to Commands.Count-1 do begin
      Cmd:=TIDECommand(Commands[i]);
      writeln('Cmd: ',Cmd.Name,
        ' A=',dbgs(Cmd.ShortcutA.Shift1),'-',Cmd.ShortcutA.Key1,
        ',',dbgs(Cmd.ShortcutA.Shift2),'-',Cmd.ShortcutA.Key2,
        ' B=',dbgs(Cmd.ShortcutB.Shift1),'-',Cmd.ShortcutB.Key1,
        ',',dbgs(Cmd.ShortcutB.Shift2),'-',Cmd.ShortcutB.Key2
        );
    end;
  finally
    Commands.Free;
  end;
end;

Config files

Cargar/Salvar configuraciones

Todos los ficheros de configuración del IDE se encuentran almacenados en un directorio como ficheros xml. Los paquetes también también pueden añadir sus propios ficheros en dicho directorio. El primary config path se puede leer con:


uses LazIDEIntf;
...
  Directory := LazarusIDE.GetPrimaryConfigPath;

Los paquetes pueden crear su propios ficheros xml con:


uses
  ..., LCLProc, BaseIDEIntf, LazConfigStorage;
 
const
  Version = 1;
var
  Config: TConfigStorage;
  SomeValue: String;
  SomeInteger: Integer;
  SomeBoolean: Boolean;
  FileVersion: LongInt;
begin
  SomeValue:='Default';
  SomeInteger:=3;
  SomeBoolean:=true;
 
  // Salva configuraciones.
  try
    Config:=GetIDEConfigStorage('mysettings.xml',false);
    try
      // Almacena el número de versión de forma que las extensiones futuras puedan manejar ficheros de configuración antiguos.
      Config.SetDeleteValue('Path/To/The/Version',Version,0);
      // Almacena la variable de cadena "SomeValue".
      // Si SomeValue tiene el valor por defecto entonces la entrada no se almacena,
      // de forma que únicamente se almacena las diferencias al valor por defecto.
      // Así el xml se mantiene pequeño y su valor por defecto puede cambiar en el futuro.
      Config.SetDeleteValue('Path/To/Some/Value',SomeValue,'Default');
      Config.SetDeleteValue('Path/To/Some/Integer',SomeInteger,3);
      Config.SetDeleteValue('Path/To/Some/Boolean',SomeBoolean,true);
      // there are more overloads for SetDeleteValue, find out with Ctrl+Space
    finally
      Config.Free;
    end;
 
  except
    on E: Exception do begin
      DebugLn(['Saving mysettings.xml failed: ',E.Message]);
    end;
  end;
 
  // load settings
  try
    Config:=GetIDEConfigStorage('mysettings.xml',true);
    try
      // read the version of the config
      FileVersion:=Config.GetValue('Path/To/The/Version',0);
      // read a string variable "SomeValue". If the entry does not exist, use
      // the Default
      SomeValue:=Config.GetValue('Path/To/Some/Value','Default');
      SomeInteger:=Config.GetValue('Path/To/Some/Integer',3);
      SomeBoolean:=Config.GetValue('Path/To/Some/Boolean',true);
    finally
      Config.Free;
    end;
  except
    on E: Exception do begin
      DebugLn(['Loading mysettings.xml failed: ',E.Message]);
    end;
  end;
end;

Notes:

  • Every IDE plugin should use its own config file. Especially you must not use the standard IDE option files like editoroptions.xml or environmentoptions.xml.
  • GetIDEConfigStorage when loading checks if the file exists and if not it will copy the template from the secondary config directory.
  • Since 0.9.31 you can use this function to load a file outside the primary config directory.

Components

Extending existing components

Source: [1] It is straightforward to customise existing LCL components, e.g. if you want to add properties.

Add the properties you need with appropriate setter and getter methods and storage fields.

Then instantiate them 'by hand', giving an owner component (to handle later destruction), and set Parent and possibly Top and Left and any other defaults your component needs when initialized.

When you are satisfied that your customised component is debugged, add a RegisterComponent() call to stick it on the IDE Component Palette and save you having to do the setup code 'by hand' (look at any component's source to see how to do this).

Here is a simple example:

unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils, Forms, StdCtrls;
 
type
  TEdX = class(TEdit)
  private
    FNewIntProp: integer;
    FNewStrProp: string;
    procedure SetNewIntProp(AValue: integer);
    procedure SetNewStrProp(AValue: string);
  public
    constructor Create(theComponent: TComponent); override;
    property NewIntProp: integer read FNewIntProp write SetNewIntProp;
    property NewStrProp: string read FNewStrProp write SetNewStrProp;
  end;
 
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    edX: TEdX;
  public
  end;
 
var
  Form1: TForm1;
 
implementation
 
{ TForm1 }
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  edX:= TEdX.Create(Self);
  edX.Parent:= Self;
  edX.Caption:= IntToStr(edX.NewIntProp);
  Caption:= edX.NewStrProp;
end;
 
{$R *.lfm}
 
{ TEdX }
 
procedure TEdX.SetNewIntProp(AValue: integer);
begin
  if FNewIntProp=AValue then Exit;
  FNewIntProp:=AValue;
  // add custom code here
end;
 
procedure TEdX.SetNewStrProp(AValue: string);
begin
  if FNewStrProp=AValue then Exit;
  FNewStrProp:=AValue;
  // add custom code here
end;
 
constructor TEdX.Create(theComponent: TComponent);
begin
  inherited Create(theComponent);
  FNewStrProp:= 'new string property value';
  FNewIntProp:= 99;
end;
 
end.

Writing components

Main article: How To Write Lazarus Component

You can create new components via the package editor.

Create or open a package, click on add, click on New Component, fill in the items: Ancestor Type = TButton, New class name = TMyButton, palette page = Misc, Unit file name = mybutton.pas (this file will be created), unit name MyButton and click ok.

Giving a new component an icon for the component palette

Create an image file of the format .bmp, .xpm or .png with the same name as the component class. For example tmybutton.png and save it in the package source directory. The image can be created by any graphic program (e.g. gimp) and should be no bigger than 24x24 pixel. Then convert the image to a resource file with the lazres tool, which can be found in the lazarus/tools directory:

~/lazarus/tools/lazres mybutton.lrs tmybutton.png
# note: Laz trunk/1.4+? uses .res resource files instead of .lrs files:
~/lazarus/tools/lazres mybutton.res tmybutton.png

This creates an pascal include file, which is used in the initialization section of mybutton.pas:

initialization
  {$I mybutton.lrs}
  // or in newer Lazarus version - to docheck syntax
  {$R mybutton.res}

Install the package.

Hiding a component in the component palette

Package IDEIntf, unit componentreg:

IDEComponentPalette.FindComponent('TButton').Visible:=false;

Writing component editors

A component editor handles things like double clicking on a component in a designer or adding menu items when right clicking on a component. Writing a component editor is easy. See the unit componenteditors.pas of the package IDEIntf for lots of examples. For instance to show an editor when double clicking see TCheckListBoxComponentEditor.

Writing property editors

Each type of property (integer, string, TFont, ...) in the Object Inspector needs a property editor. If your component has a property Password of type string, then you can define a property editor for your specific component class, with a property named Password of type string. The unit propedits.pp of the package IDEIntf contains many of the standard property editors used by the IDE itself. For example the TStringPropertyEditor is the default property editor for all strings, while TComponentNamePropertyEditor is more specific and handles only TComponent.Name. See the unit for plenty of examples.

Hiding a property in the Object Inspector

There is a special property editor for hiding:

 RegisterPropertyEditor(TypeInfo(TAnchorSide), TControl, 'AnchorSideLeft', THiddenPropertyEditor);

Forms

The IDE allows to easily create descendants of forms and frames. These forms can have new published components and new published methods. But you when you add new published properties, they are not accessible via the Object Inspector. They work only at runtime. The methods in the IDE are fake, they can not be executed. The IDE can create the components, because their classes are registered in the component palette. Your new properties only exist in your source code. The IDE can not fake them yet. That's why you have to register the new class. That means:

  • create a design time package (or extend an existing one)
  • your form must be in a unit of this package
...
interface
 
uses FormEditingIntf, Forms;
 
type
  TYourForm = class(TForm)
  private
    FYourProperty: integer;
  published
    property YourProperty: integer read FYourProperty write FYourProperty;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
 FormEditingHook.RegisterDesignerBaseClass(TYourForm);
end;
...
  • check the "Register" in the package editor for this unit.
  • Install the package in the IDE

Designer

Writing a designer mediator

The standard designer allows to visually edit LCL controls, while all others are shown as icons(like TOpenDialog or TDataSource). To visually edit non LCL control, you need to create a designer mediator. This can be used to design webpages, UML diagrams or other widgetsets like fpGUI. There is a complete example in examples/designnonlcl/.

  • Install the example package examples/designnonlcl/notlcldesigner.lpk and restart the IDE. This will register the designer mediator for TMyWidget components and add the new components TMyButton, TMyGroupBox to the component palette.
  • Open the the example project examples/designnonlcl/project/NonLCL1.lpi.
  • Open the unit1.pas and show the designer form (F12). You should see the components as red rectangles, which can be selected, moved and resized like LCL controls.

Create a new unique component name

uses FormEditingIntf;
 
...
 
NewName:=FormEditingHook.CreateUniqueComponentName(AComponent);
 
// Or if you need to create the name before creating the component:
 
NewName:=FormEditingHook.CreateUniqueComponentName(ComponentClassName,OwnerComponent);
// ComponentClassName will be used as prefix, without a leading T.
// OwnerComponent is the new owner of the new component.

Select a component in the designer/object inspector

uses propedits;
..
GlobalDesignHook.SelectOnlyThis(AComponent);

Get unit, designer, form of a file

uses ProjectIntf, LazIDEIntf, ComponentEditors;
...
// open file
if LazarusIDE.DoOpenEditorFile(Filename,-1,-1,[ofAddToRecent])<>mrOk then exit;
// get file interface
aFile:=LazarusIDE.ActiveProject.FindFile(Filename,[]);
// get designer
aDesigner:=TComponentEditorDesigner(LazarusIDE.GetDesignerWithProjectFile(AFile,true));
if aDesigner=nil then 
  exit; // this unit has no resource (form, frame, datamodule, etc.)
// get form
aForm:=aDesigner.Form;

Save unit, designer form

uses LazIDEIntf;
...
if LazarusIDE.DoSaveEditorFile(Filename,[])<>mrOk then exit;

Event handlers

There are several events in the IDE, for which plugins can add their own handlers.

Designer events

In propedits.pp there is a "GlobalDesignHook" object, which maintains several events for designing. Each event calls a list of handlers. The default handlers are added by the IDE. You can add your own handlers with the AddHandlerXXX and RemoveHandlerXXX methods. They will be called before the default handlers.

Examples:

  • Adding your handler (this is normally done in the constructor of your object):
 GlobalDesignHook.AddHandlerPersistentAdded(@YourOnPersistentAdded);
  • Removing your handler:
 GlobalDesignHook.RemoveHandlerPersistentAdded(@YourOnPersistentAdded);
  • You can remove all handlers at once. For example, it is a good idea to add this line in the destructor of your object:
 GlobalDesignHook.RemoveAllHandlersForObject(Self);

The handlers of GlobalDesignHook:

  • Lookup root
    • ChangeLookupRoot - Called when the "LookupRoot" changed. The "LookupRoot" is the owner object of the currently selected components. Normally this is a TForm.
  • Methods
    • CreateMethod
    • GetMethodName
    • GetMethods
    • MethodExists
    • RenameMethod
    • ShowMethod
    • MethodFromAncestor
    • ChainCall
  • Components
    • GetComponent
    • GetComponentName
    • GetComponentNames
    • GetRootClassName
    • AddClicked - Called when the user selected a component class and clicked on the designer to add a new component. You can change the component class and the parent.
    • ComponentRenamed - Called when a component was renamed
  • TPersistent objects
    • PersistentAdded - Called when a new TPersistent was added to the LookupRoot
    • PersistentDeleting - Called before a TPersistent is freed.
    • DeletePersistent - Called by the IDE to delete a TPersistent.
    • GetSelectedPersistents - Called when getting the current selection of TPersistent.
    • SetSelectedPersistents - Called when setting the current selection of TPersistent.
    • GetObject
    • GetObjectName
    • GetObjectNames
  • Modifing
    • Modified
    • Revert
    • RefreshPropertyValues
  • Selection
    • SetSelection
    • GetSelection

Examples for designer events

Example: When dropping a new component move control to a child

...
type
  TYourDesignerExtension = class
  private
    function AddClicked(ADesigner: TIDesigner;
             MouseDownComponent: TComponent; Button: TMouseButton;
             Shift: TShiftState; X, Y: Integer;
             var AComponentClass: TComponentClass;
             var NewParent: TComponent): boolean of object;
  public
    constructor Create;
    destructor Destroy; override;
  end;
 
...
constructor TYourDesignerExtension.Create;
begin
  // register your handler
  GlobalDesignHook.AddHandlerAddClicked(@AddClicked);
end;
 
destructor TYourDesignerExtension.Destroy;
begin
  // remove your handler:
  GlobalDesignHook.RemoveHandlerAddClicked(@AddClicked);
end;
 
function TYourDesignerExtension.AddClicked(ADesigner: TIDesigner;
             MouseDownComponent: TComponent; Button: TMouseButton;
             Shift: TShiftState; X, Y: Integer;
             var AComponentClass: TComponentClass;
             var NewParent: TComponent): boolean of object;
begin
  // in this example TYourControl is a custom control with a ChildControl.
  // Whenever the user drops a control on the TYourControl instead of adding
  // the new control to the TYourControl, the new control is added as child
  // of ChildControl.
  // created 
  if MouseDownComponent is TYourControl then
    NewParent:=TYourControl(MouseDownComponent).ChildControl;
  Result:=true;
end;

Disabling the designer mouse handler

Normally the designer catches all mouse events of a component. If your custom control should handle the mouse events handle you can either set the csDesignInteractive ControlStyle or you can fine control it via the message CM_DESIGNHITTEST in your control:

type
  TYourControl = class(TCustomControl)
  protected
    procedure CMDesignHitTest(var Message: TLMessage); message CM_DESIGNHITTEST;
  end;
...
procedure TYourControl.CMDesignHitTest(var Message: TLMessage);
var
  p: TSmallPoint;
  aShiftState: TShiftState;
begin
  aShiftState:=KeysToShiftState(PtrUInt(Message.WParam));
  p:=TSmallPoint(longint(Message.LParam));
  debugln(['TForm1.CMDesignHitTest ShiftState=',dbgs(aShiftState),' x=',p.x,' y=',p.y]);
  if SkipDesignerMouseHandler then
    Message.Result:=1; // now the designer calls the normal MouseUp, MouseMove and MouseDown methods.
end;

Getting notified when a designer form is modified

Every designed LCL form has a Designer of type TIDesigner. The IDE creates designers of type TComponentEditorDesigner defined in the IDEIntf unit componenteditors. For example:

procedure TYourAddOn.OnDesignerModified(Sender: TObject);
var
  IDEDesigner: TComponentEditorDesigner;
begin
  IDEDesigner:=TComponentEditorDesigner(Sender);
  ...
end;
 
procedure TYourAddOn.ConnectDesignerForm(Form1: TCustomForm);
var
  IDEDesigner: TComponentEditorDesigner;
begin
  IDEDesigner:=TComponentEditorDesigner(Form1.Designer);
  IDEDesigner.AddHandlerModified(@OnDesignerModified);
end;

Project events

These events are defined in unit LazIDEIntf.

  • LazarusIDE.AddHandlerOnProjectClose: called before a project is closed
  • LazarusIDE.AddHandlerOnProjectOpened: called after the project was completely opened (for example all required packages were loaded, units were opened in the source editor)
  • LazarusIDE.AddHandlerOnSavingAll: called before IDE saves everything
  • LazarusIDE.AddHandlerOnSavedAll: called after IDE saved everything
  • LazarusIDE.AddHandlerOnProjectBuilding: called before IDE builds the project
  • LazarusIDE.AddHandlerOnProjectDependenciesCompiling: called before IDE compiles package dependencies of project
  • LazarusIDE.AddHandlerOnProjectDependenciesCompiled: called after IDE compiled package dependencies of project
  • LazarusIDE.AddHandlerOnProjectBuildingFinished: called after IDE built the project (successful or not) (since 1.5)

Other IDE events

uses LazIDEIntf;

Call these in the Register procedure of your package:

  • LazarusIDE.AddHandlerOnIDERestoreWindows: called when IDE is restores its windows (before opening the first project)
  • LazarusIDE.AddHandlerOnIDEClose: called when IDE is shutting down (after closequery, so no more interactivity)
  • LazarusIDE.AddHandlerOnQuickSyntaxCheck: called when the menu item or the shortcut for Quick syntax check is executed
  • LazarusIDE.AddHandlerOnLazarusBuilding: // called before IDE builds Lazarus IDE (since 1.5)
  • LazarusIDE.AddHandlerOnLazarusBuildingFinished: // called after IDE builds Lazarus IDE (since 1.5)
  • LazarusIDE.AddHandlerGetFPCFrontEndParams: // called when the IDE gets the parameters of the 'fpc' front end tool (since 1.5)
  • LazarusIDE.AddHandlerGetFPCFrontEndPath: // called when the IDE gets the path of the 'fpc' front end tool (since 1.5)
  • LazarusIDE.AddHandlerOnUpdateIDEComponentPalette: ?
  • LazarusIDE.AddHandlerOnUpdateComponentPageControl: ?
  • LazarusIDE.AddHandlerOnShowDesignerFormOfSource: // called after showing a designer form for code editor (AEditor can be nil!) (since 1.5)
  • LazarusIDE.AddHandlerOnShowSourceOfActiveDesignerForm: // called after showing a code of designer form (since 1.5)
  • LazarusIDE.AddHandlerOnChangeToolStatus: //called when IDEToolStatus has changed (e.g. itNone->itBuilder etc.) (since 1.5)

Call these in the initialization section of one of your units:

  • AddBootHandler
    • AddBootHandler(libhTransferMacrosCreated,@YourProc): called after IDEMacros were created (since 1.3)
    • AddBootHandler(libhEnvironmentOptionsLoaded,@YourProc): called after environment options were loaded (since 1.3)

Project

Current Project

The current main project can be obtained by LazarusIDE.ActiveProject. (unit LazIDEIntf)

All units of current project

Note that the term "units of project" is ambiguous.

  • The project inspector shows the list of the .lpi file. The project might not use all these units on all platforms and maybe the developer forgot to add all used units.
  • Units from used packages are not project units.

All units of the project inspector

To iterate through all pascal units listed in the project inspector you can use for example:

uses 
  LCLProc, FileUtil, LazIDEIntf, ProjectIntf;
 
procedure ListProjectUnits;
var
  LazProject: TLazProject;
  i: Integer;
  LazFile: TLazProjectFile;
begin
  LazProject:=LazarusIDE.ActiveProject;
  if LazProject<>nil then
    for i:=0 to LazProject.FileCount-1 do
    begin
      LazFile:=LazProject.Files[i];
      if LazFile.IsPartOfProject
      and FilenameIsPascalUnit(LazFile.Filename)
      then
        debugln(LazFile.Filename);
    end;
end;

All used units of project

To find all currently used units of a project/package you can use the following. Note that the IDE needs to parse all units, so it can take some time.

uses 
  LazLoggerBase, LazIDEIntf, ProjectIntf;
 
procedure ListProjectUnits;
var
  LazProject: TLazProject;
  i: Integer;
begin
  LazProject:=LazarusIDE.ActiveProject;
  if LazProject<>nil then 
  begin
    Units:=LazarusIDE.FindUnitsOfOwner(LazProject,[fuooListed,fuooUsed]); // add fuooPackages to include units from packages
    try
      for i:=0 to Units.Count-1 do
        debugln('Filename=',Units[i]);
    finally
      Units.Free;
    end;
  end;
end;

The .lpr, .lpi and .lps file of a project

uses 
  LCLProc, FileUtil, ProjectIntf, LazIDEIntf;
var
  LazProject: TLazProject;
begin
  LazProject:=LazarusIDE.ActiveProject;
  // every project has a .lpi file:
  DebugLn(['Project'' lpi file: ',LazProject.ProjectInfoFile]);
 
  // if the project session information is stored in a separate .lps file:
  if LazProject.SessionStorage<>pssNone then
    DebugLn(['Project'' lps file: ',LazProject.ProjectSessionFile]);
 
  // If the project has a .lpr file it is the main source file:
  if (LazProject.MainFile<>nil)
  and (CompareFileExt(LazProject.MainFile.Filename,'lpr')=0) then
    DebugLn(['Project has lpr file: ',LazProject.MainFile.Filename]);
end;

The executable / target file name of a project

There is a macro $(TargetFile), which can be used in paths and external tools. You can query the macro in code:

uses MacroIntf;
 
function MyGetProjectTargetFile: string;
begin
  Result:='$(TargetFile)';
  if not IDEMacros.SubstituteMacros(Result) then
    raise Exception.Create('unable to retrieve target file of project');
end;

See here for more macros: IDE Macros in paths and filenames.

Add your own project type

You can add items to the 'New ...' dialog:


Add your own file type

You can add items to the 'New ...' dialog:

  • See the unit ProjectIntf of the package IDEIntf.
  • Choose a base class TFileDescPascalUnit for normal units or TFileDescPascalUnitWithResource for a new form/datamodule.

Add a new file type

uses ProjectIntf;
...
  { TFileDescText }
 
  TFileDescMyText = class(TProjectFileDescriptor)
  public
    constructor Create; override;
    function GetLocalizedName: string; override;
    function GetLocalizedDescription: string; override;
  end;
...
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterProjectFileDescriptor(TFileDescMyText.Create,FileDescGroupName);
end;
 
{ TFileDescMyText }
 
constructor TFileDescMyText.Create;
begin
  inherited Create;
  Name:='MyText'; // do not translate this
  DefaultFilename:='text.txt';
  AddToProject:=false;
end;
 
function TFileDescText.GetLocalizedName: string;
begin
  Result:='My Text'; // replace this with a resourcestring
end;
 
function TFileDescText.GetLocalizedDescription: string;
begin
  Result:='An empty text file';
end;

Add a new form type

uses ProjectIntf;
 
...
  TFileDescPascalUnitWithMyForm = class(TFileDescPascalUnitWithResource)
  public
    constructor Create; override;
    function GetInterfaceUsesSection: string; override;
    function GetLocalizedName: string; override;
    function GetLocalizedDescription: string; override;
  end;
...
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterProjectFileDescriptor(TFileDescPascalUnitWithMyForm.Create,FileDescGroupName);
end;
 
{ TFileDescPascalUnitWithMyForm }
 
constructor TFileDescPascalUnitWithMyForm.Create;
begin
  inherited Create;
  Name:='MyForm'; // do not translate this
  ResourceClass:=TMyForm;
  UseCreateFormStatements:=true;
end;
 
function TFileDescPascalUnitWithMyForm.GetInterfaceUsesSection: string;
begin
  Result:='Classes, SysUtils, MyWidgetSet';
end;
 
function TFileDescPascalUnitWithForm.GetLocalizedName: string;
begin
  Result:='MyForm'; // replace this with a resourcestring
end;
 
function TFileDescPascalUnitWithForm.GetLocalizedDescription: string;
begin
  Result:='Create a new MyForm from example package NotLCLDesigner';
end;

Packages

You can perform various actions with Packages:

Search in all packages

Iterate all packages loaded in the IDE (since 0.9.29).

uses PackageIntf;
...
for i:=0 to PackageEditingInterface.GetPackageCount-1 do
  writeln(PackageEditingInterface.GetPackages(i).Name);

Search a package with a name

uses PackageIntf;
...
var
  Pkg: TIDEPackage;
begin
  Pkg:=PackageEditingInterface.FindPackageWithName('LCL');
  if Pkg<>nil then 
    ...
end;

Note: FindPackageWithName does not open the package editor. For that use DoOpenPackageWithName.

Obtener el nombre de fichero .lpk de un paquete instalado

uses PackageIntf;
...
var
  Pkg: TIDEPackage;
begin
  Pkg:=PackageEditingInterface.FindPackageWithName('LCL');
  if Pkg<>nil then 
    LPKFilename:=Pkg.Filename;
end;

Instalar paquetes

Note: Instalar únicamente paquetes con plugins IDE. Instalar otros paquetes puede hacer el IDE inestable.

uses PackageIntf, contnrs;
...
  PkgList:=TObjectList.create(true);
  try
    Pkg:=TLazPackageID.Create;
    Pkg.Name:='Cody';
    PkgList.Add(Pkg);
    // Chequear si el IDE puede encontrar cody.lpk y todas las dependencias.
    // El IDE will muestra algunas advertencias/confirmaciones si algo parece extraño.
    if not PackageEditingInterface.CheckInstallPackageList(PkgList,[]) then
      exit;
    // in this example we have checked already, so skip the warnings
    // and rebuild the IDE
    if PackageEditingInterface.InstallPackages(PkgList,[piiifSkipChecks,piiifRebuildIDE])<>mrOK then
      exit;
  finally
    PkgList.Free;
  end;

Abrir un fichero de paquete (lpk)

uses PackageIntf, FileUtil;
...
var
  pkg: TIDEPackage;
begin
  if PackageEditingInterface.DoOpenPackageFile(LPKFilename,[pofAddToRecent],false)<>mrOk then
    exit;
  Pkg:=PackageEditingInterface.FindPackageWithName(ExtractFilenameOnly(LPKFilename));
  ...
end;
Warning-icon.png

Warning: El IDE cierra automáticamentes paquetes no utilizados on idle. Nunca mantener referencias a un TIDEPackage.

Find the package(s) of a unit

You have the file name of a unit, e.g. '/home/user/unit.pas', and you want to know to which package(s) and project(s) it belongs use this:

uses Classes, PackageIntf, ProjectIntf;
...
var
  Owners: TFPList;
  i: Integer;
  o: TObject;
begin
  Owners:=PackageEditingInterface.GetPossibleOwnersOfUnit('/full/path/of/unit.pas',[]);
  if Owners=nil then begin
    // unit is not directly associated with a project/package
    // maybe the unit was for some reason not added, but is reachable
    // search in all unit paths of all projects/packages
    // Beware: This can lead to false hits
    Owners:=PackageEditingInterface.GetPossibleOwnersOfUnit('/full/path/of/unit.pas',
      [piosfExcludeOwned,piosfIncludeSourceDirectories]);
  end;
  if Owners=nil then exit;
  try
    for i:=0 to Owners.Count-1 do begin
      o:=TObject(Owners[i]);
      if o is TIDEPackage then begin
        writeln('Owner is package ',TIDEPackage(o).Name);
      end else if o is TLazProject then begin
        writeln('Owner is project ',TLazProject(o).ProjectInfoFile);
      end;
    end;
  finally
    Owners.Free;
  end;

Ventanas

Hay básicamente cuatro tipos de ventandas del IDE:

  • La barra principal del IDE bar que es Application.MainForm. Siempre está presente.
  • Ventanas flotantes/acoplables tales como el editor del código fuente, the Object Inspectors and Messages.
  • Fomas modales, tales como los diálogos de búsqueda, opciones y preguntas.
  • Sugerencias y formas de completado.

Añadiendo una nueva ventana de acoplado en el IDE

Que es una ventana del IDE acoplable: Ventanas tales como el Editor de Código Fuente o el Inspector de Objetos son ventanas flotantes que se pueden acoplar si se instala un paquete de acoplamiento, y su estado, posición y tamaño se almacena y restaura en el siguiente inicio del IDE. En orden a restaurar una ventana el IDE necesita un creador como se definie en la unidad IDEWindowIntf del paquete IDEIntf. Cada ventana acoplable debe tener un único nombre. No usar por tanto nombres genéricos como 'FileBrowser' porque haría que entre fácilmente en conflicto con otros paquetes. Y no usar nombres cortos como 'XYZ' porque el creador es responsable de todas las formas que comiencen con dicho nombre.

Como registrar una ventana acoplable del IDE

Recuerda utilizar un nombre largo única que sea un identificador pascal válido. Tus ventanas pueden tener cualquier caption que necesites.

uses SysUtils, IDEWindowIntf;
...
var MyIDEWindow: TMyIDEWindow = nil; 
 
procedure CreateMyIDEWindow(Sender: TObject; aFormName: string; var AForm: TCustomForm; DoDisableAutoSizing: boolean);
begin
  // check the name
  if CompareText(aFormName,MyIDEWindowName)<>0 then exit;
  // create the form if not already done and disable autosizing
  IDEWindowCreators.CreateForm(MyIDEWindow,TMyIDEWindowm,DoDisableAutosizing,Application);
  ... init the window ...
  AForm:=MyIDEWindow;
end;
 
procedure Register;
begin
  IDEWindowCreators.Add('MyIDEWindow',@CreateMyIDEWindow,nil,'100','50%','+300','+20%');
  // the default boundsrect of the form is:
  // Left=100, Top=50% of Screen.Height, Width=300, Height=20% of Screen.Height
  // when the IDE needs an instance of this window it calls the procedure CreateMyIDEWindow.
end;

Showing an IDE window

Do not use Show. Use:

IDEWindowCreators.ShowForm(MyIDEWindow,false);

This will work with docking. The docking system might wrap the form into a docking site. The BringToFront parameter tells the docking system to make the form and all its parent sites visible and bring the top level site to the front.

Notes about IDEWindowCreators and SimpleLayoutStorage

The IDEWindowCreators.SimpleLayoutStorage simply stores the BoundsRect and WindowState of all forms that were once opened. It is used as fallback if no dockmaster is installed. It stores the state even if a DockMaster is installed, so that when the dockmaster is uninstalled the forms bounds are restored.

The IDEWindowCreators is used by all dockable forms to register themselves and to show forms. When showing a form the Creator checks if a IDEDockMaster is installed and will delegate the showing to it. If no IDEDockMaster is installed it simply shows the form. The IDEDockMaster can use the information in the IDEWindowCreators to create forms by names and get an idea where to place a form when showing it for the first time. For more details see the packages AnchorDockingDsgn and EasyDockMgDsgn.

CodeTools

The CodeTools is a package providing a vast amount of functions to parse, search and change pascal sources and some basic functions for other languages as well. You can use them without the IDE too. It is recommended that you read first about the CodeTools in general before using them in the IDE: Codetools.

Before calling any of the CodeTools functions in the IDE you should commit the current changes of the source editor to the CodeTools buffers:

uses LazIDEIntf;
...
  // save changes in source editor to codetools
  LazarusIDE.SaveSourceEditorChangesToCodeCache(-1); // -1: commit all source editors

Adding a resource directive to a file

This adds a {$R example.res} to a pascal unit:

procedure AddResourceDirectiveToPascalSource(const Filename: string);
var
  ExpandedFilename: String;
  CodeBuf: TCodeBuffer;
begin
  // make sure the filename is trimmed and contains a full path
  ExpandedFilename:=CleanAndExpandFilename(Filename);
 
  // save changes in source editor to codetools
  LazarusIDE.SaveSourceEditorChangesToCodeCache(-1);
 
  // load the file
  CodeBuf:=CodeToolBoss.LoadFile(ExpandedFilename,true,false);
 
  // add the resource directive
  if not CodeToolBoss.AddResourceDirective(CodeBuf,'example.res') then
    LazarusIDE.DoJumpToCodeToolBossError;
end;

The codetools provides also functions like FindResourceDirective and RemoveDirective.

Getting the search paths for units and include files

There are many different search paths in the IDE from projects, packages, the fpc and lazarus directory and there are many types of paths: before or after resolving macros, with or without inherited search paths, as relative or absolute paths. All files in a directory share the same set of search paths. You can get the search paths for each directory fully resolved by asking the codetools:

uses CodeToolManager;
...
 
Dir:=''; // the empty directory is for new files and has the same settings as the project directory
 
// Getting the search paths for include files:
Path:=CodeToolBoss.GetIncludePathForDirectory(Dir);
 
// Getting the search paths for units:
// This search path is passed to the compiler.
// It contains the package output directories, but not the package source directories.
Path:=CodeToolBoss.GetUnitPathForDirectory(Dir);
 
// There can be additional unit search paths for the IDE only (not passed to the compiler)
Path:=CodeToolBoss.GetSrcPathForDirectory(Dir);
 
// The complete search path contains also all package source paths for units:
Path:=CodeToolBoss.GetCompleteSrcPathForDirectory(Dir);

Source Editor

Active source editor

uses SrcEditorIntf;
...
Editor:=SourceEditorManagerIntf.ActiveEditor;
if Editor=nil then exit;
Filename:=Editor.FileName;
ScreenPos:=Editor.CursorScreenXY;
TextPos:=Editor.CursorTextXY;

SynEdit

Getting the settings for a TSynEdit

When you have a dialog using a TSynEdit and you want to use the same font and settings like the source editor use:

uses SrcEditorIntf;
...
SourceEditorManagerIntf.GetEditorControlSettings(ASynEdit);

Getting the settings for a SynEdit highlighter

When you have a dialog using a TSynEdit with a highlighter and you want to use the same colors like the source editor highlighter for this language use:

uses SrcEditorIntf;
...
SourceEditorManagerIntf.GetHighlighterSettings(ASynHighlighter);

See for an example: TSQLStringsPropertyEditorDlg.Create in the unit SQLStringsPropertyEditorDlg.

Help

Adding help for sources

First create a THelpDatabase:

HelpDB := TFPDocHTMLHelpDatabase(
  HelpDatabases.CreateHelpDatabase('ANameOfYourChoiceForTheDatabase',
  TFPDocHTMLHelpDatabase,true));
HelpDB.DefaultBaseURL := 'http://your.help.org/';
 
FPDocNode := THelpNode.CreateURL(HelpDB,
  'Package1 - A new package',
  'file://index.html');
HelpDB.TOCNode := THelpNode.Create(HelpDB,FPDocNode);// once as TOC
DirectoryItem := THelpDBISourceDirectory.Create(FPDocNode,'$(PkgDir)/lcl',
  '*.pp;*.pas',false);// and once as normal page
HelpDB.RegisterItem(DirectoryItem);

Adding lines to the messages window

unit IDEMsgIntf;
...
var Dir: String;
begin
  Dir:=GetCurrentDir;
  IDEMessagesWindow.BeginBlock;
  IDEMessagesWindow.AddMsg('unit1.pas(30,4) Error: Identifier not found "a"',Dir,0);
  IDEMessagesWindow.EndBlock;
end;

Miscellaneous

Adding a macro

You can add your own IDE macros:

uses MacroIntf, MacroDefIntf;
 
procedure Register;
begin
  // registering a Macro with a fixed value:
  IDEMacros.Add(TTransferMacro.Create('MyMacro1','Value','Description',nil,[]));
end;
 
// registering a Macro with a dynamic value requires a method of an object
procedure TForm1.Init;
begin
  IDEMacros.Add(TTransferMacro.Create('MyMacro','Value','Description',@OnResolveMyMacro,[]));
end;
 
function TForm1.OnResolveMyMacro(const s: string; const Data: PtrInt;
  var Abort: boolean): string;
// s is the parameter. For example $MyMacro(parameter)
// Data is the querying instance. Can be nil.
// If there is an error set Abort to true.
// The result string can contain macros. For example Result:='$(FPCVer)/$(TargetOS)';
begin
  Result:='MyValue';
end;

Depurando el IDE

  • Compila el IDE como habitualmente e.g. via Tools/Build de Lazarus. Asegúrate de establecer los flags de depuración tales como -g o -gw2. Lo mejor es utilizar el profile "Debug IDE" que contiene algunos flags útiles para depuración.
  • Abre el proyecto ide\lazarus.lpi. No se puede compilar directamente este proyecto.
  • Establece los puntos de ruptura (breakpoints) acorde a tus necesidades etc y ejecuta el proyecto.

See also