Recent

Author Topic: With parent use, how to dynamically create a Tform in another Tform with Linux?  (Read 11081 times)

GillesH

  • Jr. Member
  • **
  • Posts: 55
Hello,

the example "Creating a new form dynamically" of http://wiki.freepascal.org/Form_Tutorial doesn't work if I write :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyForm: TForm;
  4. begin
  5.   MyForm:=TForm.Create(nil);            
  6.   MyForm.SetBounds(100, 100, 220, 150);  
  7.   MyForm.Caption:='My dynamic created form';
  8.  
  9.   MyForm.Parent := Form1; //<--- !
  10.  
  11.   MyForm.ShowModal;                    
  12.  
  13.   FreeAndNil(MyForm);    
  14. end;
OK with Windows but not with Ubuntu 16.4 - Lazarus 1.6.4 - FPC 3.0.2 - x86_64-linux.gtk2.
Regards. Gilles
« Last Edit: April 11, 2017, 10:46:49 am by GillesH »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: How to dynamically create a Tform in another Tform ?
« Reply #1 on: April 10, 2017, 03:54:38 pm »
What doesn't work? Please describe the problem.

And why are you using a parent for the MyForm if you are going to use ShowModal.
ShowModal shows the form over all other forms disabling them.
So you could just do

Code: Pascal  [Select][+][-]
  1. var
  2.   MyForm: TForm;
  3. begin
  4.   MyForm:=TForm.Create(nil);            
  5.   try
  6.     MyForm.SetBounds(Self.Left + 100, Self.Top + 100, Self.Left + 220, Self.Top + 150);  
  7.     MyForm.Caption:='My dynamic created form';
  8.     MyForm.ShowModal;                    
  9.   finally
  10.     FreeAndNil(MyForm);  
  11.   end;

No need to make Form1 parent for the MyForm.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: How to dynamically create a Tform in another Tform ?
« Reply #2 on: April 10, 2017, 04:02:26 pm »
Hello,

the example "Creating a new form dynamically" of http://wiki.freepascal.org/Form_Tutorial doesn't work if I write :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   MyForm: TForm;
  4. begin
  5.   MyForm:=TForm.Create(nil);            
  6.   MyForm.SetBounds(100, 100, 220, 150);  
  7.   MyForm.Caption:='My dynamic created form';
  8.  
  9.   MyForm.Parent := Form1; //<--- !
  10.  
  11.   MyForm.ShowModal;                    
  12.  
  13.   FreeAndNil(MyForm);    
  14. end;
OK with Windows but not with Ubuntu 16.4 - Lazarus 1.6.4 - FPC 3.0.2 - x86_64-linux.gtk2.
Regards. Gilles
Form1 is a variable might or might not point to the form your code executes from. Never use the global variables especially in a dynamic environment where more than one instances of a form can be created.
In your case change Form1 to self. Other than that  you have two major mistakes.
1) you forget about the forms placement on the screen which is used as form1 client coordinates moving the form probably outside the form1'a visible window.
2) once a form have a parent can not be "shown" it can only be made visible and the parent is "shown".

And as a last precaution make sure that myform has no borders or caption bar to avoid some nusty surprises down the road.

GillesH

  • Jr. Member
  • **
  • Posts: 55
Re: How to dynamically create a Tform in another Tform ?
« Reply #3 on: April 10, 2017, 04:13:24 pm »
Sorry,
i don't understand. I use the example of the wiki to highlight  the problem of parent.

Here are excerpts from my code
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  var
  3.   F01, F02, F03, F04, F05, F06, F07, F08, F09, F10: TForm;
  4.   F11, F12, F13, F14, F15, F16, F17, F18, F19, F20: TForm;
  5.   F21, F22, F23, F24: TForm;
  6.   nCol, nRow, nWidth, nHeight, iCol, iRow : Integer;
  7. begin
  8.   showmessage(IntToStr(Form1.ClientWidth)+'*'+IntToStr(Form1.ClientHeight)+'|'+
  9.   IntToStr(Form1.Width)+'*'+IntToStr(Form1.Height));
  10.   F01:= TForm.Create(Application);
  11.   F01.Parent := Self;
  12.   F02:= TForm.Create(Application);
  13.   F02.Parent := Self;
  14.   F03:= TForm.Create(Application);
  15.   F03.Parent := Self;
  16.   F04:= TForm.Create(Application);
  17.   F04.Parent := Self;  
  18.  [...]
  19.  with F01 do  begin
  20.     SetBounds((icol-1)*(nwidth+16), (iRow -1) * (nheight+38), nwidth, nHeight);
  21.    Show;
  22.   end;
  23.  [...]
Please look at attachmnt [Windows result]

Avantage of Parent : All Fxx are moving with Form1. So i can remove the Parent with Ubuntu. Fxx appears. (Form1.Left+...) But if Form1 moves, you must add code to move Fxx.
Thanks. Gilles
« Last Edit: April 10, 2017, 04:22:03 pm by GillesH »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: How to dynamically create a Tform in another Tform ?
« Reply #4 on: April 10, 2017, 04:18:08 pm »
i don't understand. I use the example of the wiki to highlight  the problem of parent.
Ok, now I understand.

But you STILL haven't said what the problem under Ubuntu is.

(just saying Windows is OK but Ubuntu not isn't really a helpful description)

GillesH

  • Jr. Member
  • **
  • Posts: 55
Re: How to dynamically create a Tform in another Tform ?
« Reply #5 on: April 10, 2017, 04:29:13 pm »
Oups ! Sorry but my english is poor.
I install a Kubuntu on my Virtual Box to test the same code.
« Last Edit: April 10, 2017, 04:46:54 pm by GillesH »

GillesH

  • Jr. Member
  • **
  • Posts: 55
Re: How to dynamically create a Tform in another Tform ?
« Reply #6 on: April 11, 2017, 10:44:58 am »
Hello,

Test with Kubuntu 16.04 [64] -Lazarus 1.6.4 [2017-02-26]-FPC 3.0.2 [64 bits].
Code :
Code: Pascal  [Select][+][-]
  1. rocedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   F01: TForm;
  4.   F02: TF02;
  5. begin
  6.  
  7.   F01:= TForm.Create(Application);
  8.  
  9.  //LCLIntf.SetParent(F01.Handle, self.Handle);
  10.  
  11.   with F01 do  begin
  12.     Color:= clRed;
  13.     SetBounds(0, 30, 300, 200);
  14.     Parent := Self;
  15.     Show;
  16.   end;
  17.  
  18.   Application.CreateForm(TF02, F02);
  19.   with F02 do  begin
  20.     Color:= clGreen;
  21.     SetBounds(340, 30, 300, 200);
  22.     Parent := Self;
  23.     Show;
  24.   end;
  25.  
  26. end;
  27.  
Result in attachments :  better but not that !  BorderIcons, BorderStyle !  F01, F02 : are they Popups ?
regards. Gilles

rvk

  • Hero Member
  • *****
  • Posts: 6163
Looks fine to me.

You STILL haven't explained what the problem is.

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
Create some graphics that you would expect, plz. I can't see what you mean?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

GillesH

  • Jr. Member
  • **
  • Posts: 55
Yes...
Impossible to display TitleBar, Icon System even using Caption, BorderStyle, BorderIcon. F02 has the same properties as TForm1 (default properties). And the display of the 2 forms (Form1 and F02) is very different. F01 and F02 look like Popups.

If you remove Parent := self, F01 and F02 appear with the expected design.
« Last Edit: April 11, 2017, 11:25:23 am by GillesH »

balazsszekely

  • Guest
This is the limitation of the window manager itself not lazarus. If I understand you correctly you try to create a MDI like application?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Impossible to display TitleBar, Icon System even using Caption, BorderStyle, BorderIcon. F02 has the same properties as TForm1 (default properties).
Ah, finally an explanation.

Yes, the lack of caption, icons and border is intended behavior in Lazarus under gtk2.

When Parent <> nil for a TForm, under gtk2, a gtk_hbox is created (without caption, icons and border).
When Parent = nil, a gtk_window (with all its properties) is created.

GillesH

  • Jr. Member
  • **
  • Posts: 55
OK. I understand.
  • It's related to how gtk2 works.
  • The behavior of Lazarus windows is very different in this case of Lazarus Linux
Much differentiation in perspective.
Thanks for the help.
Regards. Gilles
« Last Edit: April 11, 2017, 12:13:47 pm by GillesH »

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
1. It is not related to Lazarus but to GTK2 itself...... Didn't you read... Try C++, same effect....
2. Lazarus obeys the principles of its underlying widget sets, in your case GTK2

Don't turn the world upside down. This is a GTK2 issue. Not a Lazarus one....

Naive programmers coming from Windows, I suppose, again.
Read first. Ask later.
« Last Edit: April 11, 2017, 12:17:54 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

GillesH

  • Jr. Member
  • **
  • Posts: 55
Yes, that's what I wrote at the beginning.  I'am not going to start by thwarting you. So I corrected  :). But I have proof to contrary. Look at the attachment : (Qt 5.8 example) on the same VirtualBox session. Lazarus has no MDI. This clearly complicates my project... and with SDIi you have a curious choice :
  - parent without caption, icons and border
  - caption, icons and border without parent


Naive programmers coming from Windows, I suppose, again.
No from Qt on WIndows, Linux, os X, Androïd ond iOS. I was using Lazarus/delphi 10 years ago. I make a little detour by Lazarus today... friendly,  without aggressiveness. You see, you think it is not feasible, I do not qualify this approach as naive : Lazarus doest not allow it easily. It will be a little more complicated. That's all.
Regards. Gilles

« Last Edit: April 11, 2017, 01:05:20 pm by GillesH »

 

TinyPortal © 2005-2018