PlanetSquires Forums

Support Forums => PlanetSquires Software => Topic started by: philbar on May 27, 2022, 04:09:11 AM

Title: Using CLayout manager in a Designer project
Post by: philbar on May 27, 2022, 04:09:11 AM
As promised in the other thread: Many thanks to José for this gem that can save a lot of trouble.

The attached picture shows the window as designed with two text boxes, a frame and a list. In the main.bas program, don't forget:

#include "afx\clayout.inc"

And this is the form.inc code:

' frmMain form code file
''
''
''These constants are enumerated in afx/clayout.inc
'   AFX_ANCHOR_NONE =  0
'   AFX_ANCHOR_WIDTH
'   AFX_ANCHOR_RIGHT
'   AFX_ANCHOR_CENTER_HORZ
'   AFX_ANCHOR_HEIGHT
'   AFX_ANCHOR_HEIGHT_WIDTH
'   AFX_ANCHOR_HEIGHT_RIGHT
'   AFX_ANCHOR_BOTTOM
'   AFX_ANCHOR_BOTTOM_WIDTH
'   AFX_ANCHOR_BOTTOM_RIGHT
'   AFX_ANCHOR_CENTER_HORZ_BOTTOM
'   AFX_ANCHOR_CENTER_VERT
'   AFX_ANCHOR_CENTER_VERT_RIGHT
'   AFX_ANCHOR_CENTER

dim shared as clayout ptr playout         'A shared pointer so it can be seen in all functions
dim shared as boolean form_ready=false    'Prevents a hard-to-debug crash

Function frmMain_Load( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   dim as long ngrip=frmmain.getnextctrlid()
   
   playout = new clayout(frmmain.hwindow)    'assigns a layout manager to this window
   
   'This is optional. Adds the little gripper spot to the bottom right corner.
   frmmain.pwindow->addcontrol("SizeGrip", frmmain.hWindow, ngrip)
   
   'Decide how each control is to be sized
   playout->anchorcontrol(frmmain.text1.hwindow, afx_anchor_width)
   playout->anchorcontrol(frmmain.text2.hwindow, afx_anchor_height_width)
   playout->anchorcontrol(frmmain.frame1.hwindow, afx_anchor_height_right)
   playout->anchorcontrol(frmmain.list1.hwindow, afx_anchor_height_right)
   playout->anchorcontrol(ngrip, afx_anchor_bottom_right)
   Function = 0
End Function

''
''
Function frmMain_Resize( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   if form_ready then            'This prevents the crash.
      playout->adjustcontrols    'This does the resizing     
   end if
   Function = 0
End Function

''
''
Function frmMain_FormClosed( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   delete playout       'Wouldn't want a memory leak
   Function = 0
End Function

''
''
Function frmMain_AllEvents( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   'This part taken from José's example layout manager programs.
   'Who knew there was such a thing?
   'I could have just put a test in the RESIZE event.
   if e.Message = wm_getminmaxinfo then
      dim ptmmi as MINMAXINFO ptr = cast(minmaxinfo ptr, e.lParam )
      ptmmi->ptmintracksize.x = 300
      ptmmi->ptmintracksize.y = 180
   end if
   Function = 0 
End Function

''
''
Function frmMain_FormReady( ByRef sender As wfxForm, ByRef e As EventArgs ) As LRESULT
   'Sometimes a Resize event happens before Form_Load is done.
   'That causes a nasty crash.
   form_ready=true
   Function = 0
End Function


I think the comments explain what's going on. The minimum size of 300x180 is a nice touch taken from the examples. Check out the layout manager examples in the templates for more complex behavior. It's possible to put layouts inside layouts.
Title: Re: Using CLayout manager in a Designer project
Post by: Petrus Vorster on May 27, 2022, 09:46:32 AM
Thank you for the example mate, it is much appreciated.

I do get errors right of the bat.
The include of the clayout.inc is fine.

The assignment of the pointer throws an error:
dim shared as clayout ptr playout
Throws the error : ERROR 14: Expected identifier, found 'clayout'

I do see where this is going though.

But I think i am going to wait until Paul adds it to his designer.
That automation made it a breeze.

Thanks for the example.

Peter
Title: Re: Using CLayout manager in a Designer project
Post by: philbar on May 27, 2022, 10:57:58 AM
Hmm. Works for me. I do get that error, among many others, if I leave out the #include.
Title: Re: Using CLayout manager in a Designer project
Post by: SeaVipe on May 27, 2022, 02:43:27 PM
Thanks, Philbar! Worked first time, no errors.
Title: Re: Using CLayout manager in a Designer project
Post by: Petrus Vorster on May 27, 2022, 02:45:14 PM
Yes it works just fine if the moron that pasted the info removes a rem comma....
I can be a real idiot.

Dear Heavens.  :-[
Title: Re: Using CLayout manager in a Designer project
Post by: Petrus Vorster on May 27, 2022, 03:28:19 PM
Great stuff. It works perfectly when I change the form size, use the maximize button and so forth.

But when i start the form using formwindowstate.maximized then it completely messes up the controls.
In Powerbasic the Maximized form worked under the form_resize function.
It does not seem to be the case in Freebasic.

-Regards, Peter
Title: Re: Using CLayout manager in a Designer project
Post by: philbar on May 27, 2022, 03:39:56 PM
I've never tried it on a form that starts out maximized. Maybe if you start out regular and maximize it in Form_Load after the layout manager has initialized?
Title: Re: Using CLayout manager in a Designer project
Post by: Petrus Vorster on May 27, 2022, 03:46:05 PM
Hi

That works fine if i do that, I was just wondering why the Maximize startup of the form does not fire the form-resize event.
Unless the Maximize startup runs before some of the other events.

Regards, Peter
Title: Re: Using CLayout manager in a Designer project
Post by: SeaVipe on May 27, 2022, 03:51:37 PM
QuoteBut when i start the form using formwindowstate.maximized then it completely messes up the controls.
FormWindowState.Normal is the only state that appears to work without messing up the control sizes and positions.
Title: Re: Using CLayout manager in a Designer project
Post by: philbar on May 27, 2022, 04:06:54 PM
After some experimenting, I got the result I expected when I started out normal and put "frmmain.windowstate = maximized" at the end of the frmmain_FormReady event.
Title: Re: Using CLayout manager in a Designer project
Post by: Petrus Vorster on May 27, 2022, 05:39:17 PM
Yes, that works perfectly.

Thanks a million.