Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-03

Help clarify use of TApplication with tiOPF example.

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

I just compiled tiOPF and I am going through the examples which are converted from Delphi, and I am using it to understand how Lazarus GUI projects are organized

The first one I tried was this project. The program is supposed creates a dialog which probably depends on the LCL, so in order to get it work I created a new project, added a form to it, triggered the dialog from the form, then proceeded to minimize the app via experimentation until the form was not needed.

Below is the original project source, and following it is the Lazarus project. I guess Delphi projects automatically include some of the units with have to be added explicitly to the Lazarus project.

program Demo_LoadPersistenceLayerIfDef;
Show quoted text 21 lines
// Adding a {$DEFINE LINK_???} to your project, either in the DPR // or the Project | Options - Conditional Defines dialog, // the specified persistence layers will be linked and loaded. // // When you run this demo, a dialog will show listing // the loaded persistence layers. (Note, the dialog will // say the database is not connected - wich is correct.) // Take a look at tiOPFManager about line 215 you will see how // this the defines are implemented. {$R *.res} uses DemoDBUtils in '..\Common\DemoDBUtils.pas'; begin ShowConnectedDatabases; end.

Here is the pared down Lazarus equivalent

program Demo_LoadPersistenceLayerIfDef_Laz;
Show quoted text 23 lines
{$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms, //frmMain, LResources, { you can add units after this } DemoDBUtils in '..\Common\DemoDBUtils.pas'; {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} begin {$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} Application.Initialize; //Application.CreateForm(TForm1, Form1); //Application.Run; DemoDBUtils.ShowConnectedDatabases; end.

I want to know if in order to display the dialog the project could be pared down further.
Is there some way to get the dialog to show if the project did not use TApplication?

Are the .rc files and .lrs files also required?

below is the DemoDBUtils unit for more clarification

unit DemoDBUtils;
Show quoted text 42 lines
interface procedure ShowConnectedDatabases; implementation uses tiOPFManager ,tiUtils ,tiDialogs ,SysUtils ; procedure ShowConnectedDatabases; var i: integer; LS: string; begin LS:= ''; for i:= 0 to GTIOPFManager.PersistenceLayers.Count - 1 do begin if LS '' then LS:= LS + Cr; if Trim(GTIOPFManager.PersistenceLayers.Items[i].DBConnectionPools.DetailsAsString) = '' then LS:= LS + 'Persistence layer: "'+ GTIOPFManager.PersistenceLayers.Items[i].PersistenceLayerName + '" loaded, but not connected to a database.' + Cr else LS:= LS + GTIOPFManager.PersistenceLayers.Items[i].DBConnectionPools.DetailsAsString + Cr end; if LS '' then tiAppMessage(LS) else tiAppMessage('No persistence layers loaded'); end; end.
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110312/646bc34c/attachment.html>

Reply by email

Sven Barth
to Frank Church
Frank Church wrote 12 lines
Here is the pared down Lazarus equivalent program Demo_LoadPersistenceLayerIfDef_Laz; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms, //frmMain,

This does not include frmMain, but the common form ancestor TForm and also the global Application variable and its TApplication type.

LResources,

This is needed for the inclusion of LRS files. This is not needed if RES files are used (don't know whether your version of Lazarus supports that already).

Show quoted text 19 lines
{ you can add units after this } DemoDBUtils in '..\Common\DemoDBUtils.pas'; {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} begin {$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} Application.Initialize; //Application.CreateForm(TForm1, Form1); //Application.Run; DemoDBUtils.ShowConnectedDatabases; end. I want to know if in order to display the dialog the project could be pared down further. Is there some way to get the dialog to show if the project did not use TApplication?

You need to initialize and maybe also run the Application so that the main loop is executed and thus events are passed to the dialog. Note: when you put DemoDBUtils.ShowConnectedDatabases below Application.Run the call won't be executed until the application is terminated, because the main event processing loop is running in there.

Are the .rc files and .lrs files also required?

If you don't need an application icon, version info or a manifest (on Windows XP and newer) then you don't need these resources.

The following is the main project source if you create a new application in Lazarus 0.9.31 (please note, that Lazarus does not use LRS resources by default anymore).

==== source begin ====

program Project1;
{$mode objfpc}{$H+}

uses

   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Interfaces, // this includes the LCL widgetset
   Forms, Unit1
   { you can add units after this };
{$R *.res}
begin
   RequireDerivedFormResource := True;
   Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   Application.Run;
end.

==== source end ====

I personally would suggest you to put a startup dialog into the applications OnShow or OnActivate event, because this is guarantied to be cross platform, while showing a dialog without the main loop might only work on Windows.

Regards,
Sven

Reply by email

Frank Church

---------- Forwarded message ----------

Earlier message 164 lines
From: Frank Church Date: 12 March 2011 22:31 Subject: Re: [Lazarus] Help clarify use of TApplication with tiOPF example. To: Sven Barth On 12 March 2011 19:55, Sven Barth wrote: > On 12.03.2011 16:01, Frank Church wrote: > >> Here is the pared down Lazarus equivalent >> >> program Demo_LoadPersistenceLayerIfDef_Laz; >> >> {$mode objfpc}{$H+} >> >> uses >> {$IFDEF UNIX}{$IFDEF UseCThreads} >> cthreads, >> {$ENDIF}{$ENDIF} >> Interfaces, // this includes the LCL widgetset >> Forms, //frmMain, >> > > This does not include frmMain, but the common form ancestor TForm and also > the global Application variable and its TApplication type. > > LResources, >> > > This is needed for the inclusion of LRS files. This is not needed if RES > files are used (don't know whether your version of Lazarus supports that > already). > > > { you can add units after this } >> DemoDBUtils in '..\Common\DemoDBUtils.pas'; >> >> {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} >> >> begin >> {$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} >> Application.Initialize; >> //Application.CreateForm(TForm1, Form1); >> //Application.Run; >> DemoDBUtils.ShowConnectedDatabases; >> end. >> >> >> I want to know if in order to display the dialog the project could be >> pared down further. >> Is there some way to get the dialog to show if the project did not use >> TApplication? >> >> > You need to initialize and maybe also run the Application so that the main > loop is executed and thus events are passed to the dialog. > Note: when you put DemoDBUtils.ShowConnectedDatabases below Application.Run > the call won't be executed until the application is terminated, because the > main event processing loop is running in there. > > > Are the .rc files and .lrs files also required? >> >> > If you don't need an application icon, version info or a manifest (on > Windows XP and newer) then you don't need these resources. > > The following is the main project source if you create a new application in > Lazarus 0.9.31 (please note, that Lazarus does not use LRS resources by > default anymore). > > ==== source begin ==== > > program Project1; > > > {$mode objfpc}{$H+} > > uses > {$IFDEF UNIX}{$IFDEF UseCThreads} > cthreads, > {$ENDIF}{$ENDIF} > Interfaces, // this includes the LCL widgetset > Forms, Unit1 > { you can add units after this }; > > {$R *.res} > > begin > RequireDerivedFormResource := True; > > Application.Initialize; > Application.CreateForm(TForm1, Form1); > Application.Run; > end. > > ==== source end ==== > > I personally would suggest you to put a startup dialog into the > applications OnShow or OnActivate event, because this is guarantied to be > cross platform, while showing a dialog without the main loop might only work > on Windows. > > Regards, > Sven > I am using 0.9.28,3 with FPC 2,4,0 on Window. I further reduced it to this program Demo_LoadPersistenceLayerIfDef_Laz; > > {$mode objfpc}{$H+} > > uses > {$IFDEF UNIX}{$IFDEF UseCThreads} > cthreads, > {$ENDIF}{$ENDIF} > Interfaces, // this includes the LCL widgetset > Forms, //frmMain, > //LResources, > > DemoDBUtils in '..\Common\DemoDBUtils.pas'; > > {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} > > begin > //{$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} > Application.Initialize; > //Application.CreateForm(TForm1, Form1); > //Application.Run; > ShowConnectedDatabases; > end. > Thanks for your response. And it looks like Application requires Interfaces and Forms, and from what you describe the ShowConnectedDatabases is probably only working at the end of the program's execution. Is it possible that a GUI app will work without TApplication on Windows, or at the least one that requires only the dialog? -- Frank Church ======================= http://devblog.brahmancreations.com -- Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110312/843b44fb/attachment-0001.html>

Reply by email

Sven Barth
to Frank Church
Frank Church wrote 19 lines
I am using 0.9.28,3 with FPC 2,4,0 on Window. I further reduced it to this program Demo_LoadPersistenceLayerIfDef_Laz; {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} Interfaces, // this includes the LCL widgetset Forms, //frmMain, //LResources, DemoDBUtils in '..\Common\DemoDBUtils.pas'; {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF}

You can disable this one, too.

Show quoted text 15 lines
begin //{$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} Application.Initialize; //Application.CreateForm(TForm1, Form1); //Application.Run; ShowConnectedDatabases; end. Thanks for your response. And it looks like Application requires Interfaces and Forms, and from what you describe the ShowConnectedDatabases is probably only working at the end of the program's execution.

As you've disabled the "Application.Run" call it will work immediatly (if at all), because the potentially endless main loop is not started.

Is it possible that a GUI app will work without TApplication on Windows, or at the least one that requires only the dialog?

You can try to disable the "Application.Initialize" call as well to see what happens, but I can't tell what the Win32 LCL needs to show a simple dialog...

Note: If you can successfully remove this call, you can remove the usage of unit "Forms" as well. The unit "Interfaces" must be kept though and if you don't need threads (on non-Windows) you can remove the part containing "cthreads" as well.

Regards,
Sven

Reply by email

Frank Church
to Sven Barth
Sven Barth wrote 62 lines
Am 13.03.2011 00:22, schrieb Frank Church: I am using 0.9.28,3 with FPC 2,4,0 on Window. > > I further reduced it to this > > program Demo_LoadPersistenceLayerIfDef_Laz; > > {$mode objfpc}{$H+} > > uses > {$IFDEF UNIX}{$IFDEF UseCThreads} > cthreads, > {$ENDIF}{$ENDIF} > Interfaces, // this includes the LCL widgetset > Forms, //frmMain, > //LResources, > > DemoDBUtils in '..\Common\DemoDBUtils.pas'; > > {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} > You can disable this one, too. > begin > //{$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} > Application.Initialize; > //Application.CreateForm(TForm1, Form1); > //Application.Run; > ShowConnectedDatabases; > end. > > > Thanks for your response. > > And it looks like Application requires Interfaces and Forms, and from > what you describe the ShowConnectedDatabases is probably only working at > the end of the program's execution. > As you've disabled the "Application.Run" call it will work immediatly (if at all), because the potentially endless main loop is not started. Is it possible that a GUI app will > work without TApplication on Windows, or at the least one that requires > only the dialog? > You can try to disable the "Application.Initialize" call as well to see what happens, but I can't tell what the Win32 LCL needs to show a simple dialog... Note: If you can successfully remove this call, you can remove the usage of unit "Forms" as well. The unit "Interfaces" must be kept though and if you don't need threads (on non-Windows) you can remove the part containing "cthreads" as well.

Application.Initialize is necessary in this case. The dialog does not come up unless it is executed.

I will try it in 0.9.31 and FPC 2.5.1 on both Windows and Win32 to find out.

Show quoted text 9 lines
Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110313/bcf87834/attachment.html>

Reply by email

Frank Church
to Frank Church
Frank Church wrote 88 lines
On 13 March 2011 08:43, Sven Barth wrote: > Am 13.03.2011 00:22, schrieb Frank Church: > > I am using 0.9.28,3 with FPC 2,4,0 on Window. >> >> I further reduced it to this >> >> program Demo_LoadPersistenceLayerIfDef_Laz; >> >> {$mode objfpc}{$H+} >> >> uses >> {$IFDEF UNIX}{$IFDEF UseCThreads} >> cthreads, >> {$ENDIF}{$ENDIF} >> Interfaces, // this includes the LCL widgetset >> Forms, //frmMain, >> //LResources, >> >> DemoDBUtils in '..\Common\DemoDBUtils.pas'; >> >> {$IFDEF WINDOWS}{$R Demo_LoadPersistenceLayerIfDef_Laz.rc}{$ENDIF} >> > > You can disable this one, too. > > > >> begin >> //{$I Demo_LoadPersistenceLayerIfDef_Laz.lrs} >> Application.Initialize; >> //Application.CreateForm(TForm1, Form1); >> //Application.Run; >> ShowConnectedDatabases; >> end. >> >> >> Thanks for your response. >> >> And it looks like Application requires Interfaces and Forms, and from >> what you describe the ShowConnectedDatabases is probably only working at >> the end of the program's execution. >> > > As you've disabled the "Application.Run" call it will work immediatly (if > at all), because the potentially endless main loop is not started. > > > Is it possible that a GUI app will >> work without TApplication on Windows, or at the least one that requires >> only the dialog? >> > > You can try to disable the "Application.Initialize" call as well to see > what happens, but I can't tell what the Win32 LCL needs to show a simple > dialog... > > Note: If you can successfully remove this call, you can remove the usage > of unit "Forms" as well. The unit "Interfaces" must be kept though and if > you don't need threads (on non-Windows) you can remove the part containing > "cthreads" as well. > > Application.Initialize is necessary in this case. The dialog does not come up unless it is executed. I will try it in 0.9.31 and FPC 2.5.1 on both Windows and Win32 to find out. > Regards, > Sven > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus >

More on the Application object

Will all the lines that come after Application.Run command execute only after the Application is terminated? The ShowConnectedDatabases does not execute if placed after the Application.Run command.

From what you mentioned earlier is that what the event loop is about? ie

after Application.Initialize the forms can be created, then event loop begins to process UI and other events?

Does that mean after the forms and data module objects are created, they will not respond to GUI events until Application.Run is executed?

I suspect that non GUI events can still be processed, without the Application.Run command such as webservers and other data module activities??

Show quoted text 6 lines
-- Frank Church ======================= http://devblog.brahmancreations.com
Frank Church ======================= http://devblog.brahmancreations.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110313/5362dfc7/attachment.html>

Reply by email