Browsing this Thread:
1 Anonymous Users
Chapter 2: Good programming habbits makes ... havoc ? |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Chapter 2: Good programming habbits makes ... havoc ?
Please also take a look at: - The Lazarus documentation - A small intro called "Developing a GUI Application with Lazarus" in english by some dutch guy. - ALB42's blog for his progress - azvareths blog for a more graphical approach This chapter is about: - some LCL stuff working out-of-the-box. - i do not understand AROS' shell - even LCL components can have bad design One of the first thing a programmer _should_ do is inform himself about what is happening around in his code. Even more, the user should know that also, so the user can return informative information to the developer when things goes wrong. Well, that is the theoretical part IMHO in practice it mostly means the developer (not you ofcourse) codes and releases the project and the user is often stuck with bad programming habbits and erraneous programs. The user complains and the developer implement workarounds or, in the worst case is unable to determine his mistakes. Yeah yeah i know i have a weird look on things ![]() Why do i tell all this ? LCL has a nice component that can inform both developer and user about what happens in the program. This component is called TLogEvent. AFAIK the component originates from the Windows platform, enabling the programmer to log events into windows event-database. Because it has found its way into the LCL it was adjusted so it could also write to a file on disk instead of into the event-database. That's why i asked myself: Wouldn't it be nice to have something like that in FPC AROS ? It sure beats all those writeln's. So i started testing and it turns out the component works. I did not see ALB42 mentioning anything about this component, so my guess is it's a native lcl component (depending only on internal fpc/lcl code) that works out-of-the-box when the code is compiled. That having said i also mention some caveats upfront. They are: - The logfile in which the events are being logged is continously in a 'open'-state. This means that when your application crashes the logfile is almost always being destroyed in the process. Not only that, it could also mean that during the crash-process the logger is trying to write something to the file resulting in complete drive-havoc. This kind of behaviour is IMHO very bad, and should absolutely not being used. But i am not a lazarus developer. - You have to update your FPC installation, using at least the version dated 26/07/2011 because otherwise the component is unable to write the correct date into the logfile. Luckily for us ALB42 fixed that :thumbsup: plus he added new cool things :even cooler: to try out yourself :sowhatareyouwaitingfor?: In the mean while the good news is that the component is usable, in that it log messages into a logfile so your messages can be added and logged. When compiling my project, i noticed that FPC was presenting me with a new message. The message itself is not so important, but i thought it would be nice to copy that message into a document for reference. So i looked at the shell-menu and under the review menu i could find a menu entery called "save as plain text". So my initial thought was ok, so i can copy the contents of the whole shell into a file on disk. That would be one nice feature to have indeed. So i tried and tried. It just does not work for me. Does anyone knows if this is supppose to work on a vanilla icaros 1.3.1 installation ? So how do we use this component ? 1) Startup lazarus and create a new application. 2) On the form drop a TEventlog Component (you can find it under the system tab) 3) Just to be sure, change the LogType in the properties panel of the objectinspector from ltSystem into ltFile 4) Using the object inspector add 2 events to the main form .OnCreate and .OnDestroy. 5) Add the line EventLog1.Info('AnyMessageYouLike'); to both Events. 6) Compile and run That shoud be easy enough ![]() The actions described above should result in something like this:
unit Unit1;
In order to make this component a bit more explanetory i have made a little example which basicly shows the same thing but a bit more practical. It has comments and all, so that i am able to bore people even more If one would adapt this example then it could even be used in order to aid ALB42 in his development as you can checkout which events and properties are working correctly in the AROS version. If you checkout this example on both windows and AROS you will notice that e.g. the MouseEnter and MouseLeave events are not fired in AROS. If this would be desirable or even possible is a completely other matter ofcourse. So, i will shutup now and paste my code into here for others to see, use and abuse Just to make things clear: i do not take any responsibility whatsoever !!First the Project file:
program project1;
Secondly here is the Main Form's layout:
object frmMain: TfrmMain
Then the Main Unit
{
And finally i attached a little picture of my example at the bottom (red: removed typo from project file) Attach file: TestEventsPicture.png (4.51 KB)![]()
Posted on: 2011/7/31 12:34
Edited by magorium on 2011/7/31 15:33:30
Edited by magorium on 2011/7/31 15:34:17 |
|
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
Joined:
2011/7/27 8:24 From Sweden
Group:
Member Posts:
70
![]() |
nice...
That could be usable... Thanks for pointing that out... I think I will use it...
Posted on: 2011/7/31 13:37
|
|
|
_________________
http://aros-fpc.blogspot.com/ |
||
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Quote:
Thanks for pointing that out... No problemo. Just another component that can be used. Still figuring out what can and can't be used. it would be nice to create a list of some sort. Alas, too many things at once. BTW: how is your calculator coming along ? Any time soon we can admire it on aminet or aros archives ? Or do you just leave it on your blog ?
Posted on: 2011/7/31 14:38
|
|
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
Joined:
2011/7/27 8:24 From Sweden
Group:
Member Posts:
70
![]() |
well... it is just as everything else there, a test app...
I could post it on my blog, and let it be for the other "beginners" to "fix" the missing parts... I put it there when I have the time... tomorrow perhaps? missing stuff! Windows resize from app... normal <-> programmer mode Binary and logical op (aka it is a basic calc now) ? more ? weird stuff See video at blog...
Posted on: 2011/8/1 10:40
|
|
|
_________________
http://aros-fpc.blogspot.com/ |
||
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Hi, Anonymous_Coward
Quote: well... it is just as everything else there, a test app... Perhaps, but it is getting more advanced then default zunecalc calculator ![]() Quote: I could post it on my blog, and let it be for the other "beginners" to "fix" the missing parts... Please do not rush on my account. I just thought it can be a good idea to actually show to others that fpf + lcl can be used for developing tools that actually work. Unfortunately i have no time todo such things atm. Quote: missing stuff! Dunno, you tell me ![]() I take it you already tried to solve things ? Or is lack of time the main thing here ? Alas also some things are still missing in lcl... i know... Just in case. If you distribute the executable, do not forget to set optimalization and when compiled to use the strip command. hmz, come to think of it that probably deserves a complete chapter . How to optimize things etc...cu
Posted on: 2011/8/1 11:00
|
|
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
Joined:
2011/4/18 7:10 From Germany
Group:
Member Posts:
156
![]() |
Nice Work...
hmm yes of course, Components which do not need the GUI should work already. I'm thinking about a test suite for easier development, like FPCUnit.
Posted on: 2011/8/2 10:04
|
|
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Quote:
hmm yes of course, Components which do not need the GUI should work already. So if i take that literally then i read that i should be able to create a daemonapp ? ![]()
Posted on: 2011/8/6 19:45
|
|
Transfer
|
||
Chapter 3: Getting to know things |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Other suggested and interresting places:
- The Lazarus reference documentation - Lazarus (IDE) tutorial for beginners - ALB42's blog for his progress of FPC+LCL - HEA's blog for examples, pictures and his experiences Chapter 3: Getting to know things. This chapter is about: - another lcl component working out-of-the-box - a little hidden surprise - some usefull functions - conditional compiling This time just more interresting things to discover. So, what is it about this time ? Well, if you are like me when it comes to developing tools and utilities, chances are that there are a bunch of variables nicely tucked away somewhere. Mostly created in a hurry with some default values stored in them. Eventually there comes a point that some of those variables need to have some more influence on the program and need some sort of manipulation. I guess changing them visually should not be too much of a problem with using such a RAD tool like lazarus. But what to do with those changes ? How do you keep those changes permantly ? What to do with such things as e.g. the last chosen directory by the user ? Well, normally we write them to disk for a more permanent storage. So we will. But how todo that ? There are numerous paths that can be chosen. We can write things to disk the old fashioned filewrite way, or one could use a little more modern Filestream to do that. Or should we perhaps use an inifile ? Nah, almost every program these days uses XML for that sort of things. Do not ask me why, because i have absolutely no clue whatsoever. So with this chapter we will step into the twilight-zone called xml. So let's start: 1) Create a new application 2) Drop a TXMLConfig component on top of the form (system tab). 3) Drop a button on the form 4) add events Form.OnCreate, Form.OnDestroy and Button.OnClick 5) add some code, so that things look something like this:
Var
So, what does all this do ? Everytime the button is pressed a value of a counter is being increased. Then this value is being stored by using the XMLConfig component. If the form get's destroyed the contents of the XMLConfig component get's flushed onto disk (Actually the component does that on it's own already so the code in the .OnDestroy event could have been omitted, but it shows how to do it on demand). Now that all this have been done we only have to restore the last known value from disk when the form is created. The value of ButtonPressCounter is being stored and restored to disk in a xml file, so each time you start this application the application will present the last used value of ButtonPressCounter. Oh, before i forget: i use an older version of lazarus to develop and i noticed fpc for aros complaining something about unit XMLCfg and symbol TXMLConfig being deprecated. Just ignore like i did. As long as things work, they work ![]() Also my distribution of lazarus came with a more comprehensive example and can be found in the fcl-base examples directory. Perhaps you can take a look there to learn more. And for the ones who payed a little attention, this component has a small hidden surprise tucked away called DOM. Yes, a complete DOM-parser is at your disposal to use and abuse ![]() There are some other interresting functions tucked away in lazarus that makes use of the TXMLConfig component like the functions WriteComponentToXMLConfig and ReadComponentFromXMLConfig in the LazXMLForms unit (can be found in the converter directory from the lazarus sources). I had a nice little overall project prepared to show here this time. It used a more visual approach using the TXMLPropStorage component (if you want to experiment and try for yourself then perhaps taking a look here might be helpfull). But for some reason Murphy honoured me with a visit, so instead i made a little example about practical use of the TXMLConfig component. In that regard it turned out nicely imho and could serve as a starting-point to extend the editor that HEA is working on. The example below shows how one can read part of a .lpi file as well as how to use conditional compilation of code (i didn't think it is necessary to spend more time to it then this). This time no fancy pictures and as always use at your own risk and please do not confuse XMLcfg with Laz_XMLCfg unit as they are different. The Project File:
program ExampleCh3;
The Main Unit's Form
object frmMain: TfrmMain
The Main Unit Itself
{
Hopefully enjoy,
Posted on: 2011/8/6 19:59
|
|
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
Joined:
2011/7/27 8:24 From Sweden
Group:
Member Posts:
70
![]() |
Thanks!
Posted on: 2011/8/7 5:34
|
|
|
_________________
http://aros-fpc.blogspot.com/ |
||
Transfer
|
||
Re: Magical Empire of FPC and Lazarus #1 |
||
|---|---|---|
|
Joined:
2011/6/30 19:50 Group:
Member Posts:
1648
![]() |
Well, i thank you instead
I started out testing the component with .lpi file as a test to see if it worked. Then i moved on to other things when things went wrong (murphy). So i had to do things fast based upon my first .lpi attempt. And the idea for that came from your editor. So thank you, for the idea ![]()
Posted on: 2011/8/7 6:35
|
|
Transfer
|
||
You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.



TestEventsPicture.png (4.51 KB)
Transfer









