Recent

Author Topic: I need advice for a new component  (Read 1317 times)

Lurgainn

  • New member
  • *
  • Posts: 7
I need advice for a new component
« on: November 20, 2024, 03:04:03 pm »
Hi,
I already programmed a lot with Pascal, long years ago with Delphi and last years with LazarusIDE/fpc, and I already read the wiki "How To Write Lazarus Component", but I have residual doubts about how it is better to proceed.

I want to create a new component (design and runtime) to show my personal "About" and "License" dialogs in my programs, by setting some properties and calling the methods "ShowAbout" and "ShowLicense", a little as it already exists with the TSplashAbout component, but with different visual aspect and different components.

But I have this two doubts:
(1) While creating the units and the package, can I include forms already designed using the IDE or it's impossible to do so and I need to dinamically create the forms at runtime, as already does TSplashAbout?
(2) Ok that I need to use the menù "Package -> New Package ...", but before that, I'm stuck at the "Project Wizard", so which type of "New Project" is more adapt to the tipology of my component? "Application"? "Program"?

Thanks for any help

wp

  • Hero Member
  • *****
  • Posts: 12523
Re: I need advice for a new component
« Reply #1 on: November 20, 2024, 05:32:18 pm »
(1) While creating the units and the package, can I include forms already designed using the IDE or it's impossible to do so and I need to dinamically create the forms at runtime, as already does TSplashAbout?
Suppose your component is named "TAboutDlg". Inherit this component from TComponent so that it can appear on the component palette. Implement a method TAboutDlg.Show which displays the splash form itself. You can design this form as usual in the Lazarus form designer, i.e. add some labels for the application name, its version or whatever you like, and a button to close the form (--> TAboutForm). In TAboutDlg.Show create an instance of TAboutForm, show it modally and then destroy it. That's all...
Code: Pascal  [Select][+][-]
  1. // in unit "splashdlg"
  2. type
  3.   TAboutDlg = class(TComponent)
  4.   private
  5.     FApplicationName: String;
  6.     FAPplicationVersion: String;
  7.     FApplicationIcon: TIcon;
  8.   public
  9.     procedure Show;
  10.     property ApplicationName: String read FApplicationName write FApplicationName;
  11.     property ApplicationVersion: String read FApplicationVersion write FApplicationVersion;
  12.     property ApplicationIcon: TIcon read FApplicationIcon write FApplicationIcon;
  13.     // etc.
  14.   end;
  15.  
  16. procedrue TAboutDlg.Show;
  17. var
  18.   Frm: TAboutForm;
  19. begin
  20.   Frm := TAboutForm.Create(nil);   // TAboutForm should be stored in a separate unit "aboutfrm"
  21.   try
  22.     Frm.ApplicationNameLabel.Caption := FApplicationName;
  23.     Frm.ApplicationVersionLabel.Caption := FApplicationVersion;
  24.     Frm.ApplicationIcon.Assign(FApplicationIcon);
  25.     // etc.
  26.     Frm.ShowModal;
  27.   finally
  28.     Frm.Free;
  29.   end;
  30. end;
  31.  
  32. // in units splashfrm
  33. procedure TForm1.Button1click(Sender: TObject);
  34. begin
  35.   Close;  // or alternatively: set the close button's ModalResult to mrClose, and this method is not needed.
  36. end;  

(2) Ok that I need to use the menù "Package -> New Package ...", but before that, I'm stuck at the "Project Wizard", so which type of "New Project" is more adapt to the tipology of my component? "Application"? "Program"?
Menu "File" > "New" > "Package" > "Package", or easier: Menu "Package" > "New Package". Navigate to the folder in which you want to store the package and its files, enter the package name, "OK". Then, in the package editor under "Files" add the units which you wrote for this package, following above example, "splashdlg.pas" and "splashfrm.pas" (you can also add "splashfrm.lfm", but not necessary). Under "Requirements" you should add "LCL" as a "new requirement", and any other packages which are needed by your component.

Now you should be able to "compile" the package. If it succeeds write a small test application, just a form with a button to open the about form:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   about: TAboutDlg;
  4. begin
  5.   about := TAboutDlg.Create(nil);
  6.   try
  7.     about.ApplicationName := 'My test application';
  8.     about.ApplicationVersion := '1.0.0';
  9.     about.AppliationIcon := Icon;
  10.     about.Show;
  11.   finally
  12.     about.Free;
  13.   end;
  14. end;

When this works as expected you can prepare the component for usage at designtime. Add a procedure "Register" to the splashdlg unit (the one with "TSplashDlg"):
Code: Pascal  [Select][+][-]
  1. procedure Register;
  2. begin
  3.   RegisterComponents('the_palette_for_my_component', [TSplashDlg]);
  4. end;
And in the package editor check the box "register unit" for the "splashdlg" unit - now this item has a green triangle in this treeview icon.

You can click "Use" > "Install" now, and the IDE will rebuild itself to contain the new component on the palette that you specified in the RegisterComponents call.

Sometimes, when the new component is buggy, the new IDE may not start any more. In this case, you should know that the folder with the lazarus binary ("lazarus.exe" on Windows) contains a backup of the previously used version, lazarus.old(.exe). Delete the new lazarus and rename lazarus.old to lazarus - and you will be able to run the previous version and to fix the component.

Lurgainn

  • New member
  • *
  • Posts: 7
Re: I need advice for a new component
« Reply #2 on: November 26, 2024, 10:31:28 am »
Thanks so much for your help!

About the second question, I understand your instructions, but when I start the Lazarus-IDE (version 3.6 under Ubuntu derivate), the first thing that I see is a Project Wizard (see my first attachment) with a list of choices (see my second attachment).
The menù is disabled, so I can't select "File" > "New" > "Package" > "Package", or "Package" > "New Package".

Am I doing something wrong or first I need to select a type of new project and then follow with your instructions?

Thanks again.

wp

  • Hero Member
  • *****
  • Posts: 12523
Re: I need advice for a new component
« Reply #3 on: November 26, 2024, 12:09:11 pm »
I must be doing something completely differenty because I never see this wizard.
- I run Lazarus -> the IDE opens with a new project (with empty form) (or with the previously active project if "Tools" > "Options" > "IDE Startup" > "Open last projet and packages at start" is checked).
- I can create a new package in two ways: Either "Package" > "New package", or "File" > "New..." > "Package" > "Package" (you must scroll down the tree a bit to see the "Package" node).

If the wizard appears, it certainly is not wrong to select "New project" - this will just create a new empty project. From here you can create the new package as I explained (a package is independent of the project).

Lurgainn

  • New member
  • *
  • Posts: 7
Re: I need advice for a new component
« Reply #4 on: December 10, 2024, 02:41:05 pm »
Sorry if I didn't reply until today.
Maybe the difference between me and you is that in my options that checkbox in the "IDE Startup" is checked, so if I close the current project and then I quit Lazarus, at the next startup Lazarus show the Project Wizard. Instead if I uncheck that checkbox and I do the same actions, when I restart Lazarus it immediately open in a new default application project without displaying the Project Wizard.

However, after some test that I did, I decided to create my component without any pre-designed form, so I will dynamically create all components/form to have a full control on how to set every thing.

So now my doubt is: due to the fact that Lazarus-IDE needs to always have a project open, which type of project is better to choice? Application or Simple Program or Program?
And if I choice Application (so I'll have already included all the "uses" needed to manage forms and components), how is it necessary to modify the unit and the project source so that I can exclude the default form to be included into the directory where I'll save the new package/component?

Thanks again for your kind support.
Cheers

wp

  • Hero Member
  • *****
  • Posts: 12523
Re: I need advice for a new component
« Reply #5 on: December 10, 2024, 03:18:11 pm »
So now my doubt is: due to the fact that Lazarus-IDE needs to always have a project open, which type of project is better to choice? Application or Simple Program or Program?
It depends on which kind of projects you usually work with: When you usually create GUI applications select "Application", and when you usually create commandline applications select "Simple Program" or "Program".

And if I choice Application (so I'll have already included all the "uses" needed to manage forms and components), how is it necessary to modify the unit and the project source so that I can exclude the default form to be included into the directory where I'll save the new package/component?
I'm afraid, I don't understand. In order to create a new package simple go to "File" > "New" > "Package" > "Package", no matter which project type is loaded. This new package, per se, is not part of the project. (To use it in a specific project, open the project and make sure that the project inspector is open. Then click "Add new requirement" and select the needed package from the list).

 

TinyPortal © 2005-2018