mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-22 13:59:22 +02:00
Examples: Add text editor sample project. Add more comments to the Laz_Hello demo project. MR !454 by Aruna Hewapathirane.
This commit is contained in:
parent
5ed7be5ee4
commit
e26c30e20c
@ -207,3 +207,4 @@
|
|||||||
./examples/canvas_test/canvas_test.ex-meta
|
./examples/canvas_test/canvas_test.ex-meta
|
||||||
./examples/cleandir/cleandir.ex-meta
|
./examples/cleandir/cleandir.ex-meta
|
||||||
./examples/fontenum/fontenum.ex-meta
|
./examples/fontenum/fontenum.ex-meta
|
||||||
|
./examples/texteditor/texteditor.ex-meta
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
<Filename Value="unit1.pas"/>
|
<Filename Value="unit1.pas"/>
|
||||||
<IsPartOfProject Value="True"/>
|
<IsPartOfProject Value="True"/>
|
||||||
<ComponentName Value="Form1"/>
|
<ComponentName Value="Form1"/>
|
||||||
|
<HasResources Value="True"/>
|
||||||
<ResourceBaseClass Value="Form"/>
|
<ResourceBaseClass Value="Form"/>
|
||||||
<UnitName Value="Unit1"/>
|
<UnitName Value="Unit1"/>
|
||||||
</Unit1>
|
</Unit1>
|
||||||
@ -56,6 +57,9 @@
|
|||||||
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
|
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
|
||||||
</SearchPaths>
|
</SearchPaths>
|
||||||
<Linking>
|
<Linking>
|
||||||
|
<Debugging>
|
||||||
|
<DebugInfoType Value="dsDwarf3"/>
|
||||||
|
</Debugging>
|
||||||
<Options>
|
<Options>
|
||||||
<Win32>
|
<Win32>
|
||||||
<GraphicApplication Value="True"/>
|
<GraphicApplication Value="True"/>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{ "Laz_Hello" : {
|
{ "Laz_Hello" : {
|
||||||
"Category" : "Beginner",
|
"Category" : "Beginner",
|
||||||
"Keywords" : ["Lazarus","Hello World","TButton","TMemo"],
|
"Keywords" : ["Lazarus","Hello World","TButton","TMemo"],
|
||||||
"Description" : "This might be your first Lazarus project, its the traditional Hello World. Two buttons have been dropped on the form, renamed and their captions set. A memo was then added and each button was double clicked and what they are expected to do was set."}
|
"Description" : "This might be your first Lazarus project, its the traditional 'Hello World' project.\n\nTwo buttons have been dropped on the form, renamed and their captions set. A memo was then added and each button was double clicked and what they are expected to do was set.\n\nMany comments are provided to explain the instructions used."}
|
||||||
}
|
}
|
@ -6,15 +6,15 @@ object Form1: TForm1
|
|||||||
Caption = 'Form1'
|
Caption = 'Form1'
|
||||||
ClientHeight = 226
|
ClientHeight = 226
|
||||||
ClientWidth = 467
|
ClientWidth = 467
|
||||||
LCLVersion = '2.3.0.0'
|
LCLVersion = '4.99.0.0'
|
||||||
object ButtonHello: TButton
|
object ButtonHello: TButton
|
||||||
Left = 40
|
Left = 40
|
||||||
Height = 41
|
Height = 41
|
||||||
Top = 160
|
Top = 160
|
||||||
Width = 103
|
Width = 103
|
||||||
Caption = 'Say Hello'
|
Caption = 'Say Hello'
|
||||||
OnClick = ButtonHelloClick
|
|
||||||
TabOrder = 0
|
TabOrder = 0
|
||||||
|
OnClick = ButtonHelloClick
|
||||||
end
|
end
|
||||||
object ButtonClose: TButton
|
object ButtonClose: TButton
|
||||||
Left = 272
|
Left = 272
|
||||||
@ -22,8 +22,8 @@ object Form1: TForm1
|
|||||||
Top = 160
|
Top = 160
|
||||||
Width = 91
|
Width = 91
|
||||||
Caption = 'Close'
|
Caption = 'Close'
|
||||||
OnClick = ButtonCloseClick
|
|
||||||
TabOrder = 1
|
TabOrder = 1
|
||||||
|
OnClick = ButtonCloseClick
|
||||||
end
|
end
|
||||||
object Memo1: TMemo
|
object Memo1: TMemo
|
||||||
Left = 24
|
Left = 24
|
||||||
|
@ -1,45 +1,60 @@
|
|||||||
unit Unit1;
|
unit Unit1; // Declares the unit name. Each Pascal unit must have a unique name.
|
||||||
|
|
||||||
{$mode objfpc}{$H+}
|
{$mode objfpc}{$H+} // Use the Free Pascal Object Pascal syntax and enable long strings (AnsiString).
|
||||||
|
|
||||||
interface
|
interface // Start of the interface section — defines what’s visible to other units.
|
||||||
|
|
||||||
uses
|
uses
|
||||||
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
|
// These are standard units that provide core functionality:
|
||||||
|
Classes, // - Classes & SysUtils: core data types and utilities.
|
||||||
|
SysUtils,
|
||||||
|
Forms, // - Forms: GUI form definitions.
|
||||||
|
Controls, // - Controls: inner functionality of controls
|
||||||
|
Graphics, // - Graphics: drawing and colors.
|
||||||
|
Dialogs, // - Dialogs: message dialogs.
|
||||||
|
StdCtrls; // - StdCtrls: standard controls like TButton, TMemo, TLabel.
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
{ TForm1 }
|
{ TForm1 }
|
||||||
|
|
||||||
TForm1 = class(TForm)
|
TForm1 = class(TForm) // This declares a form class named TForm1, inheriting from TForm (a window).
|
||||||
ButtonHello: TButton;
|
ButtonHello: TButton; // A button named ButtonHello — clicking this will show “Hello World” in the memo.
|
||||||
ButtonClose: TButton;
|
ButtonClose: TButton; // A button named ButtonClose — clicking this will close the form.
|
||||||
Memo1: TMemo;
|
Memo1: TMemo; // A TMemo component named Memo1 — used to display and edit multiple lines of text.
|
||||||
procedure ButtonCloseClick(Sender: TObject);
|
procedure ButtonCloseClick(Sender: TObject); // This procedure handles clicks on the Close button.
|
||||||
procedure ButtonHelloClick(Sender: TObject);
|
procedure ButtonHelloClick(Sender: TObject); // This procedure handles clicks on the Hello button.
|
||||||
|
|
||||||
private
|
private
|
||||||
|
// Private section for internal fields or methods (not used here).
|
||||||
|
|
||||||
public
|
public
|
||||||
|
// Public section for fields/methods accessible by other units (not used here).
|
||||||
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
var
|
var
|
||||||
Form1: TForm1;
|
Form1: TForm1; // This declares the global instance of the form.
|
||||||
|
|
||||||
implementation
|
implementation // Implementation section starts here — contains the actual code of the methods.
|
||||||
|
|
||||||
{$R *.lfm}
|
{$R *.lfm} // This links the form's design file (.lfm) into the unit at compile time.
|
||||||
|
|
||||||
{ TForm1 }
|
{ TForm1 } // This just tells you: "Hey! The following procedures belong to the TForm1 form."
|
||||||
|
|
||||||
|
// This is the handler of the OnClick event of the ButtonHello.
|
||||||
|
// "Event handler" means: The associated code is executed whenever the
|
||||||
|
// corresponding "event" is triggered, in this case, when the button is clicked.
|
||||||
procedure TForm1.ButtonHelloClick(Sender: TObject);
|
procedure TForm1.ButtonHelloClick(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
Memo1.Append('Hello World');
|
Memo1.Append('Hello World'); // Appends the string 'Hello World' as a new line at the end of the memo.
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
// This is the handler of the OnClick event of the ButtonClose.
|
||||||
|
// The event fires when the ButtonClose is clicked.
|
||||||
procedure TForm1.ButtonCloseClick(Sender: TObject);
|
procedure TForm1.ButtonCloseClick(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
close;
|
Close; // Closes the form (and ends the application if it's the main form).
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
78
examples/texteditor/project1.lpi
Normal file
78
examples/texteditor/project1.lpi
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<CONFIG>
|
||||||
|
<ProjectOptions>
|
||||||
|
<Version Value="12"/>
|
||||||
|
<General>
|
||||||
|
<SessionStorage Value="InProjectDir"/>
|
||||||
|
<Title Value="project1"/>
|
||||||
|
<Scaled Value="True"/>
|
||||||
|
<ResourceType Value="res"/>
|
||||||
|
<UseXPManifest Value="True"/>
|
||||||
|
<XPManifest>
|
||||||
|
<DpiAware Value="True"/>
|
||||||
|
</XPManifest>
|
||||||
|
</General>
|
||||||
|
<BuildModes>
|
||||||
|
<Item Name="Default" Default="True"/>
|
||||||
|
</BuildModes>
|
||||||
|
<PublishOptions>
|
||||||
|
<Version Value="2"/>
|
||||||
|
<UseFileFilters Value="True"/>
|
||||||
|
</PublishOptions>
|
||||||
|
<RunParams>
|
||||||
|
<FormatVersion Value="2"/>
|
||||||
|
</RunParams>
|
||||||
|
<RequiredPackages>
|
||||||
|
<Item>
|
||||||
|
<PackageName Value="LCL"/>
|
||||||
|
</Item>
|
||||||
|
</RequiredPackages>
|
||||||
|
<Units>
|
||||||
|
<Unit>
|
||||||
|
<Filename Value="project1.lpr"/>
|
||||||
|
<IsPartOfProject Value="True"/>
|
||||||
|
</Unit>
|
||||||
|
<Unit>
|
||||||
|
<Filename Value="unit1.pas"/>
|
||||||
|
<IsPartOfProject Value="True"/>
|
||||||
|
<ComponentName Value="Form1"/>
|
||||||
|
<HasResources Value="True"/>
|
||||||
|
<ResourceBaseClass Value="Form"/>
|
||||||
|
<UnitName Value="Unit1"/>
|
||||||
|
</Unit>
|
||||||
|
</Units>
|
||||||
|
</ProjectOptions>
|
||||||
|
<CompilerOptions>
|
||||||
|
<Version Value="11"/>
|
||||||
|
<Target>
|
||||||
|
<Filename Value="project1"/>
|
||||||
|
</Target>
|
||||||
|
<SearchPaths>
|
||||||
|
<IncludeFiles Value="$(ProjOutDir)"/>
|
||||||
|
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
|
||||||
|
</SearchPaths>
|
||||||
|
<Linking>
|
||||||
|
<Debugging>
|
||||||
|
<DebugInfoType Value="dsDwarf3"/>
|
||||||
|
</Debugging>
|
||||||
|
<Options>
|
||||||
|
<Win32>
|
||||||
|
<GraphicApplication Value="True"/>
|
||||||
|
</Win32>
|
||||||
|
</Options>
|
||||||
|
</Linking>
|
||||||
|
</CompilerOptions>
|
||||||
|
<Debugging>
|
||||||
|
<Exceptions>
|
||||||
|
<Item>
|
||||||
|
<Name Value="EAbort"/>
|
||||||
|
</Item>
|
||||||
|
<Item>
|
||||||
|
<Name Value="ECodetoolError"/>
|
||||||
|
</Item>
|
||||||
|
<Item>
|
||||||
|
<Name Value="EFOpenError"/>
|
||||||
|
</Item>
|
||||||
|
</Exceptions>
|
||||||
|
</Debugging>
|
||||||
|
</CONFIG>
|
24
examples/texteditor/project1.lpr
Normal file
24
examples/texteditor/project1.lpr
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
program project1;
|
||||||
|
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
uses
|
||||||
|
{$IFDEF UNIX}
|
||||||
|
cthreads,
|
||||||
|
{$ENDIF}
|
||||||
|
{$IFDEF HASAMIGA}
|
||||||
|
athreads,
|
||||||
|
{$ENDIF}
|
||||||
|
Interfaces, // this includes the LCL widgetset
|
||||||
|
Forms, Unit1;
|
||||||
|
|
||||||
|
{$R *.res}
|
||||||
|
|
||||||
|
begin
|
||||||
|
RequireDerivedFormResource:=True;
|
||||||
|
Application.Scaled:=True;
|
||||||
|
Application.Initialize;
|
||||||
|
Application.CreateForm(TForm1, Form1);
|
||||||
|
Application.Run;
|
||||||
|
end.
|
||||||
|
|
5
examples/texteditor/texteditor.ex-meta
Normal file
5
examples/texteditor/texteditor.ex-meta
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{ "texteditor" : {
|
||||||
|
"Category" : "Beginner",
|
||||||
|
"Keywords" : ["TMemo", "editor"],
|
||||||
|
"Description" : "Text editor example with TMemo and basic file menu operations (New, Open, Save, Exit)" }
|
||||||
|
}
|
56
examples/texteditor/unit1.lfm
Normal file
56
examples/texteditor/unit1.lfm
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
object Form1: TForm1
|
||||||
|
Left = 467
|
||||||
|
Height = 600
|
||||||
|
Top = 256
|
||||||
|
Width = 800
|
||||||
|
Caption = 'Lazarus Text Editor'
|
||||||
|
ClientHeight = 600
|
||||||
|
ClientWidth = 800
|
||||||
|
Menu = MainMenu1
|
||||||
|
LCLVersion = '4.99.0.0'
|
||||||
|
OnCreate = FormCreate
|
||||||
|
object Memo1: TMemo
|
||||||
|
Left = 0
|
||||||
|
Height = 600
|
||||||
|
Top = 0
|
||||||
|
Width = 800
|
||||||
|
Align = alClient
|
||||||
|
Font.Height = -13
|
||||||
|
Font.Name = 'Noto Sans Mono'
|
||||||
|
ParentFont = False
|
||||||
|
ScrollBars = ssAutoBoth
|
||||||
|
TabOrder = 0
|
||||||
|
WordWrap = False
|
||||||
|
end
|
||||||
|
object MainMenu1: TMainMenu
|
||||||
|
Left = 24
|
||||||
|
Top = 14
|
||||||
|
object MenuItemFile: TMenuItem
|
||||||
|
Caption = 'File'
|
||||||
|
object MenuItemNew: TMenuItem
|
||||||
|
Caption = 'New'
|
||||||
|
OnClick = MenuItemNewClick
|
||||||
|
end
|
||||||
|
object MenuItemOpen: TMenuItem
|
||||||
|
Caption = 'Open'
|
||||||
|
OnClick = MenuItemOpenClick
|
||||||
|
end
|
||||||
|
object MenuItemSave: TMenuItem
|
||||||
|
Caption = 'Save'
|
||||||
|
OnClick = MenuItemSaveClick
|
||||||
|
end
|
||||||
|
object MenuItemExit: TMenuItem
|
||||||
|
Caption = 'Exit'
|
||||||
|
OnClick = MenuItemExitClick
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
object OpenDialog1: TOpenDialog
|
||||||
|
Left = 368
|
||||||
|
Top = 58
|
||||||
|
end
|
||||||
|
object SaveDialog1: TSaveDialog
|
||||||
|
Left = 503
|
||||||
|
Top = 58
|
||||||
|
end
|
||||||
|
end
|
97
examples/texteditor/unit1.pas
Normal file
97
examples/texteditor/unit1.pas
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
unit Unit1;
|
||||||
|
|
||||||
|
{$mode objfpc}{$H+}
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, StdCtrls;
|
||||||
|
|
||||||
|
type
|
||||||
|
|
||||||
|
{ TForm1 }
|
||||||
|
|
||||||
|
TForm1 = class(TForm)
|
||||||
|
Memo1: TMemo; // Text editing area
|
||||||
|
MainMenu1: TMainMenu; // Main menu bar
|
||||||
|
MenuItemFile: TMenuItem; // 'File' menu
|
||||||
|
MenuItemNew: TMenuItem; // 'New' file
|
||||||
|
MenuItemOpen: TMenuItem; // 'Open' file
|
||||||
|
MenuItemSave: TMenuItem; // 'Save' file
|
||||||
|
MenuItemExit: TMenuItem; // 'Exit' application
|
||||||
|
OpenDialog1: TOpenDialog; // Dialog to select the file to be opened
|
||||||
|
SaveDialog1: TSaveDialog; // Dialog to select the file name for saving
|
||||||
|
procedure FormCreate(Sender: TObject);
|
||||||
|
procedure MenuItemFileClick(Sender: TObject);
|
||||||
|
|
||||||
|
procedure MenuItemExitClick(Sender: TObject);
|
||||||
|
procedure MenuItemNewClick(Sender: TObject);
|
||||||
|
procedure MenuItemOpenClick(Sender: TObject);
|
||||||
|
procedure MenuItemSaveClick(Sender: TObject);
|
||||||
|
private
|
||||||
|
|
||||||
|
public
|
||||||
|
|
||||||
|
end;
|
||||||
|
|
||||||
|
var
|
||||||
|
Form1: TForm1;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
{$R *.lfm} // Links the .lfm form file
|
||||||
|
|
||||||
|
{ TForm1 }
|
||||||
|
|
||||||
|
procedure TForm1.FormCreate(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Optional: Set dialog filters and default file extensions
|
||||||
|
OpenDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
|
||||||
|
SaveDialog1.Filter := 'Text files (*.txt)|*.txt|All files (*.*)|*.*';
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
procedure TForm1.MenuItemExitClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Closes the application
|
||||||
|
Close;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.MenuItemFileClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Shows the file open dialog
|
||||||
|
if OpenDialog1.Execute then
|
||||||
|
begin
|
||||||
|
// Loads file contents into the memo
|
||||||
|
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.MenuItemNewClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Clears the text editor for a new file
|
||||||
|
Memo1.Clear;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.MenuItemOpenClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Shows the file open dialog
|
||||||
|
if OpenDialog1.Execute then
|
||||||
|
begin
|
||||||
|
// Loads file contents into the memo
|
||||||
|
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TForm1.MenuItemSaveClick(Sender: TObject);
|
||||||
|
begin
|
||||||
|
// Shows the file save dialog
|
||||||
|
if SaveDialog1.Execute then
|
||||||
|
begin
|
||||||
|
// Saves memo contents to the selected file
|
||||||
|
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user