Lazarus
Follow https://wiki.freepascal.org/Installing_Lazarus_on_macOS to install Lazarus.
After installation on macOS, the directory
/Applications/Lazarus/examples
contains many examples.
We need to download two files from https://sourceforge.net/projects/lazarus/files/Lazarus%20macOS%20x86-64/:
Lazarus-3.4-macosx-x86_64.pkg
fpc-src-3.2.2-20210709-macosx.dmg
fpc-src
is installed in /usr/local/share/src/fpc-3.2.2
.
The source code of fpc
can also be found at https://github.com/fpc/FPCSource
To reduce .exe
file size:
Select
Project
,Project options...
,compiler options
,Debugging
Uncheck
Generate info for the debugger (slower / increase exe size)
Check
Strip symbols from exectuable
Note: We can create release and debug build mode instead of using the default mode.
See https://wiki.freepascal.org/Mac_Installation_FAQ to fix the following error:
ld: framework not found Cocoa
whole program optimization: https://wiki.freepascal.org/Whole_Program_Optimization#Concrete_Lazarus_IDE_example
Multiplatform Programming Guide
Application Bundle
Marcos
For operating systems:
DARWIN
for macOS and iOSSee https://www.freepascal.org/docs-html/3.2.0/prog/progap7.html#x341-357000G for a complete list
Samples and tutorials
TForm
See https://wiki.lazarus.freepascal.org/Form_Tutorial
To change the caption of the form, assign a string to its Caption
property.
To add a hint to the form, we need to first change its property Hint
and then set property ShowHint
to true.
How to show a second form from the main form?
There are two methods.
The form is created automatically by the application, which is the default behavior. If we want to disable auto creation, then we can either edit
*.lpr
or use menu -> project options -> form, disable auto creating the form.Note that the above code assumes the form is created automatically.
// unit1.pas var r: Integer; begin r := Form2.ShowModal; case r of 1: begin ShowMessage('1'); end; 2: begin ShowMessage('2'); end; 3: begin ShowMessage('3'); end; end; end; // unit2.pas // to exit form2, we can either use self.Close // or use // // Self.ModalResult := 10; // // we can assign any integer value to Self.ModalResult // and it will close the form immediately.https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tmodalresult.html and https://lazarus-ccr.sourceforge.io/docs/lazutils/uitypes/tmodalresult.html and https://fossies.org/linux/lazarus/components/lazutils/uitypes.pas define some constant, e.g.,
mrOK
,mrCancel
, etc.If we use
Self.Close
or click theX
to close the Form, then the returnedModalResult
ismrCancel
, which is 2.
If the form is not created automatically, then we can use
procedure TForm1.Button1Click(Sender: TObject); begin Form2:=TForm2.Create(Nil); //Form2 is created Form2.ShowModal; //Form2 is displayed FreeAndNil(Form2); //Free Form2 end;
How to create a new form dynamically?
See https://wiki.lazarus.freepascal.org/Form_Tutorial#Generate_the_form_dynamically
procedure TForm1.Button1Click(Sender: TObject); var MyForm: TForm; MyButton: TButton; begin MyForm:=TForm.Create(nil); MyForm.SetBounds(100, 100, 220, 150); MyForm.Caption:='My dynamic created form'; MyButton:=TButton.create(MyForm); MyButton.Caption:='Close my form'; MyButton.SetBounds(10, 10, 200, 30); MyButton.Parent:=MyForm; MyButton.OnClick:=@MyButtonClick; MyForm.ShowModal; FreeAndNil(MyForm); end;
Tips for manual generation of controls
TMemo
Support multiline texts.
It has an attributes Lines
, which is of type TStringList
.
Lines.Count
returns number of lines. If it is 0, then
the text is empty. Lines.Add('a new line')
to add a new line.
Lines.Clear()
to clear all lines.
See also https://wiki.lazarus.freepascal.org/TMemo.
Example 1:
// from https://wiki.lazarus.freepascal.org/TMemo
// assign a TStringList to a TMemo
// Note that we have to call Free to free the stringlist
procedure TForm1.Button1Click(Sender: TObject);
var
myStringList: TStringList;
begin
myStringList:=TStringList.Create; //Create my StringList
myStringList.Add('This is the first line.'); //add a line
myStringList.Add('This is the second line.');
myStringList.Add('This is the third line.');
myStringList.Add('etc.');
Memo1.Lines.Assign(myStringList); //assign text content
myStringList.Free; //free my StringList
end;
Example 2:
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Clear; //delete all lines of Memo1
Memo1.Lines.Add('This is the first line.'); //add a line
Memo1.Lines.Add('This is the second line.');
Memo1.Lines.Add('This is the third line.');
Memo1.Lines.Add('etc.');
end;
TListBox
See https://wiki.lazarus.freepascal.org/TListBox.
How to add an item
How to get the selected item
How to get multi-selected items
Dialogs
See https://wiki.lazarus.freepascal.org/Dialog_Examples.
ShowMessage
Application.MessageBox
, we can provide several buttons, ask users to click one button, figure out which button is clicked, e.g., the yes or no button, execute some code.MessageDlg
, likeApplication.MessageBox
but we can specify which buttons to show to users. We should prefer this one.QuestionDlg
: Users can customize the caption of the buttons.There are also dialogues for accepts user inputs.