[Lazarus] Some thoughts about the global 'Application' instance.

Martin Schreiber fpmse at bluewin.ch
Sun Jun 21 13:16:31 CEST 2009


On Saturday 20 June 2009 16:11:12 Martin Friebe wrote:

> The best way to avoid it, is or the author of the program only to use
> units that do not instantiate an application, other than the one(s) needed.
>
MSEgui has tapplication and tguiapplication which both inherit from 
tcustomapplication. tcustomapplication implements the base funcionalitiy, the 
eventloop functions are abstract.
tapplication implements a GUI independent eventloop, tguiapplication a GUI 
eventloop and further GUI application functionality.
tcustomapplication is defined in unit mseapplication, tapplication in 
msenogui, tguiapplication in msegui.

In a GUI application add msegui to uses of the mainprogram, in an application 
without GUI dependency add msenogui to uses of the mainprogram.
In a unit with GUI use msegui.application, in a unit without GUI dependence 
use mseapplication.application.

msenogui registers tnoguiapplication in initialization:
"
initialization
 registerapplicationclass(tnoguiapplication);
"
msegui registers tinternalguiapplication and returns a tguiapplication 
instance in application function:
"
function application: tguiapplication;
begin
 if appinst = nil then begin
  tinternalapplication.create(nil);
 end;
 result:= appinst;
end;
"
in order to avoid circular unit references in msegui it is a little bit 
complicated. ;-)

tinternalapplication.create() sets msegui.appinst variable:
"
constructor tinternalapplication.create(aowner: tcomponent);
begin
 appinst:= self;
 inherited;
"
The inherited tcustomapplication.create() sets mseapplication.appinst which is 
actually a copy of msegui.appinst:
"
constructor tcustomapplication.create(aowner: tcomponent);
begin
 if appinst <> nil then begin
  raise exception.create('Application already created.');
 end;
 appinst:= self;
"
mseapplication.application returns a tcustomapplication instance:
"
function application: tcustomapplication;
begin
 if appinst = nil then begin
  if appclass = nil then begin
   raise exception.create('No application class registered.');
  end;
  appclass.create(nil);
 end;
 result:= appinst;
end;
"

Martin




More information about the Lazarus mailing list