Lazarus

Free Pascal => Beginners => Topic started by: pmckone on September 26, 2020, 10:13:28 pm

Title: "Charles Petzold" Style Programming Book or Document?
Post by: pmckone on September 26, 2020, 10:13:28 pm
Can I get a recommendation for a book or document that explains how to write programs that use the component libraries WITHOUT using a form designer?  Every book that I look at starts out with dragging and dropping controls onto a form, and then augmenting code that is generated by a form designer.  I'm more comfortable writing all the code myself.  Thanks.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 26, 2020, 10:35:14 pm
I sympathise with your search for purity, but quite frankly I think your quest is ill-advised... Quixotic, even.

30 years ago I was selling programming libraries, and to be brutally frank one of the most significant developments /ever/ as far as application development is concerned was the introduction of form/screen designers which allowed a programmer to design his layout graphically.

As precursors, there were various "4GLs" many of which had a modicum of UI design capability. Regrettably, character-based free software hasn't really progressed beyond this point.

There was Visual BASIC for Windows, and ditto for DOS, and Visual C++. There was Delphi, Clarion and Java. There was Intranet Builder from Borland, and Visual Javascript from Netscape.

Those were the progenitors. Hardcoding stuff by hand is Neanderthal in comparison.

MarkMLl
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: Fred vS on September 26, 2020, 10:45:47 pm
Can I get a recommendation for a book or document that explains how to write programs that use the component libraries WITHOUT using a form designer?  Every book that I look at starts out with dragging and dropping controls onto a form, and then augmenting code that is generated by a form designer.  I'm more comfortable writing all the code myself.  Thanks.

fpGUI is exactly what you want.
https://github.com/graemeg/fpGUI/tree/maint

You may use his designer but the one of the feature of fpGUI is that all is in one file, no form file.
So, perfectly easy to do all by code.

Fre;D
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: lainz on September 26, 2020, 10:48:32 pm
You can use all bitmaps with an opengl library like sdl as well
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: winni on September 26, 2020, 11:09:53 pm
Can I get a recommendation for a book or document that explains how to write programs that use the component libraries WITHOUT using a form designer?  Every book that I look at starts out with dragging and dropping controls onto a form, and then augmenting code that is generated by a form designer.  I'm more comfortable writing all the code myself.  Thanks.

Hi!

I would recommand not a book but "Turbo Pascal for Windows" from Borland.
They published it 1 year before Delphi - mid 90s.

You reserve some memory,  you create a window pointer, you create a GDI connection, you do this and that - and after one hour you got a window with a Close-Button. Horrible!

Read the post of MarMLI again!

Winni
 
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 26, 2020, 11:31:00 pm
You reserve some memory,  you create a window pointer, you create a GDI connection, you do this and that - and after one hour you got a window with a Close-Button. Horrible!

In fairness, much of the five or six pages of code that was needed for an absolutely minimal program in those days was the message loop... which is distinct from the action of laying out the window (AKA form).

I see no reason why it shouldn't be possible to construct a GUI by hand while letting the message loop remain opaque... although I suspect that that approach would not be acceptable to the OP. See Fred's comment for a good starting point.

I am deeply disturbed by the complexity of modern hardware and software, which I believe soars to an incredible height while being founded on feet of clay. But at the same time I am reluctant to throw away accumulated expertise without very good reason.

MarkMLl
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: gour on September 27, 2020, 09:17:28 am

fpGUI is exactly what you want.
https://github.com/graemeg/fpGUI/tree/maint

You may use his designer but the one of the feature of fpGUI is that all is in one file, no form file.
So, perfectly easy to do all by code.

I was considering to use it in the past, but now when resuming my interest in FreePascal/Lazarus, it looks that fpGUI is not so much in active development - at least, long time without any new release?
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: PascalDragon on September 27, 2020, 10:10:33 am
I see no reason why it shouldn't be possible to construct a GUI by hand while letting the message loop remain opaque... although I suspect that that approach would not be acceptable to the OP. See Fred's comment for a good starting point.

Also it's perfectly possible to create LCL applications in code if one wants. It's just not the standard and most-walked route. :)
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: 440bx on September 27, 2020, 04:49:24 pm
Can I get a recommendation for a book or document that explains how to write programs that use the component libraries WITHOUT using a form designer?  Every book that I look at starts out with dragging and dropping controls onto a form, and then augmenting code that is generated by a form designer.  I'm more comfortable writing all the code myself.  Thanks.
It's hard to recommend a book these days for what you want.

For straight Windows API programming, Petzold's classic book is likely the practical way to go about it but, it requires you have some knowledge of C.  Augment Petzold's book with the Advanced Windows book from Jeffrey Richter and you'll reach a reasonable level of competence in a reasonable amount of time.

For Pascal and pure API, the only book I can think of is Dave Jewel's "Polishing Windows", it's an excellent book but, it is really old.  The examples cater to Windows 95 (or 3.1, I don't remember).  If you use that book, you'll have to tailor/port/adapt to the current API which is not really difficult but, it means that in many cases you don't have examples that work directly as presented.  One feature of Dave Jewel's book that is worth mentioning is that he strongly emphasizes program design and implementation _without_ using global variable which is very desirable as an implementation methodology and as a programming skill.

The other thing that must be mentioned is that, the current "methodology" of using forms and OOP gives the programmer close to instant gratification, which is quite appealing.  Programming pure API doesn't initially come close to instant gratification but, as you write more and more programs, you create a pool of code you can reuse and customize for a particular objective.  Initially, there is a lot more effort involved but, in the long run, you end up with much better programs and, the total time/effort spent while being somewhat greater with pure API, produces noticeably better results. 

If you don't mind the initial greater effort, I encourage you to program to the API.  I do.

HTH.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 27, 2020, 05:10:10 pm
For Pascal and pure API, the only book I can think of is Dave Jewel's "Polishing Windows", it's an excellent book but, it is really old.  The examples cater to Windows 95 (or 3.1, I don't remember).

In certain quarters, Dave is notorious for once announcing that he enjoys taking a pee under the covers. Verity never let him forget that one.

I'm pretty sure that most of his output was related to '95 and later. I'd caution that my understanding is that older APIs (i.e. Win-16) were pretty arbitrary as to which segments they used for parameters etc., so books of that era need to be approached with great caution.

MarkMLl

Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: Fred vS on September 27, 2020, 05:55:53 pm

fpGUI is exactly what you want.
https://github.com/graemeg/fpGUI/tree/maint

You may use his designer but the one of the feature of fpGUI is that all is in one file, no form file.
So, perfectly easy to do all by code.

I was considering to use it in the past, but now when resuming my interest in FreePascal/Lazarus, it looks that fpGUI is not so much in active development - at least, long time without any new release?

> but now when resuming my interest in FreePascal/Lazarus,

fpGUI uses Free Pascal, of course.

> it looks that fpGUI is not so much in active development - at least, long time without any new release

fpGUI is still in development, even more, there is now a "Wayland" branch.

But yes, there are less new commits (last one is from July), maybe because no more bugs?

IMHO, projects like fpGUI or MSEgui are much more active in development than GTK2.

Fre;D


Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 27, 2020, 06:12:52 pm
But yes, there are less new commits (last one is from July), maybe because no more bugs?

You mean it's bugrid? I'll get me coat :-)

MarkMLl
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: 440bx on September 27, 2020, 06:13:39 pm
<snip> so books of that era need to be approached with great caution.
I agree.  The only books I've seen that are up to date to use the Windows API all cater to C programmers (and possibly C++) not Pascal (probably because there is very limited interest in programming to the API in that segment.)

I would say that anyone wanting to learn the Windows API has to either learn C or be conversant/familiar with C.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: Fred vS on September 27, 2020, 06:20:50 pm
But yes, there are less new commits (last one is from July), maybe because no more bugs?

You mean it's bugrid? I'll get me coat :-)

MarkMLl

I said "maybe" ...

;-)

(By the way, if you find a bug in MSEgui, I offer you a new jacket.)

Fre;D

Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: cdavidson on September 27, 2020, 07:01:02 pm
For a good Delphi sample of straight Win32 programming, Chapter 23 of Ivan Hladni's 'Inside Delphi 2006' has a good basic coverage.  You can pick up a copy pretty cheap.  I have used that code as a base for a lot of low level Windows programming.  Once you see the pascal code you will see that low level in Free Pascal is not much different than the C code.  When I say Win32, the code will still work on Win64 either as a 32 or 64 compile.

Actually, I would say that 95+% of that book applies perfectly to Free Pascal and to Lazarus.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: marcov on September 27, 2020, 09:36:05 pm
An example program is available with FPC install in demo/win32/edit.pp

"Tomes of Delphi Win32" api and win32 graphics api might also be good books
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: pmckone on September 28, 2020, 12:34:39 am
I didn't make the original question clear enough.  I actually already know how to write old-fashioned Win32 programs in C.  I wrote Algol at Burroughs around 1980, and Pascal on a Convergent Technologies machine in 1982, and converting 'C' code to Pascal isn't too hard.  What I'm hoping to find is an explanation of how to use the component libraries, but without using a form designer.  Many years ago I found a book "Introduction to MFC Programming with Visual C++" written by Richard M. Jones.  He explains how to write what he calls "Handcrafted MFC Programs".  I was hoping to find a similar book about writing Delphi programs by hand.  It's beginning to look like no such book exists.  That's okay.  It wouldn't be the first time I tried to find something that doesn't exist!
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 28, 2020, 08:51:28 am
I for one understand perfectly well what you're saying. And I'd point out that when LBS transitioned from using a Medium System to a B7800 circa 1981 they hired about 25 coders to design TD830J and Modular Terminal screens for them... work that these days is almost entirely automated.

MarkMLl
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: marcov on September 28, 2020, 09:30:59 am
Many years ago I found a book "Introduction to MFC Programming with Visual C++" written by Richard M. Jones.  He explains how to write what he calls "Handcrafted MFC Programs".  I was hoping to find a similar book about writing Delphi programs by hand.  It's beginning to look like no such book exists.  That's okay.  It wouldn't be the first time I tried to find something that doesn't exist!

I don't think there is a book, but it is quite common to do at least parts (like whole forms) by hand. The designer operates on textfiles (lfm/dfm and .pas/pp), and just watching what changes it makes is often enough.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: lainz on September 29, 2020, 01:57:45 am
Well HTML, JavaScript and CSS are all by hand, there are editors but you can not use them if you like  :P

So what's the point =)
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 29, 2020, 09:02:22 am
Well HTML, JavaScript and CSS are all by hand, there are editors but you can not use them if you like  :P

Which is why I mentioned Borland Intranet Builder (?) and Netscape Visual Javascript in my list earlier. The distinguishing feature is that they know how to hook Javascript onto DOM events, even if they don't provide as much coding and debugging support as Delphi etc.

The fact that an influential group of people refuses to use available tools doesn't make their refusal praiseworthy.

MarkMLl
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: valdir.marcos on September 29, 2020, 11:20:46 am
Topic: "Charles Petzold" Style Programming Book or Document?

Can I get a recommendation for a book or document that explains how to write programs that use the component libraries WITHOUT using a form designer?  Every book that I look at starts out with dragging and dropping controls onto a form, and then augmenting code that is generated by a form designer.  I'm more comfortable writing all the code myself.  Thanks.
Just curiosity.
Which books specifically are you referring to?
http://www.computerofthetides.com/bio.html
https://en.wikipedia.org/wiki/Charles_Petzold
https://www.charlespetzold.com/
https://www.amazon.com/Charles-Petzold/e/B000APETXO%3Fref=dbs_a_mng_rwt_scns_share
https://www.amazon.com/Books-Charles-Petzold/s?rh=n%3A283155%2Cp_27%3ACharles+Petzold
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: valdir.marcos on September 29, 2020, 12:44:08 pm
I didn't make the original question clear enough.  I actually already know how to write old-fashioned Win32 programs in C.  I wrote Algol at Burroughs around 1980, and Pascal on a Convergent Technologies machine in 1982, and converting 'C' code to Pascal isn't too hard.  What I'm hoping to find is an explanation of how to use the component libraries, but without using a form designer.  Many years ago I found a book "Introduction to MFC Programming with Visual C++" written by Richard M. Jones.  He explains how to write what he calls "Handcrafted MFC Programs".  I was hoping to find a similar book about writing Delphi programs by hand.  It's beginning to look like no such book exists.  That's okay.  It wouldn't be the first time I tried to find something that doesn't exist!
Like marcov said, I also don't think there is such a book, but the internet is full of Delphi examples that would work on Lazarus:
https://stackoverflow.com/questions/16112924/how-to-create-a-form-programmatically-with-a-couple-components-on-it-in-delphi
https://stackoverflow.com/questions/43450671/how-to-create-a-new-form-dynamically-and-inherit-the-form-from-an-existing-form
http://www.festra.com/wwwboard/messages/12948.html


Introduction to MFC Programming with Visual C++,  Richard M. Jones, 2000:
https://catalyst.library.jhu.edu/catalog/bib_2150987
https://archive.org/details/introductiontomf00rich/page/n5/mode/2up
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: pmckone on September 29, 2020, 06:52:16 pm
The Petzold books that I own are "Programming Windows" (versions for 3.0, 95, and 98), "Programming Windows with C#" and the update "Programming Windows Forms" and "Applications = Code + Markup", which is about Windows Presentation Foundation.  I stopped reading this last book when I realized the the "SpinBox" control was not included in WPF.  I don't think Petzold used a form designer (Or "AppWizard") to produce any of examples in any of these books except possibly the last one, which I didn't finish. 

In my case, I am constructing a chessboard using an 8x8 array of Rich Text boxes, all sharing the same event handler.  I have already written this program in C#, but now I need it in Pascal, to merge with the old Turbo Games chess program.  I think it would take me longer to learn how create a two-dimensional array of text boxes in the form designer than it will take to write the code directly.  Why am I using Rich Text boxes to make the squares on the board?  Because is reduces the amount of keyboard handling code that I have to write.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: Handoko on September 29, 2020, 07:41:37 pm
What I'm hoping to find is an explanation of how to use the component libraries, but without using a form designer.

I wonder why you avoid using form designer. It is easy to learn and you can develop programs in much less time.

You mentioned you're writing games, glad to meet someone who likes writing games here. Maybe you're interested to learn writing a simple snake game I wrote some time ago:
https://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.html#msg258381

There you can learn reading user input, using TTimer as a game loop, using buffer for better keyboard input, how to reduce flickering, and many basic things for writing games. As you're already able to write games in C#, I'm sure you already knew about them but you might be don't know how to do them in Pascal. Unfortunately that thread is very long and messy.

If you're new in Pascal/Lazarus, I recommend you this page:
https://wiki.freepascal.org/Portal:HowTo_Demos

You can find many short demos especially in the 'Graphics' and 'User Interface' categories, which can help you learn GUI programming easier.
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: MarkMLl on September 29, 2020, 08:39:18 pm
I don't think Petzold used a form designer (Or "AppWizard") to produce any of examples in any of these books except possibly the last one, which I didn't finish.

For exactly the same reason that cavemen didn't use matches to light their fire: such things did not exist.

Petzold's reputation was founded on his work with DOS and Windows 3.x To a very large extent hos subsequent books hewed carefully to the same format, with the intention that they helped his existing clientele adapt to the new technology. He left other people to write about development tools.

Quote
In my case, I am constructing a chessboard using an 8x8 array of Rich Text boxes, all sharing the same event handler.  I have already written this program in C#, but now I need it in Pascal, to merge with the old Turbo Games chess program.  I think it would take me longer to learn how create a two-dimensional array of text boxes in the form designer than it will take to write the code directly.  Why am I using Rich Text boxes to make the squares on the board?  Because is reduces the amount of keyboard handling code that I have to write.

Get a form, slap an 8x8 array of panels onto it. Or use a TDrawGrid. Or just about anything. Because the work you make for yourself and the grief you cause the community insisting on doing everything by hand will far exceed the work and grief associated with helping you out with keyboard handling.

MarkMLl

(Who's BT, DT, spent far too much time reinventing the wheel and bashing his head against tools when he's tried to abuse them, and wishes that less experienced users would just now and then /listen/ to what people were trying to tell them.)
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: WayneSherman on January 17, 2021, 03:00:35 am
What I'm hoping to find is an explanation of how to use the component libraries, but without using a form designer.

Everything that can be done via the Lazarus form designer can be done in code as well.

Some references:
https://wiki.freepascal.org/Form_Tutorial#Creating_a_new_form_dynamically
https://forum.lazarus.freepascal.org/index.php?topic=37530.0

When designing a form visually, units are automatically added to the uses declaration, fields and methods are automatically add to a TForm class and properties are stored in a corresponding .lfm file.  The .lfm file is linked into the program as a resource (via the line {$R *.lfm}).  An .lfm file looks like this:
Code: Pascal  [Select][+][-]
  1. object Form1: TForm1
  2.   Left = 599
  3.   Height = 172
  4.   Top = 495
  5.   Width = 180
  6.   Caption = 'Form1'
  7.   ClientHeight = 172
  8.   ClientWidth = 180
  9.   LCLVersion = '2.0.8.0'
  10.   object Button1: TButton
  11.     Left = 32
  12.     Height = 25
  13.     Top = 64
  14.     Width = 75
  15.     Caption = 'Button1'
  16.     TabOrder = 0
  17.   end
  18.   object Label1: TLabel
  19.     Left = 40
  20.     Height = 17
  21.     Top = 40
  22.     Width = 37
  23.     Caption = 'Label1'
  24.     ParentColor = False
  25.   end
  26. end
  27.  

To create the form and components in code, and without using an .lfm file, a project1.lpr file (originally generated by Lazarus) can be modified like this:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Interfaces, // this includes the LCL widgetset
  10.   Forms, Unit1
  11.   { you can add units after this };
  12.  
  13. //{$R *.res}  //we don't need this since we are creating the form and associated controls in code
  14.  
  15. begin
  16.   RequireDerivedFormResource:=False;  //changed this to false since we are not linking/loading an .lfm resource
  17.   Application.Scaled:=True;
  18.   Application.Initialize;
  19.   unit1.CreateAndShowMyMainForm;  //this calls our code to create and show the main form
  20. //  Application.CreateForm(TForm1, Form1);  //we don't need this, since we are creating the form and setting the properties in code
  21.   Application.Run;
  22. end.
  23.  

unit1.pas which creates the form might look like this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Label1: TLabel;
  17.     procedure Button1Click(Sender: TObject);
  18.   end;
  19.  
  20. procedure CreateAndShowMyMainForm;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. //{$R *.lfm}  //we don't need this since we are creating the form in code
  28.  
  29. procedure CreateAndShowMyMainForm;
  30. begin
  31.   Form1 := TForm1.Create(Application);  //In this example, Application is the "owner" of the control and will free it for us
  32.   with Form1 do begin
  33.     Left := 603;
  34.     Height := 172;
  35.     Top := 168;
  36.     Width := 180;
  37.     Caption := 'Form1';
  38.     ClientHeight := 172;
  39.     ClientWidth := 180;
  40.   end;
  41.  
  42.   Form1.Button1 := TButton.Create(Form1);
  43.   with Form1.Button1 do begin
  44.     Left := 32;
  45.     Height := 25;
  46.     Top := 64;
  47.     Width := 75;
  48.     Caption := 'Button1';
  49.     OnClick := @Form1.Button1Click;
  50.     TabOrder := 0;
  51.     Parent := Form1;
  52.   end;
  53.  
  54.   Form1.Label1 := TLabel.Create(Form1);
  55.   with Form1.Label1 do begin
  56.     Left := 40;
  57.     Height := 17;
  58.     Top := 40;
  59.     Width := 37;
  60.     Caption := 'Label1';
  61.     ParentColor := False;
  62.     Parent := Form1;
  63.   end;
  64.  
  65.   Form1.Show;
  66. end;
  67.  
  68. { TForm1 }
  69.  
  70. procedure TForm1.Button1Click(Sender: TObject);
  71. begin
  72.   Label1.Caption := 'Hello World';
  73. end;
  74.  
  75. end.
  76.  

And if you want to control the freeing of your form, then don't give it an owner when you create it:
  Form1 := TForm1.Create(nil);

As a side point, the Delphi gexperts "Components to Code" IDE expert converts gui components to pascal code:
http://www.gexperts.org/tour/index.html?components_to_code.html
(I don't know of a Lazarus implementation of "Components to Code", but it would not be hard to port the one from gexperts)
Title: Re: "Charles Petzold" Style Programming Book or Document?
Post by: Blade on January 18, 2021, 03:00:28 am
Everything that can be done via the Lazarus form designer can be done in code as well.

That was a well done example.  Also, I think some people (maybe pmckone also) like dynamic alternatives because .lfm data shows up in tools like Resource Hacker.  So if for nothing else, dynamically generated forms can provide some additional security, for those that think it necessary.
TinyPortal © 2005-2018