Sign in Join
Wiki copytemporary

Lazarus Examples Window: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11The new Example Window that exists in Lazarus 3.0 and beyond is documented below.
22
33## For the end user
44
55### Introduction
66
77A Lazarus user gets to the Example Window after clicking the button on the Project Wizard screen or from the Tools Menu, "Example Projects". In either case you see a window offering a list of known examples. The list can be filtered by key words or by category (double click between category checkboxes turns all off). In basic use, it shows you all the examples shipped with Lazarus.
88
99The Example Window will allow you to open, build (and edit) and run any example but note some have specific package requirements. When opened, the examples from the Lazarus Source code are copied to a (user writable) area in the Lazarus Config directory ([[pcp|PCP]]) because, on some systems Lazarus Source is in read-only disk space.
1010
1111Feel free to edit or make changes to examples in Lazarus Source, if you totally mess up, its easily refreshed from the Examples Window.
1212
1313### Examples in Packages
1414
1515When you install a package that has compliant examples, they will also appear in the Lazarus Examples Window. If they don't appear, the most likely reason is that they do not have an example meta data file. See below.
1616
1717### Displaying your own Examples
1818
1919If you have your own examples in one or more Lazarus projects, you can arrange to have them listed along with the built in examples supplied with Lazarus. Following the same rules as below, you would -
2020
2121- Ensure each example is in its own directory and is self contained.
2222- Provide meta data files as described below. Perhaps set the category to something like "Private" ?
2323- Add an entry to the ./examples/examples.txt file in your Lazarus Source directory. The entry, see the existing ones, should have a relative path from the top of the lazarus source directory to your meta data file. If you are using a packaged version of Lazarus then this examples.txt file might be in read only space and you will need administrative access to it.
2424- Examples mentioned in examples.txt are always copied to a work area ensuring write access and allowing you to play without affecting the original.
2525
2626### FPC Examples
2727
2828FPC also provides a number of examples relating to its packages and RTL. These examples are generally command line driven and don't have the Lazarus project information so are unsuited to working with the Lazarus Examples Window. (It might be nice to find a way to display them however.)
2929
3030## For the Developer
3131
3232### Examples in the Lazarus Source
3333
3434You can add an example anywhere you like in the Lazarus Source Tree, there are lots under ~/examples but also many associated with different Lazarus sub system. A few rules apply -
3535
3636- The Example Name must be unique within Lazarus (when lowercased).
3737- The example should be self contained within its own directory. That directory (and any sub directories) will be copied to the work area so don't assume particular files are "just up one dir".
3838- The example should have an Example Metadata File, with a file name that corresponds to the project name and an extension of .ex-meta. The content of this file is JSON and must have a name, a category and should have a (multiline) description and keywords. See below.
3939
4040If you add a new example (or remove one) to the Lazarus Source Tree, the file ~/examples/examples.txt needs to be refreshed. Its plain text, edit it directly or generate a new one with this command (on a *nix )-
4141
42-`$> find . -name "*.ex-meta" > examples/examples.txt`
42+```text
43+$> find . -name "*.ex-meta" > examples/examples.txt
44+```
4345
4446### Examples in Third Party Packages
4547
4648Again, you are free to place your package examples directory where you like but in order for the Lazarus Examples Window to find it, some things must be provided, note, different rules than above !
4749
4850- The package must be currently installed in Lazarus (check ? see if its listed in ([[pcp|PCP]])/staticpackages.inc)
4951- Your example should be in a directory of its own. You should not put more than one example in the same directory.
5052- In the case of Package Examples, the example directory is not moved to a work area so it can (but perhaps should not) be dependent on local, relative path files.
5153- It must have a Metadata File, see below.
5254- The Package file, that is the .lpk file must have entry such as \<ExamplesDirectory Value="../demo"/\>, typically just below the Author item. The value is the relative path from the .lpk file to a directory containing your example or examples. eg -
5355
54-`[MyPackage]`\
55-`    [demo]`\
56-`        mypackage_demo1.ex-meta`\
57-`        ....`\
58-`        [demo2]`\
59-`            mypackage_demo2.ex-meta`\
60-`            ....`\
61-`    [package]`\
62-`        mypackage.lpk`
56+```text
57+[MyPackage]
58+ [demo]
59+ mypackage_demo1.ex-meta
60+ ....
61+ [demo2]
62+ mypackage_demo2.ex-meta
63+ ....
64+ [package]
65+ mypackage.lpk
66+```
6367
6468(Personally, I would put demo1 in its own directory too but the above would work !)
6569
6670Note that, at present, Lazarus, when making a package, does NOT create the ExamplesDirectory entry, it will, but its a work in progress. If you want to test, manually adding that entry is the way to go right now.
6771
6872### The Metadata File
6973
7074> **Note:** If you add a new example (or remove one) to the Lazarus Source Tree, the file ~/examples/examples.txt needs to be refreshed. Its plain text, edit it directly or generate a new one with this command from the top level Lazarus directory (on a *nix )-
7175>
7276> `$> find . -name "*.ex-meta" > examples/examples.txt`
7377
7478All examples that appear in the Example Window have a json metadata file with an extension ".ex-meta" in the top level directory of the example (not the package). Without a valid metadata file, the example will not be listed in the Example Window. An individual example project file would look like this -
7579
7680```text
7781{
7882"Laz_Hello" : {
7983 "Category" : "Beginner",
8084 "Keywords" : ["Lazarus", "Hello World", "TButton", "TMemo"],
8185 "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."
8286}
8387```
8488
8589In this case, the example project files would be in a subdirectory called laz_hello, same spelling but all lowercase consistent with Lazarus.
8690
8791The three fields are free form text, there is no dictionary so, you are free to introduce both new Categories and new Key Words. Its desirable that we do not have too many Categories so, perhaps consider posting a message on the forum before using a new one ? At present, the following Categories are in use -
8892
8993- Beginner
9094- General
9195- TAChart
9296- DBase
9397- LazReport
9498- ThirdParty
9599
96100There is a rough and ready GUI app to make, edit and importantly, validate metadata files available at <https://github.com/davidbannon/ExampleMetaData> .
97101
98102### Can the IDE find the example ?
99103
100104If the IDE does not find an example, some things to check for -
101105
102106- Does it have a metadata file ?
103107- Is there a message about the metadata from console ?
104108- If its a Lazarus Source example, is it mentioned in examples/examples.txt ?
105109- if a Package Example, is it mentioned in ([[pcp|PCP]])/staticpackages.inc ? and in ([[pcp|PCP]])/packagefiles.xml ? (Unlike the examples.txt file, do not manually edit these files, they should be updated when a package is installed into the the IDE.)
106110
107111## Notes
108112
109113The user can view the project content and, if they choose, build the project. Later, if the user users the Example Window to again open that project, they will be asked if they want to refresh it, doing so will erase any false edits they may have made.
110114
111115Important to note -
112116
113117- Example Projects will not be displayed in the Examples Window unless they have a valid metadata file. An error is dropped to the console if a metadata file with bad json is encountered.
114118- The original copy of the Example is not altered when the Example Window is used to open an Example. So, play away !
115119- New examples can, potentially be added via the normal Gitlab pull request system.
116120- When examples are added to or removed from the Lazarus Source, changes need be made to the (eg) ~/lazarus_3_0/examples/examples.txt file. See the Metadata section.
117121- Examples demonstrating aspects of third party packages are probably best added to that package rather than to Lazarus itself.
118122
119123## See also
120124
121125- <https://forum.lazarus.freepascal.org/index.php/topic,57680.0.html>
122126- <https://gitlab.com/freepascal.org/lazarus/lazarus/-/issues/37509>
123127- <https://gitlab.com/dbannon/laz_examples> - this is just a temp home.
124128- <https://gitlab.com/dbannon/laz_examples/-/tree/main/Utility/ExScanner> - a tool for manipulating Example Projects in and around the Lazarus Src Tree. Probably past its use by date. Also has a framework to 'exercise' the Lazarus Examples Window in a stand alone, easier to manage environment.
125129- <https://github.com/davidbannon/ExampleMetaData> - a very simple editor to make and validate the Example Meta Data files.
126130- <https://gitlab.com/freepascal.org/lazarus/lazarus/-/blob/main/components/exampleswindow/uexampledata.pas> the unit making decisions about your examples.
127131
128132---
129133*From the Free Pascal wiki, page "Lazarus Examples Window", as it stood on 2024-01-28 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
130134Category: Development
131135Category: Example programs
132136Category: Proposals

Back to history