Recent

Author Topic: [Solved] Custom Form: How to make this a component?  (Read 9536 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
[Solved] Custom Form: How to make this a component?
« on: July 29, 2012, 05:25:46 pm »
I have created components before, but with this one I'm a little confused on what to do and help would be greatly appreciated.

I created a regular Lazarus application with a TForm.

I placed some components on the form (a few TImage and one TImagelist).

I also added a few events;
  • TForm.onCreate for Application event handler (activate/deactivate application).
  • TForm.onPaint event to drawn custom stuff on the form (OSX like notebook header, the images function like minimize, maximize, and close buttons, a resize icon, a custom caption with background).
  • TForm.mouse events to move and resize the form.
  • TImage.mouse events for custom icons, hover behavior, and of course the clicking of icons

This so far looks and works great!

Now ideally I'd like reuse this form for future projects.
So I'm exploring options to either make a Form template for this, but I haven't found this functionality in Lazarus (like the Repository in Delphi). So my next thought was to create a component that I can drop on a form that adds exactly these functions and components.

Does anyone have an idea or suggestion on how to do this?
« Last Edit: August 09, 2012, 04:43:53 am by Hansaplast »

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Custom Form: How to make this a component?
« Reply #1 on: July 29, 2012, 05:37:12 pm »
Hi Hansaplast,

Are you perhaps looking for something like Form Inheritance?  I may be looking for something like this as well so that all my forms in an application look consistent without me having to go thru the same steps over and over.

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
Re: Custom Form: How to make this a component?
« Reply #3 on: July 29, 2012, 06:20:59 pm »
http://wiki.freepascal.org/Project_Templates

Oh didn't find that when searching for it - nice catch! That might be what I'm looking for.

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
Re: Custom Form: How to make this a component?
« Reply #4 on: July 29, 2012, 06:23:15 pm »
Are you perhaps looking for something like Form Inheritance?  I may be looking for something like this as well so that all my forms in an application look consistent without me having to go thru the same steps over and over.

Either that or maybe the ability to drop a component on the forms that need this type of functionality.
If I get this to work as a component I could share it with others - but I'd be doing it mostly for myself to make the start of future projects easier.

The project templates might be good as well - just not sure how well one can distribute that? (still have to do some reading on that per the link provided by Leledumbo)

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
Re: Custom Form: How to make this a component?
« Reply #5 on: July 29, 2012, 07:36:22 pm »
http://wiki.freepascal.org/Project_Templates

Took me some reading and experimenting but this did the trick ... :)

Unfortunately the documentation is rather limited.

For those who'd like to use this (please be patient with me, I'm totally new to this one - please correct me if needed):

1) You will have to create your own template directory (for example ~/.lazarus/mytemplates) and set this in Lazarus (menu: Tools -> Project Template Options).

2) For stuff to appear you will need at least one template;

- Create a directory in your template directory (for example ~/.lazarus/mytemplates/myfirsttemplate)
- Copy your project files in this new directory (*.lfm, *.pas, *.ico, *.lpi, *.lpr, *.res)

3) create a "project.ini" file in the new template directory.

I think the minimum is something like this:

project.ini:
Code: [Select]
[Variables]

[Project]
Name=OSX Notebook Header Form

Optional:

The nice thing of Project Templates is that it supports variables.

Since a project will probably not be named the same as your example template you will have to do some code editing and file renaming.

Files:
Rename the project file (ico, lpi, lpr, res) to __PROJNAME__.ext (where extension is the original extension of course).
For example: project.lpi becomes __PROJNAME__.lpi.
Note: the content of the lpi file might need some cleaning, for example removing history, reference to files, not sure if the lpi file is even needed.

File content:
For all files that referred to project.lpi etc, change the content of your files  so all "project" references become "__PROJNAME__" (with the quotes).

You can optionally define your own variables, for example to give the "unit.pas" a different filename.
You could define for example __MAINUNIT__ and __MAINFORM__ in project.ini as MAINUNIT and MAINFORM - during creation Lazarus will ask you for these names.
Now every occurrence of __MAINUNIT__ for example will be replaced with the text you provided (avoid special characters and spaces).

Note that this works for all variables; Every occurrence of a variable in filenames and file content will be replaced by what you entered on creation of a new project based on your template. Predefined variables are PROJNAME and PROJDIR but I didn't find a purpose for PROJDIR just yet.

Some examples:

project.ini
Code: [Select]
[Variables]
MAINUNIT=mainunit
MAINFORM=mainform

[Project]
Name=OSX Notebook Header Form
Author=Hansaplast
Description=Lazarus LCL Application modified to make the main form look like a OS X notebook header

__PROJNAME__.lpr:
Code: [Select]
program __PROJNAME__;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms, __MAINUNIT__;

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(T__MAINFORM__, __MAINFORM__);
  Application.Run;
end.

__MAINUNIT__.pas:
Code: [Select]
unit __MAINUNIT__;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  Buttons, StdCtrls, LCLIntf, LCLType, ComCtrls;

type

  { __MAINFORM__ }

  T__MAINFORM__ = class(TForm)

...

procedure T__MAINFORM__.FormPaint(Sender: TObject);
...

//etc

__MAINUNIT__.lfm:
Code: [Select]
object __MAINFORM__: T__MAINFORM__
...


Keep in mind that I threw this text quickly together after playing with Project Templates for the first time, so I'm sure it can be fine tuned ...
Just thought it might be helpful for others.

avra

  • Hero Member
  • *****
  • Posts: 2582
    • Additional info
Re: Custom Form: How to make this a component?
« Reply #6 on: July 30, 2012, 09:08:55 am »
Unfortunately the documentation is rather limited.
Once you are confident with the feature, you might use your experience to expand the Wiki. That would benefit all of us, and other Lazarus users will not waste time on something that was already explored by someone else first.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
Re: Custom Form: How to make this a component?
« Reply #7 on: July 31, 2012, 07:56:32 pm »
I will most certainly try to do that :)

Hansaplast

  • Hero Member
  • *****
  • Posts: 762
  • Tweaking4All.com
    • Tweaking4All
Re: Custom Form: How to make this a component?
« Reply #8 on: August 04, 2012, 06:32:11 pm »
I added this info as a "Getting started" section to the Wiki page with a few minor changes.

Feel free to comment and/or modify - I'm new to the Wiki-editing ... :)

avra

  • Hero Member
  • *****
  • Posts: 2582
    • Additional info
Re: Custom Form: How to make this a component?
« Reply #9 on: August 06, 2012, 02:19:12 am »
Thank you.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018